-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This one prints out a lot. It's nice to visualize how it's all working.
- Loading branch information
Elliot Williams
committed
Oct 25, 2015
1 parent
f33be28
commit a793508
Showing
3 changed files
with
94 additions
and
109 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,119 @@ | ||
#include <avr/io.h> | ||
#include <avr/interrupt.h> | ||
#include <util/delay.h> | ||
#include "USART.h" | ||
#include "circular_buffer.h" | ||
|
||
volatile struct Buffer rx_buffer = {{}, 0, 0}; | ||
volatile struct Buffer tx_buffer = {{}, 0, 0}; | ||
#define BUFFER_SIZE 16 | ||
|
||
static inline void initUSART_interrupts(void){ | ||
UCSR0B |= (1<<RXCIE0); | ||
sei(); | ||
} | ||
static inline void enable_transmission(void){ | ||
UCSR0B |= (1<<UDRIE0); | ||
} | ||
static inline void disable_transmission(void){ | ||
UCSR0B &= ~(1<<UDRIE0); | ||
} | ||
enum BufferStatus {BUFFER_OK, BUFFER_EMPTY, BUFFER_FULL}; | ||
|
||
ISR(USART_RX_vect){ | ||
bufferWrite(&rx_buffer, UDR0); | ||
struct Buffer { | ||
uint8_t data[BUFFER_SIZE]; | ||
uint8_t newest_index; | ||
uint8_t oldest_index; | ||
}; | ||
struct Buffer buffer = {{}, 0, 0}; | ||
|
||
enum BufferStatus bufferWrite(uint8_t byte){ | ||
uint8_t next_index = (buffer.newest_index+1) % BUFFER_SIZE; | ||
|
||
if (next_index == buffer.oldest_index){ | ||
return BUFFER_FULL; | ||
} | ||
buffer.data[buffer.newest_index] = byte; | ||
buffer.newest_index = next_index; | ||
return BUFFER_OK; | ||
} | ||
|
||
ISR(USART_UDRE_vect){ | ||
uint8_t tempy; | ||
enum BufferStatus status_tx = bufferRead(&tx_buffer, &tempy); | ||
if (status_tx == BUFFER_EMPTY){ | ||
disable_transmission(); | ||
} else { | ||
UDR0 = tempy; /* write it out to the hardware serial */ | ||
enum BufferStatus bufferRead(uint8_t *byte){ | ||
if (buffer.newest_index == buffer.oldest_index){ | ||
return BUFFER_EMPTY; | ||
} | ||
*byte = buffer.data[buffer.oldest_index]; | ||
buffer.oldest_index = (buffer.oldest_index+1) % BUFFER_SIZE; | ||
return BUFFER_OK; | ||
} | ||
|
||
void dumpBuffer(void){ | ||
|
||
for (uint8_t i=0; i<BUFFER_SIZE; i++){ | ||
if (buffer.oldest_index == buffer.newest_index && buffer.newest_index == i){ | ||
transmitByte('='); | ||
} else if (buffer.oldest_index == i){ | ||
transmitByte('['); | ||
} else if (buffer.newest_index == i){ | ||
transmitByte(']'); | ||
} else { | ||
transmitByte('.'); | ||
} | ||
} | ||
printString("\n"); | ||
|
||
for (uint8_t i=0; i<BUFFER_SIZE; i++){ | ||
transmitByte(buffer.data[i]); | ||
} | ||
printString("\n"); | ||
} | ||
|
||
|
||
int main(void) { | ||
enum BufferStatus status; | ||
uint8_t input_byte; | ||
|
||
initUSART(); | ||
initUSART_interrupts(); | ||
_delay_ms(200); | ||
|
||
uint8_t i; | ||
uint8_t tempCharStorage; | ||
|
||
while (1) { | ||
|
||
status = bufferRead(&rx_buffer, &input_byte); | ||
if (status == BUFFER_OK){ | ||
bufferWrite(&tx_buffer, input_byte); | ||
enable_transmission(); | ||
dumpBuffer(); | ||
printString(" Demo: adding characters to buffer\n"); | ||
uint8_t coolString[] = "Howdy"; | ||
i = 0; | ||
while(i < sizeof(coolString) - 1){ | ||
bufferWrite(coolString[i]); | ||
++i; | ||
dumpBuffer(); | ||
} | ||
|
||
printString(" Demo: reading out the first three characters\n"); | ||
for (i = 0; i<3 ; i++){ | ||
bufferRead(&tempCharStorage); | ||
transmitByte(tempCharStorage); | ||
printString("\n"); | ||
dumpBuffer(); | ||
} | ||
|
||
printString(" Demo: adding more characters to buffer\n"); | ||
uint8_t anotherString[] = "Hello"; | ||
i = 0; | ||
while(i < sizeof(anotherString) - 1){ | ||
bufferWrite(anotherString[i]); | ||
++i; | ||
dumpBuffer(); | ||
} | ||
|
||
// And read it back out using return code | ||
printString(" Demo: reading everything back out\n"); | ||
while (bufferRead(&tempCharStorage) == BUFFER_OK){ | ||
transmitByte(tempCharStorage); | ||
} | ||
printString("\n"); | ||
|
||
dumpBuffer(); | ||
printString(" Demo: empty! (newest = oldest)\n"); | ||
|
||
// Fill up buffer, using return code | ||
i = 0; | ||
while(bufferWrite('A'+i) == BUFFER_OK){ | ||
++i; | ||
dumpBuffer(); | ||
} | ||
|
||
printString(" Note: never fills up whole buffer\n"); | ||
printString(" it's full when first index equals last\n"); | ||
|
||
return 0; | ||
|
||
} /* End event loop */ | ||
return 0; /* This line is never reached */ | ||
} | ||
|