Skip to content
Snippets Groups Projects
Commit 7f958463 authored by bbartels's avatar bbartels Committed by javey
Browse files

uart: consolidate receive buffer variable names

parent 7254a0d2
No related branches found
No related tags found
No related merge requests found
...@@ -23,12 +23,14 @@ void Handler(void *CallBackRef, u32 Event, unsigned int EventData); ...@@ -23,12 +23,14 @@ void Handler(void *CallBackRef, u32 Event, unsigned int EventData);
// Pointer to the UART driver instance // Pointer to the UART driver instance
static XUartPs* uartInstPtr; static XUartPs* uartInstPtr;
static u8 recvBuf[UART_BUF_SIZE]; static u8 recvBuf[UART_BUF_SIZE + MAX_PACKET_SIZE];
static volatile size_t recvBufBegin = 0;
static volatile size_t recvBufEnd = 0;
static volatile size_t received_in_buf = 0; static volatile size_t received_in_buf = 0;
static u8 secondaryBuf[UART_BUF_SIZE + MAX_PACKET_SIZE]; //static u8 secondaryBuf[UART_BUF_SIZE + MAX_PACKET_SIZE];
static volatile size_t secondaryEnd = 0; // Index of start of valid data in the secondary buffer //static volatile size_t secondaryEnd = 0; // Index of start of valid data in the secondary buffer
static size_t secondaryBegin = 0; // Index of one past end of valid data in the secondary buffer //static size_t secondaryBegin = 0; // Index of one past end of valid data in the secondary buffer
int initUartComms() { int initUartComms() {
uartInstPtr = uart0_init(COMM_UART_DEVICE_ID, BAUD_RATE); uartInstPtr = uart0_init(COMM_UART_DEVICE_ID, BAUD_RATE);
...@@ -110,19 +112,19 @@ int process_packet(unsigned char* packet, modular_structs_t *structs) { ...@@ -110,19 +112,19 @@ int process_packet(unsigned char* packet, modular_structs_t *structs) {
void parse_available(modular_structs_t *structs) { void parse_available(modular_structs_t *structs) {
// Discard all data before packet begin character // Discard all data before packet begin character
while (secondaryBegin < secondaryEnd && secondaryBuf[secondaryBegin] != BEGIN_CHAR) { while (recvBufBegin < recvBufEnd && recvBuf[recvBufBegin] != BEGIN_CHAR) {
secondaryBegin++; recvBufBegin++;
} }
// Minimum size of a packet (header + checksum) // Minimum size of a packet (header + checksum)
int min_packet_size = sizeof(metadata_t) + 1; int min_packet_size = sizeof(metadata_t) + 1;
// TODO: Loop limit? // TODO: Loop limit?
while (secondaryEnd - secondaryBegin >= min_packet_size) { while (recvBufEnd - recvBufBegin >= min_packet_size) {
unsigned char* packet_header = secondaryBuf + secondaryBegin; unsigned char* packet_header = recvBuf + recvBufBegin;
size_t packet_len = (packet_header[6] << 8) | (packet_header[5]); size_t packet_len = (packet_header[6] << 8) | (packet_header[5]);
if (secondaryEnd - secondaryBegin >= min_packet_size + packet_len) { if (recvBufEnd - recvBufBegin >= min_packet_size + packet_len) {
process_packet(secondaryBuf + secondaryBegin, structs); process_packet(recvBuf + recvBufBegin, structs);
secondaryBegin += min_packet_size + packet_len; recvBufBegin += min_packet_size + packet_len;
} }
else { else {
// Not enough data for a packet // Not enough data for a packet
...@@ -161,7 +163,7 @@ void process_received(modular_structs_t *structs) { ...@@ -161,7 +163,7 @@ void process_received(modular_structs_t *structs) {
// received_in_buf = 0; // received_in_buf = 0;
// // Re-enable interrupts and process copied data // // Re-enable interrupts and process copied data
// XUartPs_Recv(uartInstPtr, recvBuf, UART_BUF_SIZE); // XUartPs_Recv(uartInstPtr, recvBuf, UART_BUF_SIZE);
if (secondaryEnd != 0) { if (recvBufEnd != 0) {
volatile int asdasdf = 3; volatile int asdasdf = 3;
MIO7_led_off(); MIO7_led_off();
} }
...@@ -171,10 +173,10 @@ void process_received(modular_structs_t *structs) { ...@@ -171,10 +173,10 @@ void process_received(modular_structs_t *structs) {
u32 intr_state = disable_interrupts(); u32 intr_state = disable_interrupts();
// Move unprocessed bytes to front of secondary buffer // Move unprocessed bytes to front of secondary buffer
size_t unprocessed_size = secondaryEnd - secondaryBegin; size_t unprocessed_size = recvBufEnd - recvBufBegin;
memmove(secondaryBuf, secondaryBuf + secondaryBegin, unprocessed_size); memmove(recvBuf, recvBuf + recvBufBegin, unprocessed_size);
secondaryBegin = 0; recvBufBegin = 0;
secondaryEnd = unprocessed_size; recvBufEnd = unprocessed_size;
restore_interrupts(intr_state); restore_interrupts(intr_state);
//unsigned char in_fifo = XUartPs_ReadReg(uartInstPtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET); //unsigned char in_fifo = XUartPs_ReadReg(uartInstPtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET);
...@@ -199,12 +201,14 @@ void uartInterruptHandler(XUartPs *InstancePtr) { ...@@ -199,12 +201,14 @@ void uartInterruptHandler(XUartPs *InstancePtr) {
* Read the Channel Status Register to determine if there is any data in * Read the Channel Status Register to determine if there is any data in
* the RX FIFO * the RX FIFO
*/ */
CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
XUARTPS_SR_OFFSET); XUARTPS_SR_OFFSET);
while (0 == (CsrRegister & XUARTPS_SR_RXEMPTY)) { while (0 == (CsrRegister & XUARTPS_SR_RXEMPTY)) {
secondaryBuf[secondaryEnd] = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET); recvBuf[recvBufEnd] = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET);
secondaryEnd += 1; recvBufEnd += 1;
CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_SR_OFFSET); CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_SR_OFFSET);
MIO7_led_on(); MIO7_led_on();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment