radio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * radio.c
  3. *
  4. * Created on: Oct 11, 2019
  5. * Author: curiousmuch
  6. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <math.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/semphr.h"
  13. #include "sdkconfig.h"
  14. #include "esp_log.h"
  15. #include "driver/timer.h"
  16. #include "radio.h"
  17. #include "cc1200.h"
  18. #include "cc1200_protocol.h"
  19. #include "afsk_modulator.h"
  20. #include "afsk_demodulator.h"
  21. #include "aprs_decoder.h"
  22. #include "aprs_encoder.h"
  23. /* Debugging Tag */
  24. #define RADIO_TAG "radio"
  25. /* Data Structures */
  26. typedef enum {
  27. RADIO_TIMER_NO_CONFIG = 0,
  28. RADIO_TIMER_TX,
  29. RADIO_TIMER_RX,
  30. } timer_radio_semaphore_t;
  31. /* Local Global Variables */
  32. static radio_param_t radio_param = { .type = NOT_CONFIGURED,
  33. .status = RADIO_IDLE,
  34. .rx_cb = NULL,
  35. .tx_cb = NULL };
  36. static SemaphoreHandle_t xRadioTXSemaphore;
  37. static SemaphoreHandle_t xRadioRXSemaphore;
  38. static SemaphoreHandle_t xRadioMutex;
  39. static timer_radio_semaphore_t radio_semaphore;
  40. #define DEBUG_0 16
  41. #define DEBUG_1 4
  42. #define DEBUG_2 32
  43. #define DEBUG_3 33
  44. /* Private Functions */
  45. void IRAM_ATTR radio_timer_isr(void *param)
  46. {
  47. GPIO.out_w1ts = (1 << DEBUG_0);
  48. int timer_idx = (int) param; // cast to int for timer index
  49. TIMERG0.int_clr_timers.t0 = 1; // clear interrupt
  50. TIMERG0.hw_timer[0].config.alarm_en = TIMER_ALARM_EN; // re-enable timer alarm
  51. // provide unblocking semaphore
  52. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  53. switch(radio_semaphore) {
  54. case RADIO_TIMER_NO_CONFIG:
  55. //ESP_LOGE(RADIO_TAG, "no configuration");
  56. break;
  57. case RADIO_TIMER_RX:
  58. xSemaphoreGiveFromISR(xRadioRXSemaphore, &xHigherPriorityTaskWoken);
  59. break;
  60. case RADIO_TIMER_TX:
  61. xSemaphoreGiveFromISR(xRadioTXSemaphore, &xHigherPriorityTaskWoken);
  62. break;
  63. default:
  64. //ESP_LOGE(RADIO_TAG, "invalid configuration");
  65. break;
  66. }
  67. // wake higher priority task if woken
  68. if (xHigherPriorityTaskWoken == pdTRUE)
  69. {
  70. portYIELD_FROM_ISR();
  71. }
  72. GPIO.out_w1tc = (1 << DEBUG_0);
  73. }
  74. #define RADIO_TIMER_GROUP TIMER_GROUP_0
  75. #define RADIO_TIMER TIMER_0
  76. void timer_radio_init(uint32_t sample_rate, void *timer_isr)
  77. {
  78. // setup timer settings
  79. timer_config_t timer_config;
  80. timer_config.divider = 2;
  81. timer_config.counter_dir = TIMER_COUNT_UP;
  82. timer_config.counter_en = TIMER_PAUSE;
  83. timer_config.alarm_en = TIMER_ALARM_EN;
  84. timer_config.intr_type = TIMER_INTR_LEVEL;
  85. timer_config.auto_reload = TIMER_AUTORELOAD_EN;
  86. // calculate value for timer alarm
  87. // TODO: confirm timer alarm value calculation
  88. uint32_t timer_alarm;
  89. timer_alarm = (uint32_t)(40000000 / sample_rate);
  90. // load timer settings
  91. timer_init(RADIO_TIMER_GROUP, RADIO_TIMER, &timer_config);
  92. timer_set_counter_value(RADIO_TIMER_GROUP, RADIO_TIMER, 0x00000000ULL);
  93. timer_set_alarm_value(RADIO_TIMER_GROUP, RADIO_TIMER, 3030);
  94. timer_isr_register(RADIO_TIMER_GROUP, RADIO_TIMER, timer_isr,
  95. (void *) RADIO_TIMER, ESP_INTR_FLAG_IRAM, NULL);
  96. // setup timer ISR semaphore type
  97. radio_semaphore = RADIO_TIMER_NO_CONFIG;
  98. }
  99. void timer_radio_start(timer_radio_semaphore_t type)
  100. {
  101. // setup timer ISR semaphore type
  102. radio_semaphore = type;
  103. // start timer
  104. timer_enable_intr(RADIO_TIMER_GROUP, RADIO_TIMER);
  105. timer_set_counter_value(RADIO_TIMER_GROUP, RADIO_TIMER, 0x00000000ULL);
  106. timer_start(RADIO_TIMER_GROUP, RADIO_TIMER);
  107. }
  108. void timer_radio_stop(void)
  109. {
  110. // setup timer ISR semaphore type
  111. radio_semaphore = RADIO_TIMER_NO_CONFIG;
  112. // stop timer
  113. timer_disable_intr(RADIO_TIMER_GROUP, RADIO_TIMER);
  114. timer_pause(RADIO_TIMER_GROUP, RADIO_TIMER);
  115. }
  116. /* HAL Layer */
  117. void radio_rx_task(void *para);
  118. void radio_idle(void)
  119. {
  120. // disable all radio tasks
  121. //vTaskSuspend(radio_rx_task);
  122. // disable all radio timers
  123. timer_radio_stop();
  124. cc1200_radio_idle();
  125. radio_param.status = RADIO_IDLE;
  126. }
  127. void radio_tx(void)
  128. {
  129. if (radio_param.status != RADIO_IDLE)
  130. radio_idle();
  131. cc1200_radio_tx();
  132. radio_param.status = RADIO_TX;
  133. }
  134. void radio_rx(void)
  135. {
  136. if (radio_param.status != RADIO_IDLE)
  137. radio_idle();
  138. cc1200_radio_rx();
  139. radio_param.status = RADIO_RX;
  140. }
  141. radio_status_t radio_get_status(void)
  142. {
  143. return radio_param.status;
  144. }
  145. uint8_t radio_get_cs(void)
  146. {
  147. return ax25_decoder_get_cs();
  148. }
  149. void radio_packet_tx(uint8_t *data, int32_t len, void *settings)
  150. {
  151. // configure radio for TX
  152. switch (radio_param.type) {
  153. case AX25: {
  154. ESP_LOGI(RADIO_TAG, "transmitting AX25 packet");
  155. // load ax25 encoder state machine
  156. ax25_encoder_encode(data, len);
  157. // configure radio to send CW
  158. xSemaphoreTake( xRadioMutex, portMAX_DELAY ); // lock radio
  159. radio_tx();
  160. xSemaphoreGive( xRadioMutex ); // unlock radio
  161. // start sampling timer
  162. timer_radio_start(RADIO_TIMER_TX);
  163. break;
  164. }
  165. default: {
  166. ESP_LOGE(RADIO_TAG, "invalid configuration");
  167. break;
  168. }
  169. }
  170. }
  171. void radio_set_frequency(uint32_t freq)
  172. {
  173. xSemaphoreTake( xRadioMutex, portMAX_DELAY ); // lock radio
  174. cc1200_radio_frequency(freq);
  175. xSemaphoreGive( xRadioMutex );
  176. }
  177. void radio_tx_task(void *para)
  178. {
  179. // variables for transmission
  180. uint32_t sample_count = 0;
  181. uint8_t nrzi_bit = 0;
  182. int8_t amplitude = 0;
  183. // metrics
  184. uint32_t tx_count = 0;
  185. while(1)
  186. {
  187. // block until sampling timer is running
  188. if (xSemaphoreTake(xRadioTXSemaphore, portMAX_DELAY) == pdTRUE)
  189. {
  190. if (ax25_encoder_get_status() == READY)
  191. {
  192. // reset sample count used per RF symbol
  193. sample_count = 0;
  194. // setup first amplitude for tone
  195. nrzi_bit = ax25_encoder_get_bit();
  196. amplitude = afsk_get_amplitude(nrzi_bit);
  197. }
  198. else
  199. {
  200. // process error
  201. }
  202. }
  203. // run until packet is finished
  204. while(ax25_encoder_get_status() == READY)
  205. {
  206. // block until semaphore is given or there has been a timing error
  207. if (xSemaphoreTake(xRadioTXSemaphore, portMAX_DELAY) == pdTRUE)
  208. {
  209. // update carrier for AFSK
  210. xSemaphoreTake( xRadioMutex, portMAX_DELAY ); // lock radio
  211. cc1200_radio_write_CFM(amplitude);
  212. xSemaphoreGive( xRadioMutex ); // unlock radio
  213. // increment symbol count
  214. sample_count++;
  215. if (sample_count >= 11)
  216. {
  217. GPIO.out_w1ts = (1 << DEBUG_1);
  218. nrzi_bit = ax25_encoder_get_bit();
  219. sample_count = 0;
  220. GPIO.out_w1tc = (1 << DEBUG_1);
  221. }
  222. // get amplitude for next nrzi bit
  223. amplitude = afsk_get_amplitude(nrzi_bit);
  224. }
  225. else
  226. {
  227. ESP_LOGE(RADIO_TAG, "timing error");
  228. ESP_LOGI(RADIO_TAG, "canceling transmission");
  229. break;
  230. }
  231. }
  232. // stop sampling timer
  233. timer_radio_stop();
  234. // confirm if TX failed or not
  235. // ax25_encoder will not reach DONE if their is a timing failure
  236. switch(ax25_encoder_get_status()) {
  237. case STOP:
  238. ESP_LOGI(RADIO_TAG, "transmission complete: [%d]", tx_count++);
  239. break;
  240. case READY:
  241. ESP_LOGE(RADIO_TAG, "transmission failed");
  242. break;
  243. case ERROR:
  244. ESP_LOGE(RADIO_TAG, "encoder error");
  245. break;
  246. default:
  247. ESP_LOGE(RADIO_TAG, "unknown encoder error");
  248. }
  249. // active callback function
  250. if (radio_param.tx_cb != NULL)
  251. {
  252. radio_param.tx_cb();
  253. }
  254. }
  255. }
  256. void radio_rx_task(void *para)
  257. {
  258. // analog variables
  259. int8_t cfm_value=0;
  260. // demod variables
  261. uint8_t raw_bit=0, prev_raw_bit=0;
  262. // pll variables and settings
  263. int32_t d_pll=0, dpll_error=0, err_div=0;
  264. // TODO: The following group should be made user setable fed via the settings
  265. const int32_t SAMPLE_FREQUENCY = 13200;
  266. const int32_t BAUD_RATE = 1200;
  267. const int32_t SAMPLES_BIT = SAMPLE_FREQUENCY / BAUD_RATE;
  268. const int32_t D_PLL_INC = (SAMPLES_BIT * 1);
  269. const int32_t D_PLL_MAX = (D_PLL_INC * SAMPLES_BIT * 1);
  270. const int32_t D_PLL_MARGIN = 1;
  271. // decoder variables
  272. uint32_t success=0;
  273. // task
  274. while(1)
  275. {
  276. // block until semphore is given or there has been a timing error
  277. if (xSemaphoreTake(xRadioRXSemaphore, portMAX_DELAY) == pdTRUE)
  278. {
  279. // read register analog value from radio
  280. xSemaphoreTake( xRadioMutex, portMAX_DELAY ); // lock radio
  281. cfm_value = cc1200_radio_read_CFM();
  282. xSemaphoreGive( xRadioMutex ); // unlock radio
  283. // afsk demod
  284. raw_bit = afsk_demod_add_sample(cfm_value);
  285. // clock synchronizer for sampling
  286. if (raw_bit != prev_raw_bit)
  287. {
  288. dpll_error = d_pll - (D_PLL_MAX / 2);
  289. if (dpll_error > D_PLL_MARGIN)
  290. {
  291. d_pll -= ax25_decoder_get_cs() ? D_PLL_MARGIN:
  292. (err_div + dpll_error); //(dpll_error + err_div / 2) / err_div;
  293. }
  294. else if (dpll_error < -D_PLL_MARGIN)
  295. {
  296. d_pll += ax25_decoder_get_cs() ? D_PLL_MARGIN :
  297. -(err_div + dpll_error); // (dpll_error + err_div / 2) / err_div;
  298. }
  299. }
  300. // increment clock
  301. d_pll += D_PLL_INC;
  302. // slice
  303. if (d_pll >= D_PLL_MAX)
  304. {
  305. switch (ax25_decoder_feed_bit(raw_bit))
  306. {
  307. case NORMAL:
  308. break;
  309. case FRAME_DECODED:
  310. ESP_LOGI("radio", "AX25 transmission received: [%d]", success++);
  311. // send via KISS TNC to over BLE SPP
  312. //ESP_LOG_BUFFER_HEXDUMP("APRS RX", aprs_buf, 100, ESP_LOG_INFO);
  313. //kiss_transmit(KISS_DATAFRAME, v.frame_buffer, v.frame_len);
  314. if (radio_param.rx_cb != NULL)
  315. radio_param.rx_cb(ax25_decoder_get_frame(), ax25_decoder_get_frame_len());
  316. break;
  317. case ERROR_FCS_MISMATCH:
  318. ESP_LOGV("radio", "AX25 fcs error");
  319. break;
  320. default:
  321. //printf("Weird Error\n");
  322. break;
  323. }
  324. d_pll -= D_PLL_MAX;
  325. }
  326. prev_raw_bit = raw_bit;
  327. }
  328. else
  329. {
  330. ESP_LOGE(RADIO_TAG, "rx timing error");
  331. }
  332. }
  333. }
  334. // Functionality will depend on protocol
  335. // AX.25 - Non-Block and will activate callback function
  336. void radio_packet_rx(void *settings)
  337. {
  338. switch(radio_param.type)
  339. {
  340. case AX25: {
  341. // reset decoder
  342. ax25_decoder_reset(); // this is required because sometimes it locks up if stopped
  343. // by TX Task
  344. // setup radio
  345. xSemaphoreTake( xRadioMutex, portMAX_DELAY ); // lock radio
  346. radio_rx();
  347. xSemaphoreGive( xRadioMutex ); // unlock radio
  348. // setup sampling timer
  349. timer_radio_start(RADIO_TIMER_RX);
  350. break;
  351. }
  352. default: {
  353. ESP_LOGE(RADIO_TAG, "invalid configuration");
  354. break;
  355. }
  356. }
  357. }
  358. void radio_reinit(radio_config_t type, void *settings)
  359. {
  360. // lock radio
  361. xSemaphoreTake(xRadioMutex, portMAX_DELAY);
  362. // disable radio
  363. radio_idle();
  364. // suspend radio tasks
  365. if (radio_param.type == AX25)
  366. {
  367. vTaskSuspend(radio_param.tx_task);
  368. vTaskSuspend(radio_param.rx_task);
  369. }
  370. switch(type)
  371. {
  372. case AX25:
  373. ESP_LOGI(RADIO_TAG, "APRS Mode");
  374. radio_param.type = AX25;
  375. // cast setting struct
  376. ax25_param_t *p = (ax25_param_t *)settings;
  377. // setup LUT for AFSK demodulator
  378. afsk_mod_init(p->sample_rate, p->symbol0_freq, p->symbol1_freq);
  379. // setup frequency detectors for AFSK demodulator
  380. afsk_demod_init(p->sample_rate, p->symbol0_freq, p->symbol1_freq);
  381. // setup encoder for ax25
  382. ax25_encoder_init(p->tx_delay, p->tx_tail);
  383. // setup ax25 decoder
  384. ax25_decoder_init(p->rx_buf, p->rx_buf_len);
  385. radio_param.rx_cb = p->rx_cb;
  386. radio_param.tx_cb = p->tx_cb;
  387. // load AX25_settings without reseting / reloading SPI
  388. cc1200_radio_config(AX25_SETTINGS, sizeof(AX25_SETTINGS)/sizeof(cc1200_reg_settings_t));
  389. // put chip to idle
  390. radio_idle();
  391. // resume radio tasks
  392. break;
  393. default:
  394. ESP_LOGE(RADIO_TAG, "invalid configuration");
  395. radio_param.type = NOT_CONFIGURED;
  396. break;
  397. }
  398. // resume tasks
  399. // suspend radio tasks
  400. if (radio_param.type == AX25)
  401. {
  402. vTaskResume(radio_param.tx_task);
  403. vTaskResume(radio_param.rx_task);
  404. }
  405. xSemaphoreGive(xRadioMutex);
  406. }
  407. void radio_init(radio_config_t type, void *settings)
  408. {
  409. // create mutux so SPI transactions will be multi-thread safe
  410. // TODO: fix so mutex is only created one to avoid memory leak
  411. xRadioMutex = xSemaphoreCreateMutex();
  412. // lock radio
  413. xSemaphoreTake( xRadioMutex, portMAX_DELAY );
  414. switch(type)
  415. {
  416. case AX25: {
  417. ESP_LOGI("Radio", "APRS Mode");
  418. radio_param.type = AX25;
  419. // cast setting struct
  420. ax25_param_t *p = (ax25_param_t *)settings;
  421. // setup LUT for AFSK demodulator
  422. afsk_mod_init(p->sample_rate, p->symbol0_freq, p->symbol1_freq);
  423. // setup frequency detectors for AFSK demodulator
  424. afsk_demod_init(p->sample_rate, p->symbol0_freq, p->symbol1_freq);
  425. // setup encoder for ax25
  426. ax25_encoder_init(p->tx_delay, p->tx_tail);
  427. // setup ax25 decoder
  428. ax25_decoder_init(p->rx_buf, p->rx_buf_len);
  429. radio_param.rx_cb = p->rx_cb;
  430. radio_param.tx_cb = p->tx_cb;
  431. // setup timer for TX / RX
  432. timer_radio_init(p->sample_rate, radio_timer_isr);
  433. // configure CC1200
  434. cc1200_radio_init(AX25_SETTINGS, sizeof(AX25_SETTINGS)/sizeof(cc1200_reg_settings_t));
  435. // put chip to idle
  436. radio_idle();
  437. // task communication
  438. xRadioRXSemaphore = xSemaphoreCreateBinary();
  439. xRadioTXSemaphore = xSemaphoreCreateBinary();
  440. // create task for sampling
  441. // TODO: Reduce stack requirements?
  442. xTaskCreatePinnedToCore(radio_rx_task, "radio rx", 1024*4, 0, 1, &radio_param.tx_task, p->cpu_core);
  443. xTaskCreatePinnedToCore(radio_tx_task, "radio tx", 1024*4, 0, 2, &radio_param.rx_task, p->cpu_core);
  444. //vTaskSuspend(radio_rx_task);
  445. break;
  446. }
  447. default: {
  448. ESP_LOGE(RADIO_TAG, "invalid configuration");
  449. radio_param.type = NOT_CONFIGURED;
  450. //TODO: Add assert to stop functionality
  451. break;
  452. }
  453. }
  454. // unlock radio
  455. xSemaphoreGive( xRadioMutex );
  456. }