过节吃完饭,回来继续测试板载传感器。本次测试ISM330DHCX,3轴加速度+3轴陀螺仪传感器,读Datasheet发现,是带嵌入式机器学习内核的,这部分还没有测试,趁这会先把基本功能测试一下。读完datasheet,了解个大概,看原理图,如图1所示:/ t7 k: C. |1 z
/ a5 g3 W: N2 |) Q. [
1 ^: c7 B! {; Y# E$ X图1
: O& H' N& g7 |+ Q依然使用的SPI接口,根据前面的IC测试,思路搬过来使用。依然使用CubeMx现把框架搭一下,引脚定义如图2所示:
! Y: U8 B5 I. _2 {" [
+ T8 |# L0 o# J+ Y1 `7 q1 F0 d) _, ?* T U1 T2 {, n; M
图2 d8 A* F. X' J) l- L9 d
下面就是添加所需要的初始化代码,IC初始化如下:
5 }1 L& Z/ l0 g# V- /* Start device configuration. */
& Z% j- P" ]# b; i/ O - ism330dhcx_device_conf_set(&dev_ctx, PROPERTY_ENABLE);, f' Q( @, ]0 t( O
- /* Enable Block Data Update */! ~6 d- {) ^6 u; u9 J
- ism330dhcx_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
3 n7 u/ [8 v/ L F6 e - /* Set Output Data Rate */
0 p5 X# i6 C5 i - ism330dhcx_xl_data_rate_set(&dev_ctx, ISM330DHCX_XL_ODR_12Hz5);
2 L& H7 p3 J: f- {1 y - ism330dhcx_gy_data_rate_set(&dev_ctx, ISM330DHCX_GY_ODR_12Hz5);
2 O1 W n1 f* L- p - /* Set full scale */
/ n S0 J+ ~* X9 F% o - ism330dhcx_xl_full_scale_set(&dev_ctx, ISM330DHCX_2g);
3 H8 k! [/ `, E4 U - ism330dhcx_gy_full_scale_set(&dev_ctx, ISM330DHCX_2000dps);
$ |' v9 ~' Q/ q# k" n4 Q+ P - /* Configure filtering chain(No aux interface)& r9 B- O: \6 \2 G. M0 I
- *
# S) W6 [, w( C0 e2 x - * Accelerometer - LPF1 + LPF2 path; s8 O' l5 }6 n. ~4 O% P+ k3 e- c
- */
/ E, O* B. ]' n- h4 F7 Z, ~ - ism330dhcx_xl_hp_path_on_out_set(&dev_ctx, ISM330DHCX_LP_ODR_DIV_100);5 _1 ?( l/ L2 Q
- ism330dhcx_xl_filter_lp2_set(&dev_ctx, PROPERTY_ENABLE);
复制代码
/ ^+ f4 g2 d2 Y# U, u% ^% n在while中添加读取数据的code,如下所示:& { J, ]# Z; N
- ism330dhcx_xl_flag_data_ready_get(&dev_ctx, &dreg);# o. k- U' y% O$ J+ c; ?
- if(dreg)
1 P8 s0 ]3 e+ \ - {* {/ F2 j. a" d& Q; ^7 `$ t9 {+ h( Q: c
- /* Read acceleration field data */
( O2 ] |) h. t( ]2 s - memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));' q; ^# {" u- p
- ism330dhcx_acceleration_raw_get(&dev_ctx, data_raw_acceleration);$ R/ ?/ J3 i* X8 f v# u) S
- acceleration_mg[0] =
6 |3 w* I2 o$ I, R& m - ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[0]);0 F: C+ [- ~# P* U" ^9 i8 w
- acceleration_mg[1] =
8 Q, S1 P8 |7 {8 m/ s3 e" S* q* T - ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[1]);
; i% V; U; U+ o3 k% R. q G - acceleration_mg[2] =
* L, V/ P8 P; h4 ` - ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[2]);
' m6 v- ` u& s5 R5 p! F* T5 ] - }
0 r% l! h+ |. h9 d" Y! W/ Q - ism330dhcx_gy_flag_data_ready_get(&dev_ctx, &dreg);
4 p6 o4 B5 d8 Y0 j
! k1 q- G) S8 R6 h- if (dreg) {
# ?; W- u X, K$ ~" A P: s - /* Read angular rate field data */
, _ m1 R( `) q# V2 ^7 i - memset(data_raw_angular_rate, 0x00, 3 * sizeof(int16_t));) a' {( k) S8 g m
- ism330dhcx_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate);
* a9 I0 m) X; X$ v7 B - angular_rate_mdps[0] =( H9 E; i- j; o9 i; I
- ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[0]);5 | A. ~" K. H4 q3 F. V2 ^1 e
- angular_rate_mdps[1] =
! B3 C& _% O2 u( g - ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[1]);" V0 n4 k: Q4 t% H2 k
- angular_rate_mdps[2] =6 f$ t9 \* O: g
- ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[2]);2 e6 T9 y! N7 ]2 n0 V
-
0 y1 M6 u$ R7 T3 A - }- m: {; R5 W/ j
- 2 s# K/ Z- R0 v; U
- ism330dhcx_temp_flag_data_ready_get(&dev_ctx, &dreg);4 T. X. B/ O- @. |. q
- 6 ^2 ^$ a4 e$ C3 J
- if (dreg) {& d h. m2 l' W5 Q- H
- /* Read temperature data */
5 P7 e$ c; p3 \+ S* g - memset(&data_raw_temperature, 0x00, sizeof(int16_t));
3 y3 C9 l0 v- k - ism330dhcx_temperature_raw_get(&dev_ctx, &data_raw_temperature);
$ t2 X8 F+ U8 z$ U2 u - temperature_degC = ism330dhcx_from_lsb_to_celsius(
$ _5 K) Q, M8 @3 b$ t - data_raw_temperature);9 O* ^. S2 ^3 [: x' y7 J
- & L0 C) N. l, q9 O+ H7 v- G3 w( I
- }
复制代码
7 T f7 J& d8 k7 y% L# Y4 \, C" j4 A
3 g" P: d0 ]/ Z3 V
测试失败,显示hardware_fault,硬件错误。想了想,是晚上有点恍惚,把时钟配置错了,板载的外部时钟是16M,而cubemx中的默认时钟是8M,修改过来,重新生成项目,debug,测试结果如下;
( P5 a5 X j) t3 i
! B/ K1 ~# Y) z) g7 J& n) h9 o
1 w. O3 g( \" I h% s9 Y$ ]7 ~
图3: }9 A! f0 ]( ~. R9 x" o! s2 l
9 c% O) g+ r, J, K- _+ y5 [4 s6 y) D7 J! E" w9 D' z" ]
这段时间总是感觉知识浮于基础,得往深处继续挖掘学习。下次试一试Nano edge AI。
; L$ N1 }8 g- Q! N0 q$ \8 q+ v) v( i0 x' R4 D
|