38#if defined(STM32F072xB) || defined(STM32F091xC)
39#include "stm32f0xx_ll_rcc.h"
40#include "stm32f0xx_ll_i2c.h"
44#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(5, 0x3, 0x3, 0x3, 0x9))
46#elif defined(STM32F103xB)
47#include "stm32f1xx_ll_rcc.h"
48#elif defined(STM32F303xC) || defined(STM32F303xE)
49#include "stm32f3xx_ll_rcc.h"
53#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(8, 0x3, 0x3, 0x3, 0x9))
55#elif defined(STM32L431xx) || defined(STM32L432xx)
56#include "stm32l4xx_ll_rcc.h"
57#include "stm32l4xx_ll_i2c.h"
61#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(9, 0x3, 0x3, 0x3, 0x9))
63#elif defined(STM32F767xx)
64#include "stm32f7xx_ll_rcc.h"
65#include "stm32f7xx_ll_i2c.h"
69#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(8, 9, 9, 9, 27))
71#elif defined(STM32G0B1xx)
72#include "stm32g0xx_ll_rcc.h"
73#include "stm32g0xx_ll_i2c.h"
77#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(7, 0x3, 0x3, 0x3, 0x9))
80#error Dont know what STM32 chip you have.
84static void i2c_reset(I2C_TypeDef *port)
86 switch ((
unsigned)port)
89 DIE(
"Unknown I2C port requested.");
92 LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_SYSCLK);
93 __HAL_RCC_I2C1_CLK_ENABLE();
94 __HAL_RCC_I2C1_FORCE_RESET();
95 __HAL_RCC_I2C1_RELEASE_RESET();
100 #ifdef LL_RCC_I2C2_CLKSOURCE_SYSCLK
101 LL_RCC_SetI2CClockSource(LL_RCC_I2C2_CLKSOURCE_SYSCLK);
103 __HAL_RCC_I2C2_CLK_ENABLE();
104 __HAL_RCC_I2C2_FORCE_RESET();
105 __HAL_RCC_I2C2_RELEASE_RESET();
110 #ifdef LL_RCC_I2C3_CLKSOURCE_SYSCLK
111 LL_RCC_SetI2CClockSource(LL_RCC_I2C3_CLKSOURCE_SYSCLK);
113 __HAL_RCC_I2C3_CLK_ENABLE();
114 __HAL_RCC_I2C3_FORCE_RESET();
115 __HAL_RCC_I2C3_RELEASE_RESET();
120 __HAL_RCC_I2C4_CONFIG(RCC_I2C4CLKSOURCE_SYSCLK);
121 __HAL_RCC_I2C4_CLK_ENABLE();
122 __HAL_RCC_I2C4_FORCE_RESET();
123 __HAL_RCC_I2C4_RELEASE_RESET();
128 __HAL_RCC_I2C5_CONFIG(RCC_I2C5CLKSOURCE_SYSCLK);
129 __HAL_RCC_I2C5_CLK_ENABLE();
130 __HAL_RCC_I2C5_FORCE_RESET();
131 __HAL_RCC_I2C5_RELEASE_RESET();
136 __HAL_RCC_I2C6_CONFIG(RCC_I2C6CLKSOURCE_SYSCLK);
137 __HAL_RCC_I2C6_CLK_ENABLE();
138 __HAL_RCC_I2C6_FORCE_RESET();
139 __HAL_RCC_I2C6_RELEASE_RESET();
144 __HAL_RCC_I2C7_CONFIG(RCC_I2C7CLKSOURCE_SYSCLK);
145 __HAL_RCC_I2C7_CLK_ENABLE();
146 __HAL_RCC_I2C7_FORCE_RESET();
147 __HAL_RCC_I2C7_RELEASE_RESET();
152 __HAL_RCC_I2C8_CONFIG(RCC_I2C8CLKSOURCE_SYSCLK);
153 __HAL_RCC_I2C8_CLK_ENABLE();
154 __HAL_RCC_I2C8_FORCE_RESET();
155 __HAL_RCC_I2C8_RELEASE_RESET();
168 uint32_t er_interrupt)
178 i2cHandle_.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
179 i2cHandle_.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
181 i2cHandle_.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
182 i2cHandle_.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
187 HAL_I2CEx_ConfigAnalogFilter(&
i2cHandle_, I2C_ANALOGFILTER_ENABLE);
194#ifdef configKERNEL_INTERRUPT_PRIORITY
195 SetInterruptPriority((IRQn_Type)ev_interrupt, configKERNEL_INTERRUPT_PRIORITY);
196 SetInterruptPriority((IRQn_Type)er_interrupt, configKERNEL_INTERRUPT_PRIORITY);
198 HAL_NVIC_EnableIRQ((IRQn_Type)ev_interrupt);
199 HAL_NVIC_EnableIRQ((IRQn_Type)er_interrupt);
204 int bytes = msg->len;
213 if (msg->flags & I2C_M_RD)
216 uint32_t xfer_options;
219 xfer_options = I2C_FIRST_AND_LAST_FRAME;
223 xfer_options = I2C_FIRST_FRAME;
226 if (HAL_I2C_Master_Seq_Receive_IT(&
i2cHandle_,
227 (uint16_t)msg->addr << 1, (uint8_t *)msg->buf, bytes,
228 xfer_options) != HAL_OK)
236 uint32_t xfer_options;
239 xfer_options = I2C_FIRST_AND_LAST_FRAME;
243 xfer_options = I2C_FIRST_FRAME;
245 if (HAL_I2C_Master_Seq_Transmit_IT(&
i2cHandle_,
246 (uint16_t)msg->addr << 1, (uint8_t *)msg->buf, bytes,
247 xfer_options) != HAL_OK)
258Stm32I2C *dev_from_handle(I2C_HandleTypeDef *hi2c)
260 return (
Stm32I2C *)hi2c->Init.Timing;
266 sem.post_from_isr(&woken);
267 os_isr_exit_yield_test(woken);
274 if (error & HAL_I2C_ERROR_ARLO)
278 else if (error & HAL_I2C_ERROR_AF)
282 else if (error & HAL_I2C_ERROR_TIMEOUT)
298void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
303void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
308void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
Private data for an I2C device.
void wait()
Wait on (decrement) a semaphore.
Specialization of I2C driver for STM32 devices.
I2C_HandleTypeDef i2cHandle_
Stm32 HAL device structure.
void error_from_isr()
Internal. This function is called from the error ISR callback.
OSSem sem
Semaphore to wakeup task level from ISR.
Stm32I2C()
Default constructor.
int error_
Pending transfer error field.
void complete_from_isr()
Internal. This function is called from the complete ISR callback.
int transfer(struct i2c_msg *msg, bool stop) override
Method to transmit/receive the data.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DIE(MSG)
Unconditionally terminates the current process with a message.