blob: 27afa60afdac907195e1e21a00ce60eb9cb7d10e [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
Damien Georgeb24fcd72021-03-11 13:32:00 +110039// Default pins for SPI(1), can be overridden by a board
40#ifndef MICROPY_HW_SPI1_SCK
41#define MICROPY_HW_SPI1_SCK (14)
42#define MICROPY_HW_SPI1_MOSI (13)
43#define MICROPY_HW_SPI1_MISO (12)
44#endif
45
46// Default pins for SPI(2), can be overridden by a board
47#ifndef MICROPY_HW_SPI2_SCK
48#define MICROPY_HW_SPI2_SCK (18)
49#define MICROPY_HW_SPI2_MOSI (23)
50#define MICROPY_HW_SPI2_MISO (19)
51#endif
52
Eric Poulsen9123c8d2017-11-28 18:48:30 -080053#define MP_HW_SPI_MAX_XFER_BYTES (4092)
54#define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8
55
Damien George68235142021-07-18 12:11:21 +100056#if CONFIG_IDF_TARGET_ESP32C3
57#define HSPI_HOST SPI2_HOST
Damien George54d33b22021-09-16 22:16:36 +100058#elif CONFIG_IDF_TARGET_ESP32S3
59#define HSPI_HOST SPI3_HOST
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +100060#define FSPI_HOST SPI2_HOST
Damien George68235142021-07-18 12:11:21 +100061#endif
62
Damien Georgeb24fcd72021-03-11 13:32:00 +110063typedef struct _machine_hw_spi_default_pins_t {
64 int8_t sck;
65 int8_t mosi;
66 int8_t miso;
67} machine_hw_spi_default_pins_t;
68
Damien Georgebc08c882016-12-06 12:20:10 +110069typedef struct _machine_hw_spi_obj_t {
70 mp_obj_base_t base;
71 spi_host_device_t host;
72 uint32_t baudrate;
73 uint8_t polarity;
74 uint8_t phase;
75 uint8_t bits;
76 uint8_t firstbit;
77 int8_t sck;
78 int8_t mosi;
79 int8_t miso;
80 spi_device_handle_t spi;
81 enum {
82 MACHINE_HW_SPI_STATE_NONE,
83 MACHINE_HW_SPI_STATE_INIT,
84 MACHINE_HW_SPI_STATE_DEINIT
85 } state;
86} machine_hw_spi_obj_t;
87
Damien Georgeb24fcd72021-03-11 13:32:00 +110088// Default pin mappings for the hardware SPI instances
89STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = {
90 { .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
91 { .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
92};
93
Damien Georgeda72bb62019-01-23 23:47:36 +110094// Static objects mapping to HSPI and VSPI hardware peripherals
95STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2];
96
Damien Georgebc08c882016-12-06 12:20:10 +110097STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
98 switch (spi_bus_remove_device(self->spi)) {
99 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100100 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100101 return;
102
103 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100104 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI device already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100105 return;
106 }
107
108 switch (spi_bus_free(self->host)) {
109 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100110 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100111 return;
112
113 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100114 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100115 return;
116 }
117
118 int8_t pins[3] = {self->miso, self->mosi, self->sck};
119
120 for (int i = 0; i < 3; i++) {
121 if (pins[i] != -1) {
122 gpio_pad_select_gpio(pins[i]);
123 gpio_matrix_out(pins[i], SIG_GPIO_OUT_IDX, false, false);
124 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
125 }
126 }
127}
128
129STATIC void machine_hw_spi_init_internal(
Damien George69661f32020-02-27 15:36:53 +1100130 machine_hw_spi_obj_t *self,
131 int8_t host,
132 int32_t baudrate,
133 int8_t polarity,
134 int8_t phase,
135 int8_t bits,
136 int8_t firstbit,
137 int8_t sck,
138 int8_t mosi,
139 int8_t miso) {
Damien Georgebc08c882016-12-06 12:20:10 +1100140
141 // if we're not initialized, then we're
142 // implicitly 'changed', since this is the init routine
143 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
144
145 esp_err_t ret;
146
147 machine_hw_spi_obj_t old_self = *self;
148
149 if (host != -1 && host != self->host) {
150 self->host = host;
151 changed = true;
152 }
153
Jonathan Hoggeb3029c2021-07-12 11:35:25 +0100154 if (baudrate != -1) {
155 // calculate the actual clock frequency that the SPI peripheral can produce
156 baudrate = spi_get_actual_clock(APB_CLK_FREQ, baudrate, 0);
157 if (baudrate != self->baudrate) {
158 self->baudrate = baudrate;
159 changed = true;
160 }
Damien Georgebc08c882016-12-06 12:20:10 +1100161 }
162
163 if (polarity != -1 && polarity != self->polarity) {
164 self->polarity = polarity;
165 changed = true;
166 }
167
168 if (phase != -1 && phase != self->phase) {
Damien George69661f32020-02-27 15:36:53 +1100169 self->phase = phase;
Damien Georgebc08c882016-12-06 12:20:10 +1100170 changed = true;
171 }
172
173 if (bits != -1 && bits != self->bits) {
174 self->bits = bits;
175 changed = true;
176 }
177
178 if (firstbit != -1 && firstbit != self->firstbit) {
179 self->firstbit = firstbit;
180 changed = true;
181 }
182
183 if (sck != -2 && sck != self->sck) {
184 self->sck = sck;
185 changed = true;
186 }
187
188 if (mosi != -2 && mosi != self->mosi) {
189 self->mosi = mosi;
190 changed = true;
191 }
192
193 if (miso != -2 && miso != self->miso) {
194 self->miso = miso;
195 changed = true;
196 }
197
Damien George66a86a02021-02-18 21:24:34 +1100198 if (self->host != HSPI_HOST
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +1000199 #ifdef FSPI_HOST
200 && self->host != FSPI_HOST
201 #endif
Damien George66a86a02021-02-18 21:24:34 +1100202 #ifdef VSPI_HOST
203 && self->host != VSPI_HOST
204 #endif
205 ) {
206 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host);
Damien Georgebc08c882016-12-06 12:20:10 +1100207 }
208
209 if (changed) {
210 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
211 self->state = MACHINE_HW_SPI_STATE_DEINIT;
212 machine_hw_spi_deinit_internal(&old_self);
213 }
214 } else {
215 return; // no changes
216 }
217
218 spi_bus_config_t buscfg = {
219 .miso_io_num = self->miso,
220 .mosi_io_num = self->mosi,
221 .sclk_io_num = self->sck,
222 .quadwp_io_num = -1,
223 .quadhd_io_num = -1
224 };
225
226 spi_device_interface_config_t devcfg = {
227 .clock_speed_hz = self->baudrate,
228 .mode = self->phase | (self->polarity << 1),
229 .spics_io_num = -1, // No CS pin
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100230 .queue_size = 2,
Damien Georgebc08c882016-12-06 12:20:10 +1100231 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
232 .pre_cb = NULL
233 };
234
stijn84fa3312020-04-16 09:13:57 +0200235 // Initialize the SPI bus
Damien Georgebc08c882016-12-06 12:20:10 +1100236
Damien Georged82f3442019-01-23 23:40:06 +1100237 // Select DMA channel based on the hardware SPI host
238 int dma_chan = 0;
239 if (self->host == HSPI_HOST) {
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +1000240 #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
241 dma_chan = 3;
242 #else
Damien Georged82f3442019-01-23 23:40:06 +1100243 dma_chan = 1;
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +1000244 #endif
245 #ifdef FSPI_HOST
246 } else if (self->host == FSPI_HOST) {
247 dma_chan = 1;
248 #endif
Damien George66a86a02021-02-18 21:24:34 +1100249 #ifdef VSPI_HOST
Damien Georged82f3442019-01-23 23:40:06 +1100250 } else if (self->host == VSPI_HOST) {
251 dma_chan = 2;
Damien George66a86a02021-02-18 21:24:34 +1100252 #endif
Damien Georged82f3442019-01-23 23:40:06 +1100253 }
254
255 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100256 switch (ret) {
257 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100258 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100259 return;
260
261 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100262 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI host already in use"));
Damien Georgebc08c882016-12-06 12:20:10 +1100263 return;
264 }
265
266 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
267 switch (ret) {
268 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100269 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100270 spi_bus_free(self->host);
271 return;
272
273 case ESP_ERR_NO_MEM:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100274 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("out of memory"));
Damien Georgebc08c882016-12-06 12:20:10 +1100275 spi_bus_free(self->host);
276 return;
277
278 case ESP_ERR_NOT_FOUND:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100279 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("no free slots"));
Damien Georgebc08c882016-12-06 12:20:10 +1100280 spi_bus_free(self->host);
281 return;
282 }
283 self->state = MACHINE_HW_SPI_STATE_INIT;
284}
285
286STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
Damien George1a3e3862020-03-27 00:35:04 +1100287 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100288 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
289 self->state = MACHINE_HW_SPI_STATE_DEINIT;
290 machine_hw_spi_deinit_internal(self);
291 }
292}
293
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100294STATIC mp_uint_t gcd(mp_uint_t x, mp_uint_t y) {
295 while (x != y) {
296 if (x > y) {
297 x -= y;
298 } else {
299 y -= x;
300 }
301 }
302 return x;
303}
304
Damien Georgebc08c882016-12-06 12:20:10 +1100305STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
306 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
307
Damien Georgebc08c882016-12-06 12:20:10 +1100308 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100309 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("transfer on deinitialized SPI"));
Damien Georgebc08c882016-12-06 12:20:10 +1100310 return;
311 }
312
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800313 // Round to nearest whole set of bits
314 int bits_to_send = len * 8 / self->bits * self->bits;
315
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100316 if (!bits_to_send) {
317 mp_raise_ValueError(MP_ERROR_TEXT("buffer too short"));
318 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800319
320 if (len <= 4) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100321 spi_transaction_t transaction = { 0 };
322
Damien Georgebc08c882016-12-06 12:20:10 +1100323 if (src != NULL) {
324 memcpy(&transaction.tx_data, src, len);
325 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800326
327 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
328 transaction.length = bits_to_send;
329 spi_device_transmit(self->spi, &transaction);
330
331 if (dest != NULL) {
332 memcpy(dest, &transaction.rx_data, len);
333 }
Damien Georgebc08c882016-12-06 12:20:10 +1100334 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800335 int offset = 0;
336 int bits_remaining = bits_to_send;
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100337 int optimum_word_size = 8 * self->bits / gcd(8, self->bits);
338 int max_transaction_bits = MP_HW_SPI_MAX_XFER_BITS / optimum_word_size * optimum_word_size;
339 spi_transaction_t *transaction, *result, transactions[2];
340 int i = 0;
341
342 spi_device_acquire_bus(self->spi, portMAX_DELAY);
Damien Georgebc08c882016-12-06 12:20:10 +1100343
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800344 while (bits_remaining) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100345 transaction = transactions + i++ % 2;
346 memset(transaction, 0, sizeof(spi_transaction_t));
Damien Georgebc08c882016-12-06 12:20:10 +1100347
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100348 transaction->length =
349 bits_remaining > max_transaction_bits ? max_transaction_bits : bits_remaining;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800350
351 if (src != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100352 transaction->tx_buffer = src + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800353 }
354 if (dest != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100355 transaction->rx_buffer = dest + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800356 }
357
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100358 spi_device_queue_trans(self->spi, transaction, portMAX_DELAY);
359 bits_remaining -= transaction->length;
360
361 if (offset > 0) {
362 // wait for previously queued transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100363 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100364 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100365 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100366 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800367
368 // doesn't need ceil(); loop ends when bits_remaining is 0
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100369 offset += transaction->length / 8;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800370 }
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100371
372 // wait for last transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100373 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100374 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100375 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100376 spi_device_release_bus(self->spi);
Damien Georgebc08c882016-12-06 12:20:10 +1100377 }
378}
379
380/******************************************************************************/
381// MicroPython bindings for hw_spi
382
383STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
384 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
385 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 +1100386 self->host, self->baudrate, self->polarity,
387 self->phase, self->bits, self->firstbit,
388 self->sck, self->mosi, self->miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100389}
390
391STATIC 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 +1100392 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100393
394 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
395 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100396 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100397 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
398 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
399 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
400 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
401 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
402 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
403 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
404 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
405 };
406
407 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
408 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
Damien George69661f32020-02-27 15:36:53 +1100409 allowed_args, args);
Damien Georgebc08c882016-12-06 12:20:10 +1100410 int8_t sck, mosi, miso;
411
412 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
413 sck = -2;
414 } else if (args[ARG_sck].u_obj == mp_const_none) {
415 sck = -1;
416 } else {
417 sck = machine_pin_get_id(args[ARG_sck].u_obj);
418 }
419
420 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
421 miso = -2;
422 } else if (args[ARG_miso].u_obj == mp_const_none) {
423 miso = -1;
424 } else {
425 miso = machine_pin_get_id(args[ARG_miso].u_obj);
426 }
427
428 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
429 mosi = -2;
430 } else if (args[ARG_mosi].u_obj == mp_const_none) {
431 mosi = -1;
432 } else {
433 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
434 }
435
436 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
Damien George69661f32020-02-27 15:36:53 +1100437 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
438 args[ARG_firstbit].u_int, sck, mosi, miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100439}
440
441mp_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) {
Damien George71f3ade2020-09-25 14:47:34 +1000442 MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
443
Damien Georgebc08c882016-12-06 12:20:10 +1100444 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
445 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100446 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100447 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
448 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
449 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
450 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
451 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
452 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
453 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
454 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
455 };
456 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
457 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
458
Damien Georgeda72bb62019-01-23 23:47:36 +1100459 machine_hw_spi_obj_t *self;
Damien Georgeb24fcd72021-03-11 13:32:00 +1100460 const machine_hw_spi_default_pins_t *default_pins;
Tomas Vanek3305ec42021-12-01 18:34:39 +0100461 if (args[ARG_id].u_int == 1) { // SPI2_HOST which is FSPI_HOST on ESP32Sx, HSPI_HOST on others
Damien Georgeda72bb62019-01-23 23:47:36 +1100462 self = &machine_hw_spi_obj[0];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100463 default_pins = &machine_hw_spi_default_pins[0];
Damien Georgeda72bb62019-01-23 23:47:36 +1100464 } else {
465 self = &machine_hw_spi_obj[1];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100466 default_pins = &machine_hw_spi_default_pins[1];
Damien Georgeda72bb62019-01-23 23:47:36 +1100467 }
Damien Georgebc08c882016-12-06 12:20:10 +1100468 self->base.type = &machine_hw_spi_type;
469
Jonathan Hogg89945b12021-06-16 13:31:46 +0100470 int8_t sck, mosi, miso;
471
472 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
473 sck = default_pins->sck;
474 } else if (args[ARG_sck].u_obj == mp_const_none) {
475 sck = -1;
476 } else {
477 sck = machine_pin_get_id(args[ARG_sck].u_obj);
478 }
479
480 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
481 mosi = default_pins->mosi;
482 } else if (args[ARG_mosi].u_obj == mp_const_none) {
483 mosi = -1;
484 } else {
485 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
486 }
487
488 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
489 miso = default_pins->miso;
490 } else if (args[ARG_miso].u_obj == mp_const_none) {
491 miso = -1;
492 } else {
493 miso = machine_pin_get_id(args[ARG_miso].u_obj);
494 }
495
Damien Georgebc08c882016-12-06 12:20:10 +1100496 machine_hw_spi_init_internal(
497 self,
498 args[ARG_id].u_int,
499 args[ARG_baudrate].u_int,
500 args[ARG_polarity].u_int,
501 args[ARG_phase].u_int,
502 args[ARG_bits].u_int,
503 args[ARG_firstbit].u_int,
Jonathan Hogg89945b12021-06-16 13:31:46 +0100504 sck,
505 mosi,
506 miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100507
508 return MP_OBJ_FROM_PTR(self);
509}
510
511STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
512 .init = machine_hw_spi_init,
513 .deinit = machine_hw_spi_deinit,
514 .transfer = machine_hw_spi_transfer,
515};
516
517const mp_obj_type_t machine_hw_spi_type = {
518 { &mp_type_type },
519 .name = MP_QSTR_SPI,
520 .print = machine_hw_spi_print,
521 .make_new = machine_hw_spi_make_new,
522 .protocol = &machine_hw_spi_p,
Damien George1a3e3862020-03-27 00:35:04 +1100523 .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict,
Damien Georgebc08c882016-12-06 12:20:10 +1100524};