Debian Codegraph @

ports/renesas-ra/ra/ra_adc.c

raw
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021 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 <stdbool.h>
28 #include "hal_data.h"
29 #include "ra_config.h"
30 #include "ra_gpio.h"
31 #include "ra_utils.h"
32 #include "ra_adc.h"
33
34 static R_ADC0_Type *adc_reg = R_ADC0;
35 #if defined(RA4M1) | defined(RA4W1)
36 static R_TSN_Type *tsn_reg = (R_TSN_Type *)0x407ec000;
37 #endif
38 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
39 static R_TSN_CTRL_Type *tsn_ctrl_reg = R_TSN_CTRL;
40 static R_TSN_CAL_Type *tsn_cal_reg = R_TSN_CAL;
41 #endif
42 static uint8_t resolution = RA_ADC_DEF_RESOLUTION;
43
44 typedef struct adc_pin_to_ch {
45 uint32_t pin;
46 uint8_t ch;
47 } adc_pin_to_ch_t;
48
49 static const adc_pin_to_ch_t pin_to_ch[] = {
50 #if defined(RA4M1)
51
52 { P000, AN000 },
53 { P001, AN001 },
54 { P002, AN002 },
55 { P003, AN003 },
56 { P004, AN004 },
57 { P005, AN011 },
58 { P006, AN012 },
59 { P007, AN013 },
60 { P008, AN014 },
61 { P010, AN005 },
62 { P011, AN006 },
63 { P012, AN007 },
64 { P013, AN008 },
65 { P014, AN009 },
66 { P015, AN010 },
67 { P100, AN022 },
68 { P101, AN021 },
69 { P102, AN020 },
70 { P103, AN019 },
71 { P500, AN016 },
72 { P501, AN017 },
73 { P502, AN018 },
74 { P503, AN023 },
75 { P504, AN024 },
76 { P505, AN025 },
77
78 #elif defined(RA4W1)
79
80 { P004, AN004 },
81 { P010, AN005 },
82 { P011, AN006 },
83 { P014, AN009 },
84 { P015, AN010 },
85 { P102, AN020 },
86 { P103, AN019 },
87 { P501, AN017 },
88
89 #elif defined(RA6M2)
90 { P000, AN000 },
91 { P001, AN001 },
92 { P002, AN002 },
93 { P003, AN007 },
94 { P004, AN100 },
95 { P005, AN101 },
96 { P006, AN102 },
97 { P007, AN107 },
98 { P008, AN003 },
99 { P009, AN004 },
100 { P014, AN005 },
101 { P014, AN105 },
102 { P015, AN006 },
103 { P015, AN106 },
104 { P500, AN016 },
105 { P501, AN116 },
106 { P502, AN017 },
107 { P503, AN117 },
108 { P504, AN018 },
109 { P505, AN118 },
110 { P506, AN019 },
111 { P509, AN020 },
112
113 #elif defined(RA6M3)
114 { P000, AN000 },
115 { P001, AN001 },
116 { P002, AN002 },
117 { P003, AN007 },
118 { P004, AN100 },
119 { P005, AN101 },
120 { P006, AN102 },
121 { P007, AN107 },
122 { P008, AN003 },
123 { P009, AN004 },
124 { P010, AN103 },
125 { P014, AN005 },
126 { P014, AN105 },
127 { P015, AN006 },
128 { P015, AN106 },
129 { P500, AN016 },
130 { P501, AN116 },
131 { P502, AN017 },
132 { P503, AN117 },
133 { P504, AN018 },
134 { P505, AN118 },
135 { P506, AN019 },
136 { P507, AN119 },
137 { P508, AN020 },
138
139 #elif defined(RA6M5)
140 { P000, AN000 },
141 { P001, AN001 },
142 { P002, AN002 },
143 { P003, AN003 },
144 { P004, AN004 },
145 { P005, AN005 },
146 { P006, AN006 },
147 { P007, AN007 },
148 { P008, AN008 },
149 { P009, AN009 },
150 { P010, AN010 },
151 { P014, AN012 },
152 { P015, AN013 },
153 { P000, AN100 },
154 { P001, AN101 },
155 { P002, AN002 },
156 { P500, AN116 },
157 { P501, AN117 },
158 { P502, AN118 },
159 { P503, AN119 },
160 { P504, AN120 },
161 { P505, AN121 },
162 { P506, AN122 },
163 { P507, AN123 },
164 { P508, AN124 },
165 { P800, AN125 },
166 { P801, AN126 },
167 { P802, AN127 },
168 { P803, AN128 },
169
170 #elif defined(RA6M1)
171 { P000, AN000 },
172 { P001, AN001 },
173 { P002, AN002 },
174 { P003, AN007 },
175 { P004, AN100 },
176 { P005, AN101 },
177 { P006, AN102 },
178 { P007, AN107 },
179 { P008, AN003 },
180 { P014, AN005 },
181 { P014, AN105 },
182 { P015, AN006 },
183 { P015, AN106 },
184 { P500, AN016 },
185 { P501, AN116 },
186 { P502, AN017 },
187 { P503, AN117 },
188 { P504, AN018 },
189 { P508, AN020 },
190
191 #else
192 #error "CMSIS MCU Series is not specified."
193 #endif
194 };
195 #define ADC_PIN_TO_CH_SIZE (sizeof(pin_to_ch) / sizeof(adc_pin_to_ch_t))
196
197 #define ADC_CH_MAX (32)
198 #define ADC_UNIT0_SCAN_MAX (28)
199 static uint16_t adc_values[ADC_CH_MAX];
200
201 bool ra_adc_pin_to_ch(uint32_t pin, uint8_t *ch) {
202 uint32_t i;
203 *ch = (uint8_t)ADC_NON;
204 for (i = 0; i < ADC_PIN_TO_CH_SIZE; i++) {
205 if (pin == pin_to_ch[i].pin) {
206 *ch = pin_to_ch[i].ch;
207 break;
208 }
209 }
210 if (*ch == (uint8_t)ADC_NON) {
211 return false;
212 } else {
213 return true;
214 }
215 }
216
217 bool ra_adc_ch_to_pin(uint8_t ch, uint32_t *pin) {
218 uint32_t i;
219 *pin = (uint32_t)PIN_END;
220 for (i = 0; i < ADC_PIN_TO_CH_SIZE; i++) {
221 if (ch == pin_to_ch[i].ch) {
222 *pin = pin_to_ch[i].pin;
223 break;
224 }
225 }
226 if (*pin == (uint32_t)PIN_END) {
227 return false;
228 } else {
229 return true;
230 }
231 }
232
233 uint8_t ra_adc_get_channel(uint32_t pin) {
234 bool flag;
235 uint8_t ch;
236 flag = ra_adc_pin_to_ch(pin, &ch);
237 if (!flag) {
238 ch = 0;
239 }
240 return ch;
241 }
242
243 static void ra_adc0_module_start(void) {
244 ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD16_Msk);
245 }
246
247 static void ra_adc0_module_stop(void) {
248 ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD16_Msk);
249 }
250
251 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
252 static void ra_adc1_module_start(void) {
253 ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD15_Msk);
254 }
255
256 static void ra_adc1_module_stop(void) {
257 ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD15_Msk);
258 }
259 #endif
260
261 // For RA4M1 and RA4W1, there is no TSN configuration
262 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
263 static void ra_tsn_module_start(void) {
264 ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD22_Msk);
265 }
266
267 static void ra_tsn_module_stop(void) {
268 ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD22_Msk);
269 }
270 #endif
271
272 void ra_adc_set_pin(uint32_t pin, bool adc_enable) {
273 uint32_t port = GPIO_PORT(pin);
274 uint32_t bit = GPIO_BIT(pin);
275 uint32_t pfs = _PXXPFS(port, bit);
276 pwpr_unprotect();
277 if (adc_enable) {
278 pfs &= ~PMR_MASK; /* GPIO */
279 pfs &= ~PDR_MASK; /* input */
280 pfs |= ASEL_MASK; /* set adc bit */
281 } else {
282 pfs |= PMR_MASK; /* GPIO */
283 pfs &= ~PDR_MASK; /* input */
284 pfs &= ~ASEL_MASK; /* clear adc bit */
285 }
286 _PXXPFS(port, bit) = pfs;
287 pwpr_protect();
288 }
289
290 void ra_adc_enable(uint32_t pin) {
291 ra_adc_set_pin(pin, true);
292 }
293
294 void ra_adc_disable(uint32_t pin) {
295 ra_adc_set_pin(pin, false);
296 }
297
298 __attribute__((naked)) static void min_delay(__attribute__((unused)) uint32_t loop_cnt) {
299 __asm volatile (
300 "sw_delay_loop: \n"
301 #if defined(__ICCARM__) || defined(__ARMCC_VERSION)
302 " subs r0, #1 \n" ///< 1 cycle
303 #elif defined(__GNUC__)
304 " sub r0, r0, #1 \n" ///< 1 cycle
305 #endif
306 " cmp r0, #0 \n" ///< 1 cycle
307 /* CM0 and CM23 have a different instruction set */
308 #if defined(__CORE_CM0PLUS_H_GENERIC) || defined(__CORE_CM23_H_GENERIC)
309 " bne sw_delay_loop \n" ///< 2 cycles
310 #else
311 " bne.n sw_delay_loop \n" ///< 2 cycles
312 #endif
313 " bx lr \n"); ///< 2 cycles
314 }
315
316 static void udelay(uint32_t us) {
317 while (us-- > 0) {
318 min_delay(PCLK / 1000000 / 4);
319 }
320 }
321
322 void ra_adc_set_resolution(uint8_t res) {
323 uint16_t adcer;
324 uint16_t adprc;
325 #if defined(RA4M1) | defined(RA4W1)
326 if ((res == 14) | (res == 12)) {
327 if (res == 14) {
328 adprc = 0x0006;
329 } else {
330 adprc = 0x0000;
331 }
332 adcer = adc_reg->ADCER;
333 adcer &= (uint16_t) ~0x0006;
334 adcer |= (uint16_t)adprc;
335 adc_reg->ADCER;
336 resolution = res;
337 }
338 #else
339 if ((res == 12) | (res == 10) | (res == 8)) {
340 if (res == 12) {
341 adprc = 0x0000;
342 } else if (res == 10) {
343 adprc = 0x0002;
344 } else {
345 adprc = 0x0004;
346 }
347 adcer = adc_reg->ADCER;
348 adcer &= (uint16_t) ~0x0006;
349 adcer |= (uint16_t)adprc;
350 adc_reg->ADCER;
351 resolution = res;
352 }
353 #endif
354 }
355
356 uint8_t ra_adc_get_resolution(void) {
357 uint8_t res = 0;
358 uint16_t adcer;
359 adcer = adc_reg->ADCER;
360 adcer &= 0x0006;
361 #if defined(RA4M1) | defined(RA4W1)
362 if (adcer == 0x0006) {
363 res = 14;
364 } else if (adcer == 0x0000) {
365 res = 12;
366 }
367 #else
368 if (adcer == 0x0000) {
369 res = 12;
370 } else if (adcer == 0x0002) {
371 res = 10;
372 } else if (adcer == 0x0004) {
373 res = 8;
374 }
375 #endif
376 return res;
377 }
378
379 // assumption
380 // AVCC0 is used. Neither VREFH0 nor internal reference voltage is not used.
381 uint16_t ra_adc_read_ch(uint8_t ch) {
382 uint16_t value16 = 0;
383 if ((ch == ADC_TEMP) | (ch == ADC_REF)) {
384 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
385 if (ch == ADC_TEMP) {
386 adc_reg->ADEXICR_b.TSSA = 1;
387 tsn_ctrl_reg->TSCR_b.TSEN = 1;
388 while (!tsn_ctrl_reg->TSCR_b.TSEN) {
389 ;
390 }
391 tsn_ctrl_reg->TSCR_b.TSOE = 1;
392 while (!tsn_ctrl_reg->TSCR_b.TSOE) {
393 ;
394 }
395 udelay(300);
396 } else if (ch == ADC_REF) {
397 adc_reg->ADEXICR_b.OCSA = 1;
398 udelay(300);
399 }
400 #endif
401 adc_reg->ADANSA[0] = 0;
402 adc_reg->ADANSA[1] = 0;
403 } else if (ch < 16) {
404 adc_reg->ADANSA[0] |= (uint16_t)(1 << ch);
405 } else {
406 adc_reg->ADANSA[1] |= (uint16_t)(1 << (ch - 16));
407 }
408 adc_reg->ADCSR_b.ADCS = 0; /* single scan mode */
409 adc_reg->ADCSR_b.ADST = 1; /* start a/d conversion */
410 while (adc_reg->ADCSR_b.ADST) {
411 ; /* ADC in progress*/
412 }
413 if (ch == ADC_TEMP) {
414 value16 = (uint16_t)adc_reg->ADTSDR;
415 } else if (ch == ADC_REF) {
416 value16 = (uint16_t)adc_reg->ADOCDR;
417 } else {
418 value16 = (uint16_t)adc_reg->ADDR[ch];
419 }
420 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
421 if (ch == ADC_TEMP) {
422 tsn_ctrl_reg->TSCR_b.TSOE = 0;
423 while (tsn_ctrl_reg->TSCR_b.TSOE) {
424 ;
425 }
426 tsn_ctrl_reg->TSCR_b.TSEN = 0;
427 while (tsn_ctrl_reg->TSCR_b.TSEN) {
428 ;
429 }
430 } else if (ch == ADC_REF) {
431 adc_reg->ADEXICR_b.OCSA = 0;
432 }
433 #endif
434 return value16;
435 }
436
437 uint16_t ra_adc_read(uint32_t pin) {
438 uint8_t ch = ra_adc_get_channel(pin);
439 if (ch == ADC_NON) {
440 return 0;
441 }
442 return ra_adc_read_ch(ch);
443 }
444
445 int16_t ra_adc_read_itemp(void) {
446 int16_t temp = 0;
447 int16_t vmax = (int16_t)(1 << resolution);
448 #if defined(RA4M1) | defined(RA4W1)
449 uint16_t cal125 = ((uint16_t)tsn_reg->TSCDRH << 8) + (uint16_t)tsn_reg->TSCDRL;
450 uint16_t val = ra_adc_read_ch(ADC_TEMP);
451 int16_t v125 = (int16_t)(33 * cal125 / vmax / 10);
452 int16_t vtemp = (int16_t)(33 * val / vmax / 10);
453 temp = (int16_t)(125 + ((vtemp - v125) * 1000000 / (int16_t)BSP_FEATURE_ADC_TSN_SLOPE));
454 #elif defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
455 uint16_t cal127 = (uint16_t)tsn_cal_reg->TSCDR;
456 uint16_t val = ra_adc_read_ch(ADC_TEMP);
457 int16_t v127 = (int16_t)(33 * cal127 / vmax / 10);
458 int16_t vtemp = (int16_t)(33 * val / vmax / 10);
459 temp = (int16_t)(127 + ((vtemp - v127) * 1000000 / (int16_t)BSP_FEATURE_ADC_TSN_SLOPE));
460 #else
461 #error "CMSIS MCU Series is not specified."
462 #endif
463 return temp;
464 }
465
466 float ra_adc_read_ftemp(void) {
467 float temp;
468 float vmax = (float)(1 << resolution);
469 #if defined(RA4M1) | defined(RA4W1)
470 uint16_t cal125 = ((uint16_t)tsn_reg->TSCDRH << 8) + (uint16_t)tsn_reg->TSCDRL;
471 uint16_t val = ra_adc_read_ch(ADC_TEMP);
472 float v125 = (float)(3.3f * (float)cal125 / vmax);
473 float vtemp = (float)(3.3f * (float)val / vmax);
474 temp = (float)(125.0f + ((vtemp - v125) * 1000000.0f / (float)BSP_FEATURE_ADC_TSN_SLOPE));
475 #endif
476 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
477 uint16_t cal127 = (uint16_t)tsn_cal_reg->TSCDR;
478 uint16_t val = ra_adc_read_ch(ADC_TEMP);
479 float v127 = (float)(3.3f * (float)cal127 / vmax);
480 float vtemp = (float)(3.3f * (float)val / vmax);
481 temp = (float)(127.0f + ((vtemp - v127) * 1000000.0f / (float)BSP_FEATURE_ADC_TSN_SLOPE));
482 #endif
483 return temp;
484 }
485
486 float ra_adc_read_fref(void) {
487 uint16_t val = ra_adc_read_ch(ADC_REF);
488 float vmax = (float)(1 << resolution);
489 float vref = (float)(3.3f * (float)val / vmax);
490 return vref;
491 }
492
493 uint16_t ra_adc_all_read_ch(uint32_t ch) {
494 if (ch < ADC_CH_MAX) {
495 return adc_values[ch];
496 } else {
497 return 0;
498 }
499 }
500
501 void ra_adc_all(__attribute__((unused)) uint32_t resolution, uint32_t mask) {
502 uint32_t i;
503 uint32_t pin;
504 uint32_t bit;
505 uint16_t value16 = 0;
506 bool badc = ((mask & 0x1fffffff) != 0);
507 bool btemp = ((mask & (1 << ADC_TEMP)) != 0);
508 bool bref = ((mask & (1 << ADC_REF)) != 0);
509 for (i = 0; i < ADC_CH_MAX; i++) {
510 adc_values[i] = 0;
511 }
512 if (badc) {
513 bit = 1;
514 adc_reg->ADANSA[0] = 0;
515 adc_reg->ADANSA[1] = 0;
516 for (i = 0; i < ADC_UNIT0_SCAN_MAX; i++) {
517 if (mask & bit) {
518 if (ra_adc_ch_to_pin((uint8_t)i, &pin)) {
519 ra_adc_enable(pin);
520 if (i < 16) {
521 adc_reg->ADANSA[0] |= (uint16_t)(1 << i);
522 } else {
523 adc_reg->ADANSA[1] |= (uint16_t)(1 << (i - 16));
524 }
525 }
526 }
527 bit <<= 1;
528 }
529 adc_reg->ADCSR_b.ADCS = 0; /* single scan mode */
530 adc_reg->ADCSR_b.ADST = 1; /* start a/d conversion */
531 while (adc_reg->ADCSR_b.ADST) {
532 ; /* ADC in progress*/
533 }
534 bit = 1;
535 for (i = 0; i < ADC_UNIT0_SCAN_MAX; i++) {
536 if (mask & bit) {
537 value16 = (uint16_t)adc_reg->ADDR[i];
538 adc_values[i] = value16;
539 }
540 bit <<= 1;
541 }
542 }
543 if (btemp) {
544 adc_reg->ADANSA[0] = 0;
545 adc_reg->ADANSA[1] = 0;
546 adc_reg->ADEXICR_b.TSSA = 1;
547 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
548 tsn_ctrl_reg->TSCR_b.TSEN = 1;
549 while (!tsn_ctrl_reg->TSCR_b.TSEN) {
550 ;
551 }
552 tsn_ctrl_reg->TSCR_b.TSOE = 1;
553 while (!tsn_ctrl_reg->TSCR_b.TSOE) {
554 ;
555 }
556 udelay(30);
557 #endif
558 adc_reg->ADCSR_b.ADCS = 0; /* single scan mode */
559 adc_reg->ADCSR_b.ADST = 1; /* start a/d conversion */
560 while (adc_reg->ADCSR_b.ADST) {
561 ; /* ADC in progress*/
562 }
563 value16 = (uint16_t)adc_reg->ADTSDR;
564 adc_values[ADC_TEMP] = value16;
565 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
566 tsn_ctrl_reg->TSCR_b.TSOE = 0;
567 while (tsn_ctrl_reg->TSCR_b.TSOE) {
568 ;
569 }
570 tsn_ctrl_reg->TSCR_b.TSEN = 0;
571 while (tsn_ctrl_reg->TSCR_b.TSEN) {
572 ;
573 }
574 #endif
575 }
576 if (bref) {
577 adc_reg->ADANSA[0] = 0;
578 adc_reg->ADANSA[1] = 0;
579 adc_reg->ADEXICR_b.OCSA = 1;
580 udelay(30);
581 adc_reg->ADCSR_b.ADCS = 0; /* single scan mode */
582 adc_reg->ADCSR_b.ADST = 1; /* start a/d conversion */
583 while (adc_reg->ADCSR_b.ADST) {
584 ; /* ADC in progress*/
585 }
586 value16 = (uint16_t)adc_reg->ADOCDR;
587 adc_values[ADC_REF] = value16;
588 }
589 }
590
591 bool ra_adc_init(void) {
592 ra_adc0_module_start();
593 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
594 ra_adc1_module_start();
595 ra_tsn_module_start();
596 #endif
597 resolution = RA_ADC_DEF_RESOLUTION;
598 ra_adc_set_resolution(resolution);
599 return true;
600 }
601
602 bool ra_adc_deinit(void) {
603 ra_adc0_module_stop();
604 #if defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
605 ra_adc1_module_stop();
606 ra_tsn_module_stop();
607 #endif
608 return true;
609 }
610
611 __WEAK void adc_scan_end_isr(void) {
612 // dummy
613 }