Initial commit

This commit is contained in:
2023-09-19 15:59:49 +02:00
commit bef0bade14
1860 changed files with 582828 additions and 0 deletions

View File

@ -0,0 +1,146 @@
#include "mcu/mcu.h"
#include "config/ledcontroller-config.h"
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
#include "trace/trace.h"
#endif // LEDCONTROLLER_TRACE_ENABLE
#include "ledcontroller.h"
LedController * LedController::pInstance_ = nullptr;
LedController::LedController()
{
assert(!pInstance_); // Only one instance of this class allowed!
pInstance_ = this;
// Initialize the method array with the right methods.
ledOperation_[0] = &LedController::setLed0;
ledOperation_[1] = &LedController::setLed1;
ledOperation_[2] = &LedController::setLed2;
ledOperation_[3] = &LedController::setLed3;
/*
* F7-DISCO LED GPIOs (Extension Board):
* LED0: PA15
* LED1: PH6
* LED2: PA8
* LED3: PB4
*/
}
LedController::~LedController()
{
}
void LedController::setLed(uint8_t index, bool bOn)
{
setLeds(0x01 << index, bOn);
}
void LedController::setLeds(uint8_t ledMask, bool bOn)
{
uint8_t mask = 0x01;
for (uint8_t i = 0; i < ledCount(); i++, mask <<= 1)
{
if ((ledMask & mask) == mask && ledOperation_[i])
{
(this->*ledOperation_[i])(bOn);
}
}
}
void LedController::setLed0(bool bOn /* = true */)
{
if (bOn)
{
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
}
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
if (bOn)
{
// Not using "%s" here (bug in gcc c-library!)
Trace::out(" LED0: on");
}
else
{
Trace::out(" LED0: off");
}
#endif // LEDCONTROLLER_TRACE_ENABLE
}
void LedController::setLed1(bool bOn /* = true */)
{
if (bOn)
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
}
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
if (bOn)
{
Trace::out(" LED1: on");
}
else
{
Trace::out(" LED1: off");
}
#endif // LEDCONTROLLER_TRACE_ENABLE
}
void LedController::setLed2(bool bOn /* = true */)
{
if (bOn)
{
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
}
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
if (bOn)
{
Trace::out(" LED2: on");
}
else
{
Trace::out(" LED2: off");
}
#endif // LEDCONTROLLER_TRACE_ENABLE
}
void LedController::setLed3(bool bOn /* = true */)
{
if (bOn)
{
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);
}
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
if (bOn)
{
Trace::out(" LED3: on");
}
else
{
Trace::out(" LED3: off");
}
#endif // LEDCONTROLLER_TRACE_ENABLE
}

View File

@ -0,0 +1,37 @@
#ifndef BOARD_LEDCONTROLLER_H
#define BOARD_LEDCONTROLLER_H
#include <cassert>
#include <stdint.h>
class LedController
{
public:
#define LED_COUNT 4
public:
LedController();
virtual ~LedController();
inline static LedController & getInstance() { assert(pInstance_); return *pInstance_; }
void setLed(uint8_t index, bool bOn = true);
void setLeds(uint8_t ledMask, bool bOn = true);
inline uint8_t ledCount() const { return LED_COUNT; }
void setLed0(bool bOn = true);
void setLed1(bool bOn = true);
void setLed2(bool bOn = true);
void setLed3(bool bOn = true);
protected:
typedef void (LedController::*ledMethod)(bool bOn); ///< Function prototype to led operation. Used for fast access to LED operation.
protected:
static LedController * pInstance_;
ledMethod ledOperation_[LED_COUNT]; ///< Array of pointers to led functions.
};
#endif // BOARD_LEDCONTROLLER_H

View File

@ -0,0 +1,149 @@
#include <config/trace-config.h>
#if (USE_PLATFORM_F7_DISCO_GCC_TRACE != 0)
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "hal/uart.h"
#include "trace/trace.h" // Include the general trace.h file (common for many projects)
#include "xf/interface/mutex.h"
#ifndef TRACE_ADD_CRLF_SEQU
#define TRACE_ADD_CRLF_SEQU 0
#endif
using interface::XFMutex;
static Uart traceUart(TRACE_UART_CONSTRUCTOR_PARAMETERS);
static char strTrace[255];
static int32_t checkStringEnding(char * str, uint32_t len);
static XFMutex * mutex = XFMutex::create();
static interface::XFMutex & traceMutex()
{
return *mutex;
}
static void traceMutexLock()
{
traceMutex().lock();
}
static void traceMutexUnlock()
{
traceMutex().unlock();
}
void trace_initialize()
{
Trace::initialize();
}
void trace_out(const char * const format , ...)
{
va_list args;
// Format string
va_start(args, format);
vsprintf(strTrace, format, args);
va_end(args);
checkStringEnding(strTrace, strlen(strTrace));
traceUart.write(strTrace);
}
//static
void Trace::initialize()
{
traceUart.initialize(TRACE_UART_BAUD_RATE);
}
static int32_t checkStringEnding(char * str, uint32_t len)
{
if (!len)
{
return 0;
}
if (str[len-1] != '\n')
{
#if (TRACE_ADD_CRLF_SEQU != 0)
// Add "\r\n" at the end of the string
str[len] = '\r';
str[len+1] = '\n';
str[len+2] = '\0';
#else
// Add "\n" at the end of the string
str[len] = '\n';
str[len+1] = '\0';
#endif // TRACE_ADD_CRLF_SEQU
}
#if (TRACE_ADD_CRLF_SEQU != 0)
// Check string finishing with "\r\n"
else if (str[len-1] == '\n')
{
// Check if second last char is an '\r'
if (len == 1 ||
(len >= 2 && str[len-2] != '\r'))
{
// Squeeze a '\r'
memmove(&str[len], &str[len-1], 2);
str[len-1] = '\r';
}
}
#endif // TRACE_ADD_CRLF_SEQU
return strlen(str);
}
void Trace::out(string str)
{
traceMutexLock();
if (str[str.length()-1] != '\n')
{
str += '\n';
}
#if defined(TRACE_ADD_CRLF_SEQU) && (TRACE_ADD_CRLF_SEQU != 0)
if(str[str.length()-2] != '\r'){
str.insert(str.length()-1, "\r");
}
#endif // TRACE_ADD_CRLF_SEQU
traceUart.write(str.data(), str.length());
traceMutexUnlock();
}
void Trace::out(const char * format, ...)
{
traceMutexLock();
va_list args;
// Format string
va_start(args, format);
vsprintf(strTrace, format, args);
va_end(args);
checkStringEnding(strTrace, strlen(strTrace));
traceUart.write(strTrace);
traceMutexUnlock();
}
//static
void Trace::lock()
{
traceMutexLock();
}
//static
void Trace::unlock()
{
traceMutexUnlock();
}
#endif // USE_PLATFORM_F7_DISCO_GCC_TRACE

View File

@ -0,0 +1,24 @@
#ifndef BOARD_LEDCONTROLLER_CONFIG_H
#define BOARD_LEDCONTROLLER_CONFIG_H
/**
* Do NOT edit this file!
*
* This is the default LedController config file.
* You may provide your own implementation at project level.
*
* For this:
* - Copy/paste this file into a folder named 'config' in
* you projects include/source directory.
* - Add a compiler include path so that your file is seen
* before the default config file provided in the platform
* package.
* - Adjust the content of your config file to your needs.
* - Remove this comment in your config file.
*/
#include "main.h" // To get GPIO user labels (LED0, LED1, etc.)
#define LEDCONTROLLER_TRACE_ENABLE 1
#endif // BOARD_LEDCONTROLLER_CONFIG_H

View File

@ -0,0 +1,34 @@
#ifndef TRACE_CONFIG_H
#define TRACE_CONFIG_H
/**
* Do NOT edit this file!
*
* This is the default TRACE config file.
* You may provide your own implementation at project level.
*
* For this:
* - Copy/paste this file into a folder named 'config' in
* you projects include/source directory.
* - Add a compiler include path so that your file is seen
* before the default config file provided in the platform
* package.
* - Adjust the content of your config file to your needs.
* - Remove this comment in your config file.
*/
#include "stm32f7xx_hal.h" // Do not use "mcu/mcu.h" here!
extern "C" UART_HandleTypeDef huart1; // Defined in main.c
#define USE_PLATFORM_F7_DISCO_GCC_TRACE 1
#define TRACE_UART_NUMBER 0
#define TRACE_UART_HANDLE huart1
#define TRACE_UART_BAUD_RATE /* 115200, but given by STM32CubeMX tool configuration */
#define TRACE_UART_USE_TX_DMA false
#define TRACE_UART_CONSTRUCTOR_PARAMETERS TRACE_UART_NUMBER, &TRACE_UART_HANDLE, TRACE_UART_USE_TX_DMA
#define TRACE_ADD_CRLF_SEQU 0
#endif // TRACE_CONFIG_H

View File

@ -0,0 +1,22 @@
#ifndef HAL_UART_CONFIG_H
#define HAL_UART_CONFIG_H
/**
* Do NOT edit this file!
*
* This is the default UART config file.
* You may provide your own implementation at project level.
*
* For this:
* - Copy/paste this file into a folder named 'config' in
* you projects include/source directory.
* - Add a compiler include path so that your file is seen
* before the default config file provided in the platform
* package.
* - Adjust the content of your config file to your needs.
* - Remove this comment in your config file.
*/
#include "mcu/mcu.h"
#endif // HAL_UART_CONFIG_H

View File

@ -0,0 +1,41 @@
#include "mcu/mcu.h"
#include "critical.h"
volatile int bInISR = 0;
volatile int bOMEnterCriticalRegionNested = 0;
int inISR()
{
// Variable must be put to TRUE in every ISR to indicate execution
// of an ISR and need to put back to FALSE before leaving ISR.
return bInISR;
}
void enterCritical()
{
// Only disable interrupts when not calling from an ISR
if (!inISR())
{
if (!bOMEnterCriticalRegionNested)
{
// Turn off the priority configurable interrupts
__disable_irq();
}
bOMEnterCriticalRegionNested++;
}
}
void exitCritical()
{
// Only enable interrupts when not calling from an ISR
if (!inISR())
{
bOMEnterCriticalRegionNested--;
if (!bOMEnterCriticalRegionNested)
{
// Turn on the interrupts with configurable priority
__enable_irq();
}
}
}

View File

@ -0,0 +1,17 @@
#ifndef CRITICAL_H
#define CRITICAL_H
extern volatile int bInISR;
#ifdef __cplusplus
extern "C" void ARMEnableInt();
extern "C" void ARMDisableInt();
#endif // __cplusplus
int inISR();
void enterCritical();
void exitCritical();
#endif /* CRITICAL_H */

View File

@ -0,0 +1,93 @@
#include <string.h>
#include "mcu/mcu.h"
#include "uart.h"
#ifndef TRACE_UART_PREEMPTION_PRIO
#define TRACE_UART_PREEMPTION_PRIO 0
#endif
Uart * Uart::_sUart[Uart::UART_COUNT]; // Comment: Startup script should initialize pointers to zero (.bss section)
bool Uart::_sInitialized[Uart::UART_COUNT]; // Same for other arrays
Uart::Uart(const unsigned char uartNbr, UART_HandleTypeDef * uartHandle, bool bTxDMA /* = false */)
: _uartNbr(-1),
_pUartHandle(NULL),
_bTxDMA(bTxDMA)
{
assert(uartNbr < UART_COUNT);
if (uartNbr < UART_COUNT)
{
if (!_sUart[uartNbr])
{
// Register instance
_sUart[uartNbr] = this;
_uartNbr = uartNbr;
_pUartHandle = uartHandle;
}
else
{
assert(false); // Error: Instance for specified UART already exists.
}
}
}
Uart::~Uart()
{
}
bool Uart::initialize()
{
return true;
}
bool Uart::enable()
{
// Enable the USART
__HAL_UART_ENABLE(_pUartHandle);
return true;
}
bool Uart::disable()
{
// enable the USART
__HAL_UART_DISABLE(_pUartHandle);
return true;
}
void Uart::write(const char * str, unsigned int length /* = 0 */)
{
if (!length)
{
length = strlen(str);
}
if (!_bTxDMA)
{ // Transmit without DMA
// Send next character
HAL_UART_Transmit(_pUartHandle, (uint8_t *)str, length, 50);
}
else
{ // TX DMA enabled
writeUsingDMA((const uint8_t *)str, length);
}
}
void Uart::writeUsingDMA(const uint8_t * str, uint32_t length)
{
assert(length <= sizeof(_pTxDmaBuffer));
// Copy data to TX DMA buffer
::memcpy(_pTxDmaBuffer, str, length);
// Check that a Tx process is not already ongoing
// (should never happen, but who knows!)
while (_pUartHandle->gState != HAL_UART_STATE_READY)
{ continue; }
// Give data to TX DMA
HAL_UART_Transmit_DMA(_pUartHandle, _pTxDmaBuffer, length);
}

View File

@ -0,0 +1,66 @@
#ifndef HAL_UART_H
#define HAL_UART_H
#include "config/uart-config.h"
#include "cassert"
#ifndef UART_TX_DMA_BUFFER_SIZE
#define UART_TX_DMA_BUFFER_SIZE 128
#endif
/**
* @brief Provides general access to the microcontrollers UART peripheral.
*/
class Uart
{
friend class Factory;
public:
Uart(const unsigned char uartNbr, UART_HandleTypeDef * uartHandle, bool bTxDMA = false);
virtual ~Uart();
bool initialize();
bool enable();
bool disable();
void write(const char * str, unsigned int length = 0);
/**
* Static accessor to the instances of UART. Used by the factory.
* You should not use this method directly. Use the factory to
* access an UART instead.
*/
static inline Uart & uart(const unsigned char uartNbr)
{
assert(uartNbr < UART_COUNT);
return *_sUart[uartNbr];
}
/**
* Checks if UART is present and initialized
*/
static inline bool present(const unsigned char uartNbr)
{
return (uartNbr < UART_COUNT &&
_sInitialized[uartNbr]) ? true : false;
}
protected:
void writeUsingDMA(const uint8_t * str, uint32_t length);
protected:
static const unsigned char UART_COUNT = 3; ///< Constant indicating how many USART the MCU has
unsigned char _uartNbr; ///< Number of UART. Index starting at 0
UART_HandleTypeDef * _pUartHandle; ///< Reference to the USART structure
static Uart * _sUart[UART_COUNT]; ///< Array to check if USB device was created already
static bool _sInitialized[UART_COUNT]; ///< Indicates if the UART has been initialized
const bool _bTxDMA; ///< Use DMA for transmission
uint8_t _pTxDmaBuffer[UART_TX_DMA_BUFFER_SIZE]; ///< Buffer used by DMA for transmission
};
#endif // HAL_UART_H

View File

@ -0,0 +1,21 @@
#ifndef MCU_STM32F7_MCU_H
#define MCU_STM32F7_MCU_H
#include "stm32f7xx_hal.h"
#include "trace/trace.h"
class Mcu
{
public:
inline static void msleep(uint32_t msecs) { HAL_Delay(msecs); }
inline static void wait() { for (uint32_t i = 1000000; i > 0; i--) continue; }
static void reset()
{
Trace::out("Mcu: Reset");
Trace::out("----------------------------------------------------");
wait();
NVIC_SystemReset();
}
};
#endif // MCU_STM32F7_MCU_H

View File

@ -0,0 +1,10 @@
#include "board.h"
namespace board {
void initialize()
{
}
} // namespace board

View File

@ -0,0 +1,12 @@
#ifndef BOARD_BOARD_H
#define BOARD_BOARD_H
#include <QCoreApplication>
namespace board {
void initialize();
}
#endif // BOARD_BOARD_H

View File

@ -0,0 +1,78 @@
#include <QtGlobal>
#include "config/ledcontroller-config.h"
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
#include "trace/trace.h"
#endif // LEDCONTROLLER_TRACE_ENABLE
#include "ledcontroller.h"
LedController * LedController::pInstance_ = nullptr;
LedController::LedController()
{
assert(!pInstance_); // Only one instance of this class allowed!
pInstance_ = this;
// Initialize the method array with the right methods.
ledOperation_[0] = &LedController::setLed0;
ledOperation_[1] = &LedController::setLed1;
ledOperation_[2] = &LedController::setLed2;
ledOperation_[3] = &LedController::setLed3;
}
LedController::~LedController()
{
}
void LedController::setLed(uint8_t index, bool bOn)
{
setLeds(0x01 << index, bOn);
}
void LedController::setLeds(uint8_t ledMask, bool bOn)
{
uint8_t mask = 0x01;
for (uint8_t i = 0; i < ledCount(); i++, mask <<= 1)
{
if ((ledMask & mask) == mask && ledOperation_[i])
{
(this->*ledOperation_[i])(bOn);
}
}
}
void LedController::setLed0(bool bOn /* = true */)
{
Q_UNUSED(bOn);
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
Trace::out(" LED0: %s", bOn ? "on" : "off");
#endif // LEDCONTROLLER_TRACE_ENABLE
}
void LedController::setLed1(bool bOn /* = true */)
{
Q_UNUSED(bOn);
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
Trace::out(" LED1: %s", bOn ? "on" : "off");
#endif // LEDCONTROLLER_TRACE_ENABLE
}
void LedController::setLed2(bool bOn /* = true */)
{
Q_UNUSED(bOn);
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
Trace::out(" LED2: %s", bOn ? "on" : "off");
#endif // LEDCONTROLLER_TRACE_ENABLE
}
void LedController::setLed3(bool bOn /* = true */)
{
Q_UNUSED(bOn);
#if (LEDCONTROLLER_TRACE_ENABLE != 0)
Trace::out(" LED3: %s", bOn ? "on" : "off");
#endif // LEDCONTROLLER_TRACE_ENABLE
}

View File

@ -0,0 +1,37 @@
#ifndef BOARD_LEDCONTROLLER_H
#define BOARD_LEDCONTROLLER_H
#include <cassert>
#include <stdint.h>
class LedController
{
public:
#define LED_COUNT 4
public:
LedController();
virtual ~LedController();
inline static LedController & getInstance() { assert(pInstance_); return *pInstance_; }
void setLed(uint8_t index, bool bOn = true);
void setLeds(uint8_t ledMask, bool bOn = true);
inline uint8_t ledCount() const { return LED_COUNT; }
void setLed0(bool bOn = true);
void setLed1(bool bOn = true);
void setLed2(bool bOn = true);
void setLed3(bool bOn = true);
protected:
typedef void (LedController::*ledMethod)(bool bOn); ///< Function prototype to led operation. Used for fast access to LED operation.
protected:
static LedController * pInstance_;
ledMethod ledOperation_[LED_COUNT]; ///< Array of pointers to led functions.
};
#endif // BOARD_LEDCONTROLLER_H

View File

@ -0,0 +1,53 @@
#include <config/trace-config.h>
#if (USE_PLATFORM_QT_TRACE != 0)
#include <QDebug>
#include <QTime>
#include "trace/trace.h"
#define ADD_CRLF_SEQU 0
static QString getTimeStampString()
{
return QTime::currentTime().toString("hh:mm:ss.zzz");
}
//static
void Trace::initialize()
{
}
void Trace::out(string str)
{
QString output;
#ifdef TRACE_ENABLE_TIMESTAMP
output = getTimeStampString() + ": ";
#endif // TRACE_ENABLE_TIMESTAMP
output.append(str.c_str());
qDebug(output.toLatin1());
}
void Trace::out(const char * format, ...)
{
char str[255];
va_list args;
va_start(args, format);
vsprintf(str, format, args);
va_end(args);
QString output;
#ifdef TRACE_ENABLE_TIMESTAMP
output = getTimeStampString() + ": ";
#endif // TRACE_ENABLE_TIMESTAMP
output.append(str);
qDebug(output.toLatin1());
}
#endif // USE_PLATFORM_QT_TRACE

View File

@ -0,0 +1,6 @@
#ifndef BOARD_LEDCONTROLLER_DEFAULT_CONFIG_H
#define BOARD_LEDCONTROLLER_DEFAULT_CONFIG_H
#define LEDCONTROLLER_TRACE_ENABLE 1
#endif // BOARD_LEDCONTROLLER_DEFAULT_CONFIG_H

View File

@ -0,0 +1,13 @@
DEFINES += USE_PLATFORM_QT_TRACE
INCLUDEPATH += $$PWD
SOURCES += \
$$PWD/board/trace.cpp \
$$PWD/board/board.cpp \
$$PWD/board/ledcontroller.cpp
HEADERS += \
$$PWD/board/board.h \
$$PWD/board/ledcontroller.h