blob: 4c0989b9a6f9b173d7b1d348405ad3f3120fbbdc [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
Damien George68235142021-07-18 12:11:21 +100060#endif
61
Damien Georgeb24fcd72021-03-11 13:32:00 +110062typedef struct _machine_hw_spi_default_pins_t {
63 int8_t sck;
64 int8_t mosi;
65 int8_t miso;
66} machine_hw_spi_default_pins_t;
67
Damien Georgebc08c882016-12-06 12:20:10 +110068typedef struct _machine_hw_spi_obj_t {
69 mp_obj_base_t base;
70 spi_host_device_t host;
71 uint32_t baudrate;
72 uint8_t polarity;
73 uint8_t phase;
74 uint8_t bits;
75 uint8_t firstbit;
76 int8_t sck;
77 int8_t mosi;
78 int8_t miso;
79 spi_device_handle_t spi;
80 enum {
81 MACHINE_HW_SPI_STATE_NONE,
82 MACHINE_HW_SPI_STATE_INIT,
83 MACHINE_HW_SPI_STATE_DEINIT
84 } state;
85} machine_hw_spi_obj_t;
86
Damien Georgeb24fcd72021-03-11 13:32:00 +110087// Default pin mappings for the hardware SPI instances
88STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = {
89 { .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
90 { .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
91};
92
Damien Georgeda72bb62019-01-23 23:47:36 +110093// Static objects mapping to HSPI and VSPI hardware peripherals
94STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2];
95
Damien Georgebc08c882016-12-06 12:20:10 +110096STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
97 switch (spi_bus_remove_device(self->spi)) {
98 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +110099 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100100 return;
101
102 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100103 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI device already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100104 return;
105 }
106
107 switch (spi_bus_free(self->host)) {
108 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100109 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100110 return;
111
112 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100113 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100114 return;
115 }
116
117 int8_t pins[3] = {self->miso, self->mosi, self->sck};
118
119 for (int i = 0; i < 3; i++) {
120 if (pins[i] != -1) {
121 gpio_pad_select_gpio(pins[i]);
122 gpio_matrix_out(pins[i], SIG_GPIO_OUT_IDX, false, false);
123 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
124 }
125 }
126}
127
128STATIC void machine_hw_spi_init_internal(
Damien George69661f32020-02-27 15:36:53 +1100129 machine_hw_spi_obj_t *self,
130 int8_t host,
131 int32_t baudrate,
132 int8_t polarity,
133 int8_t phase,
134 int8_t bits,
135 int8_t firstbit,
136 int8_t sck,
137 int8_t mosi,
138 int8_t miso) {
Damien Georgebc08c882016-12-06 12:20:10 +1100139
140 // if we're not initialized, then we're
141 // implicitly 'changed', since this is the init routine
142 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
143
144 esp_err_t ret;
145
146 machine_hw_spi_obj_t old_self = *self;
147
148 if (host != -1 && host != self->host) {
149 self->host = host;
150 changed = true;
151 }
152
Jonathan Hoggeb3029c2021-07-12 11:35:25 +0100153 if (baudrate != -1) {
154 // calculate the actual clock frequency that the SPI peripheral can produce
155 baudrate = spi_get_actual_clock(APB_CLK_FREQ, baudrate, 0);
156 if (baudrate != self->baudrate) {
157 self->baudrate = baudrate;
158 changed = true;
159 }
Damien Georgebc08c882016-12-06 12:20:10 +1100160 }
161
162 if (polarity != -1 && polarity != self->polarity) {
163 self->polarity = polarity;
164 changed = true;
165 }
166
167 if (phase != -1 && phase != self->phase) {
Damien George69661f32020-02-27 15:36:53 +1100168 self->phase = phase;
Damien Georgebc08c882016-12-06 12:20:10 +1100169 changed = true;
170 }
171
172 if (bits != -1 && bits != self->bits) {
173 self->bits = bits;
174 changed = true;
175 }
176
177 if (firstbit != -1 && firstbit != self->firstbit) {
178 self->firstbit = firstbit;
179 changed = true;
180 }
181
182 if (sck != -2 && sck != self->sck) {
183 self->sck = sck;
184 changed = true;
185 }
186
187 if (mosi != -2 && mosi != self->mosi) {
188 self->mosi = mosi;
189 changed = true;
190 }
191
192 if (miso != -2 && miso != self->miso) {
193 self->miso = miso;
194 changed = true;
195 }
196
Damien George66a86a02021-02-18 21:24:34 +1100197 if (self->host != HSPI_HOST
198 #ifdef VSPI_HOST
199 && self->host != VSPI_HOST
200 #endif
201 ) {
202 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host);
Damien Georgebc08c882016-12-06 12:20:10 +1100203 }
204
205 if (changed) {
206 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
207 self->state = MACHINE_HW_SPI_STATE_DEINIT;
208 machine_hw_spi_deinit_internal(&old_self);
209 }
210 } else {
211 return; // no changes
212 }
213
214 spi_bus_config_t buscfg = {
215 .miso_io_num = self->miso,
216 .mosi_io_num = self->mosi,
217 .sclk_io_num = self->sck,
218 .quadwp_io_num = -1,
219 .quadhd_io_num = -1
220 };
221
222 spi_device_interface_config_t devcfg = {
223 .clock_speed_hz = self->baudrate,
224 .mode = self->phase | (self->polarity << 1),
225 .spics_io_num = -1, // No CS pin
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100226 .queue_size = 2,
Damien Georgebc08c882016-12-06 12:20:10 +1100227 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
228 .pre_cb = NULL
229 };
230
stijn84fa3312020-04-16 09:13:57 +0200231 // Initialize the SPI bus
Damien Georgebc08c882016-12-06 12:20:10 +1100232
Damien Georged82f3442019-01-23 23:40:06 +1100233 // Select DMA channel based on the hardware SPI host
234 int dma_chan = 0;
235 if (self->host == HSPI_HOST) {
236 dma_chan = 1;
Damien George66a86a02021-02-18 21:24:34 +1100237 #ifdef VSPI_HOST
Damien Georged82f3442019-01-23 23:40:06 +1100238 } else if (self->host == VSPI_HOST) {
239 dma_chan = 2;
Damien George66a86a02021-02-18 21:24:34 +1100240 #endif
Damien Georged82f3442019-01-23 23:40:06 +1100241 }
242
243 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100244 switch (ret) {
245 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100246 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100247 return;
248
249 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100250 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI host already in use"));
Damien Georgebc08c882016-12-06 12:20:10 +1100251 return;
252 }
253
254 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
255 switch (ret) {
256 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100257 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100258 spi_bus_free(self->host);
259 return;
260
261 case ESP_ERR_NO_MEM:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100262 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("out of memory"));
Damien Georgebc08c882016-12-06 12:20:10 +1100263 spi_bus_free(self->host);
264 return;
265
266 case ESP_ERR_NOT_FOUND:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100267 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("no free slots"));
Damien Georgebc08c882016-12-06 12:20:10 +1100268 spi_bus_free(self->host);
269 return;
270 }
271 self->state = MACHINE_HW_SPI_STATE_INIT;
272}
273
274STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
Damien George1a3e3862020-03-27 00:35:04 +1100275 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100276 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
277 self->state = MACHINE_HW_SPI_STATE_DEINIT;
278 machine_hw_spi_deinit_internal(self);
279 }
280}
281
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100282STATIC mp_uint_t gcd(mp_uint_t x, mp_uint_t y) {
283 while (x != y) {
284 if (x > y) {
285 x -= y;
286 } else {
287 y -= x;
288 }
289 }
290 return x;
291}
292
Damien Georgebc08c882016-12-06 12:20:10 +1100293STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
294 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
295
Damien Georgebc08c882016-12-06 12:20:10 +1100296 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100297 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("transfer on deinitialized SPI"));
Damien Georgebc08c882016-12-06 12:20:10 +1100298 return;
299 }
300
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800301 // Round to nearest whole set of bits
302 int bits_to_send = len * 8 / self->bits * self->bits;
303
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100304 if (!bits_to_send) {
305 mp_raise_ValueError(MP_ERROR_TEXT("buffer too short"));
306 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800307
308 if (len <= 4) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100309 spi_transaction_t transaction = { 0 };
310
Damien Georgebc08c882016-12-06 12:20:10 +1100311 if (src != NULL) {
312 memcpy(&transaction.tx_data, src, len);
313 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800314
315 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
316 transaction.length = bits_to_send;
317 spi_device_transmit(self->spi, &transaction);
318
319 if (dest != NULL) {
320 memcpy(dest, &transaction.rx_data, len);
321 }
Damien Georgebc08c882016-12-06 12:20:10 +1100322 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800323 int offset = 0;
324 int bits_remaining = bits_to_send;
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100325 int optimum_word_size = 8 * self->bits / gcd(8, self->bits);
326 int max_transaction_bits = MP_HW_SPI_MAX_XFER_BITS / optimum_word_size * optimum_word_size;
327 spi_transaction_t *transaction, *result, transactions[2];
328 int i = 0;
329
330 spi_device_acquire_bus(self->spi, portMAX_DELAY);
Damien Georgebc08c882016-12-06 12:20:10 +1100331
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800332 while (bits_remaining) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100333 transaction = transactions + i++ % 2;
334 memset(transaction, 0, sizeof(spi_transaction_t));
Damien Georgebc08c882016-12-06 12:20:10 +1100335
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100336 transaction->length =
337 bits_remaining > max_transaction_bits ? max_transaction_bits : bits_remaining;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800338
339 if (src != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100340 transaction->tx_buffer = src + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800341 }
342 if (dest != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100343 transaction->rx_buffer = dest + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800344 }
345
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100346 spi_device_queue_trans(self->spi, transaction, portMAX_DELAY);
347 bits_remaining -= transaction->length;
348
349 if (offset > 0) {
350 // wait for previously queued transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100351 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100352 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100353 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100354 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800355
356 // doesn't need ceil(); loop ends when bits_remaining is 0
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100357 offset += transaction->length / 8;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800358 }
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100359
360 // wait for last transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100361 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100362 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100363 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100364 spi_device_release_bus(self->spi);
Damien Georgebc08c882016-12-06 12:20:10 +1100365 }
366}
367
368/******************************************************************************/
369// MicroPython bindings for hw_spi
370
371STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
372 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
373 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 +1100374 self->host, self->baudrate, self->polarity,
375 self->phase, self->bits, self->firstbit,
376 self->sck, self->mosi, self->miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100377}
378
379STATIC 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 +1100380 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100381
382 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
383 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100384 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100385 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
386 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
387 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
388 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
389 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
390 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
391 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
392 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
393 };
394
395 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
396 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
Damien George69661f32020-02-27 15:36:53 +1100397 allowed_args, args);
Damien Georgebc08c882016-12-06 12:20:10 +1100398 int8_t sck, mosi, miso;
399
400 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
401 sck = -2;
402 } else if (args[ARG_sck].u_obj == mp_const_none) {
403 sck = -1;
404 } else {
405 sck = machine_pin_get_id(args[ARG_sck].u_obj);
406 }
407
408 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
409 miso = -2;
410 } else if (args[ARG_miso].u_obj == mp_const_none) {
411 miso = -1;
412 } else {
413 miso = machine_pin_get_id(args[ARG_miso].u_obj);
414 }
415
416 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
417 mosi = -2;
418 } else if (args[ARG_mosi].u_obj == mp_const_none) {
419 mosi = -1;
420 } else {
421 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
422 }
423
424 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
Damien George69661f32020-02-27 15:36:53 +1100425 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
426 args[ARG_firstbit].u_int, sck, mosi, miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100427}
428
429mp_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 +1000430 MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
431
Damien Georgebc08c882016-12-06 12:20:10 +1100432 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
433 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100434 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100435 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
436 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
437 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
438 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
439 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
440 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
441 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
442 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
443 };
444 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
445 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
446
Damien Georgeda72bb62019-01-23 23:47:36 +1100447 machine_hw_spi_obj_t *self;
Damien Georgeb24fcd72021-03-11 13:32:00 +1100448 const machine_hw_spi_default_pins_t *default_pins;
Damien Georgeda72bb62019-01-23 23:47:36 +1100449 if (args[ARG_id].u_int == HSPI_HOST) {
450 self = &machine_hw_spi_obj[0];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100451 default_pins = &machine_hw_spi_default_pins[0];
Damien Georgeda72bb62019-01-23 23:47:36 +1100452 } else {
453 self = &machine_hw_spi_obj[1];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100454 default_pins = &machine_hw_spi_default_pins[1];
Damien Georgeda72bb62019-01-23 23:47:36 +1100455 }
Damien Georgebc08c882016-12-06 12:20:10 +1100456 self->base.type = &machine_hw_spi_type;
457
Jonathan Hogg89945b12021-06-16 13:31:46 +0100458 int8_t sck, mosi, miso;
459
460 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
461 sck = default_pins->sck;
462 } else if (args[ARG_sck].u_obj == mp_const_none) {
463 sck = -1;
464 } else {
465 sck = machine_pin_get_id(args[ARG_sck].u_obj);
466 }
467
468 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
469 mosi = default_pins->mosi;
470 } else if (args[ARG_mosi].u_obj == mp_const_none) {
471 mosi = -1;
472 } else {
473 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
474 }
475
476 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
477 miso = default_pins->miso;
478 } else if (args[ARG_miso].u_obj == mp_const_none) {
479 miso = -1;
480 } else {
481 miso = machine_pin_get_id(args[ARG_miso].u_obj);
482 }
483
Damien Georgebc08c882016-12-06 12:20:10 +1100484 machine_hw_spi_init_internal(
485 self,
486 args[ARG_id].u_int,
487 args[ARG_baudrate].u_int,
488 args[ARG_polarity].u_int,
489 args[ARG_phase].u_int,
490 args[ARG_bits].u_int,
491 args[ARG_firstbit].u_int,
Jonathan Hogg89945b12021-06-16 13:31:46 +0100492 sck,
493 mosi,
494 miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100495
496 return MP_OBJ_FROM_PTR(self);
497}
498
499STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
500 .init = machine_hw_spi_init,
501 .deinit = machine_hw_spi_deinit,
502 .transfer = machine_hw_spi_transfer,
503};
504
505const mp_obj_type_t machine_hw_spi_type = {
506 { &mp_type_type },
507 .name = MP_QSTR_SPI,
508 .print = machine_hw_spi_print,
509 .make_new = machine_hw_spi_make_new,
510 .protocol = &machine_hw_spi_p,
Damien George1a3e3862020-03-27 00:35:04 +1100511 .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict,
Damien Georgebc08c882016-12-06 12:20:10 +1100512};