mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-08-05 18:21:54 +00:00
Merge stm32-core repo into sta-core
This commit is contained in:
129
include/sta/stm32/can.hpp
Normal file
129
include/sta/stm32/can.hpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of CanController using STM32 HAL.
|
||||
*/
|
||||
#ifndef STA_STM32_CAN_HPP
|
||||
#define STA_STM32_CAN_HPP
|
||||
|
||||
/**
|
||||
* @defgroup stm32CAN CAN
|
||||
* @ingroup stm32
|
||||
* @brief STM32 CAN module.
|
||||
*
|
||||
* Check @ref stm32BuildConfig for configuration options.
|
||||
*/
|
||||
|
||||
#ifdef DOXYGEN
|
||||
/**
|
||||
* @def STA_STM32_CAN_ENABLE
|
||||
* @brief Enable module.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_CAN_ENABLE
|
||||
|
||||
/**
|
||||
* @def STA_STM32_CAN_GLOBAL
|
||||
* @brief Create global CanBus object using this CAN instance.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_CAN_GLOBAL
|
||||
#endif // DOXYGEN
|
||||
|
||||
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_STM32_CAN_ENABLE
|
||||
|
||||
#include <sta/can_bus/controller.hpp>
|
||||
|
||||
#include <sta/stm32/hal.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @brief Implementation of CanController interface using HAL.
|
||||
*
|
||||
* @ingroup stm32CAN
|
||||
*/
|
||||
class STM32CanController : public CanController
|
||||
{
|
||||
public:
|
||||
static constexpr uint8_t MAX_FILTER_COUNT = 14; /**< Max number of filters */
|
||||
static constexpr uint8_t MAX_FIFO_COUNT = 2; /**< Max number of FIFOs */
|
||||
static constexpr uint8_t MAX_PAYLOAD_SIZE = 8; /**< Maximum payload size */
|
||||
|
||||
public:
|
||||
/**
|
||||
* @param handle CAN handle
|
||||
*/
|
||||
STM32CanController(CAN_HandleTypeDef * handle);
|
||||
|
||||
/**
|
||||
* @brief Enable RX pending interrupts.
|
||||
*/
|
||||
void enableRxInterrupts();
|
||||
|
||||
/**
|
||||
* @brief Start CAN controller.
|
||||
*/
|
||||
void start();
|
||||
/**
|
||||
* @brief Stop CAN controller.
|
||||
*/
|
||||
void stop();
|
||||
|
||||
|
||||
// RX/TX
|
||||
//
|
||||
|
||||
bool sendFrame(const CanTxHeader & header, const uint8_t * payload) override;
|
||||
bool receiveFrame(uint8_t fifo, CanRxHeader * header, uint8_t * payload) override;
|
||||
|
||||
uint32_t getRxFifoFlags() override;
|
||||
|
||||
// RX Filter
|
||||
//
|
||||
|
||||
void configureFilter(uint8_t idx, const CanFilter & filter, bool active = false) override;
|
||||
void enableFilter(uint8_t idx) override;
|
||||
void disableFilter(uint8_t idx) override;
|
||||
void clearFilters() override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Initialize filter settings.
|
||||
*/
|
||||
void initFilters();
|
||||
|
||||
private:
|
||||
CAN_HandleTypeDef * handle_; /**< CAN handle */
|
||||
CAN_FilterTypeDef filters_[MAX_FILTER_COUNT]; /**< Filter settings */
|
||||
};
|
||||
|
||||
|
||||
|
||||
#ifdef STA_STM32_CAN_GLOBAL
|
||||
/**
|
||||
* @brief Global CAN instance.
|
||||
*
|
||||
* @ingroup stm32CAN
|
||||
*/
|
||||
extern STM32CanController CanBus;
|
||||
|
||||
/**
|
||||
* @brief Interrupt handler for pending RX frames.
|
||||
*
|
||||
* May be implemented by application.
|
||||
*
|
||||
* @ingroup stm32CAN
|
||||
*/
|
||||
void CanBus_RxPendingCallback();
|
||||
#endif // STA_STM32_CAN_GLOBAL
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_STM32_CAN_ENABLE
|
||||
|
||||
#endif // STA_STM32_CAN_HPP
|
99
include/sta/stm32/clocks.hpp
Normal file
99
include/sta/stm32/clocks.hpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Helper macros for STM32 clock queries.
|
||||
*/
|
||||
#ifndef STA_STM32_CLOCKS_HPP
|
||||
#define STA_STM32_CLOCKS_HPP
|
||||
|
||||
/**
|
||||
* @defgroup stm32 STM32
|
||||
* @brief Modules implemented for STM32 MCUs.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup stm32BuildConfig Build config
|
||||
* @ingroup stm32
|
||||
* @brief Build configuration options.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup stm32Clocks Clocks
|
||||
* @ingroup stm32
|
||||
* @brief STM32 Clock queries.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include <sta/config.hpp>
|
||||
|
||||
#include <sta/stm32/hal.hpp>
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get function returning PCLK frequency.
|
||||
*
|
||||
* @param n Index of peripheral clock
|
||||
*/
|
||||
#define STA_STM32_GET_PCLK_FREQ_FN(n) HAL_RCC_GetPCLK ## n ## Freq
|
||||
|
||||
|
||||
/**
|
||||
* @brief Internal helper for macro expansion.
|
||||
*
|
||||
* @param n PCLK index
|
||||
* @return Function returning PCLK frequency
|
||||
*/
|
||||
#define _STA_STM32_GET_PCLK_FREQ_FN(n) STA_STM32_GET_PCLK_FREQ_FN(n)
|
||||
/**
|
||||
* @brief Map instance name to PCLK index.
|
||||
*
|
||||
* @param type Hardware type
|
||||
* @param idx Instance index
|
||||
* @return PCLK index
|
||||
*/
|
||||
#define _STA_STM32_PCLK_IDX_MAP(type, idx) STA_STM32_ ## type ## _ ## idx ## _PCLK_IDX
|
||||
// Get HAL handle to PCLK index map macro
|
||||
/**
|
||||
* @brief Map instance handle to PCLK index.
|
||||
*
|
||||
* @param handle HAL handle
|
||||
* @return PCLK index
|
||||
*/
|
||||
#define _STA_STM32_HANDLE_PCLK_IDX_MAP(handle) STA_STM32_ ## handle ## _PCLK_IDX
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get function returning frequency of PCLK used by TIM.
|
||||
*
|
||||
* @param n TIM index
|
||||
*/
|
||||
#define STA_STM32_GET_TIM_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(TIM, n))
|
||||
/**
|
||||
* @brief Get function returning frequency of PCLK used by SPI interface.
|
||||
*
|
||||
* @param n SPI interface index
|
||||
*/
|
||||
#define STA_STM32_GET_SPI_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(SPI, n))
|
||||
/**
|
||||
* @brief Get function returning frequency of PCLK used by I2C interface.
|
||||
*
|
||||
* @param n I2C interface index
|
||||
*/
|
||||
#define STA_STM32_GET_I2C_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(I2C, n))
|
||||
/**
|
||||
* @brief Get function returning frequency of PCLK used by USART interface.
|
||||
*
|
||||
* @param n USART interface index
|
||||
*/
|
||||
#define STA_STM32_GET_USART_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(USART, n))
|
||||
|
||||
/**
|
||||
* @brief Get function returning frequency of PCLK used by HAL instance.
|
||||
*
|
||||
* @param handle Instance handle
|
||||
*/
|
||||
#define STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_HANDLE_PCLK_IDX_MAP(handle))
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
#endif // STA_STM32_CLOCKS_HPP
|
68
include/sta/stm32/delay.hpp
Normal file
68
include/sta/stm32/delay.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Delay functions.
|
||||
*/
|
||||
#ifndef STA_STM32_DELAY_HPP
|
||||
#define STA_STM32_DELAY_HPP
|
||||
|
||||
/**
|
||||
* @defgroup stm32Delay Delay
|
||||
* @ingroup stm32
|
||||
* @brief STM32 Delay module.
|
||||
*/
|
||||
|
||||
#ifdef DOXYGEN
|
||||
/**
|
||||
* @def STA_STM32_DELAY_ENABLE
|
||||
* @brief Enable module.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_DELAY_ENABLE
|
||||
|
||||
/**
|
||||
* @def STA_STM32_DELAY_US_TIM
|
||||
* @brief 1 MHz TIM instance used by sta::delayUs.
|
||||
*
|
||||
* NOTE: TIM time base must be started before use of sta::delayUs by calling sta::initHAL.
|
||||
* When using startup system task this is handled automatically.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_DELAY_US_TIM
|
||||
#endif // DOXYGEN
|
||||
|
||||
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_STM32_DELAY_ENABLE
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @brief Millisecond delay.
|
||||
*
|
||||
* @param ms Milliseconds
|
||||
*
|
||||
* @ingroup stm32Delay
|
||||
*/
|
||||
void delayMs(uint32_t ms);
|
||||
|
||||
#ifdef STA_STM32_DELAY_US_TIM
|
||||
/**
|
||||
* @brief Microsecond delay.
|
||||
*
|
||||
* @param us Microseconds
|
||||
*
|
||||
* @ingroup stm32Delay
|
||||
*/
|
||||
void delayUs(uint32_t us);
|
||||
#endif // STA_STM32_DELAY_US_TIM
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_STM32_DELAY_ENABLE
|
||||
|
||||
#endif // STA_STM32_DELAY_HPP
|
108
include/sta/stm32/gpio_pin.hpp
Normal file
108
include/sta/stm32/gpio_pin.hpp
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Wrapper for STM32 GPIO pins.
|
||||
*/
|
||||
#ifndef STA_STM32_GPIO_PIN_HPP
|
||||
#define STA_STM32_GPIO_PIN_HPP
|
||||
|
||||
/**
|
||||
* @defgroup stm32GPIO GPIO
|
||||
* @ingroup stm32
|
||||
* @brief STM32 GPIO module.
|
||||
*/
|
||||
|
||||
#ifdef DOXYGEN
|
||||
/**
|
||||
* @def STA_STM32_GPIO_ENABLE
|
||||
* @brief Enable module.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_GPIO_ENABLE
|
||||
#endif // DOXYGEN
|
||||
|
||||
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_STM32_GPIO_ENABLE
|
||||
|
||||
#include <sta/intf/gpio_pin.hpp>
|
||||
|
||||
#include <sta/stm32/hal.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @brief Container for STM GPIO Pin data.
|
||||
*
|
||||
* @ingroup stm32GPIO
|
||||
*/
|
||||
class STM32GpioPin : public GpioPin
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @param port GPIO port
|
||||
* @param pin Pin index
|
||||
*/
|
||||
STM32GpioPin(GPIO_TypeDef * port, uint16_t pin);
|
||||
|
||||
void setState(GpioPinState state) override;
|
||||
|
||||
/**
|
||||
* @brief Get GPIO port for pin.
|
||||
*
|
||||
* @return GPIO port
|
||||
*/
|
||||
GPIO_TypeDef * getPort() const;
|
||||
/**
|
||||
* @brief Get pin index for pin.
|
||||
*
|
||||
* @return Pin index
|
||||
*/
|
||||
uint16_t getPin() const;
|
||||
/**
|
||||
* @brief Get GPIO port index for pin.
|
||||
*
|
||||
* @return GPIO port index
|
||||
*/
|
||||
uint8_t getPortIndex() const;
|
||||
|
||||
private:
|
||||
GPIO_TypeDef * port_; /**< GPIO port */
|
||||
uint16_t pin_; /**< GPIO pin */
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Interrupt trigger edge.
|
||||
*/
|
||||
enum class InterruptEdge
|
||||
{
|
||||
RISING, /**< Rising edge */
|
||||
FALLING, /**< Falling edge */
|
||||
BOTH /**< Rising and falling edge */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Check pin EXIT pin configuration.
|
||||
*
|
||||
* @param pin GPIO pin
|
||||
* @param edge Interrupt trigger edge
|
||||
* @return True if EXIT pin and trigger edge matches
|
||||
*/
|
||||
bool isInterruptEdge(const STM32GpioPin & pin, InterruptEdge edge);
|
||||
} // namespace sta
|
||||
|
||||
/**
|
||||
* @brief Create STM32GpioPin object from pin label.
|
||||
*
|
||||
* @param label Pin label
|
||||
*
|
||||
* @ingroup stm32GPIO
|
||||
*/
|
||||
#define STA_STM32_GPIO_PIN(label) sta::STM32GpioPin{label##_GPIO_Port, label##_Pin}
|
||||
|
||||
|
||||
#endif // STA_STM32_GPIO_ENABLE
|
||||
|
||||
#endif // STA_STM32_GPIO_PIN_HPP
|
8
include/sta/stm32/hal.hpp
Normal file
8
include/sta/stm32/hal.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef STA_STM32_HAL_HPP
|
||||
#define STA_STM32_HAL_HPP
|
||||
|
||||
// Include STM32 HAL headers
|
||||
#include <main.h>
|
||||
|
||||
|
||||
#endif // STA_STM32_HAL_HPP
|
20
include/sta/stm32/init.hpp
Normal file
20
include/sta/stm32/init.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Global STM32 HAL initialization.
|
||||
*/
|
||||
#ifndef STA_STM32_INIT_HPP
|
||||
#define STA_STM32_INIT_HPP
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @brief Initialize global HAL objects.
|
||||
*
|
||||
* @ingroup stm32
|
||||
*/
|
||||
void initHAL();
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_STM32_INIT_HPP
|
77
include/sta/stm32/mcu/STM32F411xE.hpp
Normal file
77
include/sta/stm32/mcu/STM32F411xE.hpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @brief Configuration for STM32F411xE family.
|
||||
*/
|
||||
#ifndef STA_STM32_MCU_STM32F411xE_HPP
|
||||
#define STA_STM32_MCU_STM32F411xE_HPP
|
||||
|
||||
|
||||
#ifndef STM32F411xE
|
||||
# error "MCU config incompatible"
|
||||
#endif // !STM32F411xE
|
||||
|
||||
|
||||
#include <sta/stm32/mcu/common.hpp>
|
||||
|
||||
|
||||
// Peripheral clock mappings
|
||||
//
|
||||
|
||||
// TIM to PCLK
|
||||
#define STA_STM32_TIM_1_PCLK_IDX 2
|
||||
#define STA_STM32_TIM_2_PCLK_IDX 1
|
||||
#define STA_STM32_TIM_3_PCLK_IDX 1
|
||||
#define STA_STM32_TIM_4_PCLK_IDX 1
|
||||
#define STA_STM32_TIM_5_PCLK_IDX 1
|
||||
#define STA_STM32_TIM_9_PCLK_IDX 2
|
||||
#define STA_STM32_TIM_10_PCLK_IDX 2
|
||||
#define STA_STM32_TIM_11_PCLK_IDX 2
|
||||
|
||||
// SPI to PCLK
|
||||
#define STA_STM32_SPI_1_PCLK_IDX 2
|
||||
#define STA_STM32_SPI_2_PCLK_IDX 1
|
||||
#define STA_STM32_SPI_3_PCLK_IDX 1
|
||||
#define STA_STM32_SPI_4_PCLK_IDX 2
|
||||
#define STA_STM32_SPI_5_PCLK_IDX 2
|
||||
|
||||
// I2C to PCLK
|
||||
#define STA_STM32_I2C_1_PCLK_IDX 1
|
||||
#define STA_STM32_I2C_2_PCLK_IDX 1
|
||||
#define STA_STM32_I2C_3_PCLK_IDX 1
|
||||
|
||||
// USART to PCLK
|
||||
#define STA_STM32_USART_1_PCLK_IDX 2
|
||||
#define STA_STM32_USART_2_PCLK_IDX 1
|
||||
#define STA_STM32_USART_6_PCLK_IDX 2
|
||||
|
||||
|
||||
// HAL handle mappings
|
||||
//
|
||||
|
||||
#define STA_STM32_htim1_PCLK_IDX STA_STM32_TIM_1_PCLK_IDX
|
||||
#define STA_STM32_htim2_PCLK_IDX STA_STM32_TIM_2_PCLK_IDX
|
||||
#define STA_STM32_htim3_PCLK_IDX STA_STM32_TIM_3_PCLK_IDX
|
||||
#define STA_STM32_htim4_PCLK_IDX STA_STM32_TIM_4_PCLK_IDX
|
||||
#define STA_STM32_htim5_PCLK_IDX STA_STM32_TIM_5_PCLK_IDX
|
||||
#define STA_STM32_htim9_PCLK_IDX STA_STM32_TIM_9_PCLK_IDX
|
||||
#define STA_STM32_htim10_PCLK_IDX STA_STM32_TIM_10_PCLK_IDX
|
||||
#define STA_STM32_htim11_PCLK_IDX STA_STM32_TIM_11_PCLK_IDX
|
||||
|
||||
// SPI to PCLK
|
||||
#define STA_STM32_hspi1_PCLK_IDX STA_STM32_SPI_1_PCLK_IDX
|
||||
#define STA_STM32_hspi2_PCLK_IDX STA_STM32_SPI_2_PCLK_IDX
|
||||
#define STA_STM32_hspi3_PCLK_IDX STA_STM32_SPI_3_PCLK_IDX
|
||||
#define STA_STM32_hspi4_PCLK_IDX STA_STM32_SPI_4_PCLK_IDX
|
||||
#define STA_STM32_hspi5_PCLK_IDX STA_STM32_SPI_5_PCLK_IDX
|
||||
|
||||
// I2C to PCLK
|
||||
#define STA_STM32_hi2c1_PCLK_IDX STA_STM32_I2C_1_PCLK_IDX
|
||||
#define STA_STM32_hi2c2_PCLK_IDX STA_STM32_I2C_2_PCLK_IDX
|
||||
#define STA_STM32_h12c3_PCLK_IDX STA_STM32_I2C_3_PCLK_IDX
|
||||
|
||||
// USART to PCLK
|
||||
#define STA_STM32_husart1_PCLK_IDX STA_STM32_USART_1_PCLK_IDX
|
||||
#define STA_STM32_husart2_PCLK_IDX STA_STM32_USART_2_PCLK_IDX
|
||||
#define STA_STM32_husart6_PCLK_IDX STA_STM32_USART_6_PCLK_IDX
|
||||
|
||||
|
||||
#endif // STA_STM32_MCU_STM32F411xE_HPP
|
16
include/sta/stm32/mcu/STM32F413xx.hpp
Normal file
16
include/sta/stm32/mcu/STM32F413xx.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @brief Configuration for STM32F413xx family.
|
||||
*/
|
||||
#ifndef STA_STM32_MCU_STM32F413xx_HPP
|
||||
#define STA_STM32_MCU_STM32F413xx_HPP
|
||||
|
||||
|
||||
#ifndef STM32F413xx
|
||||
# error "MCU config incompatible"
|
||||
#endif // !STM32F413xx
|
||||
|
||||
|
||||
#include <sta/stm32/mcu/common.hpp>
|
||||
|
||||
|
||||
#endif // STA_STM32_MCU_STM32F413xx_HPP
|
12
include/sta/stm32/mcu/common.hpp
Normal file
12
include/sta/stm32/mcu/common.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @brief Common configuration for STM32 MCUs
|
||||
*/
|
||||
#ifndef STA_STM32_MCU_COMMON_HPP
|
||||
#define STA_STM32_MCU_COMMON_HPP
|
||||
|
||||
|
||||
// TODO: Are all STM32 MCUs little endian?
|
||||
#define STA_MCU_LITTLE_ENDIAN
|
||||
|
||||
|
||||
#endif // STA_STM32_MCU_COMMON_HPP
|
133
include/sta/stm32/spi.hpp
Normal file
133
include/sta/stm32/spi.hpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementations for SpiInterface and SpiDevice using STM32 HAL.
|
||||
*/
|
||||
#ifndef STA_STM32_SPI_HPP
|
||||
#define STA_STM32_SPI_HPP
|
||||
|
||||
/**
|
||||
* @defgroup stm32SPI SPI
|
||||
* @ingroup stm32
|
||||
* @brief STM32 SPI module.
|
||||
*/
|
||||
|
||||
#ifdef DOXYGEN
|
||||
/**
|
||||
* @def STA_STM32_SPI_ENABLE
|
||||
* @brief Enable module.
|
||||
*
|
||||
* Requires **STM_GPIO** module.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_SPI_ENABLE
|
||||
#endif // DOXYGEN
|
||||
|
||||
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_STM32_SPI_ENABLE
|
||||
|
||||
#ifndef STA_STM32_GPIO_ENABLE
|
||||
#error "STM32 GPIO module required"
|
||||
#endif // !STA_STM32_GPIO_ENABLE
|
||||
|
||||
|
||||
#include <sta/spi/device.hpp>
|
||||
#include <sta/spi/interface.hpp>
|
||||
|
||||
#include <sta/stm32/clocks.hpp>
|
||||
#include <sta/stm32/gpio_pin.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @ingroup stm32SPI
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get peripheral clock frequency.
|
||||
*
|
||||
* @return Clock frequency
|
||||
*/
|
||||
using STM32SpiPCLKFreqFn = uint32_t (*)();
|
||||
|
||||
/**
|
||||
* @brief Info related to STM SPI interface.
|
||||
*/
|
||||
struct STM32SpiInterfaceInfo
|
||||
{
|
||||
SPI_HandleTypeDef * handle; /**< Interface handle */
|
||||
STM32SpiPCLKFreqFn getPCLKFreq; /**< Getter for peripheral clock used by interface */
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Implementation of SpiInterface interface using STM32 HAL.
|
||||
*/
|
||||
class STM32SpiInterface : public SpiInterface
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @param info SPI interface info
|
||||
* @param mutex Mutex object for managing access. Pass nullptr for no access control
|
||||
*/
|
||||
STM32SpiInterface(const STM32SpiInterfaceInfo & 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:
|
||||
STM32SpiInterfaceInfo info_; /**< SPI interface info */
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Implementation of SpiDevice interface using STM32 HAL.
|
||||
*/
|
||||
class STM32SpiDevice : public SpiDevice
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @param intf SPI interface
|
||||
* @param csPin Device CS pin
|
||||
*/
|
||||
STM32SpiDevice(STM32SpiInterface * intf, STM32GpioPin csPin);
|
||||
|
||||
private:
|
||||
STM32GpioPin csPin_; /**< Device CS pin */
|
||||
};
|
||||
|
||||
|
||||
/** @} */
|
||||
} // namespace sta
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get SPI interface info struct for STM32 HAL handle.
|
||||
*
|
||||
* Requires STA_STM32_<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
|
||||
*
|
||||
* @ingroup halSPI
|
||||
*/
|
||||
#define STA_STM32_SPI_INFO(handle) sta::STM32SpiInterfaceInfo{&handle, STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle)}
|
||||
|
||||
|
||||
#endif // STA_STM32_SPI_ENABLE
|
||||
|
||||
#endif // STA_STM32_SPI_HPP
|
66
include/sta/stm32/uart.hpp
Normal file
66
include/sta/stm32/uart.hpp
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of UART using STM32 HAL.
|
||||
*/
|
||||
#ifndef STA_STM32_UART_HPP
|
||||
#define STA_STM32_UART_HPP
|
||||
|
||||
/**
|
||||
* @defgroup stm32UART UART
|
||||
* @ingroup stm32
|
||||
* @brief STM32 UART module.
|
||||
*/
|
||||
|
||||
#ifdef DOXYGEN
|
||||
/**
|
||||
* @def STA_STM32_UART_ENABLE
|
||||
* @brief Enable module.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_UART_ENABLE
|
||||
|
||||
/**
|
||||
* @def STA_STM32_UART_DEBUG_SERIAL
|
||||
* @brief Create global sta::DebugSerial object using this HAL UART instance.
|
||||
*
|
||||
* @ingroup stm32BuildConfig
|
||||
*/
|
||||
# define STA_STM32_UART_DEBUG_SERIAL
|
||||
#endif // DOXYGEN
|
||||
|
||||
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_STM32_UART_ENABLE
|
||||
|
||||
#include <sta/intf/uart.hpp>
|
||||
|
||||
#include <sta/stm32/hal.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @brief Implementation of UART interface using HAL.
|
||||
*
|
||||
* @ingroup stm32UART
|
||||
*/
|
||||
class STM32UART : public UART
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @param handle UART handle
|
||||
*/
|
||||
STM32UART(UART_HandleTypeDef * handle);
|
||||
|
||||
void write(const uint8_t * buffer, size_t size) override;
|
||||
|
||||
private:
|
||||
UART_HandleTypeDef * handle_; /**< UART handle */
|
||||
};
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_STM32_UART_ENABLE
|
||||
|
||||
#endif // STA_STM32_UART_HPP
|
Reference in New Issue
Block a user