Skip to content

Commit 26e0823

Browse files
committed
Implement analogReadResolution
The current released version, was always reading with 12 bits of resolution. The Arduino analogRead web page said that by default it should default to 10 bits. The updated code is now ignoring the arduino_adc[idx].resolution field as this is setup for 12 and you can not change it run time. Like the MBED version, this version is using the static variable read_resolution to hold the current resolution. Which we then pass through to zephyr when we do the reads. I also changed the parameter buf that we pass through to zephyr from in16_t to uint16_t as if you choose resolution of 16 bits it was returning negative values.
1 parent 3a06894 commit 26e0823

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Diff for: cores/arduino/Arduino.h

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ enum digitalPins {
101101
enum analogPins { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user),
102102
adc_pin_gpios, AN_ENUMS) };
103103

104+
// We provide analogReadResolution APIs
105+
void analogReadResolution(int bits);
106+
104107
#endif
105108

106109
#ifdef CONFIG_DAC

Diff for: cores/arduino/zephyrCommon.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,21 @@ void analogReference(uint8_t mode)
368368
}
369369
}
370370

371+
// Note: We can not update the arduino_adc structure as it is read only...
372+
static int read_resolution = 10;
373+
374+
void analogReadResolution(int bits)
375+
{
376+
read_resolution = bits;
377+
//for(size_t i=0; i<ARRAY_SIZE(arduino_analog_pins); i++) {
378+
// arduino_adc[i].resolution = bits;
379+
// }
380+
}
381+
371382
int analogRead(pin_size_t pinNumber)
372383
{
373384
int err;
374-
int16_t buf;
385+
uint16_t buf;
375386
struct adc_sequence seq = { .buffer = &buf, .buffer_size = sizeof(buf) };
376387
size_t idx = analog_pin_index(pinNumber);
377388

@@ -393,7 +404,8 @@ int analogRead(pin_size_t pinNumber)
393404
}
394405

395406
seq.channels = BIT(arduino_adc[idx].channel_id);
396-
seq.resolution = arduino_adc[idx].resolution;
407+
//seq.resolution = arduino_adc[idx].resolution;
408+
seq.resolution = read_resolution;
397409
seq.oversampling = arduino_adc[idx].oversampling;
398410

399411
err = adc_read(arduino_adc[idx].dev, &seq);

0 commit comments

Comments
 (0)