Files
@ b22a378398b3
Branch filter:
Location: seniordesign-firmware/master/master/lib/serial.c
b22a378398b3
3.2 KiB
text/plain
fixed configuration issues
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | /*
* 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
// Configure USART0
void serial0_setup()
{
/* 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
}
// Configure USART1
void serial1_setup() {
/* 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
}
// Enable USART1 rx interrupt
void serial1_ion() {
UCSR1B |= (1<<RXEN1); // Enable rx
UCSR1B |= (1 << RXCIE1); // Enable interrupt
}
// Disable USART1 rx interrupt
void serial1_ioff() {
UCSR1B &= ~(1<<RXEN1); // Disable rx
UCSR1B &= ~(1 << RXCIE1); // Disable interrupt
}
// Enable USART0 rx interrupt
void serial0_ion() {
UCSR0B |= (1 << RXCIE0); // Enable interrupt
}
// Disable USART0 rx interrupt
void serial0_ioff() {
UCSR0B &= ~(1 << RXCIE0); // Disable interrupt
}
// Send char on USART0
void serial0_sendChar( unsigned char byte )
{
while (!(UCSR0A & (1<<UDRE0)));
UDR0 = byte;
}
// Send char on USART1
void serial1_sendChar( unsigned char byte )
{
while (!(UCSR1A & (1<<UDRE1)));
UDR1 = byte;
}
// Read char on USART0 (blocking)
unsigned char serial0_readChar()
{
while(!(UCSR0A &(1<<RXC0)));
return UDR0;
}
// Returns 1 if data in USART0 buffer
uint8_t serial0_hasChar() {
return (UCSR0A &(1<<RXC0));
}
// Read line from USART0 (blocking)
// TODO: Can we make readBuf local?
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;
}
// Send string on USART0
void serial0_sendString(const char* stringPtr){
while(*stringPtr != 0x00)
{
serial0_sendChar(*stringPtr);
stringPtr++;
}
}
// Send string on USART1
void serial1_sendString(const char* stringPtr){
while(*stringPtr != 0x00)
{
serial1_sendChar(*stringPtr);
stringPtr++;
}
}
// Send custom protocol command on USART0
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
}
|