main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Project: Arrow
  3. * Author: curiousmuch
  4. * Description: This project is an APRS transceiver based on the ESP32 and CC1200.
  5. * TODO: Refactor Code to isolate into seperate components
  6. * TODO: Isolate BT_SPP.C functionality
  7. * TODO: Transfer DSP functions to APRS folder
  8. * TODO: Abstract includes to be nicer (#include aprs.h) for example
  9. */
  10. /* Standard Includes */
  11. #include <stdio.h>
  12. #include <stddef.h>
  13. /* ESP-IDF */
  14. #include "sdkconfig.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "freertos/semphr.h"
  18. #include "freertos/ringbuf.h"
  19. #include "nvs.h"
  20. #include "nvs_flash.h"
  21. #include "esp_log.h"
  22. /* Application Specific */
  23. #include "bt_spp.h"
  24. #include "board.h"
  25. #include "radio.h"
  26. #include "kiss.h"
  27. #include "fcs_calc.h"
  28. /* Data Structures */
  29. typedef enum {
  30. TNC_TX=0,
  31. TNC_RX,
  32. TNC_IDLE
  33. } tnc_status_t;
  34. typedef struct {
  35. uint8_t tnc_number;
  36. uint8_t tx_delay;
  37. uint8_t P;
  38. uint8_t slot_time;
  39. uint8_t full_duplex;
  40. uint8_t tx_tail;
  41. uint8_t frequency_index;
  42. uint8_t mode_index;
  43. uint32_t freq;
  44. radio_config_t mode;
  45. tnc_status_t status;
  46. TaskHandle_t tnc_task;
  47. } tnc_settings_t;
  48. /* Public Variables */
  49. static tnc_settings_t tnc_settings;
  50. static SemaphoreHandle_t xRadioTXSemaphore;
  51. uint8_t ax25_rx_buf[1024];
  52. RingbufHandle_t ax25_tx_buf;
  53. uint32_t freq_table[16] = { 144390000, // APRS - North America
  54. 145512500, // Arrow Test Freqs...
  55. 145537500,
  56. 145562500,
  57. 145587500,
  58. 145612500,
  59. 145637500,
  60. 145662500,
  61. 145687500,
  62. 145712500,
  63. 145737500,
  64. 145762500,
  65. 145787500,
  66. 145812500,
  67. 145837500, // ...145.5 to 145.8 is experimential band
  68. 145862500 }; // 25kHz spacing
  69. /* Radio Callback Functions */
  70. void radio_rx_ax25_cb(uint8_t *frame, uint32_t len)
  71. {
  72. // TODO: move kiss processing to other core
  73. kiss_transmit(KISS_DATAFRAME, frame, len);
  74. }
  75. void radio_tx_ax25_cb(void)
  76. {
  77. // unlock scheduling task to allow next packet to be sent
  78. xSemaphoreGive(xRadioTXSemaphore);
  79. }
  80. /* KISS callback Functions */
  81. void kiss_tx_cb(uint8_t *frame, uint32_t len)
  82. {
  83. // TODO: move function to other core
  84. // TODO: Add functionality which indicates if BLE connection is present or not
  85. // send data to UART via bluetooth
  86. bt_spp_tx(frame, len);
  87. ESP_LOGI("tnc", "kiss packet sent");
  88. }
  89. void print_tnc_settings(void)
  90. {
  91. ESP_LOGI("tnc", "TX Delay: %d", tnc_settings.tx_delay*10);
  92. ESP_LOGI("tnc", "TX Tail: %d", tnc_settings.tx_tail*10);
  93. ESP_LOGI("tnc", "Slot Time: %d", tnc_settings.slot_time*10);
  94. ESP_LOGI("tnc", "P: %d", tnc_settings.P);
  95. ESP_LOGI("tnc", "Frequency: %d", tnc_settings.freq);
  96. }
  97. void kiss_rx_cb(uint8_t *frame, uint32_t len)
  98. {
  99. uint8_t data_byte;
  100. data_byte = frame[0] & 0x0F; // remove tnc_number information
  101. // process dataframe
  102. if (data_byte == KISS_DATAFRAME)
  103. {
  104. ESP_LOGI("tnc", "kiss packet received");
  105. // feed packet to ring buffer
  106. uint32_t fcs = fcs_calc(&frame[1], len-1);
  107. frame[len] = fcs & 0xFF;
  108. frame[len+1] = fcs>>8 & 0xFF;
  109. if (xRingbufferSend(ax25_tx_buf, &frame[1], ((len+1)*sizeof(uint8_t)),10/portTICK_PERIOD_MS) == pdFALSE)
  110. {
  111. ESP_LOGE("tnc", "packet buffer overflow");
  112. }
  113. }
  114. else // assume command
  115. {
  116. ESP_LOGI("tnc", "received tnc command");
  117. switch(data_byte)
  118. {
  119. case KISS_CMD_TXDELAY:
  120. tnc_settings.tx_delay = frame[1];
  121. break;
  122. case KISS_CMD_P:
  123. tnc_settings.P = frame[1];
  124. break;
  125. case KISS_CMD_SLOTTIME:
  126. tnc_settings.slot_time = frame[1];
  127. break;
  128. case KISS_CMD_TXTAIL:
  129. tnc_settings.tx_tail = frame[1];
  130. break;
  131. case KISS_CMD_FULLDUPLEX:
  132. tnc_settings.full_duplex = frame[1];
  133. break;
  134. case KISS_CMD_SETHARDWARE:
  135. {
  136. uint8_t freq_index = frame[1] & 0x0F;
  137. tnc_settings.freq = freq_table[freq_index];
  138. break;
  139. }
  140. default:
  141. ESP_LOGE("tnc", "unknown data byte for KISS protocol");
  142. }
  143. // wait for radio to finish transmitting
  144. ESP_LOGW("tnc", "waiting to disable tnc...");
  145. while(tnc_settings.status != TNC_RX)
  146. {
  147. vTaskDelay(100 / portTICK_PERIOD_MS); // wait 100ms and try again
  148. }
  149. print_tnc_settings();
  150. // disable TNC task
  151. vTaskSuspend(tnc_settings.tnc_task);
  152. ESP_LOGW("tnc", "disabled tnc");
  153. // re-initalize radio
  154. ax25_param_t ax25_param;
  155. ax25_param.tx_tail = tnc_settings.tx_tail;
  156. ax25_param.tx_delay = tnc_settings.tx_delay;
  157. ax25_param.sample_rate = 13200;
  158. ax25_param.symbol0_freq = 1200;
  159. ax25_param.symbol1_freq = 2200;
  160. ax25_param.rx_cb = radio_rx_ax25_cb;
  161. ax25_param.tx_cb = radio_tx_ax25_cb;
  162. ax25_param.rx_buf = ax25_rx_buf;
  163. ax25_param.rx_buf_len = sizeof(ax25_rx_buf) / sizeof(ax25_rx_buf[0]);
  164. ax25_param.cpu_core = 1;
  165. radio_reinit(AX25, &ax25_param);
  166. radio_set_frequency(tnc_settings.freq);
  167. // set radio back to RX
  168. radio_packet_rx(NULL);
  169. // resume TNC task
  170. vTaskResume(tnc_settings.tnc_task);
  171. ESP_LOGI("tnc", "re-enabled tnc");
  172. }
  173. }
  174. /* TNC Tasks */
  175. void tnc_task(void *para)
  176. {
  177. uint8_t *packet;
  178. size_t packet_size;
  179. uint8_t P;
  180. radio_packet_rx(NULL);
  181. tnc_settings.status = TNC_RX;
  182. while(1)
  183. {
  184. // indicate RX
  185. enable_green_led();
  186. disable_red_led();
  187. // check ring buffer for packet
  188. packet = xRingbufferReceive(ax25_tx_buf, &packet_size, portMAX_DELAY);
  189. ESP_LOGI("tnc", "scheduling transmission");
  190. if (packet != NULL)
  191. {
  192. // wait based on persistance algorithm
  193. while (1)
  194. {
  195. // TODO: carrier detect is causing a crash somehow when it gets locked on
  196. // to high. I'm confused as the unit should continue to run despite this...
  197. if (radio_get_cs() == 0)
  198. {
  199. ESP_LOGV("radio", "carrier not detected");
  200. P = (uint8_t) esp_random();
  201. if (P <= tnc_settings.P)
  202. {
  203. ESP_LOGI("radio", "transmission scheduled");
  204. break;
  205. }
  206. else
  207. {
  208. ESP_LOGV("radio", "transmission postponed");
  209. vTaskDelay((tnc_settings.slot_time * 10) / portTICK_PERIOD_MS);
  210. }
  211. }
  212. else
  213. {
  214. ESP_LOGV("radio", "carrier detected");
  215. vTaskDelay(10 / portTICK_PERIOD_MS); // re-check in 10ms
  216. }
  217. }
  218. // indicate TX
  219. enable_red_led();
  220. disable_green_led();
  221. // TODO: modify code so the full TX DELAY isn't used when multiple packets are sent.
  222. // dump all the packets in the buffer
  223. while(packet != NULL)
  224. {
  225. // setup packet to be transmitted
  226. tnc_settings.status = TNC_TX;
  227. radio_packet_tx(packet, packet_size, NULL);
  228. // sleep until packet is finished being sent
  229. xSemaphoreTake(xRadioTXSemaphore, portMAX_DELAY);
  230. tnc_settings.status = TNC_IDLE;
  231. // remove packet from ring buffer and load next one if it exists
  232. vRingbufferReturnItem(ax25_tx_buf, (void *)packet);
  233. packet = xRingbufferReceive(ax25_tx_buf, &packet_size, 0);
  234. }
  235. }
  236. else
  237. {
  238. ESP_LOGE("tnc", "buffer error");
  239. }
  240. // setup again for reception
  241. radio_packet_rx(NULL);
  242. tnc_settings.status = TNC_RX;
  243. }
  244. }
  245. void IRAM_ATTR app_main()
  246. {
  247. // Inter-Task Communication
  248. ax25_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  249. xRadioTXSemaphore = xSemaphoreCreateBinary();
  250. // Initialize Flash
  251. esp_err_t ret = nvs_flash_init();
  252. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  253. ESP_ERROR_CHECK(nvs_flash_erase());
  254. ret = nvs_flash_init();
  255. }
  256. ESP_ERROR_CHECK( ret );
  257. // Board IO Initialize
  258. board_init();
  259. // Load default TNC settings
  260. tnc_settings.P = 63;
  261. tnc_settings.tx_tail = 0; // 10ms units
  262. tnc_settings.tx_delay = 10; // 10ms units
  263. tnc_settings.P = 63;
  264. tnc_settings.slot_time = 10; // 10ms units
  265. tnc_settings.tnc_number = 0;
  266. tnc_settings.status = TNC_IDLE;
  267. tnc_settings.freq = 144390000;
  268. tnc_settings.mode = AX25;
  269. // Radio Initialize
  270. ax25_param_t ax25_param;
  271. ax25_param.tx_tail = tnc_settings.tx_tail;
  272. ax25_param.tx_delay = tnc_settings.tx_delay;
  273. ax25_param.sample_rate = 13200;
  274. ax25_param.symbol0_freq = 1200;
  275. ax25_param.symbol1_freq = 2200;
  276. ax25_param.rx_cb = radio_rx_ax25_cb;
  277. ax25_param.tx_cb = radio_tx_ax25_cb;
  278. ax25_param.rx_buf = ax25_rx_buf;
  279. ax25_param.rx_buf_len = sizeof(ax25_rx_buf) / sizeof(ax25_rx_buf[0]);
  280. ax25_param.cpu_core = 1;
  281. // TODO: setup switch statement for settings parameters or simplify....
  282. print_tnc_settings();
  283. radio_init(AX25, &ax25_param);
  284. radio_set_frequency(tnc_settings.freq);
  285. // Kiss Decoder and Encoder
  286. kiss_init(tnc_settings.tnc_number, kiss_tx_cb, kiss_rx_cb);
  287. // BLE and SPP
  288. bt_spp_init();
  289. // Tasks
  290. xTaskCreatePinnedToCore(tnc_task, "tnc task", 1024*4, 0, 2, &tnc_settings.tnc_task, 1);
  291. }