blob: 1515b007440f54af202c1ea73b8fc6b63876df69 [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
Tomas Vanekd0888652021-11-29 12:57:28 +010039// SPI mappings by device, naming used by IDF old/new
40// upython | ESP32 | ESP32S2 | ESP32S3 | ESP32C3
41// ----------+-----------+-----------+---------+---------
42// SPI(id=1) | HSPI/SPI2 | FSPI/SPI2 | SPI2 | SPI2
43// SPI(id=2) | VSPI/SPI3 | HSPI/SPI3 | SPI3 | err
44
45// Default pins for SPI(id=1) aka IDF SPI2, can be overridden by a board
Damien Georgeb24fcd72021-03-11 13:32:00 +110046#ifndef MICROPY_HW_SPI1_SCK
Tomas Vanekd0888652021-11-29 12:57:28 +010047#ifdef SPI2_IOMUX_PIN_NUM_CLK
48// Use IO_MUX pins by default.
49// If SPI lines are routed to other pins through GPIO matrix
50// routing adds some delay and lower limit applies to SPI clk freq
51#define MICROPY_HW_SPI1_SCK SPI2_IOMUX_PIN_NUM_CLK // pin 14 on ESP32
52#define MICROPY_HW_SPI1_MOSI SPI2_IOMUX_PIN_NUM_MOSI // pin 13 on ESP32
53#define MICROPY_HW_SPI1_MISO SPI2_IOMUX_PIN_NUM_MISO // pin 12 on ESP32
54// Only for compatibility with IDF 4.2 and older
55#elif CONFIG_IDF_TARGET_ESP32S2
56#define MICROPY_HW_SPI1_SCK FSPI_IOMUX_PIN_NUM_CLK
57#define MICROPY_HW_SPI1_MOSI FSPI_IOMUX_PIN_NUM_MOSI
58#define MICROPY_HW_SPI1_MISO FSPI_IOMUX_PIN_NUM_MISO
59#else
60#define MICROPY_HW_SPI1_SCK HSPI_IOMUX_PIN_NUM_CLK
61#define MICROPY_HW_SPI1_MOSI HSPI_IOMUX_PIN_NUM_MOSI
62#define MICROPY_HW_SPI1_MISO HSPI_IOMUX_PIN_NUM_MISO
63#endif
Damien Georgeb24fcd72021-03-11 13:32:00 +110064#endif
65
Tomas Vaneke7611522021-11-29 16:49:11 +010066// Default pins for SPI(id=2) aka IDF SPI3, can be overridden by a board
Damien Georgeb24fcd72021-03-11 13:32:00 +110067#ifndef MICROPY_HW_SPI2_SCK
Tomas Vaneke7611522021-11-29 16:49:11 +010068#if CONFIG_IDF_TARGET_ESP32
69// ESP32 has IO_MUX pins for VSPI/SPI3 lines, use them as defaults
70#define MICROPY_HW_SPI2_SCK VSPI_IOMUX_PIN_NUM_CLK // pin 18
71#define MICROPY_HW_SPI2_MOSI VSPI_IOMUX_PIN_NUM_MOSI // pin 23
72#define MICROPY_HW_SPI2_MISO VSPI_IOMUX_PIN_NUM_MISO // pin 19
73#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
74// ESP32S2 and S3 uses GPIO matrix for SPI3 pins, no IO_MUX possible
75// Set defaults to the pins used by SPI2 in Octal mode
76#define MICROPY_HW_SPI2_SCK (36)
77#define MICROPY_HW_SPI2_MOSI (35)
78#define MICROPY_HW_SPI2_MISO (37)
79#endif
Damien Georgeb24fcd72021-03-11 13:32:00 +110080#endif
81
Eric Poulsen9123c8d2017-11-28 18:48:30 -080082#define MP_HW_SPI_MAX_XFER_BYTES (4092)
83#define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8
84
Damien George68235142021-07-18 12:11:21 +100085#if CONFIG_IDF_TARGET_ESP32C3
86#define HSPI_HOST SPI2_HOST
Damien George54d33b22021-09-16 22:16:36 +100087#elif CONFIG_IDF_TARGET_ESP32S3
88#define HSPI_HOST SPI3_HOST
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +100089#define FSPI_HOST SPI2_HOST
Damien George68235142021-07-18 12:11:21 +100090#endif
91
Damien Georgeb24fcd72021-03-11 13:32:00 +110092typedef struct _machine_hw_spi_default_pins_t {
93 int8_t sck;
94 int8_t mosi;
95 int8_t miso;
96} machine_hw_spi_default_pins_t;
97
Damien Georgebc08c882016-12-06 12:20:10 +110098typedef struct _machine_hw_spi_obj_t {
99 mp_obj_base_t base;
100 spi_host_device_t host;
101 uint32_t baudrate;
102 uint8_t polarity;
103 uint8_t phase;
104 uint8_t bits;
105 uint8_t firstbit;
106 int8_t sck;
107 int8_t mosi;
108 int8_t miso;
109 spi_device_handle_t spi;
110 enum {
111 MACHINE_HW_SPI_STATE_NONE,
112 MACHINE_HW_SPI_STATE_INIT,
113 MACHINE_HW_SPI_STATE_DEINIT
114 } state;
115} machine_hw_spi_obj_t;
116
Damien Georgeb24fcd72021-03-11 13:32:00 +1100117// Default pin mappings for the hardware SPI instances
118STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = {
119 { .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
Tomas Vaneke7611522021-11-29 16:49:11 +0100120 #ifdef MICROPY_HW_SPI2_SCK
Damien Georgeb24fcd72021-03-11 13:32:00 +1100121 { .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
Tomas Vaneke7611522021-11-29 16:49:11 +0100122 #endif
Damien Georgeb24fcd72021-03-11 13:32:00 +1100123};
124
Damien Georgeda72bb62019-01-23 23:47:36 +1100125// Static objects mapping to HSPI and VSPI hardware peripherals
126STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2];
127
Damien Georgebc08c882016-12-06 12:20:10 +1100128STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
129 switch (spi_bus_remove_device(self->spi)) {
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 device already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100136 return;
137 }
138
139 switch (spi_bus_free(self->host)) {
140 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100141 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100142 return;
143
144 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100145 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
Damien Georgebc08c882016-12-06 12:20:10 +1100146 return;
147 }
148
149 int8_t pins[3] = {self->miso, self->mosi, self->sck};
150
151 for (int i = 0; i < 3; i++) {
152 if (pins[i] != -1) {
153 gpio_pad_select_gpio(pins[i]);
154 gpio_matrix_out(pins[i], SIG_GPIO_OUT_IDX, false, false);
155 gpio_set_direction(pins[i], GPIO_MODE_INPUT);
156 }
157 }
158}
159
160STATIC void machine_hw_spi_init_internal(
Damien George69661f32020-02-27 15:36:53 +1100161 machine_hw_spi_obj_t *self,
162 int8_t host,
163 int32_t baudrate,
164 int8_t polarity,
165 int8_t phase,
166 int8_t bits,
167 int8_t firstbit,
168 int8_t sck,
169 int8_t mosi,
170 int8_t miso) {
Damien Georgebc08c882016-12-06 12:20:10 +1100171
172 // if we're not initialized, then we're
173 // implicitly 'changed', since this is the init routine
174 bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
175
176 esp_err_t ret;
177
178 machine_hw_spi_obj_t old_self = *self;
179
180 if (host != -1 && host != self->host) {
181 self->host = host;
182 changed = true;
183 }
184
Jonathan Hoggeb3029c2021-07-12 11:35:25 +0100185 if (baudrate != -1) {
186 // calculate the actual clock frequency that the SPI peripheral can produce
187 baudrate = spi_get_actual_clock(APB_CLK_FREQ, baudrate, 0);
188 if (baudrate != self->baudrate) {
189 self->baudrate = baudrate;
190 changed = true;
191 }
Damien Georgebc08c882016-12-06 12:20:10 +1100192 }
193
194 if (polarity != -1 && polarity != self->polarity) {
195 self->polarity = polarity;
196 changed = true;
197 }
198
199 if (phase != -1 && phase != self->phase) {
Damien George69661f32020-02-27 15:36:53 +1100200 self->phase = phase;
Damien Georgebc08c882016-12-06 12:20:10 +1100201 changed = true;
202 }
203
204 if (bits != -1 && bits != self->bits) {
205 self->bits = bits;
206 changed = true;
207 }
208
209 if (firstbit != -1 && firstbit != self->firstbit) {
210 self->firstbit = firstbit;
211 changed = true;
212 }
213
214 if (sck != -2 && sck != self->sck) {
215 self->sck = sck;
216 changed = true;
217 }
218
219 if (mosi != -2 && mosi != self->mosi) {
220 self->mosi = mosi;
221 changed = true;
222 }
223
224 if (miso != -2 && miso != self->miso) {
225 self->miso = miso;
226 changed = true;
227 }
228
Damien George66a86a02021-02-18 21:24:34 +1100229 if (self->host != HSPI_HOST
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +1000230 #ifdef FSPI_HOST
231 && self->host != FSPI_HOST
232 #endif
Damien George66a86a02021-02-18 21:24:34 +1100233 #ifdef VSPI_HOST
234 && self->host != VSPI_HOST
235 #endif
236 ) {
237 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host);
Damien Georgebc08c882016-12-06 12:20:10 +1100238 }
239
240 if (changed) {
241 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
242 self->state = MACHINE_HW_SPI_STATE_DEINIT;
243 machine_hw_spi_deinit_internal(&old_self);
244 }
245 } else {
246 return; // no changes
247 }
248
249 spi_bus_config_t buscfg = {
250 .miso_io_num = self->miso,
251 .mosi_io_num = self->mosi,
252 .sclk_io_num = self->sck,
253 .quadwp_io_num = -1,
254 .quadhd_io_num = -1
255 };
256
257 spi_device_interface_config_t devcfg = {
258 .clock_speed_hz = self->baudrate,
259 .mode = self->phase | (self->polarity << 1),
260 .spics_io_num = -1, // No CS pin
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100261 .queue_size = 2,
Damien Georgebc08c882016-12-06 12:20:10 +1100262 .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0,
263 .pre_cb = NULL
264 };
265
stijn84fa3312020-04-16 09:13:57 +0200266 // Initialize the SPI bus
Damien Georgebc08c882016-12-06 12:20:10 +1100267
Damien Georged82f3442019-01-23 23:40:06 +1100268 // Select DMA channel based on the hardware SPI host
269 int dma_chan = 0;
270 if (self->host == HSPI_HOST) {
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +1000271 #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
272 dma_chan = 3;
273 #else
Damien Georged82f3442019-01-23 23:40:06 +1100274 dma_chan = 1;
Seon Rozenblum13e6e0d2021-09-17 19:44:06 +1000275 #endif
276 #ifdef FSPI_HOST
277 } else if (self->host == FSPI_HOST) {
278 dma_chan = 1;
279 #endif
Damien George66a86a02021-02-18 21:24:34 +1100280 #ifdef VSPI_HOST
Damien Georged82f3442019-01-23 23:40:06 +1100281 } else if (self->host == VSPI_HOST) {
282 dma_chan = 2;
Damien George66a86a02021-02-18 21:24:34 +1100283 #endif
Damien Georged82f3442019-01-23 23:40:06 +1100284 }
285
286 ret = spi_bus_initialize(self->host, &buscfg, dma_chan);
Damien Georgebc08c882016-12-06 12:20:10 +1100287 switch (ret) {
288 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100289 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100290 return;
291
292 case ESP_ERR_INVALID_STATE:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100293 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI host already in use"));
Damien Georgebc08c882016-12-06 12:20:10 +1100294 return;
295 }
296
297 ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
298 switch (ret) {
299 case ESP_ERR_INVALID_ARG:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100300 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
Damien Georgebc08c882016-12-06 12:20:10 +1100301 spi_bus_free(self->host);
302 return;
303
304 case ESP_ERR_NO_MEM:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100305 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("out of memory"));
Damien Georgebc08c882016-12-06 12:20:10 +1100306 spi_bus_free(self->host);
307 return;
308
309 case ESP_ERR_NOT_FOUND:
Jim Mussareddef76fe2020-03-02 22:35:22 +1100310 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("no free slots"));
Damien Georgebc08c882016-12-06 12:20:10 +1100311 spi_bus_free(self->host);
312 return;
313 }
314 self->state = MACHINE_HW_SPI_STATE_INIT;
315}
316
317STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
Damien George1a3e3862020-03-27 00:35:04 +1100318 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100319 if (self->state == MACHINE_HW_SPI_STATE_INIT) {
320 self->state = MACHINE_HW_SPI_STATE_DEINIT;
321 machine_hw_spi_deinit_internal(self);
322 }
323}
324
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100325STATIC mp_uint_t gcd(mp_uint_t x, mp_uint_t y) {
326 while (x != y) {
327 if (x > y) {
328 x -= y;
329 } else {
330 y -= x;
331 }
332 }
333 return x;
334}
335
Damien Georgebc08c882016-12-06 12:20:10 +1100336STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
337 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
338
Damien Georgebc08c882016-12-06 12:20:10 +1100339 if (self->state == MACHINE_HW_SPI_STATE_DEINIT) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100340 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("transfer on deinitialized SPI"));
Damien Georgebc08c882016-12-06 12:20:10 +1100341 return;
342 }
343
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800344 // Round to nearest whole set of bits
345 int bits_to_send = len * 8 / self->bits * self->bits;
346
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100347 if (!bits_to_send) {
348 mp_raise_ValueError(MP_ERROR_TEXT("buffer too short"));
349 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800350
351 if (len <= 4) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100352 spi_transaction_t transaction = { 0 };
353
Damien Georgebc08c882016-12-06 12:20:10 +1100354 if (src != NULL) {
355 memcpy(&transaction.tx_data, src, len);
356 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800357
358 transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
359 transaction.length = bits_to_send;
360 spi_device_transmit(self->spi, &transaction);
361
362 if (dest != NULL) {
363 memcpy(dest, &transaction.rx_data, len);
364 }
Damien Georgebc08c882016-12-06 12:20:10 +1100365 } else {
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800366 int offset = 0;
367 int bits_remaining = bits_to_send;
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100368 int optimum_word_size = 8 * self->bits / gcd(8, self->bits);
369 int max_transaction_bits = MP_HW_SPI_MAX_XFER_BITS / optimum_word_size * optimum_word_size;
370 spi_transaction_t *transaction, *result, transactions[2];
371 int i = 0;
372
373 spi_device_acquire_bus(self->spi, portMAX_DELAY);
Damien Georgebc08c882016-12-06 12:20:10 +1100374
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800375 while (bits_remaining) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100376 transaction = transactions + i++ % 2;
377 memset(transaction, 0, sizeof(spi_transaction_t));
Damien Georgebc08c882016-12-06 12:20:10 +1100378
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100379 transaction->length =
380 bits_remaining > max_transaction_bits ? max_transaction_bits : bits_remaining;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800381
382 if (src != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100383 transaction->tx_buffer = src + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800384 }
385 if (dest != NULL) {
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100386 transaction->rx_buffer = dest + offset;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800387 }
388
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100389 spi_device_queue_trans(self->spi, transaction, portMAX_DELAY);
390 bits_remaining -= transaction->length;
391
392 if (offset > 0) {
393 // wait for previously queued transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100394 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100395 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100396 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100397 }
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800398
399 // doesn't need ceil(); loop ends when bits_remaining is 0
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100400 offset += transaction->length / 8;
Eric Poulsen9123c8d2017-11-28 18:48:30 -0800401 }
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100402
403 // wait for last transaction
Jonathan Hogg5b655662021-08-15 17:13:16 +0100404 MP_THREAD_GIL_EXIT();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100405 spi_device_get_trans_result(self->spi, &result, portMAX_DELAY);
Jonathan Hogg5b655662021-08-15 17:13:16 +0100406 MP_THREAD_GIL_ENTER();
Jonathan Hogg8be29b92021-07-16 16:12:45 +0100407 spi_device_release_bus(self->spi);
Damien Georgebc08c882016-12-06 12:20:10 +1100408 }
409}
410
411/******************************************************************************/
412// MicroPython bindings for hw_spi
413
414STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
415 machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
416 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 +1100417 self->host, self->baudrate, self->polarity,
418 self->phase, self->bits, self->firstbit,
419 self->sck, self->mosi, self->miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100420}
421
422STATIC 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 +1100423 machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t *)self_in;
Damien Georgebc08c882016-12-06 12:20:10 +1100424
425 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
426 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100427 { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100428 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
429 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
430 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
431 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
432 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
433 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
434 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
435 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
436 };
437
438 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
439 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
Damien George69661f32020-02-27 15:36:53 +1100440 allowed_args, args);
Damien Georgebc08c882016-12-06 12:20:10 +1100441 int8_t sck, mosi, miso;
442
443 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
444 sck = -2;
445 } else if (args[ARG_sck].u_obj == mp_const_none) {
446 sck = -1;
447 } else {
448 sck = machine_pin_get_id(args[ARG_sck].u_obj);
449 }
450
451 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
452 miso = -2;
453 } else if (args[ARG_miso].u_obj == mp_const_none) {
454 miso = -1;
455 } else {
456 miso = machine_pin_get_id(args[ARG_miso].u_obj);
457 }
458
459 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
460 mosi = -2;
461 } else if (args[ARG_mosi].u_obj == mp_const_none) {
462 mosi = -1;
463 } else {
464 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
465 }
466
467 machine_hw_spi_init_internal(self, args[ARG_id].u_int, args[ARG_baudrate].u_int,
Damien George69661f32020-02-27 15:36:53 +1100468 args[ARG_polarity].u_int, args[ARG_phase].u_int, args[ARG_bits].u_int,
469 args[ARG_firstbit].u_int, sck, mosi, miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100470}
471
472mp_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 +1000473 MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args);
474
Damien Georgebc08c882016-12-06 12:20:10 +1100475 enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
476 static const mp_arg_t allowed_args[] = {
Damien Georged1fd8892017-12-18 15:46:08 +1100477 { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} },
Damien Georgebc08c882016-12-06 12:20:10 +1100478 { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
479 { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
480 { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
481 { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
482 { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
483 { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
484 { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
485 { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
486 };
487 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
488 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
489
Damien Georgeda72bb62019-01-23 23:47:36 +1100490 machine_hw_spi_obj_t *self;
Damien Georgeb24fcd72021-03-11 13:32:00 +1100491 const machine_hw_spi_default_pins_t *default_pins;
Tomas Vanek3305ec42021-12-01 18:34:39 +0100492 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 +1100493 self = &machine_hw_spi_obj[0];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100494 default_pins = &machine_hw_spi_default_pins[0];
Damien Georgeda72bb62019-01-23 23:47:36 +1100495 } else {
496 self = &machine_hw_spi_obj[1];
Damien Georgeb24fcd72021-03-11 13:32:00 +1100497 default_pins = &machine_hw_spi_default_pins[1];
Damien Georgeda72bb62019-01-23 23:47:36 +1100498 }
Damien Georgebc08c882016-12-06 12:20:10 +1100499 self->base.type = &machine_hw_spi_type;
500
Jonathan Hogg89945b12021-06-16 13:31:46 +0100501 int8_t sck, mosi, miso;
502
503 if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
504 sck = default_pins->sck;
505 } else if (args[ARG_sck].u_obj == mp_const_none) {
506 sck = -1;
507 } else {
508 sck = machine_pin_get_id(args[ARG_sck].u_obj);
509 }
510
511 if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
512 mosi = default_pins->mosi;
513 } else if (args[ARG_mosi].u_obj == mp_const_none) {
514 mosi = -1;
515 } else {
516 mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
517 }
518
519 if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
520 miso = default_pins->miso;
521 } else if (args[ARG_miso].u_obj == mp_const_none) {
522 miso = -1;
523 } else {
524 miso = machine_pin_get_id(args[ARG_miso].u_obj);
525 }
526
Damien Georgebc08c882016-12-06 12:20:10 +1100527 machine_hw_spi_init_internal(
528 self,
529 args[ARG_id].u_int,
530 args[ARG_baudrate].u_int,
531 args[ARG_polarity].u_int,
532 args[ARG_phase].u_int,
533 args[ARG_bits].u_int,
534 args[ARG_firstbit].u_int,
Jonathan Hogg89945b12021-06-16 13:31:46 +0100535 sck,
536 mosi,
537 miso);
Damien Georgebc08c882016-12-06 12:20:10 +1100538
539 return MP_OBJ_FROM_PTR(self);
540}
541
542STATIC const mp_machine_spi_p_t machine_hw_spi_p = {
543 .init = machine_hw_spi_init,
544 .deinit = machine_hw_spi_deinit,
545 .transfer = machine_hw_spi_transfer,
546};
547
548const mp_obj_type_t machine_hw_spi_type = {
549 { &mp_type_type },
550 .name = MP_QSTR_SPI,
551 .print = machine_hw_spi_print,
552 .make_new = machine_hw_spi_make_new,
553 .protocol = &machine_hw_spi_p,
Damien George1a3e3862020-03-27 00:35:04 +1100554 .locals_dict = (mp_obj_dict_t *)&mp_machine_spi_locals_dict,
Damien Georgebc08c882016-12-06 12:20:10 +1100555};