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.
This commit is contained in:
Damien George 2014-04-18 23:28:56 +01:00
parent 71e9bfa20d
commit 38ae014e42
4 changed files with 21 additions and 11 deletions

View File

@ -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

View File

@ -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();

View File

@ -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);
}

View File

@ -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};