Add asserts. Fix formating

This commit is contained in:
Henrik Stickann 2022-04-12 16:27:55 +02:00
parent 9f2c73c9ab
commit f09f580f00
5 changed files with 52 additions and 10 deletions

View File

@ -1,8 +1,9 @@
/** /**
* @brief Implementations for SpiInterface and SpiDevice using HAL. * @brief Implementations for SpiInterface and SpiDevice using HAL.
* *
* Define STA_HAL_SPI_ENABLE in <sta/config.hpp> to enable. * Define **STA_HAL_SPI_ENABLE** in `<sta/config.hpp>` to enable module.
* Requires STA_HAL_GPIO_ENABLE. *
* Requires **STA_HAL_GPIO_ENABLE**.
*/ */
#ifndef STA_HAL_SPI_HPP #ifndef STA_HAL_SPI_HPP
#define STA_HAL_SPI_HPP #define STA_HAL_SPI_HPP

View File

@ -2,12 +2,16 @@
#ifdef STA_HAL_GPIO_ENABLE #ifdef STA_HAL_GPIO_ENABLE
#include <sta/assert.hpp>
namespace sta namespace sta
{ {
HalGpioPin::HalGpioPin(GPIO_TypeDef * port, uint16_t pin) HalGpioPin::HalGpioPin(GPIO_TypeDef * port, uint16_t pin)
: port_{port}, pin_{pin} : port_{port}, pin_{pin}
{} {
STA_ASSERT(port != nullptr);
}
void HalGpioPin::setState(GpioPinState state) void HalGpioPin::setState(GpioPinState state)
{ {

View File

@ -2,12 +2,16 @@
#ifdef STA_HAL_SPI_ENABLE #ifdef STA_HAL_SPI_ENABLE
#include <sta/assert.hpp>
namespace sta namespace sta
{ {
HalSpiInterface::HalSpiInterface(SPI_HandleTypeDef * handle, Mutex * mutex /* = nullptr */) HalSpiInterface::HalSpiInterface(SPI_HandleTypeDef * handle, Mutex * mutex /* = nullptr */)
: SpiInterface(mutex), handle_{handle} : SpiInterface(mutex), handle_{handle}
{} {
STA_ASSERT(handle != nullptr);
}
void HalSpiInterface::transfer(uint8_t data) void HalSpiInterface::transfer(uint8_t data)
@ -17,11 +21,17 @@ namespace sta
void HalSpiInterface::transfer(const uint8_t * buffer, size_t size) void HalSpiInterface::transfer(const uint8_t * buffer, size_t size)
{ {
STA_ASSERT(buffer != nullptr);
HAL_SPI_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY); HAL_SPI_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
} }
void HalSpiInterface::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) 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<uint8_t *>(txBuffer), rxBuffer, size, HAL_MAX_DELAY); HAL_SPI_TransmitReceive(handle_, const_cast<uint8_t *>(txBuffer), rxBuffer, size, HAL_MAX_DELAY);
} }
@ -36,6 +46,8 @@ namespace sta
void HalSpiInterface::fill(uint8_t value, size_t count) void HalSpiInterface::fill(uint8_t value, size_t count)
{ {
STA_ASSERT(count != 0);
for (size_t i = 0; i < count; ++i) for (size_t i = 0; i < count; ++i)
{ {
transfer(value); transfer(value);
@ -45,6 +57,7 @@ namespace sta
void HalSpiInterface::fill32(uint32_t value, size_t count) void HalSpiInterface::fill32(uint32_t value, size_t count)
{ {
static_assert(sizeof(uint32_t) == 4); static_assert(sizeof(uint32_t) == 4);
STA_ASSERT(count != 0);
for (size_t i = 0; i < count; ++i) 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);
} }

View File

@ -2,16 +2,22 @@
#ifdef STA_HAL_UART_ENABLE #ifdef STA_HAL_UART_ENABLE
#include <sta/assert.hpp>
namespace sta namespace sta
{ {
HalUART::HalUART(UART_HandleTypeDef * handle) HalUART::HalUART(UART_HandleTypeDef * handle)
: handle_{handle} : handle_{handle}
{} {
STA_ASSERT(handle != nullptr);
}
void HalUART::write(const uint8_t * buffer, size_t size) void HalUART::write(const uint8_t * buffer, size_t size)
{ {
STA_ASSERT(buffer != nullptr);
HAL_UART_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY); HAL_UART_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
} }
} // namespace sta } // namespace sta

View File

@ -1,11 +1,15 @@
#include <sta/spi_device.hpp> #include <sta/spi_device.hpp>
#include <sta/assert.hpp>
namespace sta namespace sta
{ {
SpiDevice::SpiDevice(SpiInterface * intf) SpiDevice::SpiDevice(SpiInterface * intf)
: intf_{intf} : intf_{intf}
{} {
STA_ASSERT(intf != nullptr);
}
void SpiDevice::beginTransmission() void SpiDevice::beginTransmission()
{ {
@ -29,13 +33,19 @@ namespace sta
intf_->transfer(data); 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) 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); intf_->transfer(txBuffer, rxBuffer, size);
} }
@ -46,16 +56,22 @@ namespace sta
void SpiDevice::fill(uint8_t value, size_t count) void SpiDevice::fill(uint8_t value, size_t count)
{ {
STA_ASSERT(count != 0);
intf_->fill(value, count); intf_->fill(value, count);
} }
void SpiDevice::fill32(uint32_t value, size_t count) void SpiDevice::fill32(uint32_t value, size_t count)
{ {
STA_ASSERT(count != 0);
intf_->fill32(value, count); intf_->fill32(value, count);
} }
void SpiDevice::receive(uint8_t * buffer, size_t size) void SpiDevice::receive(uint8_t * buffer, size_t size)
{ {
STA_ASSERT(buffer != nullptr);
intf_->receive(buffer, size); intf_->receive(buffer, size);
} }
} // namespace sta } // namespace sta