blob: 560d19a5aa5e775e383ff71bd63a938267f66847 [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
Damien Georgebc08c882016-12-06 12:20:10 +1100189
Damien Georged82f3442019-01-23 23:40:06 +1100190 // Select DMA channel based on the hardware SPI host
191 int dma_chan = 0;
192 if (self->host == HSPI_HOST) {
193 dma_chan = 1;
194 } else if (self->host == VSPI_HOST) {
195 dma_chan = 2;
196 }
197
198 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100199 switch (ret) {
200 case ESP_ERR_INVALID_ARG:
201 mp_raise_msg(&mp_type_OSError, "invalid configuration");
202 return;
203
204 case ESP_ERR_INVALID_STATE:
205 mp_raise_msg(&mp_type_OSError, "SPI device already in use");
206 return;
207 }
208
209 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
210 switch (ret) {
211 case ESP_ERR_INVALID_ARG:
212 mp_raise_msg(&mp_type_OSError, "invalid configuration");
213 spi_bus_free(self->host);
214 return;
215
216 case ESP_ERR_NO_MEM:
217 mp_raise_msg(&mp_type_OSError, "out of memory");
218 spi_bus_free(self->host);
219 return;
220
221 case ESP_ERR_NOT_FOUND:
222 mp_raise_msg(&mp_type_OSError, "no free slots");
223 spi_bus_free(self->host);
224 return;
225 }
226 self->state = MACHINE_HW_SPI_STATE_INIT;
227}
228
229STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
230 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *) self_in;
231 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
232 self->state = MACHINE_HW_SPI_STATE_DEINIT;
233 machine_hw_spi_deinit_internal(self);
234 }
235}
236
237STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
238 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
239
Damien Georgebc08c882016-12-06 12:20:10 +1100240 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
241 mp_raise_msg(&mp_type_OSError, "transfer on deinitialized SPI");
242 return;
243 }
244
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800245 struct spi_transaction_t transaction = { 0 };
Damien Georgebc08c882016-12-06 12:20:10 +1100246
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800247 // Round to nearest whole set of bits
248 int bits_to_send = len * 8 / self->bits * self->bits;
249
250
251 if (len <= 4) {
Damien Georgebc08c882016-12-06 12:20:10 +1100252 if (src != NULL) {
253 memcpy(&transaction.tx_data, src, len);
254 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800255
256 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
257 transaction.length = bits_to_send;
258 spi_device_transmit(self->spi, &transaction);
259
260 if (dest != NULL) {
261 memcpy(dest, &transaction.rx_data, len);
262 }
Damien Georgebc08c882016-12-06 12:20:10 +1100263 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800264 int offset = 0;
265 int bits_remaining = bits_to_send;
Damien Georgebc08c882016-12-06 12:20:10 +1100266
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800267 while (bits_remaining) {
268 memset(&transaction, 0, sizeof(transaction));
Damien Georgebc08c882016-12-06 12:20:10 +1100269
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800270 transaction.length =
271 bits_remaining > MP_HW_SPI_MAX_XFER_BITS ? MP_HW_SPI_MAX_XFER_BITS : bits_remaining;
272
273 if (src != NULL) {
274 transaction.tx_buffer = src + offset;
275 }
276 if (dest != NULL) {
277 transaction.rx_buffer = dest + offset;
278 }
279
280 spi_device_transmit(self->spi, &transaction);
281 bits_remaining -= transaction.length;
282
283 // doesn't need ceil(); loop ends when bits_remaining is 0
284 offset += transaction.length / 8;
285 }
Damien Georgebc08c882016-12-06 12:20:10 +1100286 }
287}
288
289/******************************************************************************/
290// MicroPython bindings for hw_spi
291
292STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
293 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
294 mp_printf(print, "SPI(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d)",
295 self->host, self->baudrate, self->polarity,
296 self->phase, self->bits, self->firstbit,
297 self->sck, self->mosi, self->miso);
298}
299
300STATIC 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) {
301 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *) self_in;
302
303 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
304 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100305 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100306 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
307 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
308 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
309 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
310 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
311 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
312 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
313 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
314 };
315
316 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
317 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
318 allowed_args, args);
319 int8_t sck, mosi, miso;
320
321 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
322 sck = -2;
323 } else if (args[ARG_sck].u_obj == mp_const_none) {
324 sck = -1;
325 } else {
326 sck = machine_pin_get_id(args[ARG_sck].u_obj);
327 }
328
329 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
330 miso = -2;
331 } else if (args[ARG_miso].u_obj == mp_const_none) {
332 miso = -1;
333 } else {
334 miso = machine_pin_get_id(args[ARG_miso].u_obj);
335 }
336
337 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
338 mosi = -2;
339 } else if (args[ARG_mosi].u_obj == mp_const_none) {
340 mosi = -1;
341 } else {
342 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
343 }
344
345 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
346 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
347 args[ARG_firstbit].u_int, sck, mosi, miso);
348}
349
350mp_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) {
351 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
352 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100353 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100354 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
355 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
356 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
357 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
358 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
359 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
360 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
361 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
362 };
363 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
364 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
365
366 machine_hw_spi_obj_t *self = m_new_obj(machine_hw_spi_obj_t);
367 self->base.type = &machine_hw_spi_type;
368
369 machine_hw_spi_init_internal(
370 self,
371 args[ARG_id].u_int,
372 args[ARG_baudrate].u_int,
373 args[ARG_polarity].u_int,
374 args[ARG_phase].u_int,
375 args[ARG_bits].u_int,
376 args[ARG_firstbit].u_int,
377 args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj),
378 args[ARG_mosi].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_mosi].u_obj),
379 args[ARG_miso].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_miso].u_obj));
380
381 return MP_OBJ_FROM_PTR(self);
382}
383
384STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
385 .init = machine_hw_spi_init,
386 .deinit = machine_hw_spi_deinit,
387 .transfer = machine_hw_spi_transfer,
388};
389
390const mp_obj_type_t machine_hw_spi_type = {
391 { &mp_type_type },
392 .name = MP_QSTR_SPI,
393 .print = machine_hw_spi_print,
394 .make_new = machine_hw_spi_make_new,
395 .protocol = &machine_hw_spi_p,
396 .locals_dict = (mp_obj_dict_t *) &mp_machine_spi_locals_dict,
397};