stmhal: Add I2C support; change accel driver to use new I2C.
This commit is contained in:
parent
681d0a9ca7
commit
6cfda3084d
@ -94,9 +94,9 @@ SRC_C = \
|
||||
servo.c \
|
||||
dac.c \
|
||||
adc.c \
|
||||
i2c.c \
|
||||
|
||||
# timer.c \
|
||||
# i2c.c \
|
||||
# pybwlan.c \
|
||||
|
||||
SRC_S = \
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
#include "runtime.h"
|
||||
#include "i2c.h"
|
||||
#include "accel.h"
|
||||
|
||||
#define MMA_ADDR (0x98)
|
||||
@ -19,8 +20,6 @@
|
||||
#define MMA_REG_MODE (7)
|
||||
#define MMA_AXIS_SIGNED_VALUE(i) (((i) & 0x3f) | ((i) & 0x20 ? (~0x1f) : 0))
|
||||
|
||||
STATIC I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
void accel_init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
@ -31,38 +30,12 @@ void accel_init(void) {
|
||||
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
|
||||
GPIO_InitStructure.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
// PB6=SCL, PB7=SDA
|
||||
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
|
||||
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
|
||||
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
|
||||
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
|
||||
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
// enable the I2C1 clock
|
||||
__I2C1_CLK_ENABLE();
|
||||
|
||||
// set up the I2C1 device
|
||||
memset(&I2cHandle, 0, sizeof(I2C_HandleTypeDef));
|
||||
I2cHandle.Instance = I2C1;
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.ClockSpeed = 400000;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0xfe; // unused
|
||||
I2cHandle.Init.OwnAddress2 = 0xfe; // unused
|
||||
|
||||
if (HAL_I2C_Init(&I2cHandle) != HAL_OK) {
|
||||
// init error
|
||||
printf("accel_init: HAL_I2C_Init failed\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void accel_init_device(void) {
|
||||
STATIC void accel_start(void) {
|
||||
// start the I2C bus
|
||||
i2c_start(&I2cHandle_X);
|
||||
|
||||
// turn off AVDD, wait 20ms, turn on AVDD, wait 20ms again
|
||||
GPIOB->BSRRH = GPIO_PIN_5; // turn off
|
||||
HAL_Delay(20);
|
||||
@ -73,7 +46,7 @@ STATIC void accel_init_device(void) {
|
||||
|
||||
//printf("IsDeviceReady\n");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
status = HAL_I2C_IsDeviceReady(&I2cHandle, MMA_ADDR, 10, 200);
|
||||
status = HAL_I2C_IsDeviceReady(&I2cHandle_X, MMA_ADDR, 10, 200);
|
||||
//printf(" got %d\n", status);
|
||||
if (status == HAL_OK) {
|
||||
break;
|
||||
@ -83,7 +56,7 @@ STATIC void accel_init_device(void) {
|
||||
//printf("MemWrite\n");
|
||||
uint8_t data[1];
|
||||
data[0] = 1; // active mode
|
||||
status = HAL_I2C_Mem_Write(&I2cHandle, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
status = HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
//printf(" got %d\n", status);
|
||||
}
|
||||
|
||||
@ -108,14 +81,14 @@ STATIC mp_obj_t pyb_accel_make_new(mp_obj_t type_in, uint n_args, uint n_kw, con
|
||||
|
||||
// init accel object
|
||||
pyb_accel_obj.base.type = &pyb_accel_type;
|
||||
accel_init_device();
|
||||
accel_start();
|
||||
|
||||
return &pyb_accel_obj;
|
||||
}
|
||||
|
||||
STATIC mp_obj_t read_axis(int axis) {
|
||||
uint8_t data[1];
|
||||
HAL_I2C_Mem_Read(&I2cHandle, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
return mp_obj_new_int(MMA_AXIS_SIGNED_VALUE(data[0]));
|
||||
}
|
||||
|
||||
@ -139,7 +112,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_z_obj, pyb_accel_z);
|
||||
|
||||
STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) {
|
||||
uint8_t data[1];
|
||||
HAL_I2C_Mem_Read(&I2cHandle, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
return mp_obj_new_int(data[0]);
|
||||
}
|
||||
|
||||
@ -151,7 +124,7 @@ STATIC mp_obj_t pyb_accel_filtered_xyz(mp_obj_t self_in) {
|
||||
memmove(self->buf, self->buf + NUM_AXIS, NUM_AXIS * (FILT_DEPTH - 1) * sizeof(int16_t));
|
||||
|
||||
uint8_t data[NUM_AXIS];
|
||||
HAL_I2C_Mem_Read(&I2cHandle, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);
|
||||
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);
|
||||
|
||||
mp_obj_t tuple[NUM_AXIS];
|
||||
for (int i = 0; i < NUM_AXIS; i++) {
|
||||
@ -170,7 +143,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_filtered_xyz_obj, pyb_accel_filtered_
|
||||
|
||||
STATIC mp_obj_t pyb_accel_read_reg(mp_obj_t self_in, mp_obj_t reg) {
|
||||
uint8_t data[1];
|
||||
HAL_I2C_Mem_Read(&I2cHandle, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
return mp_obj_new_int(data[0]);
|
||||
}
|
||||
|
||||
@ -179,7 +152,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_read_reg_obj, pyb_accel_read_reg);
|
||||
STATIC mp_obj_t pyb_accel_write_reg(mp_obj_t self_in, mp_obj_t reg, mp_obj_t val) {
|
||||
uint8_t data[1];
|
||||
data[0] = mp_obj_get_int(val);
|
||||
HAL_I2C_Mem_Write(&I2cHandle, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
void accel_init(void);
|
||||
|
||||
extern I2C_HandleTypeDef I2cHandle;
|
||||
extern const mp_obj_type_t pyb_accel_type;
|
||||
|
||||
void accel_init(void);
|
||||
|
184
stmhal/i2c.c
Normal file
184
stmhal/i2c.c
Normal file
@ -0,0 +1,184 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stm32f4xx_hal.h>
|
||||
|
||||
#include "nlr.h"
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
#include "runtime.h"
|
||||
#include "i2c.h"
|
||||
|
||||
I2C_HandleTypeDef I2cHandle_X;
|
||||
I2C_HandleTypeDef I2cHandle_Y;
|
||||
|
||||
void i2c_init(void) {
|
||||
// init the I2C1 device
|
||||
memset(&I2cHandle_X, 0, sizeof(I2C_HandleTypeDef));
|
||||
I2cHandle_X.Instance = I2C1;
|
||||
|
||||
// init the I2C2 device
|
||||
memset(&I2cHandle_Y, 0, sizeof(I2C_HandleTypeDef));
|
||||
I2cHandle_Y.Instance = I2C2;
|
||||
}
|
||||
|
||||
void i2c_start(I2C_HandleTypeDef *i2c_handle) {
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
// init the GPIO lines
|
||||
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
|
||||
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
|
||||
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
|
||||
|
||||
if (i2c_handle == &I2cHandle_X) {
|
||||
// X-skin: X9=PB6=SCL, X10=PB7=SDA
|
||||
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
|
||||
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
// enable the I2C clock
|
||||
__I2C1_CLK_ENABLE();
|
||||
} else {
|
||||
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
|
||||
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
|
||||
GPIO_InitStructure.Alternate = GPIO_AF4_I2C2;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
// enable the I2C clock
|
||||
__I2C2_CLK_ENABLE();
|
||||
}
|
||||
|
||||
// init the I2C device
|
||||
i2c_handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
i2c_handle->Init.ClockSpeed = 400000;
|
||||
i2c_handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
i2c_handle->Init.DutyCycle = I2C_DUTYCYCLE_16_9;
|
||||
i2c_handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
i2c_handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
i2c_handle->Init.OwnAddress1 = 0xfe; // unused
|
||||
i2c_handle->Init.OwnAddress2 = 0xfe; // unused
|
||||
|
||||
if (HAL_I2C_Init(i2c_handle) != HAL_OK) {
|
||||
// init error
|
||||
printf("accel_init: HAL_I2C_Init failed\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* Micro Python bindings */
|
||||
|
||||
#define PYB_NUM_I2C (2)
|
||||
|
||||
typedef struct _pyb_i2c_obj_t {
|
||||
mp_obj_base_t base;
|
||||
I2C_HandleTypeDef *i2c_handle;
|
||||
} pyb_i2c_obj_t;
|
||||
|
||||
STATIC pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2cHandle_X}, {{&pyb_i2c_type}, &I2cHandle_Y}};
|
||||
|
||||
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
||||
// check arguments
|
||||
if (!(n_args == 1 && n_kw == 0)) {
|
||||
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "I2C accepts 1 argument"));
|
||||
}
|
||||
|
||||
// get i2c number
|
||||
machine_int_t i2c_id = mp_obj_get_int(args[0]) - 1;
|
||||
|
||||
// check i2c number
|
||||
if (!(0 <= i2c_id && i2c_id < PYB_NUM_I2C)) {
|
||||
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C bus %d does not exist", i2c_id + 1));
|
||||
}
|
||||
|
||||
// get i2c object
|
||||
pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id];
|
||||
|
||||
// start the peripheral
|
||||
i2c_start(i2c_obj->i2c_handle);
|
||||
|
||||
return &pyb_i2c_obj;
|
||||
}
|
||||
|
||||
STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
|
||||
pyb_i2c_obj_t *self = self_in;
|
||||
machine_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o) << 1;
|
||||
|
||||
//printf("IsDeviceReady\n");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c_handle, i2c_addr, 10, 200);
|
||||
//printf(" got %d\n", status);
|
||||
if (status == HAL_OK) {
|
||||
return mp_const_true;
|
||||
}
|
||||
}
|
||||
|
||||
return mp_const_false;
|
||||
}
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready);
|
||||
|
||||
STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args) {
|
||||
pyb_i2c_obj_t *self = args[0];
|
||||
machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1;
|
||||
machine_uint_t mem_addr = mp_obj_get_int(args[2]);
|
||||
machine_uint_t n = mp_obj_get_int(args[3]);
|
||||
|
||||
byte *data;
|
||||
mp_obj_t o = mp_obj_str_builder_start(&bytes_type, n, &data);
|
||||
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, n, 200);
|
||||
|
||||
//printf("Read got %d\n", status);
|
||||
|
||||
if (status != HAL_OK) {
|
||||
// TODO really need a HardwareError object, or something
|
||||
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Mem_Read failed with code %d", status));
|
||||
}
|
||||
|
||||
return mp_obj_str_builder_end(o);
|
||||
}
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_i2c_mem_read_obj, 4, 4, pyb_i2c_mem_read);
|
||||
|
||||
STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) {
|
||||
pyb_i2c_obj_t *self = args[0];
|
||||
machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1;
|
||||
machine_uint_t mem_addr = mp_obj_get_int(args[2]);
|
||||
HAL_StatusTypeDef status;
|
||||
mp_obj_type_t *type = mp_obj_get_type(args[3]);
|
||||
if (type->buffer_p.get_buffer != NULL) {
|
||||
buffer_info_t bufinfo;
|
||||
type->buffer_p.get_buffer(args[3], &bufinfo, BUFFER_READ);
|
||||
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
|
||||
} else if (MP_OBJ_IS_INT(args[3])) {
|
||||
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 {
|
||||
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "data argument must be an integer or support the buffer protocol"));
|
||||
}
|
||||
|
||||
//printf("Write got %d\n", status);
|
||||
|
||||
if (status != HAL_OK) {
|
||||
// TODO really need a HardwareError object, or something
|
||||
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Mem_Write failed with code %d", status));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_i2c_mem_write_obj, 4, 4, pyb_i2c_mem_write);
|
||||
|
||||
STATIC const mp_method_t pyb_i2c_methods[] = {
|
||||
{ "is_ready", &pyb_i2c_is_ready_obj },
|
||||
{ "mem_read", &pyb_i2c_mem_read_obj },
|
||||
{ "mem_write", &pyb_i2c_mem_write_obj },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
const mp_obj_type_t pyb_i2c_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_I2C,
|
||||
.make_new = pyb_i2c_make_new,
|
||||
.methods = pyb_i2c_methods,
|
||||
};
|
6
stmhal/i2c.h
Normal file
6
stmhal/i2c.h
Normal file
@ -0,0 +1,6 @@
|
||||
extern I2C_HandleTypeDef I2cHandle_X;
|
||||
extern I2C_HandleTypeDef I2cHandle_Y;
|
||||
extern const mp_obj_type_t pyb_i2c_type;
|
||||
|
||||
void i2c_init(void);
|
||||
void i2c_start(I2C_HandleTypeDef *i2c_handle);
|
@ -32,6 +32,7 @@
|
||||
#include "sdcard.h"
|
||||
#include "ff.h"
|
||||
#include "lcd.h"
|
||||
#include "i2c.h"
|
||||
#include "accel.h"
|
||||
#include "servo.h"
|
||||
#include "dac.h"
|
||||
@ -375,6 +376,8 @@ soft_reset:
|
||||
pyb_usb_dev_init(USBD_DEVICE_CDC_MSC, usbd_medium_kind);
|
||||
#endif
|
||||
|
||||
i2c_init();
|
||||
|
||||
#if MICROPY_HW_HAS_MMA7660
|
||||
// MMA accel: init and reset
|
||||
accel_init();
|
||||
|
@ -26,9 +26,9 @@
|
||||
#include "accel.h"
|
||||
#include "servo.h"
|
||||
#include "dac.h"
|
||||
#include "i2c.h"
|
||||
#if 0
|
||||
#include "usb.h"
|
||||
#include "i2c.h"
|
||||
#endif
|
||||
#include "modpyb.h"
|
||||
#include "ff.h"
|
||||
@ -261,9 +261,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj },
|
||||
#endif
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_Led), (mp_obj_t)&pyb_led_type },
|
||||
#if 0
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_I2C_obj },
|
||||
#endif
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_i2c_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_Usart), (mp_obj_t)&pyb_Usart_obj },
|
||||
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type },
|
||||
|
Loading…
Reference in New Issue
Block a user