From c484947cdfa52a6ffd06f66630ec4547cb9463b4 Mon Sep 17 00:00:00 2001 From: William Clark <wrvc96@gmail.com> Date: Sat, 6 Jan 2024 17:19:24 +0000 Subject: [PATCH] change tab to 4s --- i2c.c | 82 ++++++++++++++++---------------- lis3dh.h | 10 ++-- spi.c | 140 +++++++++++++++++++++++++++---------------------------- 3 files changed, 116 insertions(+), 116 deletions(-) diff --git a/i2c.c b/i2c.c index 6f6a080..a797000 100644 --- a/i2c.c +++ b/i2c.c @@ -29,63 +29,63 @@ Example I2C use on linux/raspberry pi static int fd; int i2c_init(void) { - fd = open(I2C_DEVICE, O_RDWR); - if (fd < 0) { - fprintf(stderr, "i2c_init(): could not open device: %s\n", I2C_DEVICE); - return 1; - } + fd = open(I2C_DEVICE, O_RDWR); + if (fd < 0) { + fprintf(stderr, "i2c_init(): could not open device: %s\n", I2C_DEVICE); + return 1; + } - if (ioctl(fd, I2C_SLAVE, I2C_LIS3DH_ADDRESS) < 0) { - fprintf(stderr, "i2c_init(): failed to acquire bus/talk to slave\n"); - close(fd); - return 1; - } + if (ioctl(fd, I2C_SLAVE, I2C_LIS3DH_ADDRESS) < 0) { + fprintf(stderr, "i2c_init(): failed to acquire bus/talk to slave\n"); + close(fd); + return 1; + } - return 0; + return 0; } int i2c_read(uint8_t reg, uint8_t *dst, uint32_t size) { - uint8_t cmd[2]; + uint8_t cmd[2]; - if (size > 1) { - reg |= 0x80; /* AUTO INC */ - } + if (size > 1) { + reg |= 0x80; /* AUTO INC */ + } - cmd[0] = reg; - cmd[1] = 0x00; + cmd[0] = reg; + cmd[1] = 0x00; - if (write(fd, cmd, 2) != 2) { - fprintf(stderr, "i2c_read(): error write()\n"); - return 1; - } - - if (read(fd, dst, size) != (int)size) { - fprintf(stderr, "i2c_read(): error read()\n"); - return 1; - - } + if (write(fd, cmd, 2) != 2) { + fprintf(stderr, "i2c_read(): error write()\n"); + return 1; + } + + if (read(fd, dst, size) != (int)size) { + fprintf(stderr, "i2c_read(): error read()\n"); + return 1; + + } - return 0; + return 0; } int i2c_write(uint8_t reg, uint8_t value) { - uint8_t cmd[2]; + uint8_t cmd[2]; - cmd[0] = reg; - cmd[1] = value; - - if (write(fd, cmd, 2) != 2) { - fprintf(stderr, "i2c_write(): error write()\n"); - return 1; - } + cmd[0] = reg; + cmd[1] = value; + + if (write(fd, cmd, 2) != 2) { + fprintf(stderr, "i2c_write(): error write()\n"); + return 1; + } - return 0; + return 0; } int i2c_deinit(void) { - if (fd) { - close(fd); - } + if (fd) { + close(fd); + } - return 0; + return 0; } diff --git a/lis3dh.h b/lis3dh.h index e39cfce..46e249a 100644 --- a/lis3dh.h +++ b/lis3dh.h @@ -112,11 +112,11 @@ /* user provided functions, init and deinit can be set to NULL and won't be used */ struct lis3dh_device { - int (*init)(void); - int (*read)(uint8_t reg, uint8_t *dst, uint32_t size); - int (*write)(uint8_t reg, uint8_t value); - int (*sleep)(uint32_t dur_us); - int (*deinit)(void); + int (*init)(void); + int (*read)(uint8_t reg, uint8_t *dst, uint32_t size); + int (*write)(uint8_t reg, uint8_t value); + int (*sleep)(uint32_t dur_us); + int (*deinit)(void); }; struct lis3dh_click_config { diff --git a/spi.c b/spi.c index 427c928..487810b 100644 --- a/spi.c +++ b/spi.c @@ -31,111 +31,111 @@ Example SPI use on linux/raspberry pi static int fd; int spi_init(void) { - uint8_t mode = SPI_MODE_0; - uint8_t bits = 8; - uint32_t speed = SPI_SPEED; + uint8_t mode = SPI_MODE_0; + uint8_t bits = 8; + uint32_t speed = SPI_SPEED; - if ((fd = open(SPI_DEVICE, O_RDWR)) < 0) { - fprintf(stderr, "spi_init(): open(%s)\n", SPI_DEVICE); - return 1; - } + if ((fd = open(SPI_DEVICE, O_RDWR)) < 0) { + fprintf(stderr, "spi_init(): open(%s)\n", SPI_DEVICE); + return 1; + } - if (ioctl(fd, SPI_IOC_RD_MODE, &mode) == -1) { - fprintf(stderr, "spi_init(): SPI_IOC_RD_MODE\n"); - return 1; - } + if (ioctl(fd, SPI_IOC_RD_MODE, &mode) == -1) { + fprintf(stderr, "spi_init(): SPI_IOC_RD_MODE\n"); + return 1; + } - if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { - fprintf(stderr, "spi_init(): SPI_IOC_WR_MODE\n"); - return 1; - } + if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { + fprintf(stderr, "spi_init(): SPI_IOC_WR_MODE\n"); + return 1; + } - if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { - fprintf(stderr, "spi_init(): SPI_IOC_WR_BITS_PER_WORD\n"); - return 1; - } + if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { + fprintf(stderr, "spi_init(): SPI_IOC_WR_BITS_PER_WORD\n"); + return 1; + } - if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1) { - fprintf(stderr, "spi_init(): SPI_IOC_RD_BITS_PER_WORD\n"); - return 1; - } + if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1) { + fprintf(stderr, "spi_init(): SPI_IOC_RD_BITS_PER_WORD\n"); + return 1; + } - if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { - fprintf(stderr, "spi_init(): SPI_IOC_WR_MAX_SPEED_HZ\n"); - return 1; - } + if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { + fprintf(stderr, "spi_init(): SPI_IOC_WR_MAX_SPEED_HZ\n"); + return 1; + } - if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1) { - fprintf(stderr, "spi_init(): SPI_IOC_RD_MAX_SPEED_HZ\n"); - return 1; - } - - return 0; + if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1) { + fprintf(stderr, "spi_init(): SPI_IOC_RD_MAX_SPEED_HZ\n"); + return 1; + } + + return 0; } int spi_read(uint8_t reg, uint8_t *dst, uint32_t size) { - uint8_t send[2]; - struct spi_ioc_transfer tr[2] = {0}; + uint8_t send[2]; + struct spi_ioc_transfer tr[2] = {0}; - /* clear 2 MSbits */ - reg &= 0x3F; + /* clear 2 MSbits */ + reg &= 0x3F; - /* set READ bit */ + /* set READ bit */ reg |= 0x80; if (size > 1) { - /* set AUTO INC bit */ + /* set AUTO INC bit */ reg |= 0x40; } - send[0] = reg; - send[1] = 0x00; + send[0] = reg; + send[1] = 0x00; - tr[0].tx_buf = (unsigned long) send; - tr[0].rx_buf = (unsigned long) 0; - tr[0].len = 2; + tr[0].tx_buf = (unsigned long) send; + tr[0].rx_buf = (unsigned long) 0; + tr[0].len = 2; - tr[1].tx_buf = (unsigned long) 0; - tr[1].rx_buf = (unsigned long) dst; - tr[1].len = size; + tr[1].tx_buf = (unsigned long) 0; + tr[1].rx_buf = (unsigned long) dst; + tr[1].len = size; - if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) { - fprintf(stderr, "spi_read(): error ioctl()\n"); - return 1; - } + if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) { + fprintf(stderr, "spi_read(): error ioctl()\n"); + return 1; + } - return 0; + return 0; } int spi_write(uint8_t reg, uint8_t value) { - struct spi_ioc_transfer tr[2] = {0}; + struct spi_ioc_transfer tr[2] = {0}; - /* clear 2 MSbits */ - reg &= 0x3F; + /* clear 2 MSbits */ + reg &= 0x3F; - tr[0].tx_buf = (unsigned long) ® - tr[0].rx_buf = (unsigned long) 0; - tr[0].len = 1; + tr[0].tx_buf = (unsigned long) ® + tr[0].rx_buf = (unsigned long) 0; + tr[0].len = 1; - tr[1].tx_buf = (unsigned long) &value; - tr[1].rx_buf = (unsigned long) 0; - tr[1].len = 1; + tr[1].tx_buf = (unsigned long) &value; + tr[1].rx_buf = (unsigned long) 0; + tr[1].len = 1; - if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) { - fprintf(stderr, "spi_write(): error ioctl()\n"); - return 1; - } + if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) { + fprintf(stderr, "spi_write(): error ioctl()\n"); + return 1; + } - return 0; + return 0; } int spi_deinit(void) { - if (fd) { - close(fd); - } + if (fd) { + close(fd); + } - return 0; + return 0; }