command.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. int difference_msec = 0;
  85. int trigger = 10000; //10ms
  86. while (!ld_status) {
  87. clock_t difference = clock() - before;
  88. difference_msec = difference * 1000 / CLOCKS_PER_SEC;
  89. if (difference_msec >= trigger) {
  90. strcpy(messageLd, "0\n");
  91. printf("LD timeout\n");
  92. break;
  93. }
  94. ld_status = lmx_ld_status(pci_bar_1);
  95. // printf("WHILE LD status: %d\n", ld_status);
  96. }
  97. send(conn_fd, messageLd, sizeof(messageLd), 0);
  98. printf("\nSend msg LD: %d!\n", ld_status);
  99. }
  100. void handle_pow_cmd(const char *recv_buff) {
  101. printf("\nHandle command \"TMSG44:POW\"\n");
  102. double pow[1] = {0};
  103. split_lexeme(recv_buff, pow, sizeof(pow[0]), convert_to_double);
  104. printf("%f\n", pow[0]);
  105. }
  106. void handle_arm_cmd(const char *recv_buff) {
  107. printf("\nHandle command \"TMSG44:ARM\"\n");
  108. split_lexeme(recv_buff, armCode, sizeof(armCode[0]), convert_to_uint16);
  109. printf("\n%u\n", armCode[0]);
  110. dac8811_set_qspi(pci_bar_1, armCode[0]);
  111. }
  112. void handle_att_cmd(const char *recv_buff) {
  113. printf("\nHandle command \"TMSG44:ATT\"\n");
  114. split_lexeme(recv_buff, attCode, sizeof(attCode[0]), convert_to_uint16);
  115. printf("\n%u\n", attCode[0]);
  116. dac8811_att_set_qspi(pci_bar_1, attCode[0]);
  117. }
  118. void handle_idn_cmd(const char *recv_buff) {
  119. printf("\nHandle command \"*IDN?\"\n");
  120. char messageIdn[] = "TMSG44_CoolPi\n";
  121. send(conn_fd, messageIdn, sizeof(messageIdn), 0);
  122. }
  123. void handle_offset_cmd(const char *recv_buff) {
  124. printf("\nHandle command \"TMSG44:OFFSET\"\n");
  125. split_lexeme(recv_buff, offsetCode, sizeof(offsetCode[0]), convert_to_uint16);
  126. printf("\n%u\n", offsetCode[0]);
  127. potentiometer_set_offset(pci_bar_1, offsetCode[0]);
  128. }
  129. void handle_slope_cmd(const char *recv_buff) {
  130. printf("\nHandle command \"TMSG44:SLOPE\"\n");
  131. split_lexeme(recv_buff, slopeCode, sizeof(slopeCode[0]), convert_to_uint16);
  132. printf("\n%u\n", slopeCode[0]);
  133. potentiometer_set_slope(pci_bar_1, slopeCode[0]);
  134. }
  135. //Проходим по массиву команд и ищем команду, которая совпадает с началом строки recv_buff.
  136. //Если команда найдена, вызывается соответствующая функция-обработчик
  137. void process_command(const char *recv_buff) {
  138. for (int i = 0; commands[i].command != NULL; i++) {
  139. if (!strncasecmp(recv_buff, commands[i].command, strlen(commands[i].command))) {
  140. commands[i].handler(recv_buff);
  141. return;
  142. }
  143. }
  144. printf("\nUnknown command: %s\n", recv_buff);
  145. }
  146. // Преобразование строки в uint16_t
  147. void convert_to_uint16(const char *str, void *output) {
  148. *(uint16_t *) output = (uint16_t) strtoul(str, NULL, 10);
  149. }
  150. // Преобразование строки в unsigned long long int
  151. void convert_to_uint64(const char *str, void *output) {
  152. *(uint64_t *) output = (uint64_t) strtoull(str, NULL, 10);
  153. }
  154. // Преобразование строки в double
  155. void convert_to_double(const char *str, void *output) {
  156. *(double *) output = strtod(str, NULL);
  157. }
  158. // Универсальная функция для разделения строки на лексемы
  159. void split_lexeme(const char *ptr_scpi, void *out_value, size_t element_size, convert_func_type convert_func) {
  160. uint8_t counter = 0;
  161. // Разделители лексем
  162. const char charSeparator[] = {" "};
  163. char *ptr_lexeme = NULL;
  164. // Указатель для хранения контекста токенизации
  165. char *savePtr;
  166. // Инициализируем функцию
  167. ptr_lexeme = strtok_r((char *) ptr_scpi, charSeparator, &savePtr);
  168. // Ищем лексемы разделенные разделителем
  169. ptr_lexeme = strtok_r(NULL, charSeparator, &savePtr);
  170. // Ищем лексемы строки
  171. while (ptr_lexeme) {
  172. // Проверяем, является ли первый символ лексемы числом
  173. if (('0' <= ptr_lexeme[0]) && (ptr_lexeme[0] <= '9')) {
  174. // Преобразуем строку с числом в число
  175. convert_func(ptr_lexeme, (uint8_t *) out_value + counter * element_size);
  176. counter++;
  177. }
  178. // Ищем лексемы разделенные разделителем
  179. ptr_lexeme = strtok_r(NULL, charSeparator, &savePtr);
  180. }
  181. }