blob: 399cb902e8cc604a0289570c16f5a2bb105219dc [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"
Damien George90023b42023-10-26 11:39:40 +110034#include "extmod/modmachine.h"
Damien Georgebc08c882016-12-06 12:20:10 +110035
36#include "driver/spi_master.h"
Damien Georgee4650122023-05-09 09:52:54 +100037#include "soc/gpio_sig_map.h"
38#include "soc/spi_pins.h"
Damien Georgebc08c882016-12-06 12:20:10 +110039
Tomas Vanekd0888652021-11-29 12:57:28 +010040// SPI mappings by device, naming used by IDF old/new
41// upython | ESP32 | ESP32S2 | ESP32S3 | ESP32C3
42// ----------+-----------+-----------+---------+---------
43// SPI(id=1) | HSPI/SPI2 | FSPI/SPI2 | SPI2 | SPI2
44// SPI(id=2) | VSPI/SPI3 | HSPI/SPI3 | SPI3 | err
45
Damien Georgeb2adfc82023-07-25 11:33:51 +100046// Number of available hardware SPI peripherals.
47#if SOC_SPI_PERIPH_NUM > 2
48#define MICROPY_HW_SPI_MAX (2)
49#else
50#define MICROPY_HW_SPI_MAX (1)
51#endif
52
Tomas Vanekd0888652021-11-29 12:57:28 +010053// Default pins for SPI(id=1) aka IDF SPI2, can be overridden by a board
Damien Georgeb24fcd72021-03-11 13:32:00 +110054#ifndef MICROPY_HW_SPI1_SCK
Tomas Vanekd0888652021-11-29 12:57:28 +010055// Use IO_MUX pins by default.
56// If SPI lines are routed to other pins through GPIO matrix
57// routing adds some delay and lower limit applies to SPI clk freq
Damien Georgee4650122023-05-09 09:52:54 +100058#define MICROPY_HW_SPI1_SCK SPI2_IOMUX_PIN_NUM_CLK
59#define MICROPY_HW_SPI1_MOSI SPI2_IOMUX_PIN_NUM_MOSI
60#define MICROPY_HW_SPI1_MISO SPI2_IOMUX_PIN_NUM_MISO
Tomas Vanekd0888652021-11-29 12:57:28 +010061#endif
Damien Georgeb24fcd72021-03-11 13:32:00 +110062
Tomas Vaneke7611522021-11-29 16:49:11 +010063// Default pins for SPI(id=2) aka IDF SPI3, can be overridden by a board
Damien Georgeb24fcd72021-03-11 13:32:00 +110064#ifndef MICROPY_HW_SPI2_SCK
Tomas Vaneke7611522021-11-29 16:49:11 +010065#if CONFIG_IDF_TARGET_ESP32
66// ESP32 has IO_MUX pins for VSPI/SPI3 lines, use them as defaults
Damien Georgee4650122023-05-09 09:52:54 +100067#define MICROPY_HW_SPI2_SCK SPI3_IOMUX_PIN_NUM_CLK // pin 18
68#define MICROPY_HW_SPI2_MOSI SPI3_IOMUX_PIN_NUM_MOSI // pin 23
69#define MICROPY_HW_SPI2_MISO SPI3_IOMUX_PIN_NUM_MISO // pin 19
Tomas Vaneke7611522021-11-29 16:49:11 +010070#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
71// ESP32S2 and S3 uses GPIO matrix for SPI3 pins, no IO_MUX possible
72// Set defaults to the pins used by SPI2 in Octal mode
73#define MICROPY_HW_SPI2_SCK (36)
74#define MICROPY_HW_SPI2_MOSI (35)
75#define MICROPY_HW_SPI2_MISO (37)
76#endif
Damien Georgeb24fcd72021-03-11 13:32:00 +110077#endif
78
Eric Poulsen9123c8d2017-11-28 18:48:30 -080079#define MP_HW_SPI_MAX_XFER_BYTES (4092)
80#define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8
81
Damien Georgeb24fcd72021-03-11 13:32:00 +110082typedef struct _machine_hw_spi_default_pins_t {
83 int8_t sck;
84 int8_t mosi;
85 int8_t miso;
86} machine_hw_spi_default_pins_t;
87
Damien Georgebc08c882016-12-06 12:20:10 +110088typedef struct _machine_hw_spi_obj_t {
89 mp_obj_base_t base;
90 spi_host_device_t host;
91 uint32_t baudrate;
92 uint8_t polarity;
93 uint8_t phase;
94 uint8_t bits;
95 uint8_t firstbit;
96 int8_t sck;
97 int8_t mosi;
98 int8_t miso;
99 spi_device_handle_t spi;
100 enum {
101 MACHINE_HW_SPI_STATE_NONE,
102 MACHINE_HW_SPI_STATE_INIT,
103 MACHINE_HW_SPI_STATE_DEINIT
104 } state;
105} machine_hw_spi_obj_t;
106
Damien Georgeb24fcd72021-03-11 13:32:00 +1100107// Default pin mappings for the hardware SPI instances
Damien Georgeb2adfc82023-07-25 11:33:51 +1000108STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[MICROPY_HW_SPI_MAX] = {
Damien Georgeb24fcd72021-03-11 13:32:00 +1100109 { .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
Tomas Vaneke7611522021-11-29 16:49:11 +0100110 #ifdef MICROPY_HW_SPI2_SCK
Damien Georgeb24fcd72021-03-11 13:32:00 +1100111 { .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
Tomas Vaneke7611522021-11-29 16:49:11 +0100112 #endif
Damien Georgeb24fcd72021-03-11 13:32:00 +1100113};
114
Damien Georgeb2adfc82023-07-25 11:33:51 +1000115// Static objects mapping to SPI2 (and SPI3 if available) hardware peripherals.
116STATIC machine_hw_spi_obj_t machine_hw_spi_obj[MICROPY_HW_SPI_MAX];
Damien Georgeda72bb62019-01-23 23:47:36 +1100117
Damien Georgebc08c882016-12-06 12:20:10 +1100118STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
119 switch (spi_bus_remove_device(self->spi)) {
120 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100121 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100122 return;
123
124 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100125 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI device already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100126 return;
127 }
128
129 switch (spi_bus_free(self->host)) {
130 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100131 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100132 return;
133
134 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100135 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100136 return;
137 }
138
139 int8_t pins[3] = {self->miso, self->mosi, self->sck};
140
141 for (int i = 0; i < 3; i++) {
142 if (pins[i] != -1) {
Damien Georgee4650122023-05-09 09:52:54 +1000143 esp_rom_gpio_pad_select_gpio(pins[i]);
144 esp_rom_gpio_connect_out_signal(pins[i], SIG_GPIO_OUT_IDX, false, false);
Damien Georgebc08c882016-12-06 12:20:10 +1100145 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
146 }
147 }
148}
149
150STATIC void machine_hw_spi_init_internal(
Damien George69661f32020-02-27 15:36:53 +1100151 machine_hw_spi_obj_t *self,
152 int8_t host,
153 int32_t baudrate,
154 int8_t polarity,
155 int8_t phase,
156 int8_t bits,
157 int8_t firstbit,
158 int8_t sck,
159 int8_t mosi,
160 int8_t miso) {
Damien Georgebc08c882016-12-06 12:20:10 +1100161
162 // if we're not initialized, then we're
163 // implicitly 'changed', since this is the init routine
164 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
165
166 esp_err_t ret;
167
168 machine_hw_spi_obj_t old_self = *self;
169
170 if (host != -1 && host != self->host) {
171 self->host = host;
172 changed = true;
173 }
174
Jonathan Hoggeb3029c2021-07-12 11:35:25 +0100175 if (baudrate != -1) {
176 // calculate the actual clock frequency that the SPI peripheral can produce
177 baudrate = spi_get_actual_clock(APB_CLK_FREQ, baudrate, 0);
178 if (baudrate != self->baudrate) {
179 self->baudrate = baudrate;
180 changed = true;
181 }
Damien Georgebc08c882016-12-06 12:20:10 +1100182 }
183
184 if (polarity != -1 && polarity != self->polarity) {
185 self->polarity = polarity;
186 changed = true;
187 }
188
189 if (phase != -1 && phase != self->phase) {
Damien George69661f32020-02-27 15:36:53 +1100190 self->phase = phase;
Damien Georgebc08c882016-12-06 12:20:10 +1100191 changed = true;
192 }
193
194 if (bits != -1 && bits != self->bits) {
195 self->bits = bits;
196 changed = true;
197 }
198
199 if (firstbit != -1 && firstbit != self->firstbit) {
200 self->firstbit = firstbit;
201 changed = true;
202 }
203
204 if (sck != -2 && sck != self->sck) {
205 self->sck = sck;
206 changed = true;
207 }
208
209 if (mosi != -2 && mosi != self->mosi) {
210 self->mosi = mosi;
211 changed = true;
212 }
213
214 if (miso != -2 && miso != self->miso) {
215 self->miso = miso;
216 changed = true;
217 }
218
Damien Georgebc08c882016-12-06 12:20:10 +1100219 if (changed) {
220 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
221 self->state = MACHINE_HW_SPI_STATE_DEINIT;
222 machine_hw_spi_deinit_internal(&old_self);
223 }
224 } else {
225 return; // no changes
226 }
227
228 spi_bus_config_t buscfg = {
229 .miso_io_num = self->miso,
230 .mosi_io_num = self->mosi,
231 .sclk_io_num = self->sck,
232 .quadwp_io_num = -1,
233 .quadhd_io_num = -1
234 };
235
236 spi_device_interface_config_t devcfg = {
237 .clock_speed_hz = self->baudrate,
238 .mode = self->phase | (self->polarity << 1),
239 .spics_io_num = -1, // No CS pin
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100240 .queue_size = 2,
Damien Georgebc08c882016-12-06 12:20:10 +1100241 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
242 .pre_cb = NULL
243 };
244
stijn84fa3312020-04-16 09:13:57 +0200245 // Initialize the SPI bus
Damien Georgebc08c882016-12-06 12:20:10 +1100246
Damien Georged82f3442019-01-23 23:40:06 +1100247 // Select DMA channel based on the hardware SPI host
248 int dma_chan = 0;
Damien George95c614e2022-10-04 17:43:04 +1100249 #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
250 dma_chan = SPI_DMA_CH_AUTO;
251 #else
Damien Georgee4650122023-05-09 09:52:54 +1000252 if (self->host == SPI2_HOST) {
Damien Georged82f3442019-01-23 23:40:06 +1100253 dma_chan = 1;
Damien George95c614e2022-10-04 17:43:04 +1100254 } else {
Damien Georged82f3442019-01-23 23:40:06 +1100255 dma_chan = 2;
256 }
Damien George95c614e2022-10-04 17:43:04 +1100257 #endif
Damien Georged82f3442019-01-23 23:40:06 +1100258
259 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100260 switch (ret) {
261 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100262 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100263 return;
264
265 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100266 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI host already in use"));
Damien Georgebc08c882016-12-06 12:20:10 +1100267 return;
268 }
269
270 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
271 switch (ret) {
272 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100273 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100274 spi_bus_free(self->host);
275 return;
276
277 case ESP_ERR_NO_MEM:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100278 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("out of memory"));
Damien Georgebc08c882016-12-06 12:20:10 +1100279 spi_bus_free(self->host);
280 return;
281
282 case ESP_ERR_NOT_FOUND:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100283 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("no free slots"));
Damien Georgebc08c882016-12-06 12:20:10 +1100284 spi_bus_free(self->host);
285 return;
286 }
287 self->state = MACHINE_HW_SPI_STATE_INIT;
288}
289
290STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
Damien George1a3e3862020-03-27 00:35:04 +1100291 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100292 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
293 self->state = MACHINE_HW_SPI_STATE_DEINIT;
294 machine_hw_spi_deinit_internal(self);
295 }
296}
297
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100298STATIC mp_uint_t gcd(mp_uint_t x, mp_uint_t y) {
299 while (x != y) {
300 if (x > y) {
301 x -= y;
302 } else {
303 y -= x;
304 }
305 }
306 return x;
307}
308
Damien Georgebc08c882016-12-06 12:20:10 +1100309STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
310 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
311
Damien Georgebc08c882016-12-06 12:20:10 +1100312 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100313 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("transfer on deinitialized SPI"));
Damien Georgebc08c882016-12-06 12:20:10 +1100314 return;
315 }
316
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800317 // Round to nearest whole set of bits
318 int bits_to_send = len * 8 / self->bits * self->bits;
319
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100320 if (!bits_to_send) {
321 mp_raise_ValueError(MP_ERROR_TEXT("buffer too short"));
322 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800323
324 if (len <= 4) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100325 spi_transaction_t transaction = { 0 };
326
Damien Georgebc08c882016-12-06 12:20:10 +1100327 if (src != NULL) {
328 memcpy(&transaction.tx_data, src, len);
329 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800330
331 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
332 transaction.length = bits_to_send;
333 spi_device_transmit(self->spi, &transaction);
334
335 if (dest != NULL) {
336 memcpy(dest, &transaction.rx_data, len);
337 }
Damien Georgebc08c882016-12-06 12:20:10 +1100338 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800339 int offset = 0;
340 int bits_remaining = bits_to_send;
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100341 int optimum_word_size = 8 * self->bits / gcd(8, self->bits);
342 int max_transaction_bits = MP_HW_SPI_MAX_XFER_BITS / optimum_word_size * optimum_word_size;
343 spi_transaction_t *transaction, *result, transactions[2];
344 int i = 0;
345
346 spi_device_acquire_bus(self->spi, portMAX_DELAY);
Damien Georgebc08c882016-12-06 12:20:10 +1100347
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800348 while (bits_remaining) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100349 transaction = transactions + i++ % 2;
350 memset(transaction, 0, sizeof(spi_transaction_t));
Damien Georgebc08c882016-12-06 12:20:10 +1100351
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100352 transaction->length =
353 bits_remaining > max_transaction_bits ? max_transaction_bits : bits_remaining;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800354
355 if (src != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100356 transaction->tx_buffer = src + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800357 }
358 if (dest != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100359 transaction->rx_buffer = dest + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800360 }
361
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100362 spi_device_queue_trans(self->spi, transaction, portMAX_DELAY);
363 bits_remaining -= transaction->length;
364
365 if (offset > 0) {
366 // wait for previously queued transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100367 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100368 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100369 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100370 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800371
372 // doesn't need ceil(); loop ends when bits_remaining is 0
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100373 offset += transaction->length / 8;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800374 }
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100375
376 // wait for last transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100377 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100378 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100379 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100380 spi_device_release_bus(self->spi);
Damien Georgebc08c882016-12-06 12:20:10 +1100381 }
382}
383
384/******************************************************************************/
385// MicroPython bindings for hw_spi
386
387STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
388 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
389 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 +1100390 self->host, self->baudrate, self->polarity,
391 self->phase, self->bits, self->firstbit,
392 self->sck, self->mosi, self->miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100393}
394
395STATIC 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 +1100396 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100397
398 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
399 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100400 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100401 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
402 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
403 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
404 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
405 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
406 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
407 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
408 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
409 };
410
411 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
412 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
Damien George69661f32020-02-27 15:36:53 +1100413 allowed_args, args);
Damien Georgebc08c882016-12-06 12:20:10 +1100414 int8_t sck, mosi, miso;
415
416 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
417 sck = -2;
418 } else if (args[ARG_sck].u_obj == mp_const_none) {
419 sck = -1;
420 } else {
421 sck = machine_pin_get_id(args[ARG_sck].u_obj);
422 }
423
424 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
425 miso = -2;
426 } else if (args[ARG_miso].u_obj == mp_const_none) {
427 miso = -1;
428 } else {
429 miso = machine_pin_get_id(args[ARG_miso].u_obj);
430 }
431
432 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
433 mosi = -2;
434 } else if (args[ARG_mosi].u_obj == mp_const_none) {
435 mosi = -1;
436 } else {
437 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
438 }
439
440 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
Damien George69661f32020-02-27 15:36:53 +1100441 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
442 args[ARG_firstbit].u_int, sck, mosi, miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100443}
444
445mp_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 +1000446 MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
447
Damien Georgebc08c882016-12-06 12:20:10 +1100448 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
449 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100450 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100451 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
452 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
453 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
454 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
455 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
456 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
457 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
458 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
459 };
460 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
461 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
462
Damien Georgeda72bb62019-01-23 23:47:36 +1100463 machine_hw_spi_obj_t *self;
Damien Georgeb24fcd72021-03-11 13:32:00 +1100464 const machine_hw_spi_default_pins_t *default_pins;
Damien Georgeb2adfc82023-07-25 11:33:51 +1000465 mp_int_t spi_id = args[ARG_id].u_int;
466 if (1 <= spi_id && spi_id <= MICROPY_HW_SPI_MAX) {
467 self = &machine_hw_spi_obj[spi_id - 1];
468 default_pins = &machine_hw_spi_default_pins[spi_id - 1];
Damien Georgeda72bb62019-01-23 23:47:36 +1100469 } else {
Damien Georgeb2adfc82023-07-25 11:33:51 +1000470 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
Damien Georgeda72bb62019-01-23 23:47:36 +1100471 }
Damien Georgeb2adfc82023-07-25 11:33:51 +1000472
iabdalkader427670c2022-10-06 20:20:03 +0200473 self->base.type = &machine_spi_type;
Damien Georgebc08c882016-12-06 12:20:10 +1100474
Jonathan Hogg89945b12021-06-16 13:31:46 +0100475 int8_t sck, mosi, miso;
476
477 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
478 sck = default_pins->sck;
479 } else if (args[ARG_sck].u_obj == mp_const_none) {
480 sck = -1;
481 } else {
482 sck = machine_pin_get_id(args[ARG_sck].u_obj);
483 }
484
485 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
486 mosi = default_pins->mosi;
487 } else if (args[ARG_mosi].u_obj == mp_const_none) {
488 mosi = -1;
489 } else {
490 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
491 }
492
493 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
494 miso = default_pins->miso;
495 } else if (args[ARG_miso].u_obj == mp_const_none) {
496 miso = -1;
497 } else {
498 miso = machine_pin_get_id(args[ARG_miso].u_obj);
499 }
500
Damien Georgebc08c882016-12-06 12:20:10 +1100501 machine_hw_spi_init_internal(
502 self,
503 args[ARG_id].u_int,
504 args[ARG_baudrate].u_int,
505 args[ARG_polarity].u_int,
506 args[ARG_phase].u_int,
507 args[ARG_bits].u_int,
508 args[ARG_firstbit].u_int,
Jonathan Hogg89945b12021-06-16 13:31:46 +0100509 sck,
510 mosi,
511 miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100512
513 return MP_OBJ_FROM_PTR(self);
514}
515
Damien Tournoude982c1d2023-01-12 21:04:07 -0800516spi_host_device_t machine_hw_spi_get_host(mp_obj_t in) {
517 if (mp_obj_get_type(in) != &machine_spi_type) {
518 mp_raise_ValueError(MP_ERROR_TEXT("expecting a SPI object"));
519 }
520 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)in;
521 return self->host;
522}
523
Damien Georgebc08c882016-12-06 12:20:10 +1100524STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
525 .init = machine_hw_spi_init,
526 .deinit = machine_hw_spi_deinit,
527 .transfer = machine_hw_spi_transfer,
528};
529
Jim Mussared662b9762021-07-14 14:38:38 +1000530MP_DEFINE_CONST_OBJ_TYPE(
iabdalkader427670c2022-10-06 20:20:03 +0200531 machine_spi_type,
Jim Mussared662b9762021-07-14 14:38:38 +1000532 MP_QSTR_SPI,
533 MP_TYPE_FLAG_NONE,
Jim Mussared94beeab2022-09-17 00:31:23 +1000534 make_new, machine_hw_spi_make_new,
Jim Mussared662b9762021-07-14 14:38:38 +1000535 print, machine_hw_spi_print,
536 protocol, &machine_hw_spi_p,
Jim Mussared9dce8272022-06-24 16:27:46 +1000537 locals_dict, &mp_machine_spi_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000538 );