diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-18 23:28:56 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-18 23:28:56 +0100 |
commit | 38ae014e4258811d1612f9e140a35f8f9aa0ddb8 (patch) | |
tree | 2894967f48f3744515ad134f362337a3e2f64d82 /stmhal/adc.c | |
parent | 71e9bfa20dd02457e73c2bec85102b6faf527a33 (diff) |
stmhal: Update ADC, DAC and I2C objects to use new buffer protocol.
Main reason for expanding buffer protocol API was to support writes to a
buffer in ADC module (see read_timed). With this change you can now
create an array of arbitrary type and ADC.read_timed will store into
that array in the correct format (byte, int, float). I wonder though if
all these changes were really worth it to support just this function.
Hopefully this enhanced buffer protocol API (with typecode specified)
will be used elsewhere.
Diffstat (limited to 'stmhal/adc.c')
-rw-r--r-- | stmhal/adc.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/stmhal/adc.c b/stmhal/adc.c index 78f0820d6..be83b03ad 100644 --- a/stmhal/adc.c +++ b/stmhal/adc.c @@ -8,6 +8,7 @@ #include "qstr.h" #include "obj.h" #include "runtime.h" +#include "binary.h" #include "adc.h" #include "pin.h" #include "genhdr/pins.h" @@ -166,8 +167,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read); STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) { pyb_obj_adc_t *self = self_in; - buffer_info_t bufinfo; - mp_get_buffer_raise(buf_in, &bufinfo); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); + int typesize = mp_binary_get_size(bufinfo.typecode); // Init TIM6 at the required frequency (in Hz) timer_tim6_init(mp_obj_get_int(freq_in)); @@ -176,13 +178,17 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_ HAL_TIM_Base_Start(&TIM6_Handle); // This uses the timer in polling mode to do the sampling - // TODO use DMA - for (uint i = 0; i < bufinfo.len; i++) { + // We could use DMA, but then we can't convert the values correctly for the buffer + for (uint index = 0; index < bufinfo.len; index++) { // Wait for the timer to trigger while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) { } __HAL_TIM_CLEAR_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE); - ((byte*)bufinfo.buf)[i] = adc_read_channel(&self->handle) >> 4; + uint value = adc_read_channel(&self->handle); + if (typesize == 1) { + value >>= 4; + } + mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value); } // Stop timer |