blob: e09c493f5b9451121a85aae5ef9346a9994168ef [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"
Damien Georgee4650122023-05-09 09:52:54 +100038#include "soc/gpio_sig_map.h"
39#include "soc/spi_pins.h"
Damien Georgebc08c882016-12-06 12:20:10 +110040
Tomas Vanekd0888652021-11-29 12:57:28 +010041// SPI mappings by device, naming used by IDF old/new
42// upython | ESP32 | ESP32S2 | ESP32S3 | ESP32C3
43// ----------+-----------+-----------+---------+---------
44// SPI(id=1) | HSPI/SPI2 | FSPI/SPI2 | SPI2 | SPI2
45// SPI(id=2) | VSPI/SPI3 | HSPI/SPI3 | SPI3 | err
46
Damien Georgeb2adfc82023-07-25 11:33:51 +100047// Number of available hardware SPI peripherals.
48#if SOC_SPI_PERIPH_NUM > 2
49#define MICROPY_HW_SPI_MAX (2)
50#else
51#define MICROPY_HW_SPI_MAX (1)
52#endif
53
Tomas Vanekd0888652021-11-29 12:57:28 +010054// Default pins for SPI(id=1) aka IDF SPI2, can be overridden by a board
Damien Georgeb24fcd72021-03-11 13:32:00 +110055#ifndef MICROPY_HW_SPI1_SCK
Tomas Vanekd0888652021-11-29 12:57:28 +010056// Use IO_MUX pins by default.
57// If SPI lines are routed to other pins through GPIO matrix
58// routing adds some delay and lower limit applies to SPI clk freq
Damien Georgee4650122023-05-09 09:52:54 +100059#define MICROPY_HW_SPI1_SCK SPI2_IOMUX_PIN_NUM_CLK
60#define MICROPY_HW_SPI1_MOSI SPI2_IOMUX_PIN_NUM_MOSI
61#define MICROPY_HW_SPI1_MISO SPI2_IOMUX_PIN_NUM_MISO
Tomas Vanekd0888652021-11-29 12:57:28 +010062#endif
Damien Georgeb24fcd72021-03-11 13:32:00 +110063
Tomas Vaneke7611522021-11-29 16:49:11 +010064// Default pins for SPI(id=2) aka IDF SPI3, can be overridden by a board
Damien Georgeb24fcd72021-03-11 13:32:00 +110065#ifndef MICROPY_HW_SPI2_SCK
Tomas Vaneke7611522021-11-29 16:49:11 +010066#if CONFIG_IDF_TARGET_ESP32
67// ESP32 has IO_MUX pins for VSPI/SPI3 lines, use them as defaults
Damien Georgee4650122023-05-09 09:52:54 +100068#define MICROPY_HW_SPI2_SCK SPI3_IOMUX_PIN_NUM_CLK // pin 18
69#define MICROPY_HW_SPI2_MOSI SPI3_IOMUX_PIN_NUM_MOSI // pin 23
70#define MICROPY_HW_SPI2_MISO SPI3_IOMUX_PIN_NUM_MISO // pin 19
Tomas Vaneke7611522021-11-29 16:49:11 +010071#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
72// ESP32S2 and S3 uses GPIO matrix for SPI3 pins, no IO_MUX possible
73// Set defaults to the pins used by SPI2 in Octal mode
74#define MICROPY_HW_SPI2_SCK (36)
75#define MICROPY_HW_SPI2_MOSI (35)
76#define MICROPY_HW_SPI2_MISO (37)
77#endif
Damien Georgeb24fcd72021-03-11 13:32:00 +110078#endif
79
Eric Poulsen9123c8d2017-11-28 18:48:30 -080080#define MP_HW_SPI_MAX_XFER_BYTES (4092)
81#define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8
82
Damien Georgeb24fcd72021-03-11 13:32:00 +110083typedef struct _machine_hw_spi_default_pins_t {
84 int8_t sck;
85 int8_t mosi;
86 int8_t miso;
87} machine_hw_spi_default_pins_t;
88
Damien Georgebc08c882016-12-06 12:20:10 +110089typedef struct _machine_hw_spi_obj_t {
90 mp_obj_base_t base;
91 spi_host_device_t host;
92 uint32_t baudrate;
93 uint8_t polarity;
94 uint8_t phase;
95 uint8_t bits;
96 uint8_t firstbit;
97 int8_t sck;
98 int8_t mosi;
99 int8_t miso;
100 spi_device_handle_t spi;
101 enum {
102 MACHINE_HW_SPI_STATE_NONE,
103 MACHINE_HW_SPI_STATE_INIT,
104 MACHINE_HW_SPI_STATE_DEINIT
105 } state;
106} machine_hw_spi_obj_t;
107
Damien Georgeb24fcd72021-03-11 13:32:00 +1100108// Default pin mappings for the hardware SPI instances
Damien Georgeb2adfc82023-07-25 11:33:51 +1000109STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[MICROPY_HW_SPI_MAX] = {
Damien Georgeb24fcd72021-03-11 13:32:00 +1100110 { .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
Tomas Vaneke7611522021-11-29 16:49:11 +0100111 #ifdef MICROPY_HW_SPI2_SCK
Damien Georgeb24fcd72021-03-11 13:32:00 +1100112 { .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
Tomas Vaneke7611522021-11-29 16:49:11 +0100113 #endif
Damien Georgeb24fcd72021-03-11 13:32:00 +1100114};
115
Damien Georgeb2adfc82023-07-25 11:33:51 +1000116// Static objects mapping to SPI2 (and SPI3 if available) hardware peripherals.
117STATIC machine_hw_spi_obj_t machine_hw_spi_obj[MICROPY_HW_SPI_MAX];
Damien Georgeda72bb62019-01-23 23:47:36 +1100118
Damien Georgebc08c882016-12-06 12:20:10 +1100119STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
120 switch (spi_bus_remove_device(self->spi)) {
121 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100122 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100123 return;
124
125 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100126 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI device already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100127 return;
128 }
129
130 switch (spi_bus_free(self->host)) {
131 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100132 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100133 return;
134
135 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100136 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100137 return;
138 }
139
140 int8_t pins[3] = {self->miso, self->mosi, self->sck};
141
142 for (int i = 0; i < 3; i++) {
143 if (pins[i] != -1) {
Damien Georgee4650122023-05-09 09:52:54 +1000144 esp_rom_gpio_pad_select_gpio(pins[i]);
145 esp_rom_gpio_connect_out_signal(pins[i], SIG_GPIO_OUT_IDX, false, false);
Damien Georgebc08c882016-12-06 12:20:10 +1100146 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
147 }
148 }
149}
150
151STATIC void machine_hw_spi_init_internal(
Damien George69661f32020-02-27 15:36:53 +1100152 machine_hw_spi_obj_t *self,
153 int8_t host,
154 int32_t baudrate,
155 int8_t polarity,
156 int8_t phase,
157 int8_t bits,
158 int8_t firstbit,
159 int8_t sck,
160 int8_t mosi,
161 int8_t miso) {
Damien Georgebc08c882016-12-06 12:20:10 +1100162
163 // if we're not initialized, then we're
164 // implicitly 'changed', since this is the init routine
165 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
166
167 esp_err_t ret;
168
169 machine_hw_spi_obj_t old_self = *self;
170
171 if (host != -1 && host != self->host) {
172 self->host = host;
173 changed = true;
174 }
175
Jonathan Hoggeb3029c2021-07-12 11:35:25 +0100176 if (baudrate != -1) {
177 // calculate the actual clock frequency that the SPI peripheral can produce
178 baudrate = spi_get_actual_clock(APB_CLK_FREQ, baudrate, 0);
179 if (baudrate != self->baudrate) {
180 self->baudrate = baudrate;
181 changed = true;
182 }
Damien Georgebc08c882016-12-06 12:20:10 +1100183 }
184
185 if (polarity != -1 && polarity != self->polarity) {
186 self->polarity = polarity;
187 changed = true;
188 }
189
190 if (phase != -1 && phase != self->phase) {
Damien George69661f32020-02-27 15:36:53 +1100191 self->phase = phase;
Damien Georgebc08c882016-12-06 12:20:10 +1100192 changed = true;
193 }
194
195 if (bits != -1 && bits != self->bits) {
196 self->bits = bits;
197 changed = true;
198 }
199
200 if (firstbit != -1 && firstbit != self->firstbit) {
201 self->firstbit = firstbit;
202 changed = true;
203 }
204
205 if (sck != -2 && sck != self->sck) {
206 self->sck = sck;
207 changed = true;
208 }
209
210 if (mosi != -2 && mosi != self->mosi) {
211 self->mosi = mosi;
212 changed = true;
213 }
214
215 if (miso != -2 && miso != self->miso) {
216 self->miso = miso;
217 changed = true;
218 }
219
Damien Georgebc08c882016-12-06 12:20:10 +1100220 if (changed) {
221 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
222 self->state = MACHINE_HW_SPI_STATE_DEINIT;
223 machine_hw_spi_deinit_internal(&old_self);
224 }
225 } else {
226 return; // no changes
227 }
228
229 spi_bus_config_t buscfg = {
230 .miso_io_num = self->miso,
231 .mosi_io_num = self->mosi,
232 .sclk_io_num = self->sck,
233 .quadwp_io_num = -1,
234 .quadhd_io_num = -1
235 };
236
237 spi_device_interface_config_t devcfg = {
238 .clock_speed_hz = self->baudrate,
239 .mode = self->phase | (self->polarity << 1),
240 .spics_io_num = -1, // No CS pin
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100241 .queue_size = 2,
Damien Georgebc08c882016-12-06 12:20:10 +1100242 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
243 .pre_cb = NULL
244 };
245
stijn84fa3312020-04-16 09:13:57 +0200246 // Initialize the SPI bus
Damien Georgebc08c882016-12-06 12:20:10 +1100247
Damien Georged82f3442019-01-23 23:40:06 +1100248 // Select DMA channel based on the hardware SPI host
249 int dma_chan = 0;
Damien George95c614e2022-10-04 17:43:04 +1100250 #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
251 dma_chan = SPI_DMA_CH_AUTO;
252 #else
Damien Georgee4650122023-05-09 09:52:54 +1000253 if (self->host == SPI2_HOST) {
Damien Georged82f3442019-01-23 23:40:06 +1100254 dma_chan = 1;
Damien George95c614e2022-10-04 17:43:04 +1100255 } else {
Damien Georged82f3442019-01-23 23:40:06 +1100256 dma_chan = 2;
257 }
Damien George95c614e2022-10-04 17:43:04 +1100258 #endif
Damien Georged82f3442019-01-23 23:40:06 +1100259
260 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100261 switch (ret) {
262 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100263 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100264 return;
265
266 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100267 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI host already in use"));
Damien Georgebc08c882016-12-06 12:20:10 +1100268 return;
269 }
270
271 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
272 switch (ret) {
273 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100274 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100275 spi_bus_free(self->host);
276 return;
277
278 case ESP_ERR_NO_MEM:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100279 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("out of memory"));
Damien Georgebc08c882016-12-06 12:20:10 +1100280 spi_bus_free(self->host);
281 return;
282
283 case ESP_ERR_NOT_FOUND:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100284 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("no free slots"));
Damien Georgebc08c882016-12-06 12:20:10 +1100285 spi_bus_free(self->host);
286 return;
287 }
288 self->state = MACHINE_HW_SPI_STATE_INIT;
289}
290
291STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
Damien George1a3e3862020-03-27 00:35:04 +1100292 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100293 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
294 self->state = MACHINE_HW_SPI_STATE_DEINIT;
295 machine_hw_spi_deinit_internal(self);
296 }
297}
298
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100299STATIC mp_uint_t gcd(mp_uint_t x, mp_uint_t y) {
300 while (x != y) {
301 if (x > y) {
302 x -= y;
303 } else {
304 y -= x;
305 }
306 }
307 return x;
308}
309
Damien Georgebc08c882016-12-06 12:20:10 +1100310STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
311 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
312
Damien Georgebc08c882016-12-06 12:20:10 +1100313 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100314 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("transfer on deinitialized SPI"));
Damien Georgebc08c882016-12-06 12:20:10 +1100315 return;
316 }
317
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800318 // Round to nearest whole set of bits
319 int bits_to_send = len * 8 / self->bits * self->bits;
320
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100321 if (!bits_to_send) {
322 mp_raise_ValueError(MP_ERROR_TEXT("buffer too short"));
323 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800324
325 if (len <= 4) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100326 spi_transaction_t transaction = { 0 };
327
Damien Georgebc08c882016-12-06 12:20:10 +1100328 if (src != NULL) {
329 memcpy(&transaction.tx_data, src, len);
330 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800331
332 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
333 transaction.length = bits_to_send;
334 spi_device_transmit(self->spi, &transaction);
335
336 if (dest != NULL) {
337 memcpy(dest, &transaction.rx_data, len);
338 }
Damien Georgebc08c882016-12-06 12:20:10 +1100339 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800340 int offset = 0;
341 int bits_remaining = bits_to_send;
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100342 int optimum_word_size = 8 * self->bits / gcd(8, self->bits);
343 int max_transaction_bits = MP_HW_SPI_MAX_XFER_BITS / optimum_word_size * optimum_word_size;
344 spi_transaction_t *transaction, *result, transactions[2];
345 int i = 0;
346
347 spi_device_acquire_bus(self->spi, portMAX_DELAY);
Damien Georgebc08c882016-12-06 12:20:10 +1100348
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800349 while (bits_remaining) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100350 transaction = transactions + i++ % 2;
351 memset(transaction, 0, sizeof(spi_transaction_t));
Damien Georgebc08c882016-12-06 12:20:10 +1100352
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100353 transaction->length =
354 bits_remaining > max_transaction_bits ? max_transaction_bits : bits_remaining;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800355
356 if (src != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100357 transaction->tx_buffer = src + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800358 }
359 if (dest != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100360 transaction->rx_buffer = dest + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800361 }
362
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100363 spi_device_queue_trans(self->spi, transaction, portMAX_DELAY);
364 bits_remaining -= transaction->length;
365
366 if (offset > 0) {
367 // wait for previously queued transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100368 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100369 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100370 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100371 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800372
373 // doesn't need ceil(); loop ends when bits_remaining is 0
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100374 offset += transaction->length / 8;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800375 }
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100376
377 // wait for last transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100378 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100379 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100380 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100381 spi_device_release_bus(self->spi);
Damien Georgebc08c882016-12-06 12:20:10 +1100382 }
383}
384
385/******************************************************************************/
386// MicroPython bindings for hw_spi
387
388STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
389 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
390 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 +1100391 self->host, self->baudrate, self->polarity,
392 self->phase, self->bits, self->firstbit,
393 self->sck, self->mosi, self->miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100394}
395
396STATIC 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 +1100397 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100398
399 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
400 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100401 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100402 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
403 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
404 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
405 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
406 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
407 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
408 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
409 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
410 };
411
412 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
413 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
Damien George69661f32020-02-27 15:36:53 +1100414 allowed_args, args);
Damien Georgebc08c882016-12-06 12:20:10 +1100415 int8_t sck, mosi, miso;
416
417 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
418 sck = -2;
419 } else if (args[ARG_sck].u_obj == mp_const_none) {
420 sck = -1;
421 } else {
422 sck = machine_pin_get_id(args[ARG_sck].u_obj);
423 }
424
425 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
426 miso = -2;
427 } else if (args[ARG_miso].u_obj == mp_const_none) {
428 miso = -1;
429 } else {
430 miso = machine_pin_get_id(args[ARG_miso].u_obj);
431 }
432
433 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
434 mosi = -2;
435 } else if (args[ARG_mosi].u_obj == mp_const_none) {
436 mosi = -1;
437 } else {
438 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
439 }
440
441 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
Damien George69661f32020-02-27 15:36:53 +1100442 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
443 args[ARG_firstbit].u_int, sck, mosi, miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100444}
445
446mp_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 +1000447 MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
448
Damien Georgebc08c882016-12-06 12:20:10 +1100449 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
450 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100451 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100452 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
453 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
454 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
455 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
456 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
457 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
458 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
459 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
460 };
461 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
462 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
463
Damien Georgeda72bb62019-01-23 23:47:36 +1100464 machine_hw_spi_obj_t *self;
Damien Georgeb24fcd72021-03-11 13:32:00 +1100465 const machine_hw_spi_default_pins_t *default_pins;
Damien Georgeb2adfc82023-07-25 11:33:51 +1000466 mp_int_t spi_id = args[ARG_id].u_int;
467 if (1 <= spi_id && spi_id <= MICROPY_HW_SPI_MAX) {
468 self = &machine_hw_spi_obj[spi_id - 1];
469 default_pins = &machine_hw_spi_default_pins[spi_id - 1];
Damien Georgeda72bb62019-01-23 23:47:36 +1100470 } else {
Damien Georgeb2adfc82023-07-25 11:33:51 +1000471 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
Damien Georgeda72bb62019-01-23 23:47:36 +1100472 }
Damien Georgeb2adfc82023-07-25 11:33:51 +1000473
iabdalkader427670c2022-10-06 20:20:03 +0200474 self->base.type = &machine_spi_type;
Damien Georgebc08c882016-12-06 12:20:10 +1100475
Jonathan Hogg89945b12021-06-16 13:31:46 +0100476 int8_t sck, mosi, miso;
477
478 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
479 sck = default_pins->sck;
480 } else if (args[ARG_sck].u_obj == mp_const_none) {
481 sck = -1;
482 } else {
483 sck = machine_pin_get_id(args[ARG_sck].u_obj);
484 }
485
486 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
487 mosi = default_pins->mosi;
488 } else if (args[ARG_mosi].u_obj == mp_const_none) {
489 mosi = -1;
490 } else {
491 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
492 }
493
494 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
495 miso = default_pins->miso;
496 } else if (args[ARG_miso].u_obj == mp_const_none) {
497 miso = -1;
498 } else {
499 miso = machine_pin_get_id(args[ARG_miso].u_obj);
500 }
501
Damien Georgebc08c882016-12-06 12:20:10 +1100502 machine_hw_spi_init_internal(
503 self,
504 args[ARG_id].u_int,
505 args[ARG_baudrate].u_int,
506 args[ARG_polarity].u_int,
507 args[ARG_phase].u_int,
508 args[ARG_bits].u_int,
509 args[ARG_firstbit].u_int,
Jonathan Hogg89945b12021-06-16 13:31:46 +0100510 sck,
511 mosi,
512 miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100513
514 return MP_OBJ_FROM_PTR(self);
515}
516
Damien Tournoude982c1d2023-01-12 21:04:07 -0800517spi_host_device_t machine_hw_spi_get_host(mp_obj_t in) {
518 if (mp_obj_get_type(in) != &machine_spi_type) {
519 mp_raise_ValueError(MP_ERROR_TEXT("expecting a SPI object"));
520 }
521 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)in;
522 return self->host;
523}
524
Damien Georgebc08c882016-12-06 12:20:10 +1100525STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
526 .init = machine_hw_spi_init,
527 .deinit = machine_hw_spi_deinit,
528 .transfer = machine_hw_spi_transfer,
529};
530
Jim Mussared662b9762021-07-14 14:38:38 +1000531MP_DEFINE_CONST_OBJ_TYPE(
iabdalkader427670c2022-10-06 20:20:03 +0200532 machine_spi_type,
Jim Mussared662b9762021-07-14 14:38:38 +1000533 MP_QSTR_SPI,
534 MP_TYPE_FLAG_NONE,
Jim Mussared94beeab2022-09-17 00:31:23 +1000535 make_new, machine_hw_spi_make_new,
Jim Mussared662b9762021-07-14 14:38:38 +1000536 print, machine_hw_spi_print,
537 protocol, &machine_hw_spi_p,
Jim Mussared9dce8272022-06-24 16:27:46 +1000538 locals_dict, &mp_machine_spi_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000539 );