command.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "command.h"
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <inttypes.h>
  5. #include <stdio.h>
  6. #include <strings.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <sys/time.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12. #include "Devices/lmx2594.h"
  13. #include "Devices/dac8811.h"
  14. #include "Devices/ad9912.h"
  15. #include "Devices/potentiometer.h"
  16. double f_pd = 200e6;
  17. uint16_t armCode[1] = {0};
  18. uint16_t attCode[1] = {0};
  19. uint16_t offsetCode[1] = {0};
  20. uint16_t slopeCode[1] = {0};
  21. //Массив структур command, который связывает строки команд с соответствующими функциями.
  22. command commands[] = {{"TMSG44:FREQ ", handle_freq_cmd},
  23. {"TMSG44:LD?", handle_ld_cmd},
  24. {"TMSG44:POW ", handle_pow_cmd},
  25. {"TMSG44:ARM ", handle_arm_cmd},
  26. {"TMSG44:ATT ", handle_att_cmd},
  27. {"*IDN?", handle_idn_cmd},
  28. {"TMSG44:OFFSET ", handle_offset_cmd},
  29. {"TMSG44:SLOPE ", handle_slope_cmd},
  30. {NULL, NULL} // Завершающий элемент для обозначения конца массива
  31. };
  32. //handleXXXXCmd - обработчики команд
  33. void handle_freq_cmd(const char *recv_buff) {
  34. printf("\nHandle command \"TMSG44:FREQ\"\n");
  35. double freq[1] = {0};
  36. double lmx_freq = 0;
  37. split_lexeme(recv_buff, freq, sizeof(freq[0]), convert_to_double);
  38. lmx_freq = lmx_get_freq(freq[0]);
  39. f_pd = ad9912_set(pci_bar_1, lmx_freq, f_pd);
  40. printf("f_pd frequency is set to %.6f MHz\n", f_pd / 1e6);
  41. lmx_freq_set(pci_bar_1, lmx_freq, f_pd);
  42. // Switch the keys
  43. key_switch(pci_bar_1, freq[0], lmx_freq);
  44. printf("The frequency is set to %.2f MHz\n", freq[0] / 1e6);
  45. // Send the data
  46. send_data_qspi(pci_bar_1);
  47. }
  48. void send_data_qspi(reg_addr_pci *pci_bar_1) {
  49. // get the gpio reg and shift reg data
  50. uint32_t gpio_reg = get_tmsg_gpio_reg();
  51. uint32_t shift_reg = get_tmsg_shift_reg();
  52. // Create a header 4 Mosi mode
  53. uint32_t qspi_header = ((ENUM_SPIMODE_4MOSI) | (0x1 << BITP_GPIO_4MOSI_HEADER) |
  54. (0x1 << BITP_SHIFT_REG_4MOSI_HEADER) |
  55. ((sizeof(ad9912_ftw_regs_qspi) / 4) << BITP_DDS_4MOSI_HEADER) |
  56. ((sizeof(lmx_change_freq_regs) / 4) << BITP_LMX2594_4MOSI_HEADER) | (TERM_BIT_1));
  57. pci_bar_1->sbtmsg_addr = qspi_header;
  58. // Initialize the registers
  59. // Send the data for AD9912
  60. for (int i = 0; i < sizeof(ad9912_ftw_regs_qspi) / 4; i++) {
  61. pci_bar_1->sbtmsg_addr = ad9912_ftw_regs_qspi[i];
  62. }
  63. // Send the data for the GPIO
  64. pci_bar_1->sbtmsg_addr = gpio_reg;
  65. // Send the data for LMX2594
  66. for (int i = 0; i < sizeof(lmx_change_freq_regs) / 4; i++) {
  67. pci_bar_1->sbtmsg_addr = lmx_change_freq_regs[i];
  68. }
  69. // Send the data for the shift register
  70. pci_bar_1->sbtmsg_addr = shift_reg;
  71. }
  72. void handle_ld_cmd(const char *recv_buff) {
  73. char messageLd[] = "1\n";
  74. printf("\nHandle command \"TMSG44:LD?\"\n");
  75. uint32_t ld_status = lmx_ld_status(pci_bar_1);
  76. clock_t before = clock();
  77. clock_t difference;
  78. int difference_msec = 0;
  79. int trigger = 10000; //10ms
  80. while (!ld_status) {
  81. difference = clock() - before;
  82. difference_msec = difference * 1000 / CLOCKS_PER_SEC;
  83. if (difference_msec >= trigger) {
  84. strcpy(messageLd, "0\n");
  85. printf("LD timeout\n");
  86. break;
  87. }
  88. ld_status = lmx_ld_status(pci_bar_1);
  89. // printf("WHILE LD status: %d\n", ld_status);
  90. }
  91. send(conn_fd, messageLd, sizeof(messageLd), 0);
  92. printf("\nSend msg LD: %d!\n", ld_status);
  93. }
  94. void handle_pow_cmd(const char *recv_buff) {
  95. printf("\nHandle command \"TMSG44:POW\"\n");
  96. double pow[1] = {0};
  97. split_lexeme(recv_buff, pow, sizeof(pow[0]), convert_to_double);
  98. printf("%f\n", pow[0]);
  99. }
  100. void handle_arm_cmd(const char *recv_buff) {
  101. printf("\nHandle command \"TMSG44:ARM\"\n");
  102. split_lexeme(recv_buff, armCode, sizeof(armCode[0]), convert_to_uint16);
  103. printf("\n%u\n", armCode[0]);
  104. dac8811_set_qspi(pci_bar_1, armCode[0]);
  105. }
  106. void handle_att_cmd(const char *recv_buff) {
  107. printf("\nHandle command \"TMSG44:ATT\"\n");
  108. split_lexeme(recv_buff, attCode, sizeof(attCode[0]), convert_to_uint16);
  109. printf("\n%u\n", attCode[0]);
  110. dac8811_att_set_qspi(pci_bar_1, attCode[0]);
  111. }
  112. void handle_idn_cmd(const char *recv_buff) {
  113. printf("\nHandle command \"*IDN?\"\n");
  114. char messageIdn[] = "TMSG44_CoolPi\n";
  115. send(conn_fd, messageIdn, sizeof(messageIdn), 0);
  116. }
  117. void handle_offset_cmd(const char *recv_buff) {
  118. printf("\nHandle command \"TMSG44:OFFSET\"\n");
  119. split_lexeme(recv_buff, offsetCode, sizeof(offsetCode[0]), convert_to_uint16);
  120. printf("\n%u\n", offsetCode[0]);
  121. potentiometer_set_offset(pci_bar_1, offsetCode[0]);
  122. }
  123. void handle_slope_cmd(const char *recv_buff) {
  124. printf("\nHandle command \"TMSG44:SLOPE\"\n");
  125. split_lexeme(recv_buff, slopeCode, sizeof(slopeCode[0]), convert_to_uint16);
  126. printf("\n%u\n", slopeCode[0]);
  127. potentiometer_set_slope(pci_bar_1, slopeCode[0]);
  128. }
  129. //Проходим по массиву команд и ищем команду, которая совпадает с началом строки recv_buff.
  130. //Если команда найдена, вызывается соответствующая функция-обработчик
  131. void process_command(const char *recv_buff) {
  132. for (int i = 0; commands[i].command != NULL; i++) {
  133. if (!strncasecmp(recv_buff, commands[i].command, strlen(commands[i].command))) {
  134. commands[i].handler(recv_buff);
  135. return;
  136. }
  137. }
  138. printf("\nUnknown command: %s\n", recv_buff);
  139. }
  140. // Преобразование строки в uint16_t
  141. void convert_to_uint16(const char *str, void *output) {
  142. *(uint16_t *) output = (uint16_t) strtoul(str, NULL, 10);
  143. }
  144. // Преобразование строки в unsigned long long int
  145. void convert_to_uint64(const char *str, void *output) {
  146. *(uint64_t *) output = (uint64_t) strtoull(str, NULL, 10);
  147. }
  148. // Преобразование строки в double
  149. void convert_to_double(const char *str, void *output) {
  150. *(double *) output = strtod(str, NULL);
  151. }
  152. // Универсальная функция для разделения строки на лексемы
  153. void split_lexeme(const char *ptr_scpi, void *out_value, size_t element_size, convert_func_type convert_func) {
  154. uint8_t counter = 0;
  155. // Разделители лексем
  156. const char charSeparator[] = {" "};
  157. char *ptr_lexeme = NULL;
  158. // Указатель для хранения контекста токенизации
  159. char *savePtr;
  160. // Инициализируем функцию
  161. ptr_lexeme = strtok_r((char *) ptr_scpi, charSeparator, &savePtr);
  162. // Ищем лексемы разделенные разделителем
  163. ptr_lexeme = strtok_r(NULL, charSeparator, &savePtr);
  164. // Ищем лексемы строки
  165. while (ptr_lexeme) {
  166. // Проверяем, является ли первый символ лексемы числом
  167. if (('0' <= ptr_lexeme[0]) && (ptr_lexeme[0] <= '9')) {
  168. // Преобразуем строку с числом в число
  169. convert_func(ptr_lexeme, (uint8_t *) out_value + counter * element_size);
  170. counter++;
  171. }
  172. // Ищем лексемы разделенные разделителем
  173. ptr_lexeme = strtok_r(NULL, charSeparator, &savePtr);
  174. }
  175. }