main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 ax_freq_table[16] = { 144390000, // APRS - North America
  54. 145512500, // Arrow - AX25 test frequencies...
  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 experimental 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 tnc_print_settings(void)
  82. {
  83. ESP_LOGI("tnc", "TX Delay: %d", tnc_settings.tx_delay*10);
  84. ESP_LOGI("tnc", "TX Tail: %d", tnc_settings.tx_tail*10);
  85. ESP_LOGI("tnc", "Slot Time: %d", tnc_settings.slot_time*10);
  86. ESP_LOGI("tnc", "P: %d", tnc_settings.P);
  87. ESP_LOGI("tnc", "Frequency: %d", tnc_settings.freq);
  88. }
  89. void kiss_tx_cb(uint8_t *frame, uint32_t len)
  90. {
  91. // TODO: move function to other core
  92. // TODO: Add functionality which indicates if BLE connection is present or not
  93. // send data to UART via bluetooth
  94. bt_spp_tx(frame, len);
  95. ESP_LOGI("tnc", "kiss packet sent");
  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 = ax_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. // show user new settings
  150. tnc_print_settings();
  151. // disable TNC task
  152. vTaskSuspend(tnc_settings.tnc_task);
  153. ESP_LOGW("tnc", "disabled tnc");
  154. // re-initialize radio
  155. ax25_param_t ax25_param;
  156. ax25_param.tx_tail = tnc_settings.tx_tail;
  157. ax25_param.tx_delay = tnc_settings.tx_delay;
  158. ax25_param.sample_rate = 13200;
  159. ax25_param.symbol0_freq = 1200;
  160. ax25_param.symbol1_freq = 2200;
  161. ax25_param.rx_cb = radio_rx_ax25_cb;
  162. ax25_param.tx_cb = radio_tx_ax25_cb;
  163. ax25_param.rx_buf = ax25_rx_buf;
  164. ax25_param.rx_buf_len = sizeof(ax25_rx_buf) / sizeof(ax25_rx_buf[0]);
  165. ax25_param.cpu_core = 1;
  166. radio_reinit(AX25, &ax25_param);
  167. radio_set_frequency(tnc_settings.freq);
  168. // set radio back to RX
  169. radio_packet_rx(NULL);
  170. // resume TNC task
  171. vTaskResume(tnc_settings.tnc_task);
  172. ESP_LOGI("tnc", "re-enabled tnc");
  173. }
  174. }
  175. /* TNC Tasks */
  176. void tnc_task(void *para)
  177. {
  178. uint8_t *packet;
  179. size_t packet_size;
  180. uint8_t P;
  181. radio_packet_rx(NULL);
  182. tnc_settings.status = TNC_RX;
  183. while(1)
  184. {
  185. // indicate RX
  186. enable_green_led();
  187. disable_red_led();
  188. // check ring buffer for packet
  189. packet = xRingbufferReceive(ax25_tx_buf, &packet_size, portMAX_DELAY);
  190. ESP_LOGI("tnc", "scheduling transmission");
  191. if (packet != NULL)
  192. {
  193. // wait based on persistance algorithm
  194. while (1)
  195. {
  196. // TODO: carrier detect is causing a crash somehow when it gets locked on
  197. // to high. I'm confused as the unit should continue to run despite this...
  198. if (radio_get_cs() == 0)
  199. {
  200. ESP_LOGV("radio", "carrier not detected");
  201. P = (uint8_t) esp_random();
  202. if (P <= tnc_settings.P)
  203. {
  204. ESP_LOGI("radio", "transmission scheduled");
  205. break;
  206. }
  207. else
  208. {
  209. ESP_LOGV("radio", "transmission postponed");
  210. vTaskDelay((tnc_settings.slot_time * 10) / portTICK_PERIOD_MS);
  211. }
  212. }
  213. else
  214. {
  215. ESP_LOGV("radio", "carrier detected");
  216. vTaskDelay(10 / portTICK_PERIOD_MS); // re-check in 10ms
  217. }
  218. }
  219. // indicate TX
  220. enable_red_led();
  221. disable_green_led();
  222. // TODO: modify code so the full TX DELAY isn't used when multiple packets are sent.
  223. // dump all the packets in the buffer
  224. while(packet != NULL)
  225. {
  226. // setup packet to be transmitted
  227. tnc_settings.status = TNC_TX;
  228. radio_packet_tx(packet, packet_size, NULL);
  229. // sleep until packet is finished being sent
  230. xSemaphoreTake(xRadioTXSemaphore, portMAX_DELAY);
  231. tnc_settings.status = TNC_IDLE;
  232. // remove packet from ring buffer and load next one if it exists
  233. vRingbufferReturnItem(ax25_tx_buf, (void *)packet);
  234. packet = xRingbufferReceive(ax25_tx_buf, &packet_size, 0);
  235. }
  236. }
  237. else
  238. {
  239. ESP_LOGE("tnc", "buffer error");
  240. }
  241. // setup again for reception
  242. radio_packet_rx(NULL);
  243. tnc_settings.status = TNC_RX;
  244. }
  245. }
  246. void app_main()
  247. {
  248. // Inter-Task Communication
  249. ax25_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
  250. xRadioTXSemaphore = xSemaphoreCreateBinary();
  251. // Initialize Flash
  252. esp_err_t ret = nvs_flash_init();
  253. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  254. ESP_ERROR_CHECK(nvs_flash_erase());
  255. ret = nvs_flash_init();
  256. }
  257. ESP_ERROR_CHECK( ret );
  258. // Board IO Initialize
  259. board_init();
  260. // Load default TNC settings
  261. tnc_settings.P = 63;
  262. tnc_settings.tx_tail = 0; // 10ms units
  263. tnc_settings.tx_delay = 10; // 10ms units
  264. tnc_settings.P = 63;
  265. tnc_settings.slot_time = 10; // 10ms units
  266. tnc_settings.tnc_number = 0;
  267. tnc_settings.status = TNC_IDLE;
  268. tnc_settings.freq = 144390000;
  269. tnc_settings.mode = AX25;
  270. // Radio Initialize
  271. ax25_param_t ax25_param;
  272. ax25_param.tx_tail = tnc_settings.tx_tail;
  273. ax25_param.tx_delay = tnc_settings.tx_delay;
  274. ax25_param.sample_rate = 13200;
  275. ax25_param.symbol0_freq = 1200;
  276. ax25_param.symbol1_freq = 2200;
  277. ax25_param.rx_cb = radio_rx_ax25_cb;
  278. ax25_param.tx_cb = radio_tx_ax25_cb;
  279. ax25_param.rx_buf = ax25_rx_buf;
  280. ax25_param.rx_buf_len = sizeof(ax25_rx_buf) / sizeof(ax25_rx_buf[0]);
  281. ax25_param.cpu_core = 1;
  282. // TODO: setup switch statement for settings parameters or for mode....
  283. tnc_print_settings();
  284. radio_init(AX25, &ax25_param);
  285. radio_set_frequency(tnc_settings.freq);
  286. // Kiss Decoder and Encoder
  287. kiss_init(tnc_settings.tnc_number, kiss_tx_cb, kiss_rx_cb);
  288. // BLE and SPP
  289. bt_spp_init();
  290. // Tasks
  291. xTaskCreatePinnedToCore(tnc_task, "tnc task", 1024*4, 0, 2, &tnc_settings.tnc_task, 1);
  292. }