Added I2C support for raspi & first rework of debugging

This commit is contained in:
Dario
2023-06-23 15:50:54 +01:00
parent 3cf2173433
commit 6b4acfd27b
70 changed files with 985 additions and 772 deletions

View File

@@ -0,0 +1,129 @@
#include <sta/devices/raspi/bus/i2c.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/assert.hpp>
#include <cstdlib>
#include <string.h>
#include <iostream>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
namespace sta
{
RaspiI2c::RaspiI2c(I2cNode node, Mutex * mutex, bool persistent_open)
: I2c{mutex}, persistent_open_{persistent_open}
{
// Safer version of malloc + strcpy
i2cdev_ = strdup(node == I2cNode::DEV_1 ? "/dev/i2c-1" : "/dev/i2c-2");
STA_ASSERT(i2cdev_ != nullptr);
}
RaspiI2c::~RaspiI2c()
{
if (i2cdev_ != NULL ) {
free(i2cdev_);
i2cdev_ = NULL;
}
if (open_) {
close(i2cfd_);
}
}
void RaspiI2c::transfer(uint8_t value)
{
STA_ASSERT(open_);
write(i2cfd_, &value, 1);
}
void RaspiI2c::transfer16(uint16_t value)
{
STA_ASSERT(open_);
write(i2cfd_, &value, 2);
}
void RaspiI2c::transfer(const uint8_t * buffer, size_t size)
{
STA_ASSERT(open_);
write(i2cfd_, buffer, size);
}
void RaspiI2c::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{
STA_ASSERT(open_);
// TODO: Is this even possible in i2c?
}
void RaspiI2c::receive(uint8_t * buffer, size_t size)
{
STA_ASSERT(open_);
if (read(i2cfd_, buffer, size) <= 0)
{
printf("Error while reading i2c bus.");
}
}
void RaspiI2c::fill(uint8_t value, size_t count)
{
STA_ASSERT(open_);
// Initialize a buffer of size count and fill it with the value.
uint8_t *buffer = new uint8_t[count];
memset(buffer, value, count);
write(i2cfd_, buffer, count);
delete [] buffer;
}
void RaspiI2c::selectAddress(uint16_t address)
{
if (ioctl(i2cfd_, I2C_SLAVE, address) < 0)
{
printf("Failed to send the slave address.");
}
}
void RaspiI2c::acquire()
{
I2c::acquire();
if (open_) {
return;
}
i2cfd_ = open(i2cdev_, O_RDWR);
open_ = true;
STA_ASSERT(i2cfd_ >= 0);
}
void RaspiI2c::release()
{
if (!persistent_open_ && open_) {
close(i2cfd_);
}
I2c::release();
}
RaspiI2cDevice::RaspiI2cDevice(I2c * intf, uint16_t address_10bit, Mutex* mutex, bool master, bool blocking)
: I2cDevice { intf, address_10bit }
{
}
} // namespace sta
#endif // STA_PLATFORM_RASPI

View File

@@ -0,0 +1,314 @@
#include <sta/devices/raspi/bus/spi.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/devices/raspi/gpio_pin.hpp>
#include <sta/bus/spi/settings.hpp>
#include <sta/assert.hpp>
#include <sta/endian.hpp>
#include <sta/lang.hpp>
#include <cstdlib>
#include <string.h>
#include <cstdio>
// Imports needed for SPI handling on Linux.
#include <linux/spi/spidev.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#define STA_DEBUG_IOCTL_WRITE(result) STA_ASSERT_MSG(result >= 0, "ioctl writing failed!")
#define STA_DEBUG_IOCTL_READ(result) STA_ASSERT_MSG(result >= 0, "ioctl reading failed!")
#define STA_DEBUG_IOCTL_SEND(result) STA_ASSERT_MSG(result >= 0, "ioctl sending spi message failed!")
#define STA_DEBUG_FD_OPEN() STA_ASSERT_MSG(open_, "File descriptor wasn't opened!")
#define STA_DEBUG_NULLPTR(ptr) STA_ASSERT_MSG(ptr != nullptr, "Pointer is nullptr!")
namespace sta
{
RaspiSPI::RaspiSPI(SPINode node, const SPISettings & settings, Mutex * mutex, bool persistent_open)
: SPI(settings, mutex), open_{ false }, persistent_open_{ persistent_open }, settings_{ settings }
{
// Check validity of parameters.
STA_DEBUG_NULLPTR(mutex);
open_ = false;
// Safer version of malloc + strcpy
spidev_ = strdup(node == SPINode::DEV_0_0 ? "/dev/spidev0.0" : "/dev/spidev0.1");
// Check if memory allocation was successful.
STA_DEBUG_NULLPTR(spidev_);
}
RaspiSPI::~RaspiSPI()
{
if (spidev_ != NULL ) {
free(spidev_);
spidev_ = NULL;
}
if (open_) {
close(spifd_);
}
}
void RaspiSPI::transfer(uint8_t value)
{
STA_DEBUG_FD_OPEN();
struct spi_ioc_transfer spi_message[1];
// According to the documentation, spi_message should be zero intialized.
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].tx_buf = (unsigned long)&value;
spi_message[0].len = 1;
// For some reasons, this line makes the SPI interface work for the BMP388.
spi_message[0].cs_change = 1;
int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message);
if (result < 0) {
printf("Sending failed with error '%s'! \n", strerror(errno));
}
STA_DEBUG_IOCTL_SEND(result);
}
void RaspiSPI::transfer16(uint16_t value)
{
STA_DEBUG_FD_OPEN();
struct spi_ioc_transfer spi_message[1];
// According to the documentation, spi_message should be zero intialized.
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].tx_buf = (unsigned long)&value;
spi_message[0].len = 1;
spi_message[0].cs_change = 1;
int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message);
if (result < 0) {
printf("Sending failed with error '%s'! \n", strerror(errno));
}
STA_DEBUG_IOCTL_SEND(result);
}
void RaspiSPI::transfer(const uint8_t * buffer, size_t size)
{
STA_DEBUG_FD_OPEN();
STA_DEBUG_NULLPTR(buffer);
STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!");
struct spi_ioc_transfer spi_message[1];
// According to the documentation, spi_message should be zero intialized.
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].tx_buf = (unsigned long)buffer;
spi_message[0].len = size;
int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message);
if (result < 0) {
printf("Sending failed with error '%s'! \n", strerror(errno));
}
STA_DEBUG_IOCTL_SEND(result);
}
void RaspiSPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{
STA_DEBUG_FD_OPEN();
STA_DEBUG_NULLPTR(txBuffer);
STA_DEBUG_NULLPTR(rxBuffer);
STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!");
struct spi_ioc_transfer spi_message[1];
// According to the documentation, spi_message should be zero intialized.
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].rx_buf = (unsigned long)rxBuffer;
spi_message[0].tx_buf = (unsigned long)txBuffer;
spi_message[0].len = size;
int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message);
if (result < 0) {
printf("Sending failed with error '%s'! \n", strerror(errno));
}
STA_DEBUG_IOCTL_SEND(result);
}
void RaspiSPI::receive(uint8_t * buffer, size_t size)
{
STA_DEBUG_FD_OPEN();
STA_DEBUG_NULLPTR(buffer);
STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!");
struct spi_ioc_transfer spi_message[1];
// According to the documentation, spi_message should be zero intialized.
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].rx_buf = (unsigned long)buffer;
spi_message[0].len = size;
int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message);
STA_DEBUG_IOCTL_SEND(result);
}
void RaspiSPI::fill(uint8_t value, size_t count)
{
STA_DEBUG_FD_OPEN();
STA_ASSERT_MSG(count != 0, "Buffer size cannot be 0!");
uint8_t * buffer = new uint8_t[count];
memset(buffer, value, count);
struct spi_ioc_transfer spi_message[1];
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].tx_buf = (unsigned long)buffer;
spi_message[0].len = count;
int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message);
STA_DEBUG_IOCTL_SEND(result);
delete[] buffer;
}
void RaspiSPI::update_setting(int setting, int value)
{
}
void RaspiSPI::get_setting(int setting, void * rslt_ptr)
{
STA_DEBUG_FD_OPEN();
int result = ioctl(spifd_, setting, rslt_ptr);
STA_DEBUG_IOCTL_READ(result);
}
void RaspiSPI::update_settings()
{
STA_DEBUG_FD_OPEN();
switch (settings_.mode)
{
case SPIMode::MODE_0:
mode_ = SPI_MODE_0;
break;
case SPIMode::MODE_1:
mode_ = SPI_MODE_1;
break;
case SPIMode::MODE_2:
mode_ = SPI_MODE_2;
break;
case SPIMode::MODE_3:
mode_ = SPI_MODE_3;
break;
default:
STA_ASSERT_MSG(false, "Case for SPIMode enum not handled");
STA_UNREACHABLE();
}
dataSize_ = settings_.dataSize == SPIDataSize::SIZE_8 ? 0 : 16;
bitOrder_ = settings_.bitOrder == SPIBitOrder::MSB ? 0 : 1;
clkSpeed_ = settings_.clkSpeed;
// Set the spi mode.
int result = ioctl(spifd_, SPI_IOC_WR_MODE, &mode_);
STA_DEBUG_IOCTL_WRITE(result);
if (result < 0) {
printf("Update mode failed with error '%s'! \n", strerror(errno));
}
// Set the word size. According to the documentation "the value zero signifies eight bits".
result = ioctl(spifd_, SPI_IOC_WR_BITS_PER_WORD, &dataSize_);
STA_DEBUG_IOCTL_WRITE(result);
if (result < 0) {
printf("Update dataSize failed with error '%s'! \n", strerror(errno));
}
// Set the bit order. According to the documentation zero means MSB first, everything else means LSB first.
result = ioctl(spifd_, SPI_IOC_WR_LSB_FIRST, &bitOrder_);
STA_DEBUG_IOCTL_WRITE(result);
if (result < 0) {
printf("Update endianness failed with error '%s'! \n", strerror(errno));
}
// Set the maximum clock speed.
result = ioctl(spifd_, SPI_IOC_WR_MAX_SPEED_HZ, &clkSpeed_);
STA_DEBUG_IOCTL_WRITE(result);
if (result < 0) {
printf("Update clock speed failed with error '%s'! \n", strerror(errno));
}
#if defined(DEBUG)
uint8_t r_mode = 0;
get_setting(SPI_IOC_RD_MODE, &r_mode);
STA_ASSERT_MSG(r_mode == mode_, "The mode wasn't set correctly!");
uint8_t r_dataSize = 0;
get_setting(SPI_IOC_WR_BITS_PER_WORD, &r_dataSize);
STA_ASSERT_MSG(r_dataSize == (settings_.dataSize == SPIDataSize::SIZE_8 ? 8 : 16), "The data size wasn't set correctly!");
uint8_t r_bitOrder = 0;
get_setting(SPI_IOC_RD_LSB_FIRST, &r_bitOrder);
STA_ASSERT_MSG(r_bitOrder == (settings_.bitOrder == SPIBitOrder::MSB ? 0 : 1), "The data size wasn't set correctly!");
uint32_t r_clk_Speed;
get_setting(SPI_IOC_RD_MAX_SPEED_HZ, &r_clk_Speed);
STA_ASSERT_MSG(r_clk_Speed == settings_.clkSpeed, "The clock speed wasn't set correctly!");
#endif // STA_DEBUG
}
void RaspiSPI::acquire()
{
SPI::acquire();
STA_ASSERT(spidev_ != nullptr);
/* open spidev device */
if (open_ == true)
return;
spifd_ = open(spidev_, O_RDWR);
open_ = true;
STA_ASSERT(spifd_ >= 0);
update_settings();
}
void RaspiSPI::release()
{
if (!persistent_open_ && open_) {
close(spifd_);
}
SPI::release();
}
RaspiSPIDevice::RaspiSPIDevice(RaspiSPI * intf) : SPIDevice { intf, RaspiGpioPin::DUMMY_GPIO }
{
}
} // namespace sta
#endif // STA_PlATFORM_RASPI

View File

@@ -0,0 +1,23 @@
#include <sta/devices/raspi/delay.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/devices/raspi/hal.hpp>
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
void delayMs(uint32_t ms)
{
delay(ms);
}
void delayUs(uint32_t us)
{
delayMicroseconds(us);
}
} // namespace sta
#endif // STA_PLATFORM_RASPI

View File

@@ -0,0 +1,43 @@
#include <sta/raspi/gpio_pin.hpp>
#ifdef STA_RASPI_GPIO_ENABLED
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
RaspiGpioPin::RaspiGpioPin(uint8_t pin, GpioMode mode) : pin_{ pin }, mode_{ mode }
{
pinMode(pin, mode == GpioMode::GPIO_INPUT ? INPUT : OUTPUT);
}
void RaspiGpioPin::setState(GpioPinState state)
{
digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH);
}
GpioPinState RaspiGpioPin::getState()
{
return digitalRead(pin_) == LOW ? GpioPinState::GPIO_LOW : GpioPinState::GPIO_HIGH;
}
class DummyGpioPin : public RaspiGpioPin
{
public:
DummyGpioPin() : RaspiGpioPin { 0, GpioMode::GPIO_INPUT } {}
void setState(GpioPinState state) override {}
GpioPinState getState() override {
// This should really never be called since the code might rely on the returned value to be accurate.
STA_UNREACHABLE();
}
};
static DummyGpioPin dummyGpio;
RaspiGpioPin * RaspiGpioPin::DUMMY_GPIO = &dummyGpio;
} // namespace sta
#endif // STA_ARDUINO_GPIO_ENABLED

210
src/devices/stm32/can.cpp Normal file
View File

@@ -0,0 +1,210 @@
#include <sta/stm32/can.hpp>
#ifdef STA_STM32_CAN_ENABLED
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
STM32CanController::STM32CanController(CAN_HandleTypeDef * handle)
: handle_{handle}
{
initFilters();
}
void STM32CanController::enableRxInterrupts()
{
HAL_CAN_ActivateNotification(handle_,
CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_RX_FIFO1_MSG_PENDING
);
}
void STM32CanController::start()
{
HAL_CAN_Start(handle_);
}
void STM32CanController::stop()
{
HAL_CAN_Stop(handle_);
}
bool STM32CanController::sendFrame(const CanTxHeader & header, const uint8_t * payload)
{
STA_ASSERT_MSG(header.payloadLength <= 8, "CAN 2.0B payload size exceeded");
CAN_TxHeaderTypeDef halHeader;
if (header.id.format == CanIdFormat::STD)
{
halHeader.StdId = header.id.sid & 0x7FF;
halHeader.IDE = CAN_ID_STD;
}
else
{
// Combine SID and EID
halHeader.ExtId = ((header.id.sid & 0x7FF) << 18) | (header.id.eid & 0x3FFFF);
halHeader.IDE = CAN_ID_EXT;
}
halHeader.DLC = header.payloadLength;
uint32_t mailbox; // Don't care
return (HAL_OK == HAL_CAN_AddTxMessage(handle_, &halHeader, const_cast<uint8_t *>(payload), &mailbox));
}
bool STM32CanController::receiveFrame(uint8_t fifo, CanRxHeader * header, uint8_t * payload)
{
// Check if message is available
if (HAL_CAN_GetRxFifoFillLevel(handle_, fifo) == 0)
return false;
// Retrieve message
CAN_RxHeaderTypeDef halHeader;
HAL_CAN_GetRxMessage(handle_, fifo, &halHeader, payload);
if (halHeader.IDE == CAN_ID_STD)
{
header->id.format = CanIdFormat::STD;
header->id.sid = halHeader.StdId;
header->id.eid = 0;
}
else
{
header->id.format = CanIdFormat::EXT;
// Separate SID and EID
header->id.sid = (halHeader.ExtId >> 18);
header->id.eid = halHeader.ExtId & 0x3FFFF;
}
// No conversion required for CAN 2B standard
header->payloadLength = halHeader.DLC;
header->timestamp = halHeader.Timestamp;
header->filter = halHeader.FilterMatchIndex;
return true;
}
uint32_t STM32CanController::getRxFifoFlags()
{
//
return (HAL_CAN_GetRxFifoFillLevel(handle_, CAN_RX_FIFO0) != 0)
| (HAL_CAN_GetRxFifoFillLevel(handle_, CAN_RX_FIFO1) != 0) << 1;
}
void STM32CanController::configureFilter(uint8_t idx, const CanFilter & filter, bool active /* = false */)
{
CAN_FilterTypeDef * config = &filters_[idx];
if (filter.type == CanFilterIdFormat::STD)
{
config->FilterIdHigh = 0;
config->FilterIdLow = filter.obj.sid & 0x7FF;
config->FilterMaskIdHigh = 0;
config->FilterMaskIdLow = filter.mask.sid & 0x7FF;
}
else
{
config->FilterIdHigh = ((filter.obj.sid & 0x7FF) << 2) | ((filter.obj.eid >> 16) & 0x3);
config->FilterIdLow = filter.obj.eid & 0xFFFF;
config->FilterMaskIdHigh = ((filter.mask.sid & 0x7FF) << 2) | ((filter.mask.eid >> 16) & 0x3);
config->FilterMaskIdLow = filter.mask.eid & 0xFFFF;
}
config->FilterFIFOAssignment = filter.fifo;
config->FilterActivation = (active ? CAN_FILTER_ENABLE : CAN_FILTER_DISABLE);
HAL_CAN_ConfigFilter(handle_, config);
}
void STM32CanController::enableFilter(uint8_t idx)
{
CAN_FilterTypeDef * config = &filters_[idx];
config->FilterActivation = CAN_FILTER_ENABLE;
HAL_CAN_ConfigFilter(handle_, config);
}
void STM32CanController::disableFilter(uint8_t idx)
{
CAN_FilterTypeDef * config = &filters_[idx];
config->FilterActivation = CAN_FILTER_DISABLE;
HAL_CAN_ConfigFilter(handle_, config);
}
void STM32CanController::clearFilters()
{
for (uint32_t i = 0; i < MAX_FILTER_COUNT; ++i)
{
CAN_FilterTypeDef * config = &filters_[i];
// Only disable active filters
if (config->FilterActivation == CAN_FILTER_ENABLE)
{
config->FilterActivation = CAN_FILTER_DISABLE;
HAL_CAN_ConfigFilter(handle_, config);
}
}
}
void STM32CanController::initFilters()
{
for (uint32_t i = 0; i < MAX_FILTER_COUNT; ++i)
{
CAN_FilterTypeDef * config = &filters_[i];
config->FilterBank = i;
config->FilterMode = CAN_FILTERMODE_IDMASK;
config->FilterScale = CAN_FILTERSCALE_32BIT;
config->FilterActivation = CAN_FILTER_DISABLE;
config->SlaveStartFilterBank = MAX_FILTER_COUNT;
}
}
} // namespace sta
#ifdef STA_STM32_CAN_GLOBAL
#include <can.h>
namespace sta
{
STM32CanController CanBus(&STA_STM32_CAN_GLOBAL);
STA_WEAK
void CanBus_RxPendingCallback()
{}
} // namespace sta
extern "C"
{
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if (hcan == &STA_STM32_CAN_GLOBAL)
{
sta::CanBus_RxPendingCallback();
}
}
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if (hcan == &STA_STM32_CAN_GLOBAL)
{
sta::CanBus_RxPendingCallback();
}
}
}
#endif // STA_STM32_CAN_GLOBAL
#endif // STA_STM32_CAN_ENABLED

View File

@@ -0,0 +1,75 @@
#include <sta/stm32/delay.hpp>
#ifdef STA_PLATFORM_STM32
#include <sta/stm32/hal.hpp>
#include <sta/stm32/clocks.hpp>
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
void delayMs(uint32_t ms)
{
HAL_Delay(ms);
}
} // namespace sta
#ifdef STA_STM32_DELAY_US_TIM
#ifndef HAL_TIM_MODULE_ENABLED
# error "STM32 HAL TIM module not enabled!"
#endif // HAL_TIM_MODULE_ENABLED
#include <tim.h>
namespace sta
{
uint32_t gDelayUsMul = 1;
void delayUs(uint32_t us)
{
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul);
}
bool isValidDelayUsTIM()
{
// Get PCLK multiplier for TIM clock
uint32_t pclkMul = 1;
switch (STA_STM32_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_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)();
// Calculate update frequency based on prescaler value
uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1;
uint32_t updateFreq = clkFreq / prescaler;
gDelayUsMul = updateFreq / 1000000;
// TIM must have at least microsecond precision (>= 1 MHz frequency)
return (updateFreq >= 1000000);
}
} // namespace sta
#endif // STA_STM32_DELAY_US_TIM
#endif // STA_PLATFORM_STM32

View File

@@ -0,0 +1,83 @@
#include <sta/stm32/gpio_pin.hpp>
#ifdef STA_STM32_GPIO_ENABLED
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
STM32GpioPin::STM32GpioPin(GPIO_TypeDef * port, uint16_t pin)
: port_{port}, pin_{pin}
{
STA_ASSERT(port != nullptr);
}
void STM32GpioPin::setState(GpioPinState state)
{
HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
}
GPIO_TypeDef * STM32GpioPin::getPort() const
{
return port_;
}
uint16_t STM32GpioPin::getPin() const
{
return pin_;
}
uint8_t STM32GpioPin::getPortIndex() const
{
return GPIO_GET_INDEX(port_);
}
bool isInterruptEdge(const STM32GpioPin & gpioPin, InterruptEdge edge)
{
uint32_t pin = gpioPin.getPin();
for (uint32_t i = 0; i < 8 * sizeof(pin); ++i)
{
uint32_t ioPos = 1U << i;
if (pin & ioPos)
{
// Check input mode
uint32_t mode = (gpioPin.getPort()->MODER >> (2U * i)) & GPIO_MODE;
if (mode != MODE_INPUT)
{
return false;
}
// Is EXTI configured?
if (EXTI->IMR & ioPos)
{
bool rising = (EXTI->RTSR & ioPos);
bool falling = (EXTI->FTSR & ioPos);
switch (edge)
{
case InterruptEdge::RISING:
return rising;
case InterruptEdge::FALLING:
return falling;
case InterruptEdge::BOTH:
return rising && falling;
default:
STA_ASSERT(false);
STA_UNREACHABLE();
}
}
}
}
return false;
}
} // namespace sta
#endif // STA_STM32_GPIO_ENABLED

57
src/devices/stm32/i2c.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include <sta/stm32/i2c.hpp>
#if false
namespace sta {
STM32I2cDevice::STM32I2cDevice(I2C_HandleTypeDef* i2cHandle, uint16_t address, Mutex* mutex, bool master, bool blocking)
: I2cDevice(address, mutex, master, blocking) {
this->master = master;
}
bool STM32I2cDevice::transmit(uint8_t* data, uint16_t size) {
HAL_StatusTypeDef res;
if (this->blocking) {
if (!this->master) {
res = HAL_I2C_Master_Transmit(i2cHandle, address, data, size, this->timeout);
} else {
res = HAL_I2C_Slave_Transmit(i2cHandle , data, size, this->timeout);
}
} else {
if (!this->master) {
res = HAL_I2C_Master_Transmit_IT(i2cHandle, address, data, size);
} else {
res = HAL_I2C_Slave_Transmit_IT(i2cHandle , data, size);
}
}
return res == HAL_OK;
}
bool STM32I2cDevice::receive(uint8_t* data, uint16_t size) {
HAL_StatusTypeDef res;
if (this->blocking) {
if (!this->master) {
res = HAL_I2C_Master_Receive(i2cHandle, address, data, size, this->timeout);
} else {
res = HAL_I2C_Slave_Receive(i2cHandle , data, size, this->timeout);
}
} else {
if (!this->master) {
res = HAL_I2C_Master_Receive_IT(i2cHandle, address, data, size);
} else {
res = HAL_I2C_Slave_Receive_IT(i2cHandle , data, size);
}
}
return res == HAL_OK;
}
bool STM32I2cDevice::deviceReady() {
HAL_StatusTypeDef res = HAL_I2C_IsDeviceReady(this->i2cHandle, this->address, 8, this->timeout);
return res == HAL_OK;
}
}
#endif // false

View File

@@ -0,0 +1,28 @@
#include <sta/stm32/init.hpp>
#include <sta/assert.hpp>
#ifdef STA_STM32_DELAY_US_TIM
#ifndef HAL_TIM_MODULE_ENABLED
# error "STM32 HAL TIM module not enabled!"
#endif // HAL_TIM_MODULE_ENABLED
#include <tim.h>
#endif // STA_STM32_DELAY_US_TIM
namespace sta
{
void initHAL()
{
#ifdef STA_STM32_DELAY_US_TIM
// Validate TIM used for delayUs
extern bool isValidDelayUsTIM();
STA_ASSERT(isValidDelayUsTIM());
// Start timer base
HAL_TIM_Base_Start(&STA_STM32_DELAY_US_TIM);
#endif // STA_STM32_DELAY_US_TIM
}
} // namespace sta

168
src/devices/stm32/spi.cpp Normal file
View File

@@ -0,0 +1,168 @@
#include <sta/stm32/spi.hpp>
#ifdef STA_STM32_SPI_ENABLED
#include <sta/assert.hpp>
#include <sta/endian.hpp>
#include <sta/lang.hpp>
#ifdef STA_MCU_LITTLE_ENDIAN
# define STA_STM32_SPI_REVERSE_BIT_ORDER SPIBitOrder::MSB
#elif STA_MCU_BIG_ENDIAN
# define STA_STM32_SPI_REVERSE_BIT_ORDER SPIBitOrder::LSB
#endif
namespace sta
{
static SPISettings getSPISettings(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;
}
STM32SPI::STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex = nullptr)
: SPI(getSPISettings(handle, pclkFreq), mutex), handle_{handle}
{
STA_ASSERT(handle != nullptr);
}
STM32SPI::STM32SPI(const Info & info, Mutex * mutex /* = nullptr */)
: STM32SPI(info.handle, info.pclkFreq, mutex)
{}
void STM32SPI::transfer(uint8_t value)
{
if (settings().dataSize == SPIDataSize::SIZE_8)
{
HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
}
else
{
// Required since tx buffer is cast to uint16_t * internally
uint16_t dummy = value;
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
}
}
void STM32SPI::transfer16(uint16_t value)
{
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_STM32_SPI_REVERSE_BIT_ORDER)
{
// Reverse byte order from internal representation
value = STA_UINT16_SWAP_BYTE_ORDER(value);
}
}
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), size, HAL_MAX_DELAY);
}
void STM32SPI::transfer(const uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
STA_ASSERT(size != 0);
HAL_SPI_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
}
void STM32SPI::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);
}
void STM32SPI::receive(uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
HAL_SPI_Receive(handle_, buffer, size, HAL_MAX_DELAY);
}
void STM32SPI::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(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(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
}
}
}
STM32SPIDevice::STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin)
: SPIDevice(intf, &csPin_), csPin_{csPin}
{}
} // namespace sta
#endif // STA_STM32_SPI_ENABLED

View File

@@ -0,0 +1,25 @@
#include <sta/devices/stm32/uart.hpp>
#ifdef STA_STM32_UART_ENABLED
#include <sta/assert.hpp>
namespace sta
{
STM32UART::STM32UART(UART_HandleTypeDef * handle)
: handle_{handle}
{
STA_ASSERT(handle != nullptr);
}
void STM32UART::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);
}
} // namespace sta
#endif // STA_STM32_UART_ENABLED

View File

@@ -0,0 +1,36 @@
/**
* @file delay.cpp
* @author <your name> (<you>@<your_domain>.com)
* @brief
* @version 0.1
* @date 2023-06-13
*
* @copyright Copyright (c) 2023
*
* How to modify this file:
* - Ctrl + F and replace "YOUR_DEVICE" with the appropriate name.
* - Implement the functions delayMs and delayUs.
* - Remove the <sta/lang.hpp> import if no longer needed.
*/
#define STA_PLATFORM_YOUR_DEVICE
#include <sta/devices/template/delay.hpp>
#ifdef STA_PLATFORM_YOUR_DEVICE
#include <sta/lang.hpp>
namespace sta
{
void delayMs(uint32_t ms)
{
STA_NOT_IMPLEMENTED();
}
void delayUs(uint32_t us)
{
STA_NOT_IMPLEMENTED();
}
} // namespace sta
#endif // STA_PLATFORM_YOUR_DEVICE