@@ -41,98 +41,65 @@ static struct lis2ds12_config lis2ds12_config = {
4141#endif
4242};
4343
44- #if defined(LIS2DS12_ODR_RUNTIME )
45- static const uint16_t lis2ds12_hr_odr_map [] = {0 , 12 , 25 , 50 , 100 , 200 , 400 , 800 };
46-
47- static int lis2ds12_freq_to_odr_val (uint16_t freq )
48- {
49- size_t i ;
50-
51- for (i = 0 ; i < ARRAY_SIZE (lis2ds12_hr_odr_map ); i ++ ) {
52- if (freq == lis2ds12_hr_odr_map [i ]) {
53- return i ;
54- }
55- }
56-
57- return - EINVAL ;
58- }
59-
60- static int lis2ds12_accel_odr_set (const struct device * dev , uint16_t freq )
44+ static int lis2ds12_set_odr (const struct device * dev , uint16_t odr )
6145{
62- struct lis2ds12_data * data = dev -> data ;
63- int odr ;
46+ const struct lis2ds12_data * data = dev -> data ;
47+ stmdev_ctx_t * ctx = (stmdev_ctx_t * )data -> ctx ;
48+ uint8_t val ;
6449
65- odr = lis2ds12_freq_to_odr_val ( freq );
66- if (odr < 0 ) {
67- return odr ;
50+ /* check if power off */
51+ if (odr == 0U ) {
52+ return lis2ds12_xl_data_rate_set ( ctx , LIS2DS12_XL_ODR_OFF ) ;
6853 }
6954
70- if (data -> hw_tf -> update_reg (data ,
71- LIS2DS12_REG_CTRL1 ,
72- LIS2DS12_MASK_CTRL1_ODR ,
73- odr << LIS2DS12_SHIFT_CTRL1_ODR ) < 0 ) {
74- LOG_DBG ("failed to set accelerometer sampling rate" );
75- return - EIO ;
76- }
77-
78- return 0 ;
79- }
80- #endif
81-
82- #ifdef LIS2DS12_FS_RUNTIME
83- static const uint16_t lis2ds12_accel_fs_map [] = {2 , 16 , 4 , 8 };
84- static const uint16_t lis2ds12_accel_fs_sens [] = {1 , 8 , 2 , 4 };
85-
86- static int lis2ds12_accel_range_to_fs_val (int32_t range )
87- {
88- size_t i ;
89-
90- for (i = 0 ; i < ARRAY_SIZE (lis2ds12_accel_fs_map ); i ++ ) {
91- if (range == lis2ds12_accel_fs_map [i ]) {
92- return i ;
93- }
55+ val = LIS2DS12_HR_ODR_TO_REG (odr );
56+ if (val > LIS2DS12_XL_ODR_800Hz_HR ) {
57+ LOG_ERR ("ODR too high" );
58+ return - EINVAL ;
9459 }
9560
96- return - EINVAL ;
61+ return lis2ds12_xl_data_rate_set ( ctx , val ) ;
9762}
9863
99- static int lis2ds12_accel_range_set (const struct device * dev , int32_t range )
64+ static int lis2ds12_set_range (const struct device * dev , uint8_t range )
10065{
101- int fs ;
66+ int err ;
10267 struct lis2ds12_data * data = dev -> data ;
68+ stmdev_ctx_t * ctx = (stmdev_ctx_t * )data -> ctx ;
10369
104- fs = lis2ds12_accel_range_to_fs_val (range );
105- if (fs < 0 ) {
106- return fs ;
107- }
108-
109- if (data -> hw_tf -> update_reg (data ,
110- LIS2DS12_REG_CTRL1 ,
111- LIS2DS12_MASK_CTRL1_FS ,
112- fs << LIS2DS12_SHIFT_CTRL1_FS ) < 0 ) {
113- LOG_DBG ("failed to set accelerometer full-scale" );
114- return - EIO ;
70+ switch (range ) {
71+ default :
72+ case 2U :
73+ err = lis2ds12_xl_full_scale_set (ctx , LIS2DS12_2g );
74+ data -> gain = lis2ds12_from_fs2g_to_mg (1 );
75+ break ;
76+ case 4U :
77+ err = lis2ds12_xl_full_scale_set (ctx , LIS2DS12_4g );
78+ data -> gain = lis2ds12_from_fs4g_to_mg (1 );
79+ break ;
80+ case 8U :
81+ err = lis2ds12_xl_full_scale_set (ctx , LIS2DS12_8g );
82+ data -> gain = lis2ds12_from_fs8g_to_mg (1 );
83+ break ;
84+ case 16U :
85+ err = lis2ds12_xl_full_scale_set (ctx , LIS2DS12_16g );
86+ data -> gain = lis2ds12_from_fs16g_to_mg (1 );
87+ break ;
11588 }
11689
117- data -> gain = (float )(lis2ds12_accel_fs_sens [fs ] * GAIN_XL );
118- return 0 ;
90+ return err ;
11991}
120- #endif
12192
12293static int lis2ds12_accel_config (const struct device * dev ,
12394 enum sensor_channel chan ,
12495 enum sensor_attribute attr ,
12596 const struct sensor_value * val )
12697{
12798 switch (attr ) {
128- #ifdef LIS2DS12_FS_RUNTIME
12999 case SENSOR_ATTR_FULL_SCALE :
130- return lis2ds12_accel_range_set (dev , sensor_ms2_to_g (val ));
131- #endif
132- #ifdef LIS2DS12_ODR_RUNTIME
100+ return lis2ds12_set_range (dev , sensor_ms2_to_g (val ));
133101 case SENSOR_ATTR_SAMPLING_FREQUENCY :
134- return lis2ds12_accel_odr_set (dev , val -> val1 );
135- #endif
102+ return lis2ds12_set_odr (dev , val -> val1 );
136103 default :
137104 LOG_DBG ("Accel attribute not supported." );
138105 return - ENOTSUP ;
@@ -160,17 +127,18 @@ static int lis2ds12_attr_set(const struct device *dev,
160127static int lis2ds12_sample_fetch_accel (const struct device * dev )
161128{
162129 struct lis2ds12_data * data = dev -> data ;
163- uint8_t buf [6 ];
130+ stmdev_ctx_t * ctx = (stmdev_ctx_t * )data -> ctx ;
131+ int16_t buf [3 ];
164132
165- if ( data -> hw_tf -> read_data ( data , LIS2DS12_REG_OUTX_L ,
166- buf , sizeof ( buf ) ) < 0 ) {
167- LOG_DBG ( "failed to read sample" );
133+ /* fetch raw data sample */
134+ if ( lis2ds12_acceleration_raw_get ( ctx , buf ) < 0 ) {
135+ LOG_ERR ( "Failed to fetch raw data sample" );
168136 return - EIO ;
169137 }
170138
171- data -> sample_x = ( int16_t )(( uint16_t )( buf [0 ]) | (( uint16_t )( buf [ 1 ]) << 8 ) );
172- data -> sample_y = ( int16_t )(( uint16_t )( buf [2 ]) | (( uint16_t )( buf [ 3 ]) << 8 ) );
173- data -> sample_z = ( int16_t )(( uint16_t )( buf [4 ]) | (( uint16_t )( buf [ 5 ]) << 8 ) );
139+ data -> sample_x = sys_le16_to_cpu ( buf [0 ]);
140+ data -> sample_y = sys_le16_to_cpu ( buf [1 ] );
141+ data -> sample_z = sys_le16_to_cpu ( buf [2 ] );
174142
175143 return 0 ;
176144}
@@ -265,35 +233,40 @@ static int lis2ds12_init(const struct device *dev)
265233{
266234 const struct lis2ds12_config * const config = dev -> config ;
267235 struct lis2ds12_data * data = dev -> data ;
236+ stmdev_ctx_t * ctx ;
268237 uint8_t chip_id ;
238+ int ret ;
269239
270240 data -> comm_master = device_get_binding (config -> comm_master_dev_name );
271241 if (!data -> comm_master ) {
272- LOG_DBG ("master not found: %s" ,
242+ LOG_ERR ("master not found: %s" ,
273243 config -> comm_master_dev_name );
274244 return - EINVAL ;
275245 }
276246
277247 config -> bus_init (dev );
278248
279- /* s/w reset the sensor */
280- if ( data -> hw_tf -> write_reg ( data ,
281- LIS2DS12_REG_CTRL2 ,
282- LIS2DS12_SOFT_RESET ) < 0 ) {
283- LOG_DBG ( "s/w reset fail " );
284- return - EIO ;
249+ ctx = ( stmdev_ctx_t * ) data -> ctx ;
250+ /* check chip ID */
251+ ret = lis2ds12_device_id_get ( ctx , & chip_id );
252+ if ( ret < 0 ) {
253+ LOG_ERR ( "Not able to read dev id " );
254+ return ret ;
285255 }
286256
287- if (data -> hw_tf -> read_reg ( data , LIS2DS12_REG_WHO_AM_I , & chip_id ) < 0 ) {
288- LOG_DBG ( "failed reading chip id" );
289- return - EIO ;
257+ if (chip_id != LIS2DS12_ID ) {
258+ LOG_ERR ( "Invalid chip ID 0x%02x" , chip_id );
259+ return - EINVAL ;
290260 }
291261
292- if (chip_id != LIS2DS12_VAL_WHO_AM_I ) {
293- LOG_DBG ("invalid chip id 0x%x" , chip_id );
294- return - EIO ;
262+ /* reset device */
263+ ret = lis2ds12_reset_set (ctx , PROPERTY_ENABLE );
264+ if (ret < 0 ) {
265+ return ret ;
295266 }
296267
268+ k_busy_wait (100 );
269+
297270 LOG_DBG ("chip id 0x%x" , chip_id );
298271
299272#ifdef CONFIG_LIS2DS12_TRIGGER
@@ -304,23 +277,18 @@ static int lis2ds12_init(const struct device *dev)
304277#endif
305278
306279 /* set sensor default odr */
307- if (data -> hw_tf -> update_reg (data ,
308- LIS2DS12_REG_CTRL1 ,
309- LIS2DS12_MASK_CTRL1_ODR ,
310- LIS2DS12_DEFAULT_ODR ) < 0 ) {
311- LOG_DBG ("failed setting odr" );
312- return - EIO ;
280+ ret = lis2ds12_set_odr (dev , 12 );
281+ if (ret < 0 ) {
282+ LOG_ERR ("odr init error (12.5 Hz)" );
283+ return ret ;
313284 }
314285
315286 /* set sensor default scale */
316- if (data -> hw_tf -> update_reg (data ,
317- LIS2DS12_REG_CTRL1 ,
318- LIS2DS12_MASK_CTRL1_FS ,
319- LIS2DS12_DEFAULT_FS ) < 0 ) {
320- LOG_DBG ("failed setting scale" );
321- return - EIO ;
287+ ret = lis2ds12_set_range (dev , CONFIG_LIS2DS12_FS );
288+ if (ret < 0 ) {
289+ LOG_ERR ("range init error %d" , CONFIG_LIS2DS12_FS );
290+ return ret ;
322291 }
323- data -> gain = LIS2DS12_DEFAULT_GAIN ;
324292
325293 return 0 ;
326294}
0 commit comments