main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "kiss.h"
  31. #include "afsk_demodulator.h"
  32. //#include "esp_dsp.h"
  33. uint8_t APRS_TEST_PACKET[] = { 0x82, 0x98, 0x98, 0x40, 0x40, 0x40, 0xe0, 0x96, 0x84, 0x66, 0xaa, 0x96, 0xac, 0xe0, 0xae, 0x92,
  34. 0x88, 0x8a, 0x62, 0x40, 0x62, 0xae, 0x92, 0x88, 0x8a, 0x64, 0x40, 0x65, 0x03, 0xf0, 0x3a, 0x4b,
  35. 0x42, 0x33, 0x55, 0x4b, 0x56, 0x2d, 0x32, 0x20, 0x3a, 0x48, 0x69, 0x21, 0x20, 0x54, 0x68, 0x69, 0x73,
  36. 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x54, 0x65, 0x73, 0x74, 0x7b, 0x31, 0xad, 0xa1 };
  37. RingbufHandle_t radio_tx_buf;
  38. RingbufHandle_t radio_rx_buf;
  39. SemaphoreHandle_t xRadioRXISRSemaphore;
  40. SemaphoreHandle_t xRadioRXSemaphore;
  41. SemaphoreHandle_t xRadioTXSemaphore;
  42. SemaphoreHandle_t xRadioMutex;
  43. //RingbufHandle_t radio_rx_buf;
  44. extern int8_t EXTERNAL_DATA;
  45. extern decoder_varibles_t v;
  46. // Timer Functions
  47. void IRAM_ATTR rx_timer_isr(void *para)
  48. {
  49. //GPIO.out_w1ts = (1 << DEBUG_0);
  50. int timer_idx = (int) para;
  51. /* Clear the interrupt
  52. and update the alarm time for the timer with without reload */
  53. TIMERG0.int_clr_timers.t0 = 1;
  54. // after the alarm has been triggered
  55. // we need enable it again, so it is triggered the next time
  56. TIMERG0.hw_timer[0].config.alarm_en = TIMER_ALARM_EN;
  57. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  58. xSemaphoreGiveFromISR(xRadioRXISRSemaphore, &xHigherPriorityTaskWoken);
  59. if (xHigherPriorityTaskWoken == pdTRUE)
  60. {
  61. portYIELD_FROM_ISR( );
  62. }
  63. //GPIO.out_w1tc = (1 << DEBUG_0);
  64. }
  65. void Radio_Task(void *pvParameters)
  66. {
  67. size_t p_size;
  68. // Setup Timer for TX Task
  69. timer_config_t config;
  70. config.divider = 2;
  71. config.counter_dir = TIMER_COUNT_UP;
  72. config.counter_en = TIMER_PAUSE;
  73. config.alarm_en = TIMER_ALARM_EN;
  74. config.intr_type = TIMER_INTR_LEVEL;
  75. config.auto_reload = TIMER_AUTORELOAD_EN;
  76. timer_init(TIMER_GROUP_0, TIMER_1, &config);
  77. timer_set_counter_value(TIMER_GROUP_0, TIMER_1, 0x00000000ULL);
  78. timer_set_alarm_value(TIMER_GROUP_0, TIMER_1, 3030);
  79. timer_isr_register(TIMER_GROUP_0, TIMER_1, cc1200_aprs_tx_isr,
  80. (void *) TIMER_1, ESP_INTR_FLAG_IRAM, NULL);
  81. // Setup Sampling Timer for RX Task
  82. timer_config_t rx_config;
  83. rx_config.divider = 2;
  84. rx_config.counter_dir = TIMER_COUNT_UP;
  85. rx_config.counter_en = TIMER_PAUSE;
  86. rx_config.alarm_en = TIMER_ALARM_EN;
  87. rx_config.intr_type = TIMER_INTR_LEVEL;
  88. rx_config.auto_reload = TIMER_AUTORELOAD_EN;
  89. timer_init(TIMER_GROUP_0, TIMER_0, &rx_config);
  90. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  91. timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 6666);
  92. timer_isr_register(TIMER_GROUP_0, TIMER_0, rx_timer_isr,
  93. (void *) TIMER_0, ESP_INTR_FLAG_IRAM, NULL);
  94. // Initialize Radio
  95. cc1200_radio_init(APRS_RX2_SETTINGS, sizeof(APRS_RX2_SETTINGS)/sizeof(cc1200_reg_settings_t));
  96. cc1200_radio_frequency(144390000);
  97. while(1)
  98. {
  99. // setup LEDs for RX mode
  100. disable_red_led();
  101. enable_green_led();
  102. cc1200_radio_start_APRSRX();
  103. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  104. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  105. timer_start(TIMER_GROUP_0, TIMER_0);
  106. // block until packet is in queue
  107. uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, portMAX_DELAY);
  108. // send packet
  109. if (p != NULL)
  110. {
  111. // disable RX mode
  112. timer_disable_intr(TIMER_GROUP_0, TIMER_0);
  113. timer_pause(TIMER_GROUP_0, TIMER_0);
  114. cc1200_radio_stop_APRSRX();
  115. // setup LEDs for TX mode
  116. enable_red_led();
  117. disable_green_led();
  118. cc1200_radio_APRSTXPacket(p, p_size, 4, 1);
  119. vRingbufferReturnItem(radio_tx_buf, (void *)p);
  120. }
  121. }
  122. }
  123. // Phase Locked Loop (PLL) Parameters
  124. #define BAUD_RATE (1200)
  125. #define SAMPLES_BIT (SAMPLEFREQUENCY / BAUD_RATE)
  126. #define D_PLL_INC (SAMPLES_BIT * 1)
  127. #define D_PLL_MAX (D_PLL_INC * SAMPLES_BIT *1)
  128. #define D_PLL_MARGIN (dpll_margin)
  129. uint8_t aprs_buf[256];
  130. void dsps_biquad_gen_lpf_f32(float *coeffs, float f, float qFactor)
  131. {
  132. if (qFactor <= 0.0001) {
  133. qFactor = 0.0001;
  134. }
  135. float Fs = 1;
  136. float w0 = 2 * M_PI * f / Fs;
  137. float c = cosf(w0);
  138. float s = sinf(w0);
  139. float alpha = s / (2 * qFactor);
  140. float b0 = (1 - c) / 2;
  141. float b1 = 1 - c;
  142. float b2 = b0;
  143. float a0 = 1 + alpha;
  144. float a1 = -2 * c;
  145. float a2 = 1 - alpha;
  146. coeffs[0] = b0 / a0;
  147. coeffs[1] = b1 / a0;
  148. coeffs[2] = b2 / a0;
  149. coeffs[3] = a1 / a0;
  150. coeffs[4] = a2 / a0;
  151. return;
  152. }
  153. void dsps_biquad_f32_ansi(const float *input, float *output, int len, float *coef, float *w)
  154. {
  155. for (int i = 0 ; i < len ; i++) {
  156. float d0 = input[i] - coef[3] * w[0] - coef[4] * w[1];
  157. output[i] = coef[0] * d0 + coef[1] * w[0] + coef[2] * w[1];
  158. w[1] = w[0];
  159. w[0] = d0;
  160. }
  161. return;
  162. }
  163. float lpf_coef[5];
  164. float lpf_delay[2];
  165. void lpf_init(void)
  166. {
  167. // generate filter coef
  168. dsps_biquad_gen_lpf_f32(lpf_coef, 0.1999, 1);
  169. // setup delay line and lpf_input window
  170. int i;
  171. for(i=0;i<sizeof(lpf_delay)/sizeof(float);i++)
  172. {
  173. lpf_delay[i] = 0;
  174. }
  175. }
  176. float lpf_baseband(float input)
  177. {
  178. float output;
  179. dsps_biquad_f32_ansi(&input, &output, 1, lpf_coef, lpf_delay);
  180. return output;
  181. }
  182. void RX_DSP_Task(void *pvParameters)
  183. {
  184. // algorithm variables
  185. volatile float E_s1=0, E_s2=0, lpf_output;
  186. uint8_t raw_bit=0, previous_raw_bit=0;
  187. volatile int32_t d_pll=0, dpll_error=0, err_div=0, dpll_margin = 1;
  188. uint32_t success = 0;
  189. aprs_decoder_init();
  190. frame_buffer_init(aprs_buf, 256);
  191. lpf_init();
  192. // Sampling Semaphore
  193. xRadioRXISRSemaphore = xSemaphoreCreateBinary();
  194. while(1)
  195. {
  196. if( xSemaphoreTake(xRadioRXISRSemaphore, portMAX_DELAY) == pdTRUE)
  197. {
  198. //enable_debug_IO(DEBUG_0);
  199. if (xSemaphoreTake(xRadioMutex, 0) == pdTRUE)
  200. {
  201. //disable_debug_IO(DEBUG_0);
  202. //enable_debug_IO(DEBUG_0);
  203. EXTERNAL_DATA = (int)cc1200_radio_read_CFM();
  204. xSemaphoreGive(xRadioMutex);
  205. //disable_debug_IO(DEBUG_0);
  206. }
  207. else
  208. {
  209. ESP_LOGE("Radio", "Sampling Failure");
  210. }
  211. //disable_debug_IO(DEBUG_0);
  212. //enable_debug_IO(DEBUG_0);
  213. window_add(EXTERNAL_DATA);
  214. int8_t *window = window_get();
  215. E_s1 = goertzelFilter2(window, 1200, WINDOW_SIZE);
  216. E_s2 = goertzelFilter2(window, 2200, WINDOW_SIZE);
  217. lpf_output = lpf_baseband((E_s1 - E_s2));
  218. if (lpf_output > 0)
  219. {
  220. raw_bit = 1;
  221. enable_debug_IO(DEBUG_2);
  222. }
  223. else
  224. {
  225. raw_bit = 0;
  226. disable_debug_IO(DEBUG_2);
  227. }
  228. //disable_debug_IO(DEBUG_0);
  229. //enable_debug_IO(DEBUG_0);
  230. // DPLL for sampling
  231. if (raw_bit != previous_raw_bit)
  232. {
  233. dpll_error = d_pll - (D_PLL_MAX / 2);
  234. if (dpll_error > (D_PLL_MARGIN))
  235. {
  236. d_pll -= get_rx_status() ? D_PLL_MARGIN:
  237. (err_div + dpll_error);//(dpll_error + err_div / 2) / err_div;
  238. }
  239. else if (dpll_error < (-D_PLL_MARGIN))
  240. {
  241. d_pll += get_rx_status() ? D_PLL_MARGIN:
  242. -(err_div + dpll_error); //(dpll_error + err_div / 2) / err_div;
  243. }
  244. }
  245. d_pll += D_PLL_INC;
  246. if (d_pll >= D_PLL_MAX)
  247. {
  248. // set clock debug I/O high
  249. enable_debug_IO(DEBUG_0);
  250. // feed bit to aprs decoder algorithm
  251. switch(aprs_decoder_feed_bit(raw_bit))
  252. {
  253. case NORMAL:
  254. break;
  255. case FRAME_DECODED:
  256. ESP_LOGI("APRS RX", "AX.25 Frame Received [%d]", success++);
  257. // send via KISS TNC to over BLE SPP
  258. //ESP_LOG_BUFFER_HEXDUMP("APRS RX", aprs_buf, 100, ESP_LOG_INFO);
  259. kiss_transmit(KISS_DATAFRAME, v.frame_buffer, v.frame_len);
  260. break;
  261. case ERROR_FCS_MISMATCH:
  262. ESP_LOGV("APRS RX", "AX.25 FCS Error\n");
  263. break;
  264. default:
  265. //printf("Weird Error\n");
  266. break;
  267. }
  268. d_pll -= D_PLL_MAX;
  269. }
  270. else
  271. {
  272. // set clock debug I/O low
  273. disable_debug_IO(DEBUG_0);
  274. }
  275. previous_raw_bit = raw_bit;
  276. }
  277. }
  278. }
  279. void radio_init()
  280. {
  281. // Setup Task Communications
  282. radio_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  283. radio_rx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  284. xRadioRXSemaphore = xSemaphoreCreateBinary();
  285. xRadioTXSemaphore = xSemaphoreCreateBinary();
  286. xRadioMutex = xSemaphoreCreateMutex();
  287. xTaskCreatePinnedToCore(Radio_Task, "Radio Controller", 1024*4, 0, 2, NULL, 1);
  288. xTaskCreatePinnedToCore(RX_DSP_Task, "Radio DSP", 1024*4, 0, 1, NULL, 1);
  289. }
  290. void IRAM_ATTR app_main()
  291. {
  292. // Initialize Flash
  293. esp_err_t ret = nvs_flash_init();
  294. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  295. ESP_ERROR_CHECK(nvs_flash_erase());
  296. ret = nvs_flash_init();
  297. }
  298. ESP_ERROR_CHECK( ret );
  299. // Board IO Initialize
  300. board_init();
  301. // Radio Task Initialize
  302. radio_init();
  303. // AFSK Decoder and Encoder
  304. // Kiss Decoder and Encoder
  305. kiss_init();
  306. // BLE and SPP
  307. bt_spp_init();
  308. }