Skip to content

Commit 4863b22

Browse files
committed
Update readme
1 parent f667748 commit 4863b22

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,135 @@
1-
# lsm303
1+
# LSM303DLHC Support Library in C
2+
3+
This project is designed to interface the [LSM303DLHC](https://www.st.com/resource/en/datasheet/lsm303dlhc.pdf) using C language.
4+
5+
## Usage
6+
7+
### Shims
8+
First update I2C shim functions to let this library communicate with your system:
9+
10+
>**Note**: These shims are initially design to work in Zephyr RTOS using the build flag PLATFORM_ZEPHYR.
11+
12+
For I2C Write:
13+
14+
```
15+
/**
16+
* @brief Writes data to a register of the LSM303 device over I2C.
17+
*
18+
* This function writes data to a specified register of the LSM303 device
19+
* using the provided data buffer.
20+
*
21+
* @param device Pointer to the LSM303 device structure.
22+
* @param address I2C address of the LSM303 device.
23+
* @param data_buffer Pointer to the buffer containing the data to be written.
24+
*
25+
* @return
26+
* - 0 on success.
27+
* - Non-zero error code on failure.
28+
*/
29+
int8_t lsm303_i2c_write(lsm303_dev *device, uint8_t address,
30+
uint8_t *data_buffer) {
31+
#ifdef PLATFORM_ZEPHYR
32+
uint32_t bytecount = 2;
33+
return i2c_write(device->i2c0_dev, data_buffer, bytecount, address);
34+
#endif
35+
}
36+
```
37+
38+
FOr I2C Read:
39+
40+
```
41+
/**
42+
* @brief Reads a register value from the LSM303 device via I2C.
43+
*
44+
* Reads a single register from the LSM303 device and stores the retrieved value
45+
* in the specified buffer.
46+
*
47+
* @param device Pointer to the LSM303 device structure.
48+
* @param address I2C address of the LSM303 device.
49+
* @param reg Register address to read from.
50+
* @param read_data Pointer to the buffer where the read value will be stored.
51+
*
52+
* @return
53+
* - 0 on success.
54+
* - Non-zero error code on failure.
55+
*/
56+
int8_t lsm303_i2c_read(lsm303_dev *device, uint8_t address, uint8_t reg,
57+
uint8_t *read_data) {
58+
#ifdef PLATFORM_ZEPHYR
59+
uint32_t bytecount = 1;
60+
61+
if (i2c_write(device->i2c0_dev, &reg, bytecount, address) !=
62+
LSM303_STATUS_SUCCESS) {
63+
return LSM303_STATUS_API_ERR;
64+
}
65+
66+
if (i2c_read(device->i2c0_dev, read_data, sizeof(*read_data), address) != 0) {
67+
return LSM303_STATUS_API_ERR;
68+
}
69+
70+
return LSM303_STATUS_SUCCESS;
71+
#endif
72+
```
73+
74+
### Application
75+
76+
You may refer to [mcu-dev/dev-apps](https://github.com/mcu-dev/dev-apps) repository written for Zephyr RTOS for a sample application.
77+
78+
```
79+
static lsm303_dev dev;
80+
static lsm303_init_param dev_param;
81+
static lsm303_axes_data lsm303_acc_data;
82+
uint8_t addr = 0x00;
83+
84+
int main(void){
85+
dev_param.acc_power_mode = ACC_NORMAL;
86+
dev_param.acc_odr = ACC_ODR_1HZ;
87+
dev_param.acc_scale = ACC_SCALE_8G;
88+
dev_param.acc_resolution = ACC_RESOLUTION_LOW;
89+
dev_param.acc_axes_config.acc_axes = ACC_AXES_ENABLE_XYZ;
90+
91+
if (lsm303_setup(&dev, dev_param) == LSM303_STATUS_SUCCESS) {
92+
/* Successful */
93+
} else {
94+
/* Error occur */
95+
}
96+
97+
while(1){
98+
if (lsm303_data_ready(&dev) == LSM303_STATUS_SUCCESS) {
99+
if (dev.acc_axes_config.enable.x) {
100+
if (dev.acc_axes_config.ready.x) {
101+
if (lsm303_get_x_raw_data(&dev, &lsm303_acc_data) ==
102+
LSM303_STATUS_SUCCESS) {
103+
/* Get raw value or get G equivalent */
104+
}
105+
}
106+
}
107+
108+
if (dev.acc_axes_config.enable.y) {
109+
if (dev.acc_axes_config.ready.y) {
110+
if (lsm303_get_y_raw_data(&dev, &lsm303_acc_data) ==
111+
LSM303_STATUS_SUCCESS) {
112+
/* Get raw value or get G equivalent */
113+
}
114+
}
115+
}
116+
117+
if (dev.acc_axes_config.enable.z) {
118+
if (dev.acc_axes_config.ready.z) {
119+
if (lsm303_get_z_raw_data(&dev, &lsm303_acc_data) ==
120+
LSM303_STATUS_SUCCESS) {
121+
/* Get raw value or get G equivalent */
122+
}
123+
}
124+
}
125+
}
126+
/* Delay to handle polling */
127+
}
128+
return 0;
129+
}
130+
131+
```
132+
133+
## License
134+
135+
Licensed under the MIT license.

0 commit comments

Comments
 (0)