Improvements:

- HAL re-organized
- FreeRTOS running stable
- UART finished
- SPI1 & SPI3 finished and functional
- Display driver added (functional)


git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@172 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
mmi
2017-09-20 06:51:53 +00:00
parent f5dd9e0f09
commit c9562e8bfd
313 changed files with 8279 additions and 50216 deletions

View File

@@ -0,0 +1,318 @@
// -----------------------------------------------------------------------------
/// @file Logger.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: 167 $
/// $Author: mmi $
/// $Date: 2017-09-12 13:09:10 +0200 (di, 12 sep 2017) $
// (c) 2017 Micro-Key bv
// -----------------------------------------------------------------------------
/// @file main.c
/// @ingroup {group_name}
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include "FreeRTOS.h"
#include "Logger.h"
#include "semphr.h"
#include "queue.h"
#include "task.h"
#include "misc.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include "uart.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
#define LOGQUEUE_SIZE (64)
#define LOGGER_STACK_SIZE (512)
#define LOGGER_TASK_PRIORITY (1)
// Makefile compile options:
// ENABLE_SERIAL_LOGGING: Use the serial port for logging.
#if defined(ENABLE_SERIAL_LOGGING)
#define ENABLE_LOGGING
#else
#undef ENABLE_LOGGING
#endif
// -----------------------------------------------------------------------------
// Type definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// File-scope variables
// -----------------------------------------------------------------------------
static struct Uart* loggerInterface;
static xQueueHandle logQueue;
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
static bool initialized = false;
static xTaskHandle loggerTaskHandle = NULL;
// -----------------------------------------------------------------------------
// Function definitions
// -----------------------------------------------------------------------------
static void composeLogQueueItem(struct LogQueueItem* logQueueItem, const char* fileName, const char* functionName,
int lineNumber, LogType logType, const char* context);
static void loggerTask(void* parameters);
/* ---------------------*
* Function definitions *
* ---------------------*
*/
void Logger_initialize(void* const interface)
{
if(!initialized)
{
ErrorStatus errorStatus = SUCCESS;
///TODO This is currently hardcoded into the UART - there must be a way for more generic usage
loggerInterface = (struct Uart* const) interface;
if(errorStatus == SUCCESS)
{
logQueue = xQueueCreate(LOGQUEUE_SIZE, sizeof(struct LogQueueItem));
if(logQueue == 0)
{
errorStatus = ERROR;
}
}
if(errorStatus == SUCCESS)
{
if(xTaskCreate(loggerTask, (const char*)"loggerTask", LOGGER_STACK_SIZE, NULL, LOGGER_TASK_PRIORITY, &loggerTaskHandle) != pdPASS)
{
errorStatus = ERROR;
}
}
if(errorStatus == SUCCESS)
{
initialized = true;
LOGGER_INFO("Logger started");
}
else
{
}
}
}
void Logger_terminate(void)
{
}
void Logger_log(const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* format, ...)
{
if(initialized)
{
static int nrLostMessages = 0;
static bool overflowRecovery = false;
int nrOfMessages;
struct LogQueueItem logQueueItem;
va_list ap;
nrOfMessages = uxQueueMessagesWaiting(&logQueue);
if((nrOfMessages == LOGQUEUE_SIZE - 1) && !overflowRecovery)
{
// Queue almost full, only one entry left. Log a warning instead
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, "Log queue overflow");
(void)xQueueSend(logQueue, &logQueueItem, 0);
overflowRecovery = true;
nrLostMessages = 1;
}
else if((nrOfMessages == 0) && overflowRecovery)
{
// Queue empty again after an overflow
char str[128];
snprintf(str, sizeof(str) / sizeof(str[0]), "%d messages lost", nrLostMessages);
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, str);
(void)xQueueSend(logQueue, &logQueueItem, 0);
overflowRecovery = false;
}
else if(!overflowRecovery)
{
// Normal behaviour, queue not full
char str[128];
va_start(ap, format);
vsnprintf(str, sizeof(str) / sizeof(str[0]), format, ap);
va_end(ap);
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, logType, str);
(void)xQueueSend(logQueue, &logQueueItem, 0);
}
else
{
// Count number of lost messages
++nrLostMessages;
}
}
}
void Logger_logISR(const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* context)
{
#if defined(ENABLE_LOGGING)
if(initialized)
{
struct LogQueueItem logQueueItem;
portBASE_TYPE higherPriorityTaskWoken = pdFALSE;
composeLogQueueItem(&logQueueItem, fileName, functionName, lineNumber, logType, context);
if(xQueueSendFromISR(logQueue, &logQueueItem, &higherPriorityTaskWoken) != pdTRUE)
{
// Queue failed
}
portEND_SWITCHING_ISR(higherPriorityTaskWoken);
}
#endif
}
static void composeLogQueueItem(struct LogQueueItem* logQueueItem, const char* fileName, const char* functionName,
int lineNumber, LogType logType, const char* context)
{
const size_t fileNameSize = sizeof(logQueueItem->fileName) / sizeof(logQueueItem->fileName[0]);
const size_t functionNameSize = sizeof(logQueueItem->functionName) / sizeof(logQueueItem->functionName[0]);
const size_t contextSize = sizeof(logQueueItem->context) / sizeof(logQueueItem->context[0]);
logQueueItem->logType = logType;
strncpy(&(logQueueItem->context[0]), context, contextSize);
logQueueItem->context[contextSize - 1] = '\0';
if(logType != LOGTYPE_PRINT)
{
int fileNameIndex = 0;
// If filename starts with "src/", strip this part
if((fileName[0] == 's') &&
(fileName[1] == 'r') &&
(fileName[2] == 'c') &&
(fileName[3] == '/'))
{
fileNameIndex = 4;
}
// All logtypes except LOGTYPE_PRINT need filename, functionname and linenumber.
strncpy(&(logQueueItem->fileName[0]), &fileName[fileNameIndex], fileNameSize);
strncpy(&(logQueueItem->functionName[0]), functionName, functionNameSize);
logQueueItem->lineNumber = lineNumber;
// Fix terminating null byte in strncpy in case string to be copied is too long
logQueueItem->fileName[fileNameSize - 1] = '\0';
logQueueItem->functionName[functionNameSize - 1] = '\0';
}
}
static void loggerTask(void* parameters)
{
for(;;)
{
struct LogQueueItem logQueueItem;
xQueueReceive(logQueue, &logQueueItem, portMAX_DELAY);
if(logQueueItem.logType == LOGTYPE_PRINT)
{
// Raw print
#if defined(ENABLE_SERIAL_LOGGING)
Uart_Write(loggerInterface, (const uint8_t*)logQueueItem.context, strlen(logQueueItem.context));
#endif
}
else
{
#if defined(ENABLE_SERIAL_LOGGING)
char str[256];
char* vt100Prefix = "";
const char* vt100Postfix = "\033[0m";
#endif
int hours;
int minutes;
int seconds;
#if defined(ENABLE_SERIAL_LOGGING)
if(logQueueItem.logType == LOGTYPE_INFO)
{
vt100Prefix = "\033[33m";
}
else if(logQueueItem.logType == LOGTYPE_WARNING)
{
vt100Prefix = "\033[35m";
}
else if(logQueueItem.logType == LOGTYPE_ERROR)
{
vt100Prefix = "\033[31m";
}
#endif
///TODO add RTC support
// RTC_getTime(&hours, &minutes, &seconds);
hours = 13;
minutes = 37;
seconds = 10;
#if defined(ENABLE_SERIAL_LOGGING)
// Formatted print
snprintf(str, sizeof(str) / sizeof(str[0]), "%s[%s] %02d:%02d:%02d %s, %s, %d: %s%s\n",
vt100Prefix,
(logQueueItem.logType == LOGTYPE_DEBUG) ? "DBG" :
(logQueueItem.logType == LOGTYPE_INFO) ? "INF" :
(logQueueItem.logType == LOGTYPE_WARNING) ? "WRN" : "ERR",
///TODO add RTC support
hours,
minutes,
seconds,
logQueueItem.fileName,
logQueueItem.functionName,
logQueueItem.lineNumber,
logQueueItem.context,
vt100Postfix);
#endif
#if defined(ENABLE_SERIAL_LOGGING)
Uart_Write(loggerInterface, (const uint8_t*)str, strlen(str));
#endif
}
}
}

View File

@@ -69,7 +69,7 @@ void OS_logTaskInfo(xTaskHandle taskHandle)
// Shell_sendString(text);
highWaterMark = uxTaskGetStackHighWaterMark(taskHandle);
// snprintf(text, sizeof(text), "Stack high water mark : %lu long words\n", highWaterMark);
snprintf(text, sizeof(text), "Stack high water mark : %lu long words\n", highWaterMark);
// Shell_sendString(text);
}

View File

@@ -28,8 +28,17 @@
// FreeRTOS includes
#include "FreeRTOS.h"
#include "task.h"
#include "freeRTOSFixes.h"
#include "Logger.h"
#include "misc.h"
#include "stm32f10x_rcc.h"
#include "nhd0420.h"
#include "platform.h"
#include "led.h"
#include "uart.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
@@ -43,6 +52,11 @@ tick hook. */
// Type definitions
// -----------------------------------------------------------------------------
struct LedTaskArguments
{
struct Led* led;
int frequency;
};
// -----------------------------------------------------------------------------
// File-scope variables
@@ -52,12 +66,16 @@ tick hook. */
stats. */
unsigned long ulRunTimeStatsClock = 0UL;
static struct LedTaskArguments ledTaskArguments;
static xTaskHandle initTaskHandle;
static xTaskHandle ledTaskHandle;
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
static void initTask(void* parameters);
static void ledBlinkTask(void* parameters);
// -----------------------------------------------------------------------------
// Function definitions
// -----------------------------------------------------------------------------
@@ -66,7 +84,17 @@ unsigned long ulRunTimeStatsClock = 0UL;
int main (void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
// Create TaskHandles
ledTaskArguments.led = ledOrange;
ledTaskArguments.frequency = 2;
xTaskCreate(initTask, (const char* const)"initTask", 1024, NULL, 0, &initTaskHandle);
xTaskCreate(ledBlinkTask, (const char* const)"ledTask", 1024, &ledTaskArguments, 0, &ledTaskHandle);
LOGGER_INFO("Starting the scheduler");
/* Start the scheduler. */
vTaskStartScheduler();
@@ -83,3 +111,46 @@ void vApplicationTickHook ()
{
}
static void initTask(void* parameters)
{
initPlatform();
Logger_initialize(uart1);
NHD0420_construct(spiDisplay);
NHD0420_turnOffDisplay();
vTaskDelay(1000);
NHD0420_clearScreen();
vTaskDelay(1000);
NHD0420_turnOnDisplay();
vTaskDelay(1000);
NHD0420_setContrast(30);
vTaskDelay(1000);
NHD0420_setBacklightBrightness(3);
vTaskDelay(1000);
NHD0420_setCursorToHome();
vTaskDelay(1000);
NHD0420_sendData("Hallo Welt", 10);
vTaskDelay(1);
NHD0420_setCursorToPosition(4, 5);
NHD0420_sendData("Koetjeboe", 9);
}
static void ledBlinkTask (void* parameters)
{
struct LedTaskArguments* arguments = (struct LedTaskArguments*) parameters;
struct Led* led = arguments->led;
int frequency = arguments->frequency;
while (1)
{
LED_turnOn(led);
vTaskDelay(configTICK_RATE_HZ / (frequency * 2));
LED_turnOff(led);
vTaskDelay(configTICK_RATE_HZ / (frequency * 2));
}
}

View File

@@ -0,0 +1,266 @@
// -----------------------------------------------------------------------------
/// @file stm32f10x_it.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 stm32f10x_it.c
/// @ingroup {group_name}
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include "FreeRTOS.h"
#include "misc.h"
#include "queue.h"
#include "semphr.h"
#include "stm32f10x_it.h"
#include "led.h"
#include "platform.h"
#include "spi.h"
#include "uart.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Type definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// File-scope variables
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Function definitions
// -----------------------------------------------------------------------------
void IRQ_setInterruptProperties(uint8_t irqChannel, uint8_t preemptionPriority, uint8_t subPriority, FunctionalState command)
{
NVIC_InitTypeDef NVIC_InitStructure; //! Define empty NVIC structure
//! Configure the USARTx Interrupt
NVIC_InitStructure.NVIC_IRQChannel = irqChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = preemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = subPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = command;
//! initialize the interrupts interface will all configured items
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
// void SVC_Handler(void)
// {
// }
/**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
// void PendSV_Handler(void)
// {
// }
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
// void SysTick_Handler(void)
// {
// }
/** ----------------------------------------------------------------------------
* @brief Function: USART1_IRQHandler
*
* Dedicated Interrupt Service Routine for USART1
*
* @return void
*
* @todo
* -----------------------------------------------------------------------------
*/
void USART1_IRQHandler(void)
{
static signed portBASE_TYPE higherPriorityTaskWoken = pdFALSE;
//! Transmission register empty interrupt
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
//! Receive element from usart transmission queue
struct usartQueueItem usartTxItem;
xQueueReceiveFromISR(uart1->txQueue, &usartTxItem, &higherPriorityTaskWoken);
//! Write one byte to the transmit data register
USART_SendData(USART1, usartTxItem.byte);
//! check if queue is empty -> all bytes transmit
if(pdTRUE == xQueueIsQueueEmptyFromISR(uart1->txQueue))
{
//! Disable the COMPORT Transmit interrupt and release semaphore
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
xSemaphoreGiveFromISR(uart1->txSemaphore, &higherPriorityTaskWoken);
}
}
//! Current interrupt is triggered by USART_RXNE (receive register not empty)
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
//! Read one byte from the receive data register
struct usartQueueItem usartRxItem;
//! Reading from reception register automatically clears the RXNE interrupt
usartRxItem.byte = (unsigned char) 0xFF & USART_ReceiveData(USART1);
//! Add the byte to the bluetooth RX queue
//! In case of a full queue, the data is dumped
(void)xQueueSendFromISR(uart1->rxQueue, &usartRxItem, &higherPriorityTaskWoken);
}
portEND_SWITCHING_ISR(higherPriorityTaskWoken);
}
/** ----------------------------------------------------------------------------
* @brief Function: SPI1_IRQHandler
*
* Dedicated Interrupt Service Routine for SPI1
*
* @return void
*
* @todo
* -----------------------------------------------------------------------------
*/
void SPI1_IRQHandler (void)
{
static signed portBASE_TYPE higherPriorityTaskWoken = pdFALSE;
//! Transmission register empty interrupt
if(SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET)
{
//! Receive element from usart transmission queue
struct spiQueueItem spiTxItem;
xQueueReceiveFromISR(spi1->txQueue, &spiTxItem, &higherPriorityTaskWoken);
//! Write one byte to the transmit data register
if (spi1->SPI_InitStruct.SPI_NSS == SPI_NSS_Soft)
{
GPIO_ResetBits(spi1->SPI_CE->GPIO_Typedef, spi1->SPI_CE->GPIO_InitStruct.GPIO_Pin);
}
SPI_I2S_SendData(SPI1, spiTxItem.byte);
if (spi1->SPI_InitStruct.SPI_NSS == SPI_NSS_Soft)
{
GPIO_SetBits(spi1->SPI_CE->GPIO_Typedef, spi1->SPI_CE->GPIO_InitStruct.GPIO_Pin);
}
//! check if queue is empty -> all bytes transmit
if(pdTRUE == xQueueIsQueueEmptyFromISR(spi1->txQueue))
{
//! Disable the COMPORT Transmit interrupt and release semaphore
SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
xSemaphoreGiveFromISR(spi1->txSemaphore, &higherPriorityTaskWoken);
}
}
//! Current interrupt is triggered by USART_RXNE (receive register not empty)
if(SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_RXNE) != RESET)
{
//! Read one byte from the receive data register
struct spiQueueItem spiRxItem;
//! Reading from reception register automatically clears the RXNE interrupt
spiRxItem.byte = (unsigned char) 0xFF & SPI_I2S_ReceiveData(SPI1);
//! Add the byte to the bluetooth RX queue
//! In case of a full queue, the data is dumped
(void)xQueueSendFromISR(spi1->rxQueue, &spiRxItem, &higherPriorityTaskWoken);
}
portEND_SWITCHING_ISR(higherPriorityTaskWoken);
}
/** ----------------------------------------------------------------------------
* @brief Function: SPI3_IRQHandler
*
* Dedicated Interrupt Service Routine for SPI3
*
* @return void
*
* @todo
* -----------------------------------------------------------------------------
*/
void SPI3_IRQHandler (void)
{
static signed portBASE_TYPE higherPriorityTaskWoken = pdFALSE;
//! Transmission register empty interrupt
if(SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_TXE) != RESET)
{
//! Receive element from usart transmission queue
struct spiQueueItem spiTxItem;
xQueueReceiveFromISR(spi3->txQueue, &spiTxItem, &higherPriorityTaskWoken);
//! Write one byte to the transmit data register
SPI_I2S_SendData(SPI3, spiTxItem.byte);
//! check if queue is empty -> all bytes transmit
if(pdTRUE == xQueueIsQueueEmptyFromISR(spi3->txQueue))
{
//! Disable the COMPORT Transmit interrupt and release semaphore
SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, DISABLE);
xSemaphoreGiveFromISR(spi3->txSemaphore, &higherPriorityTaskWoken);
}
}
//! Current interrupt is triggered by USART_RXNE (receive register not empty)
if(SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_RXNE) != RESET)
{
//! Read one byte from the receive data register
struct spiQueueItem spiRxItem;
//! Reading from reception register automatically clears the RXNE interrupt
spiRxItem.byte = (unsigned char) 0xFF & SPI_I2S_ReceiveData(SPI3);
//! Add the byte to the bluetooth RX queue
//! In case of a full queue, the data is dumped
(void)xQueueSendFromISR(spi3->rxQueue, &spiRxItem, &higherPriorityTaskWoken);
}
portEND_SWITCHING_ISR(higherPriorityTaskWoken);
}