// Copyright (2019-2024) Paul Scherrer Institute #include #include #include #include "HLSDevice.h" #include "hls_jfjoch.h" #include "datamover_model.h" #include "../../jungfrau/sls_packet.h" #include "../../common/Logger.h" #include "../../common/JFJochException.h" struct HLSDeviceImpl { AXI_STREAM din_eth; AXI_STREAM din_frame_generator; AXI_STREAM dout_eth; constexpr static const size_t hbm_if_count = 32; constexpr static const size_t hbm_if_size = 32 * 1024 * 1024LU; ap_uint<2> data_source = STREAM_MERGE_SRC_NETWORK; std::vector> hbm; hls::stream > work_request_stream; hls::stream > completion_stream; Datamover<512> datamover_in; Datamover<512> datamover_out; Datamover<256> datamover_in_hbm_0; Datamover<256> datamover_in_hbm_1; Datamover<256, 16> datamover_out_hbm_0; Datamover<256, 16> datamover_out_hbm_1; ap_uint<1> run_data_collection; ap_uint<1> cancel_data_collection; volatile ap_uint<1> host_writer_idle; HLSDeviceImpl() : hbm(hbm_if_size / 32 * hbm_if_count), datamover_in(Direction::Input, nullptr), datamover_out(Direction::Output, nullptr), datamover_out_hbm_0(Direction::Output, (char *) hbm.data()), datamover_out_hbm_1(Direction::Output, (char *) hbm.data()), datamover_in_hbm_0(Direction::Input, (char *) hbm.data()), datamover_in_hbm_1(Direction::Input, (char *) hbm.data()) {} }; uint16_t checksum(const uint16_t *addr, size_t count) { /* Compute Internet Checksum for "count" bytes * beginning at location "addr". */ long sum = 0; for (int i = 0; i < count / 2; i++) sum += addr[i]; /* Add left-over byte, if any */ if (count % 2 == 1) sum += ((uint8_t *) addr)[count / 2]; /* Fold 32-bit sum to 16 bits */ while (sum>>16) sum = (sum & 0xffff) + (sum >> 16); return ~sum; } HLSDevice::HLSDevice(std::vector &buffer_device) : idle(true), dma_address_table(65536) { impl_ = std::make_unique(); auto in_mem_location32 = (uint32_t *) dma_address_table.data(); for (int i = 0; i < buffer_device.size(); i++) { in_mem_location32[2 * i ] = ((uint64_t) buffer_device[i]) & UINT32_MAX; in_mem_location32[2 * i + 1] = ((uint64_t) buffer_device[i]) >> 32; } } HLSDevice::~HLSDevice() { if (action_thread.joinable()) action_thread.join(); impl_.reset(); } void HLSDevice::CreateFinalPacket(const DiffractionExperiment& experiment) { CreateJFPacket(experiment, UINT64_MAX, 0, 0, nullptr, false); } void HLSDevice::SendPacket(char *buffer, int len, uint8_t user) { auto obuff = (ap_uint<512> *)buffer; for (int i = 0; i < (len + 63) / 64; i++) { packet_512_t packet_in; if (i == (len + 63) / 64 - 1) packet_in.last = 1; else packet_in.last = 0; packet_in.keep = 0xFFFFFFFFFFFFFFFF; packet_in.user = user; packet_in.data = obuff[i]; impl_->din_eth.write(packet_in); } } void HLSDevice::CreateJFPacket(const DiffractionExperiment& experiment, uint64_t frame_number, uint32_t eth_packet, uint32_t module_number, const uint16_t *data, int8_t adjust_axis, uint8_t user) { char buff[256*64]; memset(buff, 0, 256*64); auto packet = (jf_raw_packet *)buff; packet->ether_type = htons(0x0800); packet->sour_mac[0] = 0x00; // module 0 uint64_t tmp_mac = mac_addr; for (int i = 0; i < 6; i++) packet->dest_mac[i] = (tmp_mac >> (8*i)) % 256; uint32_t half_module = 2 * module_number | ((eth_packet >= 64) ? 1 : 0); packet->ipv4_header_h = htons(0x4500); // Big endian in IP header! packet->ipv4_header_total_length = htons(8268); // Big endian in IP header! packet->ipv4_header_dest_ip = ipv4_addr; packet->ipv4_header_sour_ip = experiment.GetSrcIPv4Address(0, half_module); packet->ipv4_header_ttl_protocol = htons(0x0011); packet->ipv4_header_checksum = checksum( (uint16_t *) &packet->ipv4_header_h, 20); // checksum is already in network order packet->udp_dest_port = htons(234); // port doesn't matter packet->udp_sour_port = htons(0xDFAC); packet->udp_length = htons(8248); // JF headers are little endian packet->jf.detectortype = SLS_DETECTOR_TYPE_JUNGFRAU; packet->jf.timestamp = 0xABCDEF0000FEDCBAL; packet->jf.bunchid = 0x1234567898765431L; packet->jf.row = half_module; packet->jf.column = 0; packet->jf.framenum = frame_number; packet->jf.packetnum = eth_packet % 64; if (data != nullptr) { for (int i = 0; i < 4096; i++) packet->jf.data[i] = data[i]; } packet->udp_checksum = htons(checksum( (uint16_t *) (buff+42), 8192+48)); SendPacket(buff, (130+adjust_axis)*64, user); } void HLSDevice::CreateJFPackets(const DiffractionExperiment& experiment, uint64_t frame_number_0, uint64_t frames, uint32_t module_number, const uint16_t *data) { for (uint64_t i = 0; i < frames; i++) { for (int j = 0; j < 128; j++) CreateJFPacket(experiment, frame_number_0 + i, j, module_number, data + (i * 128 + j) * 4096, 0, 0); } } void HLSDevice::CreateEIGERPacket(const DiffractionExperiment &experiment, uint64_t frame_number, uint32_t eth_packet, uint32_t module_number, uint32_t col, uint32_t row, const uint16_t *data) { char buff[256*64]; memset(buff, 0, 256*64); auto packet = (eiger_raw_packet *)buff; packet->ether_type = htons(0x0800); packet->sour_mac[0] = 0x00; // module 0 uint64_t tmp_mac = mac_addr; for (int i = 0; i < 6; i++) packet->dest_mac[i] = (tmp_mac >> (8*i)) % 256; packet->ipv4_header_h = htons(0x4500); // Big endian in IP header! packet->ipv4_header_total_length = htons(4172); // Big endian in IP header! packet->ipv4_header_dest_ip = ipv4_addr; packet->ipv4_header_sour_ip = experiment.GetSrcIPv4Address(0, 0); packet->ipv4_header_ttl_protocol = htons(0x0011); packet->ipv4_header_checksum = checksum( (uint16_t *) &packet->ipv4_header_h, 20); // checksum is already in network order packet->udp_dest_port = htons(234); // ignored packet->udp_sour_port = htons(0xDFAC); packet->udp_length = htons(4152); // JF headers are little endian packet->eiger.detectortype = SLS_DETECTOR_TYPE_EIGER; packet->eiger.timestamp = 0xABCDEF0000FEDCBAL; packet->eiger.bunchid = 0x1234567898765431L; packet->eiger.row = (2 * module_number) | (row % 2); packet->eiger.column = col % 2; packet->eiger.framenum = frame_number; packet->eiger.packetnum = eth_packet % 64; if (data != nullptr) { for (int i = 0; i < 2048; i++) packet->eiger.data[i] = data[i]; } packet->udp_checksum = htons(checksum( (uint16_t *) (buff+42), 4096+48)); SendPacket(buff, 66*64, 0); } void HLSDevice::HW_ReadActionRegister(DataCollectionConfig *job) { memcpy(job, &cfg, sizeof(DataCollectionConfig)); } void HLSDevice::HW_WriteActionRegister(const DataCollectionConfig *job) { memcpy(&cfg, job, sizeof(DataCollectionConfig)); } void HLSDevice::FPGA_StartAction(const DiffractionExperiment &experiment) { if (action_thread.joinable()) action_thread.join(); run_counter += 1; impl_->run_data_collection = 1; impl_->cancel_data_collection = 0; idle = false; while (!impl_->din_frame_generator.empty()) impl_->din_frame_generator.read(); impl_->datamover_out.ClearCompletedDescriptors(); action_thread = std::thread(&HLSDevice::HLSMainThread, this ); } void HLSDevice::FrameGeneratorFuture(FrameGeneratorConfig config) { frame_generator(impl_->din_frame_generator, impl_->hbm.data(), impl_->hbm.data(), impl_->hbm_if_size, mac_addr, ipv4_addr, impl_->cancel_data_collection, config); } void HLSDevice::HW_RunInternalGenerator(const FrameGeneratorConfig &config) { frame_generator_future = std::async(std::launch::async, &HLSDevice::FrameGeneratorFuture, this, config); } void HLSDevice::FPGA_EndAction() { if (action_thread.joinable()) action_thread.join(); } bool HLSDevice::HW_ReadMailbox(uint32_t *values) { std::unique_lock ul(completion_mutex); ap_uint<32> tmp; bool ret = impl_->completion_stream.read_nb(tmp); values[0] = tmp; // equivalent to driver functionality if (ret) { uint32_t data_collection_id = (values[0] >> 16) & 0xFFFF; uint32_t handle = values[0] & 0xFFFF; if (handle == HANDLE_START) completion_count = 0; else if ((handle != HANDLE_END) && (data_collection_id != DATA_COLLECTION_ID_PURGE)) { completion_count++; while (completion_count * DMA_DESCRIPTORS_PER_MODULE > impl_->datamover_out.GetCompletedDescriptors()) std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } return ret; } void HLSDevice::Cancel() { impl_->cancel_data_collection = 1; } bool HLSDevice::HW_IsIdle() const { return idle && impl_->datamover_out.IsIdle(); } bool HLSDevice::HW_SendWorkRequest(uint32_t handle) { impl_->work_request_stream.write(handle); return true; } inline uint32_t float2uint(float f) { float_uint32 fu; fu.f = f; return fu.u; } void HLSDevice::HLSMainThread() { ap_uint<1> clear_counters = 0; uint64_t packets_processed; std::vector hls_cores; STREAM_512 ip1, udp1, udp2, icmp1, arp1; STREAM_512 network0; STREAM_768 stream_768_0; STREAM_768 stream_768_1; STREAM_768 stream_768_2; STREAM_768 stream_768_3; STREAM_768 stream_768_4; STREAM_768 stream_768_5; STREAM_768 stream_768_6; STREAM_512 data_0; hls::stream, 2> data_1; hls::stream, 2> data_2; STREAM_512 data_3; STREAM_512 data_4; STREAM_512 data_5; STREAM_512 data_6; STREAM_512 data_7; STREAM_512 data_8; STREAM_512 data_9; STREAM_512 data_10; STREAM_512 data_12; STREAM_512 data_13; hls::stream addr0; hls::stream addr1; hls::stream addr2; hls::stream addr3; hls::stream axi_compl[12]; hls::stream> hbm_handles; hls::stream> adu_histo_result; hls::stream> integration_result_0; hls::stream> integration_result_1; hls::stream> spot_finder_result_0; hls::stream> spot_finder_result_1; hls::stream> spot_finder_conn_0; hls::stream> spot_finder_result_2; hls::stream> spot_finder_result_3; hls::stream> spot_finder_mask_0; hls::stream> roi_calc_result_0; hls::stream > udp_metadata; volatile ap_uint<1> idle_data_collection = 1; ap_uint<1> load_to_hbm_idle; ap_uint<1> save_to_hbm_idle; ap_uint<1> integration_idle; ap_uint<1> stream_conv_idle; ap_uint<1> frame_summation_idle; volatile bool done = false; volatile bool udp_done = false; // done AND udp_idle volatile bool sls_done = false; // done AND sls_idle // Sent gratuitous ARP message arp(arp1, impl_->dout_eth, mac_addr, ipv4_addr, 1, 1); Logger logger_hls("HLS"); volatile rcv_state_t state = RCV_INIT; // Start data collection data_collection_fsm(data_0, data_1, addr0, addr1, impl_->run_data_collection, impl_->cancel_data_collection, idle_data_collection, cfg.mode, float2uint(cfg.energy_kev), cfg.nframes, cfg.nmodules, cfg.nstorage_cells, cfg.nsummation , cfg.sqrtmult, state); impl_->run_data_collection = 0; data_collection_fsm(data_0, data_1, addr0, addr1, impl_->run_data_collection, impl_->cancel_data_collection, idle_data_collection, cfg.mode, float2uint(cfg.energy_kev), cfg.nframes, cfg.nmodules, cfg.nstorage_cells, cfg.nsummation, cfg.sqrtmult, state); hls_cores.emplace_back([&] { while (!udp_done) { if (impl_->din_eth.empty() && impl_->din_frame_generator.empty()) std::this_thread::sleep_for(std::chrono::microseconds(10)); else stream_merge(impl_->din_eth, impl_->din_frame_generator, network0, impl_->data_source); } logger_hls.Info("Stream_merge done"); }); hls_cores.emplace_back([&] { while (!udp_done) { if (network0.empty()) std::this_thread::sleep_for(std::chrono::microseconds(10)); else ethernet(network0, ip1, arp1, mac_addr, eth_packets, clear_counters); } logger_hls.Info("ethernet done"); }); hls_cores.emplace_back([&] { while (!udp_done) { if (ip1.empty()) std::this_thread::sleep_for(std::chrono::microseconds(10)); else ipv4(ip1, udp1, icmp1, ipv4_addr); } logger_hls.Info("ipv4 done"); }); hls_cores.emplace_back([&] { ap_uint<1> udp_idle = 1; while (!done || !udp_idle) { if (udp1.empty()) std::this_thread::sleep_for(std::chrono::microseconds(10)); else udp(udp1, udp2, udp_metadata, udp_packets, clear_counters, udp_idle); } udp_done = true; logger_hls.Info("udp done"); }); hls_cores.emplace_back([&] { ap_uint<1> sls_idle = 1; while (!done || !sls_idle) { if (udp2.empty()) std::this_thread::sleep_for(std::chrono::microseconds (10)); else sls_detector(udp2, udp_metadata, data_0, addr0, sls_packets, udp_eth_err, udp_len_err, current_pulse_id, clear_counters, sls_idle); } sls_done = true; logger_hls.Info("sls_detector done"); }); // 1. Parse incoming UDP packets hls_cores.emplace_back([&] { while ((state != RCV_WAIT_FOR_START) || (idle_data_collection.read() == 0) || (!data_0.empty())) { data_collection_fsm(data_0, data_1, addr0, addr1, impl_->run_data_collection, impl_->cancel_data_collection, idle_data_collection, cfg.mode, float2uint(cfg.energy_kev), cfg.nframes, cfg.nmodules, cfg.nstorage_cells, cfg.nsummation, cfg.sqrtmult, state); } done = true; logger_hls.Info("data collection done"); }); // 2. Cache images in HBM hls_cores.emplace_back([&] { save_to_hbm(addr1, axi_compl[0], hbm_handles, impl_->datamover_out_hbm_0.GetCtrlStream(), impl_->datamover_out_hbm_1.GetCtrlStream(), save_to_hbm_idle, impl_->hbm_if_size);}); hls_cores.emplace_back([&] { save_to_hbm_data(data_1, data_3, impl_->datamover_out_hbm_0.GetDataStream(), impl_->datamover_out_hbm_1.GetDataStream());}); hls_cores.emplace_back([&] {frame_summation_reorder_compl(data_3, data_4, axi_compl[0], axi_compl[1]);}); hls_cores.emplace_back([&] { load_from_hbm(data_4, data_5, axi_compl[1], axi_compl[2], hbm_handles, impl_->datamover_in_hbm_0.GetDataStream(), impl_->datamover_in_hbm_1.GetDataStream(), impl_->datamover_in_hbm_0.GetCtrlStream(), impl_->datamover_in_hbm_1.GetCtrlStream(), load_to_hbm_idle, impl_->hbm_if_size);}); hls_cores.emplace_back([&] { pedestal(data_5, data_6, axi_compl[2], axi_compl[3], impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm_if_size);}); // 3. Calculate histogram of ADU values hls_cores.emplace_back([&] { adu_histo(data_6, data_7, adu_histo_result, axi_compl[3], axi_compl[4]);}); // 4. Mask missing pixels hls_cores.emplace_back([&] { mask_missing(data_7, data_8, axi_compl[4], axi_compl[5]);}); // 5. Handle EIGER packets properly hls_cores.emplace_back([&] { eiger_reorder(data_8, data_9, axi_compl[5], axi_compl[6]);}); // 6. Apply pedestal & gain corrections hls_cores.emplace_back([&] { jf_conversion(data_9, stream_768_0, axi_compl[6], axi_compl[7], impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm_if_size); }); // 7. Frame summation hls_cores.emplace_back([&] { frame_summation(stream_768_0, stream_768_1, axi_compl[7], axi_compl[8], frame_summation_idle);}); // 8. Integration of pixels hls_cores.emplace_back([&] { integration(stream_768_1, stream_768_2, integration_result_0, axi_compl[8], axi_compl[9], impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), impl_->hbm.data(), integration_idle, impl_->hbm_if_size);}); hls_cores.emplace_back([&] { axis_64_to_512(integration_result_0, integration_result_1);}); // 9. Spot finding ap_uint<32> tmp_snr_threshold = float2uint(spot_finder_parameters.snr_threshold); ap_uint<32> min_d = float2uint(spot_finder_parameters.min_d); ap_uint<32> max_d = float2uint(spot_finder_parameters.max_d); ap_int<32> tmp_count_threshold = spot_finder_parameters.count_threshold; ap_uint<32> min_pix_per_spot = spot_finder_parameters.min_pix_per_spot; hls_cores.emplace_back([&] { spot_finder_mask(stream_768_2, stream_768_3, spot_finder_mask_0, axi_compl[9], axi_compl[10], impl_->hbm.data(), impl_->hbm.data(), min_d, max_d, impl_->hbm_if_size); logger_hls.Info("spot_finder_mask done"); }); hls_cores.emplace_back([&] { spot_finder(stream_768_3, spot_finder_mask_0, stream_768_4, spot_finder_result_0, tmp_count_threshold, tmp_snr_threshold); logger_hls.Info("spot_finder done"); }); hls_cores.emplace_back([&] { spot_finder_connectivity(spot_finder_result_0, spot_finder_result_1, spot_finder_conn_0); logger_hls.Info("spot_finder_connectivity done"); }); hls_cores.emplace_back([&] { spot_finder_merge(spot_finder_result_1, spot_finder_conn_0, spot_finder_result_2, min_pix_per_spot); logger_hls.Info("spot_finder_merge done"); }); hls_cores.emplace_back([&] { axis_32_to_512(spot_finder_result_2, spot_finder_result_3); logger_hls.Info("axis_32_to_512 done"); }); // 10. ROI calculation hls_cores.emplace_back([&] { roi_calc(stream_768_4, stream_768_5, roi_calc_result_0, axi_compl[10], axi_compl[11], impl_->hbm.data(), impl_->hbm.data(), impl_->hbm_if_size); logger_hls.Info("roi_calc done"); }); // 11. Pixel sq root hls_cores.emplace_back([&] { pixel_sqrt(stream_768_5, stream_768_6); logger_hls.Info("pixel_sqrt done"); }); // 12. Reduce/extend 24-bit stream hls_cores.emplace_back([&] { stream_24bit_conv(stream_768_6, data_12, stream_conv_idle);}); // 13. Prepare data to write to host memory hls_cores.emplace_back([&] { ap_uint<3> state; host_writer(data_12, adu_histo_result, integration_result_1, spot_finder_result_3, roi_calc_result_0, axi_compl[11], impl_->datamover_out.GetDataStream(), impl_->datamover_out.GetCtrlStream(), impl_->work_request_stream, impl_->completion_stream, dma_address_table.data(), packets_processed, impl_->host_writer_idle, impl_->cancel_data_collection, state); logger_hls.Info("host_writer done"); }); for (auto &i : hls_cores) i.join(); if (frame_generator_future.valid()) frame_generator_future.get(); // reset static counter arp(arp1, impl_->dout_eth, mac_addr, ipv4_addr, 0, 1); try { while (!impl_->din_eth.empty()) impl_->din_eth.read(); if (!addr1.empty()) throw std::runtime_error("Addr1 queue not empty"); if (!addr2.empty()) throw std::runtime_error("Addr2 queue not empty"); if (!addr3.empty()) throw std::runtime_error("Addr3 queue not empty"); if (!data_1.empty()) throw std::runtime_error("data_1 queue not empty"); if (!data_2.empty()) throw std::runtime_error("data_2 queue not empty"); if (!data_3.empty()) throw std::runtime_error("data_3 queue not empty"); if (!data_4.empty()) throw std::runtime_error("data_4 queue not empty"); if (!data_5.empty()) throw std::runtime_error("data_5 queue not empty"); if (!data_7.empty()) throw std::runtime_error("data_7 queue not empty"); if (!data_8.empty()) throw std::runtime_error("data_8 queue not empty"); if (!data_9.empty()) throw std::runtime_error("data_9 queue not empty"); if (!data_10.empty()) throw std::runtime_error("data_10 queue not empty"); if (!data_12.empty()) throw std::runtime_error("data_12 queue not empty"); if (!data_13.empty()) throw std::runtime_error("data_13 queue not empty"); for (auto &c: axi_compl) { if (!c.empty()) throw std::runtime_error("Compl queue not empty"); } if (!stream_768_0.empty()) throw std::runtime_error("stream_768_0 queue not empty"); if (!stream_768_1.empty()) throw std::runtime_error("stream_768_1 queue not empty"); if (!stream_768_2.empty()) throw std::runtime_error("stream_768_2 queue not empty"); if (!stream_768_3.empty()) throw std::runtime_error("stream_768_3 queue not empty"); if (!stream_768_4.empty()) throw std::runtime_error("stream_768_4 queue not empty"); if (!hbm_handles.empty()) throw std::runtime_error("Handles queue not empty"); if (!integration_result_0.empty()) throw std::runtime_error("Integration result queue not empty"); if (!integration_result_1.empty()) throw std::runtime_error("Integration result queue not empty"); if (!impl_->datamover_in.GetDataStream().empty()) throw std::runtime_error("Datamover queue is not empty"); while (!impl_->datamover_out.IsIdle()) std::this_thread::sleep_for(std::chrono::milliseconds(100)); } catch (const std::runtime_error &e) { logger_hls.ErrorException(e); throw e; } idle = true; } DataCollectionStatus HLSDevice::GetDataCollectionStatus() const { DataCollectionStatus status{}; union { uint64_t u64; double d; } pulse_id_conv; pulse_id_conv.u64 = current_pulse_id; status.ctrl_reg = ap_uint<1>(impl_->host_writer_idle) ? (1 << 4) : 0; status.max_modules = MAX_MODULES_FPGA; status.hbm_size_bytes = impl_->hbm_if_size; status.run_counter = run_counter; status.current_pulseid = pulse_id_conv.d; status.packets_udp = udp_packets; status.packets_eth = eth_packets; status.packets_sls = sls_packets; return status; } void HLSDevice::HW_LoadCalibration(const LoadCalibrationConfig &config) { int ret = load_calibration(impl_->hbm.data(), impl_->hbm.data(), config, impl_->hbm_if_size, impl_->datamover_in.GetCtrlStream(), impl_->datamover_in.GetDataStream(), dma_address_table.data()); if (ret) throw JFJochException(JFJochExceptionCategory::AcquisitionDeviceError, "Error in loading calibration " + std::to_string(ret)); if (!impl_->datamover_in.GetDataStream().empty()) throw std::runtime_error("Datamover queue is not empty"); } void HLSDevice::HW_SetSpotFinderParameters(const SpotFinderParameters ¶ms) { spot_finder_parameters = params; } void HLSDevice::HW_SetDataSource(uint32_t val) { impl_->data_source = val; } uint32_t HLSDevice::HW_GetDataSource() { return impl_->data_source; } void HLSDevice::CreateXfelBunchIDPacket(double pulse_id, uint32_t event_code) { union { uint64_t u64; double d; } pulse_id_conv; pulse_id_conv.d = pulse_id; bunchid_raw_packet packet{}; packet.ether_type = htons(0x0800); for (int i = 0; i < 6; i++) packet.dest_mac[i] = 0xFF; packet.ipv4_header_h = htons(0x4500); // Big endian in IP header! packet.ipv4_header_total_length = htons(sizeof(bunchid_payload) + 8 + 20); // Big endian in IP header! packet.ipv4_header_dest_ip = UINT32_MAX; packet.ipv4_header_ttl_protocol = htons(0x0011); packet.ipv4_header_checksum = checksum( (uint16_t *) &packet.ipv4_header_h, 20); // checksum is already in network order packet.udp_length = htons(sizeof(bunchid_payload) + 8); packet.payload.magicn[0] = BUNCHID_MAGICN; packet.payload.magicn[1] = BUNCHID_MAGICN; packet.payload.magicn[2] = BUNCHID_MAGICN; packet.payload.bunchid_msb[0] = (pulse_id_conv.u64 >> 32) & UINT32_MAX; packet.payload.bunchid_msb[1] = (pulse_id_conv.u64 >> 32) & UINT32_MAX; packet.payload.bunchid_lsb[0] = pulse_id_conv.u64 & UINT32_MAX; packet.payload.bunchid_lsb[1] = pulse_id_conv.u64 & UINT32_MAX; packet.payload.montrig = event_code; SendPacket((char *) &packet, sizeof(bunchid_raw_packet)); }