SL_Sc7a20_Driver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "SL_Sc7a20_Driver.h"
  2. #include "string.h"
  3. Std_ReturnType Gsensor_SPI_Read(uint8 Addr,uint8 *DataBufferPtr,uint16 Length)
  4. {
  5. Std_ReturnType spiSendStatus;
  6. Spi_DataBufferType txBuffer[Length+1];
  7. Spi_DataBufferType rxBuffer[Length+1];
  8. memset(txBuffer,0,sizeof(txBuffer));
  9. memset(rxBuffer,0,sizeof(rxBuffer));
  10. if(Length == 1)
  11. {
  12. txBuffer[0] = Addr | 0x80;
  13. }
  14. else if(Length > 1)
  15. {
  16. txBuffer[0] = Addr | 0x80 | 0x40;
  17. }
  18. else
  19. {
  20. return E_NOT_OK;
  21. }
  22. Spi_SetupEB(0,txBuffer,rxBuffer,Length+1);
  23. spiSendStatus = Spi_SyncTransmit(0);
  24. if(spiSendStatus == E_OK)
  25. {
  26. memcpy(DataBufferPtr,&rxBuffer[1],Length);
  27. return E_OK;
  28. }
  29. else
  30. {
  31. return E_NOT_OK;
  32. }
  33. }
  34. Std_ReturnType Gsensor_SPI_Write(uint8 Addr,uint8 *DataBufferPtr,uint16 Length)
  35. {
  36. Std_ReturnType spiSendStatus;
  37. Spi_DataBufferType txBuffer[Length+1];
  38. memset(txBuffer,0,sizeof(txBuffer));
  39. if(Length == 1)
  40. {
  41. txBuffer[0] = Addr;
  42. }
  43. else if(Length > 1)
  44. {
  45. txBuffer[0] = Addr | 0x40;
  46. }
  47. else
  48. {
  49. return E_NOT_OK;
  50. }
  51. memcpy(&txBuffer[1],DataBufferPtr,Length);
  52. Spi_SetupEB(0,txBuffer,NULL,Length+1);
  53. spiSendStatus = Spi_SyncTransmit(0);
  54. if(spiSendStatus == E_OK)
  55. {
  56. return E_OK;
  57. }
  58. else
  59. {
  60. return E_NOT_OK;
  61. }
  62. }
  63. /***************在线测试**********************/
  64. Std_ReturnType SL_SC7A20_Online_Test(void)
  65. {
  66. uint8 SL_Read_Reg[1]= {0xff};
  67. Gsensor_SPI_Read(SC7A20_CHIP_ID_ADDRESS,SL_Read_Reg,1);
  68. if(SL_Read_Reg[0]==SC7A20_CHIP_ID_VALUE)
  69. return E_OK;
  70. else
  71. return E_NOT_OK;
  72. }
  73. /***************BOOT 重载内部寄存器值*********************/
  74. Std_ReturnType SL_SC7A20_BOOT(void)
  75. {
  76. uint8 SL_Read_Reg[1]={0xff};
  77. uint8 SL_Write_Reg[1];
  78. Gsensor_SPI_Read(SL_SC7A20_CTRL_REG5,SL_Read_Reg,1);
  79. SL_Write_Reg[0] = SL_SC7A20_BOOT_ENABLE | SL_Read_Reg[0];
  80. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG5, SL_Write_Reg,1);
  81. return E_OK;
  82. }
  83. Std_ReturnType SL_SC7A20_INT_Config(void)
  84. {
  85. uint8 SL_Read_Reg[1]={0xff};
  86. uint8 SL_Write_Reg[1];
  87. /*******************AOI1 IN INT1********************/
  88. SL_Write_Reg[0]=0x08;//AOI1 LATCH
  89. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG5, SL_Write_Reg,1);
  90. SL_Write_Reg[0]=SL_SC7A20_INT_ACTIVE_HIGH_LEVEL;
  91. //interrupt happen,int pin output lower level
  92. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG6, SL_Write_Reg,1);
  93. //AOI1 CONFIG 只开高中断
  94. SL_Write_Reg[0]=0x00; //0x7F
  95. #if SL_6D4D2D1D_SEL== 1
  96. SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
  97. #elif SL_6D4D2D1D_SEL== 2
  98. SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
  99. SL_Write_Reg[0]=SL_Write_Reg[0]|0x04;//Yֵ
  100. #elif SL_6D4D2D1D_SEL== 4
  101. SL_Write_Reg[0]=SL_Write_Reg[0]|0x03;//X
  102. SL_Write_Reg[0]=SL_Write_Reg[0]|0x0C;//Y
  103. #elif SL_6D4D2D1D_SEL== 6
  104. SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//Xֵ
  105. SL_Write_Reg[0]=SL_Write_Reg[0]|0x08;//Yֵ
  106. SL_Write_Reg[0]=SL_Write_Reg[0]|0x20;//Z
  107. #endif
  108. Gsensor_SPI_Write(SL_SC7A20_INT1_CFG, SL_Write_Reg,1);
  109. //HPF SET
  110. // Gsensor_SPI_Read(SL_SC7A20_CTRL_REG2,1, &SL_Read_Reg);
  111. SL_Write_Reg[0]=0x81;//SL_Read_Reg|0xCF;//Normal HP , HPF TO AOI1
  112. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG2, SL_Write_Reg,1);
  113. #if SL_6D4D2D1D_SEL== 6
  114. SL_Write_Reg[0] = SL_SC7A20_INT_THS_20PERCENT;
  115. Gsensor_SPI_Write(SL_SC7A20_INT1_THS, SL_Write_Reg,1);
  116. SL_Write_Reg[0] = SL_SC7A20_INT_DURATION_10CLK;
  117. Gsensor_SPI_Write(SL_SC7A20_INT1_DURATION, SL_Write_Reg, 1);
  118. #else
  119. SL_Write_Reg[0] = SL_SC7A20_INT_THS_10PERCENT;
  120. Gsensor_SPI_Write(SL_SC7A20_INT1_THS, SL_Write_Reg,1);
  121. SL_Write_Reg[0] = SL_SC7A20_INT_DURATION_2CLK;
  122. Gsensor_SPI_Write(SL_SC7A20_INT1_DURATION, SL_Write_Reg, 1);
  123. #endif
  124. //AOI1 TO INT1
  125. // Gsensor_SPI_Read(SL_SC7A20_CTRL_REG3,1, &SL_Read_Reg);
  126. SL_Write_Reg[0]=0x40;//SL_Read_Reg|0x40; //AOI1 TO INT1
  127. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG3, SL_Write_Reg,1);
  128. /*******************AOI1 IN INT1********************/
  129. /*******************AOI2 IN INT2********************/
  130. Gsensor_SPI_Read(SL_SC7A20_CTRL_REG5,SL_Read_Reg, 1);
  131. SL_Write_Reg[0]=SL_Read_Reg[0]|0x02;//AOI2 LATCH
  132. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG5, SL_Write_Reg,1);
  133. //AOI2 CONFIG
  134. SL_Write_Reg[0]=0x00|0x0C; //0xFF
  135. #if SL_6D4D2D1D_SEL== 1
  136. SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
  137. #elif SL_6D4D2D1D_SEL== 2
  138. SL_Write_Reg[0]=SL_Write_Reg[0]|0x02;//X
  139. SL_Write_Reg[0]=SL_Write_Reg[0]|0x04;//Y
  140. #elif SL_6D4D2D1D_SEL== 4
  141. SL_Write_Reg[0]=SL_Write_Reg[0]|0x03;//Xֵ
  142. SL_Write_Reg[0]=SL_Write_Reg[0]|0x0C;//Y
  143. #elif SL_6D4D2D1D_SEL== 6
  144. SL_Write_Reg[0]=SL_Write_Reg[0]|0x03;//Xֵ
  145. SL_Write_Reg[0]=SL_Write_Reg[0]|0x0C;//Y
  146. SL_Write_Reg[0]=SL_Write_Reg[0]|0x30;//Z
  147. #endif
  148. Gsensor_SPI_Write(SL_SC7A20_INT2_CFG, SL_Write_Reg,1);
  149. //HPF SET
  150. #if SL_6D4D2D1D_SEL== 6
  151. SL_Read_Reg[0]= 0x00;
  152. Gsensor_SPI_Read(SL_SC7A20_CTRL_REG2,SL_Read_Reg,1);
  153. #endif
  154. //SL_Read_Reg=SL_Read_Reg&0xFD;//NO HPF TO AOI2
  155. // Gsensor_SPI_Write(SL_SC7A20_CTRL_REG2, SL_Read_Reg);
  156. #if SL_6D4D2D1D_SEL== 6
  157. SL_Write_Reg[0] = SL_SC7A20_INT_THS_20PERCENT;
  158. #else
  159. SL_Write_Reg[0] = SL_SC7A20_INT_THS_10PERCENT;
  160. #endif
  161. Gsensor_SPI_Write(SL_SC7A20_INT2_THS,SL_Write_Reg,1 );
  162. SL_Write_Reg[0] = SL_SC7A20_INT_DURATION_10CLK;
  163. Gsensor_SPI_Write(SL_SC7A20_INT2_DURATION, SL_Write_Reg,1);
  164. //AOI2 TO INT2
  165. memset(SL_Read_Reg,0xFF,1);
  166. Gsensor_SPI_Read(SL_SC7A20_CTRL_REG6,SL_Read_Reg,1);
  167. SL_Write_Reg[0]=SL_Read_Reg[0]|0x20; //AOI2 TO INT2
  168. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG6, SL_Write_Reg,1);
  169. /*******************AOI2 IN INT2********************/
  170. return E_OK;
  171. }
  172. //retrun_value&0x20=0x20 ZH
  173. //retrun_value&0x20=0x10 ZL
  174. //retrun_value&0x20=0x08 YH
  175. //retrun_value&0x20=0x04 YL
  176. //retrun_value&0x20=0x02 XH
  177. //retrun_value&0x20=0x01 XL
  178. Std_ReturnType SL_SC7A20_INT_RESET(void)
  179. {
  180. uint8 SL_Write_Reg[1] = {0x00};
  181. uint8 SL_Read_Reg1[1] = {0xFF};
  182. uint8 SL_Read_Reg2[1] = {0xFF};
  183. //SL_Read_Reg1 display the int1 type
  184. Gsensor_SPI_Read(SL_SC7A20_INT1_SRC,SL_Read_Reg1,1);
  185. //SL_Read_Reg2 display the int2 type
  186. Gsensor_SPI_Read(SL_SC7A20_INT2_SRC,SL_Read_Reg2,1);
  187. //close INT1
  188. SL_Write_Reg[0] = 0x00;
  189. Gsensor_SPI_Write(SL_SC7A20_INT1_CFG, SL_Write_Reg, 1);
  190. //close INT2
  191. Gsensor_SPI_Write(SL_SC7A20_INT2_CFG, SL_Write_Reg ,1);
  192. if((SL_Read_Reg2[0]&0x02)==0x02)
  193. {
  194. //X+
  195. }
  196. if((SL_Read_Reg2[0]&0x01)==0x01)
  197. {
  198. //X-
  199. }
  200. if((SL_Read_Reg2[0]&0x08)==0x08)
  201. {
  202. //Y+
  203. }
  204. if((SL_Read_Reg2[0]&0x04)==0x04)
  205. {
  206. //Y-
  207. }
  208. if((SL_Read_Reg2[0]&0x20)==0x20)
  209. {
  210. //Z+
  211. }
  212. if((SL_Read_Reg2[0]&0x10)==0x10)
  213. {
  214. //Z-
  215. }
  216. if(SL_Read_Reg1[0]!=0||SL_Read_Reg2[0]!=0)
  217. {
  218. return E_OK;
  219. }
  220. else
  221. {
  222. return E_NOT_OK;
  223. }
  224. }
  225. void SL_SC7A20_AOI1_event(void)
  226. {
  227. uint8 SL_SRC_Reg1[1] = {0xFF};
  228. uint8 SL_THS_Reg1[1] = {0xFF};
  229. uint8 SL_DURATION_Reg1 = {0xFF};
  230. //SL_Read_Reg1 display the int1 type
  231. Gsensor_SPI_Read(SL_SC7A20_INT1_SRC,SL_SRC_Reg1,1);
  232. Gsensor_SPI_Read(SL_SC7A20_INT1_THS,SL_THS_Reg1,1);
  233. Gsensor_SPI_Read(SL_SC7A20_INT1_DURATION,SL_DURATION_Reg1,1);
  234. // printf("SL_SC7A20_INT1_SRC=%#x\r\n",SL_SRC_Reg1);
  235. // if((SL_SRC_Reg1&0x40)==0x40){
  236. if((SL_SRC_Reg1[0]&0x01)==0x01)
  237. {
  238. //X-
  239. // printf("X1-\r\n");
  240. }
  241. if((SL_SRC_Reg1[0]&0x02)==0x02)
  242. {
  243. //X+
  244. // printf("X1+\r\n");
  245. }
  246. if((SL_SRC_Reg1[0]&0x04)==0x04)
  247. {
  248. //Y-
  249. // printf("Y1-\r\n");
  250. }
  251. if((SL_SRC_Reg1[0]&0x08)==0x08)
  252. {
  253. //Y+
  254. // printf("Y1+\r\n");
  255. }
  256. if((SL_SRC_Reg1[0]&0x10)==0x10)
  257. {
  258. //Z-
  259. // printf("Z1-\r\n");
  260. }
  261. if((SL_SRC_Reg1[0]&0x20)==0x20)
  262. {
  263. //Z+
  264. // printf("Z1+\r\n");
  265. }
  266. // }
  267. }
  268. void SL_SC7A20_AOI2_event(void)
  269. {
  270. uint8 SL_SRC_Reg2[1] = {0xFF};
  271. uint8 SL_THS_Reg2 = {0xFF};
  272. uint8 SL_DURATION_Reg2 = {0xFF};
  273. //SL_Read_Reg1 display the int1 type
  274. Gsensor_SPI_Read(SL_SC7A20_INT2_SRC,SL_SRC_Reg2,1);
  275. Gsensor_SPI_Read(SL_SC7A20_INT2_THS,SL_THS_Reg2,1);
  276. Gsensor_SPI_Read(SL_SC7A20_INT2_DURATION,SL_DURATION_Reg2,1);
  277. // printf("SL_SC7A20_INT2_SRC=%#x\r\n",SL_SRC_Reg2);
  278. // if((SL_SRC_Reg2&0x40)==0x40){
  279. if((SL_SRC_Reg2[0]&0x01)==0x01)
  280. {
  281. //X-
  282. // printf("X2-\r\n");
  283. }
  284. if((SL_SRC_Reg2[0]&0x02)==0x02)
  285. {
  286. //X+
  287. // printf("X2+\r\n");
  288. }
  289. if((SL_SRC_Reg2[0]&0x04)==0x04)
  290. {
  291. //Y-
  292. // printf("Y2-\r\n");
  293. }
  294. if((SL_SRC_Reg2[0]&0x08)==0x08)
  295. {
  296. //Y+
  297. // printf("Y2+\r\n");
  298. }
  299. if((SL_SRC_Reg2[0]&0x10)==0x10)
  300. {
  301. //Z-
  302. // printf("Z2-\r\n");
  303. }
  304. if((SL_SRC_Reg2[0]&0x20)==0x20)
  305. {
  306. //Z+
  307. // printf("Z2+\r\n");
  308. }
  309. // }
  310. }
  311. void SL_SC7A20_Reg_read_all(void)
  312. {
  313. uint8 SL_Read_Reg[32];
  314. memset(SL_Read_Reg,0xFF,32);
  315. uint8 adr = 0x20;
  316. for(adr=0x20;adr<=0x3F;adr++)
  317. {
  318. Gsensor_SPI_Read(adr,&SL_Read_Reg[adr-0x20],1);
  319. }
  320. return;
  321. }
  322. /***************传感器量程设置**********************/
  323. Std_ReturnType SL_SC7A20_FS_Config(uint8 Sc7a20_FS_Reg)
  324. {
  325. uint8 SL_Read_Reg[1]={0xff};
  326. uint8 SL_Write_Reg[1];
  327. Gsensor_SPI_Read(SL_SC7A20_CTRL_REG4,SL_Read_Reg,1);
  328. SL_Write_Reg[0]=0x80|Sc7a20_FS_Reg|SL_SC7A20_HR_ENABLE;
  329. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG4, SL_Write_Reg,1);
  330. Gsensor_SPI_Read(SL_SC7A20_CTRL_REG4,SL_Read_Reg,1);
  331. if(SL_Read_Reg[0]==SL_Write_Reg[0])
  332. {
  333. return E_OK;
  334. }
  335. else
  336. {
  337. return E_NOT_OK;
  338. }
  339. }
  340. /***************数据更新速率**加速度计使能**********/
  341. Std_ReturnType SL_SC7A20_Power_Config(uint8 Power_Config_Reg)
  342. {
  343. uint8 SL_Read_Reg[1]={0xff};
  344. uint8 SL_Write_Reg[1];
  345. #if SL_SC7A20_MTP_ENABLE == 0X01
  346. SL_Read_Reg = 0x00;
  347. Gsensor_SPI_Write(SL_SC7A20_SPI_IIC_MODE,SL_SC7A20_MTP_CFG, SL_SC7A20_MTP_VALUE);
  348. Gsensor_SPI_Read(SL_SC7A20_SPI_IIC_MODE,SL_SC7A20_SDOI2C_PU_CFG,1, &SL_Read_Reg);
  349. SL_Read_Reg=SL_Read_Reg|SL_SC7A20_SDO_PU_MSK|SL_SC7A20_I2C_PU_MSK;
  350. Gsensor_SPI_Write(SL_SC7A20_SPI_IIC_MODE,SL_SC7A20_SDOI2C_PU_CFG, SL_Read_Reg);
  351. #endif
  352. SL_Write_Reg[0] = Power_Config_Reg;
  353. Gsensor_SPI_Write(SL_SC7A20_CTRL_REG1, SL_Write_Reg,1);
  354. Gsensor_SPI_Read(SL_SC7A20_CTRL_REG1,SL_Read_Reg,1);
  355. if(SL_Read_Reg[0]==Power_Config_Reg)
  356. return E_OK;
  357. else
  358. return E_NOT_OK;
  359. }
  360. /***************加速度计数据读取*16bits*********/
  361. Std_ReturnType SL_SC7A20_Read_XYZ_Data(sint16 *SL_SC7A20_Data_XYZ_Buf)
  362. {
  363. uint8 SL_Read_Buf[7];
  364. Gsensor_SPI_Read(SL_SC7A20_STATUS_REG,&SL_Read_Buf[0],1);
  365. if((SL_Read_Buf[0]&0x0f)==0x0f)
  366. {
  367. Gsensor_SPI_Read(SL_SC7A20_DATA_OUT,&SL_Read_Buf[1],6);
  368. SL_SC7A20_Data_XYZ_Buf[0]=(sint16)((SL_Read_Buf[2]<<8) + SL_Read_Buf[1]);
  369. SL_SC7A20_Data_XYZ_Buf[1]=(sint16)((SL_Read_Buf[4]<<8) + SL_Read_Buf[3]);
  370. SL_SC7A20_Data_XYZ_Buf[2]=(sint16)((SL_Read_Buf[6]<<8) + SL_Read_Buf[5]);
  371. SL_SC7A20_Data_XYZ_Buf[0] = SL_SC7A20_Data_XYZ_Buf[0] >>4;
  372. SL_SC7A20_Data_XYZ_Buf[1] = SL_SC7A20_Data_XYZ_Buf[1] >>4;
  373. SL_SC7A20_Data_XYZ_Buf[2] = SL_SC7A20_Data_XYZ_Buf[2] >>4;
  374. SL_SC7A20_Data_XYZ_Buf[0] = SL_SC7A20_Data_XYZ_Buf[0] * 2;
  375. SL_SC7A20_Data_XYZ_Buf[1] = SL_SC7A20_Data_XYZ_Buf[1] * 2;
  376. SL_SC7A20_Data_XYZ_Buf[2] = SL_SC7A20_Data_XYZ_Buf[2] * 2;
  377. return E_OK;
  378. }
  379. else
  380. {
  381. return E_NOT_OK;
  382. }
  383. }
  384. Std_ReturnType GsensorInit(void)
  385. {
  386. Std_ReturnType Status = E_NOT_OK;
  387. Status = SL_SC7A20_Online_Test();
  388. if(Status==E_OK)
  389. {
  390. SL_SC7A20_BOOT();
  391. SL_SC7A20_FS_Config(SL_SC7A20_FS_4G);
  392. SL_SC7A20_INT_Config();
  393. //SL_SC7A20_INT_RESET();
  394. SL_SC7A20_Power_Config(SL_SC7A20_ODR_400HZ);
  395. }
  396. return Status;
  397. }