8000 stmhal: Update ADC, DAC and I2C objects to use new buffer protocol. · errordeveloper/micropython@38ae014 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 38ae014

Browse files
committed
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.
1 parent 71e9bfa commit 38ae014

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

stmhal/adc.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "qstr.h"
99
#include "obj.h"
1010
#include "runtime.h"
11+
#include "binary.h"
1112
#include "adc.h"
1213
#include "pin.h"
1314
#include "genhdr/pins.h"
@@ -166,8 +167,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
166167
STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) {
167168
pyb_obj_adc_t *self = self_in;
168169

169-
buffer_info_t bufinfo;
170-
mp_get_buffer_raise(buf_in, &bufinfo);
170+
mp_buffer_info_t bufinfo;
171+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
172+
int typesize = mp_binary_get_size(bufinfo.typecode);
171173

172174
// Init TIM6 at the required frequency (in Hz)
173175
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_
176178
HAL_TIM_Base_Start(&TIM6_Handle);
177179

178180
// This uses the timer in polling mode to do the sampling
179-
// TODO use DMA
180-
for (uint i = 0; i < bufinfo.len; i++) {
181+
// We could use DMA, but then we can't convert the values correctly for the buffer
182+
for (uint index = 0; index < bufinfo.len; index++) {
181183
// Wait for the timer to trigger
182184
while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) {
183185
}
184186
__HAL_TIM_CLEAR_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE);
185-
((byte*)bufinfo.buf)[i] = adc_read_channel(&self->handle) >> 4;
187+
uint value = adc_read_channel(&self->handle);
188+
if (typesize == 1) {
189+
value >>= 4;
190+
}
191+
mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value);
186192
}
187193

188194
// Stop timer

stmhal/dac.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ mp_obj_t pyb_dac_dma(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
169169
// set TIM6 to trigger the DAC at the given frequency
170170
TIM6_Config(mp_obj_get_int(args[2]));
171171

172-
buffer_info_t bufinfo;
173-
mp_get_buffer_raise(args[1], &bufinfo);
172+
mp_buffer_info_t bufinfo;
173+
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
174174

175175
__DMA1_CLK_ENABLE();
176176

stmhal/i2c.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
163163
uint8_t data[1] = {mp_obj_get_int(data_in)};
164164
status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, data, 1, 500);
165165
} else {
166-
buffer_info_t bufinfo;
167-
mp_get_buffer_raise(data_in, &bufinfo);
166+
mp_buffer_info_t bufinfo;
167+
mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
168168
status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, bufinfo.buf, bufinfo.len, 500);
169169
}
170170

@@ -209,8 +209,8 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) {
209209
uint8_t data[1] = {mp_obj_get_int(args[3])};
210210
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
211211
} else {
212-
buffer_info_t bufinfo;
213-
mp_get_buffer_raise(args[3], &bufinfo);
212+
mp_buffer_info_t bufinfo;
213+
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
214214
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
215215
}
216216

stmhal/math.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ typedef union {
2020
};
2121
} double_s_t;
2222

23+
double __attribute__((pcs("aapcs"))) __aeabi_i2d(int32_t x) {
24+
return (float)x;
25+
}
26+
2327
double __attribute__((pcs("aapcs"))) __aeabi_f2d(float x) {
2428
float_s_t fx={0};
2529
double_s_t dx={0};

0 commit comments

Comments
 (0)
0