main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 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. 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. // Setup Timer for TX Task
  116. timer_config_t config;
  117. config.divider = 2;
  118. config.counter_dir = TIMER_COUNT_UP;
  119. config.counter_en = TIMER_PAUSE;
  120. config.alarm_en = TIMER_ALARM_EN;
  121. config.intr_type = TIMER_INTR_LEVEL;
  122. config.auto_reload = TIMER_AUTORELOAD_EN;
  123. timer_init(TIMER_GROUP_0, TIMER_1, &config);
  124. timer_set_counter_value(TIMER_GROUP_0, TIMER_1, 0x00000000ULL);
  125. timer_set_alarm_value(TIMER_GROUP_0, TIMER_1, 3030);
  126. timer_isr_register(TIMER_GROUP_0, TIMER_1, cc1200_aprs_tx_isr,
  127. (void *) TIMER_1, ESP_INTR_FLAG_IRAM, NULL);
  128. // Setup Sampling Timer for RX Task
  129. timer_config_t rx_config;
  130. rx_config.divider = 2;
  131. rx_config.counter_dir = TIMER_COUNT_UP;
  132. rx_config.counter_en = TIMER_PAUSE;
  133. rx_config.alarm_en = TIMER_ALARM_EN;
  134. rx_config.intr_type = TIMER_INTR_LEVEL;
  135. rx_config.auto_reload = TIMER_AUTORELOAD_EN;
  136. timer_init(TIMER_GROUP_0, TIMER_0, &rx_config);
  137. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  138. timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 6666);
  139. timer_isr_register(TIMER_GROUP_0, TIMER_0, rx_timer_isr,
  140. (void *) TIMER_0, ESP_INTR_FLAG_IRAM, NULL);
  141. // Initialize Radio
  142. cc1200_radio_init(APRS_RX2_SETTINGS, sizeof(APRS_RX2_SETTINGS)/sizeof(cc1200_reg_settings_t));
  143. cc1200_radio_frequency(144390000);
  144. while(1)
  145. {
  146. // setup LEDs for RX mode
  147. disable_red_led();
  148. enable_green_led();
  149. cc1200_radio_start_APRSRX();
  150. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  151. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  152. timer_start(TIMER_GROUP_0, TIMER_0);
  153. // block until packet is in queue
  154. uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, portMAX_DELAY);
  155. // send packet
  156. if (p != NULL)
  157. {
  158. // disable RX mode
  159. timer_disable_intr(TIMER_GROUP_0, TIMER_0);
  160. timer_pause(TIMER_GROUP_0, TIMER_0);
  161. cc1200_radio_stop_APRSRX();
  162. // setup LEDs for TX mode
  163. enable_red_led();
  164. disable_green_led();
  165. cc1200_radio_APRSTXPacket(p, p_size, 4, 1);
  166. vRingbufferReturnItem(radio_tx_buf, (void *)p);
  167. }
  168. }
  169. }
  170. // Phase Locked Loop (PLL) Parameters
  171. #define BAUD_RATE (1200)
  172. #define SAMPLES_BIT (SAMPLEFREQUENCY / BAUD_RATE)
  173. #define D_PLL_INC (SAMPLES_BIT * 1)
  174. #define D_PLL_MAX (D_PLL_INC * SAMPLES_BIT *1)
  175. #define D_PLL_MARGIN (1)
  176. uint8_t aprs_buf[256];
  177. void RX_Task(void *pvParameters)
  178. {
  179. // debugging variables
  180. uint8_t toggle = 0;
  181. uint8_t count = 0;
  182. // algorithm variables
  183. double E_s1=0, E_s2=0;
  184. uint8_t raw_bit=0, previous_raw_bit=0;
  185. int32_t d_pll=0, dpll_error=0, err_div=2;
  186. int lpf_input = 0;
  187. int lpf_output = 0;
  188. aprs_decoder_init();
  189. frame_buffer_init(aprs_buf, 256);
  190. // Sampling Semaphore
  191. xRadioRXISRSemaphore = xSemaphoreCreateBinary();
  192. while(1)
  193. {
  194. if( xSemaphoreTake(xRadioRXISRSemaphore, portMAX_DELAY) == pdTRUE)
  195. {
  196. //enable_debug_IO(DEBUG_0);
  197. EXTERNAL_DATA = (int)cc1200_radio_read_CFM();
  198. //disable_debug_IO(DEBUG_0);
  199. //enable_debug_IO(DEBUG_0);
  200. //lpf_output += (50000 * (((int)EXTERNAL_DATA) - lpf_output)) / 100000;
  201. window_add((int) EXTERNAL_DATA);
  202. E_s1 = goertzelFilter(window, 1200, WINDOW_SIZE);
  203. E_s2 = goertzelFilter(window, 2200, WINDOW_SIZE);
  204. if (E_s1 > E_s2)
  205. {
  206. raw_bit = 1;
  207. enable_debug_IO(DEBUG_2);
  208. enable_debug_IO(DEBUG_0);
  209. }
  210. else
  211. {
  212. raw_bit = 0;
  213. disable_debug_IO(DEBUG_2);
  214. disable_debug_IO(DEBUG_0);
  215. }
  216. //disable_debug_IO(DEBUG_0);
  217. //enable_debug_IO(DEBUG_0);
  218. // DPLL for sampling
  219. if (raw_bit != previous_raw_bit)
  220. {
  221. dpll_error = d_pll - (D_PLL_MAX / 2);
  222. if (dpll_error > (D_PLL_MARGIN))
  223. {
  224. d_pll -= get_rx_status() ? D_PLL_MARGIN:
  225. (dpll_error + err_div / 2) / err_div;
  226. }
  227. else if (dpll_error < (-D_PLL_MARGIN))
  228. {
  229. d_pll += get_rx_status() ? D_PLL_MARGIN:
  230. (dpll_error + err_div / 2) / err_div;
  231. }
  232. }
  233. d_pll += D_PLL_INC;
  234. disable_debug_IO(DEBUG_3);
  235. if (d_pll >= D_PLL_MAX)
  236. {
  237. // set clock debug I/O high
  238. enable_debug_IO(DEBUG_3);
  239. // feed bit to aprs decoder algorithm
  240. switch(aprs_decoder_feed_bit(raw_bit))
  241. {
  242. case NORMAL:
  243. break;
  244. case FRAME_DECODED:
  245. ESP_LOGI("APRS RX", "AX.25 Frame Received");
  246. // send via KISS TNC to over BLE SPP
  247. //ESP_LOG_BUFFER_HEXDUMP("APRS RX", aprs_buf, 100, ESP_LOG_INFO);
  248. break;
  249. case ERROR_FCS_MISMATCH:
  250. ESP_LOGV("APRS RX", "AX.25 FCS Error\n");
  251. break;
  252. default:
  253. //printf("Weird Error\n");
  254. break;
  255. }
  256. d_pll -= D_PLL_MAX;
  257. }
  258. else
  259. {
  260. // set clock debug I/O low
  261. }
  262. previous_raw_bit = raw_bit;
  263. //disable_debug_IO(DEBUG_0);
  264. }
  265. }
  266. }
  267. void radio_init()
  268. {
  269. // Setup Task Communications
  270. radio_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  271. xRadioRXSemaphore = xSemaphoreCreateBinary();
  272. xRadioTXSemaphore = xSemaphoreCreateBinary();
  273. xTaskCreatePinnedToCore(TX_Task, "TX Task", 1024*4, 0, 2, NULL, 1);
  274. xTaskCreatePinnedToCore(RX_Task, "RX Task", 1024*4, 0, 1, NULL, 1);
  275. }
  276. void IRAM_ATTR app_main()
  277. {
  278. // Initialize Flash
  279. esp_err_t ret = nvs_flash_init();
  280. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  281. ESP_ERROR_CHECK(nvs_flash_erase());
  282. ret = nvs_flash_init();
  283. }
  284. ESP_ERROR_CHECK( ret );
  285. // Board IO Initialize
  286. board_init();
  287. // Radio Task Initialize
  288. radio_init();
  289. // Setup Kiss Decoder and Encoder
  290. kiss_init();
  291. // Initalize BLE
  292. bt_spp_init();
  293. }