Skip to content

Commit 639fed2

Browse files
Add slave_mode parameter to the SPI bus constructor
1 parent ebf19bd commit 639fed2

11 files changed

Lines changed: 43 additions & 12 deletions

File tree

  • ports
    • atmel-samd/common-hal/busio
    • broadcom/common-hal/busio
    • cxd56/common-hal/busio
    • espressif/common-hal/busio
    • mimxrt10xx/common-hal/busio
    • nrf/common-hal/busio
    • raspberrypi/common-hal/busio
    • silabs/common-hal/busio
    • stm/common-hal/busio
  • shared-bindings/busio

ports/atmel-samd/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
4646
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
47-
const mcu_pin_obj_t *miso, bool half_duplex) {
47+
const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
4848
Sercom *sercom = NULL;
4949
uint8_t sercom_index;
5050
uint32_t clock_pinmux = 0;
@@ -60,6 +60,9 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
6060
if (half_duplex) {
6161
mp_raise_NotImplementedError(MP_ERROR_TEXT("Half duplex SPI is not implemented"));
6262
}
63+
if (slave_mode) {
64+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
65+
}
6366

6467
// Ensure the object starts in its deinit state.
6568
self->clock_pin = NO_PIN;

ports/broadcom/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void reset_spi(void) {
7777

7878
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
7979
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
80-
const mcu_pin_obj_t *miso, bool half_duplex) {
80+
const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
8181
size_t instance_index = NUM_SPI;
8282
BP_Function_Enum clock_alt = 0;
8383
BP_Function_Enum mosi_alt = 0;
@@ -86,6 +86,9 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
8686
if (half_duplex) {
8787
mp_raise_NotImplementedError(MP_ERROR_TEXT("Half duplex SPI is not implemented"));
8888
}
89+
if (slave_mode) {
90+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
91+
}
8992

9093
// BCM_VERSION != 2711 have 3 SPI but as listed in peripherals/gen/pins.c two are on
9194
// index 0, once one index 0 SPI is found the other will throw an invalid_pins error.

ports/cxd56/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@
3636
#include "shared-bindings/microcontroller/Pin.h"
3737

3838
void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock,
39-
const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso, bool half_duplex) {
39+
const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
4040
int port = -1;
4141

4242
if (half_duplex) {
4343
mp_raise_NotImplementedError(MP_ERROR_TEXT("Half duplex SPI is not implemented"));
4444
}
45+
if (slave_mode) {
46+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
47+
}
4548

4649
if (clock->number == PIN_SPI4_SCK &&
4750
(mosi == NULL || mosi->number == PIN_SPI4_MOSI) &&

ports/espressif/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void set_spi_config(busio_spi_obj_t *self,
7777

7878
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
7979
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
80-
const mcu_pin_obj_t *miso, bool half_duplex) {
80+
const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
8181

8282
const spi_bus_config_t bus_config = {
8383
.mosi_io_num = mosi != NULL ? mosi->number : -1,
@@ -90,6 +90,9 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
9090
if (half_duplex) {
9191
mp_raise_NotImplementedError(MP_ERROR_TEXT("Half duplex SPI is not implemented"));
9292
}
93+
if (slave_mode) {
94+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
95+
}
9396

9497
for (spi_host_device_t host_id = SPI2_HOST; host_id < SOC_SPI_PERIPH_NUM; host_id++) {
9598
if (spi_bus_is_free(host_id)) {

ports/mimxrt10xx/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void spi_reset(void) {
9191

9292
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
9393
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
94-
const mcu_pin_obj_t *miso, bool half_duplex) {
94+
const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
9595

9696
const uint32_t sck_count = MP_ARRAY_SIZE(mcu_spi_sck_list);
9797
const uint32_t miso_count = MP_ARRAY_SIZE(mcu_spi_sdi_list);
@@ -101,6 +101,9 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
101101
if (half_duplex) {
102102
mp_raise_NotImplementedError(MP_ERROR_TEXT("Half duplex SPI is not implemented"));
103103
}
104+
if (slave_mode) {
105+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
106+
}
104107

105108
for (uint i = 0; i < sck_count; i++) {
106109
if (mcu_spi_sck_list[i].pin != clock) {

ports/nrf/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate)
143143
return 0;
144144
}
145145

146-
void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso, bool half_duplex) {
146+
void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
147147

148148
if (half_duplex) {
149149
mp_raise_NotImplementedError(MP_ERROR_TEXT("Half duplex SPI is not implemented"));
150150
}
151+
if (slave_mode) {
152+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
153+
}
151154

152155
// Find a free instance, with most desirable (highest freq and not shared) allocated first.
153156
self->spim_peripheral = NULL;

ports/raspberrypi/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ void reset_spi(void) {
5454

5555
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
5656
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
57-
const mcu_pin_obj_t *miso, bool half_duplex) {
57+
const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
5858
size_t instance_index = NO_INSTANCE;
5959

6060
if (half_duplex) {
6161
mp_raise_NotImplementedError(MP_ERROR_TEXT("Half duplex SPI is not implemented"));
6262
}
63+
if (slave_mode) {
64+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
65+
}
6366

6467
if (clock->number % 4 == 2) {
6568
instance_index = (clock->number / 8) % 2;

ports/silabs/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
5353
const mcu_pin_obj_t *sck,
5454
const mcu_pin_obj_t *mosi,
5555
const mcu_pin_obj_t *miso,
56-
bool half_duplex) {
56+
bool half_duplex, bool slave_mode) {
5757
Ecode_t sc = ECODE_OK;
5858

5959
if (half_duplex) {
6060
mp_raise_NotImplementedError(
6161
MP_ERROR_TEXT("Half duplex SPI is not implemented"));
6262
}
63+
if (slave_mode) {
64+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
65+
}
6366

6467
if ((sck != NULL) && (mosi != NULL) && (miso != NULL)) {
6568
if (sck->function_list[FN_EUSART1_SCLK] == 1

ports/stm/common-hal/busio/SPI.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ STATIC int check_pins(busio_spi_obj_t *self,
167167

168168
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
169169
const mcu_pin_obj_t *sck, const mcu_pin_obj_t *mosi,
170-
const mcu_pin_obj_t *miso, bool half_duplex) {
170+
const mcu_pin_obj_t *miso, bool half_duplex, bool slave_mode) {
171+
if (slave_mode) {
172+
mp_raise_NotImplementedError(MP_ERROR_TEXT("Slave mode SPI is not implemented"));
173+
}
171174

172175
int periph_index = check_pins(self, sck, mosi, miso);
173176
SPI_TypeDef *SPIx = mcu_spi_banks[periph_index - 1];

shared-bindings/busio/SPI.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
//| MOSI: Optional[microcontroller.Pin] = None,
7575
//| MISO: Optional[microcontroller.Pin] = None,
7676
//| half_duplex: bool = False,
77+
//| slave_mode: bool = False
7778
//| ) -> None:
7879
//| """Construct an SPI object on the given pins.
7980
//|
@@ -96,8 +97,10 @@
9697
//| :param ~microcontroller.Pin MOSI: the Main Out Selected In pin.
9798
//| :param ~microcontroller.Pin MISO: the Main In Selected Out pin.
9899
//| :param bool half_duplex: True when MOSI is used for bidirectional data. False when SPI is full-duplex or simplex.
100+
//| :param-bool slave_mode: True when the chip is operating as a slave. False when the chip is operating as a master.
99101
//|
100102
//| **Limitations:** ``half_duplex`` is available only on STM; other chips do not have the hardware support.
103+
//| **Limitations:** ``slave_mode`` is available only on SAMD51; other chips do not have the firmware support.
101104
//| """
102105
//| ...
103106

@@ -106,12 +109,13 @@
106109
STATIC mp_obj_t busio_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
107110
#if CIRCUITPY_BUSIO_SPI
108111
busio_spi_obj_t *self = mp_obj_malloc(busio_spi_obj_t, &busio_spi_type);
109-
enum { ARG_clock, ARG_MOSI, ARG_MISO, ARG_half_duplex };
112+
enum { ARG_clock, ARG_MOSI, ARG_MISO, ARG_half_duplex, ARG_slave_mode };
110113
static const mp_arg_t allowed_args[] = {
111114
{ MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ },
112115
{ MP_QSTR_MOSI, MP_ARG_OBJ, {.u_obj = mp_const_none} },
113116
{ MP_QSTR_MISO, MP_ARG_OBJ, {.u_obj = mp_const_none} },
114117
{ MP_QSTR_half_duplex, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_bool = false} },
118+
{ MP_QSTR_slave_mode, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_bool = false} },
115119
};
116120
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
117121
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -124,7 +128,7 @@ STATIC mp_obj_t busio_spi_make_new(const mp_obj_type_t *type, size_t n_args, siz
124128
mp_raise_ValueError(MP_ERROR_TEXT("Must provide MISO or MOSI pin"));
125129
}
126130

127-
common_hal_busio_spi_construct(self, clock, mosi, miso, args[ARG_half_duplex].u_bool);
131+
common_hal_busio_spi_construct(self, clock, mosi, miso, args[ARG_half_duplex].u_bool, args[ARG_slave_mode].u_bool);
128132
return MP_OBJ_FROM_PTR(self);
129133
#else
130134
raise_ValueError_invalid_pins();

0 commit comments

Comments
 (0)