Browse Source

Attempted to merge together code for Bluetooth TNC, but it's failing. I believe CPU0 is during everything. CC1120 code needs to be in a seperate task to run on a different CPU.

curiousmuch 4 years ago
parent
commit
f5ae722a81
5 changed files with 61 additions and 40 deletions
  1. 3 2
      main/bt_spp.c
  2. 8 2
      main/cc1200.c
  3. 27 27
      main/main.c
  4. 23 7
      main/tnc_kiss.c
  5. 0 2
      main/tnc_kiss.h

+ 3 - 2
main/bt_spp.c

@@ -18,9 +18,10 @@
 #include "esp_bt_device.h"
 #include "esp_spp_api.h"
 
-
+// TODO: Remove and add call back functions to tie to main
 #include "tnc_kiss.c"
 
+
 // Why are these global? Should I move this to a .h settings file?
 #define DEVICE_NAME		"Arrow"
 #define SPP_TAG			"BT SPP"
@@ -60,7 +61,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
 	case ESP_SPP_DATA_IND_EVT:
 		// called when SPP connection receives data
 		ESP_LOGI(SPP_TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d", param->data_ind.len, param->data_ind.handle);
-		esp_log_buffer_hex("", param->data_ind.data, param->data_ind.len);
+		//esp_log_buffer_hex("", param->data_ind.data, param->data_ind.len);
 		tnc_receive(param->data_ind.data, param->data_ind.len);
 		// send data to Arrow State-Machine
 		// esp_spp_write(param->data_ind.handle, param->data_ind.len, param->data_ind.data);

+ 8 - 2
main/cc1200.c

@@ -29,7 +29,8 @@ spi_bus_config_t bus_config =
 	.quadwp_io_num = -1,
 	.quadhd_io_num = -1,
 	.max_transfer_sz = 150,
-	.flags = ESP_INTR_FLAG_IRAM
+	.flags = SPICOMMON_BUSFLAG_MASTER,
+	.intr_flags = ESP_INTR_FLAG_IRAM
 };
 
 
@@ -397,7 +398,7 @@ void IRAM_ATTR cc1200_radio_APRSTXPacket(uint8_t  *f, uint16_t f_len, uint8_t tx
 	gpio_set_intr_type(CC1120_GPIO3, GPIO_INTR_POSEDGE);
 
 	// acquire SPI bus for fastest possible SPI transactions
-	spi_device_acquire_bus(spi, portMAX_DELAY);
+	//spi_device_acquire_bus(spi, portMAX_DELAY);
 
 
 	int16_t i,j;
@@ -414,6 +415,7 @@ void IRAM_ATTR cc1200_radio_APRSTXPacket(uint8_t  *f, uint16_t f_len, uint8_t tx
 		// Send HDLC Flag for TX Delay Time
 		for (i = 0; i<p_len; i++)
 		{
+			esp_task_wdt_reset();
 			aprs_flags.byte = 0x7E;
 			for(j=0; j<8; j++)
 			{
@@ -453,6 +455,7 @@ void IRAM_ATTR cc1200_radio_APRSTXPacket(uint8_t  *f, uint16_t f_len, uint8_t tx
 		//for (i=0;i<aprs_flags.packet_len;i++)
 		for (i=0;i<f_len;i++)
 		{
+			esp_task_wdt_reset();
 			aprs_flags.byte = f[i];
 			for(j=0; j<8; j++)
 			{
@@ -512,6 +515,7 @@ void IRAM_ATTR cc1200_radio_APRSTXPacket(uint8_t  *f, uint16_t f_len, uint8_t tx
 		// Send HDLC Flag for TX Tail
 		for (i = 0; i<t_len; i++)
 		{
+			esp_task_wdt_reset();
 			aprs_flags.byte = 0x7E;
 			for(j=0; j<8;j++)
 			{
@@ -545,6 +549,8 @@ void IRAM_ATTR cc1200_radio_APRSTXPacket(uint8_t  *f, uint16_t f_len, uint8_t tx
 			}
 		}
 		cc1200_spi_strobe(CC120X_SIDLE);
+		spi_device_release_bus(spi);
+
 }
 
 RingbufHandle_t cfm_buf_handle;

+ 27 - 27
main/main.c

@@ -13,10 +13,21 @@
 #include "cc1200_protocol.h"
 #include "board.h"
 
-//#include "ax25_pad2.h"
-//#include "ax25_pad.h"
+#include "ax25_pad2.h"
+#include "ax25_pad.h"
 #include "fcs_calc.h"
 
+#include "nvs.h"
+#include "nvs_flash.h"
+#include "esp_log.h"
+#include "esp_bt.h"
+#include "esp_bt_main.h"
+#include "esp_gap_bt_api.h"
+#include "esp_bt_device.h"
+#include "esp_spp_api.h"
+#include "bt_spp.c"
+
+
 uint8_t APRS_TEST_PACKET[] = { 0x82, 0x98, 0x98, 0x40, 0x40, 0x40, 0xe0, 0x96, 0x84, 0x66, 0xaa, 0x96, 0xac, 0xe0, 0xae, 0x92,
 									 0x88, 0x8a, 0x62, 0x40, 0x62, 0xae, 0x92, 0x88, 0x8a, 0x64, 0x40, 0x65, 0x03, 0xf0, 0x3a, 0x4b,
 									 0x42, 0x33, 0x55, 0x4b, 0x56, 0x2d, 0x32, 0x20, 0x3a, 0x48, 0x69, 0x21, 0x20, 0x54, 0x68, 0x69, 0x73,
@@ -24,34 +35,23 @@ uint8_t APRS_TEST_PACKET[] = { 0x82, 0x98, 0x98, 0x40, 0x40, 0x40, 0xe0, 0x96, 0
 
 void IRAM_ATTR app_main()
 {
-	cc1200_radio_init(APRS_SETTINGS, sizeof(APRS_SETTINGS)/sizeof(cc1200_reg_settings_t));
-	vTaskDelay(500/portTICK_PERIOD_MS);
-
-//	// generate sample packet
-//	packet_t pp;
-//	unsigned char fbuf[AX25_MAX_PACKET_LEN+2];
-//	uint32_t flen;
-//	uint32_t c;
-//
-//	pp = ax25_from_text("WB2OSZ-15>TEST:,The quick brown fox jumps over the lazy dog!  1 of 4", 1);
-//	flen = ax25_pack(pp, fbuf);
-//
-//	uint32_t fcs = fcs_calc(fbuf, flen);
-//
-//	ax25_hex_dump(pp);
-//	printf("FCS: %x\n", fcs);
-
-
-	//while(1)
-	{
-
-		//cc1200_radio_APRSTXPacket(APRS_TEST_PACKET, sizeof(APRS_TEST_PACKET)/sizeof(uint8_t), 4, 2);
-		vTaskDelay(1000/portTICK_PERIOD_MS);
+	// Initialize Flash
+	esp_err_t ret = nvs_flash_init();
+	if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
+		ESP_ERROR_CHECK(nvs_flash_erase());
+		ret = nvs_flash_init();
 	}
+	ESP_ERROR_CHECK( ret );
 
-	//cc1120_radio_APRSTXPacket();
-	//xTaskCreatePinnedToCore();
+	// Setup Radio
+	cc1200_radio_init(APRS_SETTINGS, sizeof(APRS_SETTINGS)/sizeof(cc1200_reg_settings_t));
+	cc1200_radio_frequency(144390000-6000);
 
+	vTaskDelay(500/portTICK_PERIOD_MS);
 
+	// Setup TNC
+	tnc_init();
 
+	// Initalize BLE
+	bt_spp_init();
 }

+ 23 - 7
main/tnc_kiss.c

@@ -14,6 +14,10 @@
 #include <stdio.h>
 #include "tnc_kiss.h"
 
+// TODO: remove and use call back function for TNC
+#include "cc1200.h"
+#include "fcs_calc.h"
+
 buffer_handle_t buffer_handle;
 tnc_settings_t tnc_settings;
 
@@ -47,8 +51,12 @@ void kiss_process_frame(void)
 	switch( data_byte ) {
 		case KISS_DATAFRAME: {
 			ESP_LOGI(TNC_TAG, "Received Data Frame - Length %d", (buffer_handle.index));
-			ESP_LOG_BUFFER_HEXDUMP(TNC_TAG, buffer_handle.buf, buffer_handle.index, ESP_LOG_INFO);
+			//ESP_LOG_BUFFER_HEXDUMP(TNC_TAG, buffer_handle.buf, buffer_handle.index, ESP_LOG_INFO);
 			// unblock AX.25 code to process code
+			uint32_t fcs = fcs_calc(buffer_handle.buf, buffer_handle.index);
+			buffer_handle.buf[buffer_handle.index] = fcs & 0xFF;
+			buffer_handle.buf[buffer_handle.index] = fcs>>8 & 0xFF;
+			cc1200_radio_APRSTXPacket(buffer_handle.buf, (buffer_handle.index+2), tnc_settings.tx_delay, tnc_settings.tx_tail);
 			break;
 		}
 		case KISS_CMD_TXDELAY: {
@@ -92,6 +100,15 @@ void kiss_process_frame(void)
 	return;
 }
 
+void tnc_set_paramters(tnc_settings_t s)
+{
+		tnc_settings.tx_delay = s.tx_delay;
+		tnc_settings.persistence = s.persistence;
+		tnc_settings.slot_time = s.slot_time;
+		tnc_settings.tx_tail = s.tx_tail;
+		tnc_settings.full_duplex = s.full_duplex;
+//		tnc_settings.tnc_number = s.tnc_number;
+}
 
 
 // TNC needs the ability to RX KISS frame, decode it, schedule it, and then send out via APRS or Arrow-Net
@@ -105,12 +122,11 @@ void tnc_init(void)
 	// TODO: Store / retrieve default TNC settings from flash?
 
 	// set default TNC settings
-//	tnc_settings.tx_delay = s.tx_delay;
-//	tnc_settings.persistence = s.persistence;
-//	tnc_settings.slot_time = s.slot_time;
-//	tnc_settings.tx_tail = s.tx_tail;
-//	tnc_settings.full_duplex = s.full_duplex;
-//	tnc_settings.tnc_number = s.tnc_number;
+	tnc_settings.tx_delay = 5;
+	tnc_settings.persistence = 63;
+	tnc_settings.slot_time = 10;
+	tnc_settings.tx_tail = 1;
+	tnc_settings.full_duplex = 0;
 
 	// setup TNC task with queue
 }

+ 0 - 2
main/tnc_kiss.h

@@ -48,8 +48,6 @@
 	uint8_t slot_time;
 	uint8_t tx_tail;
 	uint8_t full_duplex;
-	uint8_t tnc_number;
-//	uint8_t max_frame_size;
 } tnc_settings_t;
 
 typedef enum {