Hello, guys!
I'm currently just starting to work on the nrf9160dk board. I want to try to explore the communication of nrf9160 with external acceleration sensor. First, I learned the official routine lis2dh; then I want to try to use I2C to communicate nrf9160 and mma845x. Unfortunately, I encountered a lot of difficulties. I carried out the program on the basis of the lis2dh routine, added a new sensor driver, and finally the device can not be bound.
The procedure is as follows:
#define DT_DRV_COMPAT fsl_mma845x
const static int32_t read_sensor(const struct device *sensor,
enum sensor_channel channel)
{
struct sensor_value val[3];
int32_t ret = 0;
ret = sensor_sample_fetch(sensor);
if (ret < 0 && ret != -EBADMSG) {
printf("Sensor sample update error\n");
goto end;
}
ret = sensor_channel_get(sensor, channel, val);
if (ret < 0) {
printf("Cannot read sensor channels\n");
goto end;
}
printf("( x y z ) = ( %f %f %f )\n", sensor_value_to_double(&val[0]),
sensor_value_to_double(&val[1]),
sensor_value_to_double(&val[2]));
end:
return ret;
}
void main(void)
{
const struct device *accelerometer = device_get_binding(
DT_LABEL(DT_INST(0, fsl_mma845x)));
if (accelerometer == NULL) {
printf("Could not get %s device\n",
DT_LABEL(DT_INST(0, fsl_mma845x)));
return;
}
while (1) {
printf("Accelerometer data:\n");
if (read_sensor(accelerometer, SENSOR_CHAN_ACCEL_XYZ) < 0) {
printf("Failed to read accelerometer data\n");
}
k_sleep(K_MSEC(2000));
}
}
&i2c2 {
compatible = "nordic,nrf-twim";
status = "okay";
sda-pin = <11>;
scl-pin = <12>;
clock-frequency = <I2C_BITRATE_FAST>;
mma845x@1d {
compatible = "fsl,mma845x";
reg = <0x1d>;
label = "MMA854x";
};
};
CONFIG_STDOUT_CONSOLE=y CONFIG_I2C=y CONFIG_SENSOR=y CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_LOG=y
Change sensor driver based on lis2dh,add new sensors as follows:
1.Create a binding in zephyr\dts\bindings\sensor\<compatible string>.yaml
E.g. copy zephyr\dts\bindings\sensor\adi,adxl362.yaml, change the name and modify it according to your sensor
2. Create the driver in zephyr\drivers\sensor
E.g. copy zephyr\drivers\sensor\adxl362, change the name and modify it according to your sensor
3. In \zephyr\drivers\sensor\CMakeLists.txt add the following line
eg:add_subdirectory_ifdef(CONFIG_MMA845X mma845x)
4. In \zephyr\drivers\sensor\Kconfig add the following line
source "drivers/sensor/<sensor directory>/Kconfig"
eg:source "drivers/sensor/lis2dh/Kconfig"
5. Create a sample in zephyr\samples\sensor, to test if it works as expected
I would like to ask what should I do and how can I improve the program?
Thank you so much for your time and effort! Really appreciate it.
Sincerely,,
Jessie.