Skip to content

Commit c26ac3c

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 00ec495 commit c26ac3c

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Diff for: cores/arduino/Arduino.h

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

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

105108
void interrupts(void);

Diff for: cores/arduino/zephyrCommon.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ void analogWrite(pin_size_t pinNumber, int value)
282282
{
283283
size_t idx = pwm_pin_index(pinNumber);
284284

285-
if (!pwm_is_ready_dt(&arduino_pwm[idx])) {
285+
if (idx >= ARRAY_SIZE(arduino_pwm) ) {
286286
return;
287287
}
288288

289-
if (idx >= ARRAY_SIZE(arduino_pwm) ) {
289+
if (!pwm_is_ready_dt(&arduino_pwm[idx])) {
290290
return;
291291
}
292292

@@ -319,10 +319,21 @@ void analogReference(uint8_t mode)
319319
}
320320
}
321321

322+
// Note: We can not update the arduino_adc structure as it is read only...
323+
static int read_resolution = 10;
324+
325+
void analogReadResolution(int bits)
326+
{
327+
read_resolution = bits;
328+
//for(size_t i=0; i<ARRAY_SIZE(arduino_analog_pins); i++) {
329+
// arduino_adc[i].resolution = bits;
330+
// }
331+
}
332+
322333
int analogRead(pin_size_t pinNumber)
323334
{
324335
int err;
325-
int16_t buf;
336+
uint16_t buf;
326337
struct adc_sequence seq = { .buffer = &buf, .buffer_size = sizeof(buf) };
327338
size_t idx = analog_pin_index(pinNumber);
328339

@@ -344,7 +355,8 @@ int analogRead(pin_size_t pinNumber)
344355
}
345356

346357
seq.channels = BIT(arduino_adc[idx].channel_id);
347-
seq.resolution = arduino_adc[idx].resolution;
358+
//seq.resolution = arduino_adc[idx].resolution;
359+
seq.resolution = read_resolution;
348360
seq.oversampling = arduino_adc[idx].oversampling;
349361

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

0 commit comments

Comments
 (0)