From db4f0c24cd04ca1b71a87b5fbc0c8bd9f0ffe500 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 26 Nov 2018 22:38:46 -0500 Subject: [PATCH] spicmds: Check for an incorrect mode in spicmds.c Check the mode parameter in spicmds.c so that the mcu code does not need to check it. Signed-off-by: Kevin O'Connor --- src/avr/spi.c | 2 +- src/lpc176x/spi.c | 2 +- src/sam3x8e/spi.c | 2 +- src/sam4e8e/spi.c | 2 +- src/spicmds.c | 12 ++++++++---- src/stm32f1/spi.c | 2 -- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/avr/spi.c b/src/avr/spi.c index bf0738c1..1216ad1b 100644 --- a/src/avr/spi.c +++ b/src/avr/spi.c @@ -39,7 +39,7 @@ spi_init(void) struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) { - if (bus || mode > 3) + if (bus) shutdown("Invalid spi_setup parameters"); // Make sure the SPI interface is enabled diff --git a/src/lpc176x/spi.c b/src/lpc176x/spi.c index 8373a39f..e85f348c 100644 --- a/src/lpc176x/spi.c +++ b/src/lpc176x/spi.c @@ -35,7 +35,7 @@ spi_init(void) struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) { - if (bus || mode > 3) + if (bus) shutdown("Invalid spi_setup parameters"); // Make sure bus is enabled diff --git a/src/sam3x8e/spi.c b/src/sam3x8e/spi.c index 8ff9e97d..51799506 100644 --- a/src/sam3x8e/spi.c +++ b/src/sam3x8e/spi.c @@ -54,7 +54,7 @@ spi_init(void) struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) { - if (bus != CHANNEL || mode > 3) + if (bus != CHANNEL) shutdown("Invalid spi_setup parameters"); // Make sure bus is enabled diff --git a/src/sam4e8e/spi.c b/src/sam4e8e/spi.c index 77f9ddc9..5a8a6ae3 100644 --- a/src/sam4e8e/spi.c +++ b/src/sam4e8e/spi.c @@ -21,7 +21,7 @@ struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) { Usart *p_usart = USART0; - if (bus > 2 || mode > 3) { + if (bus > 2) { shutdown("Invalid spi_setup parameters"); } diff --git a/src/spicmds.c b/src/spicmds.c index 5af6dafc..0017c56b 100644 --- a/src/spicmds.c +++ b/src/spicmds.c @@ -26,12 +26,14 @@ enum { void command_config_spi(uint32_t *args) { - uint8_t shutdown_msg_len = args[5]; + uint8_t mode = args[3], shutdown_msg_len = args[5]; + if (mode > 3) + shutdown("Invalid spi mode"); struct spidev_s *spi = oid_alloc(args[0], command_config_spi , sizeof(*spi) + shutdown_msg_len); spi->pin = gpio_out_setup(args[2], 1); spi->flags = SF_HAVE_PIN; - spi->spi_config = spi_setup(args[1], args[3], args[4]); + spi->spi_config = spi_setup(args[1], mode, args[4]); spi->shutdown_msg_len = shutdown_msg_len; uint8_t *shutdown_msg = (void*)(size_t)args[6]; memcpy(spi->shutdown_msg, shutdown_msg, shutdown_msg_len); @@ -42,10 +44,12 @@ DECL_COMMAND(command_config_spi, void command_config_spi_without_cs(uint32_t *args) { - uint8_t shutdown_msg_len = args[4]; + uint8_t mode = args[2], shutdown_msg_len = args[4]; + if (mode > 3) + shutdown("Invalid spi mode"); struct spidev_s *spi = oid_alloc(args[0], command_config_spi , sizeof(*spi) + shutdown_msg_len); - spi->spi_config = spi_setup(args[1], args[2], args[3]); + spi->spi_config = spi_setup(args[1], mode, args[3]); spi->shutdown_msg_len = shutdown_msg_len; uint8_t *shutdown_msg = (void*)(size_t)args[5]; memcpy(spi->shutdown_msg, shutdown_msg, shutdown_msg_len); diff --git a/src/stm32f1/spi.c b/src/stm32f1/spi.c index ab3a4589..7bea6cb5 100644 --- a/src/stm32f1/spi.c +++ b/src/stm32f1/spi.c @@ -30,8 +30,6 @@ static void spi_set_mode(SPI_TypeDef *spi, uint8_t mode) LL_SPI_SetClockPolarity(spi, LL_SPI_POLARITY_HIGH); LL_SPI_SetClockPhase(spi, LL_SPI_PHASE_2EDGE); break; - default: - shutdown("Invalid SPI mode"); } }