change tab to 4s
This commit is contained in:
parent
8648c517f6
commit
c484947cdf
82
i2c.c
82
i2c.c
@ -29,63 +29,63 @@ Example I2C use on linux/raspberry pi
|
|||||||
static int fd;
|
static int fd;
|
||||||
|
|
||||||
int i2c_init(void) {
|
int i2c_init(void) {
|
||||||
fd = open(I2C_DEVICE, O_RDWR);
|
fd = open(I2C_DEVICE, O_RDWR);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
fprintf(stderr, "i2c_init(): could not open device: %s\n", I2C_DEVICE);
|
fprintf(stderr, "i2c_init(): could not open device: %s\n", I2C_DEVICE);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, I2C_SLAVE, I2C_LIS3DH_ADDRESS) < 0) {
|
if (ioctl(fd, I2C_SLAVE, I2C_LIS3DH_ADDRESS) < 0) {
|
||||||
fprintf(stderr, "i2c_init(): failed to acquire bus/talk to slave\n");
|
fprintf(stderr, "i2c_init(): failed to acquire bus/talk to slave\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i2c_read(uint8_t reg, uint8_t *dst, uint32_t size) {
|
int i2c_read(uint8_t reg, uint8_t *dst, uint32_t size) {
|
||||||
uint8_t cmd[2];
|
uint8_t cmd[2];
|
||||||
|
|
||||||
if (size > 1) {
|
if (size > 1) {
|
||||||
reg |= 0x80; /* AUTO INC */
|
reg |= 0x80; /* AUTO INC */
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd[0] = reg;
|
cmd[0] = reg;
|
||||||
cmd[1] = 0x00;
|
cmd[1] = 0x00;
|
||||||
|
|
||||||
if (write(fd, cmd, 2) != 2) {
|
if (write(fd, cmd, 2) != 2) {
|
||||||
fprintf(stderr, "i2c_read(): error write()\n");
|
fprintf(stderr, "i2c_read(): error write()\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (read(fd, dst, size) != (int)size) {
|
if (read(fd, dst, size) != (int)size) {
|
||||||
fprintf(stderr, "i2c_read(): error read()\n");
|
fprintf(stderr, "i2c_read(): error read()\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i2c_write(uint8_t reg, uint8_t value) {
|
int i2c_write(uint8_t reg, uint8_t value) {
|
||||||
uint8_t cmd[2];
|
uint8_t cmd[2];
|
||||||
|
|
||||||
cmd[0] = reg;
|
cmd[0] = reg;
|
||||||
cmd[1] = value;
|
cmd[1] = value;
|
||||||
|
|
||||||
if (write(fd, cmd, 2) != 2) {
|
if (write(fd, cmd, 2) != 2) {
|
||||||
fprintf(stderr, "i2c_write(): error write()\n");
|
fprintf(stderr, "i2c_write(): error write()\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i2c_deinit(void) {
|
int i2c_deinit(void) {
|
||||||
if (fd) {
|
if (fd) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
10
lis3dh.h
10
lis3dh.h
@ -112,11 +112,11 @@
|
|||||||
|
|
||||||
/* user provided functions, init and deinit can be set to NULL and won't be used */
|
/* user provided functions, init and deinit can be set to NULL and won't be used */
|
||||||
struct lis3dh_device {
|
struct lis3dh_device {
|
||||||
int (*init)(void);
|
int (*init)(void);
|
||||||
int (*read)(uint8_t reg, uint8_t *dst, uint32_t size);
|
int (*read)(uint8_t reg, uint8_t *dst, uint32_t size);
|
||||||
int (*write)(uint8_t reg, uint8_t value);
|
int (*write)(uint8_t reg, uint8_t value);
|
||||||
int (*sleep)(uint32_t dur_us);
|
int (*sleep)(uint32_t dur_us);
|
||||||
int (*deinit)(void);
|
int (*deinit)(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct lis3dh_click_config {
|
struct lis3dh_click_config {
|
||||||
|
140
spi.c
140
spi.c
@ -31,111 +31,111 @@ Example SPI use on linux/raspberry pi
|
|||||||
static int fd;
|
static int fd;
|
||||||
|
|
||||||
int spi_init(void) {
|
int spi_init(void) {
|
||||||
uint8_t mode = SPI_MODE_0;
|
uint8_t mode = SPI_MODE_0;
|
||||||
uint8_t bits = 8;
|
uint8_t bits = 8;
|
||||||
uint32_t speed = SPI_SPEED;
|
uint32_t speed = SPI_SPEED;
|
||||||
|
|
||||||
if ((fd = open(SPI_DEVICE, O_RDWR)) < 0) {
|
if ((fd = open(SPI_DEVICE, O_RDWR)) < 0) {
|
||||||
fprintf(stderr, "spi_init(): open(%s)\n", SPI_DEVICE);
|
fprintf(stderr, "spi_init(): open(%s)\n", SPI_DEVICE);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_RD_MODE, &mode) == -1) {
|
if (ioctl(fd, SPI_IOC_RD_MODE, &mode) == -1) {
|
||||||
fprintf(stderr, "spi_init(): SPI_IOC_RD_MODE\n");
|
fprintf(stderr, "spi_init(): SPI_IOC_RD_MODE\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
|
if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
|
||||||
fprintf(stderr, "spi_init(): SPI_IOC_WR_MODE\n");
|
fprintf(stderr, "spi_init(): SPI_IOC_WR_MODE\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
|
if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
|
||||||
fprintf(stderr, "spi_init(): SPI_IOC_WR_BITS_PER_WORD\n");
|
fprintf(stderr, "spi_init(): SPI_IOC_WR_BITS_PER_WORD\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1) {
|
if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1) {
|
||||||
fprintf(stderr, "spi_init(): SPI_IOC_RD_BITS_PER_WORD\n");
|
fprintf(stderr, "spi_init(): SPI_IOC_RD_BITS_PER_WORD\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
|
if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
|
||||||
fprintf(stderr, "spi_init(): SPI_IOC_WR_MAX_SPEED_HZ\n");
|
fprintf(stderr, "spi_init(): SPI_IOC_WR_MAX_SPEED_HZ\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1) {
|
if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1) {
|
||||||
fprintf(stderr, "spi_init(): SPI_IOC_RD_MAX_SPEED_HZ\n");
|
fprintf(stderr, "spi_init(): SPI_IOC_RD_MAX_SPEED_HZ\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int spi_read(uint8_t reg, uint8_t *dst, uint32_t size) {
|
int spi_read(uint8_t reg, uint8_t *dst, uint32_t size) {
|
||||||
|
|
||||||
uint8_t send[2];
|
uint8_t send[2];
|
||||||
struct spi_ioc_transfer tr[2] = {0};
|
struct spi_ioc_transfer tr[2] = {0};
|
||||||
|
|
||||||
/* clear 2 MSbits */
|
/* clear 2 MSbits */
|
||||||
reg &= 0x3F;
|
reg &= 0x3F;
|
||||||
|
|
||||||
/* set READ bit */
|
/* set READ bit */
|
||||||
reg |= 0x80;
|
reg |= 0x80;
|
||||||
|
|
||||||
if (size > 1) {
|
if (size > 1) {
|
||||||
/* set AUTO INC bit */
|
/* set AUTO INC bit */
|
||||||
reg |= 0x40;
|
reg |= 0x40;
|
||||||
}
|
}
|
||||||
|
|
||||||
send[0] = reg;
|
send[0] = reg;
|
||||||
send[1] = 0x00;
|
send[1] = 0x00;
|
||||||
|
|
||||||
tr[0].tx_buf = (unsigned long) send;
|
tr[0].tx_buf = (unsigned long) send;
|
||||||
tr[0].rx_buf = (unsigned long) 0;
|
tr[0].rx_buf = (unsigned long) 0;
|
||||||
tr[0].len = 2;
|
tr[0].len = 2;
|
||||||
|
|
||||||
tr[1].tx_buf = (unsigned long) 0;
|
tr[1].tx_buf = (unsigned long) 0;
|
||||||
tr[1].rx_buf = (unsigned long) dst;
|
tr[1].rx_buf = (unsigned long) dst;
|
||||||
tr[1].len = size;
|
tr[1].len = size;
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) {
|
if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) {
|
||||||
fprintf(stderr, "spi_read(): error ioctl()\n");
|
fprintf(stderr, "spi_read(): error ioctl()\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int spi_write(uint8_t reg, uint8_t value) {
|
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 */
|
/* clear 2 MSbits */
|
||||||
reg &= 0x3F;
|
reg &= 0x3F;
|
||||||
|
|
||||||
tr[0].tx_buf = (unsigned long) ®
|
tr[0].tx_buf = (unsigned long) ®
|
||||||
tr[0].rx_buf = (unsigned long) 0;
|
tr[0].rx_buf = (unsigned long) 0;
|
||||||
tr[0].len = 1;
|
tr[0].len = 1;
|
||||||
|
|
||||||
tr[1].tx_buf = (unsigned long) &value;
|
tr[1].tx_buf = (unsigned long) &value;
|
||||||
tr[1].rx_buf = (unsigned long) 0;
|
tr[1].rx_buf = (unsigned long) 0;
|
||||||
tr[1].len = 1;
|
tr[1].len = 1;
|
||||||
|
|
||||||
if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) {
|
if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) {
|
||||||
fprintf(stderr, "spi_write(): error ioctl()\n");
|
fprintf(stderr, "spi_write(): error ioctl()\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int spi_deinit(void) {
|
int spi_deinit(void) {
|
||||||
|
|
||||||
if (fd) {
|
if (fd) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user