1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2016-2018 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 <string.h>
29
30 #include "py/runtime.h"
31 #include "py/mphal.h"
32 #include "py/mperrno.h"
33 #include "extmod/machine_i2c.h"
34 #include "i2c.h"
35 #include "modmachine.h"
36
37 #if MICROPY_HW_ENABLE_HW_I2C
38
39 #define I2C_POLL_DEFAULT_TIMEOUT_US (50000) // 50ms
40
41 #if defined(STM32F0) || defined(STM32F4) || defined(STM32F7)
42
43 typedef struct _machine_hard_i2c_obj_t {
44 mp_obj_base_t base;
45 i2c_t *i2c;
46 mp_hal_pin_obj_t scl;
47 mp_hal_pin_obj_t sda;
48 } machine_hard_i2c_obj_t;
49
50 STATIC const machine_hard_i2c_obj_t machine_hard_i2c_obj[MICROPY_HW_MAX_I2C] = {
51 #if defined(MICROPY_HW_I2C1_SCL)
52 [0] = {{&machine_hard_i2c_type}, I2C1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA},
53 #endif
54 #if defined(MICROPY_HW_I2C2_SCL)
55 [1] = {{&machine_hard_i2c_type}, I2C2, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA},
56 #endif
57 #if defined(MICROPY_HW_I2C3_SCL)
58 [2] = {{&machine_hard_i2c_type}, I2C3, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA},
59 #endif
60 #if defined(MICROPY_HW_I2C4_SCL)
61 [3] = {{&machine_hard_i2c_type}, I2C4, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA},
62 #endif
63 };
64
machine_hard_i2c_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)65 STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
66 machine_hard_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
67
68 #if defined(STM32F4)
69
70 uint32_t freq = self->i2c->CR2 & 0x3f;
71 uint32_t ccr = self->i2c->CCR;
72 if (ccr & 0x8000) {
73 // Fast mode, assume duty cycle of 16/9
74 freq = freq * 40000 / (ccr & 0xfff);
75 } else {
76 // Standard mode
77 freq = freq * 500000 / (ccr & 0xfff);
78 }
79
80 mp_printf(print, "I2C(%u, scl=%q, sda=%q, freq=%u)",
81 self - &machine_hard_i2c_obj[0] + 1,
82 mp_hal_pin_name(self->scl), mp_hal_pin_name(self->sda),
83 freq);
84
85 #else
86
87 uint32_t timingr = self->i2c->TIMINGR;
88 uint32_t presc = timingr >> 28;
89 uint32_t sclh = timingr >> 8 & 0xff;
90 uint32_t scll = timingr & 0xff;
91 uint32_t freq = HAL_RCC_GetPCLK1Freq() / (presc + 1) / (sclh + scll + 2);
92 mp_printf(print, "I2C(%u, scl=%q, sda=%q, freq=%u, timingr=0x%08x)",
93 self - &machine_hard_i2c_obj[0] + 1,
94 mp_hal_pin_name(self->scl), mp_hal_pin_name(self->sda),
95 freq, timingr);
96
97 #endif
98 }
99
machine_hard_i2c_init(machine_hard_i2c_obj_t * self,uint32_t freq,uint32_t timeout_us)100 void machine_hard_i2c_init(machine_hard_i2c_obj_t *self, uint32_t freq, uint32_t timeout_us) {
101 uint32_t timeout_ms = (timeout_us + 999) / 1000;
102 i2c_init(self->i2c, self->scl, self->sda, freq, timeout_ms);
103 }
104
machine_hard_i2c_transfer(mp_obj_base_t * self_in,uint16_t addr,size_t n,mp_machine_i2c_buf_t * bufs,unsigned int flags)105 int machine_hard_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags) {
106 machine_hard_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
107
108 size_t remain_len = 0;
109 for (size_t i = 0; i < n; ++i) {
110 remain_len += bufs[i].len;
111 }
112
113 int ret = i2c_start_addr(self->i2c, flags & MP_MACHINE_I2C_FLAG_READ, addr, remain_len, flags & MP_MACHINE_I2C_FLAG_STOP);
114 if (ret < 0) {
115 return ret;
116 }
117
118 int num_acks = 0; // only valid for write; for read it'll be 0
119 for (; n--; ++bufs) {
120 remain_len -= bufs->len;
121 if (flags & MP_MACHINE_I2C_FLAG_READ) {
122 ret = i2c_read(self->i2c, bufs->buf, bufs->len, remain_len);
123 } else {
124 ret = i2c_write(self->i2c, bufs->buf, bufs->len, remain_len);
125 }
126 if (ret < 0) {
127 return ret;
128 }
129 num_acks += ret;
130 }
131
132 return num_acks;
133 }
134
135 #else
136
137 // No hardware I2C driver for this MCU so use the software implementation
138
139 typedef mp_machine_soft_i2c_obj_t machine_hard_i2c_obj_t;
140
141 STATIC machine_hard_i2c_obj_t machine_hard_i2c_obj[MICROPY_HW_MAX_I2C] = {
142 #if defined(MICROPY_HW_I2C1_SCL)
143 [0] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA},
144 #endif
145 #if defined(MICROPY_HW_I2C2_SCL)
146 [1] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C2_SCL, MICROPY_HW_I2C2_SDA},
147 #endif
148 #if defined(MICROPY_HW_I2C3_SCL)
149 [2] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C3_SCL, MICROPY_HW_I2C3_SDA},
150 #endif
151 #if defined(MICROPY_HW_I2C4_SCL)
152 [3] = {{&machine_hard_i2c_type}, 1, I2C_POLL_DEFAULT_TIMEOUT_US, MICROPY_HW_I2C4_SCL, MICROPY_HW_I2C4_SDA},
153 #endif
154 };
155
machine_hard_i2c_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)156 STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
157 machine_hard_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
158 mp_printf(print, "I2C(%u, scl=%q, sda=%q, freq=%u, timeout=%u)",
159 self - &machine_hard_i2c_obj[0] + 1,
160 self->scl->name, self->sda->name, 500000 / self->us_delay, self->us_timeout);
161 }
162
machine_hard_i2c_init(machine_hard_i2c_obj_t * self,uint32_t freq,uint32_t timeout)163 STATIC void machine_hard_i2c_init(machine_hard_i2c_obj_t *self, uint32_t freq, uint32_t timeout) {
164 // set parameters
165 if (freq >= 1000000) {
166 // allow fastest possible bit-bang rate
167 self->us_delay = 0;
168 } else {
169 self->us_delay = 500000 / freq;
170 if (self->us_delay == 0) {
171 self->us_delay = 1;
172 }
173 }
174
175 self->us_timeout = timeout;
176
177 // init pins
178 mp_hal_pin_open_drain(self->scl);
179 mp_hal_pin_open_drain(self->sda);
180 }
181
182 #define machine_hard_i2c_transfer mp_machine_soft_i2c_transfer
183
184 #endif
185
186 /******************************************************************************/
187 /* MicroPython bindings for machine API */
188
189 #if defined(STM32F0) || defined(STM32F7)
190 #define MACHINE_I2C_TIMINGR (1)
191 #else
192 #define MACHINE_I2C_TIMINGR (0)
193 #endif
194
machine_hard_i2c_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * all_args)195 mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
196 MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
197
198 // parse args
199 enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout, ARG_timingr };
200 static const mp_arg_t allowed_args[] = {
201 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
202 { MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
203 { MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
204 { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} },
205 { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = I2C_POLL_DEFAULT_TIMEOUT_US} },
206 #if MACHINE_I2C_TIMINGR
207 { MP_QSTR_timingr, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
208 #endif
209 };
210 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
211 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
212
213 // get static peripheral object
214 int i2c_id = i2c_find_peripheral(args[ARG_id].u_obj);
215 machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t *)&machine_hard_i2c_obj[i2c_id - 1];
216
217 // here we would check the scl/sda pins and configure them, but it's not implemented
218 if (args[ARG_scl].u_obj != MP_OBJ_NULL || args[ARG_sda].u_obj != MP_OBJ_NULL) {
219 mp_raise_ValueError(MP_ERROR_TEXT("explicit choice of scl/sda is not implemented"));
220 }
221
222 // initialise the I2C peripheral
223 machine_hard_i2c_init(self, args[ARG_freq].u_int, args[ARG_timeout].u_int);
224
225 #if MACHINE_I2C_TIMINGR
226 // If given, explicitly set the TIMINGR value
227 if (args[ARG_timingr].u_obj != mp_const_none) {
228 self->i2c->TIMINGR = mp_obj_get_int_truncated(args[ARG_timingr].u_obj);
229 }
230 #endif
231
232 return MP_OBJ_FROM_PTR(self);
233 }
234
235 STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
236 .transfer = machine_hard_i2c_transfer,
237 };
238
239 const mp_obj_type_t machine_hard_i2c_type = {
240 { &mp_type_type },
241 .name = MP_QSTR_I2C,
242 .print = machine_hard_i2c_print,
243 .make_new = machine_hard_i2c_make_new,
244 .protocol = &machine_hard_i2c_p,
245 .locals_dict = (mp_obj_dict_t *)&mp_machine_i2c_locals_dict,
246 };
247
248 #endif // MICROPY_HW_ENABLE_HW_I2C
249