Skip to content

Commit eb8bdf4

Browse files
committed
stmhal, SPI and I2C: Improvements to functionality and consistency.
1 parent 64ba6ca commit eb8bdf4

8 files changed

Lines changed: 406 additions & 124 deletions

File tree

stmhal/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ SRC_C = \
9797
servo.c \
9898
dac.c \
9999
adc.c \
100+
bufhelper.c \
100101
i2c.c \
101102
spi.c \
102103

stmhal/accel.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ void accel_init(void) {
3535
}
3636

3737
STATIC void accel_start(void) {
38-
// start the I2C bus
38+
// start the I2C bus in master mode
39+
I2CHandle1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
40+
I2CHandle1.Init.ClockSpeed = 400000;
41+
I2CHandle1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
42+
I2CHandle1.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
43+
I2CHandle1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
44+
I2CHandle1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
45+
I2CHandle1.Init.OwnAddress1 = PYB_I2C_MASTER_ADDRESS;
46+
I2CHandle1.Init.OwnAddress2 = 0xfe; // unused
3947
i2c_init(&I2CHandle1);
4048

4149
// turn off AVDD, wait 20ms, turn on AVDD, wait 20ms again

stmhal/bufhelper.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "misc.h"
2+
#include "mpconfig.h"
3+
#include "qstr.h"
4+
#include "obj.h"
5+
#include "bufhelper.h"
6+
7+
void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, uint8_t *tmp_data) {
8+
if (MP_OBJ_IS_INT(o)) {
9+
tmp_data[0] = mp_obj_get_int(o);
10+
bufinfo->buf = tmp_data;
11+
bufinfo->len = 1;
12+
bufinfo->typecode = 'B';
13+
} else {
14+
mp_get_buffer_raise(o, bufinfo, MP_BUFFER_READ);
15+
}
16+
}
17+
18+
mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, mp_buffer_info_t *bufinfo) {
19+
if (MP_OBJ_IS_INT(o)) {
20+
// allocate a new bytearray of given length
21+
bufinfo->len = mp_obj_get_int(o);
22+
bufinfo->typecode = 'B';
23+
return mp_obj_str_builder_start(&mp_type_bytes, bufinfo->len, (byte**)&bufinfo->buf);
24+
} else {
25+
// get the existing buffer
26+
mp_get_buffer_raise(o, bufinfo, MP_BUFFER_WRITE);
27+
return MP_OBJ_NULL;
28+
}
29+
}

stmhal/bufhelper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, uint8_t *tmp_data);
2+
mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, mp_buffer_info_t *bufinfo);

0 commit comments

Comments
 (0)