1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2020-2021 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 "py/runtime.h"
28 #include "py/mphal.h"
29 #include "py/mperrno.h"
30 #include "extmod/machine_i2c.h"
31 #include "modmachine.h"
32
33 #include "hardware/i2c.h"
34
35 #define DEFAULT_I2C_FREQ (400000)
36
37 #ifndef MICROPY_HW_I2C0_SCL
38 #define MICROPY_HW_I2C0_SCL (9)
39 #define MICROPY_HW_I2C0_SDA (8)
40 #endif
41
42 #ifndef MICROPY_HW_I2C1_SCL
43 #define MICROPY_HW_I2C1_SCL (7)
44 #define MICROPY_HW_I2C1_SDA (6)
45 #endif
46
47 // SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins.
48 #define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c))
49 #define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c))
50
51 typedef struct _machine_i2c_obj_t {
52 mp_obj_base_t base;
53 i2c_inst_t *const i2c_inst;
54 uint8_t i2c_id;
55 uint8_t scl;
56 uint8_t sda;
57 uint32_t freq;
58 } machine_i2c_obj_t;
59
60 STATIC machine_i2c_obj_t machine_i2c_obj[] = {
61 {{&machine_hw_i2c_type}, i2c0, 0, MICROPY_HW_I2C0_SCL, MICROPY_HW_I2C0_SDA, 0},
62 {{&machine_hw_i2c_type}, i2c1, 1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA, 0},
63 };
64
machine_i2c_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)65 STATIC void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
66 machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
67 mp_printf(print, "I2C(%u, freq=%u, scl=%u, sda=%u)",
68 self->i2c_id, self->freq, self->scl, self->sda);
69 }
70
machine_i2c_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * all_args)71 mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
72 enum { ARG_id, ARG_freq, ARG_scl, ARG_sda };
73 static const mp_arg_t allowed_args[] = {
74 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
75 { MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} },
76 { MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
77 { MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
78 };
79
80 // Parse args.
81 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
82 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
83
84 // Get I2C bus.
85 int i2c_id = mp_obj_get_int(args[ARG_id].u_obj);
86 if (i2c_id < 0 || i2c_id >= MP_ARRAY_SIZE(machine_i2c_obj)) {
87 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
88 }
89
90 // Get static peripheral object.
91 machine_i2c_obj_t *self = (machine_i2c_obj_t *)&machine_i2c_obj[i2c_id];
92
93 // Set SCL/SDA pins if configured.
94 if (args[ARG_scl].u_obj != mp_const_none) {
95 int scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
96 if (!IS_VALID_SCL(self->i2c_id, scl)) {
97 mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
98 }
99 self->scl = scl;
100 }
101 if (args[ARG_sda].u_obj != mp_const_none) {
102 int sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
103 if (!IS_VALID_SDA(self->i2c_id, sda)) {
104 mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
105 }
106 self->sda = sda;
107 }
108
109 // Initialise the I2C peripheral if any arguments given, or it was not initialised previously.
110 if (n_args > 1 || n_kw > 0 || self->freq == 0) {
111 self->freq = args[ARG_freq].u_int;
112 i2c_init(self->i2c_inst, self->freq);
113 self->freq = i2c_set_baudrate(self->i2c_inst, self->freq);
114 gpio_set_function(self->scl, GPIO_FUNC_I2C);
115 gpio_set_function(self->sda, GPIO_FUNC_I2C);
116 gpio_set_pulls(self->scl, true, 0);
117 gpio_set_pulls(self->sda, true, 0);
118 }
119
120 return MP_OBJ_FROM_PTR(self);
121 }
122
machine_i2c_transfer_single(mp_obj_base_t * self_in,uint16_t addr,size_t len,uint8_t * buf,unsigned int flags)123 STATIC int machine_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size_t len, uint8_t *buf, unsigned int flags) {
124 machine_i2c_obj_t *self = (machine_i2c_obj_t *)self_in;
125 int ret;
126 bool nostop = !(flags & MP_MACHINE_I2C_FLAG_STOP);
127 if (flags & MP_MACHINE_I2C_FLAG_READ) {
128 ret = i2c_read_blocking(self->i2c_inst, addr, buf, len, nostop);
129 } else {
130 if (len <= 2) {
131 // Workaround issue with hardware I2C not accepting short writes.
132 mp_machine_soft_i2c_obj_t soft_i2c = {
133 .base = { &mp_machine_soft_i2c_type },
134 .us_delay = 500000 / self->freq + 1,
135 .us_timeout = 255,
136 .scl = self->scl,
137 .sda = self->sda,
138 };
139 mp_machine_i2c_buf_t bufs = {
140 .len = len,
141 .buf = buf,
142 };
143 mp_hal_pin_open_drain(self->scl);
144 mp_hal_pin_open_drain(self->sda);
145 ret = mp_machine_soft_i2c_transfer(&soft_i2c.base, addr, 1, &bufs, flags);
146 gpio_set_function(self->scl, GPIO_FUNC_I2C);
147 gpio_set_function(self->sda, GPIO_FUNC_I2C);
148 } else {
149 ret = i2c_write_blocking(self->i2c_inst, addr, buf, len, nostop);
150 }
151 }
152 return (ret < 0) ? -MP_EIO : ret;
153 }
154
155 STATIC const mp_machine_i2c_p_t machine_i2c_p = {
156 .transfer = mp_machine_i2c_transfer_adaptor,
157 .transfer_single = machine_i2c_transfer_single,
158 };
159
160 const mp_obj_type_t machine_hw_i2c_type = {
161 { &mp_type_type },
162 .name = MP_QSTR_I2C,
163 .print = machine_i2c_print,
164 .make_new = machine_i2c_make_new,
165 .protocol = &machine_i2c_p,
166 .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict,
167 };
168