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);
84 if (
p->trans_insert_idx ==
p->trans_extract_idx) {
99 memcpy(i->dma_buf, (
void *)t->
buf, (
size_t)(t->
len_w));
100 status = i2cMasterTransmitTimeout(
101 (I2CDriver *)
p->reg_addr,
106 memcpy((
void *)t->
buf, i->dma_buf, (
size_t)(t->
len_r));
108 status = i2cMasterTransmitTimeout(
109 (I2CDriver *)
p->reg_addr,
118 memcpy(i->dma_buf, (
void *)t->
buf, (
size_t)(t->
len_w));
119 status = i2cMasterReceiveTimeout(
120 (I2CDriver *)
p->reg_addr,
124 memcpy((
void *)t->
buf, i->dma_buf, (
size_t)(t->
len_r));
126 status = i2cMasterReceiveTimeout(
127 (I2CDriver *)
p->reg_addr,
136 p->trans_extract_idx++;
138 p->trans_extract_idx = 0;
153 i2cStart((I2CDriver *)
p->reg_addr, i->cfg);
159 i2cflags_t errors = i2cGetErrors((I2CDriver *)
p->reg_addr);
160 if (errors & I2C_BUS_ERROR) {
161 p->errors->miss_start_stop_cnt++;
163 if (errors & I2C_ARBITRATION_LOST) {
164 p->errors->arb_lost_cnt++;
166 if (errors & I2C_ACK_FAILURE) {
167 p->errors->ack_fail_cnt++;
169 if (errors & I2C_OVERRUN) {
170 p->errors->over_under_cnt++;
172 if (errors & I2C_PEC_ERROR) {
173 p->errors->pec_recep_cnt++;
176 p->errors->timeout_tlow_cnt++;
178 if (errors & I2C_SMB_ALERT) {
179 p->errors->smbus_alert_cnt++;
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
461 temp =
p->trans_insert_idx + 1;
463 if (temp ==
p->trans_extract_idx) {
465 p->errors->queue_full_cnt++;
474 p->trans[
p->trans_insert_idx] = t;
475 p->trans_insert_idx = temp;
478 chSemSignal(((
struct i2c_init *)
p->init_struct)->sem);