Fixed UART RX problems

git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@232 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
mmi
2017-10-04 12:54:11 +00:00
parent 73a391f720
commit 8b315602e9
7 changed files with 115 additions and 51 deletions

View File

@@ -126,8 +126,8 @@ extern ErrorStatus Uart_construct(struct Uart* self, struct UartParameters* para
extern ErrorStatus Uart_getDefaultParameters(struct UartParameters* parameters); extern ErrorStatus Uart_getDefaultParameters(struct UartParameters* parameters);
/** ---------------------------------------------------------------------------- /** ----------------------------------------------------------------------------
* Uart_Write * Uart_write
* Description of function * Writes length number of bytes from buffer to Uart object self
* *
* @param self The UART class object * @param self The UART class object
* @param buffer Message string to send * @param buffer Message string to send
@@ -139,7 +139,27 @@ extern ErrorStatus Uart_getDefaultParameters(struct UartParameters* parameters);
* @todo * @todo
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
*/ */
extern ErrorStatus Uart_Write(struct Uart* self, const char* buffer, int length); extern ErrorStatus Uart_write(struct Uart* self, const char* buffer, int length);
/** ----------------------------------------------------------------------------
* Uart_read
* Reads length number of bytes from Uart object self into buffer. The actual
* number of read bytes are put in actualLength. Ususally they should be equal
* but in some cases less bytes are read than requested.
*
* @param self The UART class object
* @param buffer Message string to send
* @param length Message length
* @param actualLength THe actual number of bytes read
*
* @return ErrorStatus SUCCESS if writing message was successful
* ERROR otherwise
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus Uart_read(struct Uart* self, char* buffer, size_t length, size_t* actualLength);

View File

@@ -76,3 +76,16 @@ ErrorStatus IODevice_write(const struct IODevice* self, const char* buffer, size
return returnValue; return returnValue;
} }
ErrorStatus IODevice_read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength)
{
ErrorStatus returnValue = SUCCESS;
if (self->_read != NULL)
{
returnValue = self->_read(self, buffer, length, actualLength);
}
return returnValue;
}

View File

@@ -455,36 +455,39 @@ static ErrorStatus initIO (void)
gpio.GPIO_Typedef = GPIOA; gpio.GPIO_Typedef = GPIOA;
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
adc1->channel[ADC_Channel_0].input = gpio;
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct); GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
adc1->channel[ADC_Channel_0].input = gpio;
// Channel 1 - PA1 // Channel 1 - PA1
gpio.GPIO_Typedef = GPIOA; gpio.GPIO_Typedef = GPIOA;
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1; gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
adc1->channel[ADC_Channel_1].input = gpio;
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct); GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
adc1->channel[ADC_Channel_1].input = gpio;
// Channel 2 - PA2 // Channel 2 - PA2
gpio.GPIO_Typedef = GPIOA; gpio.GPIO_Typedef = GPIOA;
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
adc1->channel[ADC_Channel_2].input = gpio;
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct); GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
adc1->channel[ADC_Channel_2].input = gpio;
/* USART1 initialisation -------------------------------------------------*/ /* USART1 initialisation -------------------------------------------------*/
// Init TX line // Init TX line
_uart1.USART_TX.GPIO_Typedef = GPIOB; gpio.GPIO_Typedef = GPIOB;
_uart1.USART_TX.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
_uart1.USART_TX.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
_uart1.USART_TX.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; gpio.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(_uart1.USART_TX.GPIO_Typedef, &_uart1.USART_TX.GPIO_InitStruct); uart1->USART_TX = gpio;
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
// Init RX line // Init RX line
_uart1.USART_RX.GPIO_Typedef = GPIOB; gpio.GPIO_Typedef = GPIOB;
_uart1.USART_RX.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
_uart1.USART_RX.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
_uart1.USART_RX.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; uart1->USART_RX = gpio;
GPIO_Init(_uart1.USART_RX.GPIO_Typedef, &_uart1.USART_RX.GPIO_InitStruct); GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
// Apply pin-remapping for UART1 I/Os (alternative I/Os usage) // Apply pin-remapping for UART1 I/Os (alternative I/Os usage)
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
@@ -498,7 +501,7 @@ static ErrorStatus initIO (void)
// Init RX line // Init RX line
uart3->USART_RX.GPIO_Typedef = GPIOB; uart3->USART_RX.GPIO_Typedef = GPIOB;
uart3->USART_RX.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; uart3->USART_RX.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
uart3->USART_RX.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11; uart3->USART_RX.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
uart3->USART_RX.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; uart3->USART_RX.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(uart3->USART_RX.GPIO_Typedef, &uart3->USART_RX.GPIO_InitStruct); GPIO_Init(uart3->USART_RX.GPIO_Typedef, &uart3->USART_RX.GPIO_InitStruct);

View File

@@ -32,7 +32,7 @@
#include "uart.h" #include "uart.h"
#include "misc.h" #include "misc.h"
#include "led.h"
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Constant and macro definitions // Constant and macro definitions
@@ -55,6 +55,7 @@
static ErrorStatus write(const struct IODevice* self, const char* buffer, size_t length); static ErrorStatus write(const struct IODevice* self, const char* buffer, size_t length);
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength);
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Function definitions // Function definitions
@@ -65,20 +66,20 @@ ErrorStatus Uart_construct(struct Uart* self, struct UartParameters* parameters)
{ {
ErrorStatus returnValue = SUCCESS; ErrorStatus returnValue = SUCCESS;
IODevice_construct(&self->device, NULL, write); IODevice_construct(&self->device, read, write);
//! Create semaphore to synchronize with USART interrupt handler //! Create semaphore to synchronize with USART interrupt handler
vSemaphoreCreateBinary(self->txSemaphore); vSemaphoreCreateBinary(self->txSemaphore);
USART_DeInit(self->USART_TypeDef); USART_DeInit(self->USART_TypeDef);
self->USART_ClockInitStruct->USART_Clock = USART_Clock_Enable; // self->USART_ClockInitStruct->USART_Clock = USART_Clock_Enable;
self->USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge; // self->USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
self->USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low; // self->USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
self->USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable; // self->USART_ClockInitStruct->USART_LastBit = USART_LastBit_Enable;
//
//! Enable USART clock // //! Enable USART clock
USART_ClockInit(self->USART_TypeDef, self->USART_ClockInitStruct); // USART_ClockInit(self->USART_TypeDef, self->USART_ClockInitStruct);
// Initialise the UART // Initialise the UART
self->USART_InitStruct.USART_BaudRate = parameters->baudrate; self->USART_InitStruct.USART_BaudRate = parameters->baudrate;
@@ -117,6 +118,14 @@ ErrorStatus Uart_construct(struct Uart* self, struct UartParameters* parameters)
returnValue = ERROR; returnValue = ERROR;
} }
struct usartQueueItem tmp;
tmp.byte = 0x01;
xQueueSend(self->rxQueue, &tmp, 0);
tmp.byte++;
xQueueSend(self->rxQueue, &tmp, 0);
tmp.byte++;
xQueueSend(self->rxQueue, &tmp, 0);
if (returnValue == SUCCESS) if (returnValue == SUCCESS)
{ {
//! Enable the UART RX not empty interrupt //! Enable the UART RX not empty interrupt
@@ -146,11 +155,16 @@ ErrorStatus Uart_getDefaultParameters(struct UartParameters* parameters)
static ErrorStatus write(const struct IODevice* self, const char* buffer, size_t length) static ErrorStatus write(const struct IODevice* self, const char* buffer, size_t length)
{ {
return Uart_Write((struct Uart*)self, buffer, length); return Uart_write((struct Uart*)self, buffer, length);
} }
ErrorStatus Uart_Write(struct Uart* self, const char* buffer, int length) static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength)
{
return Uart_read((struct Uart*)self, buffer, length, actualLength);
}
ErrorStatus Uart_write(struct Uart* self, const char* buffer, int length)
{ {
struct usartQueueItem usartTxItem; struct usartQueueItem usartTxItem;
ErrorStatus returnValue = SUCCESS; //! Define return variable ErrorStatus returnValue = SUCCESS; //! Define return variable
@@ -202,8 +216,32 @@ ErrorStatus Uart_Write(struct Uart* self, const char* buffer, int length)
{ {
//! Do nothing //! Do nothing
} }
return (returnValue); //! Return result to caller return (returnValue); //! Return result to caller
} }
ErrorStatus Uart_read (struct Uart* self, char* buffer, size_t length, size_t* actualLength)
{
ErrorStatus returnValue = SUCCESS;
int loopCounter = 0;
*actualLength = 0;
struct usartQueueItem usartRxItem;
for (loopCounter = 0; loopCounter < length; loopCounter++)
{
if (xQueueReceive(self->rxQueue, &usartRxItem, 0) != pdFALSE)
{
// Item successfully fetched from Queue
buffer[loopCounter] = usartRxItem.byte;
*actualLength = *actualLength + 1;
}
else
{
break;
}
}
return returnValue;
}

View File

@@ -327,6 +327,12 @@ static void loggerTask(void* parameters)
IODevice_write(loggerDevice, str, strlen(str)); IODevice_write(loggerDevice, str, strlen(str));
#endif #endif
char buffer[5] = {0,};
size_t actualLength = 0;
IODevice_read(loggerDevice, buffer, 5, &actualLength);
snprintf(str, sizeof(str) / sizeof(str[0]), "%d - %x %x %x %x %x", actualLength, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
IODevice_write(loggerDevice, str, strlen(str));
} }
} }
} }

View File

@@ -170,7 +170,7 @@ static void initTask(void* parameters)
xTaskCreate(ledBlinkTask, (const char* const)"ledTask", 40, &ledTaskArguments, 0, &ledTaskHandle); xTaskCreate(ledBlinkTask, (const char* const)"ledTask", 40, &ledTaskArguments, 0, &ledTaskHandle);
Logger_construct(&uart1->device); Logger_construct(&uart3->device);
NHD0420_construct(&nhd0420, &spiDisplay->device); NHD0420_construct(&nhd0420, &spiDisplay->device);

View File

@@ -142,11 +142,8 @@ void USART1_IRQHandler(void)
//! Transmission register empty interrupt //! Transmission register empty interrupt
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{ {
//! Receive element from usart transmission queue //! Receive element from usart transmission queue
struct usartQueueItem usartTxItem; struct usartQueueItem usartTxItem;
xQueueReceiveFromISR(uart1->txQueue, &usartTxItem, &higherPriorityTaskWoken); xQueueReceiveFromISR(uart1->txQueue, &usartTxItem, &higherPriorityTaskWoken);
//! Write one byte to the transmit data register //! Write one byte to the transmit data register
USART_SendData(USART1, usartTxItem.byte); USART_SendData(USART1, usartTxItem.byte);
@@ -163,11 +160,10 @@ void USART1_IRQHandler(void)
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{ {
//! Read one byte from the receive data register //! Read one byte from the receive data register
struct usartQueueItem usartRxItem; struct usartQueueItem usartRxItem;
//! Reading from reception register automatically clears the RXNE interrupt //! Reading from reception register automatically clears the RXNE interrupt
usartRxItem.byte = (unsigned char) 0xFF & USART_ReceiveData(USART1); usartRxItem.byte = USART_ReceiveData(USART1);
//! Add the byte to the bluetooth RX queue //! Add the byte to the USART RX queue
//! In case of a full queue, the data is dumped //! In case of a full queue, the data is dumped
(void)xQueueSendFromISR(uart1->rxQueue, &usartRxItem, &higherPriorityTaskWoken); (void)xQueueSendFromISR(uart1->rxQueue, &usartRxItem, &higherPriorityTaskWoken);
} }
@@ -195,11 +191,8 @@ void USART3_IRQHandler(void)
//! Transmission register empty interrupt //! Transmission register empty interrupt
if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET) if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
{ {
//! Receive element from usart transmission queue //! Receive element from usart transmission queue
struct usartQueueItem usartTxItem; struct usartQueueItem usartTxItem;
xQueueReceiveFromISR(uart3->txQueue, &usartTxItem, &higherPriorityTaskWoken); xQueueReceiveFromISR(uart3->txQueue, &usartTxItem, &higherPriorityTaskWoken);
//! Write one byte to the transmit data register //! Write one byte to the transmit data register
USART_SendData(USART3, usartTxItem.byte); USART_SendData(USART3, usartTxItem.byte);
@@ -216,11 +209,10 @@ void USART3_IRQHandler(void)
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{ {
//! Read one byte from the receive data register //! Read one byte from the receive data register
struct usartQueueItem usartRxItem; struct usartQueueItem usartRxItem;
//! Reading from reception register automatically clears the RXNE interrupt //! Reading from reception register automatically clears the RXNE interrupt
usartRxItem.byte = (unsigned char) 0xFF & USART_ReceiveData(USART3); usartRxItem.byte = (char)USART_ReceiveData(USART3);
//! Add the byte to the bluetooth RX queue //! Add the byte to the USART RX queue
//! In case of a full queue, the data is dumped //! In case of a full queue, the data is dumped
(void)xQueueSendFromISR(uart3->rxQueue, &usartRxItem, &higherPriorityTaskWoken); (void)xQueueSendFromISR(uart3->rxQueue, &usartRxItem, &higherPriorityTaskWoken);
} }
@@ -387,14 +379,6 @@ void RTC_IRQHandler(void)
xSemaphoreGiveFromISR(rtc->secondSync, &higherPriorityTaskWoken); xSemaphoreGiveFromISR(rtc->secondSync, &higherPriorityTaskWoken);
Display_feedRefreshCounter(display); Display_feedRefreshCounter(display);
if (ledGreen->status)
{
LED_turnOff(ledGreen);
}
else
{
LED_turnOn(ledGreen);
}
/* Wait until last write operation on RTC registers has finished */ /* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask(); RTC_WaitForLastTask();