blob: 5b59f2431cc686a4470530940a2ea396cae60e8e [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 Georgeb24fcd72021-03-11 13:32:00 +110056typedef struct _machine_hw_spi_default_pins_t {
57 int8_t sck;
58 int8_t mosi;
59 int8_t miso;
60} machine_hw_spi_default_pins_t;
61
Damien Georgebc08c882016-12-06 12:20:10 +110062typedef struct _machine_hw_spi_obj_t {
63 mp_obj_base_t base;
64 spi_host_device_t host;
65 uint32_t baudrate;
66 uint8_t polarity;
67 uint8_t phase;
68 uint8_t bits;
69 uint8_t firstbit;
70 int8_t sck;
71 int8_t mosi;
72 int8_t miso;
73 spi_device_handle_t spi;
74 enum {
75 MACHINE_HW_SPI_STATE_NONE,
76 MACHINE_HW_SPI_STATE_INIT,
77 MACHINE_HW_SPI_STATE_DEINIT
78 } state;
79} machine_hw_spi_obj_t;
80
Damien Georgeb24fcd72021-03-11 13:32:00 +110081// Default pin mappings for the hardware SPI instances
82STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = {
83 { .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
84 { .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
85};
86
Damien Georgeda72bb62019-01-23 23:47:36 +110087// Static objects mapping to HSPI and VSPI hardware peripherals
88STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2];
89
Damien Georgebc08c882016-12-06 12:20:10 +110090STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
91 switch (spi_bus_remove_device(self->spi)) {
92 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +110093 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +110094 return;
95
96 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +110097 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI device already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +110098 return;
99 }
100
101 switch (spi_bus_free(self->host)) {
102 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100103 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100104 return;
105
106 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100107 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100108 return;
109 }
110
111 int8_t pins[3] = {self->miso, self->mosi, self->sck};
112
113 for (int i = 0; i < 3; i++) {
114 if (pins[i] != -1) {
115 gpio_pad_select_gpio(pins[i]);
116 gpio_matrix_out(pins[i], SIG_GPIO_OUT_IDX, false, false);
117 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
118 }
119 }
120}
121
122STATIC void machine_hw_spi_init_internal(
Damien George69661f32020-02-27 15:36:53 +1100123 machine_hw_spi_obj_t *self,
124 int8_t host,
125 int32_t baudrate,
126 int8_t polarity,
127 int8_t phase,
128 int8_t bits,
129 int8_t firstbit,
130 int8_t sck,
131 int8_t mosi,
132 int8_t miso) {
Damien Georgebc08c882016-12-06 12:20:10 +1100133
134 // if we're not initialized, then we're
135 // implicitly 'changed', since this is the init routine
136 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
137
138 esp_err_t ret;
139
140 machine_hw_spi_obj_t old_self = *self;
141
142 if (host != -1 && host != self->host) {
143 self->host = host;
144 changed = true;
145 }
146
Jonathan Hoggeb3029c2021-07-12 11:35:25 +0100147 if (baudrate != -1) {
148 // calculate the actual clock frequency that the SPI peripheral can produce
149 baudrate = spi_get_actual_clock(APB_CLK_FREQ, baudrate, 0);
150 if (baudrate != self->baudrate) {
151 self->baudrate = baudrate;
152 changed = true;
153 }
Damien Georgebc08c882016-12-06 12:20:10 +1100154 }
155
156 if (polarity != -1 && polarity != self->polarity) {
157 self->polarity = polarity;
158 changed = true;
159 }
160
161 if (phase != -1 && phase != self->phase) {
Damien George69661f32020-02-27 15:36:53 +1100162 self->phase = phase;
Damien Georgebc08c882016-12-06 12:20:10 +1100163 changed = true;
164 }
165
166 if (bits != -1 && bits != self->bits) {
167 self->bits = bits;
168 changed = true;
169 }
170
171 if (firstbit != -1 && firstbit != self->firstbit) {
172 self->firstbit = firstbit;
173 changed = true;
174 }
175
176 if (sck != -2 && sck != self->sck) {
177 self->sck = sck;
178 changed = true;
179 }
180
181 if (mosi != -2 && mosi != self->mosi) {
182 self->mosi = mosi;
183 changed = true;
184 }
185
186 if (miso != -2 && miso != self->miso) {
187 self->miso = miso;
188 changed = true;
189 }
190
Damien George66a86a02021-02-18 21:24:34 +1100191 if (self->host != HSPI_HOST
192 #ifdef VSPI_HOST
193 && self->host != VSPI_HOST
194 #endif
195 ) {
196 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host);
Damien Georgebc08c882016-12-06 12:20:10 +1100197 }
198
199 if (changed) {
200 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
201 self->state = MACHINE_HW_SPI_STATE_DEINIT;
202 machine_hw_spi_deinit_internal(&old_self);
203 }
204 } else {
205 return; // no changes
206 }
207
208 spi_bus_config_t buscfg = {
209 .miso_io_num = self->miso,
210 .mosi_io_num = self->mosi,
211 .sclk_io_num = self->sck,
212 .quadwp_io_num = -1,
213 .quadhd_io_num = -1
214 };
215
216 spi_device_interface_config_t devcfg = {
217 .clock_speed_hz = self->baudrate,
218 .mode = self->phase | (self->polarity << 1),
219 .spics_io_num = -1, // No CS pin
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100220 .queue_size = 2,
Damien Georgebc08c882016-12-06 12:20:10 +1100221 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
222 .pre_cb = NULL
223 };
224
stijn84fa3312020-04-16 09:13:57 +0200225 // Initialize the SPI bus
Damien Georgebc08c882016-12-06 12:20:10 +1100226
Damien Georged82f3442019-01-23 23:40:06 +1100227 // Select DMA channel based on the hardware SPI host
228 int dma_chan = 0;
229 if (self->host == HSPI_HOST) {
230 dma_chan = 1;
Damien George66a86a02021-02-18 21:24:34 +1100231 #ifdef VSPI_HOST
Damien Georged82f3442019-01-23 23:40:06 +1100232 } else if (self->host == VSPI_HOST) {
233 dma_chan = 2;
Damien George66a86a02021-02-18 21:24:34 +1100234 #endif
Damien Georged82f3442019-01-23 23:40:06 +1100235 }
236
237 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100238 switch (ret) {
239 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100240 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100241 return;
242
243 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100244 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI host already in use"));
Damien Georgebc08c882016-12-06 12:20:10 +1100245 return;
246 }
247
248 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
249 switch (ret) {
250 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100251 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100252 spi_bus_free(self->host);
253 return;
254
255 case ESP_ERR_NO_MEM:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100256 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("out of memory"));
Damien Georgebc08c882016-12-06 12:20:10 +1100257 spi_bus_free(self->host);
258 return;
259
260 case ESP_ERR_NOT_FOUND:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100261 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("no free slots"));
Damien Georgebc08c882016-12-06 12:20:10 +1100262 spi_bus_free(self->host);
263 return;
264 }
265 self->state = MACHINE_HW_SPI_STATE_INIT;
266}
267
268STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
Damien George1a3e3862020-03-27 00:35:04 +1100269 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100270 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
271 self->state = MACHINE_HW_SPI_STATE_DEINIT;
272 machine_hw_spi_deinit_internal(self);
273 }
274}
275
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100276STATIC mp_uint_t gcd(mp_uint_t x, mp_uint_t y) {
277 while (x != y) {
278 if (x > y) {
279 x -= y;
280 } else {
281 y -= x;
282 }
283 }
284 return x;
285}
286
Damien Georgebc08c882016-12-06 12:20:10 +1100287STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
288 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
289
Damien Georgebc08c882016-12-06 12:20:10 +1100290 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100291 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("transfer on deinitialized SPI"));
Damien Georgebc08c882016-12-06 12:20:10 +1100292 return;
293 }
294
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800295 // Round to nearest whole set of bits
296 int bits_to_send = len * 8 / self->bits * self->bits;
297
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100298 if (!bits_to_send) {
299 mp_raise_ValueError(MP_ERROR_TEXT("buffer too short"));
300 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800301
302 if (len <= 4) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100303 spi_transaction_t transaction = { 0 };
304
Damien Georgebc08c882016-12-06 12:20:10 +1100305 if (src != NULL) {
306 memcpy(&transaction.tx_data, src, len);
307 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800308
309 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
310 transaction.length = bits_to_send;
311 spi_device_transmit(self->spi, &transaction);
312
313 if (dest != NULL) {
314 memcpy(dest, &transaction.rx_data, len);
315 }
Damien Georgebc08c882016-12-06 12:20:10 +1100316 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800317 int offset = 0;
318 int bits_remaining = bits_to_send;
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100319 int optimum_word_size = 8 * self->bits / gcd(8, self->bits);
320 int max_transaction_bits = MP_HW_SPI_MAX_XFER_BITS / optimum_word_size * optimum_word_size;
321 spi_transaction_t *transaction, *result, transactions[2];
322 int i = 0;
323
324 spi_device_acquire_bus(self->spi, portMAX_DELAY);
Damien Georgebc08c882016-12-06 12:20:10 +1100325
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800326 while (bits_remaining) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100327 transaction = transactions + i++ % 2;
328 memset(transaction, 0, sizeof(spi_transaction_t));
Damien Georgebc08c882016-12-06 12:20:10 +1100329
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100330 transaction->length =
331 bits_remaining > max_transaction_bits ? max_transaction_bits : bits_remaining;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800332
333 if (src != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100334 transaction->tx_buffer = src + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800335 }
336 if (dest != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100337 transaction->rx_buffer = dest + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800338 }
339
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100340 spi_device_queue_trans(self->spi, transaction, portMAX_DELAY);
341 bits_remaining -= transaction->length;
342
343 if (offset > 0) {
344 // wait for previously queued transaction
345 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
346 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800347
348 // doesn't need ceil(); loop ends when bits_remaining is 0
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100349 offset += transaction->length / 8;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800350 }
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100351
352 // wait for last transaction
353 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
354 spi_device_release_bus(self->spi);
Damien Georgebc08c882016-12-06 12:20:10 +1100355 }
356}
357
358/******************************************************************************/
359// MicroPython bindings for hw_spi
360
361STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
362 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
363 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 +1100364 self->host, self->baudrate, self->polarity,
365 self->phase, self->bits, self->firstbit,
366 self->sck, self->mosi, self->miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100367}
368
369STATIC 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 +1100370 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100371
372 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
373 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100374 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100375 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
376 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
377 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
378 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
379 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
380 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
381 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
382 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
383 };
384
385 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
386 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
Damien George69661f32020-02-27 15:36:53 +1100387 allowed_args, args);
Damien Georgebc08c882016-12-06 12:20:10 +1100388 int8_t sck, mosi, miso;
389
390 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
391 sck = -2;
392 } else if (args[ARG_sck].u_obj == mp_const_none) {
393 sck = -1;
394 } else {
395 sck = machine_pin_get_id(args[ARG_sck].u_obj);
396 }
397
398 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
399 miso = -2;
400 } else if (args[ARG_miso].u_obj == mp_const_none) {
401 miso = -1;
402 } else {
403 miso = machine_pin_get_id(args[ARG_miso].u_obj);
404 }
405
406 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
407 mosi = -2;
408 } else if (args[ARG_mosi].u_obj == mp_const_none) {
409 mosi = -1;
410 } else {
411 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
412 }
413
414 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
Damien George69661f32020-02-27 15:36:53 +1100415 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
416 args[ARG_firstbit].u_int, sck, mosi, miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100417}
418
419mp_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 +1000420 MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
421
Damien Georgebc08c882016-12-06 12:20:10 +1100422 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
423 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100424 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100425 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
426 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
427 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
428 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
429 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
430 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
431 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
432 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
433 };
434 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
435 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
436
Damien Georgeda72bb62019-01-23 23:47:36 +1100437 machine_hw_spi_obj_t *self;
Damien Georgeb24fcd72021-03-11 13:32:00 +1100438 const machine_hw_spi_default_pins_t *default_pins;
Damien Georgeda72bb62019-01-23 23:47:36 +1100439 if (args[ARG_id].u_int == HSPI_HOST) {
440 self = &machine_hw_spi_obj[0];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100441 default_pins = &machine_hw_spi_default_pins[0];
Damien Georgeda72bb62019-01-23 23:47:36 +1100442 } else {
443 self = &machine_hw_spi_obj[1];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100444 default_pins = &machine_hw_spi_default_pins[1];
Damien Georgeda72bb62019-01-23 23:47:36 +1100445 }
Damien Georgebc08c882016-12-06 12:20:10 +1100446 self->base.type = &machine_hw_spi_type;
447
Jonathan Hogg89945b12021-06-16 13:31:46 +0100448 int8_t sck, mosi, miso;
449
450 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
451 sck = default_pins->sck;
452 } else if (args[ARG_sck].u_obj == mp_const_none) {
453 sck = -1;
454 } else {
455 sck = machine_pin_get_id(args[ARG_sck].u_obj);
456 }
457
458 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
459 mosi = default_pins->mosi;
460 } else if (args[ARG_mosi].u_obj == mp_const_none) {
461 mosi = -1;
462 } else {
463 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
464 }
465
466 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
467 miso = default_pins->miso;
468 } else if (args[ARG_miso].u_obj == mp_const_none) {
469 miso = -1;
470 } else {
471 miso = machine_pin_get_id(args[ARG_miso].u_obj);
472 }
473
Damien Georgebc08c882016-12-06 12:20:10 +1100474 machine_hw_spi_init_internal(
475 self,
476 args[ARG_id].u_int,
477 args[ARG_baudrate].u_int,
478 args[ARG_polarity].u_int,
479 args[ARG_phase].u_int,
480 args[ARG_bits].u_int,
481 args[ARG_firstbit].u_int,
Jonathan Hogg89945b12021-06-16 13:31:46 +0100482 sck,
483 mosi,
484 miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100485
486 return MP_OBJ_FROM_PTR(self);
487}
488
489STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
490 .init = machine_hw_spi_init,
491 .deinit = machine_hw_spi_deinit,
492 .transfer = machine_hw_spi_transfer,
493};
494
495const mp_obj_type_t machine_hw_spi_type = {
496 { &mp_type_type },
497 .name = MP_QSTR_SPI,
498 .print = machine_hw_spi_print,
499 .make_new = machine_hw_spi_make_new,
500 .protocol = &machine_hw_spi_p,
Damien George1a3e3862020-03-27 00:35:04 +1100501 .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict,
Damien Georgebc08c882016-12-06 12:20:10 +1100502};