From 3cf2173433000fb8d5bd8dfbce7a3101b272d6b0 Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 23 May 2023 21:05:31 +0100 Subject: [PATCH] Added fixes to the SPI implementation, removed debugging --- include/sta/raspi/gpio_pin.hpp | 10 ++++---- src/raspi/spi.cpp | 42 +++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/raspi/gpio_pin.hpp index 6f64fc7..2e5e456 100644 --- a/include/sta/raspi/gpio_pin.hpp +++ b/include/sta/raspi/gpio_pin.hpp @@ -15,14 +15,14 @@ namespace sta { + enum class GpioMode { + GPIO_OUTPUT, + GPIO_INPUT + }; + class RaspiGpioPin : public GpioPin { public: - enum class GpioMode { - GPIO_OUTPUT, - GPIO_INPUT - }; - /** * @param pin Pin index * @param mode The mode of the GPIO pin. Either INPUT or OUTPUT diff --git a/src/raspi/spi.cpp b/src/raspi/spi.cpp index 1b60e6b..0d9d92f 100644 --- a/src/raspi/spi.cpp +++ b/src/raspi/spi.cpp @@ -68,8 +68,15 @@ namespace sta spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; + // For some reasons, this line makes the SPI interface work for the BMP388. + spi_message[0].cs_change = 1; + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + if (result < 0) { + printf("Sending failed with error '%s'! \n", strerror(errno)); + } + STA_DEBUG_IOCTL_SEND(result); } @@ -83,6 +90,7 @@ namespace sta memset(spi_message, 0, sizeof(spi_message)); spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; + spi_message[0].cs_change = 1; int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); @@ -102,14 +110,12 @@ namespace sta spi_message[0].tx_buf = (unsigned long)buffer; spi_message[0].len = size; - printf("Sending "); - for (int i = 0; i < size; i++) { - printf("%x, ", buffer[i]); - } - printf("\n"); - int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + if (result < 0) { + printf("Sending failed with error '%s'! \n", strerror(errno)); + } + STA_DEBUG_IOCTL_SEND(result); } @@ -131,7 +137,7 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - if (result == -1) { + if (result < 0) { printf("Sending failed with error '%s'! \n", strerror(errno)); } @@ -154,12 +160,6 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - printf("Receiving "); - for (int i = 0; i < size; i++) { - printf("%x, ", buffer[i]); - } - printf("\n"); - STA_DEBUG_IOCTL_SEND(result); } @@ -226,18 +226,34 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_WR_MODE, &mode_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update mode failed with error '%s'! \n", strerror(errno)); + } + // Set the word size. According to the documentation "the value zero signifies eight bits". result = ioctl(spifd_, SPI_IOC_WR_BITS_PER_WORD, &dataSize_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update dataSize failed with error '%s'! \n", strerror(errno)); + } + // Set the bit order. According to the documentation zero means MSB first, everything else means LSB first. result = ioctl(spifd_, SPI_IOC_WR_LSB_FIRST, &bitOrder_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update endianness failed with error '%s'! \n", strerror(errno)); + } + // Set the maximum clock speed. result = ioctl(spifd_, SPI_IOC_WR_MAX_SPEED_HZ, &clkSpeed_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update clock speed failed with error '%s'! \n", strerror(errno)); + } + #if defined(DEBUG) uint8_t r_mode = 0; get_setting(SPI_IOC_RD_MODE, &r_mode);