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 <string.h>
29
30#include "py/runtime.h"
31#include "extmod/machine_spi.h"
32
33#if MICROPY_PY_MACHINE_SPI
34
35// 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
41/******************************************************************************/
42// MicroPython bindings for generic machine.SPI
43
44STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
45 mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
46 mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol;
47 spi_p->init(s, n_args - 1, args + 1, kw_args);
48 return mp_const_none;
49}
50STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
51
52STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
53 mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self);
54 mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol;
55 if (spi_p->deinit != NULL) {
56 spi_p->deinit(s);
57 }
58 return mp_const_none;
59}
60STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
61
62STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
63 mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self);
64 mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol;
65 spi_p->transfer(s, len, src, dest);
66}
67
68STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
69 vstr_t vstr;
70 vstr_init_len(&vstr, mp_obj_get_int(args[1]));
71 memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
72 mp_machine_spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf);
73 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
74}
75MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
76
77STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
78 mp_buffer_info_t bufinfo;
79 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
80 memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
81 mp_machine_spi_transfer(args[0], bufinfo.len, bufinfo.buf, bufinfo.buf);
82 return mp_const_none;
83}
84MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
85
86STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
87 mp_buffer_info_t src;
88 mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
89 mp_machine_spi_transfer(self, src.len, (const uint8_t *)src.buf, NULL);
90 return mp_const_none;
91}
92MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
93
94STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
95 mp_buffer_info_t src;
96 mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
97 mp_buffer_info_t dest;
98 mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
99 if (src.len != dest.len) {
100 mp_raise_ValueError(MP_ERROR_TEXT("buffers must be the same length"));
101 }
102 mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
103 return mp_const_none;
104}
105MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
106
107STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
108 { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
109 { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
110 { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
111 { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
112 { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
113 { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
114
115 { MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
116 { MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },
117};
118
119MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
120
121/******************************************************************************/
122// Implementation of soft SPI
123
124STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
125 #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
126 if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
127 return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
128 } else
129 #endif
130 {
131 return 500000 / delay_half;
132 }
133}
134
135STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
136 #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
137 if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE) {
138 return MICROPY_HW_SOFTSPI_MIN_DELAY;
139 } else
140 #endif
141 {
142 uint32_t delay_half = 500000 / baudrate;
143 // round delay_half up so that: actual_baudrate <= requested_baudrate
144 if (500000 % baudrate != 0) {
145 delay_half += 1;
146 }
147 return delay_half;
148 }
149}
150
151STATIC void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
152 mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
153 mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u,"
154 " sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
155 baudrate_from_delay_half(self->spi.delay_half), self->spi.polarity, self->spi.phase,
156 mp_hal_pin_name(self->spi.sck), mp_hal_pin_name(self->spi.mosi), mp_hal_pin_name(self->spi.miso));
157}
158
159STATIC 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) {
160 enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
161 static const mp_arg_t allowed_args[] = {
162 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
163 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
164 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
165 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
166 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
167 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
168 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
169 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
170 };
171 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
172 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
173
174 // create new object
175 mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
176 self->base.type = &mp_machine_soft_spi_type;
177
178 // set parameters
179 self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
180 self->spi.polarity = args[ARG_polarity].u_int;
181 self->spi.phase = args[ARG_phase].u_int;
182 if (args[ARG_bits].u_int != 8) {
183 mp_raise_ValueError(MP_ERROR_TEXT("bits must be 8"));
184 }
185 if (args[ARG_firstbit].u_int != MICROPY_PY_MACHINE_SPI_MSB) {
186 mp_raise_ValueError(MP_ERROR_TEXT("firstbit must be MSB"));
187 }
188 if (args[ARG_sck].u_obj == MP_OBJ_NULL
189 || args[ARG_mosi].u_obj == MP_OBJ_NULL
190 || args[ARG_miso].u_obj == MP_OBJ_NULL) {
191 mp_raise_ValueError(MP_ERROR_TEXT("must specify all of sck/mosi/miso"));
192 }
193 self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
194 self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
195 self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
196
197 // configure bus
198 mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
199
200 return MP_OBJ_FROM_PTR(self);
201}
202
203STATIC 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) {
204 mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t *)self_in;
205
206 enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
207 static const mp_arg_t allowed_args[] = {
208 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
209 { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
210 { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
211 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
212 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
213 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
214 };
215 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
216 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
217
218 if (args[ARG_baudrate].u_int != -1) {
219 self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
220 }
221 if (args[ARG_polarity].u_int != -1) {
222 self->spi.polarity = args[ARG_polarity].u_int;
223 }
224 if (args[ARG_phase].u_int != -1) {
225 self->spi.phase = args[ARG_phase].u_int;
226 }
227 if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
228 self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
229 }
230 if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
231 self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
232 }
233 if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
234 self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
235 }
236
237 // configure bus
238 mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
239}
240
241STATIC void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
242 mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t *)self_in;
243 mp_soft_spi_transfer(&self->spi, len, src, dest);
244}
245
246const mp_machine_spi_p_t mp_machine_soft_spi_p = {
247 .init = mp_machine_soft_spi_init,
248 .deinit = NULL,
249 .transfer = mp_machine_soft_spi_transfer,
250};
251
252const mp_obj_type_t mp_machine_soft_spi_type = {
253 { &mp_type_type },
254 .name = MP_QSTR_SoftSPI,
255 .print = mp_machine_soft_spi_print,
256 .make_new = mp_machine_soft_spi_make_new,
257 .protocol = &mp_machine_soft_spi_p,
258 .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict,
259};
260
261#endif // MICROPY_PY_MACHINE_SPI
262