From b5c70af6e53161b0744cc6c981b5bdcc0438cd38 Mon Sep 17 00:00:00 2001 From: the0ne Date: Mon, 16 May 2022 11:23:07 +0200 Subject: [PATCH] using SPI with a CS of -1 should be valid this could possibly fix #384 --- components/sys/drivers/spi.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/components/sys/drivers/spi.c b/components/sys/drivers/spi.c index 0f645afc..8b865c8c 100644 --- a/components/sys/drivers/spi.c +++ b/components/sys/drivers/spi.c @@ -1,6 +1,6 @@ /* - * Copyright (C) 2015 - 2020, IBEROXARXA SERVICIOS INTEGRALES, S.L. - * Copyright (C) 2015 - 2020, Jaume Olivé Petrus (jolive@whitecatboard.org) + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) * * All rights reserved. * @@ -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); } @@ -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); @@ -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); } @@ -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); } @@ -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"); } @@ -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