- added IODevice support
- fixed some issues with the logger and stack sizes git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@216 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
@@ -23,10 +23,7 @@ CCFLAGS = -c -O2 -Wall -g -fno-common -mcpu=cortex-m3 -mthumb \
|
||||
ARFLAGS = rs
|
||||
|
||||
OBJECTS = \
|
||||
stm32f10x_it.o \
|
||||
led.o \
|
||||
spi.o \
|
||||
uart.o \
|
||||
IODevice.o
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file led.h
|
||||
/// @file IODevice.h
|
||||
/// @brief File description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
@@ -11,29 +11,30 @@
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
/// $Revision$
|
||||
/// $Author$
|
||||
/// $Date$
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @defgroup {group_name} {group_description}
|
||||
/// Description
|
||||
|
||||
/// @file led.h
|
||||
/// @file IODevice.h
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
#ifndef LED_INC_LED_H_
|
||||
#define LED_INC_LED_H_
|
||||
#ifndef MISC_INC_IODEVICE_H_
|
||||
#define MISC_INC_IODEVICE_H_
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "platform.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "stm32f10x.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
@@ -42,46 +43,28 @@
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// Type definitions.
|
||||
// -----------------------------------------------------------------------------
|
||||
struct IODevice;
|
||||
|
||||
struct Led
|
||||
typedef ErrorStatus (*ReadFunction)(struct IODevice* self, char* buffer, size_t length, size_t* actualLength);
|
||||
typedef ErrorStatus (*WriteFunction)(struct IODevice* self, const char* buffer, size_t length);
|
||||
|
||||
struct IODevice
|
||||
{
|
||||
T_PL_GPIO ledGpio;
|
||||
bool status;
|
||||
ReadFunction _read;
|
||||
WriteFunction _write;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
extern ErrorStatus IODevice_construct (struct IODevice* self, ReadFunction read, WriteFunction write);
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* LED_turnOn
|
||||
* Turns on the LED identified with the ID
|
||||
*
|
||||
* @param ledID Unique identifier of the LED
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if init was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus LED_turnOn(struct Led* const led);
|
||||
extern ErrorStatus IODevice_write(struct IODevice* self, const char* buffer, size_t length);
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* LED_turnOff
|
||||
* Turns off the LED identified with the ID
|
||||
*
|
||||
* @param ledID Unique identifier of the LED
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if init was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus LED_turnOff(struct Led* const led);
|
||||
extern ErrorStatus IODevice_read(struct IODevice* self, char* buffer, size_t length, size_t* actualLength);
|
||||
|
||||
#endif /* LED_INC_LED_H_ */
|
||||
|
||||
#endif /* MISC_INC_IODEVICE_H_ */
|
||||
@@ -1,136 +0,0 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file spi.h
|
||||
/// @brief File description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
// Industrieweg 28, 9804 TG Noordhorn
|
||||
// Postbus 92, 9800 AB Zuidhorn
|
||||
// The Netherlands
|
||||
// Tel: +31 594 503020
|
||||
// Fax: +31 594 505825
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @defgroup {group_name} {group_description}
|
||||
/// Description
|
||||
|
||||
/// @file spi.h
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
#ifndef MISC_INC_SPI_H_
|
||||
#define MISC_INC_SPI_H_
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
struct spiQueueItem
|
||||
{
|
||||
char byte;
|
||||
};
|
||||
|
||||
struct Spi
|
||||
{
|
||||
SPI_TypeDef* SPI_TypeDef;
|
||||
SPI_InitTypeDef SPI_InitStruct;
|
||||
T_PL_GPIO SPI_CLK;
|
||||
T_PL_GPIO* SPI_CE;
|
||||
T_PL_GPIO SPI_MOSI;
|
||||
T_PL_GPIO SPI_MISO;
|
||||
SemaphoreHandle_t spiClaimed; //! Semaphore to protect SPI bus
|
||||
//! against multiple use
|
||||
SemaphoreHandle_t txSemaphore; //! Semaphore for transmit handler
|
||||
//! to allow wait state while
|
||||
//! transmission is active
|
||||
xQueueHandle txQueue; //! SPI Transfer queue identifier
|
||||
xQueueHandle rxQueue; //! SPI Receive queue identifier
|
||||
bool initialized;
|
||||
};
|
||||
|
||||
struct SpiDevice
|
||||
{
|
||||
struct Spi* spi;
|
||||
T_PL_GPIO SPI_CE;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Spi_construct
|
||||
* Description of function
|
||||
*
|
||||
* @param self The UART object to initialize
|
||||
* @param baudrate Baudrate to use
|
||||
* @param wordlength Wordlength for the UART
|
||||
* @param stopbits Number of stopbits to use
|
||||
* @param parity Parity of the UART
|
||||
* @param mode Mode (TX, RX, Both)
|
||||
* @param hwFlowControl Control of hardware flow control
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if writing message was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus SPI_construct(struct Spi* self, uint16_t SPI_Direction, uint16_t SPI_Mode, uint16_t SPI_DataSize, uint16_t SPI_CPOL, uint16_t SPI_CPHA, uint16_t SPI_NSS, uint16_t SPI_BaudRatePrescaler, uint16_t SPI_FirstBit, uint16_t SPI_CRCPolynomial, UBaseType_t txQueueSize, UBaseType_t rxQueueSize);
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* SPI_destruct
|
||||
* Destructor for SPI interface in argument "self"
|
||||
*
|
||||
* @param self SPI to destruct
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if destruct was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus SPI_destruct(struct Spi* self);
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Spi_Write
|
||||
* Write the data in buffer to the SPI in argument self
|
||||
*
|
||||
* @param self The UART class object
|
||||
* @param buffer Message string to send
|
||||
* @parm length Message length
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if writing message was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus SPI_write (struct SpiDevice* self, const uint8_t* buffer, int length);
|
||||
#endif /* MISC_INC_SPI_H_ */
|
||||
@@ -1,117 +0,0 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file uart.h
|
||||
/// @brief File description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
// Industrieweg 28, 9804 TG Noordhorn
|
||||
// Postbus 92, 9800 AB Zuidhorn
|
||||
// The Netherlands
|
||||
// Tel: +31 594 503020
|
||||
// Fax: +31 594 505825
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @defgroup {group_name} {group_description}
|
||||
/// Description
|
||||
|
||||
/// @file uart.h
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
#ifndef MISC_INC_UART_H_
|
||||
#define MISC_INC_UART_H_
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
struct usartQueueItem
|
||||
{
|
||||
char byte;
|
||||
};
|
||||
|
||||
struct UartParameters
|
||||
{
|
||||
int baudrate;
|
||||
};
|
||||
|
||||
struct Uart
|
||||
{
|
||||
USART_TypeDef* USART_TypeDef;
|
||||
USART_InitTypeDef USART_InitStruct;
|
||||
USART_ClockInitTypeDef* USART_ClockInitStruct;
|
||||
T_PL_GPIO USART_RX;
|
||||
T_PL_GPIO USART_TX;
|
||||
T_PL_GPIO USART_CTS;
|
||||
T_PL_GPIO USART_RTS;
|
||||
SemaphoreHandle_t txSemaphore; //! Semaphore for transmit handler
|
||||
xQueueHandle txQueue; //! USART Transfer queue identifier
|
||||
xQueueHandle rxQueue; //! USART Receive queue identifier
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Uart_Init
|
||||
* Description of function
|
||||
*
|
||||
* @param _self The UART object to initialize
|
||||
* @param baudrate Baudrate to use
|
||||
* @param wordlength Wordlength for the UART
|
||||
* @param stopbits Number of stopbits to use
|
||||
* @param parity Parity of the UART
|
||||
* @param mode Mode (TX, RX, Both)
|
||||
* @param hwFlowControl Control of hardware flow control
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if writing message was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus Uart_Init(struct Uart* _self, uint32_t baudrate, uint16_t wordlength, uint16_t stopbits, uint16_t parity, uint16_t mode, uint16_t hwFlowControl, UBaseType_t txQueueSize, UBaseType_t rxQueueSize);
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Uart_Write
|
||||
* Description of function
|
||||
*
|
||||
* @param _self The UART class object
|
||||
* @param buffer Message string to send
|
||||
* @parm length Message length
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if writing message was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus Uart_Write(struct Uart* _self, const uint8_t* buffer, int length);
|
||||
|
||||
|
||||
|
||||
#endif /* MISC_INC_UART_H_ */
|
||||
@@ -1,5 +1,5 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file led.c
|
||||
/// @file IODevice.c
|
||||
/// @brief Description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
@@ -11,13 +11,13 @@
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
/// $Revision$
|
||||
/// $Author$
|
||||
/// $Date$
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @file led.c
|
||||
/// @file IODevice.c
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
@@ -25,8 +25,7 @@
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include "led.h"
|
||||
|
||||
#include "IODevice.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
@@ -39,12 +38,12 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -55,25 +54,25 @@
|
||||
// Function definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
ErrorStatus LED_turnOn(struct Led* const led)
|
||||
|
||||
ErrorStatus IODevice_construct (struct IODevice* self, ReadFunction read, WriteFunction write)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
GPIO_SetBits(led->ledGpio.GPIO_Typedef, led->ledGpio.GPIO_InitStruct.GPIO_Pin);
|
||||
led->status = true;
|
||||
self->_write = write;
|
||||
self->_read = read;
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus LED_turnOff(struct Led* const led)
|
||||
ErrorStatus IODevice_write(struct IODevice* self, const char* buffer, size_t length)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
GPIO_ResetBits(led->ledGpio.GPIO_Typedef, led->ledGpio.GPIO_InitStruct.GPIO_Pin);
|
||||
led->status = false;
|
||||
if (self->_write != NULL)
|
||||
{
|
||||
self->_write(self, buffer, length);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,203 +0,0 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file spi.c
|
||||
/// @brief Description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
// Industrieweg 28, 9804 TG Noordhorn
|
||||
// Postbus 92, 9800 AB Zuidhorn
|
||||
// The Netherlands
|
||||
// Tel: +31 594 503020
|
||||
// Fax: +31 594 505825
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @file spi.c
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "spi.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorStatus SPI_construct(struct Spi* self, uint16_t SPI_Direction, uint16_t SPI_Mode, uint16_t SPI_DataSize, uint16_t SPI_CPOL, uint16_t SPI_CPHA, uint16_t SPI_NSS, uint16_t SPI_BaudRatePrescaler, uint16_t SPI_FirstBit, uint16_t SPI_CRCPolynomial, UBaseType_t txQueueSize, UBaseType_t rxQueueSize)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
if (!self->initialized)
|
||||
{
|
||||
//! Create semaphore to synchronize with USART interrupt handler
|
||||
vSemaphoreCreateBinary(self->txSemaphore);
|
||||
|
||||
//! Create semaphore to avoid multiple usage
|
||||
vSemaphoreCreateBinary(self->spiClaimed);
|
||||
|
||||
SPI_I2S_DeInit(self->SPI_TypeDef);
|
||||
|
||||
self->SPI_InitStruct.SPI_Direction = SPI_Direction;
|
||||
self->SPI_InitStruct.SPI_Mode = SPI_Mode;
|
||||
self->SPI_InitStruct.SPI_DataSize = SPI_DataSize;
|
||||
self->SPI_InitStruct.SPI_CPOL = SPI_CPOL;
|
||||
self->SPI_InitStruct.SPI_CPHA = SPI_CPHA;
|
||||
self->SPI_InitStruct.SPI_NSS = SPI_NSS;
|
||||
self->SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler;
|
||||
self->SPI_InitStruct.SPI_FirstBit = SPI_FirstBit;
|
||||
self->SPI_InitStruct.SPI_CRCPolynomial = SPI_CRCPolynomial;
|
||||
SPI_Init(self->SPI_TypeDef, &self->SPI_InitStruct);
|
||||
|
||||
//! Enable USART interface
|
||||
SPI_Cmd(self->SPI_TypeDef, ENABLE);
|
||||
|
||||
//! Create a new FREERTOS queue to handle data from app to USART output
|
||||
self->txQueue = xQueueCreate(txQueueSize, sizeof(struct spiQueueItem));
|
||||
//! Create a new FREERTOS queue to handle data from USART input to app
|
||||
self->rxQueue = xQueueCreate(rxQueueSize, sizeof(struct spiQueueItem));
|
||||
//! Queue identifier must not be 0 (0 means that the queue is not available)
|
||||
if (self->txQueue == 0)
|
||||
{
|
||||
//! Queue identifier is 0 -> error
|
||||
returnValue = ERROR; //! Set error flag
|
||||
}
|
||||
if (self->rxQueue == 0)
|
||||
{
|
||||
//! Queue identifier is 0 -> error
|
||||
returnValue = ERROR; //! Set error flag
|
||||
}
|
||||
//! Queue identifier is not 0 -> queue is available
|
||||
|
||||
//! take txSemaphore
|
||||
if (xSemaphoreTake(self->txSemaphore, 0) == pdFALSE)
|
||||
{
|
||||
//! An error has occurred
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
//! Enable the UART RX not empty interrupt
|
||||
SPI_I2S_ITConfig(self->SPI_TypeDef, SPI_I2S_IT_RXNE, ENABLE);
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
self->initialized = true;
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus SPI_destruct (struct Spi* self)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
ErrorStatus SPI_write (struct SpiDevice* self, const uint8_t* buffer, int length)
|
||||
{
|
||||
struct spiQueueItem txItem;
|
||||
ErrorStatus returnValue = SUCCESS; //! Define return variable
|
||||
int txCounter; //! Define a loop counter var
|
||||
|
||||
xSemaphoreTake(self->spi->spiClaimed, portMAX_DELAY);
|
||||
self->spi->SPI_CE = &self->SPI_CE;
|
||||
|
||||
// De-select the current device to avoid start-issues
|
||||
if (self->spi->SPI_InitStruct.SPI_NSS == SPI_NSS_Soft)
|
||||
{
|
||||
GPIO_SetBits(self->spi->SPI_CE->GPIO_Typedef, self->spi->SPI_CE->GPIO_InitStruct.GPIO_Pin);
|
||||
}
|
||||
|
||||
//! Copy the incoming data into BLUETOOTH data structure
|
||||
for (txCounter = 0; txCounter < length; txCounter++)
|
||||
{
|
||||
txItem.byte = buffer[txCounter]; //! Copy current data in struct
|
||||
//! Add the current set of data to bluetooth transmission queue
|
||||
if (pdTRUE != xQueueSend(self->spi->txQueue, &txItem, 0))
|
||||
{
|
||||
//! Adding item was NOT successful - break out of loop
|
||||
returnValue = ERROR; //! Set return value to FALSE
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
if (self->spi->SPI_InitStruct.SPI_NSS == SPI_NSS_Soft)
|
||||
{
|
||||
GPIO_ResetBits(self->spi->SPI_CE->GPIO_Typedef, self->spi->SPI_CE->GPIO_InitStruct.GPIO_Pin);
|
||||
}
|
||||
|
||||
//! Semaphore has been taken
|
||||
//! Enable the SPI TXE (transmission empty) interrupt
|
||||
SPI_I2S_ITConfig(self->spi->SPI_TypeDef, SPI_I2S_IT_TXE, ENABLE);
|
||||
|
||||
//! Try to take Semaphore - If the USART transmission is still busy, the
|
||||
//! Semaphore cannot be taken - FREERTOS will suspend this task until the
|
||||
//! Semaphore is released again
|
||||
xSemaphoreTake(self->spi->txSemaphore, portMAX_DELAY);
|
||||
|
||||
/** Enabling the TX interrupt will immediately cause an interrupt because
|
||||
* the transmission register is still empty. The ISR will get the data
|
||||
* from the uart transmission queue and transmit byte-wise until the
|
||||
* queue is empty.
|
||||
* An empty queue will cause the transmission complete flag (TC) to be set,
|
||||
* which is polled
|
||||
*/
|
||||
while (SPI_I2S_GetFlagStatus(self->spi->SPI_TypeDef, SPI_I2S_FLAG_BSY) == SET)
|
||||
{
|
||||
//! The software must wait until TXE=1. The TXE flag remains cleared during
|
||||
//! all data transfers and it is set by hardware at the last frame's
|
||||
//! end of transmission
|
||||
}
|
||||
if (self->spi->SPI_InitStruct.SPI_NSS == SPI_NSS_Soft)
|
||||
{
|
||||
GPIO_SetBits(self->spi->SPI_CE->GPIO_Typedef, self->spi->SPI_CE->GPIO_InitStruct.GPIO_Pin);
|
||||
}
|
||||
xSemaphoreGive(self->spi->spiClaimed);
|
||||
}
|
||||
else
|
||||
{
|
||||
//! Do nothing
|
||||
}
|
||||
|
||||
|
||||
return (returnValue); //! Return result to caller
|
||||
}
|
||||
|
||||
@@ -1,179 +0,0 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file uart.c
|
||||
/// @brief Description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
// Industrieweg 28, 9804 TG Noordhorn
|
||||
// Postbus 92, 9800 AB Zuidhorn
|
||||
// The Netherlands
|
||||
// Tel: +31 594 503020
|
||||
// Fax: +31 594 505825
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @file uart.c
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
|
||||
#include "stm32f10x_usart.h"
|
||||
|
||||
#include "uart.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorStatus Uart_Init(struct Uart* _self, uint32_t baudrate, uint16_t wordlength, uint16_t stopbits, uint16_t parity, uint16_t mode, uint16_t hwFlowControl, UBaseType_t txQueueSize, UBaseType_t rxQueueSize)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
//! 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);
|
||||
|
||||
// Initialise the UART
|
||||
_self->USART_InitStruct.USART_BaudRate = baudrate;
|
||||
_self->USART_InitStruct.USART_WordLength = wordlength;
|
||||
_self->USART_InitStruct.USART_StopBits = stopbits;
|
||||
_self->USART_InitStruct.USART_Parity = parity;
|
||||
_self->USART_InitStruct.USART_Mode = mode;
|
||||
_self->USART_InitStruct.USART_HardwareFlowControl = hwFlowControl;
|
||||
|
||||
USART_Init(_self->USART_TypeDef, &_self->USART_InitStruct);
|
||||
|
||||
//! Enable USART interface
|
||||
USART_Cmd(_self->USART_TypeDef, ENABLE);
|
||||
|
||||
//! Create a new FREERTOS queue to handle data from app to USART output
|
||||
_self->txQueue = xQueueCreate(txQueueSize, sizeof(struct usartQueueItem));
|
||||
//! Create a new FREERTOS queue to handle data from USART input to app
|
||||
_self->rxQueue = xQueueCreate(rxQueueSize, sizeof(struct usartQueueItem));
|
||||
//! Queue identifier must not be 0 (0 means that the queue is not available)
|
||||
if (_self->txQueue == 0)
|
||||
{
|
||||
//! Queue identifier is 0 -> error
|
||||
returnValue = ERROR; //! Set error flag
|
||||
}
|
||||
if (_self->rxQueue == 0)
|
||||
{
|
||||
//! Queue identifier is 0 -> error
|
||||
returnValue = ERROR; //! Set error flag
|
||||
}
|
||||
//! Queue identifier is not 0 -> queue is available
|
||||
|
||||
//! take txSemaphore
|
||||
if (xSemaphoreTake(_self->txSemaphore, 0) == pdFALSE)
|
||||
{
|
||||
//! An error has occurred
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
//! Enable the UART RX not empty interrupt
|
||||
USART_ITConfig(_self->USART_TypeDef, USART_IT_RXNE, ENABLE);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus Uart_Write(struct Uart* _self, const uint8_t* buffer, int length)
|
||||
{
|
||||
struct usartQueueItem usartTxItem;
|
||||
ErrorStatus returnValue = SUCCESS; //! Define return variable
|
||||
int txCounter; //! Define a loop counter var
|
||||
|
||||
//! Copy the incoming data into BLUETOOTH data structure
|
||||
for (txCounter = 0; txCounter < length; txCounter++)
|
||||
{
|
||||
usartTxItem.byte = buffer[txCounter]; //! Copy current data in struct
|
||||
//! Add the current set of data to bluetooth transmission queue
|
||||
if (pdTRUE != xQueueSend(_self->txQueue, &usartTxItem, 0))
|
||||
{
|
||||
//! Adding item was NOT successful - break out of loop
|
||||
returnValue = ERROR; //! Set return value to FALSE
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
//! Semaphore has been taken
|
||||
//! Enable the USARTx TXE (transmission empty) interrupt
|
||||
USART_ITConfig(_self->USART_TypeDef, USART_IT_TXE, ENABLE);
|
||||
|
||||
//! Try to take Semaphore - If the USART transmission is still busy, the
|
||||
//! Semaphore cannot be taken - FREERTOS will suspend this task until the
|
||||
//! Semaphore is released again
|
||||
xSemaphoreTake(_self->txSemaphore, portMAX_DELAY);
|
||||
|
||||
/** Enabling the TX interrupt will immediately cause an interrupt because
|
||||
* the transmission register is still empty. The ISR will get the data
|
||||
* from the uart transmission queue and transmit byte-wise until the
|
||||
* queue is empty.
|
||||
* An empty queue will cause the transmission complete flag (TC) to be set,
|
||||
* which is polled
|
||||
*/
|
||||
while (USART_GetFlagStatus(_self->USART_TypeDef, USART_FLAG_TC) == RESET)
|
||||
{
|
||||
//! The software must wait until TC=1. The TC flag remains cleared during
|
||||
//! all data transfers and it is set by hardware at the last frame's
|
||||
//! end of transmission
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//! Do nothing
|
||||
}
|
||||
|
||||
|
||||
return (returnValue); //! Return result to caller
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user