Changeset - 5b9c23cdba0a
[Not reviewed]
default
0 4 0
mkanning@CL-SEC241-09.cedarville.edu - 13 years ago 2012-10-25 21:57:28
mkanning@CL-SEC241-09.cedarville.edu
parser for master module
4 files changed with 80 insertions and 41 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -11,6 +11,6 @@
 
 
#define F_CPU 11059200
 
#define USART_BAUDRATE 19200
 
 
#define MODULE_ID '1'
 
 
#endif /* CONFIG_H_ */
 
\ No newline at end of file
master/master/lib/serial.c
Show inline comments
 
@@ -13,7 +13,7 @@ void serial_SendChar( char byte )
 
	UDR0 = byte;
 
}
 
 
void serial_SendCommand( char moduleID, char measureType, uint8_t dataLength, char* data )
 
void serial_SendCommand( char moduleID, char sensorID, uint8_t dataLength, char* data )
 
{
 
	char checkSum = 0x00; //initialize checksum
 
	
 
@@ -21,8 +21,8 @@ void serial_SendCommand( char moduleID, 
 
	serial_SendChar(moduleID);
 
	checkSum+=moduleID;
 
	
 
	serial_SendChar(measureType);
 
	checkSum+=measureType;
 
	serial_SendChar(sensorID);
 
	checkSum+=sensorID;
 
	
 
	//send each character of data individually
 
	for (int i=0; i<dataLength; i++)
master/master/lib/serial.h
Show inline comments
 
@@ -12,7 +12,7 @@
 
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) -1 )
 
 
void serial_SendChar( char byte );
 
void serial_SendCommand( char moduleID, char measureType, uint8_t dataLength, char* data );
 
void serial_SendCommand( char moduleID, char sensorID, uint8_t dataLength, char* data );
 
void serial_SendResponseData();
 
void serial_Configure();
 
master/master/lib/serparser.c
Show inline comments
 
/*
 
 * serparser.c
 
 *
 
 * Created: 10/25/2012 8:11:43 PM
 
 *  Author: ethanzonca
 
 */ 
 
* serparser.c
 
*
 
* Created: 10/25/2012 8:11:43 PM
 
*  Author: ethanzonca
 
*/
 
 
 
// ************* Macros ***************
 
#define SERIAL_RX_HASBYTES UCSRA & _BV(RXC)
 
#define MAX_CMD_LEN 8
 
#define BROADCAST_ADDR 0
 
#define SERIAL_RX_HASBYTES UCSR0A & _BV(RXC)
 
#define MAX_CMD_LEN 16
 
#define BROADCAST_ADDR 0 //public address
 
#include <avr/io.h>
 
#include "../config.h"
 
 
//#define DEBUG
 
 
// ************* Command Definitions ***************
 
 
// Serial Commands
 
enum luma_cmd {  // CMD ID#
 
enum cmd // CMD ID#
 
{
 
	BOARDTEMP = 0, // 0
 
	PRESSURE,      // 1
 
};
 
 
// Data length of each command, in bytes
 
uint8_t cmd_len[] = {
 
	2, // BOARDTEMP_LEN
 
	0, // PRESSURE_LEN
 
};
 
 
// Incoming command buffer
 
uint8_t buffer[MAX_CMD_LEN+2];
 
 
// Current buffer location
 
uint8_t buffercount = 0;
 
uint8_t bufferPosition = 0;
 
 
//current length of the data transmission (and checksum)
 
int dataLength;
 
 
//data (and checksum) of most recent transmission
 
char data[16];
 
 
/* Get char from UART */
 
//ID of the sensor of most recent transmission
 
char sensorID;
 
 
/* return char from UART (h/w buffer) */
 
uint8_t uart_getchar(void)
 
{
 
	// Wait for chars
 
	while (!(UCSRA & (1<<RXC))){
 
	/*	while (!(UCSRA & (1<<RXC)))
 
	{
 
		idle();
 
	}
 
	return UDR;
 
	*/
 
	return 0;
 
}
 
 
 
 
/* Process incoming serial data */
 
int uart_machine(void) {
 
int uart_Parser(void)
 
{
 
 
	char byte;
 
 
	// Only process data if buffer has a byte
 
	if(SERIAL_RX_HASBYTES) {
 
		#ifdef DEBUG
 
		redflash();
 
		#endif
 
	if(SERIAL_RX_HASBYTES) // Only process data if buffer has a byte
 
	{
 
		
 
		// Pull byte off of the buffer
 
		byte = uart_getchar();
 
		buffer[buffercount] = byte;
 
 
		buffer[bufferPosition] = byte;
 
		
 
		// Start byte
 
		if(buffercount == 0) {
 
			if(byte == '[') {
 
				// If this is a start byte, we're OK. Keep reading.
 
				buffercount++;
 
		// byte checking/validation
 
		if(bufferPosition == 0)
 
		{
 
			if(byte == '[') // If this is a start byte, we're OK. Keep reading.
 
			{
 
				bufferPosition++;
 
			}
 
			else // Not a start byte that we're expecting. Reset to initial state.
 
			{
 
				bufferPosition = 0;
 
			}
 
			else {
 
				// Not a start byte that we're expecting. Reset to initial state.
 
				buffercount = 0;
 
		}
 
		else if(bufferPosition == 1)
 
		{
 
			if(byte == MODULE_ID) //this transmission is intended for the master; continue parsing
 
			{
 
				bufferPosition++;
 
				dataLength = 0; //reset dataLength here to protect from bad inputs
 
			}
 
			else //this transmission is intended for another module; stop parsing and reset
 
			{
 
				bufferPosition = 0;
 
			}
 
		}
 
		
 
		else if(bufferPosition == 2)
 
		{
 
			sensorID = byte; //store the type of data receiving
 
			bufferPosition++;
 
		}
 
		else if (bufferPosition == MAX_CMD_LEN) //command is too long and bad data has been recieved; reset
 
		{
 
			bufferPosition = 0;
 
		}
 
		else
 
		{
 
			if (byte == ']') //this is the end of the transmission
 
			{
 
				bufferPosition = 0;
 
			}
 
			else //still receiving data
 
			{
 
				data[dataLength] = byte;
 
				dataLength++;
 
			}
 
 
		}
 
		
 
	}
 
	else {
 
	else
 
	{
 
		return 0; // serial data not available
 
	}
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)