main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "aprs_decoder.h"
  30. //#include "esp_dsp.h"
  31. uint8_t APRS_TEST_PACKET[] = { 0x82, 0x98, 0x98, 0x40, 0x40, 0x40, 0xe0, 0x96, 0x84, 0x66, 0xaa, 0x96, 0xac, 0xe0, 0xae, 0x92,
  32. 0x88, 0x8a, 0x62, 0x40, 0x62, 0xae, 0x92, 0x88, 0x8a, 0x64, 0x40, 0x65, 0x03, 0xf0, 0x3a, 0x4b,
  33. 0x42, 0x33, 0x55, 0x4b, 0x56, 0x2d, 0x32, 0x20, 0x3a, 0x48, 0x69, 0x21, 0x20, 0x54, 0x68, 0x69, 0x73,
  34. 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x54, 0x65, 0x73, 0x74, 0x7b, 0x31, 0xad, 0xa1 };
  35. RingbufHandle_t radio_tx_buf;
  36. SemaphoreHandle_t xRadioRXISRSemaphore;
  37. SemaphoreHandle_t xRadioRXSemaphore;
  38. SemaphoreHandle_t xRadioTXSemaphore;
  39. //RingbufHandle_t radio_rx_buf;
  40. extern int8_t EXTERNAL_DATA;
  41. #define WINDOW_SIZE 6
  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. int* window_get(void)
  84. {
  85. return window;
  86. }
  87. int window_get_size(void)
  88. {
  89. return WINDOW_SIZE;
  90. }
  91. // APRS Decoder
  92. // Timer Functions
  93. void IRAM_ATTR rx_timer_isr(void *para)
  94. {
  95. GPIO.out_w1ts = (1 << DEBUG_0);
  96. int timer_idx = (int) para;
  97. /* Clear the interrupt
  98. and update the alarm time for the timer with without reload */
  99. TIMERG0.int_clr_timers.t0 = 1;
  100. // after the alarm has been triggered
  101. // we need enable it again, so it is triggered the next time
  102. TIMERG0.hw_timer[0].config.alarm_en = TIMER_ALARM_EN;
  103. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  104. xSemaphoreGiveFromISR(xRadioRXISRSemaphore, &xHigherPriorityTaskWoken);
  105. if (xHigherPriorityTaskWoken == pdTRUE)
  106. {
  107. portYIELD_FROM_ISR( );
  108. }
  109. GPIO.out_w1tc = (1 << DEBUG_0);
  110. }
  111. // The TX Task should have the highest
  112. void TX_Task(void *pvParameters)
  113. {
  114. size_t p_size;
  115. while(1)
  116. {
  117. // block until packet is in queue
  118. uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, portMAX_DELAY);
  119. // send packet
  120. if (p != NULL)
  121. {
  122. // disable RX mode
  123. timer_pause(TIMER_GROUP_0, TIMER_0);
  124. timer_disable_intr(TIMER_GROUP_0, TIMER_0);
  125. cc1200_radio_stop_APRSRX();
  126. // setup LEDs for TX mode
  127. enable_red_led();
  128. disable_green_led();
  129. cc1200_radio_APRSTXPacket(p, p_size, 2, 0);
  130. vRingbufferReturnItem(radio_tx_buf, (void *)p);
  131. // setup LEDs for RX mode
  132. disable_red_led();
  133. enable_green_led();
  134. cc1200_radio_start_APRSRX();
  135. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  136. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  137. timer_start(TIMER_GROUP_0, TIMER_0);
  138. }
  139. }
  140. }
  141. // Phase Locked Loop (PLL) Parameters
  142. #define BAUD_RATE (1200)
  143. #define SAMPLES_BIT (SAMPLEFREQUENCY / BAUD_RATE)
  144. #define D_PLL_INC (SAMPLES_BIT * 1)
  145. #define D_PLL_MAX (D_PLL_INC * SAMPLES_BIT *1)
  146. #define D_PLL_MARGIN (1)
  147. uint8_t aprs_buf[256];
  148. void RX_Task(void *pvParameters)
  149. {
  150. // debugging variables
  151. uint8_t toggle = 0;
  152. uint8_t count = 0;
  153. // algorithm variables
  154. double E_s1=0, E_s2=0;
  155. uint8_t raw_bit=0, previous_raw_bit=0;
  156. int32_t d_pll=0, dpll_error=0, err_div=2;
  157. int lpf_output = 0;
  158. aprs_decoder_init();
  159. frame_buffer_init(aprs_buf, 256);
  160. // Sampling Semaphore
  161. xRadioRXISRSemaphore = xSemaphoreCreateBinary();
  162. // setup LEDs for RX mode
  163. enable_green_led();
  164. disable_red_led();
  165. cc1200_radio_start_APRSRX();
  166. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  167. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  168. timer_start(TIMER_GROUP_0, TIMER_0);
  169. while(1)
  170. {
  171. if( xSemaphoreTake(xRadioRXISRSemaphore, portMAX_DELAY) == pdTRUE)
  172. {
  173. enable_debug_IO(DEBUG_0);
  174. EXTERNAL_DATA = (int)cc1200_radio_read_CFM();
  175. disable_debug_IO(DEBUG_0);
  176. enable_debug_IO(DEBUG_0);
  177. lpf_output += (50000 * (((int)EXTERNAL_DATA) - lpf_output)) / 100000;
  178. window_add((int) EXTERNAL_DATA);
  179. E_s1 = goertzelFilter(window, 1200, WINDOW_SIZE);
  180. E_s2 = goertzelFilter(window, 2200, WINDOW_SIZE);
  181. if (E_s1 > E_s2)
  182. {
  183. raw_bit = 1;
  184. }
  185. else
  186. {
  187. raw_bit = 0;
  188. }
  189. disable_debug_IO(DEBUG_0);
  190. enable_debug_IO(DEBUG_0);
  191. // DPLL for sampling
  192. if (raw_bit != previous_raw_bit)
  193. {
  194. dpll_error = d_pll - (D_PLL_MAX / 2);
  195. if (dpll_error > (D_PLL_MARGIN))
  196. {
  197. d_pll -= get_rx_status() ? D_PLL_MARGIN:
  198. (dpll_error + err_div / 2) / err_div;
  199. }
  200. else if (dpll_error < (-D_PLL_MARGIN))
  201. {
  202. d_pll += get_rx_status() ? D_PLL_MARGIN:
  203. (dpll_error + err_div / 2) / err_div;
  204. }
  205. }
  206. d_pll += D_PLL_INC;
  207. disable_debug_IO(DEBUG_0);
  208. enable_debug_IO(DEBUG_0);
  209. if (d_pll >= D_PLL_MAX)
  210. {
  211. // set clock debug I/O high
  212. // feed bit to aprs decoder algorithm
  213. switch(aprs_decoder_feed_bit(raw_bit))
  214. {
  215. case NORMAL:
  216. break;
  217. case FRAME_DECODED:
  218. ESP_LOGI("APRS RX", "AX.25 Frame Received");
  219. ESP_LOG_BUFFER_HEXDUMP("APRS RX", aprs_buf, 100, ESP_LOG_INFO);
  220. break;
  221. case ERROR_FCS_MISMATCH:
  222. ESP_LOGV("APRS RX", "AX.25 FCS Error\n");
  223. break;
  224. default:
  225. //printf("Weird Error\n");
  226. break;
  227. }
  228. d_pll -= D_PLL_MAX;
  229. }
  230. else
  231. {
  232. // set clock debug I/O low
  233. }
  234. previous_raw_bit = raw_bit;
  235. disable_debug_IO(DEBUG_0);
  236. }
  237. }
  238. }
  239. void radio_init()
  240. {
  241. // Setup Task Communications
  242. radio_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  243. xRadioRXSemaphore = xSemaphoreCreateBinary();
  244. xRadioTXSemaphore = xSemaphoreCreateBinary();
  245. // Initialize Radio
  246. cc1200_radio_init(APRS_RX2_SETTINGS, sizeof(APRS_RX2_SETTINGS)/sizeof(cc1200_reg_settings_t));
  247. cc1200_radio_frequency(144390000-6000);
  248. // Setup Sampling Timer
  249. timer_config_t config;
  250. config.divider = 2;
  251. config.counter_dir = TIMER_COUNT_UP;
  252. config.counter_en = TIMER_PAUSE;
  253. config.alarm_en = TIMER_ALARM_EN;
  254. config.intr_type = TIMER_INTR_LEVEL;
  255. config.auto_reload = TIMER_AUTORELOAD_EN;
  256. timer_init(TIMER_GROUP_0, TIMER_0, &config);
  257. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  258. timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 6666);
  259. timer_isr_register(TIMER_GROUP_0, TIMER_0, rx_timer_isr,
  260. (void *) TIMER_0, ESP_INTR_FLAG_IRAM, NULL);
  261. xTaskCreatePinnedToCore(TX_Task, "TX Task", 1024*4, 0, 2, NULL, 1);
  262. xTaskCreatePinnedToCore(RX_Task, "RX Task", 1024*4, 0, 1, NULL, 1);
  263. }
  264. void IRAM_ATTR app_main()
  265. {
  266. // Initialize Flash
  267. esp_err_t ret = nvs_flash_init();
  268. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  269. ESP_ERROR_CHECK(nvs_flash_erase());
  270. ret = nvs_flash_init();
  271. }
  272. ESP_ERROR_CHECK( ret );
  273. // Board IO Initialize
  274. board_init();
  275. // Radio Task Initialize
  276. radio_init();
  277. // Setup Kiss Decoder and Encoder
  278. kiss_init();
  279. // Initalize BLE
  280. bt_spp_init();
  281. }