From f09f580f0042ead2dd62f616afbe201e96b9dbb0 Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Tue, 12 Apr 2022 16:27:55 +0200 Subject: [PATCH] Add asserts. Fix formating --- include/sta/hal/spi.hpp | 5 +++-- src/hal/gpio_pin.cpp | 6 +++++- src/hal/spi.cpp | 21 ++++++++++++++++++--- src/hal/uart.cpp | 8 +++++++- src/spi_device.cpp | 22 +++++++++++++++++++--- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/include/sta/hal/spi.hpp b/include/sta/hal/spi.hpp index 7d86d5d..f298541 100644 --- a/include/sta/hal/spi.hpp +++ b/include/sta/hal/spi.hpp @@ -1,8 +1,9 @@ /** * @brief Implementations for SpiInterface and SpiDevice using HAL. * - * Define STA_HAL_SPI_ENABLE in to enable. - * Requires STA_HAL_GPIO_ENABLE. + * Define **STA_HAL_SPI_ENABLE** in `` to enable module. + * + * Requires **STA_HAL_GPIO_ENABLE**. */ #ifndef STA_HAL_SPI_HPP #define STA_HAL_SPI_HPP diff --git a/src/hal/gpio_pin.cpp b/src/hal/gpio_pin.cpp index 6a23c87..d8fd93e 100644 --- a/src/hal/gpio_pin.cpp +++ b/src/hal/gpio_pin.cpp @@ -2,12 +2,16 @@ #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) { diff --git a/src/hal/spi.cpp b/src/hal/spi.cpp index 9411bef..69a428e 100644 --- a/src/hal/spi.cpp +++ b/src/hal/spi.cpp @@ -2,12 +2,16 @@ #ifdef STA_HAL_SPI_ENABLE +#include + namespace sta { HalSpiInterface::HalSpiInterface(SPI_HandleTypeDef * handle, Mutex * mutex /* = nullptr */) : SpiInterface(mutex), handle_{handle} - {} + { + STA_ASSERT(handle != nullptr); + } void HalSpiInterface::transfer(uint8_t data) @@ -17,11 +21,17 @@ namespace sta void HalSpiInterface::transfer(const uint8_t * buffer, size_t size) { + STA_ASSERT(buffer != nullptr); + HAL_SPI_Transmit(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(handle_, const_cast(txBuffer), rxBuffer, size, HAL_MAX_DELAY); } @@ -36,6 +46,8 @@ namespace sta void HalSpiInterface::fill(uint8_t value, size_t count) { + STA_ASSERT(count != 0); + for (size_t i = 0; i < count; ++i) { transfer(value); @@ -45,6 +57,7 @@ namespace sta void HalSpiInterface::fill32(uint32_t value, size_t count) { static_assert(sizeof(uint32_t) == 4); + STA_ASSERT(count != 0); for (size_t i = 0; i < count; ++i) { @@ -53,9 +66,11 @@ namespace sta } - void HalSpiInterface::receive(uint8_t * data, size_t size) + void HalSpiInterface::receive(uint8_t * buffer, size_t size) { - HAL_SPI_Receive(handle_, data, size, HAL_MAX_DELAY); + STA_ASSERT(buffer != nullptr); + + HAL_SPI_Receive(handle_, buffer, size, HAL_MAX_DELAY); } diff --git a/src/hal/uart.cpp b/src/hal/uart.cpp index 7d3fcc6..d0bd319 100644 --- a/src/hal/uart.cpp +++ b/src/hal/uart.cpp @@ -2,16 +2,22 @@ #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 diff --git a/src/spi_device.cpp b/src/spi_device.cpp index 12d2571..701d0e3 100644 --- a/src/spi_device.cpp +++ b/src/spi_device.cpp @@ -1,11 +1,15 @@ #include +#include + namespace sta { SpiDevice::SpiDevice(SpiInterface * intf) : intf_{intf} - {} + { + STA_ASSERT(intf != nullptr); + } void SpiDevice::beginTransmission() { @@ -29,13 +33,19 @@ namespace sta intf_->transfer(data); } - void SpiDevice::transfer(const uint8_t * data, size_t size) + void SpiDevice::transfer(const uint8_t * buffer, size_t size) { - intf_->transfer(data, size); + STA_ASSERT(buffer != nullptr); + + intf_->transfer(buffer, size); } void SpiDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) { + STA_ASSERT(txBuffer != nullptr); + STA_ASSERT(rxBuffer != nullptr); + STA_ASSERT(size != 0); + intf_->transfer(txBuffer, rxBuffer, size); } @@ -46,16 +56,22 @@ namespace sta void SpiDevice::fill(uint8_t value, size_t count) { + STA_ASSERT(count != 0); + intf_->fill(value, count); } void SpiDevice::fill32(uint32_t value, size_t count) { + STA_ASSERT(count != 0); + intf_->fill32(value, count); } void SpiDevice::receive(uint8_t * buffer, size_t size) { + STA_ASSERT(buffer != nullptr); + intf_->receive(buffer, size); } } // namespace sta