Skip to content

Commit 0f048a5

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt/machine_spi: Allow a setting of -1 for cs in the constructor.
In that case, no Pin will be configured for the CS signal, even if it is internally still generated. That setting allows to use any pin for CS, which then must be controlled by the Python script. Also make the default cs=-1 to match other ports (software CS).
1 parent 8e54225 commit 0f048a5

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

docs/mimxrt/pinout.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ Olimex RT1010Py - CS0/-/SDO/SDI/SCK SDCARD wi
322322
Seeed ARCH MIX J4_12/-/J4_14/J4_13/J4_15 J3_09/J3_05/J3_08_J3_11
323323
================= ========================= ======================= ===============
324324

325-
Pins denoted with (*) are by default not wired at the board.
325+
Pins denoted with (*) are by default not wired at the board. The CS0 and CS1 signals
326+
are enabled with the keyword option cs=0 or cs=1 of the SPI object constructor.
326327

327328
.. _mimxrt_i2c_pinout:
328329

docs/mimxrt/quickref.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,19 @@ There are up to four hardware SPI channels that allow faster transmission
301301
rates (up to 30Mhz). Hardware SPI is accessed via the
302302
:ref:`machine.SPI <machine.SPI>` class and has the same methods as software SPI above::
303303

304-
from machine import SPI
304+
from machine import SPI, Pin
305305

306306
spi = SPI(0, 10000000)
307+
cs_pin = Pin(6, Pin.OUT, value=1)
308+
cs_pin(0)
307309
spi.write('Hello World')
310+
cs_pin(1)
308311

309312
For the assignment of Pins to SPI signals, refer to
310313
:ref:`Hardware SPI pinout <mimxrt_spi_pinout>`.
314+
The keyword option cs=n can be used to enable the cs pin 0 or 1 for an automatic cs signal. The
315+
default is cs=-1. Using cs=-1 the automatic cs signal is not created.
316+
In that case, cs has to be set by the script. Clearing that assignment requires a power cycle.
311317

312318
Notes:
313319

ports/mimxrt/machine_spi.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static const iomux_table_t iomux_table[] = {
7777
IOMUX_TABLE_SPI
7878
};
7979

80-
bool lpspi_set_iomux(int8_t spi, uint8_t drive, uint8_t cs) {
80+
bool lpspi_set_iomux(int8_t spi, uint8_t drive, int8_t cs) {
8181
int index = (spi - 1) * 5;
8282

8383
if (SCK.muxRegister != 0) {
@@ -93,7 +93,7 @@ bool lpspi_set_iomux(int8_t spi, uint8_t drive, uint8_t cs) {
9393
IOMUXC_SetPinMux(CS1.muxRegister, CS1.muxMode, CS1.inputRegister, CS1.inputDaisy, CS1.configRegister, 0U);
9494
IOMUXC_SetPinConfig(CS1.muxRegister, CS1.muxMode, CS1.inputRegister, CS1.inputDaisy, CS1.configRegister,
9595
pin_generate_config(PIN_PULL_UP_100K, PIN_MODE_OUT, drive, CS1.configRegister));
96-
} else {
96+
} else if (cs != -1) {
9797
mp_raise_ValueError(MP_ERROR_TEXT("The chosen CS is not available"));
9898
}
9999

@@ -131,7 +131,7 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
131131
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT} },
132132
{ MP_QSTR_gap_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
133133
{ MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_DRIVE} },
134-
{ MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
134+
{ MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
135135
};
136136

137137
// Parse the arguments.
@@ -173,8 +173,11 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
173173
}
174174
self->master_config->lastSckToPcsDelayInNanoSec = self->master_config->betweenTransferDelayInNanoSec;
175175
self->master_config->pcsToSckDelayInNanoSec = self->master_config->betweenTransferDelayInNanoSec;
176-
uint8_t cs = args[ARG_cs].u_int;
177-
if (cs <= 1) {
176+
int8_t cs = args[ARG_cs].u_int;
177+
// In the SPI master_config for automatic CS the value cs=0 is set already,
178+
// so only cs=1 has to be addressed here. The case cs == -1 for manual CS is handled
179+
// in the function spi_set_iomux() and the value in the master_config can stay at 0.
180+
if (cs == 1) {
178181
self->master_config->whichPcs = cs;
179182
}
180183
LPSPI_MasterInit(self->spi_inst, self->master_config, BOARD_BOOTCLOCKRUN_LPSPI_CLK_ROOT);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy