|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +#include <stm32f4xx_hal.h> |
| 5 | + |
| 6 | +#include "nlr.h" |
| 7 | +#include "misc.h" |
| 8 | +#include "mpconfig.h" |
| 9 | +#include "qstr.h" |
| 10 | +#include "obj.h" |
| 11 | +#include "runtime.h" |
| 12 | +#include "i2c.h" |
| 13 | + |
| 14 | +I2C_HandleTypeDef I2cHandle_X; |
| 15 | +I2C_HandleTypeDef I2cHandle_Y; |
| 16 | + |
| 17 | +void i2c_init(void) { |
| 18 | + // init the I2C1 device |
| 19 | + memset(&I2cHandle_X, 0, sizeof(I2C_HandleTypeDef)); |
| 20 | + I2cHandle_X.Instance = I2C1; |
| 21 | + |
| 22 | + // init the I2C2 device |
| 23 | + memset(&I2cHandle_Y, 0, sizeof(I2C_HandleTypeDef)); |
| 24 | + I2cHandle_Y.Instance = I2C2; |
| 25 | +} |
| 26 | + |
| 27 | +void i2c_start(I2C_HandleTypeDef *i2c_handle) { |
| 28 | + GPIO_InitTypeDef GPIO_InitStructure; |
| 29 | + |
| 30 | + // init the GPIO lines |
| 31 | + GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; |
| 32 | + GPIO_InitStructure.Speed = GPIO_SPEED_FAST; |
| 33 | + GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines |
| 34 | + |
| 35 | + if (i2c_handle == &I2cHandle_X) { |
| 36 | + // X-skin: X9=PB6=SCL, X10=PB7=SDA |
| 37 | + GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
| 38 | + GPIO_InitStructure.Alternate = GPIO_AF4_I2C1; |
| 39 | + HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); |
| 40 | + // enable the I2C clock |
| 41 | + __I2C1_CLK_ENABLE(); |
| 42 | + } else { |
| 43 | + // Y-skin: Y9=PB10=SCL, Y10=PB11=SDA |
| 44 | + GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11; |
| 45 | + GPIO_InitStructure.Alternate = GPIO_AF4_I2C2; |
| 46 | + HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); |
| 47 | + // enable the I2C clock |
| 48 | + __I2C2_CLK_ENABLE(); |
| 49 | + } |
| 50 | + |
| 51 | + // init the I2C device |
| 52 | + i2c_handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
| 53 | + i2c_handle->Init.ClockSpeed = 400000; |
| 54 | + i2c_handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
| 55 | + i2c_handle->Init.DutyCycle = I2C_DUTYCYCLE_16_9; |
| 56 | + i2c_handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
| 57 | + i2c_handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
| 58 | + i2c_handle->Init.OwnAddress1 = 0xfe; // unused |
| 59 | + i2c_handle->Init.OwnAddress2 = 0xfe; // unused |
| 60 | + |
| 61 | + if (HAL_I2C_Init(i2c_handle) != HAL_OK) { |
| 62 | + // init error |
| 63 | + printf("accel_init: HAL_I2C_Init failed\n"); |
| 64 | + return; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/******************************************************************************/ |
| 69 | +/* Micro Python bindings */ |
| 70 | + |
| 71 | +#define PYB_NUM_I2C (2) |
| 72 | + |
| 73 | +typedef struct _pyb_i2c_obj_t { |
| 74 | + mp_obj_base_t base; |
| 75 | + I2C_HandleTypeDef *i2c_handle; |
| 76 | +} pyb_i2c_obj_t; |
| 77 | + |
| 78 | +STATIC pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2cHandle_X}, {{&pyb_i2c_type}, &I2cHandle_Y}}; |
| 79 | + |
| 80 | +STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
| 81 | + // check arguments |
| 82 | + if (!(n_args == 1 && n_kw == 0)) { |
| 83 | + nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "I2C accepts 1 argument")); |
| 84 | + } |
| 85 | + |
| 86 | + // get i2c number |
| 87 | + machine_int_t i2c_id = mp_obj_get_int(args[0]) - 1; |
| 88 | + |
| 89 | + // check i2c number |
| 90 | + if (!(0 <= i2c_id && i2c_id < PYB_NUM_I2C)) { |
| 91 | + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C bus %d does not exist", i2c_id + 1)); |
| 92 | + } |
| 93 | + |
| 94 | + // get i2c object |
| 95 | + pyb_i2c_obj_t *i2c_obj = &pyb_i2c_obj[i2c_id]; |
| 96 | + |
| 97 | + // start the peripheral |
| 98 | + i2c_start(i2c_obj->i2c_handle); |
| 99 | + |
| 100 | + return &pyb_i2c_obj; |
| 101 | +} |
| 102 | + |
| 103 | +STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) { |
| 104 | + pyb_i2c_obj_t *self = self_in; |
| 105 | + machine_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o) << 1; |
| 106 | + |
| 107 | + //printf("IsDeviceReady\n"); |
| 108 | + for (int i = 0; i < 10; i++) { |
| 109 | + HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c_handle, i2c_addr, 10, 200); |
| 110 | + //printf(" got %d\n", status); |
| 111 | + if (status == HAL_OK) { |
| 112 | + return mp_const_true; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return mp_const_false; |
| 117 | +} |
| 118 | + |
| 119 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready); |
| 120 | + |
| 121 | +STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args) { |
| 122 | + pyb_i2c_obj_t *self = args[0]; |
| 123 | + machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1; |
| 124 | + machine_uint_t mem_addr = mp_obj_get_int(args[2]); |
| 125 | + machine_uint_t n = mp_obj_get_int(args[3]); |
| 126 | + |
| 127 | + byte *data; |
| 128 | + mp_obj_t o = mp_obj_str_builder_start(&bytes_type, n, &data); |
| 129 | + HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, n, 200); |
| 130 | + |
| 131 | + //printf("Read got %d\n", status); |
| 132 | + |
| 133 | + if (status != HAL_OK) { |
| 134 | + // TODO really need a HardwareError object, or something |
| 135 | + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Mem_Read failed with code %d", status)); |
| 136 | + } |
| 137 | + |
| 138 | + return mp_obj_str_builder_end(o); |
| 139 | +} |
| 140 | + |
| 141 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_i2c_mem_read_obj, 4, 4, pyb_i2c_mem_read); |
| 142 | + |
| 143 | +STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) { |
| 144 | + pyb_i2c_obj_t *self = args[0]; |
| 145 | + machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1; |
| 146 | + machine_uint_t mem_addr = mp_obj_get_int(args[2]); |
| 147 | + HAL_StatusTypeDef status; |
| 148 | + mp_obj_type_t *type = mp_obj_get_type(args[3]); |
| 149 | + if (type->buffer_p.get_buffer != NULL) { |
| 150 | + buffer_info_t bufinfo; |
| 151 | + type->buffer_p.get_buffer(args[3], &bufinfo, BUFFER_READ); |
| 152 | + status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200); |
| 153 | + } else if (MP_OBJ_IS_INT(args[3])) { |
| 154 | + uint8_t data[1] = {mp_obj_get_int(args[3])}; |
| 155 | + status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200); |
| 156 | + } else { |
| 157 | + nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "data argument must be an integer or support the buffer protocol")); |
| 158 | + } |
| 159 | + |
| 160 | + //printf("Write got %d\n", status); |
| 161 | + |
| 162 | + if (status != HAL_OK) { |
| 163 | + // TODO really need a HardwareError object, or something |
| 164 | + nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Mem_Write failed with code %d", status)); |
| 165 | + } |
| 166 | + |
| 167 | + return mp_const_none; |
| 168 | +} |
| 169 | + |
| 170 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_i2c_mem_write_obj, 4, 4, pyb_i2c_mem_write); |
| 171 | + |
| 172 | +STATIC const mp_method_t pyb_i2c_methods[] = { |
| 173 | + { "is_ready", &pyb_i2c_is_ready_obj }, |
| 174 | + { "mem_read", &pyb_i2c_mem_read_obj }, |
| 175 | + { "mem_write", &pyb_i2c_mem_write_obj }, |
| 176 | + { NULL, NULL }, |
| 177 | +}; |
| 178 | + |
| 179 | +const mp_obj_type_t pyb_i2c_type = { |
| 180 | + { &mp_type_type }, |
| 181 | + .name = MP_QSTR_I2C, |
| 182 | + .make_new = pyb_i2c_make_new, |
| 183 | + .methods = pyb_i2c_methods, |
| 184 | +}; |
0 commit comments