mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-09-29 05:17:33 +00:00
Added I2C support for raspi & first rework of debugging
This commit is contained in:
210
src/devices/stm32/can.cpp
Normal file
210
src/devices/stm32/can.cpp
Normal 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
|
75
src/devices/stm32/delay.cpp
Normal file
75
src/devices/stm32/delay.cpp
Normal 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
|
83
src/devices/stm32/gpio_pin.cpp
Normal file
83
src/devices/stm32/gpio_pin.cpp
Normal 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
57
src/devices/stm32/i2c.cpp
Normal 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
|
28
src/devices/stm32/init.cpp
Normal file
28
src/devices/stm32/init.cpp
Normal 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
168
src/devices/stm32/spi.cpp
Normal 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
|
25
src/devices/stm32/uart.cpp
Normal file
25
src/devices/stm32/uart.cpp
Normal 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
|
Reference in New Issue
Block a user