diff --git a/quad/sw/comm_dev/src/new_comm.c b/quad/sw/comm_dev/src/new_comm.c index 17a9274a4f0c1549234267e1ba9c3bf171f38f9e..19ed16a02d2f7cd270af76c7800597cd7e9db9b8 100644 --- a/quad/sw/comm_dev/src/new_comm.c +++ b/quad/sw/comm_dev/src/new_comm.c @@ -23,12 +23,14 @@ void Handler(void *CallBackRef, u32 Event, unsigned int EventData); // Pointer to the UART driver instance 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 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 size_t secondaryBegin = 0; // Index of one past end of valid data in the secondary buffer +//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 size_t secondaryBegin = 0; // Index of one past end of valid data in the secondary buffer int initUartComms() { uartInstPtr = uart0_init(COMM_UART_DEVICE_ID, BAUD_RATE); @@ -110,19 +112,19 @@ int process_packet(unsigned char* packet, modular_structs_t *structs) { void parse_available(modular_structs_t *structs) { // Discard all data before packet begin character - while (secondaryBegin < secondaryEnd && secondaryBuf[secondaryBegin] != BEGIN_CHAR) { - secondaryBegin++; + while (recvBufBegin < recvBufEnd && recvBuf[recvBufBegin] != BEGIN_CHAR) { + recvBufBegin++; } // Minimum size of a packet (header + checksum) int min_packet_size = sizeof(metadata_t) + 1; // TODO: Loop limit? - while (secondaryEnd - secondaryBegin >= min_packet_size) { - unsigned char* packet_header = secondaryBuf + secondaryBegin; + while (recvBufEnd - recvBufBegin >= min_packet_size) { + unsigned char* packet_header = recvBuf + recvBufBegin; size_t packet_len = (packet_header[6] << 8) | (packet_header[5]); - if (secondaryEnd - secondaryBegin >= min_packet_size + packet_len) { - process_packet(secondaryBuf + secondaryBegin, structs); - secondaryBegin += min_packet_size + packet_len; + if (recvBufEnd - recvBufBegin >= min_packet_size + packet_len) { + process_packet(recvBuf + recvBufBegin, structs); + recvBufBegin += min_packet_size + packet_len; } else { // Not enough data for a packet @@ -161,7 +163,7 @@ void process_received(modular_structs_t *structs) { // received_in_buf = 0; // // Re-enable interrupts and process copied data // XUartPs_Recv(uartInstPtr, recvBuf, UART_BUF_SIZE); - if (secondaryEnd != 0) { + if (recvBufEnd != 0) { volatile int asdasdf = 3; MIO7_led_off(); } @@ -171,10 +173,10 @@ void process_received(modular_structs_t *structs) { u32 intr_state = disable_interrupts(); // Move unprocessed bytes to front of secondary buffer - size_t unprocessed_size = secondaryEnd - secondaryBegin; - memmove(secondaryBuf, secondaryBuf + secondaryBegin, unprocessed_size); - secondaryBegin = 0; - secondaryEnd = unprocessed_size; + size_t unprocessed_size = recvBufEnd - recvBufBegin; + memmove(recvBuf, recvBuf + recvBufBegin, unprocessed_size); + recvBufBegin = 0; + recvBufEnd = unprocessed_size; restore_interrupts(intr_state); //unsigned char in_fifo = XUartPs_ReadReg(uartInstPtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET); @@ -199,12 +201,14 @@ void uartInterruptHandler(XUartPs *InstancePtr) { * Read the Channel Status Register to determine if there is any data in * the RX FIFO */ + CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_SR_OFFSET); while (0 == (CsrRegister & XUARTPS_SR_RXEMPTY)) { - secondaryBuf[secondaryEnd] = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET); - secondaryEnd += 1; + recvBuf[recvBufEnd] = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET); + recvBufEnd += 1; + CsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_SR_OFFSET); MIO7_led_on(); }