summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/adc.c
diff options
context:
space:
mode:
Diffstat (limited to 'stmhal/adc.c')
-rw-r--r--stmhal/adc.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/stmhal/adc.c b/stmhal/adc.c
index 8a1c23c4f6..dafa3f7bce 100644
--- a/stmhal/adc.c
+++ b/stmhal/adc.c
@@ -11,6 +11,7 @@
#include "adc.h"
#include "pin.h"
#include "build/pins.h"
+#include "timer.h"
// Usage Model:
//
@@ -162,8 +163,39 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) {
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);
+
+ // Init TIM6 at the required frequency (in Hz)
+ timer_tim6_init(mp_obj_get_int(freq_in));
+
+ // Start timer
+ 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++) {
+ // 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;
+ }
+
+ // Stop timer
+ HAL_TIM_Base_Stop(&TIM6_Handle);
+
+ return mp_obj_new_int(bufinfo.len);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(adc_read_timed_obj, adc_read_timed);
+
STATIC const mp_map_elem_t adc_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj},
+ { MP_OBJ_NEW_QSTR(MP_QSTR_read_timed), (mp_obj_t)&adc_read_timed_obj},
};
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);