|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdint.h> |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "ets_sys.h" |
| 32 | +#include "osapi.h" |
| 33 | +#include "gpio.h" |
| 34 | + |
| 35 | +#include "py/runtime.h" |
| 36 | +#include "modpyb.h" |
| 37 | + |
| 38 | +typedef struct _pyb_i2c_obj_t { |
| 39 | + mp_obj_base_t base; |
| 40 | + pyb_pin_obj_t *scl; |
| 41 | + pyb_pin_obj_t *sda; |
| 42 | + uint8_t prev_sda; |
| 43 | + uint8_t prev_scl; |
| 44 | +} pyb_i2c_obj_t; |
| 45 | + |
| 46 | +// these set the frequency of SCL |
| 47 | +#define mphal_i2c_wait_a() os_delay_us(2) |
| 48 | +#define mphal_i2c_wait_b() os_delay_us(1) |
| 49 | + |
| 50 | +STATIC void mphal_i2c_set_sda_scl(pyb_i2c_obj_t *self, uint8_t sda, uint8_t scl) { |
| 51 | + sda &= 0x01; |
| 52 | + scl &= 0x01; |
| 53 | + self->prev_sda = sda; |
| 54 | + self->prev_scl = scl; |
| 55 | + gpio_output_set((sda << self->sda->phys_port) | (scl << self->scl->phys_port), |
| 56 | + ((1 - sda) << self->sda->phys_port) | ((1 - scl) << self->scl->phys_port), |
| 57 | + (1 << self->sda->phys_port) | (1 << self->scl->phys_port), 0); |
| 58 | +} |
| 59 | + |
| 60 | +STATIC int mphal_i2c_get_sda(pyb_i2c_obj_t *self) { |
| 61 | + return GPIO_INPUT_GET(GPIO_ID_PIN(self->sda->phys_port)); |
| 62 | +} |
| 63 | + |
| 64 | +STATIC void mphal_i2c_start(pyb_i2c_obj_t *self) { |
| 65 | + mphal_i2c_set_sda_scl(self, 1, self->prev_scl); |
| 66 | + mphal_i2c_wait_a(); |
| 67 | + mphal_i2c_set_sda_scl(self, 1, 1); |
| 68 | + mphal_i2c_wait_a(); |
| 69 | + mphal_i2c_set_sda_scl(self, 0, 1); |
| 70 | + mphal_i2c_wait_a(); |
| 71 | +} |
| 72 | + |
| 73 | +STATIC void mphal_i2c_stop(pyb_i2c_obj_t *self) { |
| 74 | + mphal_i2c_wait_a(); |
| 75 | + mphal_i2c_set_sda_scl(self, 0, self->prev_scl); |
| 76 | + mphal_i2c_wait_a(); |
| 77 | + mphal_i2c_set_sda_scl(self, 0, 1); |
| 78 | + mphal_i2c_wait_a(); |
| 79 | + mphal_i2c_set_sda_scl(self, 1, 1); |
| 80 | + mphal_i2c_wait_a(); |
| 81 | +} |
| 82 | + |
| 83 | +STATIC void mphal_i2c_init(pyb_i2c_obj_t *self, uint32_t freq) { |
| 84 | + pyb_pin_obj_t *scl = self->scl; |
| 85 | + pyb_pin_obj_t *sda = self->sda; |
| 86 | + |
| 87 | + ETS_GPIO_INTR_DISABLE(); |
| 88 | + //ETS_INTR_LOCK(); |
| 89 | + |
| 90 | + PIN_FUNC_SELECT(sda->periph, sda->func); |
| 91 | + PIN_FUNC_SELECT(scl->periph, scl->func); |
| 92 | + |
| 93 | + GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(sda->phys_port)), |
| 94 | + GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(sda->phys_port))) |
| 95 | + | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); // open drain |
| 96 | + GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, |
| 97 | + GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << sda->phys_port)); |
| 98 | + GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(scl->phys_port)), |
| 99 | + GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(scl->phys_port))) |
| 100 | + | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); // open drain |
| 101 | + GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, |
| 102 | + GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << scl->phys_port)); |
| 103 | + |
| 104 | + mphal_i2c_set_sda_scl(self, 1, 1); |
| 105 | + |
| 106 | + ETS_GPIO_INTR_ENABLE(); |
| 107 | + //ETS_INTR_UNLOCK(); |
| 108 | + |
| 109 | + mphal_i2c_set_sda_scl(self, 1, 0); |
| 110 | + mphal_i2c_wait_a(); |
| 111 | + |
| 112 | + // when SCL = 0, toggle SDA to clear up |
| 113 | + mphal_i2c_set_sda_scl(self, 0, 0); |
| 114 | + mphal_i2c_wait_a(); |
| 115 | + mphal_i2c_set_sda_scl(self, 1, 0); |
| 116 | + mphal_i2c_wait_a(); |
| 117 | + |
| 118 | + // set data_cnt to max value |
| 119 | + for (uint8_t i = 0; i < 28; i++) { |
| 120 | + mphal_i2c_set_sda_scl(self, 1, 0); |
| 121 | + mphal_i2c_wait_a(); |
| 122 | + mphal_i2c_set_sda_scl(self, 1, 1); |
| 123 | + mphal_i2c_wait_a(); |
| 124 | + } |
| 125 | + |
| 126 | + // reset all |
| 127 | + mphal_i2c_stop(self); |
| 128 | +} |
| 129 | + |
| 130 | +STATIC int mphal_i2c_write_byte(pyb_i2c_obj_t *self, uint8_t val) { |
| 131 | + uint8_t dat; |
| 132 | + sint8 i; |
| 133 | + |
| 134 | + mphal_i2c_wait_a(); |
| 135 | + |
| 136 | + mphal_i2c_set_sda_scl(self, self->prev_sda, 0); |
| 137 | + mphal_i2c_wait_a(); |
| 138 | + |
| 139 | + for (i = 7; i >= 0; i--) { |
| 140 | + dat = val >> i; |
| 141 | + mphal_i2c_set_sda_scl(self, dat, 0); |
| 142 | + mphal_i2c_wait_a(); |
| 143 | + mphal_i2c_set_sda_scl(self, dat, 1); |
| 144 | + mphal_i2c_wait_a(); |
| 145 | + |
| 146 | + if (i == 0) { |
| 147 | + mphal_i2c_wait_b(); |
| 148 | + } |
| 149 | + |
| 150 | + mphal_i2c_set_sda_scl(self, dat, 0); |
| 151 | + mphal_i2c_wait_a(); |
| 152 | + } |
| 153 | + |
| 154 | + mphal_i2c_set_sda_scl(self, self->prev_sda, 0); |
| 155 | + mphal_i2c_wait_a(); |
| 156 | + mphal_i2c_set_sda_scl(self, 1, 0); |
| 157 | + mphal_i2c_wait_a(); |
| 158 | + mphal_i2c_set_sda_scl(self, 1, 1); |
| 159 | + mphal_i2c_wait_a(); |
| 160 | + |
| 161 | + int ret = mphal_i2c_get_sda(self); |
| 162 | + mphal_i2c_wait_a(); |
| 163 | + mphal_i2c_set_sda_scl(self, 1, 0); |
| 164 | + mphal_i2c_wait_a(); |
| 165 | + |
| 166 | + return !ret; |
| 167 | +} |
| 168 | + |
| 169 | +STATIC void mphal_i2c_write(pyb_i2c_obj_t *self, uint8_t addr, uint8_t *data, size_t len, bool stop) { |
| 170 | + mphal_i2c_start(self); |
| 171 | + if (!mphal_i2c_write_byte(self, addr << 1)) { |
| 172 | + goto er; |
| 173 | + } |
| 174 | + while (len--) { |
| 175 | + if (!mphal_i2c_write_byte(self, *data++)) { |
| 176 | + goto er; |
| 177 | + } |
| 178 | + } |
| 179 | + if (stop) { |
| 180 | + mphal_i2c_stop(self); |
| 181 | + } |
| 182 | + return; |
| 183 | + |
| 184 | +er: |
| 185 | + mphal_i2c_stop(self); |
| 186 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error")); |
| 187 | +} |
| 188 | + |
| 189 | +/******************************************************************************/ |
| 190 | +// MicroPython bindings for I2C |
| 191 | + |
| 192 | +STATIC void pyb_i2c_obj_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 193 | + enum { ARG_scl, ARG_sda, ARG_freq }; |
| 194 | + static const mp_arg_t allowed_args[] = { |
| 195 | + { MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 196 | + { MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 197 | + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, |
| 198 | + }; |
| 199 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 200 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 201 | + self->scl = mp_obj_get_pin_obj(args[ARG_scl].u_obj); |
| 202 | + self->sda = mp_obj_get_pin_obj(args[ARG_sda].u_obj); |
| 203 | + mphal_i2c_init(self, args[ARG_freq].u_int); |
| 204 | +} |
| 205 | + |
| 206 | +STATIC mp_obj_t pyb_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 207 | + mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); |
| 208 | + pyb_i2c_obj_t *self = m_new_obj(pyb_i2c_obj_t); |
| 209 | + self->base.type = &pyb_i2c_type; |
| 210 | + mp_map_t kw_args; |
| 211 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 212 | + pyb_i2c_obj_init_helper(self, n_args, args, &kw_args); |
| 213 | + return (mp_obj_t)self; |
| 214 | +} |
| 215 | + |
| 216 | +STATIC mp_obj_t pyb_i2c_obj_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 217 | + pyb_i2c_obj_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 218 | + return mp_const_none; |
| 219 | +} |
| 220 | +MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_obj_init); |
| 221 | + |
| 222 | +STATIC mp_obj_t pyb_i2c_writeto(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 223 | + enum { ARG_addr, ARG_buf, ARG_stop }; |
| 224 | + static const mp_arg_t allowed_args[] = { |
| 225 | + { MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, |
| 226 | + { MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 227 | + { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, |
| 228 | + }; |
| 229 | + pyb_i2c_obj_t *self = pos_args[0]; |
| 230 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 231 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 232 | + |
| 233 | + // get the buffer to write from |
| 234 | + mp_buffer_info_t bufinfo; |
| 235 | + mp_get_buffer_raise(args[ARG_buf].u_obj, &bufinfo, MP_BUFFER_READ); |
| 236 | + |
| 237 | + // do the I2C transfer |
| 238 | + mphal_i2c_write(self, args[ARG_addr].u_int, bufinfo.buf, bufinfo.len, args[ARG_stop].u_bool); |
| 239 | + |
| 240 | + return mp_const_none; |
| 241 | +} |
| 242 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_obj, 1, pyb_i2c_writeto); |
| 243 | + |
| 244 | +STATIC const mp_rom_map_elem_t pyb_i2c_locals_dict_table[] = { |
| 245 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_i2c_init_obj) }, |
| 246 | + { MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&pyb_i2c_writeto_obj) }, |
| 247 | +}; |
| 248 | + |
| 249 | +STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table); |
| 250 | + |
| 251 | +const mp_obj_type_t pyb_i2c_type = { |
| 252 | + { &mp_type_type }, |
| 253 | + .name = MP_QSTR_I2C, |
| 254 | + .make_new = pyb_i2c_make_new, |
| 255 | + .locals_dict = (mp_obj_dict_t*)&pyb_i2c_locals_dict, |
| 256 | +}; |
0 commit comments