Browse Source

Issues with Transmission need to be fixed. I believe the extra RX configuration registers are breaking the setup. Need to switch TX timing to ESP-Timer based configuration

curiousmuch 4 years ago
parent
commit
7c2c356d23
4 changed files with 224 additions and 77 deletions
  1. 80 56
      main/aprs_decoder.c
  2. 66 0
      main/aprs_decoder.h
  3. 1 1
      main/cc1200.c
  4. 77 20
      main/main.c

+ 80 - 56
main/aprs_decoder.c

@@ -5,82 +5,81 @@
  *      Author: curiousmuch
  *      Author: curiousmuch
  */
  */
 
 
+#include "aprs_decoder.h"
+//#include "fcs_calc.h"
 
 
-typedef enum {
-	NORMAL,
-	FRAME_DECODED,
-	ERROR_FCS_MISMATCH,
-	ERROR_PACKET_FORMAT,
-	ERROR_BUFFER_OVERFLOW
-} decoder_output_t;
-
-typedef enum {
-	NONE,
-	BUFFER_OVERFLOW,
-	BIT_STUFFING_FAILURE,
-	FCS_MISMATCH,
-} decoder_error_t;
-
-typedef enum {
-	FLAG_SEARCH,
-	FLAG_FOUND,
-	FRAME_START,
-	PACKET_END,
-	FRAME_END,
-	ABORT,
-} decoder_state_t;
-
-typedef struct {
-	decoder_state_t decoder_state;
-	uint8_t 		*frame_buffer;
-	uint8_t 		frame_buffer_index;
-	uint8_t 		frame_buffer_len;
-	uint8_t 		flag_buffer;
-	uint8_t			flag_buffer_index;
-	uint8_t			byte_buffer;
-	uint8_t 		current_nrzi_bit;
-	uint8_t 		previous_nrzi_bit;
-	uint8_t 		current_bit;
-	uint8_t 		one_count;
-	uint32_t 		byte_buffer_index;
-	uint8_t			skip_bit_flag;
-} decoder_varibles_t;
+//#define uint8_t unsigned int
+//#define uint32_t unsigned int
 
 
 decoder_varibles_t v;
 decoder_varibles_t v;
 
 
 #define APRS_MAX_FRAME 256
 #define APRS_MAX_FRAME 256
 
 
-void frame_buffer_init(uint8_t *buf, uint8_t len)
+uint32_t fcs_calc(uint8_t *data, uint32_t len)
+{
+	uint32_t crc, aprs_polynomial, i, j, byte_lsb, crc_lsb;
+	crc = 0xFFFF;
+	aprs_polynomial = 0x8408;
+	for (i=0;i<len;i++)
+	{
+		for (j=0;j<8;j++)
+		{
+			byte_lsb = (data[i] >> j) & 0x01;
+			crc_lsb = crc & 0x0001;
+			if (crc_lsb != byte_lsb)
+				crc = (crc >> 1) ^ aprs_polynomial;
+			else
+				crc = crc >> 1;
+		}
+	}
+	crc = crc ^ 0xFFFF;
+	return crc & 0xFFFF;
+}
+
+void frame_buffer_init(uint8_t *buf, uint32_t len)
 {
 {
 	v.frame_buffer = buf;
 	v.frame_buffer = buf;
 	v.frame_buffer_len = len;
 	v.frame_buffer_len = len;
 }
 }
 
 
-void aprs_decode_init(void)
+void aprs_decoder_init(void)
 {
 {
 	v.decoder_state = FLAG_SEARCH;
 	v.decoder_state = FLAG_SEARCH;
 	v.frame_buffer_index = 0;
 	v.frame_buffer_index = 0;
 	//v.frame_max_len = APRS_MAX_FRAME;
 	//v.frame_max_len = APRS_MAX_FRAME;
 	v.flag_buffer = 0;
 	v.flag_buffer = 0;
 	v.flag_buffer_index = 0;
 	v.flag_buffer_index = 0;
-	v.byte_buffer_index = 0;
 	v.byte_buffer = 0;
 	v.byte_buffer = 0;
+	v.byte_buffer_index = 0;
 	v.current_nrzi_bit = 0;
 	v.current_nrzi_bit = 0;
 	v.previous_nrzi_bit = 0;
 	v.previous_nrzi_bit = 0;
 	v.current_bit = 0;
 	v.current_bit = 0;
 	v.one_count = 0;
 	v.one_count = 0;
 	v.skip_bit_flag = 0;
 	v.skip_bit_flag = 0;
+	v.packet_rx = 0;
+}
+
+uint8_t get_rx_status(void)
+{
+	if (v.packet_rx)
+		return 1;
+	else
+		return 0;
 }
 }
 
 
 uint8_t flag_found(void)
 uint8_t flag_found(void)
 {
 {
 	if (v.flag_buffer == 0x7E)
 	if (v.flag_buffer == 0x7E)
+	{
 		return 1;
 		return 1;
+	}
 	else
 	else
+	{
 		return 0;
 		return 0;
+	}
 }
 }
 
 
-decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
+decoder_output_t aprs_decoder_feed_bit(uint8_t nrzi_bit)
 {
 {
 	v.current_nrzi_bit = nrzi_bit;
 	v.current_nrzi_bit = nrzi_bit;
 
 
@@ -88,7 +87,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 	if (v.previous_nrzi_bit == v.current_nrzi_bit)
 	if (v.previous_nrzi_bit == v.current_nrzi_bit)
 	{
 	{
 		v.current_bit = 1;
 		v.current_bit = 1;
-		v.one_count =+ 1;
+		v.one_count += 1;
 	}
 	}
 	else
 	else
 	{
 	{
@@ -98,6 +97,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 	v.previous_nrzi_bit = v.current_nrzi_bit;
 	v.previous_nrzi_bit = v.current_nrzi_bit;
 
 
 	// load bit into flag buffer
 	// load bit into flag buffer
+	// TODO: Is this in the correct format????
 	v.flag_buffer = (v.flag_buffer >> 1) + (v.current_bit*0x80);
 	v.flag_buffer = (v.flag_buffer >> 1) + (v.current_bit*0x80);
 
 
 	switch(v.decoder_state)
 	switch(v.decoder_state)
@@ -107,6 +107,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 			{
 			{
 				// set state variable
 				// set state variable
 				v.decoder_state = FLAG_FOUND;
 				v.decoder_state = FLAG_FOUND;
+				v.packet_rx = 1;
 
 
 				// re-initialize buffer indexes
 				// re-initialize buffer indexes
 				v.flag_buffer_index = 0;
 				v.flag_buffer_index = 0;
@@ -115,6 +116,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 			else
 			else
 			{
 			{
 				v.decoder_state = FLAG_SEARCH;
 				v.decoder_state = FLAG_SEARCH;
+				v.packet_rx = 0;
 			}
 			}
 			break;
 			break;
 		case FLAG_FOUND:
 		case FLAG_FOUND:
@@ -132,6 +134,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 					// load current bits in byte buffer and remove 0 stuffing
 					// load current bits in byte buffer and remove 0 stuffing
 					uint8_t i, bit;
 					uint8_t i, bit;
 					v.skip_bit_flag = 0;
 					v.skip_bit_flag = 0;
+					v.byte_buffer = 0;
 					v.byte_buffer_index = 0;
 					v.byte_buffer_index = 0;
 					v.one_count = 0;
 					v.one_count = 0;
 
 
@@ -141,7 +144,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 
 
 						// count ones for to remove bit stuffing
 						// count ones for to remove bit stuffing
 						if (bit)
 						if (bit)
-							v.one_count =+ 1;
+							v.one_count += 1;
 						else
 						else
 							v.one_count = 0;
 							v.one_count = 0;
 
 
@@ -152,12 +155,15 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 
 
 							// if "0" is not stuffed packet is invalid
 							// if "0" is not stuffed packet is invalid
 							if (bit != 0)
 							if (bit != 0)
+							{
 								v.decoder_state = ABORT;
 								v.decoder_state = ABORT;
+								break;
+							}
 						}
 						}
 						else
 						else
 						{
 						{
-							v.byte_buffer |= bit*(0x8>>i);
-							v.byte_buffer_index =+ 1;
+							v.byte_buffer |=  (bit) ? (0x80>>i): 0;
+							v.byte_buffer_index += 1;
 						}
 						}
 
 
 						if (v.one_count == 5)
 						if (v.one_count == 5)
@@ -176,7 +182,10 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 					break;
 					break;
 				}
 				}
 			}
 			}
-			v.flag_buffer_index =+ 1;
+			else
+			{
+				v.flag_buffer_index += 1;
+			}
 			break;
 			break;
 		case FRAME_START:
 		case FRAME_START:
 			// skip stuffed "0"
 			// skip stuffed "0"
@@ -188,18 +197,18 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 				// indicating the end of the frame or the packet has become corrupted.
 				// indicating the end of the frame or the packet has become corrupted.
 				if (v.current_bit != 0)
 				if (v.current_bit != 0)
 				{
 				{
-					v.decoder_state = PACKET_END;
+					v.decoder_state = FRAME_BREAK;
 					break;
 					break;
 				}
 				}
 			}
 			}
 			else
 			else
 			{
 			{
 				// load bit
 				// load bit
-				v.byte_buffer |= v.current_bit*(0x8>>v.byte_buffer_index);
-				v.byte_buffer_index =+ 1;
+				v.byte_buffer |= v.current_bit*(0x01<<v.byte_buffer_index);
+				v.byte_buffer_index += 1;
 
 
 				// check if byte buffer is full
 				// check if byte buffer is full
-				if (v.byte_buffer_index == 7)
+				if (v.byte_buffer_index > 7)
 				{
 				{
 					v.frame_buffer[v.frame_buffer_index] = v.byte_buffer;
 					v.frame_buffer[v.frame_buffer_index] = v.byte_buffer;
 					v.byte_buffer = 0;
 					v.byte_buffer = 0;
@@ -207,7 +216,7 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 					v.frame_buffer_index += 1;
 					v.frame_buffer_index += 1;
 
 
 					// check for overflow
 					// check for overflow
-					if (v.frame_buffer_index == v.frame_buffer_len)
+					if (v.frame_buffer_index >= v.frame_buffer_len)
 					{
 					{
 						v.decoder_state = ABORT;
 						v.decoder_state = ABORT;
 						break;
 						break;
@@ -224,12 +233,27 @@ decoder_output_t aprs_decode_feed_bit(uint8_t nrzi_bit)
 			break;
 			break;
 	}
 	}
 
 
-	if (v.decoder_state == FRAME_END)
+	if (v.decoder_state == FRAME_BREAK)
 	{
 	{
+		// state
+		v.decoder_state = FLAG_SEARCH;
+
 		// calculate CRC
 		// calculate CRC
+		volatile uint32_t fcs, frame_fcs;
 
 
-		// re-initialize state machine
-		return FRAME_DECODED;
+		if (v.frame_buffer_index < 10) // TODO: Determine minimum APRS packet
+			return ERROR_PACKET_FORMAT;
+
+		v.frame_len = v.frame_buffer_index - 2;
+		fcs = fcs_calc(v.frame_buffer, v.frame_len);
+
+		frame_fcs = (v.frame_buffer[v.frame_len+1]<<8) |
+				v.frame_buffer[v.frame_len];
+
+		if (fcs == frame_fcs)
+			return FRAME_DECODED;
+		else
+			return ERROR_FCS_MISMATCH;
 	}
 	}
 
 
 	if (v.decoder_state == ABORT)
 	if (v.decoder_state == ABORT)

+ 66 - 0
main/aprs_decoder.h

@@ -0,0 +1,66 @@
+/*
+ * aprs_decoder.h
+ *
+ *  Created on: Sep 1, 2019
+ *      Author: curiousmuch
+ */
+
+#ifndef APRS_DECODER_H_
+#define APRS_DECODER_H_
+
+#include <stdio.h>
+#include "freertos/FreeRTOS.h"
+
+//#define uint8_t unsigned int
+//#define uint32_t unsigned int
+
+typedef enum {
+	NORMAL,
+	FRAME_DECODED,
+	ERROR_FCS_MISMATCH,
+	ERROR_PACKET_FORMAT,
+	ERROR_BUFFER_OVERFLOW
+} decoder_output_t;
+
+typedef enum {
+	NONE,
+	BUFFER_OVERFLOW,
+	BIT_STUFFING_FAILURE,
+	FCS_MISMATCH,
+} decoder_error_t;
+
+typedef enum {
+	FLAG_SEARCH,
+	FLAG_FOUND,
+	FRAME_START,
+	FRAME_BREAK,
+	ABORT,
+} decoder_state_t;
+
+typedef struct {
+	decoder_state_t decoder_state;
+	uint8_t 		*frame_buffer;
+	uint32_t 		frame_buffer_index;
+	uint32_t 		frame_buffer_len;
+	uint32_t		frame_len;
+	uint8_t 		flag_buffer;
+	uint8_t			flag_buffer_index;
+	uint8_t			byte_buffer;
+	uint8_t			byte_buffer_index;
+	uint8_t 		current_nrzi_bit;
+	uint8_t 		previous_nrzi_bit;
+	uint8_t 		current_bit;
+	uint8_t 		one_count;
+	uint8_t			skip_bit_flag;
+	uint8_t 		packet_rx;
+} decoder_varibles_t;
+
+
+uint8_t get_rx_status(void);
+void frame_buffer_init(uint8_t *buf, uint32_t len);
+void aprs_decoder_init(void);
+decoder_output_t aprs_decoder_feed_bit(uint8_t nrzi_bit);
+
+
+
+#endif /* APRS_DECODER_H_ */

+ 1 - 1
main/cc1200.c

@@ -259,7 +259,7 @@ rf_status_t cc1200_radio_reset(void)
 	cc1200_spi_strobe(CC120X_SRES);
 	cc1200_spi_strobe(CC120X_SRES);
 	status = cc1200_spi_strobe(CC120X_SNOP);
 	status = cc1200_spi_strobe(CC120X_SNOP);
 
 
-	vTaskDelay(10 / portTICK_PERIOD_MS);
+	vTaskDelay(20 / portTICK_PERIOD_MS);
 
 
 	while((CC120X_RDYn_BIT & (status & 0x80)))
 	while((CC120X_RDYn_BIT & (status & 0x80)))
 	{
 	{

+ 77 - 20
main/main.c

@@ -31,6 +31,7 @@
 #include "math.h"
 #include "math.h"
 
 
 #include "driver/timer.h"
 #include "driver/timer.h"
+#include "aprs_decoder.h"
 
 
 
 
 //#include "esp_dsp.h"
 //#include "esp_dsp.h"
@@ -97,7 +98,7 @@ void window_add(int sample)
 	window[0] = sample;
 	window[0] = sample;
 }
 }
 
 
-uint8_t * window_get(void)
+int* window_get(void)
 {
 {
 	return window;
 	return window;
 }
 }
@@ -114,7 +115,7 @@ int window_get_size(void)
 void IRAM_ATTR rx_timer_isr(void *para)
 void IRAM_ATTR rx_timer_isr(void *para)
 {
 {
 
 
-    //GPIO.out_w1ts = (1 << DEBUG_0);
+    GPIO.out_w1ts = (1 << DEBUG_0);
 
 
 	int timer_idx = (int) para;
 	int timer_idx = (int) para;
 
 
@@ -134,7 +135,7 @@ void IRAM_ATTR rx_timer_isr(void *para)
 		portYIELD_FROM_ISR( );
 		portYIELD_FROM_ISR( );
 	}
 	}
 
 
-	//GPIO.out_w1tc = (1 << DEBUG_0);
+	GPIO.out_w1tc = (1 << DEBUG_0);
 
 
 }
 }
 
 
@@ -178,6 +179,14 @@ void TX_Task(void *pvParameters)
 	}
 	}
 }
 }
 
 
+// Phase Locked Loop (PLL) Parameters
+#define BAUD_RATE (1200)
+#define SAMPLES_BIT (SAMPLEFREQUENCY / BAUD_RATE)
+#define D_PLL_INC (SAMPLES_BIT * 1)
+#define D_PLL_MAX (D_PLL_INC * SAMPLES_BIT *1)
+#define D_PLL_MARGIN (1)
+
+uint8_t aprs_buf[256];
 
 
 void RX_Task(void *pvParameters)
 void RX_Task(void *pvParameters)
 {
 {
@@ -186,9 +195,13 @@ void RX_Task(void *pvParameters)
 	uint8_t count = 0;
 	uint8_t count = 0;
 
 
 	// algorithm variables
 	// algorithm variables
-	double E_s1, E_s2, result;
-	E_s1 = 0; E_s2 = 0;
-	int f_output = 0;
+	double E_s1=0, E_s2=0;
+	uint8_t raw_bit=0, previous_raw_bit=0;
+	int32_t d_pll=0, dpll_error=0, err_div=2;
+	int lpf_output = 0;
+
+	aprs_decoder_init();
+	frame_buffer_init(aprs_buf, 256);
 
 
 	// Sampling Semaphore
 	// Sampling Semaphore
 	xRadioRXISRSemaphore = xSemaphoreCreateBinary();
 	xRadioRXISRSemaphore = xSemaphoreCreateBinary();
@@ -208,23 +221,74 @@ void RX_Task(void *pvParameters)
 		if( xSemaphoreTake(xRadioRXISRSemaphore, portMAX_DELAY) == pdTRUE)
 		if( xSemaphoreTake(xRadioRXISRSemaphore, portMAX_DELAY) == pdTRUE)
 		{
 		{
 
 
-			//enable_debug_IO(DEBUG_0);
+			enable_debug_IO(DEBUG_0);
 			EXTERNAL_DATA = (int)cc1200_radio_read_CFM();
 			EXTERNAL_DATA = (int)cc1200_radio_read_CFM();
-			//disable_debug_IO(DEBUG_0);
-			//enable_debug_IO(DEBUG_0);
-			f_output += (50000 * (((int)EXTERNAL_DATA) - f_output)) / 100000;
+			disable_debug_IO(DEBUG_0);
+			enable_debug_IO(DEBUG_0);
+			lpf_output += (50000 * (((int)EXTERNAL_DATA) - lpf_output)) / 100000;
 			window_add((int) EXTERNAL_DATA);
 			window_add((int) EXTERNAL_DATA);
 			E_s1 = goertzelFilter(window, 1200, WINDOW_SIZE);
 			E_s1 = goertzelFilter(window, 1200, WINDOW_SIZE);
 			E_s2 = goertzelFilter(window, 2200, WINDOW_SIZE);
 			E_s2 = goertzelFilter(window, 2200, WINDOW_SIZE);
 			if (E_s1 > E_s2)
 			if (E_s1 > E_s2)
 			{
 			{
-				enable_debug_IO(DEBUG_0);
+				raw_bit = 1;
+			}
+			else
+			{
+				raw_bit = 0;
+			}
+
+			disable_debug_IO(DEBUG_0);
+			enable_debug_IO(DEBUG_0);
+			// DPLL for sampling
+			if (raw_bit != previous_raw_bit)
+			{
+				dpll_error = d_pll - (D_PLL_MAX / 2);
+				if (dpll_error >  (D_PLL_MARGIN))
+				{
+					d_pll -= get_rx_status() ? D_PLL_MARGIN:
+							(dpll_error + err_div / 2) / err_div;
+				}
+				else if (dpll_error < (-D_PLL_MARGIN))
+				{
+					d_pll += get_rx_status() ? D_PLL_MARGIN:
+							(dpll_error + err_div / 2) / err_div;
+				}
+			}
+
+			d_pll += D_PLL_INC;
+
+			disable_debug_IO(DEBUG_0);
+			enable_debug_IO(DEBUG_0);
+			if (d_pll >= D_PLL_MAX)
+			{
+				// set clock debug I/O high
+
+				// feed bit to aprs decoder algorithm
+				switch(aprs_decoder_feed_bit(raw_bit))
+				{
+					case NORMAL:
+						break;
+					case FRAME_DECODED:
+						printf("Success!\n");
+						ESP_LOG_BUFFER_HEXDUMP("APRS RX", aprs_buf, 100, ESP_LOG_INFO);
+						break;
+					case ERROR_FCS_MISMATCH:
+						//printf("FCS Incorrect!\n");
+						break;
+					default:
+						//printf("Weird Error\n");
+						break;
+				}
+
+				d_pll -= D_PLL_MAX;
 			}
 			}
 			else
 			else
 			{
 			{
-				disable_debug_IO(DEBUG_0);
+				// set clock debug I/O low
 			}
 			}
-			//disable_debug_IO(DEBUG_0);
+			previous_raw_bit = raw_bit;
+			disable_debug_IO(DEBUG_0);
 		}
 		}
 	}
 	}
 }
 }
@@ -251,19 +315,12 @@ void radio_init()
     timer_init(TIMER_GROUP_0, TIMER_0, &config);
     timer_init(TIMER_GROUP_0, TIMER_0, &config);
     timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
     timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
     timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 6666);
     timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 6666);
-    //timer_enable_intr(TIMER_GROUP_0, TIMER_0);
     timer_isr_register(TIMER_GROUP_0, TIMER_0, rx_timer_isr,
     timer_isr_register(TIMER_GROUP_0, TIMER_0, rx_timer_isr,
     		(void *) TIMER_0, ESP_INTR_FLAG_IRAM, NULL);
     		(void *) TIMER_0, ESP_INTR_FLAG_IRAM, NULL);
-    //timer_start(TIMER_GROUP_0, TIMER_0);
-
 
 
-	// Create Tasks
-//	xTaskCreatePinnedToCore(Radio_Controller_Task, "Radio_Controller", 1024*4, 0, 10, NULL, 0);
 	xTaskCreatePinnedToCore(TX_Task, "TX Task", 1024*4, 0, 2, NULL, 1);
 	xTaskCreatePinnedToCore(TX_Task, "TX Task", 1024*4, 0, 2, NULL, 1);
 	xTaskCreatePinnedToCore(RX_Task, "RX Task", 1024*4, 0, 1, NULL, 1);
 	xTaskCreatePinnedToCore(RX_Task, "RX Task", 1024*4, 0, 1, NULL, 1);
 
 
-
-//	xTaskCreatePinnedToCore(UART_Task, "UART Task", 1024*4, 0, 5, NULL, 0);
 }
 }
 
 
 void IRAM_ATTR app_main()
 void IRAM_ATTR app_main()