Changeset - 2e9d4bb3d9c7
[Not reviewed]
default
0 4 0
ethanzonca@CL-SEC241-08.cedarville.edu - 13 years ago 2012-11-29 15:34:24
ethanzonca@CL-SEC241-08.cedarville.edu
Circular buffer serial parser is operational
4 files changed with 70 insertions and 28 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -60,8 +60,8 @@
 
// USART config (serial.c)
 
// --------------------------------------------------------------------------
 
 
#define USART0_BAUDRATE 115200
 
#define USART1_BAUDRATE 115200
 
#define USART0_BAUDRATE 9600
 
#define USART1_BAUDRATE 9600
 
 
 
// --------------------------------------------------------------------------
master/master/lib/serial.c
Show inline comments
 
@@ -11,26 +11,41 @@
 
 */
 
 
#include "serial.h"
 
#include "led.h"
 
#include "../config.h"
 
#include <avr/io.h>
 
#include <avr/interrupt.h>
 
 
// NOTE: USART ISRs for charachter reception are included in serparser.c
 
// NOTE: USART ISRs for character reception are included in serparser.c
 
 
void serial0_setup() {
 
	PORTD &= ~(1<<PD0);
 
	PORTD |= (1<<PD1);
 
	//PORTD &= ~(1<<PD0);
 
	//PORTD |= (1<<PD1);
 
	
 
	/* Enable receiver and transmitter */
 
	UCSR0B |= (1<<RXEN0)|(1<<TXEN0); // Enable rx/tx
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01); // - 1 stop bit
 
	
 
	/* Set baud rate */
 
	UBRR0H = (unsigned char)(USART0_BAUD_PRESCALE>>8);
 
	UBRR0L = (unsigned char)USART0_BAUD_PRESCALE;
 
	
 
	UCSR0A |= (1<<RXC0);
 
	/* Enable receiver and transmitter */
 
	UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(RXCIE0); // Enable rx/tx/rx interrupt
 
	/* Set frame format: 8data, 1stop bit */
 
	UCSR0C = (3<<UCSZ00); // - 1 stop bit
 
	UCSR0B |= (1 << RXCIE0); // Enable interrupt
 
	
 
	//UCSR0A |= (1<<RXC0);
 
 
 
}
 
 
/*
 
uint8_t test = 0;
 
ISR(USART0__RX_vect)
 
{
 
	led_on(POWER);
 
	test = UDR0;
 
}*/
 
 
void serial1_setup() {
 
	/* Set baud rate */
 
	UBRR1H = (unsigned char)(USART1_BAUD_PRESCALE>>8);
master/master/lib/serparser.c
Show inline comments
 
@@ -12,10 +12,11 @@
 
 
 
#include <avr/io.h>
 
#include <avr/interrupt.h>
 
#include "../config.h"
 
#include "serial.h"
 
#include "serparser.h"
 
 
#include "led.h"
 
 
// Circular buffer for incoming data
 
uint8_t buffer[BUFFER_SIZE];
 
@@ -60,16 +61,26 @@ static void setParserState(uint8_t state
 
}
 
 
// Receive data on USART
 
ISR(USART0_RX_vect) 
 
 
char buffmeh[16];
 
 
ISR(USART0__RX_vect)
 
{
 
	led_on(POWER);
 
	buffer[bufferDataPosition % BUFFER_SIZE] = UDR0;
 
	bufferDataPosition = (bufferDataPosition + 1) % BUFFER_SIZE;
 
	sprintf(buffmeh, "bdp: %d, bpp: %d \r\n", bufferDataPosition, bufferParsePosition);
 
	serial0_sendString((buffmeh));
 
}
 
 
 
 
#define DEBUG
 
 
// Parse data from circular buffer
 
int serparser_parse(void)
 
{
 
	
 
	char byte;
 
 
	// Process first command (if any) on the circular buffer
 
@@ -82,12 +93,16 @@ int serparser_parse(void)
 
		{
 
			if(byte == '[') // Start of frame; keep parsing
 
			{
 
				serial0_sendString("PARSER: start ok\r\n");
 
				#ifdef DEBUG
 
				serial0_sendString("start ok\r\n");
 
				#endif
 
				setParserState(STATE_GETID);
 
			}
 
			else // Not start of frame, reset
 
			{
 
				serial0_sendString("PARSER: not start, reset\r\n");
 
				#ifdef DEBUG
 
				serial0_sendString("not start\r\n");
 
				#endif
 
				setParserState(STATE_RESET);
 
				return PARSERESULT_NODATA; // no valid start bit; better luck next time. run the function the next time around the main loop.
 
			}
 
@@ -98,13 +113,17 @@ int serparser_parse(void)
 
		{
 
			if(byte == MODULE_ID) // Message intended for this module; keep parsing
 
			{
 
				serial0_sendString("SER: dest ok\r\n");
 
				#ifdef DEBUG
 
				serial0_sendString("dest ok\r\n");
 
				#endif
 
				checksumCalc += byte;
 
				setParserState(STATE_GETDATATYPE);
 
			}
 
			else // Transmission is intended for another module; reset
 
			{
 
				serial0_sendString("SER: bad dest, reset\r\n");
 
				#ifdef DEBUG
 
				serial0_sendString("bad dest\r\n");
 
				#endif
 
				setParserState(STATE_RESET);
 
			}
 
		}
 
@@ -112,7 +131,9 @@ int serparser_parse(void)
 
		// Get payload type ID
 
		else if(parserState == STATE_GETDATATYPE)
 
		{
 
			serial0_sendString("SER: type ok\r\n");
 
			#ifdef DEBUG
 
			serial0_sendString("type ok\r\n");
 
			#endif
 
			receivedDataType = byte; // Store the type of data receiving
 
			checksumCalc += byte;
 
			setParserState(STATE_GETDATA);
 
@@ -123,7 +144,9 @@ int serparser_parse(void)
 
		{		
 
			if (byte == ']') // End of frame
 
			{
 
				serial0_sendString("SER: eof ok\r\n");
 
				#ifdef DEBUG
 
				serial0_sendString("eof ok\r\n");
 
				#endif
 
				if(bufferParsePosition == bufferDataPosition) 
 
				{
 
					// We are at the end of the line. No more data to parse.
 
@@ -140,14 +163,18 @@ int serparser_parse(void)
 
			}
 
			else // Still receiving data
 
			{
 
				serial0_sendString("SER: data ok\r\n");
 
				#ifdef DEBUG
 
				serial0_sendString("data ok\r\n");
 
				#endif
 
				receivedPayload[dataLength] = byte;
 
				dataLength++;
 
				checksumCalc += byte;
 
				
 
				// Data buffer overrun protection
 
				if(dataLength > MAX_PAYLOAD_LEN) {
 
					serial0_sendString("SER: buf overflow, reset\r\n");
 
					#ifdef DEBUG
 
					serial0_sendString("buf ovf\r\n");
 
					#endif
 
					setParserState(STATE_RESET);
 
					return PARSERESULT_FAIL;
 
				}
master/master/master.c
Show inline comments
 
@@ -54,7 +54,7 @@ int main(void)
 
	serial0_sendString("HAB Controller 1.0 - Initialized!\r\n");
 
	serial0_sendString("---------------------------------\r\n\r\n");
 
	
 
	led_on(POWER);
 
	//led_on(POWER);
 
	
 
	// Buffer for string operations
 
	char logbuf[32];
 
@@ -92,8 +92,8 @@ int main(void)
 
			led_on(STAT);
 
			snprintf(logbuf, 32, "%lu,%lu,%lu,\r\n", time_millis(), lastAprsBroadcast,lastLog);
 
			logger_log(logbuf);
 
			serial0_sendString("SD Logger: ");
 
			serial0_sendString(logBufPtr);
 
			//serial0_sendString("SD Logger: ");
 
			//serial0_sendString(logBufPtr);
 
			led_off(STAT);
 
			lastLog = time_millis();
 
		}		
 
@@ -103,7 +103,7 @@ int main(void)
 
		{
 
			while(afsk_busy());
 
			aprs_send(); // non-blocking
 
			serial0_sendString("Initiating APRS transmission...\r\n");
 
			//serial0_sendString("Initiating APRS transmission...\r\n");
 
			
 
			// Start getting values for next transmission
 
			isProcessing = true;
 
@@ -115,10 +115,10 @@ int main(void)
 
		
 
		// TODO: If we receive a command, we should still process it even if we aren't isProcessing, right?
 
		// Maybe we should isolate the received command handling ("Execution") and the issuing of requests.
 
		if(isProcessing) 
 
		{
 
			isProcessing = slavesensors_process(parseResult);
 
		}
 
		//if(isProcessing) 
 
		//{
 
		//	isProcessing = slavesensors_process(parseResult);
 
		//}
 
		wdt_reset();
 
    }
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)