blob: f0c4896c2e5730cbce82a4822dcb62f7677f2ebe [file] [log] [blame]
Damien George0823c1b2016-09-01 15:07:20 +10001/*
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>
Damien George5bb28c72016-10-03 12:39:31 +110028#include <string.h>
Damien George0823c1b2016-09-01 15:07:20 +100029
30#include "py/runtime.h"
31#include "extmod/machine_spi.h"
32
33#if MICROPY_PY_MACHINE_SPI
34
Damien George1eb3c662016-12-08 13:47:01 +110035// if a port didn't define MSB/LSB constants then provide them
36#ifndef MICROPY_PY_MACHINE_SPI_MSB
37#define MICROPY_PY_MACHINE_SPI_MSB (0)
38#define MICROPY_PY_MACHINE_SPI_LSB (1)
39#endif
40
Damien George1eb3c662016-12-08 13:47:01 +110041/******************************************************************************/
42// MicroPython bindings for generic machine.SPI
43
44STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
45
46mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
47 // check the id argument, if given
48 if (n_args > 0) {
49 if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
50 #if defined(MICROPY_PY_MACHINE_SPI_MAKE_NEW)
51 // dispatch to port-specific constructor
52 extern mp_obj_t MICROPY_PY_MACHINE_SPI_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
53 return MICROPY_PY_MACHINE_SPI_MAKE_NEW(type, n_args, n_kw, args);
54 #else
55 mp_raise_ValueError("invalid SPI peripheral");
56 #endif
57 }
58 --n_args;
59 ++args;
60 }
61
62 // software SPI
63 return mp_machine_soft_spi_make_new(type, n_args, n_kw, args);
64}
65
66STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
67 mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
68 mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
69 spi_p->init(s, n_args - 1, args + 1, kw_args);
70 return mp_const_none;
71}
72STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
73
74STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
75 mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
76 mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
77 if (spi_p->deinit != NULL) {
78 spi_p->deinit(s);
79 }
80 return mp_const_none;
81}
82STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
83
Damien George5bb28c72016-10-03 12:39:31 +110084STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
Damien George0823c1b2016-09-01 15:07:20 +100085 mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
86 mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
Damien George5bb28c72016-10-03 12:39:31 +110087 spi_p->transfer(s, len, src, dest);
Damien George0823c1b2016-09-01 15:07:20 +100088}
89
90STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
Damien George0823c1b2016-09-01 15:07:20 +100091 vstr_t vstr;
92 vstr_init_len(&vstr, mp_obj_get_int(args[1]));
Damien George5bb28c72016-10-03 12:39:31 +110093 memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
94 mp_machine_spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf);
Damien George0823c1b2016-09-01 15:07:20 +100095 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
96}
97MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
98
99STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
100 mp_buffer_info_t bufinfo;
101 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
Damien George5bb28c72016-10-03 12:39:31 +1100102 memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
103 mp_machine_spi_transfer(args[0], bufinfo.len, bufinfo.buf, bufinfo.buf);
Damien George0823c1b2016-09-01 15:07:20 +1000104 return mp_const_none;
105}
106MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
107
108STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
109 mp_buffer_info_t src;
110 mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
Damien George5bb28c72016-10-03 12:39:31 +1100111 mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, NULL);
Damien George0823c1b2016-09-01 15:07:20 +1000112 return mp_const_none;
113}
114MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
115
116STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
117 mp_buffer_info_t src;
118 mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
119 mp_buffer_info_t dest;
120 mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
121 if (src.len != dest.len) {
Damien George5bb28c72016-10-03 12:39:31 +1100122 mp_raise_ValueError("buffers must be the same length");
Damien George0823c1b2016-09-01 15:07:20 +1000123 }
Damien George5bb28c72016-10-03 12:39:31 +1100124 mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
Damien George0823c1b2016-09-01 15:07:20 +1000125 return mp_const_none;
126}
127MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
128
Damien George1eb3c662016-12-08 13:47:01 +1100129STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
130 { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
131 { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
132 { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
133 { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
134 { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
135 { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
136
137 { MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
138 { MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },
139};
140
141MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
142
143/******************************************************************************/
144// Implementation of soft SPI
145
146STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
Damien George58ebeca2018-03-09 17:25:58 +1100147 #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
148 if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
149 return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
Damien George1eb3c662016-12-08 13:47:01 +1100150 } else
151 #endif
152 {
153 return 500000 / delay_half;
154 }
155}
156
157STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
Damien George58ebeca2018-03-09 17:25:58 +1100158 #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
159 if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE) {
160 return MICROPY_HW_SOFTSPI_MIN_DELAY;
Damien George1eb3c662016-12-08 13:47:01 +1100161 } else
162 #endif
163 {
164 uint32_t delay_half = 500000 / baudrate;
165 // round delay_half up so that: actual_baudrate <= requested_baudrate
166 if (500000 % baudrate != 0) {
167 delay_half += 1;
168 }
169 return delay_half;
170 }
171}
172
173STATIC void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
174 mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
175 mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u,"
176 " sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
Damien George58ebeca2018-03-09 17:25:58 +1100177 baudrate_from_delay_half(self->spi.delay_half), self->spi.polarity, self->spi.phase,
178 mp_hal_pin_name(self->spi.sck), mp_hal_pin_name(self->spi.mosi), mp_hal_pin_name(self->spi.miso));
Damien George1eb3c662016-12-08 13:47:01 +1100179}
180
181STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
182 enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
183 static const mp_arg_t allowed_args[] = {
184 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
185 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
186 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
187 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
188 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
189 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
190 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
191 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
192 };
193 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
194 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
195
196 // create new object
197 mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
198 self->base.type = &mp_machine_soft_spi_type;
199
200 // set parameters
Damien George58ebeca2018-03-09 17:25:58 +1100201 self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
202 self->spi.polarity = args[ARG_polarity].u_int;
203 self->spi.phase = args[ARG_phase].u_int;
Damien George1eb3c662016-12-08 13:47:01 +1100204 if (args[ARG_bits].u_int != 8) {
205 mp_raise_ValueError("bits must be 8");
206 }
207 if (args[ARG_firstbit].u_int != MICROPY_PY_MACHINE_SPI_MSB) {
208 mp_raise_ValueError("firstbit must be MSB");
209 }
210 if (args[ARG_sck].u_obj == MP_OBJ_NULL
211 || args[ARG_mosi].u_obj == MP_OBJ_NULL
212 || args[ARG_miso].u_obj == MP_OBJ_NULL) {
213 mp_raise_ValueError("must specify all of sck/mosi/miso");
214 }
Damien George58ebeca2018-03-09 17:25:58 +1100215 self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
216 self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
217 self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
Damien George1eb3c662016-12-08 13:47:01 +1100218
Damien George58ebeca2018-03-09 17:25:58 +1100219 // configure bus
220 mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
Damien George1eb3c662016-12-08 13:47:01 +1100221
222 return MP_OBJ_FROM_PTR(self);
223}
224
225STATIC void mp_machine_soft_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
226 mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
227
228 enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
229 static const mp_arg_t allowed_args[] = {
230 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
231 { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
232 { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
233 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
234 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
235 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
236 };
237 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
238 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
239
240 if (args[ARG_baudrate].u_int != -1) {
Damien George58ebeca2018-03-09 17:25:58 +1100241 self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
Damien George1eb3c662016-12-08 13:47:01 +1100242 }
243 if (args[ARG_polarity].u_int != -1) {
Damien George58ebeca2018-03-09 17:25:58 +1100244 self->spi.polarity = args[ARG_polarity].u_int;
Damien George1eb3c662016-12-08 13:47:01 +1100245 }
246 if (args[ARG_phase].u_int != -1) {
Damien George58ebeca2018-03-09 17:25:58 +1100247 self->spi.phase = args[ARG_phase].u_int;
Damien George1eb3c662016-12-08 13:47:01 +1100248 }
249 if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
Damien George58ebeca2018-03-09 17:25:58 +1100250 self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
Damien George1eb3c662016-12-08 13:47:01 +1100251 }
252 if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
Damien George58ebeca2018-03-09 17:25:58 +1100253 self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
Damien George1eb3c662016-12-08 13:47:01 +1100254 }
255 if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
Damien George58ebeca2018-03-09 17:25:58 +1100256 self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
Damien George1eb3c662016-12-08 13:47:01 +1100257 }
258
Damien George58ebeca2018-03-09 17:25:58 +1100259 // configure bus
260 mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
261}
262
263STATIC void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
264 mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
265 mp_soft_spi_transfer(&self->spi, len, src, dest);
Damien George1eb3c662016-12-08 13:47:01 +1100266}
267
Damien George21d55272018-03-02 19:12:52 +1100268const mp_machine_spi_p_t mp_machine_soft_spi_p = {
Damien George1eb3c662016-12-08 13:47:01 +1100269 .init = mp_machine_soft_spi_init,
270 .deinit = NULL,
271 .transfer = mp_machine_soft_spi_transfer,
272};
273
274const mp_obj_type_t mp_machine_soft_spi_type = {
275 { &mp_type_type },
276 .name = MP_QSTR_SoftSPI,
277 .print = mp_machine_soft_spi_print,
278 .make_new = mp_machine_spi_make_new, // delegate to master constructor
279 .protocol = &mp_machine_soft_spi_p,
280 .locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict,
281};
282
Damien George0823c1b2016-09-01 15:07:20 +1000283#endif // MICROPY_PY_MACHINE_SPI