From 24e28af57c2746eea8609f7db2b759c65f00b354 Mon Sep 17 00:00:00 2001 From: Sergei Rebrov Date: Wed, 11 Sep 2024 23:43:16 +0300 Subject: [PATCH 1/2] Fix set spi mode and bits length Incorrect order of calling ioctl read and write functions in setting spi mode and bit length. Function parameters are overwritten with the value read from ioctl. First you need to write the value, and then read. --- media/luckfox/src/luckfox_spi.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/media/luckfox/src/luckfox_spi.c b/media/luckfox/src/luckfox_spi.c index a90cc0eff..0f9c5a0e8 100755 --- a/media/luckfox/src/luckfox_spi.c +++ b/media/luckfox/src/luckfox_spi.c @@ -46,13 +46,13 @@ int luckfox_spi_init(char *dev, uint32_t mode, uint8_t bits, uint32_t speed) } // Set SPI mode - ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); + ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) { printf("SPI_IOC_RD_MODE error......\n "); goto fd_close; } - ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); + ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); if (ret == -1) { printf("SPI_IOC_WR_MODE error......\n "); @@ -60,13 +60,13 @@ int luckfox_spi_init(char *dev, uint32_t mode, uint8_t bits, uint32_t speed) } // Set the length of SPI communication - ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); + ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) { printf("SPI_IOC_RD_BITS_PER_WORD error......\n "); goto fd_close; } - ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); + ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) { printf("SPI_IOC_WR_BITS_PER_WORD error......\n "); From 213778278badc22453d7fa6cdffeb2cf6d102c3e Mon Sep 17 00:00:00 2001 From: Sergei Rebrov Date: Wed, 11 Sep 2024 23:59:14 +0300 Subject: [PATCH 2/2] printf fix --- media/luckfox/src/luckfox_spi.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/media/luckfox/src/luckfox_spi.c b/media/luckfox/src/luckfox_spi.c index 0f9c5a0e8..7a7324c80 100755 --- a/media/luckfox/src/luckfox_spi.c +++ b/media/luckfox/src/luckfox_spi.c @@ -49,13 +49,13 @@ int luckfox_spi_init(char *dev, uint32_t mode, uint8_t bits, uint32_t speed) ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) { - printf("SPI_IOC_RD_MODE error......\n "); + printf("SPI_IOC_WR_MODE error......\n "); goto fd_close; } ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); if (ret == -1) { - printf("SPI_IOC_WR_MODE error......\n "); + printf("SPI_IOC_RD_MODE error......\n "); goto fd_close; } @@ -63,13 +63,13 @@ int luckfox_spi_init(char *dev, uint32_t mode, uint8_t bits, uint32_t speed) ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) { - printf("SPI_IOC_RD_BITS_PER_WORD error......\n "); + printf("SPI_IOC_WR_BITS_PER_WORD error......\n "); goto fd_close; } ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) { - printf("SPI_IOC_WR_BITS_PER_WORD error......\n "); + printf("SPI_IOC_RD_BITS_PER_WORD error......\n "); goto fd_close; }