summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--stmhal/adc.c3
-rw-r--r--tests/pyb/adc.py24
-rw-r--r--tests/pyb/adc.py.exp3
3 files changed, 27 insertions, 3 deletions
diff --git a/stmhal/adc.c b/stmhal/adc.c
index a59543710b..867edc6486 100644
--- a/stmhal/adc.c
+++ b/stmhal/adc.c
@@ -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_
// This uses the timer in polling mode to do the sampling
// We could use DMA, but then we can't convert the values correctly for the buffer
adc_config_channel(self);
- for (uint index = 0; index < bufinfo.len; index++) {
+ uint nelems = bufinfo.len / typesize;
+ for (uint index = 0; index < nelems; index++) {
// Wait for the timer to trigger
while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) {
}
diff --git a/tests/pyb/adc.py b/tests/pyb/adc.py
index 7bed54e9f8..6508d7e24a 100644
--- a/tests/pyb/adc.py
+++ b/tests/pyb/adc.py
@@ -1,10 +1,30 @@
from pyb import ADC
from pyb import Pin
+pin = Pin('X22', mode=Pin.IN, pull=Pin.PULL_DOWN)
adc = ADC('X22')
print(adc)
-adc.read()
+# read single sample
+val = adc.read()
+assert val < 500
-buf = bytearray(100)
+# read into bytearray
+buf = bytearray(50)
adc.read_timed(buf, 500)
+print(len(buf))
+for i in buf:
+ assert i < 500
+
+# read into arrays with different element sizes
+import array
+ar = array.array('h', 25 * [0])
+adc.read_timed(ar, 500)
+print(len(ar))
+for i in buf:
+ assert i < 500
+ar = array.array('i', 30 * [0])
+adc.read_timed(ar, 500)
+print(len(ar))
+for i in buf:
+ assert i < 500
diff --git a/tests/pyb/adc.py.exp b/tests/pyb/adc.py.exp
index bbc6af7379..76f3914b03 100644
--- a/tests/pyb/adc.py.exp
+++ b/tests/pyb/adc.py.exp
@@ -1 +1,4 @@
<ADC on X22 channel=13>
+50
+25
+30