From 38ae014e4258811d1612f9e140a35f8f9aa0ddb8 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Fri, 18 Apr 2014 23:28:56 +0100
Subject: [PATCH] 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.
---
 stmhal/adc.c  | 16 +++++++++++-----
 stmhal/dac.c  |  4 ++--
 stmhal/i2c.c  |  8 ++++----
 stmhal/math.c |  4 ++++
 4 files changed, 21 insertions(+), 11 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
diff --git a/stmhal/dac.c b/stmhal/dac.c
index c018a4046..5b2d17d88 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 780d4fb49..4bbfec3ea 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 75a5aeb2e..5fb9c4ba1 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};
-- 
GitLab