Skip to content

Commit ac4f6b8

Browse files
committed
stmhal: Fix adc.read_timed so buffer store respects element size.
Addresses issue adafruit#1154.
1 parent 1129de5 commit ac4f6b8

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

stmhal/adc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
228228
// This uses the timer in polling mode to do the sampling
229229
// We could use DMA, but then we can't convert the values correctly for the buffer
230230
adc_config_channel(self);
231-
for (uint index = 0; index < bufinfo.len; index++) {
231+
uint nelems = bufinfo.len / typesize;
232+
for (uint index = 0; index < nelems; index++) {
232233
// Wait for the timer to trigger
233234
while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) {
234235
}

tests/pyb/adc.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
from pyb import ADC
22
from pyb import Pin
33

4+
pin = Pin('X22', mode=Pin.IN, pull=Pin.PULL_DOWN)
45
adc = ADC('X22')
56
print(adc)
67

7-
adc.read()
8+
# read single sample
9+
val = adc.read()
10+
assert val < 500
811

9-
buf = bytearray(100)
12+
# read into bytearray
13+
buf = bytearray(50)
1014
adc.read_timed(buf, 500)
15+
print(len(buf))
16+
for i in buf:
17+
assert i < 500
18+
19+
# read into arrays with different element sizes
20+
import array
21+
ar = array.array('h', 25 * [0])
22+
adc.read_timed(ar, 500)
23+
print(len(ar))
24+
for i in buf:
25+
assert i < 500
26+
ar = array.array('i', 30 * [0])
27+
adc.read_timed(ar, 500)
28+
print(len(ar))
29+
for i in buf:
30+
assert i < 500

tests/pyb/adc.py.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
<ADC on X22 channel=13>
2+
50
3+
25
4+
30

0 commit comments

Comments
 (0)