diff --git a/stmhal/adc.c b/stmhal/adc.c index 78f0820d69..be83b03ad7 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 diff --git a/stmhal/dac.c b/stmhal/dac.c index c018a4046a..5b2d17d889 100644 --- a/stmhal/dac.c +++ b/stmhal/dac.c @@ -169,8 +169,8 @@ mp_obj_t pyb_dac_dma(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // set TIM6 to trigger the DAC at the given frequency TIM6_Config(mp_obj_get_int(args[2])); - buffer_info_t bufinfo; - mp_get_buffer_raise(args[1], &bufinfo); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); __DMA1_CLK_ENABLE(); diff --git a/stmhal/i2c.c b/stmhal/i2c.c index 780d4fb498..4bbfec3ea8 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -163,8 +163,8 @@ STATIC mp_obj_t pyb_i2c_write(mp_obj_t self_in, mp_obj_t i2c_addr_in, mp_obj_t d uint8_t data[1] = {mp_obj_get_int(data_in)}; status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, data, 1, 500); } else { - buffer_info_t bufinfo; - mp_get_buffer_raise(data_in, &bufinfo); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, bufinfo.buf, bufinfo.len, 500); } @@ -209,8 +209,8 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) { uint8_t data[1] = {mp_obj_get_int(args[3])}; status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200); } else { - buffer_info_t bufinfo; - mp_get_buffer_raise(args[3], &bufinfo); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200); } diff --git a/stmhal/math.c b/stmhal/math.c index 75a5aeb2ee..5fb9c4ba16 100644 --- a/stmhal/math.c +++ b/stmhal/math.c @@ -20,6 +20,10 @@ typedef union { }; } double_s_t; +double __attribute__((pcs("aapcs"))) __aeabi_i2d(int32_t x) { + return (float)x; +} + double __attribute__((pcs("aapcs"))) __aeabi_f2d(float x) { float_s_t fx={0}; double_s_t dx={0};