command.c 8.0 KB

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