main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Project: Arrow
  3. * Author: curiousmuch
  4. */
  5. #include <stdio.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "freertos/semphr.h"
  9. #include "driver/gpio.h"
  10. #include "sdkconfig.h"
  11. #include "esp_task_wdt.h"
  12. #include "cc1200.h"
  13. #include "cc1200_protocol.h"
  14. #include "board.h"
  15. #include "nvs.h"
  16. #include "nvs_flash.h"
  17. #include "esp_log.h"
  18. #include "esp_bt.h"
  19. #include "esp_bt_main.h"
  20. #include "esp_gap_bt_api.h"
  21. #include "esp_bt_device.h"
  22. #include "esp_spp_api.h"
  23. #include "bt_spp.c"
  24. #include "ax25_pad2.h"
  25. #include "ax25_pad.h"
  26. #include "fcs_calc.h"
  27. #include "math.h"
  28. #include "driver/timer.h"
  29. //#include "esp_dsp.h"
  30. uint8_t APRS_TEST_PACKET[] = { 0x82, 0x98, 0x98, 0x40, 0x40, 0x40, 0xe0, 0x96, 0x84, 0x66, 0xaa, 0x96, 0xac, 0xe0, 0xae, 0x92,
  31. 0x88, 0x8a, 0x62, 0x40, 0x62, 0xae, 0x92, 0x88, 0x8a, 0x64, 0x40, 0x65, 0x03, 0xf0, 0x3a, 0x4b,
  32. 0x42, 0x33, 0x55, 0x4b, 0x56, 0x2d, 0x32, 0x20, 0x3a, 0x48, 0x69, 0x21, 0x20, 0x54, 0x68, 0x69, 0x73,
  33. 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x54, 0x65, 0x73, 0x74, 0x7b, 0x31, 0xad, 0xa1 };
  34. RingbufHandle_t radio_tx_buf;
  35. SemaphoreHandle_t xRadioRXISRSemaphore;
  36. SemaphoreHandle_t xRadioRXSemaphore;
  37. SemaphoreHandle_t xRadioTXSemaphore;
  38. TaskHandle_t xRadioRXTaskHandle;
  39. //RingbufHandle_t radio_rx_buf;
  40. extern int8_t EXTERNAL_DATA;
  41. #define WINDOW_SIZE 7
  42. #define SAMPLEFREQUENCY 6000
  43. int window[WINDOW_SIZE];
  44. int lpf_window[WINDOW_SIZE];
  45. double goertzelFilter(int samples[], double freq, int N)
  46. {
  47. double s_prev = 0.0;
  48. double s_prev2 = 0.0;
  49. double coeff,normalizedfreq,power,s;
  50. int i;
  51. normalizedfreq = freq / SAMPLEFREQUENCY;
  52. coeff = 2*cos(2*M_PI*normalizedfreq);
  53. for (i=0; i<N; i++) {
  54. s = samples[i] + coeff * s_prev - s_prev2;
  55. s_prev2 = s_prev;
  56. s_prev = s;
  57. }
  58. power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
  59. return power;
  60. }
  61. //double calculate_symbols_energy(int samples[], int N)
  62. //{
  63. // double E_s1, E_s2;
  64. // E_s1 = goertzelFilter(samples, 1200, N);
  65. // E_s2 = goertzelFilter(samples, 2200, N);
  66. // return (E_s1 - E_s2);
  67. //}
  68. void window_init(void)
  69. {
  70. for(int i=0;i<WINDOW_SIZE;i++)
  71. {
  72. window[i] = 0;
  73. }
  74. }
  75. void window_add(int sample)
  76. {
  77. for(int i=0;i<(WINDOW_SIZE-1);i++)
  78. {
  79. window[(WINDOW_SIZE-1)-i] = window[(WINDOW_SIZE-2)-i];
  80. }
  81. window[0] = sample;
  82. }
  83. uint8_t * window_get(void)
  84. {
  85. return window;
  86. }
  87. int window_get_size(void)
  88. {
  89. return WINDOW_SIZE;
  90. }
  91. // The TX Task should have the highest
  92. void TX_Task(void *pvParameters)
  93. {
  94. size_t p_size;
  95. while(1)
  96. {
  97. // block until packet is in queue
  98. uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, portMAX_DELAY);
  99. // send packet
  100. if (p != NULL)
  101. {
  102. // disable RX mode
  103. cc1200_radio_stop_APRSRX();
  104. // setup LEDs for TX mode
  105. enable_red_led();
  106. disable_green_led();
  107. cc1200_radio_APRSTXPacket(p, p_size, 2, 0);
  108. vRingbufferReturnItem(radio_tx_buf, (void *)p);
  109. // setup LEDs for RX mode
  110. disable_red_led();
  111. enable_green_led();
  112. cc1200_radio_start_APRSRX();
  113. }
  114. }
  115. }
  116. void RX_Task(void *pvParameters)
  117. {
  118. // debugging variables
  119. uint8_t toggle = 0;
  120. uint8_t count = 0;
  121. // algorithm variables
  122. double E_s1, E_s2, result;
  123. E_s1 = 0; E_s2 = 0;
  124. int f_output = 0;
  125. // Sampling Semaphore
  126. xRadioRXISRSemaphore = xSemaphoreCreateBinary();
  127. // get task handle for sampling isr
  128. xRadioRXTaskHandle = xTaskGetCurrentTaskHandle();
  129. // setup LEDs for RX mode
  130. enable_green_led();
  131. disable_red_led();
  132. cc1200_radio_start_APRSRX();
  133. uint32_t ulNotificationValue;
  134. while(1)
  135. {
  136. //ulNotificationValue = ulTaskNotifyTake(pdTRUE, 0);
  137. if( xSemaphoreTake(xRadioRXISRSemaphore, portMAX_DELAY) == pdTRUE)
  138. {
  139. //enable_debug_IO(DEBUG_0);
  140. EXTERNAL_DATA = (int)cc1200_radio_read_CFM();
  141. //disable_debug_IO(DEBUG_0);
  142. //enable_debug_IO(DEBUG_0);
  143. //f_output += (50000 * (((int)EXTERNAL_DATA) - f_output)) / 100000;
  144. window_add((int) EXTERNAL_DATA);
  145. E_s1 = goertzelFilter(window, 1200, WINDOW_SIZE);
  146. E_s2 = goertzelFilter(window, 2200, WINDOW_SIZE);
  147. if (E_s1 > E_s2)
  148. {
  149. enable_debug_IO(DEBUG_0);
  150. }
  151. else
  152. {
  153. disable_debug_IO(DEBUG_0);
  154. }
  155. ulNotificationValue = 0;
  156. //disable_debug_IO(DEBUG_0);
  157. }
  158. }
  159. }
  160. // This task might want to run on CPU0 instead of CPU1. This tasks purpose is to decide which radio should be running and how long
  161. // we should wait before transmitting if data is received. It's also responsible for LBT, etc. This task should be lower priority than BLE etc.
  162. //void Radio_Controller_Task(void *pvParameters)
  163. //{
  164. // size_t p_size;
  165. //
  166. // cc1200_radio_init(APRS_RX_SETTINGS, sizeof(APRS_RX_SETTINGS)/sizeof(cc1200_reg_settings_t));
  167. // cc1200_radio_frequency(144390000-6000);
  168. //
  169. // while(1)
  170. // {
  171. // // check for a pending APRS packet in the buffer queue
  172. // uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, 0); // TODO: Modify to something which will check CC1200 status
  173. // if (p != NULL)
  174. // {
  175. // // TODO:schedule packet based on TNC settings
  176. // xSemaphoreGive(xRadioTXSemaphore);
  177. // }
  178. // else
  179. // {
  180. // xSemaphoreGive(xRadioRXSemaphore);
  181. // }
  182. // }
  183. //}
  184. void radio_init()
  185. {
  186. // Setup Task Communications
  187. radio_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  188. xRadioRXSemaphore = xSemaphoreCreateBinary();
  189. xRadioTXSemaphore = xSemaphoreCreateBinary();
  190. // Initialize Radio
  191. cc1200_radio_init(APRS_RX_SETTINGS, sizeof(APRS_RX_SETTINGS)/sizeof(cc1200_reg_settings_t));
  192. cc1200_radio_frequency(144390000-6000);
  193. // Create Tasks
  194. // xTaskCreatePinnedToCore(Radio_Controller_Task, "Radio_Controller", 1024*4, 0, 10, NULL, 0);
  195. xTaskCreatePinnedToCore(RX_Task, "RX Task", 1024*4, 0, 1, NULL, 1);
  196. xTaskCreatePinnedToCore(TX_Task, "TX Task", 1024*4, 0, 2, NULL, 1);
  197. // xTaskCreatePinnedToCore(UART_Task, "UART Task", 1024*4, 0, 5, NULL, 0);
  198. }
  199. void IRAM_ATTR app_main()
  200. {
  201. // Initialize Flash
  202. esp_err_t ret = nvs_flash_init();
  203. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  204. ESP_ERROR_CHECK(nvs_flash_erase());
  205. ret = nvs_flash_init();
  206. }
  207. ESP_ERROR_CHECK( ret );
  208. // Board IO Initialize
  209. board_init();
  210. // Radio Task Initialize
  211. radio_init();
  212. // Setup Kiss Decoder and Encoder
  213. kiss_init();
  214. // Initalize BLE
  215. bt_spp_init();
  216. }