13#if defined(STM32) && defined(HAL_I2C_MODULE_ENABLED)
16LinkedQueue<Stm32I2cSlave> Stm32I2cSlave::m_queue;
18: m_hwHandle{&hwHandle}
25 HAL_StatusTypeDef
error = HAL_I2C_Init(m_hwHandle);
28 if (
error == HAL_ERROR)
33 else if (
error == HAL_BUSY)
38 else if (
error == HAL_TIMEOUT)
49 HAL_StatusTypeDef
error = HAL_I2C_DeInit(m_hwHandle);
52 if (
error == HAL_ERROR)
57 else if (
error == HAL_BUSY)
62 else if (
error == HAL_TIMEOUT)
94 for (
auto& i2c : m_queue)
96 if (i2c.m_hwHandle == &hwHandle)
98 i2c.onDataAvailable();
105 for (
auto& i2c : m_queue)
107 if (i2c.m_hwHandle == &hwHandle)
116 for (
auto& i2c : m_queue)
118 if (i2c.m_hwHandle == &hwHandle)
120 HAL_StatusTypeDef
error = HAL_I2C_EnableListen_IT(i2c.m_hwHandle);
123 if (
error == HAL_ERROR)
128 else if (
error == HAL_BUSY)
133 else if (
error == HAL_TIMEOUT)
146 for (
auto& i2c : m_queue)
148 if (i2c.m_hwHandle == &hwHandle)
155 for (
auto& i2c : m_queue)
157 if (i2c.m_hwHandle == &hwHandle)
159 if (direction == I2C_DIRECTION_TRANSMIT)
161 else if (direction == I2C_DIRECTION_RECEIVE)
169 SEMF_INFO(
"data %p, size %u", buffer, bufferSize);
170 __HAL_UNLOCK(m_hwHandle);
171 HAL_StatusTypeDef
error = HAL_I2C_Slave_Seq_Receive_IT(m_hwHandle, buffer,
static_cast<uint16_t
>(bufferSize),
hardwareFrame(
frame()));
174 if (
error == HAL_ERROR)
179 else if (
error == HAL_BUSY)
184 else if (
error == HAL_TIMEOUT)
195 SEMF_INFO(
"data %p, size %u", data, dataSize);
196 __HAL_UNLOCK(m_hwHandle);
197 HAL_StatusTypeDef
error = HAL_I2C_Slave_Seq_Transmit_IT(m_hwHandle,
const_cast<uint8_t*
>(data),
static_cast<uint16_t
>(dataSize),
hardwareFrame(
frame()));
200 if (
error == HAL_ERROR)
205 else if (
error == HAL_BUSY)
210 else if (
error == HAL_TIMEOUT)
221 HAL_StatusTypeDef
error = HAL_I2C_EnableListen_IT(m_hwHandle);
224 if (
error == HAL_ERROR)
229 else if (
error == HAL_BUSY)
234 else if (
error == HAL_TIMEOUT)
246 HAL_StatusTypeDef
error = HAL_I2C_DisableListen_IT(m_hwHandle);
249 if (
error == HAL_ERROR)
254 else if (
error == HAL_BUSY)
259 else if (
error == HAL_TIMEOUT)
272 m_hwHandle->Init.OwnAddress1 =
address << 1;
280 return I2C_FIRST_FRAME;
284 return I2C_FIRST_AND_LAST_FRAME;
288 return I2C_NEXT_FRAME;
292 return I2C_LAST_FRAME;
@ FirstAndLast
start AND stop condition
@ Next
no start AND no stop condition
@ First
start condition, but no stop condition
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
void onError(Error thrown)
Is called if an error occurred by hardware read or write access. Will emit error signal.
bool isBusyReading() const override
Communication hardware is busy reading at the moment.
bool isBusyWriting() const override
Communication hardware is busy writing at the moment.
uint8_t address() const override
Returns the I2C slave device address.
CommunicationHardware::Frame frame() const
Returns the actual frame mode setting.
virtual void setFrequencyHardware(uint32_t hz)=0
Configures the frequency of the bus.
void setListening(bool listening)
Sets the listening flag.
void stopListeningHardware() override
@ StartListeningHardware_HalError
@ SystemIsrError_HalError
@ SystemIsrListen_EnableListenHalBusy
@ ReadHardware_HalTimeout
@ WriteHardware_HalTimeout
@ StartListeningHardware_HalTimeout
@ SystemIsrListen_EnableListenHalError
@ SystemIsrListen_EnableListenHalTimeout
@ StopListeningHardware_HalError
@ StartListeningHardware_HalBusy
void writeHardware(const uint8_t data[], size_t dataSize) override
Hardware will write data. After finished write operation, onDataWritten() function will be called.
void setFrequency(uint32_t hz) override
Sets the speed (I2C baud rate).
static void systemIsrAddressing(I2C_HandleTypeDef &hwHandle, uint8_t direction, uint16_t addrMatchCode)
I2C_HandleTypeDef * hardwareHandle()
Returns the hardware Handle.
void readHardware(uint8_t buffer[], size_t bufferSize) override
Hardware will read data. After finished read operation, onDataAvailable() function will be called.
void setAddressHardware(uint8_t address) override
Sets the address of the i2c hardware.
static void systemIsrWritten(I2C_HandleTypeDef &hwHandle)
System-wide isr for data transmission on an I2C-bus.
static void systemIsrError(I2C_HandleTypeDef &hwHandle)
virtual uint32_t hardwareFrame(CommunicationHardware::Frame frame) const
Converts the frame information from semf to STM.
static void systemIsrListen(I2C_HandleTypeDef &hwHandle)
System-wide isr for end of listening on an I2C-bus.
static void systemIsrRead(I2C_HandleTypeDef &hwHandle)
System-wide isr for data reception on an I2C-bus.
void stopWrite() override
Stm32I2cSlave(I2C_HandleTypeDef &hwHandle)
Constructor.
void startListeningHardware() override
#define SEMF_WARNING(...)
#define SEMF_SINGLETON_ERROR(object,...)