blob: d011ce53e37d5f66ced12aebd05cdfb0749568c3 [file] [log] [blame]
Damien Georgebc08c882016-12-06 12:20:10 +11001/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
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 "py/runtime.h"
32#include "py/stream.h"
33#include "py/mphal.h"
34#include "extmod/machine_spi.h"
35#include "modmachine.h"
36
37#include "driver/spi_master.h"
38
Eric Poulsen9123c8d2017-11-28 18:48:30 -080039#define MP_HW_SPI_MAX_XFER_BYTES (4092)
40#define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8
41
Damien Georgebc08c882016-12-06 12:20:10 +110042typedef struct _machine_hw_spi_obj_t {
43 mp_obj_base_t base;
44 spi_host_device_t host;
45 uint32_t baudrate;
46 uint8_t polarity;
47 uint8_t phase;
48 uint8_t bits;
49 uint8_t firstbit;
50 int8_t sck;
51 int8_t mosi;
52 int8_t miso;
53 spi_device_handle_t spi;
54 enum {
55 MACHINE_HW_SPI_STATE_NONE,
56 MACHINE_HW_SPI_STATE_INIT,
57 MACHINE_HW_SPI_STATE_DEINIT
58 } state;
59} machine_hw_spi_obj_t;
60
61STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
62 switch (spi_bus_remove_device(self->spi)) {
63 case ESP_ERR_INVALID_ARG:
64 mp_raise_msg(&mp_type_OSError, "invalid configuration");
65 return;
66
67 case ESP_ERR_INVALID_STATE:
68 mp_raise_msg(&mp_type_OSError, "SPI device already freed");
69 return;
70 }
71
72 switch (spi_bus_free(self->host)) {
73 case ESP_ERR_INVALID_ARG:
74 mp_raise_msg(&mp_type_OSError, "invalid configuration");
75 return;
76
77 case ESP_ERR_INVALID_STATE:
78 mp_raise_msg(&mp_type_OSError, "SPI bus already freed");
79 return;
80 }
81
82 int8_t pins[3] = {self->miso, self->mosi, self->sck};
83
84 for (int i = 0; i < 3; i++) {
85 if (pins[i] != -1) {
86 gpio_pad_select_gpio(pins[i]);
87 gpio_matrix_out(pins[i], SIG_GPIO_OUT_IDX, false, false);
88 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
89 }
90 }
91}
92
93STATIC void machine_hw_spi_init_internal(
94 machine_hw_spi_obj_t *self,
95 int8_t host,
96 int32_t baudrate,
97 int8_t polarity,
98 int8_t phase,
99 int8_t bits,
100 int8_t firstbit,
101 int8_t sck,
102 int8_t mosi,
103 int8_t miso) {
104
105 // if we're not initialized, then we're
106 // implicitly 'changed', since this is the init routine
107 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
108
109 esp_err_t ret;
110
111 machine_hw_spi_obj_t old_self = *self;
112
113 if (host != -1 && host != self->host) {
114 self->host = host;
115 changed = true;
116 }
117
118 if (baudrate != -1 && baudrate != self->baudrate) {
119 self->baudrate = baudrate;
120 changed = true;
121 }
122
123 if (polarity != -1 && polarity != self->polarity) {
124 self->polarity = polarity;
125 changed = true;
126 }
127
128 if (phase != -1 && phase != self->phase) {
129 self->phase = phase;
130 changed = true;
131 }
132
133 if (bits != -1 && bits != self->bits) {
134 self->bits = bits;
135 changed = true;
136 }
137
138 if (firstbit != -1 && firstbit != self->firstbit) {
139 self->firstbit = firstbit;
140 changed = true;
141 }
142
143 if (sck != -2 && sck != self->sck) {
144 self->sck = sck;
145 changed = true;
146 }
147
148 if (mosi != -2 && mosi != self->mosi) {
149 self->mosi = mosi;
150 changed = true;
151 }
152
153 if (miso != -2 && miso != self->miso) {
154 self->miso = miso;
155 changed = true;
156 }
157
158 if (self->host != HSPI_HOST && self->host != VSPI_HOST) {
159 mp_raise_ValueError("SPI ID must be either HSPI(1) or VSPI(2)");
160 }
161
162 if (changed) {
163 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
164 self->state = MACHINE_HW_SPI_STATE_DEINIT;
165 machine_hw_spi_deinit_internal(&old_self);
166 }
167 } else {
168 return; // no changes
169 }
170
171 spi_bus_config_t buscfg = {
172 .miso_io_num = self->miso,
173 .mosi_io_num = self->mosi,
174 .sclk_io_num = self->sck,
175 .quadwp_io_num = -1,
176 .quadhd_io_num = -1
177 };
178
179 spi_device_interface_config_t devcfg = {
180 .clock_speed_hz = self->baudrate,
181 .mode = self->phase | (self->polarity << 1),
182 .spics_io_num = -1, // No CS pin
183 .queue_size = 1,
184 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
185 .pre_cb = NULL
186 };
187
188 //Initialize the SPI bus
189 // FIXME: Does the DMA matter? There are two
190
191 ret = spi_bus_initialize(self->host, &buscfg, 1);
192 switch (ret) {
193 case ESP_ERR_INVALID_ARG:
194 mp_raise_msg(&mp_type_OSError, "invalid configuration");
195 return;
196
197 case ESP_ERR_INVALID_STATE:
198 mp_raise_msg(&mp_type_OSError, "SPI device already in use");
199 return;
200 }
201
202 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
203 switch (ret) {
204 case ESP_ERR_INVALID_ARG:
205 mp_raise_msg(&mp_type_OSError, "invalid configuration");
206 spi_bus_free(self->host);
207 return;
208
209 case ESP_ERR_NO_MEM:
210 mp_raise_msg(&mp_type_OSError, "out of memory");
211 spi_bus_free(self->host);
212 return;
213
214 case ESP_ERR_NOT_FOUND:
215 mp_raise_msg(&mp_type_OSError, "no free slots");
216 spi_bus_free(self->host);
217 return;
218 }
219 self->state = MACHINE_HW_SPI_STATE_INIT;
220}
221
222STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
223 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *) self_in;
224 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
225 self->state = MACHINE_HW_SPI_STATE_DEINIT;
226 machine_hw_spi_deinit_internal(self);
227 }
228}
229
230STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
231 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
232
Damien Georgebc08c882016-12-06 12:20:10 +1100233 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
234 mp_raise_msg(&mp_type_OSError, "transfer on deinitialized SPI");
235 return;
236 }
237
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800238 struct spi_transaction_t transaction = { 0 };
Damien Georgebc08c882016-12-06 12:20:10 +1100239
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800240 // Round to nearest whole set of bits
241 int bits_to_send = len * 8 / self->bits * self->bits;
242
243
244 if (len <= 4) {
Damien Georgebc08c882016-12-06 12:20:10 +1100245 if (src != NULL) {
246 memcpy(&transaction.tx_data, src, len);
247 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800248
249 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
250 transaction.length = bits_to_send;
251 spi_device_transmit(self->spi, &transaction);
252
253 if (dest != NULL) {
254 memcpy(dest, &transaction.rx_data, len);
255 }
Damien Georgebc08c882016-12-06 12:20:10 +1100256 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800257 int offset = 0;
258 int bits_remaining = bits_to_send;
Damien Georgebc08c882016-12-06 12:20:10 +1100259
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800260 while (bits_remaining) {
261 memset(&transaction, 0, sizeof(transaction));
Damien Georgebc08c882016-12-06 12:20:10 +1100262
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800263 transaction.length =
264 bits_remaining > MP_HW_SPI_MAX_XFER_BITS ? MP_HW_SPI_MAX_XFER_BITS : bits_remaining;
265
266 if (src != NULL) {
267 transaction.tx_buffer = src + offset;
268 }
269 if (dest != NULL) {
270 transaction.rx_buffer = dest + offset;
271 }
272
273 spi_device_transmit(self->spi, &transaction);
274 bits_remaining -= transaction.length;
275
276 // doesn't need ceil(); loop ends when bits_remaining is 0
277 offset += transaction.length / 8;
278 }
Damien Georgebc08c882016-12-06 12:20:10 +1100279 }
280}
281
282/******************************************************************************/
283// MicroPython bindings for hw_spi
284
285STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
286 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
287 mp_printf(print, "SPI(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d)",
288 self->host, self->baudrate, self->polarity,
289 self->phase, self->bits, self->firstbit,
290 self->sck, self->mosi, self->miso);
291}
292
293STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
294 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *) self_in;
295
296 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
297 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100298 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100299 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
300 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
301 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
302 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
303 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
304 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
305 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
306 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
307 };
308
309 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
310 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
311 allowed_args, args);
312 int8_t sck, mosi, miso;
313
314 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
315 sck = -2;
316 } else if (args[ARG_sck].u_obj == mp_const_none) {
317 sck = -1;
318 } else {
319 sck = machine_pin_get_id(args[ARG_sck].u_obj);
320 }
321
322 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
323 miso = -2;
324 } else if (args[ARG_miso].u_obj == mp_const_none) {
325 miso = -1;
326 } else {
327 miso = machine_pin_get_id(args[ARG_miso].u_obj);
328 }
329
330 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
331 mosi = -2;
332 } else if (args[ARG_mosi].u_obj == mp_const_none) {
333 mosi = -1;
334 } else {
335 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
336 }
337
338 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
339 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
340 args[ARG_firstbit].u_int, sck, mosi, miso);
341}
342
343mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
344 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
345 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100346 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100347 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
348 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
349 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
350 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
351 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
352 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
353 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
354 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
355 };
356 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
357 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
358
359 machine_hw_spi_obj_t *self = m_new_obj(machine_hw_spi_obj_t);
360 self->base.type = &machine_hw_spi_type;
361
362 machine_hw_spi_init_internal(
363 self,
364 args[ARG_id].u_int,
365 args[ARG_baudrate].u_int,
366 args[ARG_polarity].u_int,
367 args[ARG_phase].u_int,
368 args[ARG_bits].u_int,
369 args[ARG_firstbit].u_int,
370 args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj),
371 args[ARG_mosi].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_mosi].u_obj),
372 args[ARG_miso].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_miso].u_obj));
373
374 return MP_OBJ_FROM_PTR(self);
375}
376
377STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
378 .init = machine_hw_spi_init,
379 .deinit = machine_hw_spi_deinit,
380 .transfer = machine_hw_spi_transfer,
381};
382
383const mp_obj_type_t machine_hw_spi_type = {
384 { &mp_type_type },
385 .name = MP_QSTR_SPI,
386 .print = machine_hw_spi_print,
387 .make_new = machine_hw_spi_make_new,
388 .protocol = &machine_hw_spi_p,
389 .locals_dict = (mp_obj_dict_t *) &mp_machine_spi_locals_dict,
390};