ruuvi.drivers.c ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
Loading...
Searching...
No Matches
ruuvi_interface_spi_lis2dh12.c
Go to the documentation of this file.
1
8#if RI_LIS2DH12_ENABLED
9#include <stdint.h>
10#include <string.h>
11
12#include "ruuvi_driver_error.h"
13#include "ruuvi_driver_sensor.h"
15#include "ruuvi_interface_spi.h"
16
17
18int32_t ri_spi_lis2dh12_write (void * dev_ptr, uint8_t reg_addr,
19 uint8_t * reg_data, uint16_t len)
20{
21 rd_status_t err_code = RD_SUCCESS;
22 uint8_t dev_id = * ( (uint8_t *) dev_ptr);
23 // bit 0: READ bit. The value is 0.
24 reg_addr &= 0x7F;
25
26 // bit 1: MS bit. When 0, does not increment the address; when 1, increments the address in
27 // multiple read / writes.
28 if (len > 1) { reg_addr |= 0x40; }
29
30 ri_gpio_id_t ss;
31 ss = RD_HANDLE_TO_GPIO (dev_id);
32 err_code |= ri_gpio_write (ss, RI_GPIO_LOW);
33 err_code |= ri_spi_xfer_blocking (&reg_addr, 1, NULL, 0);
34 err_code |= ri_spi_xfer_blocking (reg_data, len, NULL, 0);
35 err_code |= ri_gpio_write (ss, RI_GPIO_HIGH);
36 return err_code;
37}
38
39int32_t ri_spi_lis2dh12_read (void * dev_ptr, uint8_t reg_addr,
40 uint8_t * reg_data, uint16_t len)
41{
42 rd_status_t err_code = RD_SUCCESS;
43 uint8_t dev_id = * ( (uint8_t *) dev_ptr);
44 // bit 0: READ bit. The value is 1.
45 reg_addr |= 0x80;
46
47 // bit 1: MS bit. When 0, does not increment the address; when 1, increments the address in
48 // multiple read / writes.
49 if (len > 1) { reg_addr |= 0x40; }
50
51 ri_gpio_id_t ss;
52 ss = RD_HANDLE_TO_GPIO (dev_id);
53 err_code |= ri_gpio_write (ss, RI_GPIO_LOW);
54 err_code |= ri_spi_xfer_blocking (&reg_addr, 1, NULL, 0);
55 err_code |= ri_spi_xfer_blocking (NULL, 0, reg_data, len);
56 err_code |= ri_gpio_write (ss, RI_GPIO_HIGH);
57 return err_code;
58}
59#endif
uint32_t rd_status_t
bitfield for representing errors
#define RD_SUCCESS
Internal Error.
rd_status_t ri_gpio_write(const ri_gpio_id_t pin, const ri_gpio_state_t state)
Write a pin of a port into given state If there are several ports the platform driver must implement ...
#define RD_HANDLE_TO_GPIO(handle)
convert uint8_t into Ruuvi GPIO
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
Ruuvi sensor interface Lifecycle: Beta
uint16_t ri_gpio_id_t
port<<8 + pin
@ RI_GPIO_LOW
GPIO electrically low.
@ RI_GPIO_HIGH
GPIO electrically high.
Interface for SPI operations.
rd_status_t ri_spi_xfer_blocking(const uint8_t *const p_tx, const size_t tx_len, uint8_t *const p_rx, const size_t rx_len)
SPI transfer function.
int32_t ri_spi_lis2dh12_write(void *dev_ptr, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
int32_t ri_spi_lis2dh12_read(void *dev_ptr, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)