Skip to content

Commit c8e82c1

Browse files
facchinmpillo79
authored andcommitted
unoq: implement analogReference
Take into account the analog switch
1 parent 5d94bd0 commit c8e82c1

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ void analogWrite(enum dacPins dacName, int value) {
353353

354354
#ifdef CONFIG_ADC
355355

356-
void analogReference(uint8_t mode) {
356+
void __attribute__((weak)) analogReference(uint8_t mode) {
357357
/*
358358
* The Arduino API not clearly defined what means of
359359
* the mode argument of analogReference().

loader/fixups.c

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,63 @@ SYS_INIT(maybe_flash_bootloader, POST_KERNEL, CONFIG_FILE_SYSTEM_INIT_PRIORITY);
186186
#include <stm32_ll_adc.h>
187187
#include <zephyr/devicetree.h>
188188

189-
static int enable_adc_reference(void) {
189+
int analog_reference(uint8_t reference) {
190190
uint8_t init_status;
191-
/* VREF+ is not connected to VDDA by default */
192-
/* Use 2.5V as reference (instead of 3.3V) for internal channels
193-
* calculation
194-
*/
191+
/* VREF+ is connected to VDDA by default */
192+
const struct gpio_dt_spec spec =
193+
GPIO_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), analog_switch_gpios, 0);
194+
195+
gpio_pin_configure_dt(&spec, GPIO_OUTPUT);
196+
195197
__HAL_RCC_SYSCFG_CLK_ENABLE();
198+
__HAL_RCC_VREF_CLK_ENABLE();
199+
200+
HAL_SYSCFG_VREFBUF_HighImpedanceConfig(SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE);
201+
HAL_SYSCFG_DisableVREFBUF();
202+
203+
if (reference == AR_DEFAULT) {
204+
/* VREF+ is connected to VDDA */
205+
gpio_pin_set_dt(&spec, 0);
206+
return;
207+
}
208+
209+
gpio_pin_set_dt(&spec, 1);
196210

197-
/* VREF_OUT2 = 2.5 V */
198-
HAL_SYSCFG_VREFBUF_VoltageScalingConfig(SYSCFG_VREFBUF_VOLTAGE_SCALE1);
211+
if (reference == AR_EXTERNAL) {
212+
return 0;
213+
}
214+
215+
uint32_t voltageScaling = SYSCFG_VREFBUF_VOLTAGE_SCALE3;
216+
switch (reference) {
217+
case AR_INTERNAL2V5:
218+
voltageScaling = SYSCFG_VREFBUF_VOLTAGE_SCALE3;
219+
break;
220+
case AR_INTERNAL2V05:
221+
voltageScaling = SYSCFG_VREFBUF_VOLTAGE_SCALE2;
222+
break;
223+
case AR_INTERNAL1V8:
224+
voltageScaling = SYSCFG_VREFBUF_VOLTAGE_SCALE1;
225+
break;
226+
case AR_INTERNAL1V5:
227+
voltageScaling = SYSCFG_VREFBUF_VOLTAGE_SCALE0;
228+
break;
229+
}
230+
231+
HAL_SYSCFG_VREFBUF_VoltageScalingConfig(voltageScaling);
232+
HAL_SYSCFG_EnableVREFBUF();
199233
HAL_SYSCFG_VREFBUF_HighImpedanceConfig(SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE);
200234

201-
init_status = HAL_SYSCFG_EnableVREFBUF();
202235
__ASSERT(init_status == HAL_OK, "ADC Conversion value may be incorrect");
203236

204237
return init_status;
205238
}
206239

207-
SYS_INIT(enable_adc_reference, POST_KERNEL, 0);
240+
EXPORT_SYMBOL(analog_reference);
241+
242+
int disable_vrefbuf() {
243+
// This is the safe HW configuration
244+
analog_reference(AR_DEFAULT);
245+
}
246+
247+
SYS_INIT(disable_vrefbuf, POST_KERNEL, 0);
208248
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <Arduino.h>
2+
3+
extern "C" {
4+
int analog_reference(uint8_t mode);
5+
};
6+
7+
void analogReference(uint8_t mode)
8+
{
9+
analog_reference(mode);
10+
}

variants/arduino_uno_q_stm32u585xx/arduino_uno_q_stm32u585xx.overlay

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
<&gpiof 9 GPIO_ACTIVE_HIGH>,
163163
<&gpiof 10 GPIO_ACTIVE_HIGH>,
164164
<&gpiog 13 GPIO_ACTIVE_HIGH>, /* Internal SPI RDY */
165+
<&gpioa 2 GPIO_ACTIVE_HIGH>, /* Analog switch for VREF */
165166
<&gpioh 3 GPIO_ACTIVE_HIGH>; /* BOOT0 */
166167

167168
builtin-led-gpios = <&gpioh 10 GPIO_ACTIVE_HIGH>,
@@ -186,6 +187,7 @@
186187
pwms = <>;
187188

188189
control-gpios = <&gpiog 13 GPIO_ACTIVE_HIGH>; /* Internal SPI RDY */
190+
analog-switch-gpios = <&gpioa 2 GPIO_ACTIVE_HIGH>; /* Analog switch for VREF */
189191

190192
io-channels = <&adc1 9>,
191193
<&adc1 10>,

variants/arduino_uno_q_stm32u585xx/variant.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@
1111
#define SS 0
1212
#define SDA 0
1313
#define SCL 0
14+
15+
#define AR_DEFAULT 0
16+
#define AR_INTERNAL2V5 1
17+
#define AR_INTERNAL2V05 2
18+
#define AR_INTERNAL1V5 3
19+
#define AR_INTERNAL1V8 4
20+
#define AR_EXTERNAL 5
21+
#define AR_INTERNAL AR_INTERNAL2V5

0 commit comments

Comments
 (0)