Skip to content

Commit 9152f6a

Browse files
committed
analog: provide analogReference() getter w/ measure
1 parent f05666b commit 9152f6a

File tree

7 files changed

+73
-10
lines changed

7 files changed

+73
-10
lines changed

cores/arduino/Arduino.h

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ pin_size_t digitalPinToInterrupt(pin_size_t pin);
8686
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
8787
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
8888

89+
// analog extra functionalities
90+
91+
typedef enum _ar_aref {
92+
AR_INTERNAL,
93+
AR_INTERNAL_1_5V,
94+
AR_INTERNAL_2_0V,
95+
AR_INTERNAL_2_5V,
96+
AR_EXTERNAL,
97+
AR_DEFAULT,
98+
} ar_aref;
99+
100+
#if defined(__cplusplus)
101+
float analogReference();
102+
#endif
103+
89104
#ifndef NO_USB
90105
#define Serial SerialUSB
91106
#define Serial1 _UART1_

cores/arduino/analog.cpp

+48-9
Original file line numberDiff line numberDiff line change
@@ -526,24 +526,63 @@ int analogRead(pin_size_t pinNumber) {
526526
return adcConvert(_adc,cfg_adc);
527527
}
528528

529+
void analogReference(uint8_t mode) {
529530

531+
adc_vref_control_t ctrl;
532+
533+
switch (mode) {
534+
case AR_INTERNAL:
535+
ctrl = ADC_VREF_CONTROL_IVREF_AVSS0; break;
536+
case AR_INTERNAL_1_5V:
537+
ctrl = ADC_VREF_CONTROL_1_5V_OUTPUT; break;
538+
case AR_INTERNAL_2_0V:
539+
ctrl = ADC_VREF_CONTROL_2_0V_OUTPUT; break;
540+
case AR_INTERNAL_2_5V:
541+
ctrl = ADC_VREF_CONTROL_2_5V_OUTPUT; break;
542+
case AR_EXTERNAL:
543+
ctrl = ADC_VREF_CONTROL_VREFH0_AVSS0; break;
544+
default:
545+
ctrl = ADC_VREF_CONTROL_AVCC0_AVSS0; break;
546+
}
530547

531-
void analogReference(uint8_t mode) {
532-
// TODO: in case VREFH is selected, please configure the pin accordingly
533-
// something like
534-
// pinPeripheral(digitalPinToBspPin(VREFH), (uint32_t) IOPORT_CFG_PERIPHERAL_PIN | VREFH)
535548
R_ADC_Close(&adc.ctrl);
536-
adc.cfg_extend.adc_vref_control = (adc_vref_control_t)mode;
549+
adc.cfg_extend.adc_vref_control = ctrl;
537550
R_ADC_Open(&adc.ctrl, &adc.cfg);
538551

539552
R_ADC_Close(&adc1.ctrl);
540-
adc1.cfg_extend.adc_vref_control = (adc_vref_control_t)mode;
553+
adc1.cfg_extend.adc_vref_control = ctrl;
541554
R_ADC_Open(&adc1.ctrl, &adc1.cfg);
542555
}
543556

544-
545-
546-
557+
static float aref = 0;
558+
float analogReference() {
559+
switch (adc.cfg_extend.adc_vref_control) {
560+
case ADC_VREF_CONTROL_1_5V_OUTPUT:
561+
return 1.5;
562+
case ADC_VREF_CONTROL_2_0V_OUTPUT:
563+
return 2.0;
564+
case ADC_VREF_CONTROL_2_5V_OUTPUT:
565+
return 2.5;
566+
case ADC_VREF_CONTROL_VREFH0_AVSS0:
567+
// the user must know the voltage he applies from outside
568+
return NAN;
569+
default:
570+
#if defined(AVCC_MEASURE_PIN)
571+
if (aref == 0) {
572+
analogReference(AR_INTERNAL);
573+
delayMicroseconds(5);
574+
for (int i = 0; i < 10; i++) {
575+
aref += analogRead(AVCC_MEASURE_PIN) * 1.43f * AVCC_MULTIPLY_FACTOR / (1 << _analogRequestedReadResolution);
576+
}
577+
aref = aref / 10;
578+
analogReference(AR_DEFAULT);
579+
}
580+
return aref;
581+
#else
582+
return NAN;
583+
#endif
584+
}
585+
}
547586

548587
void analogReadResolution(int bits) {
549588
/* 20221109 [maidnl] FIX Requested resolution:

cores/arduino/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void arduino_main(void)
9292

9393
_init();
9494
initVariant();
95+
analogReference();
9596
#ifndef NO_USB
9697
__USBStart();
9798
Serial.begin(115200);

variants/SANTIAGO/pins_arduino.h

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ static const uint8_t SS = PIN_SPI_CS;
138138

139139
#define EXT_INTERRUPTS_HOWMANY 2
140140

141+
#define AVCC_MEASURE_PIN 36
142+
#define AVCC_MULTIPLY_FACTOR 8.33
143+
141144
#define USB_VID (0x2341)
142145
#define USB_PID (0x0069)
143146
#define USB_NAME "Santiago"

variants/SANTIAGO/variant.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ extern "C" const PinMuxCfg_t g_pin_cfg[] = {
120120
{ BSP_IO_PORT_04_PIN_08, P408 }, /* (34) D34 EXT_SCL */
121121
{ BSP_IO_PORT_04_PIN_09, P409 }, /* (35) D35 EXT_SDA */
122122

123+
{ BSP_IO_PORT_05_PIN_00, P500 }, /* (36) Analog voltage measure pin */
123124
};
124125

125126
extern "C" const size_t g_pin_cfg_size = sizeof(g_pin_cfg);

variants/SANTIAGO_COMPOSTA/pins_arduino.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ static const uint8_t SS = PIN_SPI_CS;
119119
/****** AGT CORE DEFINES *******/
120120
#define AGT_HOWMANY 2
121121

122-
123122
/****** CAN CORE DEFINES ******/
124123

125124
#define CAN_HOWMANY 1
@@ -130,6 +129,9 @@ static const uint8_t SS = PIN_SPI_CS;
130129

131130
#define EXT_INTERRUPTS_HOWMANY 2
132131

132+
#define AVCC_MEASURE_PIN 38
133+
#define AVCC_MULTIPLY_FACTOR 8.33
134+
133135
#define USB_VID (0x2341)
134136
#define USB_PID (0x0069)
135137
#define USB_NAME "Santiago"

variants/SANTIAGO_COMPOSTA/variant.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ extern "C" const PinMuxCfg_t g_pin_cfg[] = {
119119
{ BSP_IO_PORT_02_PIN_06, P206 }, /* (33) D33 */
120120
{ BSP_IO_PORT_02_PIN_13, P213 }, /* (34) D34 */
121121
{ BSP_IO_PORT_02_PIN_12, P212 }, /* (35) D35 */
122+
123+
{ BSP_IO_PORT_05_PIN_00, P500 }, /* (36) Analog voltage measure pin */
122124
};
123125

124126
extern "C" const size_t g_pin_cfg_size = sizeof(g_pin_cfg);

0 commit comments

Comments
 (0)