main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. uint8_t APRS_TEST_PACKET[] = { 0x82, 0x98, 0x98, 0x40, 0x40, 0x40, 0xe0, 0x96, 0x84, 0x66, 0xaa, 0x96, 0xac, 0xe0, 0xae, 0x92,
  29. 0x88, 0x8a, 0x62, 0x40, 0x62, 0xae, 0x92, 0x88, 0x8a, 0x64, 0x40, 0x65, 0x03, 0xf0, 0x3a, 0x4b,
  30. 0x42, 0x33, 0x55, 0x4b, 0x56, 0x2d, 0x32, 0x20, 0x3a, 0x48, 0x69, 0x21, 0x20, 0x54, 0x68, 0x69, 0x73,
  31. 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x54, 0x65, 0x73, 0x74, 0x7b, 0x31, 0xad, 0xa1 };
  32. RingbufHandle_t radio_tx_buf;
  33. SemaphoreHandle_t xRadioRXSemaphore;
  34. //RingbufHandle_t radio_rx_buf;
  35. extern int8_t EXTERNAL_DATA;
  36. #define WINDOW_SIZE 11
  37. #define SAMPLEFREQUENCY 6000
  38. int window[WINDOW_SIZE];
  39. double goertzelFilter(int samples[], double freq, int N) {
  40. double s_prev = 0.0;
  41. double s_prev2 = 0.0;
  42. double coeff,normalizedfreq,power,s;
  43. int i;
  44. normalizedfreq = freq / SAMPLEFREQUENCY;
  45. coeff = 2*cos(2*M_PI*normalizedfreq);
  46. for (i=0; i<N; i++) {
  47. s = samples[i] + coeff * s_prev - s_prev2;
  48. s_prev2 = s_prev;
  49. s_prev = s;
  50. }
  51. power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
  52. return power;
  53. }
  54. //double calculate_symbols_energy(int samples[], int N)
  55. //{
  56. // double E_s1, E_s2;
  57. // E_s1 = goertzelFilter(samples, 1200, N);
  58. // E_s2 = goertzelFilter(samples, 2200, N);
  59. // return (E_s1 - E_s2);
  60. //}
  61. void window_init(void)
  62. {
  63. for(int i=0;i<WINDOW_SIZE;i++)
  64. {
  65. window[i] = 0;
  66. }
  67. }
  68. void window_add(int sample)
  69. {
  70. for(int i=0;i<(WINDOW_SIZE-1);i++)
  71. {
  72. window[(WINDOW_SIZE-1)-i] = window[(WINDOW_SIZE-2)-i];
  73. }
  74. window[0] = sample;
  75. }
  76. uint8_t * window_get(void)
  77. {
  78. return window;
  79. }
  80. int window_get_size(void)
  81. {
  82. return WINDOW_SIZE;
  83. }
  84. void Radio_Task(void *pvParameters)
  85. {
  86. size_t p_size;
  87. // Setup Radio
  88. xRadioRXSemaphore = xSemaphoreCreateBinary();
  89. cc1200_radio_init(APRS_RX_SETTINGS, sizeof(APRS_RX_SETTINGS)/sizeof(cc1200_reg_settings_t));
  90. cc1200_radio_frequency(144390000-6000);
  91. cc1200_radio_start_APRSRX();
  92. uint8_t toggle=0;
  93. uint8_t count = 0;
  94. double E_s1, E_s2;
  95. while(1)
  96. {
  97. // // setup LEDs for RX mode
  98. // enable_green_led();
  99. // disable_red_led();
  100. //cc1200_radio_start_APRSRX();
  101. if (xSemaphoreTake(xRadioRXSemaphore, 0) == pdTRUE)
  102. {
  103. //gpio_set_level(DEBUG_0, 1);
  104. gpio_intr_disable(CC1120_GPIO2);
  105. toggle = toggle ^ 1;
  106. gpio_set_level(DEBUG_0, toggle);
  107. //data = cc1200_radio_read_CFM();
  108. window_add((int) EXTERNAL_DATA);
  109. E_s1 = goertzelFilter(window, 1200, WINDOW_SIZE);
  110. E_s2 = goertzelFilter(window, 2200, WINDOW_SIZE);
  111. //printf("%f\n", (E_s1 - E_s2));
  112. if (E_s1 > E_s2 )
  113. {
  114. enable_green_led();
  115. disable_red_led();
  116. }
  117. else
  118. {
  119. enable_red_led();
  120. disable_green_led();
  121. }
  122. //gpio_set_level(DEBUG_0, 0);
  123. }
  124. //esp_task_wdt_reset();
  125. // Transmit Queued Packet
  126. uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, 0); // TODO: Modify to something which will check CC1200 status
  127. if (p != NULL)
  128. {
  129. // disable RX mode
  130. cc1200_radio_stop_APRSRX();
  131. // // setup LEDs for TX mode
  132. // enable_red_led();
  133. // disable_green_led();
  134. cc1200_radio_APRSTXPacket(p, p_size, 2, 0);
  135. vRingbufferReturnItem(radio_tx_buf, (void *)p);
  136. cc1200_radio_start_APRSRX();
  137. }
  138. }
  139. }
  140. //void UART_Task(void *pvParameters)
  141. //{
  142. // printf("RSSI: %x", data);
  143. //}
  144. void radio_init()
  145. {
  146. // Setup Task Communications
  147. radio_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  148. // Create Tasks
  149. xTaskCreatePinnedToCore(Radio_Task, "Radio_Task", 1024*4, 0, 1, NULL, 1);
  150. // xTaskCreatePinnedToCore(UART_Task, "UART Task", 1024*4, 0, 5, NULL, 0);
  151. }
  152. void IRAM_ATTR app_main()
  153. {
  154. // Initialize Flash
  155. esp_err_t ret = nvs_flash_init();
  156. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  157. ESP_ERROR_CHECK(nvs_flash_erase());
  158. ret = nvs_flash_init();
  159. }
  160. ESP_ERROR_CHECK( ret );
  161. // Board IO Initialize
  162. board_init();
  163. // Radio Task Initialize
  164. radio_init();
  165. // Setup Kiss Decoder and Encoder
  166. kiss_init();
  167. // Initalize BLE
  168. bt_spp_init();
  169. }