Debian Codegraph @

ports/renesas-ra/ra/ra_i2c.c

raw
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021,2022 Renesas Electronics Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include "hal_data.h"
28 #include "ra_config.h"
29 #include "ra_gpio.h"
30 #include "ra_icu.h"
31 #include "ra_int.h"
32 #include "ra_timer.h"
33 #include "ra_utils.h"
34 #include "ra_i2c.h"
35
36 #if !defined(RA_PRI_I2C)
37 #define RA_PRI_I2C (8)
38 #endif
39
40 #if defined(__GNUC__)
41 #pragma GCC diagnostic ignored "-Wunused-parameter"
42 #pragma GCC diagnostic ignored "-Wconversion"
43 #pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
44 #pragma GCC diagnostic ignored "-Wunused-variable"
45 #endif
46
47 extern volatile uint32_t uwTick;
48
49 static const ra_af_pin_t scl_pins[] = {
50 #if defined(RA4M1)
51
52 { AF_I2C, 0, P204 },
53 { AF_I2C, 0, P400 },
54 { AF_I2C, 0, P408 },
55 { AF_I2C, 1, P100 },
56 { AF_I2C, 1, P205 },
57
58 #elif defined(RA4W1)
59
60 { AF_I2C, 0, P204 },
61 { AF_I2C, 1, P100 },
62 { AF_I2C, 1, P205 },
63
64 #elif defined(RA6M1)
65
66 { AF_I2C, 0, P400 },
67 { AF_I2C, 0, P408 },
68 { AF_I2C, 1, P100 },
69 { AF_I2C, 1, P205 },
70
71 #elif defined(RA6M2) || defined(RA6M3)
72
73 { AF_I2C, 0, P204 },
74 { AF_I2C, 0, P400 },
75 { AF_I2C, 0, P408 },
76 { AF_I2C, 1, P100 },
77 { AF_I2C, 1, P205 },
78 { AF_I2C, 2, P512 },
79
80 #elif defined(RA6M5)
81
82 { AF_I2C, 0, P400 },
83 { AF_I2C, 0, P408 },
84 { AF_I2C, 1, P205 },
85 { AF_I2C, 1, P512 },
86 { AF_I2C, 2, P410 },
87 { AF_I2C, 2, P415 },
88
89 #else
90 #error "CMSIS MCU Series is not specified."
91 #endif
92 };
93 #define SCL_PINS_SIZE sizeof(scl_pins) / sizeof(ra_af_pin_t)
94
95 static const ra_af_pin_t sda_pins[] = {
96 #if defined(RA4M1)
97
98 { AF_I2C, 0, P401 },
99 { AF_I2C, 0, P407 },
100 { AF_I2C, 1, P101 },
101 { AF_I2C, 1, P206 },
102
103 #elif defined(RA4W1)
104
105 { AF_I2C, 0, P407 },
106 { AF_I2C, 1, P101 },
107 { AF_I2C, 1, P206 },
108
109 #elif defined(RA6M1)
110
111 { AF_I2C, 0, P401 },
112 { AF_I2C, 0, P407 },
113 { AF_I2C, 1, P101 },
114 { AF_I2C, 1, P206 },
115
116 #elif defined(RA6M2) || defined(RA6M3)
117
118 { AF_I2C, 0, P401 },
119 { AF_I2C, 0, P407 },
120 { AF_I2C, 1, P101 },
121 { AF_I2C, 1, P206 },
122 { AF_I2C, 2, P511 },
123
124 #elif defined(RA6M5)
125
126 { AF_I2C, 0, P401 },
127 { AF_I2C, 0, P407 },
128 { AF_I2C, 1, P206 },
129 { AF_I2C, 1, P511 },
130 { AF_I2C, 2, P409 },
131 { AF_I2C, 2, P414 },
132
133 #else
134 #error "CMSIS MCU Series is not specified."
135 #endif
136 };
137 #define SDA_PINS_SIZE sizeof(sda_pins) / sizeof(ra_af_pin_t)
138
139 static const uint8_t ra_i2c_ch_to_rxirq[] = {
140 #if defined(VECTOR_NUMBER_IIC0_RXI)
141 VECTOR_NUMBER_IIC0_RXI,
142 #else
143 VECTOR_NUMBER_NONE,
144 #endif
145 #if defined(VECTOR_NUMBER_IIC1_RXI)
146 VECTOR_NUMBER_IIC1_RXI,
147 #else
148 VECTOR_NUMBER_NONE,
149 #endif
150 #if defined(VECTOR_NUMBER_IIC2_RXI)
151 VECTOR_NUMBER_IIC2_RXI,
152 #else
153 VECTOR_NUMBER_NONE,
154 #endif
155 };
156 static const uint8_t ra_i2c_ch_to_txirq[] = {
157 #if defined(VECTOR_NUMBER_IIC0_TXI)
158 VECTOR_NUMBER_IIC0_TXI,
159 #else
160 VECTOR_NUMBER_NONE,
161 #endif
162 #if defined(VECTOR_NUMBER_IIC1_TXI)
163 VECTOR_NUMBER_IIC1_TXI,
164 #else
165 VECTOR_NUMBER_NONE,
166 #endif
167 #if defined(VECTOR_NUMBER_IIC2_TXI)
168 VECTOR_NUMBER_IIC2_TXI,
169 #else
170 VECTOR_NUMBER_NONE,
171 #endif
172 };
173 static const uint8_t ra_i2c_ch_to_teirq[] = {
174 #if defined(VECTOR_NUMBER_IIC0_TEI)
175 VECTOR_NUMBER_IIC0_TEI,
176 #else
177 VECTOR_NUMBER_NONE,
178 #endif
179 #if defined(VECTOR_NUMBER_IIC1_TEI)
180 VECTOR_NUMBER_IIC1_TEI,
181 #else
182 VECTOR_NUMBER_NONE,
183 #endif
184 #if defined(VECTOR_NUMBER_IIC2_TEI)
185 VECTOR_NUMBER_IIC2_TEI,
186 #else
187 VECTOR_NUMBER_NONE,
188 #endif
189 };
190 static const uint8_t ra_i2c_ch_to_erirq[] = {
191 #if defined(VECTOR_NUMBER_IIC0_ERI)
192 VECTOR_NUMBER_IIC0_ERI,
193 #else
194 VECTOR_NUMBER_NONE,
195 #endif
196 #if defined(VECTOR_NUMBER_IIC1_ERI)
197 VECTOR_NUMBER_IIC1_ERI,
198 #else
199 VECTOR_NUMBER_NONE,
200 #endif
201 #if defined(VECTOR_NUMBER_IIC2_ERI)
202 VECTOR_NUMBER_IIC2_ERI,
203 #else
204 VECTOR_NUMBER_NONE,
205 #endif
206 };
207
208 static xaction_t *current_xaction;
209 static xaction_unit_t *current_xaction_unit;
210 static bool last_stop;
211 static uint8_t pclk_div[8] = {
212 1, 2, 4, 8, 16, 32, 64, 128
213 };
214
215 static uint32_t R_IIC0_Type_to_ch(R_IIC0_Type *i2c_inst) {
216 if (i2c_inst == R_IIC2) {
217 return 2; /* channel 2 */
218 } else if (i2c_inst == R_IIC1) {
219 return 1; /* channel 1 */
220 } else {
221 return 0; /* channel 0 */
222 }
223 }
224
225 static R_IIC0_Type *ch_to_R_IIC0_Type(uint32_t ch) {
226 if (ch == 2) {
227 return R_IIC2;
228 } else if (ch == 1) {
229 return R_IIC1;
230 } else {
231 return R_IIC0;
232 }
233 }
234
235 bool ra_i2c_find_af_ch(uint32_t scl, uint32_t sda, uint8_t *ch) {
236 bool find = false;
237 uint8_t scl_ch;
238 uint8_t sda_ch;
239 find = ra_af_find_ch((ra_af_pin_t *)&scl_pins, SCL_PINS_SIZE, scl, &scl_ch);
240 if (find) {
241 find = ra_af_find_ch((ra_af_pin_t *)&sda_pins, SDA_PINS_SIZE, sda, &sda_ch);
242 if (find) {
243 find = (scl_ch == sda_ch);
244 if (find) {
245 *ch = scl_ch;
246 } else {
247 *ch = 0;
248 }
249 }
250 }
251 return find;
252 }
253
254 static void ra_i2c_module_start(R_IIC0_Type *i2c_inst) {
255 if (i2c_inst == R_IIC0) {
256 ra_mstpcrb_start(R_MSTP_MSTPCRB_MSTPB9_Msk);
257 } else if (i2c_inst == R_IIC1) {
258 ra_mstpcrb_start(R_MSTP_MSTPCRB_MSTPB8_Msk);
259 } else if (i2c_inst == R_IIC2) {
260 ra_mstpcrb_start(R_MSTP_MSTPCRB_MSTPB7_Msk);
261 }
262 }
263
264 static void ra_i2c_module_stop(R_IIC0_Type *i2c_inst) {
265 if (i2c_inst == R_IIC0) {
266 ra_mstpcrb_stop(R_MSTP_MSTPCRB_MSTPB9_Msk);
267 } else if (i2c_inst == R_IIC1) {
268 ra_mstpcrb_stop(R_MSTP_MSTPCRB_MSTPB8_Msk);
269 } else if (i2c_inst == R_IIC2) {
270 ra_mstpcrb_stop(R_MSTP_MSTPCRB_MSTPB7_Msk);
271 }
272 }
273
274 void ra_i2c_irq_enable(R_IIC0_Type *i2c_inst) {
275 uint32_t ch = R_IIC0_Type_to_ch(i2c_inst);
276 R_BSP_IrqEnable((IRQn_Type const)ra_i2c_ch_to_rxirq[ch]);
277 R_BSP_IrqEnable((IRQn_Type const)ra_i2c_ch_to_txirq[ch]);
278 R_BSP_IrqEnable((IRQn_Type const)ra_i2c_ch_to_teirq[ch]);
279 R_BSP_IrqEnable((IRQn_Type const)ra_i2c_ch_to_erirq[ch]);
280 }
281
282 void ra_i2c_irq_disable(R_IIC0_Type *i2c_inst) {
283 uint32_t ch = R_IIC0_Type_to_ch(i2c_inst);
284 R_BSP_IrqDisable((IRQn_Type const)ra_i2c_ch_to_rxirq[ch]);
285 R_BSP_IrqDisable((IRQn_Type const)ra_i2c_ch_to_txirq[ch]);
286 R_BSP_IrqDisable((IRQn_Type const)ra_i2c_ch_to_teirq[ch]);
287 R_BSP_IrqDisable((IRQn_Type const)ra_i2c_ch_to_erirq[ch]);
288 }
289
290 void ra_i2c_priority(R_IIC0_Type *i2c_inst, uint32_t ipl) {
291 uint32_t ch = R_IIC0_Type_to_ch(i2c_inst);
292 R_BSP_IrqCfg((IRQn_Type const)ra_i2c_ch_to_rxirq[ch], ipl, (void *)NULL);
293 R_BSP_IrqCfg((IRQn_Type const)ra_i2c_ch_to_txirq[ch], ipl, (void *)NULL);
294 R_BSP_IrqCfg((IRQn_Type const)ra_i2c_ch_to_teirq[ch], ipl, (void *)NULL);
295 R_BSP_IrqCfg((IRQn_Type const)ra_i2c_ch_to_erirq[ch], ipl, (void *)NULL);
296 }
297
298 void ra_i2c_clear_IR(R_IIC0_Type *i2c_inst) {
299 uint32_t ch = R_IIC0_Type_to_ch(i2c_inst);
300 R_BSP_IrqStatusClear((IRQn_Type const)ra_i2c_ch_to_rxirq[ch]);
301 R_BSP_IrqStatusClear((IRQn_Type const)ra_i2c_ch_to_txirq[ch]);
302 R_BSP_IrqStatusClear((IRQn_Type const)ra_i2c_ch_to_teirq[ch]);
303 R_BSP_IrqStatusClear((IRQn_Type const)ra_i2c_ch_to_erirq[ch]);
304 }
305
306 // ToDo: need to properly implement
307 static void ra_i2c_clock_calc(uint32_t baudrate, uint8_t *cks, uint8_t *brh, uint8_t *brl);
308 static void ra_i2c_clock_calc(uint32_t baudrate, uint8_t *cks, uint8_t *brh, uint8_t *brl) {
309 #if defined(RA4M1)
310 if (baudrate >= 400000) {
311 // assume clock is 400000Hz (PCLKB 32MHz)
312 *cks = 1;
313 *brh = 9;
314 *brl = 20;
315 } else {
316 // assume clock is 100000Hz (PCLKB 32MHz)
317 *cks = 3;
318 *brh = 15;
319 *brl = 18;
320 }
321 #elif defined(RA4W1)
322 if (baudrate >= 400000) {
323 // assume clock is 400000Hz (PCLKB 32MHz)
324 *cks = 1;
325 *brh = 9;
326 *brl = 20;
327 } else if (baudrate >= 100000) {
328 // assume clock is 100000Hz (PCLKB 32MHz)
329 *cks = 3;
330 *brh = 15;
331 *brl = 18;
332 } else {
333 // assume clock is 50000Hz (PCLKB 32MHz)
334 *cks = 4;
335 *brh = 15;
336 *brl = 18;
337 }
338 #elif defined(RA6M1)
339 // PCLKB 60MHz SCLE=0
340 if (baudrate >= 1000000) {
341 *cks = 0;
342 *brh = 15;
343 *brl = 29;
344 } else if (baudrate >= 400000) {
345 // assume clock is 400000Hz (PCLKB 32MHz)
346 *cks = 2;
347 *brh = 8;
348 *brl = 19;
349 } else {
350 // assume clock is 100000Hz (PCLKB 32MHz)
351 *cks = 4;
352 *brh = 14;
353 *brl = 17;
354 }
355 #elif defined(RA6M2) || defined(RA6M3)
356 // PCLKB 60MHz SCLE=0
357 if (baudrate >= RA_I2C_CLOCK_MAX) {
358 *cks = 0;
359 *brh = 15;
360 *brl = 29;
361 } else if (baudrate >= 400000) {
362 // assume clock is 400000Hz
363 *cks = 2;
364 *brh = 8;
365 *brl = 19;
366 } else if (baudrate >= 100000) {
367 // assume clock is 100000Hz
368 *cks = 4;
369 *brh = 14;
370 *brl = 17;
371 } else {
372 // assume clock is 50000Hz
373 *cks = 5;
374 *brh = 14;
375 *brl = 17;
376 }
377 #elif defined(RA6M5)
378 // PCLKB 50MHz SCLE=0
379 if (baudrate >= RA_I2C_CLOCK_MAX) {
380 *cks = 0;
381 *brh = 12;
382 *brl = 24;
383 } else if (baudrate >= 400000) {
384 // assume clock is 400000Hz
385 *cks = 2;
386 *brh = 7;
387 *brl = 15;
388 } else if (baudrate >= 100000) {
389 // assume clock is 100000Hz
390 *cks = 3;
391 *brh = 24;
392 *brl = 30;
393 } else {
394 // assume clock is 50000Hz
395 *cks = 4;
396 *brh = 24;
397 *brl = 30;
398 }
399 #else
400 #error "CMSIS MCU Series is not specified."
401 #endif
402 }
403
404 void ra_i2c_set_baudrate(R_IIC0_Type *i2c_inst, uint32_t baudrate) {
405 uint8_t cks;
406 uint8_t brh;
407 uint8_t brl;
408 ra_i2c_clock_calc(baudrate, &cks, &brh, &brl);
409 i2c_inst->ICMR1_b.CKS = cks;
410 i2c_inst->ICBRH_b.BRH = brh;
411 i2c_inst->ICBRL_b.BRL = brl;
412 }
413
414 void ra_i2c_init(R_IIC0_Type *i2c_inst, uint32_t scl, uint32_t sda, uint32_t baudrate) {
415 ra_i2c_module_start(i2c_inst);
416 ra_gpio_config(scl, GPIO_MODE_AF_OD, GPIO_NOPULL, GPIO_LOW_POWER, AF_I2C);
417 ra_gpio_config(sda, GPIO_MODE_AF_OD, GPIO_NOPULL, GPIO_LOW_POWER, AF_I2C);
418 ra_i2c_priority(i2c_inst, RA_PRI_I2C);
419 i2c_inst->ICCR1_b.ICE = 0; // I2C disable
420 i2c_inst->ICCR1_b.IICRST = 1; // I2C internal reset
421 i2c_inst->ICIER = 0x00; // I2C disable all interrupts
422 while (i2c_inst->ICIER != 0) {
423 ;
424 }
425 ra_i2c_clear_IR(i2c_inst); // clear IR
426 i2c_inst->ICCR1_b.ICE = 1; // I2C enable
427 ra_i2c_set_baudrate(i2c_inst, baudrate);
428 i2c_inst->ICSER = 0x00; // I2C reset bus status enable register
429 i2c_inst->ICMR3_b.ACKWP = 0x00; // I2C not allow to write ACKBT (transfer acknowledge bit)
430 i2c_inst->ICIER = 0xFF; // Enable all interrupts
431 i2c_inst->ICCR1_b.IICRST = 0; // I2C internal reset
432 ra_i2c_irq_enable(i2c_inst);
433 last_stop = true;
434 return;
435 }
436
437 void ra_i2c_deinit(R_IIC0_Type *i2c_inst) {
438 i2c_inst->ICIER = 0; // I2C interrupt disable
439 i2c_inst->ICCR1_b.ICE = 0; // I2C disable
440 ra_i2c_module_stop(i2c_inst);
441 return;
442 }
443
444 void ra_i2c_xaction_start(R_IIC0_Type *i2c_inst, xaction_t *action, bool repeated_start) {
445 uint32_t timeout;
446
447 if (last_stop == false) {
448 i2c_inst->ICSR2_b.START = 0;
449 i2c_inst->ICCR2_b.RS = 1;
450 return; /* We still keep I2C bus */
451 }
452 timeout = RA_I2C_TIMEOUT_BUS_BUSY;
453 while (i2c_inst->ICCR2_b.BBSY) {
454 if (timeout-- == 0) {
455 action->m_status = RA_I2C_STATUS_Stopped;
456 action->m_error = RA_I2C_ERROR_BUSY;
457 return;
458 }
459 }
460 i2c_inst->ICCR2_b.ST = 1; // I2C start condition
461 }
462
463 void ra_i2c_xaction_stop() {
464 last_stop = current_xaction->m_stop;
465 }
466
467 void ra_i2c_xunit_write_byte(R_IIC0_Type *i2c_inst, xaction_unit_t *unit) {
468 i2c_inst->ICDRT = unit->buf[unit->m_bytes_transferred];
469 ++unit->m_bytes_transferred;
470 --unit->m_bytes_transfer;
471 }
472
473 void ra_i2c_xunit_read_byte(R_IIC0_Type *i2c_inst, xaction_unit_t *unit) {
474 uint8_t data = i2c_inst->ICDRR;
475 unit->buf[unit->m_bytes_transferred] = data;
476 ++unit->m_bytes_transferred;
477 --unit->m_bytes_transfer;
478 }
479
480 void ra_i2c_xunit_init(xaction_unit_t *unit, uint8_t *buf, uint32_t size, bool fread, void *next) {
481 unit->m_bytes_transferred = 0;
482 unit->m_bytes_transfer = size;
483 unit->m_bytes_total = size;
484 unit->m_fread = fread;
485 unit->buf = buf;
486 unit->next = (void *)next;
487 }
488
489 void ra_i2c_xaction_init(xaction_t *action, xaction_unit_t *units, uint32_t size, uint32_t address, bool stop) {
490 action->units = units;
491 action->m_num_of_units = size;
492 action->m_current = 0;
493 action->m_address = (((address << 1) & 0xFE) | (units->m_fread ? 0x1 : 0x0));
494 action->m_status = RA_I2C_STATUS_Idle;
495 action->m_error = RA_I2C_ERROR_OK;
496 action->m_stop = stop;
497 }
498
499 static void ra_i2c_iceri_isr(R_IIC0_Type *i2c_inst) {
500 xaction_t *action = current_xaction;
501 if (i2c_inst->ICSR2_b.TMOF != 0) {
502 action->m_error = RA_I2C_ERROR_TMOF;
503 i2c_inst->ICSR2_b.TMOF = 0;
504 i2c_inst->ICCR2_b.SP = 1; // request stop condition
505 }
506 if (i2c_inst->ICSR2_b.AL != 0) {
507 action->m_error = RA_I2C_ERROR_AL;
508 i2c_inst->ICSR2_b.AL = 0;
509 i2c_inst->ICCR2_b.SP = 1; // request stop condition
510 }
511 // Check Start
512 if (i2c_inst->ICSR2_b.START != 0) {
513 if (action->m_status == RA_I2C_STATUS_Idle) {
514 action->m_status = RA_I2C_STATUS_Started;
515 }
516 i2c_inst->ICSR2_b.START = 0;
517 }
518 // Check Stop
519 if (i2c_inst->ICSR2_b.STOP != 0) {
520 action->m_status = RA_I2C_STATUS_Stopped;
521 i2c_inst->ICSR2_b.STOP = 0;
522 i2c_inst->ICSR2_b.NACKF = 0; // clear for next transaction
523 }
524 // Check NACK reception
525 if (i2c_inst->ICSR2_b.NACKF != 0) {
526 action->m_error = RA_I2C_ERROR_NACK;
527 i2c_inst->ICSR2_b.NACKF = 0;
528 i2c_inst->ICCR2_b.SP = 1; // request stop condition
529 }
530 }
531
532 static void ra_i2c_icrxi_isr(R_IIC0_Type *i2c_inst) {
533 xaction_unit_t *unit = current_xaction_unit;
534 xaction_t *action = current_xaction;
535 // 1 byte or 2 bytes
536 if (unit->m_bytes_total <= 2) {
537 if (action->m_status == RA_I2C_STATUS_AddrWriteCompleted) {
538 action->m_status = RA_I2C_STATUS_FirstReceiveCompleted;
539 i2c_inst->ICMR3_b.WAIT = 1;
540 // need dummy read processes for 1 byte and 2 bytes receive
541 if (unit->m_bytes_total == 2) {
542 (void)i2c_inst->ICDRR; // dummy read for 2 bytes receive
543 } else { // m_bytes_total == 1
544 i2c_inst->ICMR3_b.ACKWP = 0x01; // enable write ACKBT (transfer acknowledge bit)
545 i2c_inst->ICMR3_b.ACKBT = 1;
546 i2c_inst->ICMR3_b.ACKWP = 0x00; // disable write ACKBT (transfer acknowledge bit)
547 (void)i2c_inst->ICDRR; // dummy read for 1 byte receive
548 }
549 return;
550 }
551 if (unit->m_bytes_transfer == 2) { // last two data
552 i2c_inst->ICMR3_b.ACKWP = 0x01; // enable write ACKBT (transfer acknowledge bit)
553 i2c_inst->ICMR3_b.ACKBT = 1;
554 i2c_inst->ICMR3_b.ACKWP = 0x00; // disable write ACKBT (transfer acknowledge bit)
555 ra_i2c_xunit_read_byte(i2c_inst, unit);
556 } else { // last data
557 action->m_status = RA_I2C_STATUS_LastReceiveCompleted;
558 if (action->m_stop == true) {
559 i2c_inst->ICCR2_b.SP = 1; // request top condition
560 }
561 ra_i2c_xunit_read_byte(i2c_inst, unit);
562 }
563 return;
564 }
565 // 3 bytes or more
566 if (action->m_status == RA_I2C_STATUS_AddrWriteCompleted) {
567 (void)i2c_inst->ICDRR; // dummy read
568 action->m_status = RA_I2C_STATUS_FirstReceiveCompleted;
569 return;
570 }
571 if (unit->m_bytes_transfer > 2) {
572 if (unit->m_bytes_transfer == 3) {
573 i2c_inst->ICMR3_b.WAIT = 1;
574 }
575 ra_i2c_xunit_read_byte(i2c_inst, unit);
576 } else if (unit->m_bytes_transfer == 2) {
577 i2c_inst->ICMR3_b.ACKWP = 0x01; // enable write ACKBT (transfer acknowledge bit)
578 i2c_inst->ICMR3_b.ACKBT = 1;
579 i2c_inst->ICMR3_b.ACKWP = 0x00; // disable write ACKBT (transfer acknowledge bit)
580 ra_i2c_xunit_read_byte(i2c_inst, unit);
581 } else {
582 // last data
583 action->m_status = RA_I2C_STATUS_LastReceiveCompleted;
584 if (action->m_stop == true) {
585 i2c_inst->ICCR2_b.SP = 1; // request top condition
586 }
587 ra_i2c_xunit_read_byte(i2c_inst, unit);
588 }
589 }
590
591 static void ra_i2c_ictxi_isr(R_IIC0_Type *i2c_inst) {
592 xaction_t *action = current_xaction;
593 xaction_unit_t *unit = current_xaction_unit;
594 // When STIE is already checked. When TIE occurs before STIE
595 if (action->m_status == RA_I2C_STATUS_Started || action->m_status == RA_I2C_STATUS_Idle) {
596 i2c_inst->ICDRT = action->m_address; // I2C send slave address
597 action->m_status = RA_I2C_STATUS_AddrWriteCompleted;
598 return;
599 }
600 if (action->m_status == RA_I2C_STATUS_AddrWriteCompleted &&
601 unit->m_fread == false) {
602 if (unit->m_bytes_transfer != 0) {
603 ra_i2c_xunit_write_byte(i2c_inst, unit);
604 } else {
605 if (unit->next == (void *)NULL) {
606 action->m_status = RA_I2C_STATUS_DataWriteCompleted;
607 } else {
608 current_xaction_unit = (xaction_unit_t *)unit->next;
609 if (current_xaction_unit->m_bytes_transfer == 0) {
610 action->m_status = RA_I2C_STATUS_DataWriteCompleted;
611 }
612 }
613 }
614 return;
615 }
616 }
617
618 static void ra_i2c_ictei_isr(R_IIC0_Type *i2c_inst) {
619 xaction_t *action = current_xaction;
620 i2c_inst->ICSR2_b.TEND = 0;
621 action->m_current++;
622 if (action->m_current == action->m_num_of_units) {
623 // We wrote all data and received transfer end, so request stop condition
624 if (action->m_stop == true) {
625 action->m_status = RA_I2C_STATUS_DataSendCompleted;
626 i2c_inst->ICCR2_b.SP = 1;
627 } else {
628 action->m_status = RA_I2C_STATUS_Stopped; // set Stopped status instead STOP condition
629 }
630 } else {
631 ra_i2c_xaction_start(i2c_inst, action, true);
632 }
633 }
634
635 void iic_master_rxi_isr(void) {
636 IRQn_Type irq = R_FSP_CurrentIrqGet();
637 uint8_t ch = irq_to_ch[(uint32_t)irq];
638 ra_i2c_icrxi_isr(ch_to_R_IIC0_Type(ch));
639 R_BSP_IrqStatusClear(irq);
640 }
641
642 void iic_master_txi_isr(void) {
643 IRQn_Type irq = R_FSP_CurrentIrqGet();
644 uint8_t ch = irq_to_ch[(uint32_t)irq];
645 ra_i2c_ictxi_isr(ch_to_R_IIC0_Type(ch));
646 R_BSP_IrqStatusClear(irq);
647 }
648
649 void iic_master_tei_isr(void) {
650 IRQn_Type irq = R_FSP_CurrentIrqGet();
651 uint8_t ch = irq_to_ch[(uint32_t)irq];
652 ra_i2c_ictei_isr(ch_to_R_IIC0_Type(ch));
653 R_BSP_IrqStatusClear(irq);
654 }
655
656 void iic_master_eri_isr(void) {
657 IRQn_Type irq = R_FSP_CurrentIrqGet();
658 uint8_t ch = irq_to_ch[(uint32_t)irq];
659 ra_i2c_iceri_isr(ch_to_R_IIC0_Type(ch));
660 R_BSP_IrqStatusClear(irq);
661 }
662
663 bool ra_i2c_action_execute(R_IIC0_Type *i2c_inst, xaction_t *action, bool repeated_start, uint32_t timeout_ms) {
664 bool flag = false;
665 uint32_t start = uwTick;
666
667 current_xaction = action;
668 current_xaction_unit = action->units;
669
670 ra_i2c_xaction_start(i2c_inst, action, repeated_start);
671 while (true) {
672 if (action->m_status == RA_I2C_STATUS_Stopped) {
673 if (action->m_error == RA_I2C_ERROR_OK) {
674 flag = true;
675 }
676 break;
677 }
678 if (uwTick - start > timeout_ms) {
679 break;
680 }
681 }
682 ra_i2c_xaction_stop();
683 if (last_stop == true) {
684 mp_hal_delay_ms(3); // avoid device busy of next access.
685 }
686 current_xaction = (xaction_t *)NULL;
687 current_xaction_unit = (xaction_unit_t *)NULL;
688
689 return flag;
690 }