Merge io_interfaces repo into helpers

This commit is contained in:
Henrik Stickann
2022-05-02 13:20:25 +02:00
24 changed files with 1726 additions and 4 deletions

View File

@@ -0,0 +1,60 @@
/**
* @brief Helper macros for HAL clock queries.
*/
#ifndef STA_HAL_CLOCKS_HPP
#define STA_HAL_CLOCKS_HPP
#include <sta/config.hpp>
#include <sta/hal.hpp>
/**
* @brief Get function returning PCLK frequency.
*
* @param n Index of peripheral clock
*/
#define STA_HAL_GET_PCLK_FREQ_FN(n) HAL_RCC_GetPCLK ## n ## Freq
// Internal helper for macro expansion
#define _STA_HAL_GET_PCLK_FREQ_FN(n) STA_HAL_GET_PCLK_FREQ_FN(n)
// Get instance to PCLK index map macro
#define _STA_PCLK_IDX_MAP(type, idx) STA_ ## type ## _ ## idx ## _PCLK_IDX
// Get HAL handle to PCLK index map macro
#define _STA_HAL_PCLK_IDX_MAP(handle) STA_HAL_ ## handle ## _PCLK_IDX
/**
* @brief Get function returning frequency of PCLK used by TIM.
*
* @param n TIM index
*/
#define STA_HAL_GET_TIM_PCLK_FREQ_FN(n) _STA_HAL_GET_PCLK_FREQ_FN(_STA_PCLK_IDX_MAP(TIM, n))
/**
* @brief Get function returning frequency of PCLK used by SPI interface.
*
* @param n SPI interface index
*/
#define STA_HAL_GET_SPI_PCLK_FREQ_FN(n) _STA_HAL_GET_PCLK_FREQ_FN(_STA_PCLK_IDX_MAP(SPI, n))
/**
* @brief Get function returning frequency of PCLK used by I2C interface.
*
* @param n I2C interface index
*/
#define STA_HAL_GET_I2C_PCLK_FREQ_FN(n) _STA_HAL_GET_PCLK_FREQ_FN(_STA_PCLK_IDX_MAP(I2C, n))
/**
* @brief Get function returning frequency of PCLK used by USART interface.
*
* @param n USART interface index
*/
#define STA_HAL_GET_USART_PCLK_FREQ_FN(n) _STA_HAL_GET_PCLK_FREQ_FN(_STA_PCLK_IDX_MAP(USART, n))
/**
* @brief Get function returning frequency of PCLK used by HAL instance.
*
* @param handle Instance handle
*/
#define STA_HAL_GET_HANDLE_PCLK_FREQ_FN(handle) _STA_HAL_GET_PCLK_FREQ_FN(_STA_HAL_PCLK_IDX_MAP(handle))
#endif // STA_HAL_CLOCKS_HPP

View File

@@ -0,0 +1,49 @@
/**
* @brief Wrapper for HAL GPIO pins.
*
* Configuration:
* STA_HAL_GPIO_ENABLE: Enable module
*/
#ifndef STA_HAL_GPIO_PIN_HPP
#define STA_HAL_GPIO_PIN_HPP
#include <sta/config.hpp>
#ifdef STA_HAL_GPIO_ENABLE
#include <sta/gpio_pin.hpp>
#include <sta/hal.hpp>
namespace sta
{
/**
* @brief Container for HAL GPIO Pin objects.
*/
class HalGpioPin : public GpioPin
{
public:
HalGpioPin(GPIO_TypeDef * port, uint16_t pin);
void setState(GpioPinState state) override;
GPIO_TypeDef * getPort() const;
uint16_t getPin() const;
uint8_t getIndex() const;
private:
GPIO_TypeDef * port_; /**< GPIO port */
uint16_t pin_; /**< GPIO pin */
};
}
/**
* @brief Create HalGpioPin object from pin label.
*
* @param label Pin label
*/
#define STA_HAL_GPIO_PIN(label) sta::HalGpioPin{label##_GPIO_Port, label##_Pin}
#endif // STA_HAL_GPIO_ENABLE
#endif // STA_HAL_GPIO_PIN_HPP

108
include/sta/hal/spi.hpp Normal file
View File

@@ -0,0 +1,108 @@
/**
* @brief Implementations for `SpiInterface` and `SpiDevice` using HAL.
*
* Configuration:
* STA_HAL_SPI_ENABLE: Enable module
*
* Requires **HAL_GPIO** module.
*/
#ifndef STA_HAL_SPI_HPP
#define STA_HAL_SPI_HPP
#include <sta/config.hpp>
#ifdef STA_HAL_SPI_ENABLE
#ifndef STA_HAL_GPIO_ENABLE
#error "HAL GPIO required"
#endif // !STA_HAL_GPIO_ENABLE
#include <sta/hal.hpp>
#include <sta/spi_device.hpp>
#include <sta/hal/clocks.hpp>
#include <sta/hal/gpio_pin.hpp>
namespace sta
{
/**
* @brief Get peripheral clock frequency.
*
* @return Clock frequency
*/
using HalSpiPCLKFreqFn = uint32_t (*)();
/**
* @brief Info related to HAL SPI interface.
*/
struct HalSpiInterfaceInfo
{
SPI_HandleTypeDef * handle; /**< Interface handle */
HalSpiPCLKFreqFn getPCLKFreq; /**< Getter for peripheral clock used by interface */
};
/**
* @brief Implementation of `SpiInterface` interface using HAL.
*/
class HalSpiInterface : public SpiInterface
{
public:
/**
* @param info SPI interface info
* @param mutex Mutex object for managing access. Pass nullptr for no access control
*/
HalSpiInterface(const HalSpiInterfaceInfo & info, Mutex * mutex = nullptr);
void transfer(uint8_t value) override;
void transfer16(uint16_t value) override;
void transfer(const uint8_t * buffer, size_t size) override;
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override;
void receive(uint8_t * buffer, size_t size) override;
void fill(uint8_t value, size_t count) override;
const SpiSettings & settings() const override;
private:
HalSpiInterfaceInfo info_; /**< SPI interface info */
};
/**
* @brief Implementation of `SpiDevice` interface using HAL.
*/
class HalSpiDevice : public SpiDevice
{
public:
/**
* @param intf SPI interface
* @param csPin Device CS pin
*/
HalSpiDevice(SpiInterface * intf, HalGpioPin csPin);
void select() override;
void deselect() override;
private:
HalGpioPin csPin_; /**< Device CS pin */
};
} // namespace sta
/**
* @brief Get SPI interface info struct for HAL handle.
*
* Requires STA_HAL_<handle>_PCLK_IDX to be defined for the MCU.
* MCU mappings are found in `core` -> sta/mcu/.hpp files.
*
* Check the MCUs Reference Manual RCC register documentation to see which
* peripheral clock is used.
*
* @param handle SPI interface handle
*/
#define STA_HAL_SPI_INFO(handle) sta::HalSpiInterfaceInfo{&handle, STA_HAL_GET_HANDLE_PCLK_FREQ_FN(handle)}
#endif // STA_HAL_SPI_ENABLE
#endif // STA_HAL_SPI_HPP

43
include/sta/hal/uart.hpp Normal file
View File

@@ -0,0 +1,43 @@
/**
* @brief Implementation of UART using HAL.
*
* Configuration:
* STA_HAL_UART_ENABLE: Enable module
* STA_HAL_UART_DEBUG_SERIAL: Create global `sta::DebugSerial` object using this UART instance
*/
#ifndef STA_HAL_UART_HPP
#define STA_HAL_UART_HPP
#include <sta/config.hpp>
#ifdef STA_HAL_UART_ENABLE
#include <sta/hal.hpp>
#include <sta/uart.hpp>
namespace sta
{
/**
* @brief Implementation of `UART` interface using HAL.
*/
class HalUART : public UART
{
public:
/**
* @param handle UART handle
*/
HalUART(UART_HandleTypeDef * handle);
using UART::print;
void write(const uint8_t * buffer, size_t size) override;
private:
UART_HandleTypeDef * handle_; /**< UART handle */
};
} // namespace sta
#endif // STA_HAL_UART_ENABLE
#endif // STA_HAL_UART_HPP