blob: e7554e38092d36de3e97555c70a19718796357df [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
Damien Georgeda72bb62019-01-23 23:47:36 +110061// Static objects mapping to HSPI and VSPI hardware peripherals
62STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2];
63
Damien Georgebc08c882016-12-06 12:20:10 +110064STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
65 switch (spi_bus_remove_device(self->spi)) {
66 case ESP_ERR_INVALID_ARG:
67 mp_raise_msg(&mp_type_OSError, "invalid configuration");
68 return;
69
70 case ESP_ERR_INVALID_STATE:
71 mp_raise_msg(&mp_type_OSError, "SPI device already freed");
72 return;
73 }
74
75 switch (spi_bus_free(self->host)) {
76 case ESP_ERR_INVALID_ARG:
77 mp_raise_msg(&mp_type_OSError, "invalid configuration");
78 return;
79
80 case ESP_ERR_INVALID_STATE:
81 mp_raise_msg(&mp_type_OSError, "SPI bus already freed");
82 return;
83 }
84
85 int8_t pins[3] = {self->miso, self->mosi, self->sck};
86
87 for (int i = 0; i < 3; i++) {
88 if (pins[i] != -1) {
89 gpio_pad_select_gpio(pins[i]);
90 gpio_matrix_out(pins[i], SIG_GPIO_OUT_IDX, false, false);
91 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
92 }
93 }
94}
95
96STATIC void machine_hw_spi_init_internal(
Damien George69661f32020-02-27 15:36:53 +110097 machine_hw_spi_obj_t *self,
98 int8_t host,
99 int32_t baudrate,
100 int8_t polarity,
101 int8_t phase,
102 int8_t bits,
103 int8_t firstbit,
104 int8_t sck,
105 int8_t mosi,
106 int8_t miso) {
Damien Georgebc08c882016-12-06 12:20:10 +1100107
108 // if we're not initialized, then we're
109 // implicitly 'changed', since this is the init routine
110 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
111
112 esp_err_t ret;
113
114 machine_hw_spi_obj_t old_self = *self;
115
116 if (host != -1 && host != self->host) {
117 self->host = host;
118 changed = true;
119 }
120
121 if (baudrate != -1 && baudrate != self->baudrate) {
122 self->baudrate = baudrate;
123 changed = true;
124 }
125
126 if (polarity != -1 && polarity != self->polarity) {
127 self->polarity = polarity;
128 changed = true;
129 }
130
131 if (phase != -1 && phase != self->phase) {
Damien George69661f32020-02-27 15:36:53 +1100132 self->phase = phase;
Damien Georgebc08c882016-12-06 12:20:10 +1100133 changed = true;
134 }
135
136 if (bits != -1 && bits != self->bits) {
137 self->bits = bits;
138 changed = true;
139 }
140
141 if (firstbit != -1 && firstbit != self->firstbit) {
142 self->firstbit = firstbit;
143 changed = true;
144 }
145
146 if (sck != -2 && sck != self->sck) {
147 self->sck = sck;
148 changed = true;
149 }
150
151 if (mosi != -2 && mosi != self->mosi) {
152 self->mosi = mosi;
153 changed = true;
154 }
155
156 if (miso != -2 && miso != self->miso) {
157 self->miso = miso;
158 changed = true;
159 }
160
161 if (self->host != HSPI_HOST && self->host != VSPI_HOST) {
162 mp_raise_ValueError("SPI ID must be either HSPI(1) or VSPI(2)");
163 }
164
165 if (changed) {
166 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
167 self->state = MACHINE_HW_SPI_STATE_DEINIT;
168 machine_hw_spi_deinit_internal(&old_self);
169 }
170 } else {
171 return; // no changes
172 }
173
174 spi_bus_config_t buscfg = {
175 .miso_io_num = self->miso,
176 .mosi_io_num = self->mosi,
177 .sclk_io_num = self->sck,
178 .quadwp_io_num = -1,
179 .quadhd_io_num = -1
180 };
181
182 spi_device_interface_config_t devcfg = {
183 .clock_speed_hz = self->baudrate,
184 .mode = self->phase | (self->polarity << 1),
185 .spics_io_num = -1, // No CS pin
186 .queue_size = 1,
187 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
188 .pre_cb = NULL
189 };
190
191 //Initialize the SPI bus
Damien Georgebc08c882016-12-06 12:20:10 +1100192
Damien Georged82f3442019-01-23 23:40:06 +1100193 // Select DMA channel based on the hardware SPI host
194 int dma_chan = 0;
195 if (self->host == HSPI_HOST) {
196 dma_chan = 1;
197 } else if (self->host == VSPI_HOST) {
198 dma_chan = 2;
199 }
200
201 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100202 switch (ret) {
203 case ESP_ERR_INVALID_ARG:
204 mp_raise_msg(&mp_type_OSError, "invalid configuration");
205 return;
206
207 case ESP_ERR_INVALID_STATE:
Mike Teachmanf3011702019-10-22 10:25:37 -0700208 mp_raise_msg(&mp_type_OSError, "SPI host already in use");
Damien Georgebc08c882016-12-06 12:20:10 +1100209 return;
210 }
211
212 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
213 switch (ret) {
214 case ESP_ERR_INVALID_ARG:
215 mp_raise_msg(&mp_type_OSError, "invalid configuration");
216 spi_bus_free(self->host);
217 return;
218
219 case ESP_ERR_NO_MEM:
220 mp_raise_msg(&mp_type_OSError, "out of memory");
221 spi_bus_free(self->host);
222 return;
223
224 case ESP_ERR_NOT_FOUND:
225 mp_raise_msg(&mp_type_OSError, "no free slots");
226 spi_bus_free(self->host);
227 return;
228 }
229 self->state = MACHINE_HW_SPI_STATE_INIT;
230}
231
232STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
Damien George1a3e3862020-03-27 00:35:04 +1100233 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100234 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
235 self->state = MACHINE_HW_SPI_STATE_DEINIT;
236 machine_hw_spi_deinit_internal(self);
237 }
238}
239
240STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
241 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
242
Damien Georgebc08c882016-12-06 12:20:10 +1100243 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
244 mp_raise_msg(&mp_type_OSError, "transfer on deinitialized SPI");
245 return;
246 }
247
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800248 struct spi_transaction_t transaction = { 0 };
Damien Georgebc08c882016-12-06 12:20:10 +1100249
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800250 // Round to nearest whole set of bits
251 int bits_to_send = len * 8 / self->bits * self->bits;
252
253
254 if (len <= 4) {
Damien Georgebc08c882016-12-06 12:20:10 +1100255 if (src != NULL) {
256 memcpy(&transaction.tx_data, src, len);
257 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800258
259 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
260 transaction.length = bits_to_send;
261 spi_device_transmit(self->spi, &transaction);
262
263 if (dest != NULL) {
264 memcpy(dest, &transaction.rx_data, len);
265 }
Damien Georgebc08c882016-12-06 12:20:10 +1100266 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800267 int offset = 0;
268 int bits_remaining = bits_to_send;
Damien Georgebc08c882016-12-06 12:20:10 +1100269
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800270 while (bits_remaining) {
271 memset(&transaction, 0, sizeof(transaction));
Damien Georgebc08c882016-12-06 12:20:10 +1100272
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800273 transaction.length =
274 bits_remaining > MP_HW_SPI_MAX_XFER_BITS ? MP_HW_SPI_MAX_XFER_BITS : bits_remaining;
275
276 if (src != NULL) {
277 transaction.tx_buffer = src + offset;
278 }
279 if (dest != NULL) {
280 transaction.rx_buffer = dest + offset;
281 }
282
283 spi_device_transmit(self->spi, &transaction);
284 bits_remaining -= transaction.length;
285
286 // doesn't need ceil(); loop ends when bits_remaining is 0
287 offset += transaction.length / 8;
288 }
Damien Georgebc08c882016-12-06 12:20:10 +1100289 }
290}
291
292/******************************************************************************/
293// MicroPython bindings for hw_spi
294
295STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
296 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
297 mp_printf(print, "SPI(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d)",
Damien George69661f32020-02-27 15:36:53 +1100298 self->host, self->baudrate, self->polarity,
299 self->phase, self->bits, self->firstbit,
300 self->sck, self->mosi, self->miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100301}
302
303STATIC 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) {
Damien George1a3e3862020-03-27 00:35:04 +1100304 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100305
306 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
307 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100308 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100309 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
310 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
311 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
312 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
313 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
314 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
315 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
316 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
317 };
318
319 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
320 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
Damien George69661f32020-02-27 15:36:53 +1100321 allowed_args, args);
Damien Georgebc08c882016-12-06 12:20:10 +1100322 int8_t sck, mosi, miso;
323
324 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
325 sck = -2;
326 } else if (args[ARG_sck].u_obj == mp_const_none) {
327 sck = -1;
328 } else {
329 sck = machine_pin_get_id(args[ARG_sck].u_obj);
330 }
331
332 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
333 miso = -2;
334 } else if (args[ARG_miso].u_obj == mp_const_none) {
335 miso = -1;
336 } else {
337 miso = machine_pin_get_id(args[ARG_miso].u_obj);
338 }
339
340 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
341 mosi = -2;
342 } else if (args[ARG_mosi].u_obj == mp_const_none) {
343 mosi = -1;
344 } else {
345 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
346 }
347
348 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
Damien George69661f32020-02-27 15:36:53 +1100349 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
350 args[ARG_firstbit].u_int, sck, mosi, miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100351}
352
353mp_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) {
354 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
355 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100356 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100357 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
358 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
359 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
360 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
361 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
362 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
363 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
364 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
365 };
366 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
367 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
368
Damien Georgeda72bb62019-01-23 23:47:36 +1100369 machine_hw_spi_obj_t *self;
370 if (args[ARG_id].u_int == HSPI_HOST) {
371 self = &machine_hw_spi_obj[0];
372 } else {
373 self = &machine_hw_spi_obj[1];
374 }
Damien Georgebc08c882016-12-06 12:20:10 +1100375 self->base.type = &machine_hw_spi_type;
376
377 machine_hw_spi_init_internal(
378 self,
379 args[ARG_id].u_int,
380 args[ARG_baudrate].u_int,
381 args[ARG_polarity].u_int,
382 args[ARG_phase].u_int,
383 args[ARG_bits].u_int,
384 args[ARG_firstbit].u_int,
385 args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj),
386 args[ARG_mosi].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_mosi].u_obj),
387 args[ARG_miso].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_miso].u_obj));
388
389 return MP_OBJ_FROM_PTR(self);
390}
391
392STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
393 .init = machine_hw_spi_init,
394 .deinit = machine_hw_spi_deinit,
395 .transfer = machine_hw_spi_transfer,
396};
397
398const mp_obj_type_t machine_hw_spi_type = {
399 { &mp_type_type },
400 .name = MP_QSTR_SPI,
401 .print = machine_hw_spi_print,
402 .make_new = machine_hw_spi_make_new,
403 .protocol = &machine_hw_spi_p,
Damien George1a3e3862020-03-27 00:35:04 +1100404 .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict,
Damien Georgebc08c882016-12-06 12:20:10 +1100405};