Browse Source

Basic demo with SPP and APRS TX

curiousmuch 4 years ago
parent
commit
cffa3df4fd
5 changed files with 147 additions and 30 deletions
  1. 54 0
      main/board.c
  2. 16 0
      main/board.h
  3. 16 15
      main/cc1200.c
  4. 54 5
      main/main.c
  5. 7 10
      main/tnc_kiss.c

+ 54 - 0
main/board.c

@@ -0,0 +1,54 @@
+/*
+ * board.c
+ *
+ *  Created on: Jun 15, 2019
+ *      Author: curiousmuch
+ */
+#include <stdio.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "driver/gpio.h"
+#include "driver/adc.h"
+#include "board.h"
+
+void enable_red_led(void)
+{
+	gpio_set_level(RED_LED, 1);
+}
+
+void enable_green_led(void)
+{
+	gpio_set_level(GREEN_LED, 1);
+}
+
+void disable_red_led(void)
+{
+	gpio_set_level(RED_LED, 0);
+}
+
+void disable_green_led(void)
+{
+	gpio_set_level(GREEN_LED, 0);
+}
+
+int32_t battery_measure(void)
+{
+	   adc1_config_width(ADC_WIDTH_BIT_12);
+	   adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_DB_0);
+	   int val = adc1_get_raw(BATTERY_ADC_CHANNEL);
+	   return val;
+}
+
+void board_init(void)
+{
+	// setup LED IO
+	gpio_config_t led_pin_config =
+	{
+			.pin_bit_mask = (uint64_t) (BIT64(RED_LED)|BIT64(GREEN_LED)),
+			.mode = GPIO_MODE_OUTPUT,
+			.pull_up_en = GPIO_PULLUP_DISABLE,
+			.pull_down_en = GPIO_PULLDOWN_DISABLE,
+			.intr_type = GPIO_INTR_DISABLE
+	};
+	gpio_config(&led_pin_config);
+}

+ 16 - 0
main/board.h

@@ -8,6 +8,15 @@
 #ifndef MAIN_BOARD_H_
 #define MAIN_BOARD_H_
 
+// Battery Measurement
+#define ENABLE_VOLTAGE_DIVIDER	25
+#define BATTERY_ADC				35
+#define BATTERY_ADC_CHANNEL		ADC1_CHANNEL_7
+
+// LEDs
+#define RED_LED 	21
+#define GREEN_LED 	2
+
 // Radio Selection
 #define CC1200		1
 #define CC1120		1
@@ -49,5 +58,12 @@
 #define DEBUG_0 			2
 #define DEBUG_1				4
 
+void enable_red_led(void);
+void enable_green_led(void);
+void disable_red_led(void);
+void disable_green_led(void);
+void board_init(void);
+int32_t battery_measure(void);
+
 
 #endif /* MAIN_BOARD_H_ */

+ 16 - 15
main/cc1200.c

@@ -69,18 +69,18 @@ void cc1200_gpio_init(void)
 			.pull_down_en = GPIO_PULLDOWN_DISABLE,
 			.intr_type = GPIO_INTR_DISABLE
 	};
-	gpio_config_t debug_pin_config =
-	{
-			.pin_bit_mask = (uint64_t) (BIT64(DEBUG_0)|BIT64(DEBUG_1)),
-			.mode = GPIO_MODE_OUTPUT,
-			.pull_up_en = GPIO_PULLUP_DISABLE,
-			.pull_down_en = GPIO_PULLDOWN_DISABLE,
-			.intr_type = GPIO_INTR_DISABLE
-	};
+//	gpio_config_t debug_pin_config =
+//	{
+//			.pin_bit_mask = (uint64_t) (BIT64(DEBUG_0)|BIT64(DEBUG_1)),
+//			.mode = GPIO_MODE_OUTPUT,
+//			.pull_up_en = GPIO_PULLUP_DISABLE,
+//			.pull_down_en = GPIO_PULLDOWN_DISABLE,
+//			.intr_type = GPIO_INTR_DISABLE
+//	};
 
 	gpio_config(&reset_pin_config);
 	gpio_config(&gpio_pin_config);
-	gpio_config(&debug_pin_config);
+	//gpio_config(&debug_pin_config);
 
 
 	gpio_set_level(CC1120_RESET, 1);
@@ -369,7 +369,7 @@ static void IRAM_ATTR cc1200_aprs_tx_isr(void* arg)
     new_sample = 1;
 
 	toggle = toggle ^ 1;
-	gpio_set_level(DEBUG_1, toggle);
+	//gpio_set_level(DEBUG_1, toggle);
 }
 
 void cc1200_lut_init(void)
@@ -398,12 +398,12 @@ 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;
 	uint16_t p_len = tx_delay * 12;
-	uint16_t t_len = tx_tail * 12;
+	uint16_t t_len = tx_tail * 12 + 1;
 
 		// Start CW transmission
 		cc1200_spi_write_byte(CC120X_FIFO, 0x12);
@@ -477,7 +477,7 @@ void IRAM_ATTR cc1200_radio_APRSTXPacket(uint8_t  *f, uint16_t f_len, uint8_t tx
 						}
 					}
 					toggle2 = toggle2 ^ 1;
-					gpio_set_level(DEBUG_0, toggle2);
+					//gpio_set_level(DEBUG_0, toggle2);
 					sample_count = 0;
 				}
 
@@ -505,7 +505,7 @@ void IRAM_ATTR cc1200_radio_APRSTXPacket(uint8_t  *f, uint16_t f_len, uint8_t tx
 					}
 				}
 				toggle2 = toggle2 ^ 1;
-				gpio_set_level(DEBUG_0, toggle2);
+				//gpio_set_level(DEBUG_0, toggle2);
 				sample_count = 0;
 				//printf("Symbol: %x\n", aprs_flags.cur_bit);
 			}
@@ -550,6 +550,7 @@ 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);
+		gpio_uninstall_isr_service();
 
 }
 
@@ -562,7 +563,7 @@ static void IRAM_ATTR cc1200_aprs_rx_isr(void* arg)
 	cc1200_spi_read_byte(CC120X_CFM_RX_DATA_OUT, &data);
 
 	toggle = toggle ^ 1;
-	gpio_set_level(DEBUG_1, toggle);
+	//gpio_set_level(DEBUG_1, toggle);
 	xRingbufferSendFromISR(cfm_buf_handle, &data, sizeof(data), NULL);
 	//ets_write_char_uart(data);
     //new_sample = 1;

+ 54 - 5
main/main.c

@@ -33,6 +33,55 @@ uint8_t APRS_TEST_PACKET[] = { 0x82, 0x98, 0x98, 0x40, 0x40, 0x40, 0xe0, 0x96, 0
 									 0x42, 0x33, 0x55, 0x4b, 0x56, 0x2d, 0x32, 0x20, 0x3a, 0x48, 0x69, 0x21, 0x20, 0x54, 0x68, 0x69, 0x73,
 									 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x54, 0x65, 0x73, 0x74, 0x7b, 0x31, 0xad, 0xa1 };
 
+RingbufHandle_t radio_tx_buf;
+//RingbufHandle_t radio_rx_buf;
+
+void Radio_Task(void *pvParameters)
+{
+	size_t p_size;
+
+	// 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);
+
+
+
+	while(1)
+	{
+		// setup LEDs for RX mode
+		enable_green_led();
+		disable_red_led();
+
+
+		// Transmit Queued Packet
+		uint8_t *p = (uint8_t *)xRingbufferReceive(radio_tx_buf, &p_size, portMAX_DELAY);	// TODO: Modify to something which will check CC1200 status
+
+		if (p != NULL)
+		{
+			// setup LEDs for TX mode
+			enable_red_led();
+			disable_green_led();
+
+			//vTaskSuspendAll();
+
+			cc1200_radio_APRSTXPacket(p, p_size, 2, 0);
+
+			//xTaskResumeAll();
+			vRingbufferReturnItem(radio_tx_buf, (void *)p);
+
+		}
+    }
+}
+
+
+void radio_task_init()
+{
+	radio_tx_buf = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
+	xTaskCreatePinnedToCore(Radio_Task, "Radio_Task", 1024*4, 0, 1, NULL, 1);
+}
+
 void IRAM_ATTR app_main()
 {
 	// Initialize Flash
@@ -43,13 +92,13 @@ void IRAM_ATTR app_main()
 	}
 	ESP_ERROR_CHECK( ret );
 
-	// Setup Radio
-	cc1200_radio_init(APRS_SETTINGS, sizeof(APRS_SETTINGS)/sizeof(cc1200_reg_settings_t));
-	cc1200_radio_frequency(144390000-6000);
+	// Board IO Initialize
+	board_init();
 
-	vTaskDelay(500/portTICK_PERIOD_MS);
+	// Radio Task Initialize
+	radio_task_init();
 
-	// Setup TNC
+	// Setup Kiss Decoder and Encoder
 	tnc_init();
 
 	// Initalize BLE

+ 7 - 10
main/tnc_kiss.c

@@ -40,6 +40,8 @@ void kiss_append_buffer(uint8_t chr)
 	buffer_handle.index = buffer_handle.index + 1;
 }
 
+extern RingbufHandle_t radio_tx_buf;
+
 // Frame Format
 // [ Command Byte ][ Data ]
 void kiss_process_frame(void)
@@ -51,12 +53,13 @@ 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);
 			// unblock AX.25 code to process code
-			uint32_t fcs = fcs_calc(buffer_handle.buf, buffer_handle.index);
+			uint32_t fcs = fcs_calc(&buffer_handle.buf[1], buffer_handle.index-1);
 			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);
+			buffer_handle.buf[buffer_handle.index+1] = fcs>>8 & 0xFF;
+			xRingbufferSend(radio_tx_buf, &buffer_handle.buf[1], ((buffer_handle.index+1)*sizeof(uint8_t)), 10/portTICK_PERIOD_MS);
+			ESP_LOG_BUFFER_HEXDUMP(TNC_TAG, &buffer_handle.buf[1], buffer_handle.index+1, ESP_LOG_INFO);
+			//cc1200_radio_APRSTXPacket(buffer_handle.buf, (buffer_handle.index+2), tnc_settings.tx_delay, tnc_settings.tx_tail);
 			break;
 		}
 		case KISS_CMD_TXDELAY: {
@@ -207,9 +210,3 @@ void tnc_receive(uint8_t* data, uint16_t len)
 		}
 	}
 }
-
-
-
-
-
-