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:
@@ -126,8 +126,8 @@ extern ErrorStatus Uart_construct(struct Uart* self, struct UartParameters* para
|
||||
extern ErrorStatus Uart_getDefaultParameters(struct UartParameters* parameters);
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Uart_Write
|
||||
* Description of function
|
||||
* Uart_write
|
||||
* Writes length number of bytes from buffer to Uart object self
|
||||
*
|
||||
* @param self The UART class object
|
||||
* @param buffer Message string to send
|
||||
@@ -139,7 +139,27 @@ extern ErrorStatus Uart_getDefaultParameters(struct UartParameters* parameters);
|
||||
* @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);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -76,3 +76,16 @@ ErrorStatus IODevice_write(const struct IODevice* self, const char* buffer, size
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -455,36 +455,39 @@ static ErrorStatus initIO (void)
|
||||
gpio.GPIO_Typedef = GPIOA;
|
||||
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
|
||||
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
|
||||
adc1->channel[ADC_Channel_0].input = gpio;
|
||||
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
|
||||
adc1->channel[ADC_Channel_0].input = gpio;
|
||||
// Channel 1 - PA1
|
||||
gpio.GPIO_Typedef = GPIOA;
|
||||
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
|
||||
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
|
||||
adc1->channel[ADC_Channel_1].input = gpio;
|
||||
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
|
||||
adc1->channel[ADC_Channel_1].input = gpio;
|
||||
// Channel 2 - PA2
|
||||
gpio.GPIO_Typedef = GPIOA;
|
||||
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
|
||||
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
|
||||
adc1->channel[ADC_Channel_2].input = gpio;
|
||||
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
|
||||
adc1->channel[ADC_Channel_2].input = gpio;
|
||||
|
||||
|
||||
/* USART1 initialisation -------------------------------------------------*/
|
||||
// Init TX line
|
||||
_uart1.USART_TX.GPIO_Typedef = GPIOB;
|
||||
_uart1.USART_TX.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
_uart1.USART_TX.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
|
||||
_uart1.USART_TX.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(_uart1.USART_TX.GPIO_Typedef, &_uart1.USART_TX.GPIO_InitStruct);
|
||||
gpio.GPIO_Typedef = GPIOB;
|
||||
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
|
||||
gpio.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
uart1->USART_TX = gpio;
|
||||
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
|
||||
|
||||
|
||||
// Init RX line
|
||||
_uart1.USART_RX.GPIO_Typedef = GPIOB;
|
||||
_uart1.USART_RX.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
_uart1.USART_RX.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
|
||||
_uart1.USART_RX.GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(_uart1.USART_RX.GPIO_Typedef, &_uart1.USART_RX.GPIO_InitStruct);
|
||||
gpio.GPIO_Typedef = GPIOB;
|
||||
gpio.GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
gpio.GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
|
||||
uart1->USART_RX = gpio;
|
||||
GPIO_Init(gpio.GPIO_Typedef, &gpio.GPIO_InitStruct);
|
||||
|
||||
// Apply pin-remapping for UART1 I/Os (alternative I/Os usage)
|
||||
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
|
||||
|
||||
@@ -498,7 +501,7 @@ static ErrorStatus initIO (void)
|
||||
|
||||
// Init RX line
|
||||
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_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(uart3->USART_RX.GPIO_Typedef, &uart3->USART_RX.GPIO_InitStruct);
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
#include "uart.h"
|
||||
#include "misc.h"
|
||||
|
||||
#include "led.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
@@ -55,6 +55,7 @@
|
||||
|
||||
|
||||
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
|
||||
@@ -65,20 +66,20 @@ ErrorStatus Uart_construct(struct Uart* self, struct UartParameters* parameters)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
IODevice_construct(&self->device, NULL, write);
|
||||
IODevice_construct(&self->device, read, write);
|
||||
|
||||
//! Create semaphore to synchronize with USART interrupt handler
|
||||
vSemaphoreCreateBinary(self->txSemaphore);
|
||||
|
||||
USART_DeInit(self->USART_TypeDef);
|
||||
|
||||
self->USART_ClockInitStruct->USART_Clock = USART_Clock_Enable;
|
||||
self->USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
|
||||
self->USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
|
||||
self->USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
|
||||
|
||||
//! Enable USART clock
|
||||
USART_ClockInit(self->USART_TypeDef, self->USART_ClockInitStruct);
|
||||
// self->USART_ClockInitStruct->USART_Clock = USART_Clock_Enable;
|
||||
// self->USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
|
||||
// self->USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
|
||||
// self->USART_ClockInitStruct->USART_LastBit = USART_LastBit_Enable;
|
||||
//
|
||||
// //! Enable USART clock
|
||||
// USART_ClockInit(self->USART_TypeDef, self->USART_ClockInitStruct);
|
||||
|
||||
// Initialise the UART
|
||||
self->USART_InitStruct.USART_BaudRate = parameters->baudrate;
|
||||
@@ -117,6 +118,14 @@ ErrorStatus Uart_construct(struct Uart* self, struct UartParameters* parameters)
|
||||
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)
|
||||
{
|
||||
//! 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)
|
||||
{
|
||||
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;
|
||||
ErrorStatus returnValue = SUCCESS; //! Define return variable
|
||||
@@ -202,8 +216,32 @@ ErrorStatus Uart_Write(struct Uart* self, const char* buffer, int length)
|
||||
{
|
||||
//! Do nothing
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user