From 11106c0c472ad7b39ef87ecbd2d8d6f87a782429 Mon Sep 17 00:00:00 2001 From: Florent Duchon Date: Tue, 17 Apr 2012 22:15:37 +0200 Subject: digital/beacon: check USART bus state during TX transfer --- digital/beacon/src/configuration.h | 2 +- digital/beacon/src/debug.c | 49 +++++++++++++++++++++++++++++++++++--- digital/beacon/src/debug.h | 14 +++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) (limited to 'digital/beacon/src') diff --git a/digital/beacon/src/configuration.h b/digital/beacon/src/configuration.h index 5d42df85..dc7c2eac 100644 --- a/digital/beacon/src/configuration.h +++ b/digital/beacon/src/configuration.h @@ -21,7 +21,7 @@ #define APP_INTERFACE_UART 0x04 #define APP_INTERFACE_USBFIFO 0x05 #define APP_USART_RX_BUFFER_SIZE 100 /* Receive buffer size for USART. */ -#define APP_USART_TX_BUFFER_SIZE 100 /* Transmit buffer size for USART */ +#define APP_USART_TX_BUFFER_SIZE 500 /* Transmit buffer size for USART */ #define APP_INTERFACE APP_INTERFACE_USART /* Defines primary serial interface type to be used by application */ #define APP_USART_CHANNEL USART_CHANNEL_1 /* Defines USART interface name to be used by application.*/ diff --git a/digital/beacon/src/debug.c b/digital/beacon/src/debug.c index 9c30d77e..224934fe 100644 --- a/digital/beacon/src/debug.c +++ b/digital/beacon/src/debug.c @@ -23,6 +23,7 @@ * * }}} */ #include +#include #include "configuration.h" #include "debug.h" #include "servo.h" @@ -31,6 +32,14 @@ HAL_UsartDescriptor_t appUsartDescriptor; // USART descriptor (requi uint8_t usartRxBuffer[APP_USART_RX_BUFFER_SIZE]; // USART Rx buffer uint8_t usartTxBuffer[APP_USART_TX_BUFFER_SIZE]; // USART Tx buffer +TUSART_buffer_level TXbuffer_level = EMPTY; // TX buffer state +TUSART_bus_state TXbus_state = FREE; // TX line state + +uint16_t start_offset = 0; // Start offset for TX buffer +uint16_t end_offset = 0; // Stop offset for TX buffer + + + /* This function initializes the USART interface for debugging on avr */ void initSerialInterface(void) { @@ -45,11 +54,29 @@ uint8_t usartTxBuffer[APP_USART_TX_BUFFER_SIZE]; // USART Tx buffer appUsartDescriptor.txBuffer = NULL; // use callback mode appUsartDescriptor.txBufferLength = 0; appUsartDescriptor.rxCallback = usartRXCallback; - appUsartDescriptor.txCallback = NULL; + appUsartDescriptor.txCallback = usartTXCallback; appUsartDescriptor.flowControl = USART_FLOW_CONTROL_NONE; OPEN_USART(&appUsartDescriptor); } +/* TX USART Callback */ +void usartTXCallback(void) +{ + /* If buffer is not empty continue to send via USART line */ + if(TXbuffer_level != EMPTY) + { + WRITE_USART(&appUsartDescriptor,usartTxBuffer+start_offset,end_offset); + TXbuffer_level = FILLED; + } + else + { + /* Bus is free so reset variables and flags */ + memset(usartTxBuffer,0,APP_USART_TX_BUFFER_SIZE); + start_offset = 0; + end_offset = 0; + TXbus_state = FREE; + } +} /* RX USART Callback */ void usartRXCallback(uint16_t bytesToRead) @@ -86,7 +113,23 @@ void uprintf(char *format, ...) { va_list args; va_start(args,format); - vsprintf(usartTxBuffer,format,args); - WRITE_USART(&appUsartDescriptor,usartTxBuffer,strlen(usartTxBuffer)); + + if(end_offset+strlen(format)+strlen(args) < APP_USART_TX_BUFFER_SIZE) + { + vsprintf(usartTxBuffer+end_offset,format,args); + end_offset = strlen(usartTxBuffer); + + /* Check if the bus is busy */ + if(TXbus_state == FREE) + { + WRITE_USART(&appUsartDescriptor,usartTxBuffer+start_offset,end_offset); + start_offset = end_offset; + TXbus_state = BUSY; + } + else + { + TXbuffer_level = FILLED; + } + } va_end(args); } diff --git a/digital/beacon/src/debug.h b/digital/beacon/src/debug.h index 3e79501d..6ad1e074 100644 --- a/digital/beacon/src/debug.h +++ b/digital/beacon/src/debug.h @@ -79,9 +79,23 @@ #define DEBUG_TRUST(f,s...) ((void)0) #endif +typedef enum +{ + FREE, + BUSY +} TUSART_bus_state; + +typedef enum +{ + EMPTY, + FILLED +} TUSART_buffer_level; /* This function initializes the USART interface for debugging on avr */ void initSerialInterface(void); +/* TX USART Callback */ +void usartTXCallback(void); + /* RX USART Callback */ void usartRXCallback(uint16_t bytesToRead); -- cgit v1.2.3