#include "command.h" #include #include #include #include #include #include #include #include #include #include #include "Devices/lmx2594.h" #include "Devices/dac8811.h" #include "Devices/ad9912.h" #include "Devices/potentiometer.h" double f_pd = 200e6; uint16_t armCode[1] = {0}; uint16_t attCode[1] = {0}; uint16_t offsetCode[1] = {0}; uint16_t slopeCode[1] = {0}; //Массив структур command, который связывает строки команд с соответствующими функциями. command commands[] = { {"TMSG44:FREQ ", handle_freq_cmd}, {"TMSG44:LD?", handle_ld_cmd}, {"TMSG44:POW ", handle_pow_cmd}, {"TMSG44:ARM ", handle_arm_cmd}, {"TMSG44:ATT ", handle_att_cmd}, {"*IDN?", handle_idn_cmd}, {"TMSG44:OFFSET ", handle_offset_cmd}, {"TMSG44:SLOPE ", handle_slope_cmd}, {NULL, NULL} // Завершающий элемент для обозначения конца массива }; //handleXXXXCmd - обработчики команд void handle_freq_cmd(const char* recv_buff) { printf("\nHandle command \"TMSG44:FREQ\"\n"); double freq[1] = {0}; double lmx_freq = 0; split_lexeme(recv_buff, freq, sizeof(freq[0]), convert_to_double); lmx_freq = lmx_get_freq(freq[0]); f_pd = ad9912_set(pci_bar_1, lmx_freq, f_pd); printf("f_pd frequency is set to %.6f MHz\n", f_pd/1e6); lmx_freq_set(pci_bar_1, lmx_freq, f_pd); // Switch the keys key_switch(pci_bar_1, freq[0],lmx_freq); printf("The frequency is set to %.2f MHz\n", freq[0]/1e6); // Send the data send_data_qspi(pci_bar_1); // Return the 1 MOSI mode // usleep(1); // cfg_reg = get_cfg_reg(); // SET_REGISTER_PARAM(cfg_reg,CFG_REG_SPI_MODE_BITM,CFG_REG_SPI_MODE_BITP, CFG_REG_SPI_MODE_1MOSI); // *spi_mode = cfg_reg; // set_cfg_reg(cfg_reg); } void send_data_qspi(reg_addr_pci* pci_bar_1) { // get the gpio reg and shift reg data uint32_t gpio_reg = get_tmsg_gpio_reg(); uint32_t shift_reg = get_tmsg_shift_reg(); // Create a header 4 Mosi mode uint32_t qspi_header = ((ENUM_SPIMODE_4MOSI) | (0x1 << BITP_GPIO_4MOSI_HEADER) | (0x1 << BITP_SHIFT_REG_4MOSI_HEADER ) | ((sizeof(ad9912_ftw_regs_qspi) / 4) << BITP_DDS_4MOSI_HEADER) | ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | (TERM_BIT_1)); pci_bar_1->sbtmsg_addr = qspi_header; // Initialize the registers // Send the data for AD9912 for (int i = 0; i < sizeof(ad9912_ftw_regs_qspi) / 4; i++) { pci_bar_1->sbtmsg_addr = ad9912_ftw_regs_qspi[i]; } // Send the data for the GPIO pci_bar_1->sbtmsg_addr = gpio_reg; // Send the data for LMX2594 for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) { pci_bar_1->sbtmsg_addr = lmx_change_freq_regs[i]; } // Send the data for the shift register pci_bar_1->sbtmsg_addr = shift_reg; } void handle_ld_cmd(const char* recv_buff) { char messageLd[] = "1\n"; printf("\nHandle command \"TMSG44:LD?\"\n"); uint32_t ld_status = lmx_ld_status(pci_bar_1); clock_t before = clock(); clock_t difference; int difference_msec = 0; int trigger = 10000; //10ms while(!ld_status) { difference = clock() - before; difference_msec = difference * 1000 / CLOCKS_PER_SEC; if(difference_msec >= trigger) { strcpy(messageLd, "0\n"); printf("LD timeout\n"); break; } ld_status = lmx_ld_status(pci_bar_1); // printf("WHILE LD status: %d\n", ld_status); } send(conn_fd, messageLd, sizeof(messageLd), 0); printf("\nSend msg LD: %d!\n", ld_status); } void handle_pow_cmd(const char* recv_buff) { printf("\nHandle command \"TMSG44:POW\"\n"); double pow[1] = {0}; split_lexeme(recv_buff, pow, sizeof(pow[0]), convert_to_double); printf("%f\n", pow[0]); } void handle_arm_cmd(const char* recv_buff) { printf("\nHandle command \"TMSG44:ARM\"\n"); split_lexeme(recv_buff, armCode, sizeof(armCode[0]), convert_to_uint16); printf("\n%u\n", armCode[0]); dac8811_set_qspi(pci_bar_1, armCode[0]); } void handle_att_cmd(const char* recv_buff) { printf("\nHandle command \"TMSG44:ATT\"\n"); split_lexeme(recv_buff, attCode, sizeof(attCode[0]), convert_to_uint16); printf("\n%u\n", attCode[0]); dac8811_att_set_qspi(pci_bar_1, attCode[0]); } void handle_idn_cmd(const char* recv_buff) { printf("\nHandle command \"*IDN?\"\n"); char messageIdn[] = "TMSG44_CoolPi\n"; send(conn_fd, messageIdn, sizeof(messageIdn), 0); } void handle_offset_cmd(const char* recv_buff) { printf("\nHandle command \"TMSG44:OFFSET\"\n"); split_lexeme(recv_buff, offsetCode, sizeof(offsetCode[0]), convert_to_uint16); printf("\n%u\n", offsetCode[0]); potentiometer_set_offset(pci_bar_1, offsetCode[0]); } void handle_slope_cmd(const char* recv_buff) { printf("\nHandle command \"TMSG44:SLOPE\"\n"); split_lexeme(recv_buff, slopeCode, sizeof(slopeCode[0]), convert_to_uint16); printf("\n%u\n", slopeCode[0]); potentiometer_set_slope(pci_bar_1, slopeCode[0]); } //Проходим по массиву команд и ищем команду, которая совпадает с началом строки recv_buff. //Если команда найдена, вызывается соответствующая функция-обработчик void process_command(const char* recv_buff) { for (int i = 0; commands[i].command != NULL; i++) { if (!strncasecmp(recv_buff, commands[i].command, strlen(commands[i].command))) { commands[i].handler(recv_buff); return; } } printf("\nUnknown command: %s\n", recv_buff); } // Преобразование строки в uint16_t void convert_to_uint16(const char *str, void *output) { *(uint16_t *)output = (uint16_t)strtoul(str, NULL, 10); } // Преобразование строки в unsigned long long int void convert_to_uint64(const char *str, void *output) { *(uint64_t *)output = (uint64_t)strtoull(str, NULL, 10); } // Преобразование строки в double void convert_to_double(const char *str, void *output) { *(double *)output = strtod(str, NULL); } // Универсальная функция для разделения строки на лексемы void split_lexeme(const char *ptr_scpi, void *out_value, size_t element_size, convert_func_type convert_func) { uint8_t counter = 0; // Разделители лексем const char charSeparator[] = {" "}; char *ptr_lexeme = NULL; // Указатель для хранения контекста токенизации char *savePtr; // Инициализируем функцию ptr_lexeme = strtok_r((char *)ptr_scpi, charSeparator, &savePtr); // Ищем лексемы разделенные разделителем ptr_lexeme = strtok_r(NULL, charSeparator, &savePtr); // Ищем лексемы строки while (ptr_lexeme) { // Проверяем, является ли первый символ лексемы числом if(('0' <= ptr_lexeme[0]) && (ptr_lexeme[0] <= '9')) { // Преобразуем строку с числом в число convert_func(ptr_lexeme, (uint8_t *)out_value + counter * element_size); counter++; } // Ищем лексемы разделенные разделителем ptr_lexeme = strtok_r(NULL, charSeparator, &savePtr); } }