Skip to content

Commit

Permalink
using SPI with a CS of -1 should be valid
Browse files Browse the repository at this point in the history
this could possibly fix whitecatboard#384
  • Loading branch information
the0ne committed May 16, 2022
1 parent 8a3789b commit b5c70af
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions components/sys/drivers/spi.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2015 - 2020, IBEROXARXA SERVICIOS INTEGRALES, S.L.
* Copyright (C) 2015 - 2020, Jaume Olivé Petrus ([email protected])
* Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L.
* Copyright (C) 2015 - 2018, Jaume Olivé Petrus ([email protected])
*
* All rights reserved.
*
Expand Down Expand Up @@ -549,7 +549,7 @@ int spi_ll_setup(uint8_t unit, uint8_t master, int8_t cs, uint8_t mode, uint32_t
}

// Setup CS
if (!(flags & SPI_FLAG_CS_AUTO)) {
if (cs >= 0 && !(flags & SPI_FLAG_CS_AUTO)) {
gpio_pin_output(cs);
gpio_ll_pin_set(cs);
}
Expand Down Expand Up @@ -704,7 +704,7 @@ void IRAM_ATTR spi_ll_bulk_read(int deviceid, uint32_t nbytes, uint8_t *data) {
}

int IRAM_ATTR spi_ll_bulk_rw(int deviceid, uint32_t nbytes, uint8_t *data) {
uint8_t *read = (uint8_t *) malloc(nbytes + 3);
uint8_t *read = (uint8_t *) malloc(nbytes);
if (read) {
spi_master_op(deviceid, 1, nbytes, data, read);

Expand Down Expand Up @@ -773,7 +773,7 @@ void IRAM_ATTR spi_ll_select(int deviceid) {
spi_bus[spi_idx(unit)].last_device = deviceid;
spi_bus[spi_idx(unit)].selected_device = deviceid;

if (!(spi_bus[spi_idx(unit)].device[device].flags & SPI_FLAG_CS_AUTO)) {
if (spi_bus[spi_idx(unit)].device[device].cs >= 0 && !(spi_bus[spi_idx(unit)].device[device].flags & SPI_FLAG_CS_AUTO)) {
// Select device
gpio_ll_pin_clr(spi_bus[spi_idx(unit)].device[device].cs);
}
Expand All @@ -783,7 +783,7 @@ void IRAM_ATTR spi_ll_deselect(int deviceid) {
int unit = (deviceid & 0xff00) >> 8;
int device = (deviceid & 0x00ff);

if (!(spi_bus[spi_idx(unit)].device[device].flags & SPI_FLAG_CS_AUTO)) {
if (spi_bus[spi_idx(unit)].device[device].cs >= 0 && !(spi_bus[spi_idx(unit)].device[device].flags & SPI_FLAG_CS_AUTO)) {
// Deselect device
gpio_ll_pin_set(spi_bus[spi_idx(unit)].device[device].cs);
}
Expand Down Expand Up @@ -900,12 +900,14 @@ driver_error_t *spi_setup(uint8_t unit, uint8_t master, int8_t cs, uint8_t mode,
cs = CONFIG_LUA_RTOS_SPI3_CS;
}

/* -1 is a valid configuration which signifies that CS should not be used
if (cs == -1) {
return driver_error(SPI_DRIVER, SPI_ERR_PIN_NOT_ALLOWED, "default cs is not set in kconfig");
}
*/
}

if (!(GPIO_ALL_OUT & (GPIO_BIT_MASK << cs))) {
if (cs >= 0 && !(GPIO_ALL_OUT & (GPIO_BIT_MASK << cs))) {
return driver_error(SPI_DRIVER, SPI_ERR_PIN_NOT_ALLOWED, "cs, selected pin is not valid for output");
}

Expand All @@ -922,7 +924,7 @@ driver_error_t *spi_setup(uint8_t unit, uint8_t master, int8_t cs, uint8_t mode,
}

driver_unit_lock_error_t *lock_error = NULL;
if ((lock_error = driver_lock(SPI_DRIVER, unit, GPIO_DRIVER, cs, flags, "CS"))) {
if (cs >= 0 && (lock_error = driver_lock(SPI_DRIVER, unit, GPIO_DRIVER, cs, flags, "CS"))) {
return driver_lock_error(SPI_DRIVER, lock_error);
}
#endif
Expand Down

0 comments on commit b5c70af

Please sign in to comment.