From 92e3dd474bb0619eed27a42b9fd3d2bb87a30ae2 Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Mon, 9 May 2022 21:14:05 +0200 Subject: [PATCH] Move STM32 implementations into sta-stm32-core repository --- include/sta/hal/clocks.hpp | 81 -------------- include/sta/hal/delay.hpp | 69 ------------ include/sta/hal/gpio_pin.hpp | 68 ----------- include/sta/hal/init.hpp | 20 ---- include/sta/hal/spi.hpp | 134 ---------------------- include/sta/hal/uart.hpp | 65 ----------- include/sta/mcu/STM32F411xE.hpp | 77 ------------- include/sta/mcu/STM32F413xx.hpp | 16 --- include/sta/mcu/stm32base.hpp | 12 -- src/hal/delay.cpp | 65 ----------- src/hal/gpio_pin.cpp | 37 ------ src/hal/init.cpp | 22 ---- src/hal/spi.cpp | 193 -------------------------------- src/hal/uart.cpp | 43 ------- 14 files changed, 902 deletions(-) delete mode 100644 include/sta/hal/clocks.hpp delete mode 100644 include/sta/hal/delay.hpp delete mode 100644 include/sta/hal/gpio_pin.hpp delete mode 100644 include/sta/hal/init.hpp delete mode 100644 include/sta/hal/spi.hpp delete mode 100644 include/sta/hal/uart.hpp delete mode 100644 include/sta/mcu/STM32F411xE.hpp delete mode 100644 include/sta/mcu/STM32F413xx.hpp delete mode 100644 include/sta/mcu/stm32base.hpp delete mode 100644 src/hal/delay.cpp delete mode 100644 src/hal/gpio_pin.cpp delete mode 100644 src/hal/init.cpp delete mode 100644 src/hal/spi.cpp delete mode 100644 src/hal/uart.cpp diff --git a/include/sta/hal/clocks.hpp b/include/sta/hal/clocks.hpp deleted file mode 100644 index ef9a9c3..0000000 --- a/include/sta/hal/clocks.hpp +++ /dev/null @@ -1,81 +0,0 @@ -/** - * @file - * @brief Helper macros for HAL clock queries. - */ -#ifndef STA_HAL_CLOCKS_HPP -#define STA_HAL_CLOCKS_HPP - -/** - * @defgroup hal HAL - * @brief HAL base implementations. - */ - -/** - * @defgroup halBuildConfig Build config - * @ingroup hal - * @brief Build configuration options. - */ - -/** - * @defgroup halClocks Clocks - * @ingroup hal - * @brief HAL clock queries. - * @{ - */ - -#include -#include - - -/** - * @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 diff --git a/include/sta/hal/delay.hpp b/include/sta/hal/delay.hpp deleted file mode 100644 index dc28900..0000000 --- a/include/sta/hal/delay.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @file - * @brief Delay functions. - */ -#ifndef STA_HAL_DELAY_HPP -#define STA_HAL_DELAY_HPP - -/** - * @defgroup halDelay Delay - * @ingroup hal - * @brief HAL Delay module - */ - -#ifdef DOXYGEN -/** - * @def STA_HAL_DELAY_ENABLE - * @brief Enable module. - * - * @ingroup halBuildConfig - */ -# define STA_HAL_DELAY_ENABLE - -/** - * @def STA_HAL_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 halBuildConfig - */ -# define STA_HAL_DELAY_US_TIM -#endif // DOXYGEN - - -#include -#ifdef STA_HAL_DELAY_ENABLE - -#include - - -namespace sta -{ - /** - * @brief Millisecond delay. - * - * @param ms Milliseconds - * - * @ingroup halDelay - */ - void delayMs(uint32_t ms); - -#ifdef STA_HAL_DELAY_US_TIM - /** - * @brief Microsecond delay. - * - * @param us Microseconds - * - * @ingroup halDelay - */ - void delayUs(uint32_t us); -#endif // STA_HAL_DELAY_US_TIM -} // namespace sta - - -#endif // STA_HAL_DELAY_TIM - -#endif // STA_HAL_DELAY_HPP -/** @} */ diff --git a/include/sta/hal/gpio_pin.hpp b/include/sta/hal/gpio_pin.hpp deleted file mode 100644 index d7c2461..0000000 --- a/include/sta/hal/gpio_pin.hpp +++ /dev/null @@ -1,68 +0,0 @@ -/** - * @file - * @brief Wrapper for HAL GPIO pins. - */ -#ifndef STA_HAL_GPIO_PIN_HPP -#define STA_HAL_GPIO_PIN_HPP - -/** - * @defgroup halGPIO GPIO - * @ingroup hal - * @brief HAL GPIO module - */ - -#ifdef DOXYGEN -/** - * @def STA_HAL_GPIO_ENABLE - * @brief Enable module. - * - * @ingroup halBuildConfig - */ -# define STA_HAL_GPIO_ENABLE -#endif // DOXYGEN - - -#include -#ifdef STA_HAL_GPIO_ENABLE - -#include -#include - - -namespace sta -{ - /** - * @brief Container for HAL GPIO Pin objects. - * - * @ingroup halGPIO - */ - 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 - * - * @ingroup halGPIO - */ -#define STA_HAL_GPIO_PIN(label) sta::HalGpioPin{label##_GPIO_Port, label##_Pin} - - -#endif // STA_HAL_GPIO_ENABLE - -#endif // STA_HAL_GPIO_PIN_HPP diff --git a/include/sta/hal/init.hpp b/include/sta/hal/init.hpp deleted file mode 100644 index c21042a..0000000 --- a/include/sta/hal/init.hpp +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @file - * @brief Global HAL initialization. - */ -#ifndef STA_HAL_INIT_HPP -#define STA_HAL_INIT_HPP - - -namespace sta -{ - /** - * @brief Initialize global HAL objects. - * - * @ingroup hal - */ - void initHAL(); -} // namespace sta - - -#endif // STA_HAL_INIT_HPP diff --git a/include/sta/hal/spi.hpp b/include/sta/hal/spi.hpp deleted file mode 100644 index f90b56d..0000000 --- a/include/sta/hal/spi.hpp +++ /dev/null @@ -1,134 +0,0 @@ -/** - * @file - * @brief Implementations for `SpiInterface` and `SpiDevice` using HAL. - */ -#ifndef STA_HAL_SPI_HPP -#define STA_HAL_SPI_HPP - -/** - * @defgroup halSPI SPI - * @ingroup hal - * @brief HAL SPI module. - */ - -#ifdef DOXYGEN -/** - * @def STA_HAL_SPI_ENABLE - * @brief Enable module. - * - * Requires **HAL_GPIO** module. - * - * @ingroup halBuildConfig - */ -# define STA_HAL_SPI_ENABLE -#endif // DOXYGEN - - -#include -#ifdef STA_HAL_SPI_ENABLE - -#ifndef STA_HAL_GPIO_ENABLE -#error "HAL GPIO module required" -#endif // !STA_HAL_GPIO_ENABLE - -#include -#include -#include -#include - - -namespace sta -{ - /** - * @ingroup halSPI - * @{ - */ - - - /** - * @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__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_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 diff --git a/include/sta/hal/uart.hpp b/include/sta/hal/uart.hpp deleted file mode 100644 index fdfdfcc..0000000 --- a/include/sta/hal/uart.hpp +++ /dev/null @@ -1,65 +0,0 @@ -/** - * @file - * @brief Implementation of UART using HAL. - */ -#ifndef STA_HAL_UART_HPP -#define STA_HAL_UART_HPP - -/** - * @defgroup halUART UART - * @ingroup hal - * @brief HAL UART module. - */ - -#ifdef DOXYGEN -/** - * @def STA_HAL_UART_ENABLE - * @brief Enable module. - * - * @ingroup halBuildConfig - */ -# define STA_HAL_UART_ENABLE - -/** - * @def STA_HAL_UART_DEBUG_SERIAL - * @brief Create global sta::DebugSerial object using this HAL UART instance. - * - * @ingroup halBuildConfig - */ -# define STA_HAL_UART_DEBUG_SERIAL -#endif // DOXYGEN - - -#include -#ifdef STA_HAL_UART_ENABLE - -#include -#include - - -namespace sta -{ - /** - * @brief Implementation of UART interface using HAL. - * - * @ingroup halUART - */ - class HalUART : public UART - { - public: - /** - * @param handle UART handle - */ - HalUART(UART_HandleTypeDef * handle); - - 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 diff --git a/include/sta/mcu/STM32F411xE.hpp b/include/sta/mcu/STM32F411xE.hpp deleted file mode 100644 index d3138aa..0000000 --- a/include/sta/mcu/STM32F411xE.hpp +++ /dev/null @@ -1,77 +0,0 @@ -/** - * @brief Configuration for STM32F411xE family. - */ -#ifndef STA_MCU_STM32F411xE_HPP -#define STA_MCU_STM32F411xE_HPP - - -#ifndef STM32F411xE -# error "MCU config incompatible" -#endif // !STM32F411xE - - -#include - - -// Peripheral clock mappings -// - -// TIM to PCLK -#define STA_TIM_1_PCLK_IDX 2 -#define STA_TIM_2_PCLK_IDX 1 -#define STA_TIM_3_PCLK_IDX 1 -#define STA_TIM_4_PCLK_IDX 1 -#define STA_TIM_5_PCLK_IDX 1 -#define STA_TIM_9_PCLK_IDX 2 -#define STA_TIM_10_PCLK_IDX 2 -#define STA_TIM_11_PCLK_IDX 2 - -// SPI to PCLK -#define STA_SPI_1_PCLK_IDX 2 -#define STA_SPI_2_PCLK_IDX 1 -#define STA_SPI_3_PCLK_IDX 1 -#define STA_SPI_4_PCLK_IDX 2 -#define STA_SPI_5_PCLK_IDX 2 - -// I2C to PCLK -#define STA_I2C_1_PCLK_IDX 1 -#define STA_I2C_2_PCLK_IDX 1 -#define STA_I2C_3_PCLK_IDX 1 - -// USART to PCLK -#define STA_USART_1_PCLK_IDX 2 -#define STA_USART_2_PCLK_IDX 1 -#define STA_USART_6_PCLK_IDX 2 - - -// HAL handle mappings -// - -#define STA_HAL_htim1_PCLK_IDX STA_TIM_1_PCLK_IDX -#define STA_HAL_htim2_PCLK_IDX STA_TIM_2_PCLK_IDX -#define STA_HAL_htim3_PCLK_IDX STA_TIM_3_PCLK_IDX -#define STA_HAL_htim4_PCLK_IDX STA_TIM_4_PCLK_IDX -#define STA_HAL_htim5_PCLK_IDX STA_TIM_5_PCLK_IDX -#define STA_HAL_htim9_PCLK_IDX STA_TIM_9_PCLK_IDX -#define STA_HAL_htim10_PCLK_IDX STA_TIM_10_PCLK_IDX -#define STA_HAL_htim11_PCLK_IDX STA_TIM_11_PCLK_IDX - -// SPI to PCLK -#define STA_HAL_hspi1_PCLK_IDX STA_SPI_1_PCLK_IDX -#define STA_HAL_hspi2_PCLK_IDX STA_SPI_2_PCLK_IDX -#define STA_HAL_hspi3_PCLK_IDX STA_SPI_3_PCLK_IDX -#define STA_HAL_hspi4_PCLK_IDX STA_SPI_4_PCLK_IDX -#define STA_HAL_hspi5_PCLK_IDX STA_SPI_5_PCLK_IDX - -// I2C to PCLK -#define STA_HAL_hi2c1_PCLK_IDX STA_I2C_1_PCLK_IDX -#define STA_HAL_hi2c2_PCLK_IDX STA_I2C_2_PCLK_IDX -#define STA_HAL_h12c3_PCLK_IDX STA_I2C_3_PCLK_IDX - -// USART to PCLK -#define STA_HAL_husart1_PCLK_IDX STA_USART_1_PCLK_IDX -#define STA_HAL_husart2_PCLK_IDX STA_USART_2_PCLK_IDX -#define STA_HAL_husart6_PCLK_IDX STA_USART_6_PCLK_IDX - - -#endif // STA_MCU_STM32F411xE_HPP diff --git a/include/sta/mcu/STM32F413xx.hpp b/include/sta/mcu/STM32F413xx.hpp deleted file mode 100644 index d393c5d..0000000 --- a/include/sta/mcu/STM32F413xx.hpp +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @brief Configuration for STM32F413xx family. - */ -#ifndef STA_MCU_STM32F413xx_HPP -#define STA_MCU_STM32F413xx_HPP - - -#ifndef STM32F413xx -# error "MCU config incompatible" -#endif // !STM32F413xx - - -#include - - -#endif // STA_MCU_STM32F413xx_HPP diff --git a/include/sta/mcu/stm32base.hpp b/include/sta/mcu/stm32base.hpp deleted file mode 100644 index e1c0c2e..0000000 --- a/include/sta/mcu/stm32base.hpp +++ /dev/null @@ -1,12 +0,0 @@ -/** - * @brief Common configuration for STM32 MCUs - */ -#ifndef STA_MCU_STM32_BASE_HPP -#define STA_MCU_STM32_BASE_HPP - - -// TODO: Are all STM32 MCUs little endian? -#define STA_MCU_LITTLE_ENDIAN - - -#endif // STA_MCU_STM32_BASE_HPP diff --git a/src/hal/delay.cpp b/src/hal/delay.cpp deleted file mode 100644 index a0d2724..0000000 --- a/src/hal/delay.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include -#ifdef STA_HAL_DELAY_ENABLE - -#include -#include -#include -#include - - -namespace sta -{ - void delayMs(uint32_t ms) - { - HAL_Delay(ms); - } -} // namespace sta - - -#ifdef STA_HAL_DELAY_US_TIM - -#include - -namespace sta -{ - void delayUs(uint32_t us) - { - __HAL_TIM_SET_COUNTER(&STA_HAL_DELAY_US_TIM, 0); - while (__HAL_TIM_GET_COUNTER(&STA_HAL_DELAY_US_TIM) < us); - } - - - bool isValidDelayUsTIM() - { - // Get PCLK multiplier for TIM clock - uint32_t pclkMul = 1; - switch (STA_HAL_DELAY_US_TIM.Init.ClockDivision) - { - case TIM_CLOCKDIVISION_DIV1: - pclkMul = 1; - break; - case TIM_CLOCKDIVISION_DIV2: - pclkMul = 2; - break; - case TIM_CLOCKDIVISION_DIV4: - pclkMul = 4; - break; - default: - STA_ASSERT(false); - STA_UNREACHABLE(); - } - - // Calculate TIM clock frequency - uint32_t clkFreq = pclkMul * STA_HAL_GET_HANDLE_PCLK_FREQ_FN(STA_HAL_DELAY_US_TIM)(); - // Calculate update frequency based on prescaler value - uint32_t updateFreq = clkFreq / STA_HAL_DELAY_US_TIM.Init.Prescaler; - - // TIM must have at least microsecond precision (>= 1 MHz frequency) - return (updateFreq == 1000000); - } -} // namespace sta - -#endif // STA_HAL_DELAY_US_TIM - - -#endif // STA_HAL_DELAY_ENABLE diff --git a/src/hal/gpio_pin.cpp b/src/hal/gpio_pin.cpp deleted file mode 100644 index 7113749..0000000 --- a/src/hal/gpio_pin.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#ifdef STA_HAL_GPIO_ENABLE - -#include - - -namespace sta -{ - HalGpioPin::HalGpioPin(GPIO_TypeDef * port, uint16_t pin) - : port_{port}, pin_{pin} - { - STA_ASSERT(port != nullptr); - } - - void HalGpioPin::setState(GpioPinState state) - { - HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET); - } - - GPIO_TypeDef * HalGpioPin::getPort() const - { - return port_; - } - - uint16_t HalGpioPin::getPin() const - { - return pin_; - } - - uint8_t HalGpioPin::getIndex() const - { - return GPIO_GET_INDEX(port_); - } -} // namespace sta - - -#endif // STA_HAL_GPIO_ENABLE diff --git a/src/hal/init.cpp b/src/hal/init.cpp deleted file mode 100644 index 4d6d13a..0000000 --- a/src/hal/init.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include - -#include - -#ifdef STA_HAL_DELAY_US_TIM -#include -#endif // STA_HAL_DELAY_US_TIM - - -namespace sta -{ - void initHAL() - { -#ifdef STA_HAL_DELAY_US_TIM - // Validate TIM used for delayUs - extern bool isValidDelayUsTIM(); - STA_ASSERT(isValidDelayUsTIM()); - // Start timer base - HAL_TIM_Base_Start(&STA_HAL_DELAY_US_TIM); -#endif // STA_HAL_DELAY_US_TIM - } -} diff --git a/src/hal/spi.cpp b/src/hal/spi.cpp deleted file mode 100644 index 3e7121c..0000000 --- a/src/hal/spi.cpp +++ /dev/null @@ -1,193 +0,0 @@ -#include -#ifdef STA_HAL_SPI_ENABLE - -#include -#include -#include - - -#ifdef STA_MCU_LITTLE_ENDIAN -# define STA_HAL_SPI_REVERSE_BIT_ORDER SpiBitOrder::MSB -#elif STA_MCU_BIG_ENDIAN -# define STA_HAL_SPI_REVERSE_BIT_ORDER SpiBitOrder::LSB -#else // !STA_MCU_LITTLE_ENDIAN && !STA_MCU_BIG_ENDIAN -# ifdef STA_HAL_SPI_REVERSE_BIT_ORDER -# warning "Internal STA_HAL_SPI_REVERSE_BIT_ORDER macro manually defined! Better now what you are doing!!!" -# else // !STA_HAL_SPI_REVERSE_BIT_ORDER -# error "Unknown endian-ness. Define STA_MCU_LITTLE_ENDIAN or STA_MCU_BIG_ENDIAN in " -# endif // !STA_HAL_SPI_REVERSE_BIT_ORDER -#endif // !STA_MCU_LITTLE_ENDIAN && !STA_MCU_BIG_ENDIAN - - -namespace sta -{ - static SpiSettings getHalSpiSettings(SPI_HandleTypeDef * handle, uint32_t pclkFreq) - { - SpiSettings settings; - - settings.mode = getSpiMode( - (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SpiClkPolarity::LOW : SpiClkPolarity::HIGH, - (handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SpiClkPhase::EDGE_1 : SpiClkPhase::EDGE_2 - ); - settings.dataSize = (handle->Init.DataSize == SPI_DATASIZE_8BIT) ? SpiDataSize::SIZE_8 : SpiDataSize::SIZE_16; - settings.bitOrder = (handle->Init.FirstBit == SPI_FIRSTBIT_MSB) ? SpiBitOrder::MSB : SpiBitOrder::LSB; - - uint32_t prescaler = 1; - switch (handle->Init.BaudRatePrescaler) - { - case SPI_BAUDRATEPRESCALER_2: - prescaler = 2; - break; - case SPI_BAUDRATEPRESCALER_4: - prescaler = 4; - break; - case SPI_BAUDRATEPRESCALER_8: - prescaler = 8; - break; - case SPI_BAUDRATEPRESCALER_16: - prescaler = 16; - break; - case SPI_BAUDRATEPRESCALER_32: - prescaler = 32; - break; - case SPI_BAUDRATEPRESCALER_64: - prescaler = 64; - break; - case SPI_BAUDRATEPRESCALER_128: - prescaler = 128; - break; - case SPI_BAUDRATEPRESCALER_256: - prescaler = 256; - break; - default: - // Unreachable case - STA_ASSERT_MSG(false, "Case for SPI_BAUDRATEPRESCALER not handled"); - STA_UNREACHABLE(); - } - - // SPI clock speed is based of PCLK - settings.clkSpeed = pclkFreq / prescaler; - - return settings; - } - - - HalSpiInterface::HalSpiInterface(const HalSpiInterfaceInfo & info, Mutex * mutex /* = nullptr */) - : SpiInterface(mutex), info_{info} - { - STA_ASSERT(info.handle != nullptr); - STA_ASSERT(info.getPCLKFreq != nullptr); - } - - - void HalSpiInterface::transfer(uint8_t value) - { - if (settings().dataSize == SpiDataSize::SIZE_8) - { - HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY); - } - else - { - // Required since tx buffer is cast to uint16_t * internally - uint16_t dummy = value; - HAL_SPI_Transmit(info_.handle, reinterpret_cast(&dummy), 1, HAL_MAX_DELAY); - } - } - - void HalSpiInterface::transfer16(uint16_t value) - { - static_assert(sizeof(value) == 2, "Unexpected uint16_t size"); - - uint16_t size = 1; - - // Send as two bytes if data size is 8-bit - if (settings().dataSize == SpiDataSize::SIZE_8) - { - size = 2; - - if (settings().bitOrder == STA_HAL_SPI_REVERSE_BIT_ORDER) - { - // Reverse byte order from internal representation - value = STA_UINT16_SWAP_BYTE_ORDER(value); - } - } - - HAL_SPI_Transmit(info_.handle, reinterpret_cast(&value), size, HAL_MAX_DELAY); - } - - void HalSpiInterface::transfer(const uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - STA_ASSERT(size != 0); - - HAL_SPI_Transmit(info_.handle, const_cast(buffer), size, HAL_MAX_DELAY); - } - - void HalSpiInterface::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - STA_ASSERT(txBuffer != nullptr); - STA_ASSERT(rxBuffer != nullptr); - STA_ASSERT(size != 0); - - HAL_SPI_TransmitReceive(info_.handle, const_cast(txBuffer), rxBuffer, size, HAL_MAX_DELAY); - } - - void HalSpiInterface::receive(uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - - HAL_SPI_Receive(info_.handle, buffer, size, HAL_MAX_DELAY); - } - - - void HalSpiInterface::fill(uint8_t value, size_t count) - { - STA_ASSERT(count != 0); - - if (settings().dataSize == SpiDataSize::SIZE_8) - { - for (size_t i = 0; i < count; ++i) - { - HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY); - } - } - else - { - // Required since tx buffer is cast to uint16_t * internally - uint16_t dummy = value; - for (size_t i = 0; i < count; ++i) - { - HAL_SPI_Transmit(info_.handle, reinterpret_cast(&dummy), 1, HAL_MAX_DELAY); - } - } - } - - - const SpiSettings & HalSpiInterface::settings() const - { - // Cache settings - static SpiSettings settings = getHalSpiSettings(info_.handle, info_.getPCLKFreq()); - - return settings; - } - - - - - HalSpiDevice::HalSpiDevice(SpiInterface * intf, HalGpioPin csPin) - : SpiDevice(intf), csPin_{csPin} - {} - - void HalSpiDevice::select() - { - csPin_.setState(GpioPinState::LOW); - } - - void HalSpiDevice::deselect() - { - csPin_.setState(GpioPinState::HIGH); - } -} // namespace sta - - -#endif // STA_HAL_SPI_ENABLE diff --git a/src/hal/uart.cpp b/src/hal/uart.cpp deleted file mode 100644 index e349825..0000000 --- a/src/hal/uart.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#ifdef STA_HAL_UART_ENABLE - -#include - - -namespace sta -{ - HalUART::HalUART(UART_HandleTypeDef * handle) - : handle_{handle} - { - STA_ASSERT(handle != nullptr); - } - - - void HalUART::write(const uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - - HAL_UART_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); - } -} // namespace sta - - -#ifdef STA_HAL_UART_DEBUG_SERIAL - -// Get extern declaration for DebugSerial because const namespace level variables have internal linkage by default -#include - -#include - -namespace sta -{ - HalUART gHalDebugSerial(&STA_HAL_UART_DEBUG_SERIAL); - - // Used by - PrintableUART DebugSerial(&gHalDebugSerial); -} // namespace sta - -#endif // STA_HAL_UART_DEBUG_SERIAL - - -#endif // STA_HAL_UART_ENABLE