Debian Codegraph @

ports/renesas-ra/ra/ra_sci.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 "hal_data.h"
26 #include "ra_config.h"
27 #include "ra_gpio.h"
28 #include "ra_int.h"
29 #include "ra_utils.h"
30 #include "ra_sci.h"
31 #include "r_sci_uart.h"
32
33 #if !defined(RA_PRI_UART)
34 #define RA_PRI_UART (1)
35 #endif
36
37 #if defined(__GNUC__)
38 #pragma GCC diagnostic ignored "-Wunused-parameter"
39 // #pragma GCC diagnostic ignored "-Wconversion"
40 // #pragma GCC diagnostic ignored "-Wshift-negative-value"
41 // #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
42 // #pragma GCC diagnostic ignored "-Wsequence-point"
43 // #pragma GCC diagnostic ignored "-Wunused-function"
44 #endif
45
46 enum
47 {
48 #if defined(VECTOR_NUMBER_SCI0_RXI)
49 SCI0_IDX,
50 #endif
51 #if defined(VECTOR_NUMBER_SCI1_RXI)
52 SCI1_IDX,
53 #endif
54 #if defined(VECTOR_NUMBER_SCI2_RXI)
55 SCI2_IDX,
56 #endif
57 #if defined(VECTOR_NUMBER_SCI3_RXI)
58 SCI3_IDX,
59 #endif
60 #if defined(VECTOR_NUMBER_SCI4_RXI)
61 SCI4_IDX,
62 #endif
63 #if defined(VECTOR_NUMBER_SCI5_RXI)
64 SCI5_IDX,
65 #endif
66 #if defined(VECTOR_NUMBER_SCI6_RXI)
67 SCI6_IDX,
68 #endif
69 #if defined(VECTOR_NUMBER_SCI7_RXI)
70 SCI7_IDX,
71 #endif
72 #if defined(VECTOR_NUMBER_SCI8_RXI)
73 SCI8_IDX,
74 #endif
75 #if defined(VECTOR_NUMBER_SCI9_RXI)
76 SCI9_IDX,
77 #endif
78 SCI_IDX_MAX,
79 };
80
81 static const IRQn_Type idx_to_rxirq[] = {
82 #if defined(VECTOR_NUMBER_SCI0_RXI)
83 VECTOR_NUMBER_SCI0_RXI,
84 #endif
85 #if defined(VECTOR_NUMBER_SCI1_RXI)
86 VECTOR_NUMBER_SCI1_RXI,
87 #endif
88 #if defined(VECTOR_NUMBER_SCI2_RXI)
89 VECTOR_NUMBER_SCI2_RXI,
90 #endif
91 #if defined(VECTOR_NUMBER_SCI3_RXI)
92 VECTOR_NUMBER_SCI3_RXI,
93 #endif
94 #if defined(VECTOR_NUMBER_SCI4_RXI)
95 VECTOR_NUMBER_SCI4_RXI,
96 #endif
97 #if defined(VECTOR_NUMBER_SCI5_RXI)
98 VECTOR_NUMBER_SCI5_RXI,
99 #endif
100 #if defined(VECTOR_NUMBER_SCI6_RXI)
101 VECTOR_NUMBER_SCI6_RXI,
102 #endif
103 #if defined(VECTOR_NUMBER_SCI7_RXI)
104 VECTOR_NUMBER_SCI7_RXI,
105 #endif
106 #if defined(VECTOR_NUMBER_SCI8_RXI)
107 VECTOR_NUMBER_SCI8_RXI,
108 #endif
109 #if defined(VECTOR_NUMBER_SCI9_RXI)
110 VECTOR_NUMBER_SCI9_RXI,
111 #endif
112 };
113
114 static const IRQn_Type idx_to_txirq[] = {
115 #if defined(VECTOR_NUMBER_SCI0_TXI)
116 VECTOR_NUMBER_SCI0_TXI,
117 #endif
118 #if defined(VECTOR_NUMBER_SCI1_TXI)
119 VECTOR_NUMBER_SCI1_TXI,
120 #endif
121 #if defined(VECTOR_NUMBER_SCI2_TXI)
122 VECTOR_NUMBER_SCI2_TXI,
123 #endif
124 #if defined(VECTOR_NUMBER_SCI3_TXI)
125 VECTOR_NUMBER_SCI3_TXI,
126 #endif
127 #if defined(VECTOR_NUMBER_SCI4_TXI)
128 VECTOR_NUMBER_SCI4_TXI,
129 #endif
130 #if defined(VECTOR_NUMBER_SCI5_TXI)
131 VECTOR_NUMBER_SCI5_TXI,
132 #endif
133 #if defined(VECTOR_NUMBER_SCI6_TXI)
134 VECTOR_NUMBER_SCI6_TXI,
135 #endif
136 #if defined(VECTOR_NUMBER_SCI7_TXI)
137 VECTOR_NUMBER_SCI7_TXI,
138 #endif
139 #if defined(VECTOR_NUMBER_SCI8_TXI)
140 VECTOR_NUMBER_SCI8_TXI,
141 #endif
142 #if defined(VECTOR_NUMBER_SCI9_TXI)
143 VECTOR_NUMBER_SCI9_TXI,
144 #endif
145 };
146
147 static const IRQn_Type idx_to_teirq[] = {
148 #if defined(VECTOR_NUMBER_SCI0_TEI)
149 VECTOR_NUMBER_SCI0_TEI,
150 #endif
151 #if defined(VECTOR_NUMBER_SCI1_TEI)
152 VECTOR_NUMBER_SCI1_TEI,
153 #endif
154 #if defined(VECTOR_NUMBER_SCI2_TEI)
155 VECTOR_NUMBER_SCI2_TEI,
156 #endif
157 #if defined(VECTOR_NUMBER_SCI3_TEI)
158 VECTOR_NUMBER_SCI3_TEI,
159 #endif
160 #if defined(VECTOR_NUMBER_SCI4_TEI)
161 VECTOR_NUMBER_SCI4_TEI,
162 #endif
163 #if defined(VECTOR_NUMBER_SCI5_TEI)
164 VECTOR_NUMBER_SCI5_TEI,
165 #endif
166 #if defined(VECTOR_NUMBER_SCI6_TEI)
167 VECTOR_NUMBER_SCI6_TEI,
168 #endif
169 #if defined(VECTOR_NUMBER_SCI7_TEI)
170 VECTOR_NUMBER_SCI7_TEI,
171 #endif
172 #if defined(VECTOR_NUMBER_SCI8_TEI)
173 VECTOR_NUMBER_SCI8_TEI,
174 #endif
175 #if defined(VECTOR_NUMBER_SCI9_TEI)
176 VECTOR_NUMBER_SCI9_TEI,
177 #endif
178 };
179
180 static const IRQn_Type idx_to_erirq[] = {
181 #if defined(VECTOR_NUMBER_SCI0_ERI)
182 VECTOR_NUMBER_SCI0_ERI,
183 #endif
184 #if defined(VECTOR_NUMBER_SCI1_ERI)
185 VECTOR_NUMBER_SCI1_ERI,
186 #endif
187 #if defined(VECTOR_NUMBER_SCI2_ERI)
188 VECTOR_NUMBER_SCI2_ERI,
189 #endif
190 #if defined(VECTOR_NUMBER_SCI3_ERI)
191 VECTOR_NUMBER_SCI3_ERI,
192 #endif
193 #if defined(VECTOR_NUMBER_SCI4_ERI)
194 VECTOR_NUMBER_SCI4_ERI,
195 #endif
196 #if defined(VECTOR_NUMBER_SCI5_ERI)
197 VECTOR_NUMBER_SCI5_ERI,
198 #endif
199 #if defined(VECTOR_NUMBER_SCI6_ERI)
200 VECTOR_NUMBER_SCI6_ERI,
201 #endif
202 #if defined(VECTOR_NUMBER_SCI7_ERI)
203 VECTOR_NUMBER_SCI7_ERI,
204 #endif
205 #if defined(VECTOR_NUMBER_SCI8_ERI)
206 VECTOR_NUMBER_SCI8_ERI,
207 #endif
208 #if defined(VECTOR_NUMBER_SCI9_ERI)
209 VECTOR_NUMBER_SCI9_ERI,
210 #endif
211 };
212
213 static uint32_t ch_to_idx[SCI_CH_MAX] = {
214 #if defined(VECTOR_NUMBER_SCI0_RXI)
215 SCI0_IDX,
216 #else
217 0,
218 #endif
219 #if defined(VECTOR_NUMBER_SCI1_RXI)
220 SCI1_IDX,
221 #else
222 0,
223 #endif
224 #if defined(VECTOR_NUMBER_SCI2_RXI)
225 SCI2_IDX,
226 #else
227 0,
228 #endif
229 #if defined(VECTOR_NUMBER_SCI3_RXI)
230 SCI3_IDX,
231 #else
232 0,
233 #endif
234 #if defined(VECTOR_NUMBER_SCI4_RXI)
235 SCI4_IDX,
236 #else
237 0,
238 #endif
239 #if defined(VECTOR_NUMBER_SCI5_RXI)
240 SCI5_IDX,
241 #else
242 0,
243 #endif
244 #if defined(VECTOR_NUMBER_SCI6_RXI)
245 SCI6_IDX,
246 #else
247 0,
248 #endif
249 #if defined(VECTOR_NUMBER_SCI7_RXI)
250 SCI7_IDX,
251 #else
252 0,
253 #endif
254 #if defined(VECTOR_NUMBER_SCI8_RXI)
255 SCI8_IDX,
256 #else
257 0,
258 #endif
259 #if defined(VECTOR_NUMBER_SCI9_RXI)
260 SCI9_IDX,
261 #else
262 0,
263 #endif
264 };
265
266 static R_SCI0_Type *sci_regs[] = {
267 #if defined(VECTOR_NUMBER_SCI0_RXI)
268 R_SCI0,
269 #endif
270 #if defined(VECTOR_NUMBER_SCI1_RXI)
271 R_SCI1,
272 #endif
273 #if defined(VECTOR_NUMBER_SCI2_RXI)
274 R_SCI2,
275 #endif
276 #if defined(VECTOR_NUMBER_SCI3_RXI)
277 R_SCI3,
278 #endif
279 #if defined(VECTOR_NUMBER_SCI4_RXI)
280 R_SCI4,
281 #endif
282 #if defined(VECTOR_NUMBER_SCI5_RXI)
283 R_SCI5,
284 #endif
285 #if defined(VECTOR_NUMBER_SCI6_RXI)
286 R_SCI6,
287 #endif
288 #if defined(VECTOR_NUMBER_SCI7_RXI)
289 R_SCI7,
290 #endif
291 #if defined(VECTOR_NUMBER_SCI8_RXI)
292 R_SCI8,
293 #endif
294 #if defined(VECTOR_NUMBER_SCI9_RXI)
295 R_SCI9,
296 #endif
297 };
298
299 static uint32_t sci_module_mask[] = {
300 #if defined(VECTOR_NUMBER_SCI0_RXI)
301 R_MSTP_MSTPCRB_MSTPB31_Msk,
302 #endif
303 #if defined(VECTOR_NUMBER_SCI1_RXI)
304 R_MSTP_MSTPCRB_MSTPB30_Msk,
305 #endif
306 #if defined(VECTOR_NUMBER_SCI2_RXI)
307 R_MSTP_MSTPCRB_MSTPB29_Msk,
308 #endif
309 #if defined(VECTOR_NUMBER_SCI3_RXI)
310 R_MSTP_MSTPCRB_MSTPB28_Msk,
311 #endif
312 #if defined(VECTOR_NUMBER_SCI4_RXI)
313 R_MSTP_MSTPCRB_MSTPB27_Msk,
314 #endif
315 #if defined(VECTOR_NUMBER_SCI5_RXI)
316 R_MSTP_MSTPCRB_MSTPB26_Msk,
317 #endif
318 #if defined(VECTOR_NUMBER_SCI6_RXI)
319 R_MSTP_MSTPCRB_MSTPB25_Msk,
320 #endif
321 #if defined(VECTOR_NUMBER_SCI7_RXI)
322 R_MSTP_MSTPCRB_MSTPB24_Msk,
323 #endif
324 #if defined(VECTOR_NUMBER_SCI8_RXI)
325 R_MSTP_MSTPCRB_MSTPB23_Msk,
326 #endif
327 #if defined(VECTOR_NUMBER_SCI9_RXI)
328 R_MSTP_MSTPCRB_MSTPB22_Msk,
329 #endif
330 };
331
332 static const ra_af_pin_t ra_sci_tx_pins[] = {
333 #if defined(RA4M1)
334
335 { AF_SCI1, 0, P101 },
336 { AF_SCI1, 0, P205 },
337 { AF_SCI1, 0, P411 },
338
339 { AF_SCI2, 1, P213 },
340 { AF_SCI2, 1, P401 },
341 { AF_SCI2, 1, P501 },
342
343 { AF_SCI2, 2, P102 },
344 { AF_SCI1, 2, P112 },
345 { AF_SCI1, 2, P302 },
346
347 { AF_SCI2, 9, P109 },
348 { AF_SCI2, 9, P203 },
349 { AF_SCI2, 9, P409 },
350 { AF_SCI2, 9, P602 },
351
352 #elif defined(RA4W1)
353 { AF_SCI1, 0, P101 },
354
355 { AF_SCI2, 1, P213 },
356
357 { AF_SCI1, 4, P205 },
358
359 { AF_SCI2, 9, P109 },
360
361 #elif defined(RA6M1)
362
363 { AF_SCI1, 0, P101 },
364 { AF_SCI1, 0, P411 },
365
366 { AF_SCI2, 1, P213 },
367
368 { AF_SCI1, 2, P112 },
369 { AF_SCI1, 2, P302 },
370
371 { AF_SCI2, 3, P409 },
372
373 { AF_SCI1, 4, P205 },
374
375 { AF_SCI1, 8, P105 },
376
377 { AF_SCI2, 9, P109 },
378 { AF_SCI2, 9, P602 },
379
380 #elif defined(RA6M2)
381
382 { AF_SCI1, 0, P101 },
383 { AF_SCI1, 0, P411 },
384
385 { AF_SCI1, 1, P213 },
386 { AF_SCI2, 1, P709 },
387
388 { AF_SCI1, 2, P112 },
389 { AF_SCI1, 2, P302 },
390
391 { AF_SCI2, 3, P310 },
392 { AF_SCI2, 3, P409 },
393
394 { AF_SCI1, 4, P205 },
395 { AF_SCI1, 4, P512 },
396
397 { AF_SCI2, 5, P501 },
398
399 { AF_SCI1, 6, P305 },
400 { AF_SCI1, 6, P506 },
401
402 { AF_SCI2, 7, P401 },
403 { AF_SCI2, 7, P613 },
404
405 { AF_SCI1, 8, P105 },
406
407 { AF_SCI2, 9, P109 },
408 { AF_SCI2, 9, P203 },
409 { AF_SCI2, 9, P602 },
410
411 #elif defined(RA6M3)
412
413 { AF_SCI1, 0, P101 },
414 { AF_SCI1, 0, P411 },
415
416 { AF_SCI2, 1, P213 },
417 { AF_SCI2, 1, P709 },
418
419 { AF_SCI1, 2, P112 },
420 { AF_SCI1, 2, P302 },
421
422 { AF_SCI2, 3, P310 },
423 { AF_SCI2, 3, P409 },
424 { AF_SCI2, 3, P707 },
425
426 { AF_SCI1, 4, P205 },
427 { AF_SCI1, 4, P512 },
428 { AF_SCI1, 4, P900 },
429
430 { AF_SCI2, 5, P501 },
431 { AF_SCI2, 5, P805 },
432
433 { AF_SCI1, 6, P305 },
434 { AF_SCI1, 6, P506 },
435
436 { AF_SCI2, 7, P401 },
437 { AF_SCI2, 7, P613 },
438
439 { AF_SCI1, 8, P105 },
440 { AF_SCI1, 8, PA00 },
441
442 { AF_SCI2, 9, P109 },
443 { AF_SCI2, 9, P203 },
444 { AF_SCI2, 9, P602 },
445
446 #elif defined(RA6M5)
447
448 { AF_SCI1, 0, P101 },
449 { AF_SCI1, 0, P411 },
450
451 { AF_SCI2, 1, P213 },
452 { AF_SCI2, 1, P709 },
453
454 { AF_SCI1, 2, P112 },
455 { AF_SCI1, 2, P302 },
456
457 { AF_SCI2, 3, P310 },
458 { AF_SCI2, 3, P409 },
459 { AF_SCI2, 3, P707 },
460
461 { AF_SCI1, 4, P205 },
462 { AF_SCI1, 4, P207 },
463 { AF_SCI1, 4, P512 },
464 { AF_SCI1, 4, P900 },
465
466 { AF_SCI2, 5, P501 },
467 { AF_SCI2, 5, P805 },
468
469 { AF_SCI1, 6, P305 },
470 { AF_SCI1, 6, P506 },
471
472 { AF_SCI2, 7, P401 },
473 { AF_SCI2, 7, P613 },
474
475 { AF_SCI1, 8, P105 },
476 { AF_SCI1, 8, PA00 },
477
478 { AF_SCI2, 9, P109 },
479 { AF_SCI2, 9, P203 },
480 { AF_SCI2, 9, P602 },
481
482 #else
483 #error "CMSIS MCU Series is not specified."
484 #endif
485 };
486 #define SCI_TX_PINS_SIZE sizeof(ra_sci_tx_pins) / sizeof(ra_af_pin_t)
487
488 static const ra_af_pin_t ra_sci_rx_pins[] = {
489 #if defined(RA4M1)
490
491 { AF_SCI1, 0, P100 },
492 { AF_SCI1, 0, P104 },
493 { AF_SCI1, 0, P206 },
494 { AF_SCI1, 0, P410 },
495
496 { AF_SCI2, 1, P212 },
497 { AF_SCI2, 1, P402 },
498 { AF_SCI2, 1, P502 },
499 { AF_SCI2, 1, P708 },
500
501 { AF_SCI1, 2, P301 },
502
503 { AF_SCI2, 9, P110 },
504 { AF_SCI2, 9, P202 },
505 { AF_SCI2, 9, P408 },
506 { AF_SCI2, 9, P601 },
507
508 #elif defined(RA4W1)
509 { AF_SCI1, 0, P100 },
510 { AF_SCI1, 0, P104 },
511
512 { AF_SCI2, 1, P212 },
513 { AF_SCI2, 1, P402 },
514
515 { AF_SCI1, 4, P206 },
516
517 { AF_SCI2, 9, P110 },
518
519 #elif defined(RA6M1)
520
521 { AF_SCI1, 0, P100 },
522 { AF_SCI1, 0, P410 },
523
524 { AF_SCI2, 1, P212 },
525 { AF_SCI2, 1, P708 },
526
527 { AF_SCI1, 2, P113 },
528 { AF_SCI1, 2, P301 },
529
530 { AF_SCI2, 3, P408 },
531
532 { AF_SCI1, 4, P206 },
533
534 { AF_SCI1, 8, P104 },
535
536 { AF_SCI2, 9, P110 },
537 { AF_SCI2, 9, P601 },
538
539 #elif defined(RA6M2)
540
541 { AF_SCI1, 0, P100 },
542 { AF_SCI1, 0, P410 },
543
544 { AF_SCI1, 1, P212 },
545 { AF_SCI2, 1, P708 },
546
547 { AF_SCI1, 2, P113 },
548 { AF_SCI1, 2, P301 },
549
550 { AF_SCI2, 3, P309 },
551 { AF_SCI2, 3, P408 },
552
553 { AF_SCI1, 4, P206 },
554 { AF_SCI1, 4, P511 },
555
556 { AF_SCI2, 5, P502 },
557
558 { AF_SCI1, 6, P304 },
559 { AF_SCI1, 6, P505 },
560
561 { AF_SCI2, 7, P402 },
562 { AF_SCI2, 7, P614 },
563
564 { AF_SCI1, 8, P104 },
565
566 { AF_SCI2, 9, P110 },
567 { AF_SCI2, 9, P202 },
568 { AF_SCI2, 9, P601 },
569
570 #elif defined(RA6M3) || defined(RA6M5)
571
572 { AF_SCI1, 0, P100 },
573 { AF_SCI1, 0, P410 },
574
575 { AF_SCI2, 1, P212 },
576 { AF_SCI2, 1, P708 },
577
578 { AF_SCI1, 2, P113 },
579 { AF_SCI1, 2, P301 },
580
581 { AF_SCI2, 3, P309 },
582 { AF_SCI2, 3, P408 },
583 { AF_SCI2, 3, P706 },
584
585 { AF_SCI1, 4, P206 },
586 { AF_SCI1, 4, P315 },
587 { AF_SCI1, 4, P511 },
588
589 { AF_SCI2, 5, P502 },
590 { AF_SCI2, 5, P513 },
591
592 { AF_SCI1, 6, P304 },
593 { AF_SCI1, 6, P505 },
594
595 { AF_SCI2, 7, P402 },
596 { AF_SCI2, 7, P614 },
597
598 { AF_SCI1, 8, P104 },
599 { AF_SCI1, 8, P607 },
600
601 { AF_SCI2, 9, P110 },
602 { AF_SCI2, 9, P202 },
603 { AF_SCI2, 9, P601 },
604
605 #else
606 #error "CMSIS MCU Series is not specified."
607 #endif
608 };
609 #define SCI_RX_PINS_SIZE sizeof(ra_sci_rx_pins) / sizeof(ra_af_pin_t)
610
611 static const ra_af_pin_t ra_sci_cts_pins[] = {
612 #if defined(RA4M1)
613
614 { AF_SCI1, 0, P103 },
615 { AF_SCI1, 0, P401 },
616 { AF_SCI1, 0, P407 },
617 { AF_SCI1, 0, P413 },
618
619 { AF_SCI2, 1, P101 },
620 { AF_SCI2, 1, P403 },
621 { AF_SCI1, 1, P408 },
622 { AF_SCI2, 1, P504 },
623
624 { AF_SCI1, 2, P110 },
625 { AF_SCI1, 2, P203 },
626
627 { AF_SCI2, 9, P108 },
628 { AF_SCI2, 9, P205 },
629 { AF_SCI2, 9, P301 },
630 { AF_SCI2, 9, P603 },
631
632 #elif defined(RA4W1)
633 { AF_SCI1, 0, P103 },
634
635 { AF_SCI2, 1, P101 },
636
637 { AF_SCI1, 4, P407 },
638
639 { AF_SCI2, 9, P108 },
640 { AF_SCI2, 9, P205 },
641
642 #elif defined(RA6M1)
643
644 { AF_SCI1, 0, P103 },
645 { AF_SCI1, 0, P413 },
646
647 { AF_SCI2, 1, P101 },
648
649 { AF_SCI1, 2, P110 },
650
651 { AF_SCI2, 3, P411 },
652
653 { AF_SCI1, 4, P401 },
654 { AF_SCI1, 4, P407 },
655
656 { AF_SCI1, 8, P107 },
657
658 { AF_SCI2, 9, P108 },
659 { AF_SCI2, 9, P301 },
660
661 #elif defined(RA6M2)
662
663 { AF_SCI1, 0, P103 },
664 { AF_SCI1, 0, P413 },
665
666 { AF_SCI2, 1, P101 },
667 { AF_SCI2, 1, P711 },
668
669 { AF_SCI1, 2, P110 },
670 { AF_SCI1, 2, P203 },
671
672 { AF_SCI2, 3, P312 },
673 { AF_SCI2, 3, P411 },
674
675 { AF_SCI1, 4, P401 },
676 { AF_SCI1, 4, P407 },
677
678 { AF_SCI2, 5, P504 },
679
680 { AF_SCI1, 6, P307 },
681 { AF_SCI1, 6, P503 },
682
683 { AF_SCI2, 7, P403 },
684 { AF_SCI2, 7, P611 },
685
686 { AF_SCI1, 8, P107 },
687
688 { AF_SCI2, 9, P108 },
689 { AF_SCI2, 9, P205 },
690 { AF_SCI2, 9, P301 },
691 { AF_SCI2, 9, P603 },
692
693 #elif defined(RA6M3)
694
695 { AF_SCI1, 0, P103 },
696 { AF_SCI1, 0, P413 },
697
698 { AF_SCI2, 1, P101 },
699 { AF_SCI2, 1, P711 },
700
701 { AF_SCI1, 2, P110 },
702 { AF_SCI1, 2, P203 },
703
704 { AF_SCI2, 3, P312 },
705 { AF_SCI2, 3, P411 },
706 { AF_SCI2, 3, PB01 },
707
708 { AF_SCI1, 4, P401 },
709 { AF_SCI1, 4, P407 },
710
711 { AF_SCI2, 5, P504 },
712 { AF_SCI2, 5, P507 },
713
714 { AF_SCI1, 6, P307 },
715 { AF_SCI1, 6, P503 },
716
717 { AF_SCI2, 7, P403 },
718 { AF_SCI2, 7, P611 },
719
720 { AF_SCI1, 8, P107 },
721 { AF_SCI1, 8, P606 },
722
723 { AF_SCI2, 9, P108 },
724 { AF_SCI2, 9, P205 },
725 { AF_SCI2, 9, P301 },
726 { AF_SCI2, 9, P603 },
727
728 #elif defined(RA6M5)
729
730 { AF_SCI1, 0, P103 },
731 { AF_SCI1, 0, P413 },
732 { AF_SCI1, 0, P414 }, /* CTS only */
733 { AF_SCI1, 0, P800 }, /* CTS only */
734
735 { AF_SCI2, 1, P101 },
736 { AF_SCI2, 1, P711 },
737
738 { AF_SCI1, 2, P110 },
739 { AF_SCI1, 2, P203 },
740
741 { AF_SCI2, 3, P308 }, /* CTS only */
742 { AF_SCI2, 3, P312 },
743 { AF_SCI2, 3, P411 },
744 { AF_SCI2, 3, P412 }, /* CTS only */
745 { AF_SCI2, 3, P705 }, /* CTS only */
746
747 { AF_SCI1, 4, P401 },
748 { AF_SCI1, 4, P402 }, /* CTS only */
749 { AF_SCI1, 4, P407 },
750 { AF_SCI1, 4, P408 }, /* CTS only */
751
752 { AF_SCI2, 5, P500 }, /* CTS only */
753 { AF_SCI2, 5, P504 },
754 { AF_SCI2, 5, P508 },
755
756 { AF_SCI1, 6, P307 },
757 { AF_SCI1, 6, P308 }, /* CTS only */
758 { AF_SCI1, 6, P502 }, /* CTS only */
759 { AF_SCI1, 6, P503 },
760
761 { AF_SCI2, 7, P403 },
762 { AF_SCI2, 7, P404 }, /* CTS only */
763 { AF_SCI2, 7, P610 }, /* CTS only */
764 { AF_SCI2, 7, P611 },
765
766 { AF_SCI1, 8, P107 },
767 { AF_SCI1, 8, P605 }, /* CTS only */
768 { AF_SCI1, 8, P606 },
769 { AF_SCI1, 8, P801 }, /* CTS only */
770
771 { AF_SCI2, 9, P108 },
772 { AF_SCI2, 9, P114 }, /* CTS only */
773 { AF_SCI2, 9, P205 },
774 { AF_SCI2, 9, P206 }, /* CTS only */
775 { AF_SCI2, 9, P301 },
776 { AF_SCI2, 9, P303 }, /* CTS only */
777 { AF_SCI2, 9, P603 },
778 { AF_SCI2, 9, P604 }, /* CTS only */
779
780 #else
781 #error "CMSIS MCU Series is not specified."
782 #endif
783 };
784 #define SCI_CTS_PINS_SIZE sizeof(ra_sci_cts_pins) / sizeof(ra_af_pin_t)
785
786 typedef struct _sci_fifo {
787 volatile uint32_t tail, head, len, busy;
788 uint8_t *bufp;
789 uint32_t size;
790 } sci_fifo;
791
792 static uint32_t ra_sci_init_flag[] = {
793 #if defined(VECTOR_NUMBER_SCI0_RXI)
794 0,
795 #endif
796 #if defined(VECTOR_NUMBER_SCI1_RXI)
797 0,
798 #endif
799 #if defined(VECTOR_NUMBER_SCI2_RXI)
800 0,
801 #endif
802 #if defined(VECTOR_NUMBER_SCI3_RXI)
803 0,
804 #endif
805 #if defined(VECTOR_NUMBER_SCI4_RXI)
806 0,
807 #endif
808 #if defined(VECTOR_NUMBER_SCI5_RXI)
809 0,
810 #endif
811 #if defined(VECTOR_NUMBER_SCI6_RXI)
812 0,
813 #endif
814 #if defined(VECTOR_NUMBER_SCI7_RXI)
815 0,
816 #endif
817 #if defined(VECTOR_NUMBER_SCI8_RXI)
818 0,
819 #endif
820 #if defined(VECTOR_NUMBER_SCI9_RXI)
821 0,
822 #endif
823 };
824 static SCI_CB sci_cb[] = {
825 #if defined(VECTOR_NUMBER_SCI0_RXI)
826 (SCI_CB)0,
827 #endif
828 #if defined(VECTOR_NUMBER_SCI1_RXI)
829 (SCI_CB)0,
830 #endif
831 #if defined(VECTOR_NUMBER_SCI2_RXI)
832 (SCI_CB)0,
833 #endif
834 #if defined(VECTOR_NUMBER_SCI3_RXI)
835 (SCI_CB)0,
836 #endif
837 #if defined(VECTOR_NUMBER_SCI4_RXI)
838 (SCI_CB)0,
839 #endif
840 #if defined(VECTOR_NUMBER_SCI5_RXI)
841 (SCI_CB)0,
842 #endif
843 #if defined(VECTOR_NUMBER_SCI6_RXI)
844 (SCI_CB)0,
845 #endif
846 #if defined(VECTOR_NUMBER_SCI7_RXI)
847 (SCI_CB)0,
848 #endif
849 #if defined(VECTOR_NUMBER_SCI8_RXI)
850 (SCI_CB)0,
851 #endif
852 #if defined(VECTOR_NUMBER_SCI9_RXI)
853 (SCI_CB)0,
854 #endif
855 };
856 static uint8_t ch_9bit[] = {
857 #if defined(VECTOR_NUMBER_SCI0_RXI)
858 0,
859 #endif
860 #if defined(VECTOR_NUMBER_SCI1_RXI)
861 0,
862 #endif
863 #if defined(VECTOR_NUMBER_SCI2_RXI)
864 0,
865 #endif
866 #if defined(VECTOR_NUMBER_SCI3_RXI)
867 0,
868 #endif
869 #if defined(VECTOR_NUMBER_SCI4_RXI)
870 0,
871 #endif
872 #if defined(VECTOR_NUMBER_SCI5_RXI)
873 0,
874 #endif
875 #if defined(VECTOR_NUMBER_SCI6_RXI)
876 0,
877 #endif
878 #if defined(VECTOR_NUMBER_SCI7_RXI)
879 0,
880 #endif
881 #if defined(VECTOR_NUMBER_SCI8_RXI)
882 0,
883 #endif
884 #if defined(VECTOR_NUMBER_SCI9_RXI)
885 0,
886 #endif
887 };
888 static uint8_t tx_buf[SCI_IDX_MAX][SCI_TX_BUF_SIZE];
889 static uint8_t rx_buf[SCI_IDX_MAX][SCI_RX_BUF_SIZE];
890 static volatile sci_fifo tx_fifo[SCI_IDX_MAX];
891 static volatile sci_fifo rx_fifo[SCI_IDX_MAX];
892 static uint32_t m_cts_pin[] = {
893 #if defined(VECTOR_NUMBER_SCI0_RXI)
894 PIN_END,
895 #endif
896 #if defined(VECTOR_NUMBER_SCI1_RXI)
897 PIN_END,
898 #endif
899 #if defined(VECTOR_NUMBER_SCI2_RXI)
900 PIN_END,
901 #endif
902 #if defined(VECTOR_NUMBER_SCI3_RXI)
903 PIN_END,
904 #endif
905 #if defined(VECTOR_NUMBER_SCI4_RXI)
906 PIN_END,
907 #endif
908 #if defined(VECTOR_NUMBER_SCI5_RXI)
909 PIN_END,
910 #endif
911 #if defined(VECTOR_NUMBER_SCI6_RXI)
912 PIN_END,
913 #endif
914 #if defined(VECTOR_NUMBER_SCI7_RXI)
915 PIN_END,
916 #endif
917 #if defined(VECTOR_NUMBER_SCI8_RXI)
918 PIN_END,
919 #endif
920 #if defined(VECTOR_NUMBER_SCI9_RXI)
921 PIN_END,
922 #endif
923 };
924 static uint32_t m_rts_pin[] = {
925 #if defined(VECTOR_NUMBER_SCI0_RXI)
926 PIN_END,
927 #endif
928 #if defined(VECTOR_NUMBER_SCI1_RXI)
929 PIN_END,
930 #endif
931 #if defined(VECTOR_NUMBER_SCI2_RXI)
932 PIN_END,
933 #endif
934 #if defined(VECTOR_NUMBER_SCI3_RXI)
935 PIN_END,
936 #endif
937 #if defined(VECTOR_NUMBER_SCI4_RXI)
938 PIN_END,
939 #endif
940 #if defined(VECTOR_NUMBER_SCI5_RXI)
941 PIN_END,
942 #endif
943 #if defined(VECTOR_NUMBER_SCI6_RXI)
944 PIN_END,
945 #endif
946 #if defined(VECTOR_NUMBER_SCI7_RXI)
947 PIN_END,
948 #endif
949 #if defined(VECTOR_NUMBER_SCI8_RXI)
950 PIN_END,
951 #endif
952 #if defined(VECTOR_NUMBER_SCI9_RXI)
953 PIN_END,
954 #endif
955 };
956
957 static void delay_us(volatile unsigned int us) {
958 us *= 48;
959 while (us-- > 0) {
960 ;
961 }
962 }
963
964 bool ra_af_find_ch_af(ra_af_pin_t *af_pin, uint32_t size, uint32_t pin, uint32_t *ch, uint32_t *af) {
965 bool find = false;
966 uint32_t i;
967 for (i = 0; i < size; i++) {
968 if (af_pin->pin == pin) {
969 find = true;
970 *ch = af_pin->ch;
971 *af = af_pin->af;
972 break;
973 }
974 af_pin++;
975 }
976 return find;
977 }
978
979 static void ra_sci_tx_set_pin(uint32_t pin) {
980 bool find = false;
981 uint32_t ch;
982 uint32_t af;
983 find = ra_af_find_ch_af((ra_af_pin_t *)&ra_sci_tx_pins, SCI_TX_PINS_SIZE, pin, &ch, &af);
984 if (find) {
985 ra_gpio_config(pin, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_LOW_POWER, af);
986 }
987 }
988
989 static void ra_sci_rx_set_pin(uint32_t pin) {
990 bool find = false;
991 uint32_t ch;
992 uint32_t af;
993 find = ra_af_find_ch_af((ra_af_pin_t *)&ra_sci_rx_pins, SCI_RX_PINS_SIZE, pin, &ch, &af);
994 if (find) {
995 ra_gpio_config(pin, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_LOW_POWER, af);
996 }
997 }
998
999 static void ra_sci_cts_set_pin(uint32_t pin) {
1000 bool find = false;
1001 uint32_t ch;
1002 uint32_t af;
1003 find = ra_af_find_ch_af((ra_af_pin_t *)&ra_sci_cts_pins, SCI_CTS_PINS_SIZE, pin, &ch, &af);
1004 if (find) {
1005 ra_gpio_config(pin, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_LOW_POWER, af);
1006 }
1007 }
1008
1009 static void ra_sci_module_start(uint32_t ch) {
1010 ra_mstpcrb_start(sci_module_mask[ch_to_idx[ch]]);
1011 }
1012
1013 static void ra_sci_module_stop(uint32_t ch) {
1014 ra_mstpcrb_stop(sci_module_mask[ch_to_idx[ch]]);
1015 }
1016
1017 void ra_sci_rx_set_callback(int ch, SCI_CB cb) {
1018 sci_cb[ch_to_idx[ch]] = cb;
1019 }
1020
1021 static void ra_sci_irq_disable(uint32_t ch) {
1022 uint32_t idx = ch_to_idx[ch];
1023 R_BSP_IrqDisable(idx_to_rxirq[idx]);
1024 R_BSP_IrqDisable(idx_to_txirq[idx]);
1025 R_BSP_IrqDisable(idx_to_teirq[idx]);
1026 R_BSP_IrqDisable(idx_to_erirq[idx]);
1027 R_BSP_IrqStatusClear(idx_to_rxirq[idx]);
1028 R_BSP_IrqStatusClear(idx_to_txirq[idx]);
1029 R_BSP_IrqStatusClear(idx_to_teirq[idx]);
1030 R_BSP_IrqStatusClear(idx_to_erirq[idx]);
1031 }
1032
1033 static void ra_sci_irq_enable(uint32_t ch) {
1034 uint32_t idx = ch_to_idx[ch];
1035 R_BSP_IrqEnable(idx_to_rxirq[idx]);
1036 R_BSP_IrqEnable(idx_to_txirq[idx]);
1037 R_BSP_IrqEnable(idx_to_teirq[idx]);
1038 R_BSP_IrqEnable(idx_to_erirq[idx]);
1039 }
1040
1041 void ra_sci_rxirq_disable(uint32_t ch) {
1042 uint32_t idx = ch_to_idx[ch];
1043 R_BSP_IrqDisable(idx_to_rxirq[idx]);
1044 }
1045
1046 void ra_sci_rxirq_enable(uint32_t ch) {
1047 uint32_t idx = ch_to_idx[ch];
1048 R_BSP_IrqEnable(idx_to_rxirq[idx]);
1049 }
1050
1051 bool ra_sci_is_rxirq_enable(uint32_t ch) {
1052 uint32_t idx = ch_to_idx[ch];
1053 uint32_t _irq = (uint32_t)idx_to_rxirq[idx];
1054 return NVIC->ISER[(_irq >> 5UL)] == (uint32_t)(1UL << (_irq & 0x1FUL));
1055 }
1056
1057 static void ra_sci_irq_priority(uint32_t ch, uint32_t ipl) {
1058 uint32_t idx = ch_to_idx[ch];
1059 R_BSP_IrqCfg(idx_to_rxirq[idx], ipl, (void *)NULL);
1060 R_BSP_IrqCfg(idx_to_txirq[idx], ipl, (void *)NULL);
1061 R_BSP_IrqCfg(idx_to_teirq[idx], ipl, (void *)NULL);
1062 R_BSP_IrqCfg(idx_to_erirq[idx], ipl, (void *)NULL);
1063 }
1064
1065 static void ra_sci_isr_rx(uint32_t ch) {
1066 IRQn_Type irq = R_FSP_CurrentIrqGet();
1067 uint32_t idx = ch_to_idx[ch];
1068 uint16_t d;
1069 if (ch_9bit[idx]) {
1070 d = (uint16_t)sci_regs[idx]->RDRHL;
1071 } else {
1072 d = (uint16_t)sci_regs[idx]->RDR;
1073 }
1074 if (sci_cb[idx]) {
1075 if ((*sci_cb[idx])(ch, (int)d)) {
1076 // goto ra_sci_isr_rx_exit;
1077 }
1078 }
1079 uint32_t size = rx_fifo[idx].size;
1080 sci_fifo *rxfifo = (sci_fifo *)&rx_fifo[idx];
1081 if (rxfifo->len < size) {
1082 uint32_t i = rxfifo->head;
1083 if (ch_9bit[idx]) {
1084 *(uint16_t *)(rxfifo->bufp + i) = (uint16_t)d;
1085 i += 2;
1086 rxfifo->len += 2;
1087 } else {
1088 *(rxfifo->bufp + i) = (uint8_t)d;
1089 i++;
1090 rxfifo->len++;
1091 }
1092 rxfifo->head = i % size;
1093 if (m_rts_pin[idx] != PIN_END) {
1094 if (rxfifo->len > (size - RA_SCI_FLOW_START_NUM)) {
1095 ra_gpio_write(m_rts_pin[idx], 1);
1096 }
1097 }
1098 }
1099 // ra_sci_isr_rx_exit:
1100 R_BSP_IrqStatusClear(irq);
1101 }
1102
1103 static void ra_sci_isr_er(uint32_t ch) {
1104 IRQn_Type irq = R_FSP_CurrentIrqGet();
1105 R_BSP_IrqStatusClear(irq);
1106 uint32_t idx = ch_to_idx[ch];
1107 R_SCI0_Type *sci_reg = sci_regs[idx];
1108 sci_reg->RDR;
1109 while (0 != (sci_reg->SSR & 0x38)) {
1110 sci_reg->RDR;
1111 sci_reg->SSR = (uint8_t)((sci_reg->SSR & ~0x38) | 0xc0);
1112 if (0 != (sci_reg->SSR & 0x38)) {
1113 __asm__ __volatile__ ("nop");
1114 }
1115 }
1116 }
1117
1118 static void ra_sci_isr_tx(uint32_t ch) {
1119 IRQn_Type irq = R_FSP_CurrentIrqGet();
1120 uint32_t idx = ch_to_idx[ch];
1121 uint32_t size = tx_fifo[idx].size;
1122 sci_fifo *txfifo = (sci_fifo *)&tx_fifo[idx];
1123 if (txfifo->len != 0) {
1124 uint32_t i = txfifo->tail;
1125 if (ch_9bit[idx]) {
1126 sci_regs[idx]->TDRHL = *(uint16_t *)(txfifo->bufp + i);
1127 i += 2;
1128 txfifo->len -= 2;
1129 } else {
1130 sci_regs[idx]->TDR = (uint8_t)*(txfifo->bufp + i);
1131 i++;
1132 txfifo->len--;
1133 }
1134 txfifo->tail = i % size;
1135 } else {
1136 /* tx_fifo[idx].len == 0 */
1137 /* after transfer completed */
1138 uint8_t scr = sci_regs[idx]->SCR;
1139 scr &= (uint8_t) ~0x80; /* TIE disable */
1140 scr |= (uint8_t)0x04; /* TEIE enable */
1141 sci_regs[idx]->SCR = scr;
1142 }
1143 R_BSP_IrqStatusClear(irq);
1144 }
1145
1146 void ra_sci_isr_te(uint32_t ch) {
1147 IRQn_Type irq = R_FSP_CurrentIrqGet();
1148 uint32_t idx = ch_to_idx[ch];
1149 tx_fifo[idx].busy = 0;
1150 sci_regs[idx]->SCR &= (uint8_t) ~0x84; /* TIE and TEIE disable */
1151 R_BSP_IrqStatusClear(irq);
1152 }
1153
1154 int ra_sci_rx_ch(uint32_t ch) {
1155 uint16_t c;
1156 uint32_t idx = ch_to_idx[ch];
1157 uint32_t size = rx_fifo[idx].size;
1158 sci_fifo *rxfifo = (sci_fifo *)&rx_fifo[idx];
1159 if (rxfifo->len) {
1160 uint32_t state = ra_disable_irq();
1161 uint32_t i = rxfifo->tail;
1162 if (ch_9bit[idx]) {
1163 c = *(uint16_t *)(rxfifo->bufp + i);
1164 i += 2;
1165 rxfifo->len -= 2;
1166 } else {
1167 c = (uint16_t)*(rxfifo->bufp + i);
1168 i++;
1169 rxfifo->len--;
1170 }
1171 rxfifo->tail = i % size;
1172 if (m_rts_pin[idx] != PIN_END) {
1173 if (rxfifo->len <= (size - RA_SCI_FLOW_START_NUM)) {
1174 ra_gpio_write(m_rts_pin[idx], 0);
1175 }
1176 }
1177 ra_enable_irq(state);
1178 } else {
1179 c = 0;
1180 }
1181 return (int)c;
1182 }
1183
1184 int ra_sci_rx_any(uint32_t ch) {
1185 uint32_t idx = ch_to_idx[ch];
1186 return (int)(rx_fifo[idx].len != 0);
1187 }
1188
1189 void ra_sci_tx_ch(uint32_t ch, int c) {
1190 uint32_t idx = ch_to_idx[ch];
1191 uint32_t size = tx_fifo[idx].size;
1192 sci_fifo *txfifo = (sci_fifo *)&tx_fifo[idx];
1193 while (tx_fifo[idx].len == size) {
1194 }
1195 uint32_t state = ra_disable_irq();
1196 uint32_t i = tx_fifo[idx].head;
1197 if (ch_9bit[idx]) {
1198 *(uint16_t *)(txfifo->bufp + i) = (uint16_t)c;
1199 i += 2;
1200 txfifo->len += 2;
1201 } else {
1202 *(txfifo->bufp + i) = (uint8_t)c;
1203 i++;
1204 txfifo->len++;
1205 }
1206 txfifo->head = i % size;
1207 if (!txfifo->busy) {
1208 txfifo->busy = 1;
1209 uint8_t scr = sci_regs[idx]->SCR;
1210 if ((scr & 0xa0) != 0) {
1211 sci_regs[idx]->SCR &= ~0xa0;
1212 }
1213 sci_regs[idx]->SCR |= 0xa0; /* TIE- and TE-interrupt enable */
1214 }
1215 ra_enable_irq(state);
1216 }
1217
1218 int ra_sci_tx_wait(uint32_t ch) {
1219 uint32_t idx = ch_to_idx[ch];
1220 return (int)(tx_fifo[idx].len != (tx_fifo[idx].size - 1));
1221 }
1222
1223 int ra_sci_tx_busy(uint32_t ch) {
1224 uint32_t idx = ch_to_idx[ch];
1225 return (int)(tx_fifo[idx].busy);
1226 }
1227
1228 int ra_sci_tx_bufsize(uint32_t ch) {
1229 uint32_t idx = ch_to_idx[ch];
1230 return (int)(tx_fifo[idx].size - 1);
1231 }
1232
1233 void ra_sci_tx_break(uint32_t ch) {
1234 uint32_t idx = ch_to_idx[ch];
1235 R_SCI0_Type *sci_reg = sci_regs[idx];
1236 uint8_t scr = sci_reg->SCR;
1237 uint8_t smr = sci_reg->SMR;
1238 sci_reg->SCR = 0;
1239 while (sci_reg->SCR != 0) {
1240 ;
1241 }
1242 sci_reg->SMR_b.STOP = 1;
1243 sci_reg->SCR = scr;
1244 sci_reg->TDR = 0;
1245 while (sci_reg->SSR_b.TDRE == 0) {
1246 ;
1247 }
1248 sci_reg->SMR = smr;
1249 return;
1250 }
1251
1252 void ra_sci_tx_str(uint32_t ch, uint8_t *p) {
1253 int c;
1254 uint32_t idx = ch_to_idx[ch];
1255 if (ch_9bit[idx]) {
1256 uint16_t *q = (uint16_t *)p;
1257 while ((c = *q++) != 0) {
1258 ra_sci_tx_ch(ch, (int)c);
1259 }
1260 } else {
1261 while ((c = (int)*p++) != 0) {
1262 ra_sci_tx_ch(ch, (int)c);
1263 }
1264 }
1265 }
1266
1267 static void ra_sci_fifo_set(sci_fifo *fifo, uint8_t *bufp, uint32_t size) {
1268 fifo->head = 0;
1269 fifo->tail = 0;
1270 fifo->len = 0;
1271 fifo->busy = 0;
1272 fifo->bufp = bufp;
1273 fifo->size = size;
1274 }
1275
1276 void ra_sci_txfifo_set(uint32_t ch, uint8_t *bufp, uint32_t size) {
1277 uint32_t idx = ch_to_idx[ch];
1278 sci_fifo *fifo = (sci_fifo *)&tx_fifo[idx];
1279 ra_sci_fifo_set(fifo, bufp, size);
1280 }
1281
1282 void ra_sci_rxfifo_set(uint32_t ch, uint8_t *bufp, uint32_t size) {
1283 uint32_t idx = ch_to_idx[ch];
1284 sci_fifo *fifo = (sci_fifo *)&rx_fifo[idx];
1285 ra_sci_fifo_set(fifo, bufp, size);
1286 }
1287
1288 static void ra_sci_fifo_init(uint32_t ch) {
1289 uint32_t idx = ch_to_idx[ch];
1290 ra_sci_txfifo_set(ch, (uint8_t *)&tx_buf[idx][0], SCI_TX_BUF_SIZE);
1291 ra_sci_rxfifo_set(ch, (uint8_t *)&rx_buf[idx][0], SCI_RX_BUF_SIZE);
1292 }
1293
1294 void ra_sci_set_baud(uint32_t ch, uint32_t baud) {
1295 uint32_t idx = ch_to_idx[ch];
1296 R_SCI0_Type *sci_reg = sci_regs[idx];
1297 baud_setting_t baud_setting;
1298
1299 // baudrate_modulation=1(enabled), baudrate_max_err=5%(general value for FSP example).
1300 if (R_SCI_UART_BaudCalculate(baud, 1, 5000, &baud_setting) == FSP_SUCCESS) {
1301 // Disable transmitter and receiver.
1302 uint8_t scr = sci_reg->SCR;
1303 sci_reg->SCR = scr & ~(0xF0);
1304
1305 // Change baudrate
1306 sci_reg->BRR = baud_setting.brr;
1307 // Update CKS(b1:b0)
1308 sci_reg->SMR_b.CKS = (uint8_t)(0x03U & baud_setting.cks);
1309 sci_reg->MDDR = baud_setting.mddr;
1310 // Update BGDM(b6)/ABCS(b4)/ABCSE(b3)/BRME(b2) bits
1311 sci_reg->SEMR = (uint8_t)((sci_reg->SEMR & ~(0x5cU)) | (baud_setting.semr_baudrate_bits & 0x5cU));
1312
1313 // Restore SCR
1314 sci_reg->SCR = scr;
1315 }
1316 }
1317
1318 /*
1319 * bits: 7, 8, 9
1320 * parity: none:0, odd:1, even:2
1321 */
1322 void ra_sci_init_with_flow(uint32_t ch, uint32_t tx_pin, uint32_t rx_pin, uint32_t baud, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow, uint32_t cts_pin, uint32_t rts_pin) {
1323 uint8_t smr = 0;
1324 uint8_t scmr = (uint8_t)0xf2;
1325 uint32_t idx = ch_to_idx[ch];
1326 R_SCI0_Type *sci_reg = sci_regs[idx];
1327 if (ra_sci_init_flag[idx] == 0) {
1328 ra_sci_fifo_init(ch);
1329 sci_cb[idx] = 0;
1330 ra_sci_init_flag[idx]++;
1331 } else {
1332 ra_sci_irq_disable(ch);
1333 ra_sci_module_stop(ch);
1334 }
1335 ra_sci_module_start(ch);
1336 ra_sci_tx_set_pin(tx_pin);
1337 ra_sci_rx_set_pin(rx_pin);
1338 if (flow) {
1339 if (cts_pin != (uint32_t)PIN_END) {
1340 m_cts_pin[idx] = cts_pin;
1341 ra_sci_cts_set_pin(cts_pin);
1342 }
1343 if (rts_pin != (uint32_t)PIN_END) {
1344 m_rts_pin[idx] = rts_pin;
1345 ra_gpio_config(rts_pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_LOW_POWER, 0);
1346 ra_gpio_write(rts_pin, 0);
1347 }
1348 }
1349 ra_sci_irq_disable(ch);
1350 ra_sci_irq_priority(ch, RA_PRI_UART);
1351 uint32_t state = ra_disable_irq();
1352 sci_reg->SCR = 0;
1353 while (sci_reg->SCR != 0) {
1354 ;
1355 }
1356 if (bits == 7) {
1357 smr |= (uint8_t)0x40;
1358 } else {
1359 smr &= (uint8_t) ~0x40;
1360 }
1361 if (parity != 0) {
1362 smr |= (uint8_t)0x20;
1363 } else {
1364 smr &= (uint8_t) ~0x20;
1365 }
1366 if (parity == 1) {
1367 smr |= (uint8_t)0x10;
1368 } else {
1369 smr &= (uint8_t) ~0x10;
1370 }
1371 if (stop == 2) {
1372 smr |= (uint8_t)0x80;
1373 } else {
1374 smr &= (uint8_t) ~0x80;
1375 }
1376 sci_reg->SMR = smr;
1377 if (bits == 9) {
1378 scmr &= (uint8_t) ~0x10;
1379 ch_9bit[idx] = 1;
1380 } else {
1381 scmr |= (uint8_t)0x10;
1382 ch_9bit[idx] = 0;
1383 }
1384 if (flow) {
1385 sci_reg->SPMR |= 0x01;
1386 }
1387 sci_reg->SCMR = scmr;
1388 sci_reg->SEMR = (uint8_t)0xc0;
1389 ra_sci_set_baud(ch, baud);
1390 delay_us(10);
1391 sci_reg->SCR = (uint8_t)0x50;
1392 ra_sci_irq_enable(ch);
1393 ra_enable_irq(state);
1394 if (!ra_sci_init_flag[idx]) {
1395 ra_sci_init_flag[idx] = true;
1396 }
1397 }
1398
1399 void ra_sci_init(uint32_t ch, uint32_t tx_pin, uint32_t rx_pin, uint32_t baud, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow) {
1400 ra_sci_init_with_flow(ch, tx_pin, rx_pin, baud, bits, parity, stop, flow, PIN_END, PIN_END);
1401 }
1402
1403 void ra_sci_deinit(uint32_t ch) {
1404 uint32_t idx = ch_to_idx[ch];
1405 if (ra_sci_init_flag[idx] != 0) {
1406 ra_sci_init_flag[idx]--;
1407 if (ra_sci_init_flag[idx] == 0) {
1408 ra_sci_irq_disable(ch);
1409 ra_sci_module_stop(ch);
1410 sci_cb[idx] = 0;
1411 }
1412 }
1413 }
1414
1415 /* rx-interrupt */
1416 void sci_uart_rxi_isr(void) {
1417 IRQn_Type irq = R_FSP_CurrentIrqGet();
1418 uint32_t ch = irq_to_ch[(uint32_t)irq];
1419 ra_sci_isr_rx(ch);
1420 }
1421
1422 /* tx-interrupt */
1423 void sci_uart_txi_isr(void) {
1424 IRQn_Type irq = R_FSP_CurrentIrqGet();
1425 uint32_t ch = irq_to_ch[(uint32_t)irq];
1426 ra_sci_isr_tx(ch);
1427 }
1428
1429 /* er-interrupt */
1430 void sci_uart_eri_isr(void) {
1431 IRQn_Type irq = R_FSP_CurrentIrqGet();
1432 uint32_t ch = irq_to_ch[(uint32_t)irq];
1433 ra_sci_isr_er(ch);
1434 }
1435
1436 /* te-interrupt */
1437 void sci_uart_tei_isr(void) {
1438 IRQn_Type irq = R_FSP_CurrentIrqGet();
1439 uint32_t ch = irq_to_ch[(uint32_t)irq];
1440 ra_sci_isr_te(ch);
1441 }