Enable CS "active-high" device support

Reference https://github.com/adafruit/Adafruit_CircuitPython_BusDevice/issues/71

Add a new parameter cs_active_value for devices that require CS to use "active high" logic.

Update mpconfigboard.mk to disable pyb_nano_v2 from core build as its flash is too small now.
This commit is contained in:
Michael Thomson 2021-09-21 20:12:43 +01:00
parent 9689022fb7
commit 94ca3f594e
5 changed files with 11 additions and 6 deletions

View File

@ -20,3 +20,4 @@ CIRCUITPY_KEYPAD = 0
CIRCUITPY_MIDI = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_BUSDEVICE = 0

View File

@ -48,6 +48,7 @@
//|
//| :param ~busio.SPI spi: The SPI bus the device is on
//| :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the DigitalInOut API.
//| :param bool cs_active_value: Set to true if your device requires CS to be active high. Defaults to false.
//| :param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.)
//|
//| Example::
@ -73,10 +74,11 @@
STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
adafruit_bus_device_spidevice_obj_t *self = m_new_obj(adafruit_bus_device_spidevice_obj_t);
self->base.type = &adafruit_bus_device_spidevice_type;
enum { ARG_spi, ARG_chip_select, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks };
enum { ARG_spi, ARG_chip_select, ARG_cs_active_value, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_cs_active_value, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
@ -87,7 +89,7 @@ STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type
busio_spi_obj_t *spi = args[ARG_spi].u_obj;
common_hal_adafruit_bus_device_spidevice_construct(MP_OBJ_TO_PTR(self), spi, args[ARG_chip_select].u_obj, args[ARG_baudrate].u_int, args[ARG_polarity].u_int,
common_hal_adafruit_bus_device_spidevice_construct(MP_OBJ_TO_PTR(self), spi, args[ARG_chip_select].u_obj, args[ARG_cs_active_value].u_bool, args[ARG_baudrate].u_int, args[ARG_polarity].u_int,
args[ARG_phase].u_int, args[ARG_extra_clocks].u_int);
if (args[ARG_chip_select].u_obj != MP_OBJ_NULL) {

View File

@ -43,7 +43,7 @@ extern const mp_obj_type_t adafruit_bus_device_spidevice_type;
// Initializes the hardware peripheral.
extern void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks);
bool cs_active_value, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks);
extern mp_obj_t common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self);
extern void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self);

View File

@ -32,13 +32,14 @@
#include "py/runtime.h"
void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks) {
bool cs_active_value, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks) {
self->spi = spi;
self->baudrate = baudrate;
self->polarity = polarity;
self->phase = phase;
self->extra_clocks = extra_clocks;
self->chip_select = cs;
self->cs_active_value = cs_active_value;
}
mp_obj_t common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self) {
@ -66,14 +67,14 @@ mp_obj_t common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spid
}
if (self->chip_select != MP_OBJ_NULL) {
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), false);
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), self->cs_active_value);
}
return self->spi;
}
void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self) {
if (self->chip_select != MP_OBJ_NULL) {
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), true);
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), !(self->cs_active_value));
}
if (self->extra_clocks > 0) {

View File

@ -39,6 +39,7 @@ typedef struct {
uint8_t phase;
uint8_t extra_clocks;
digitalio_digitalinout_obj_t *chip_select;
bool cs_active_value;
} adafruit_bus_device_spidevice_obj_t;
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H