main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. //RingbufHandle_t radio_rx_buf;
  39. extern int8_t EXTERNAL_DATA;
  40. #define WINDOW_SIZE 6
  41. #define SAMPLEFREQUENCY 6000
  42. int window[WINDOW_SIZE];
  43. int lpf_window[WINDOW_SIZE];
  44. double goertzelFilter(int samples[], double freq, int N)
  45. {
  46. double s_prev = 0.0;
  47. double s_prev2 = 0.0;
  48. double coeff,normalizedfreq,power,s;
  49. int i;
  50. normalizedfreq = freq / SAMPLEFREQUENCY;
  51. coeff = 2*cos(2*M_PI*normalizedfreq);
  52. for (i=0; i<N; i++) {
  53. s = samples[i] + coeff * s_prev - s_prev2;
  54. s_prev2 = s_prev;
  55. s_prev = s;
  56. }
  57. power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
  58. return power;
  59. }
  60. //double calculate_symbols_energy(int samples[], int N)
  61. //{
  62. // double E_s1, E_s2;
  63. // E_s1 = goertzelFilter(samples, 1200, N);
  64. // E_s2 = goertzelFilter(samples, 2200, N);
  65. // return (E_s1 - E_s2);
  66. //}
  67. void window_init(void)
  68. {
  69. for(int i=0;i<WINDOW_SIZE;i++)
  70. {
  71. window[i] = 0;
  72. }
  73. }
  74. void window_add(int sample)
  75. {
  76. for(int i=0;i<(WINDOW_SIZE-1);i++)
  77. {
  78. window[(WINDOW_SIZE-1)-i] = window[(WINDOW_SIZE-2)-i];
  79. }
  80. window[0] = sample;
  81. }
  82. uint8_t * window_get(void)
  83. {
  84. return window;
  85. }
  86. int window_get_size(void)
  87. {
  88. return WINDOW_SIZE;
  89. }
  90. // Timer Functions
  91. void IRAM_ATTR rx_timer_isr(void *para)
  92. {
  93. //GPIO.out_w1ts = (1 << DEBUG_0);
  94. int timer_idx = (int) para;
  95. /* Clear the interrupt
  96. and update the alarm time for the timer with without reload */
  97. TIMERG0.int_clr_timers.t0 = 1;
  98. // after the alarm has been triggered
  99. // we need enable it again, so it is triggered the next time
  100. TIMERG0.hw_timer[0].config.alarm_en = TIMER_ALARM_EN;
  101. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  102. xSemaphoreGiveFromISR(xRadioRXISRSemaphore, &xHigherPriorityTaskWoken);
  103. if (xHigherPriorityTaskWoken == pdTRUE)
  104. {
  105. portYIELD_FROM_ISR( );
  106. }
  107. //GPIO.out_w1tc = (1 << DEBUG_0);
  108. }
  109. // The TX Task should have the highest
  110. void TX_Task(void *pvParameters)
  111. {
  112. size_t p_size;
  113. while(1)
  114. {
  115. // block until packet is in queue
  116. uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, portMAX_DELAY);
  117. // send packet
  118. if (p != NULL)
  119. {
  120. // disable RX mode
  121. timer_pause(TIMER_GROUP_0, TIMER_0);
  122. timer_disable_intr(TIMER_GROUP_0, TIMER_0);
  123. cc1200_radio_stop_APRSRX();
  124. // setup LEDs for TX mode
  125. enable_red_led();
  126. disable_green_led();
  127. cc1200_radio_APRSTXPacket(p, p_size, 2, 0);
  128. vRingbufferReturnItem(radio_tx_buf, (void *)p);
  129. // setup LEDs for RX mode
  130. disable_red_led();
  131. enable_green_led();
  132. cc1200_radio_start_APRSRX();
  133. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  134. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  135. timer_start(TIMER_GROUP_0, TIMER_0);
  136. }
  137. }
  138. }
  139. void RX_Task(void *pvParameters)
  140. {
  141. // debugging variables
  142. uint8_t toggle = 0;
  143. uint8_t count = 0;
  144. // algorithm variables
  145. double E_s1, E_s2, result;
  146. E_s1 = 0; E_s2 = 0;
  147. int f_output = 0;
  148. // Sampling Semaphore
  149. xRadioRXISRSemaphore = xSemaphoreCreateBinary();
  150. // setup LEDs for RX mode
  151. enable_green_led();
  152. disable_red_led();
  153. cc1200_radio_start_APRSRX();
  154. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  155. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  156. timer_start(TIMER_GROUP_0, TIMER_0);
  157. while(1)
  158. {
  159. //ulNotificationValue = ulTaskNotifyTake(pdTRUE, 0);
  160. if( xSemaphoreTake(xRadioRXISRSemaphore, portMAX_DELAY) == pdTRUE)
  161. {
  162. //enable_debug_IO(DEBUG_0);
  163. EXTERNAL_DATA = (int)cc1200_radio_read_CFM();
  164. //disable_debug_IO(DEBUG_0);
  165. //enable_debug_IO(DEBUG_0);
  166. //f_output += (50000 * (((int)EXTERNAL_DATA) - f_output)) / 100000;
  167. window_add((int) EXTERNAL_DATA);
  168. E_s1 = goertzelFilter(window, 1200, WINDOW_SIZE);
  169. E_s2 = goertzelFilter(window, 2200, WINDOW_SIZE);
  170. if (E_s1 > E_s2)
  171. {
  172. enable_debug_IO(DEBUG_0);
  173. }
  174. else
  175. {
  176. disable_debug_IO(DEBUG_0);
  177. }
  178. //disable_debug_IO(DEBUG_0);
  179. }
  180. }
  181. }
  182. void radio_init()
  183. {
  184. // Setup Task Communications
  185. radio_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  186. xRadioRXSemaphore = xSemaphoreCreateBinary();
  187. xRadioTXSemaphore = xSemaphoreCreateBinary();
  188. // Initialize Radio
  189. cc1200_radio_init(APRS_RX_SETTINGS, sizeof(APRS_RX_SETTINGS)/sizeof(cc1200_reg_settings_t));
  190. cc1200_radio_frequency(144390000-6000);
  191. // Setup Sampling Timer
  192. timer_config_t config;
  193. config.divider = 2;
  194. config.counter_dir = TIMER_COUNT_UP;
  195. config.counter_en = TIMER_PAUSE;
  196. config.alarm_en = TIMER_ALARM_EN;
  197. config.intr_type = TIMER_INTR_LEVEL;
  198. config.auto_reload = TIMER_AUTORELOAD_EN;
  199. timer_init(TIMER_GROUP_0, TIMER_0, &config);
  200. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  201. timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 6666);
  202. //timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  203. timer_isr_register(TIMER_GROUP_0, TIMER_0, rx_timer_isr,
  204. (void *) TIMER_0, ESP_INTR_FLAG_IRAM, NULL);
  205. //timer_start(TIMER_GROUP_0, TIMER_0);
  206. // Create Tasks
  207. // xTaskCreatePinnedToCore(Radio_Controller_Task, "Radio_Controller", 1024*4, 0, 10, NULL, 0);
  208. xTaskCreatePinnedToCore(TX_Task, "TX Task", 1024*4, 0, 2, NULL, 1);
  209. xTaskCreatePinnedToCore(RX_Task, "RX Task", 1024*4, 0, 1, NULL, 1);
  210. // xTaskCreatePinnedToCore(UART_Task, "UART Task", 1024*4, 0, 5, NULL, 0);
  211. }
  212. void IRAM_ATTR app_main()
  213. {
  214. // Initialize Flash
  215. esp_err_t ret = nvs_flash_init();
  216. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  217. ESP_ERROR_CHECK(nvs_flash_erase());
  218. ret = nvs_flash_init();
  219. }
  220. ESP_ERROR_CHECK( ret );
  221. // Board IO Initialize
  222. board_init();
  223. // Radio Task Initialize
  224. radio_init();
  225. // Setup Kiss Decoder and Encoder
  226. kiss_init();
  227. // Initalize BLE
  228. bt_spp_init();
  229. }