34 #include "mcu_periph/i2c_arch.h"
45 #ifndef I2C_THREAD_STACK_SIZE
46 #define I2C_THREAD_STACK_SIZE 512
55 #if USE_I2C1 || USE_I2C2 || USE_I2C3 || USE_I2C4
70 static const systime_t tmo = TIME_US2I(10000000 / PERIODIC_FREQUENCY);
99 memcpy(i->dma_buf, (
void *)t->
buf, (
size_t)(t->
len_w));
100 status = i2cMasterTransmitTimeout(
106 memcpy((
void *)t->
buf, i->dma_buf, (
size_t)(t->
len_r));
108 status = i2cMasterTransmitTimeout(
118 memcpy(i->dma_buf, (
void *)t->
buf, (
size_t)(t->
len_w));
119 status = i2cMasterReceiveTimeout(
124 memcpy((
void *)t->
buf, i->dma_buf, (
size_t)(t->
len_r));
126 status = i2cMasterReceiveTimeout(
153 i2cStart((I2CDriver *)p->
reg_addr, i->cfg);
159 i2cflags_t errors = i2cGetErrors((I2CDriver *)p->
reg_addr);
160 if (errors & I2C_BUS_ERROR) {
163 if (errors & I2C_ARBITRATION_LOST) {
166 if (errors & I2C_ACK_FAILURE) {
169 if (errors & I2C_OVERRUN) {
172 if (errors & I2C_PEC_ERROR) {
178 if (errors & I2C_SMB_ALERT) {
196 static struct i2c_init i2c1_init_s = {
199 .dma_buf = i2c1_dma_buf
202 static struct i2c_init i2c1_init_s = {
210 static __attribute__((noreturn)) void thd_i2c1(
void *arg);
216 void i2c1_hw_init(
void)
222 i2cStart(&I2CD1, &i2cfg1);
223 i2c1.reg_addr = &I2CD1;
224 i2c1.errors = &i2c1_errors;
225 i2c1.init_struct = &i2c1_init_s;
227 chThdCreateStatic(wa_thd_i2c1,
sizeof(wa_thd_i2c1),
228 NORMALPRIO + 1, thd_i2c1, NULL);
235 static void thd_i2c1(
void *arg)
238 chRegSetThreadName(
"i2c1");
241 handle_i2c_thd(&i2c1);
254 static struct i2c_init i2c2_init_s = {
257 .dma_buf = i2c2_dma_buf
260 static struct i2c_init i2c2_init_s = {
268 static __attribute__((noreturn)) void thd_i2c2(
void *arg);
274 void i2c2_hw_init(
void)
280 i2cStart(&I2CD2, &i2cfg2);
281 i2c2.reg_addr = &I2CD2;
282 i2c2.errors = &i2c2_errors;
283 i2c2.init_struct = &i2c2_init_s;
285 chThdCreateStatic(wa_thd_i2c2,
sizeof(wa_thd_i2c2),
286 NORMALPRIO + 1, thd_i2c2, NULL);
293 static void thd_i2c2(
void *arg)
296 chRegSetThreadName(
"i2c2");
299 handle_i2c_thd(&i2c2);
312 static struct i2c_init i2c3_init_s = {
315 .dma_buf = i2c3_dma_buf
318 static struct i2c_init i2c3_init_s = {
326 static __attribute__((noreturn)) void thd_i2c3(
void *arg);
332 void i2c3_hw_init(
void)
338 i2cStart(&I2CD3, &i2cfg3);
339 i2c3.reg_addr = &I2CD3;
340 i2c3.init_struct = NULL;
341 i2c3.errors = &i2c3_errors;
342 i2c3.init_struct = &i2c3_init_s;
344 chThdCreateStatic(wa_thd_i2c3,
sizeof(wa_thd_i2c3),
345 NORMALPRIO + 1, thd_i2c3, NULL);
352 static void thd_i2c3(
void *arg)
355 chRegSetThreadName(
"i2c3");
358 handle_i2c_thd(&i2c3);
371 static struct i2c_init i2c4_init_s = {
374 .dma_buf = i2c4_dma_buf
377 static struct i2c_init i2c4_init_s = {
385 static __attribute__((noreturn)) void thd_i2c4(
void *arg);
391 void i2c4_hw_init(
void)
397 i2cStart(&I2CD4, &i2cfg4);
398 i2c4.reg_addr = &I2CD4;
399 i2c4.init_struct = NULL;
400 i2c4.errors = &i2c4_errors;
401 i2c4.init_struct = &i2c4_init_s;
403 chThdCreateStatic(wa_thd_i2c4,
sizeof(wa_thd_i2c4),
404 NORMALPRIO + 1, thd_i2c4, NULL);
411 static void thd_i2c4(
void *arg)
414 chRegSetThreadName(
"i2c4");
417 handle_i2c_thd(&i2c4);
457 #if USE_I2C1 || USE_I2C2 || USE_I2C3 || USE_I2C4
Specific RAM section for DMA usage on F7.
volatile uint16_t arb_lost_cnt
#define I2C_TRANSACTION_QUEUE_LEN
I2C transaction queue length.
volatile uint16_t miss_start_stop_cnt
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
uint16_t len_r
Number of bytes to read/receive.
transaction successfully finished by I2C driver
uint8_t trans_extract_idx
void i2c_init(struct i2c_periph *p)
Initialize I2C peripheral.
volatile uint16_t queue_full_cnt
struct i2c_errors * errors
static SEMAPHORE_DECL(spi1_sem, 0)
Configure SPI peripherals.
uint8_t len_w
Number of bytes to write/transmit.
struct i2c_transaction * trans[I2C_TRANSACTION_QUEUE_LEN]
volatile uint16_t over_under_cnt
static THD_WORKING_AREA(wa_thd_spi1, SPI_THREAD_STACK_SIZE)
I2C transaction structure.
enum I2CTransactionStatus status
Transaction status.
static void i2c_chibios_setbitrate(struct i2c_periph *p, int bitrate)
i2c_setbitrate() function
uint8_t slave_addr
Slave address.
#define IN_DMA_SECTION(var)
static bool i2c_chibios_submit(struct i2c_periph *p, struct i2c_transaction *t)
i2c_submit() function
#define I2C_BUF_LEN
I2C buffer length.
#define I2C_THREAD_STACK_SIZE
volatile uint16_t smbus_alert_cnt
void i2c_event(void)
i2c_event() function
#define I2C1_CLOCK_SPEED
I2C defines.
volatile uint16_t pec_recep_cnt
transaction is pending in queue
volatile uint16_t ack_fail_cnt
volatile uint16_t timeout_tlow_cnt
static bool i2c_chibios_idle(struct i2c_periph *p)
i2c_idle() function
Architecture independent I2C (Inter-Integrated Circuit Bus) API.