Files @ be853f009bb8
Branch filter:

Location: seniordesign-firmware/master/master/lib/serial.c

ethanzonca@CL-ENS241-08.cedarville.edu
Started new infrastructure for requesting slave data.
/*
 * Master Firmware: USART Send/Recieve
 *
 * Wireless Observational Modular Aerial Network
 * 
 * Ethan Zonca
 * Matthew Kanning
 * Kyle Ripperger
 * Matthew Kroening
 *
 */

#include "serial.h"
#include "led.h"
#include "../config.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>

// NOTE: USART ISRs for character reception are included in serparser.c

void serial0_setup() {
	//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;
	
	UCSR0B |= (1 << RXCIE0); // Enable interrupt
	
	//UCSR0A |= (1<<RXC0);
}






void serial1_setup() {
//PROVEN in test project

	/* Enable receiver and transmitter */
	UCSR1B |= (1<<RXEN1)|(1<<TXEN1); // Enable rx/tx
	/* Set frame format: 8data, 1stop bit */
	UCSR1C |= (1 << UCSZ10) | (1 << UCSZ11); // - 1 stop bit
	
	/* Set baud rate */
	UBRR1H = (unsigned char)(USART1_BAUD_PRESCALE>>8);
	UBRR1L = (unsigned char)USART1_BAUD_PRESCALE;
	
	UCSR1B |= (1 << RXCIE1); // Enable interrupt
}

void serial1_ion() {
	UCSR1B |= (1<<RXEN1); // Enable rx
	UCSR1B |= (1 << RXCIE1); // Enable interrupt
}
void serial1_ioff() {
	UCSR1B &= ~(1<<RXEN1); // Disable rx
	UCSR1B &= ~(1 << RXCIE1); // Disable interrupt
}

void serial0_ion() {
	UCSR0B |= (1 << RXCIE0); // Enable interrupt
}
void serial0_ioff() {
	UCSR0B &= ~(1 << RXCIE0); // Disable interrupt
}


void serial0_sendChar( unsigned char byte )
{
	while (!(UCSR0A & (1<<UDRE0)));
	UDR0 = byte;
}

void serial1_sendChar( unsigned char byte )
{
	while (!(UCSR1A & (1<<UDRE1)));
	UDR1 = byte;
}


unsigned char serial0_readChar()
{
	while(!(UCSR0A &(1<<RXC0)));
	//char c = UDR0;
	//serial0_sendString("Read: ");
	//serial0_sendChar(c);
	//serial0_sendChar('\n');
	return UDR0;
}

uint8_t serial0_hasChar() {
	return (UCSR0A &(1<<RXC0));
}

char readBuf[128];
char* serial0_readLine() {
	char c;
	
	int i = 0;
	
	while(i < 128) {
		c = serial0_readChar();

		if(c == 0x0D) break;
		readBuf[i] = c;
		i++;
	}
	
	readBuf[i] = '\0';
	return readBuf;
}


void serial0_sendString(const char* stringPtr){
	while(*stringPtr != 0x00)
	{
		serial0_sendChar(*stringPtr);
		stringPtr++;
	}
}

void serial1_sendString(const char* stringPtr){
	while(*stringPtr != 0x00)
	{
		serial1_sendChar(*stringPtr);
		stringPtr++;
	}
}

void serial_sendCommand(char* data )
{
	char checkSum = 0x00; //initialize checksum
	
	serial0_sendChar('['); //bracket indicates start of command
		
	// send data, null-terminated
	while(*data != 0x00)
	{
		serial0_sendChar(*data);
		checkSum += *data;
		data++;
	}
		
	serial0_sendChar(']'); //bracket indicates end of command
	serial0_sendChar(checkSum); // checksum moved behind bracket to solve bug FS#29
}

void serial_sendResponseData()
{
	
}