LSM303C_MAG_driver.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
  2. * File Name : LSM303C_MAG_driver.c
  3. * Author : MEMS Application Team
  4. * Version : v1.1
  5. * Date : 22 Nov 2016
  6. * Description : LSM303C Platform Independent Driver
  7. *
  8. ********************************************************************************
  9. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  10. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  11. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  12. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  13. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  14. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  15. *
  16. * THIS SOFTWARE IS SPECIFICALLY DESIGNED FOR EXCLUSIVE USE WITH ST PARTS.
  17. *******************************************************************************/
  18. /* Includes ------------------------------------------------------------------*/
  19. #include "includes.h"
  20. /* Private typedef -----------------------------------------------------------*/
  21. /* Private define ------------------------------------------------------------*/
  22. /* Private macro -------------------------------------------------------------*/
  23. /* Private variables ---------------------------------------------------------*/
  24. /* Private functions ---------------------------------------------------------*/
  25. /************** Generic Function *******************/
  26. /*******************************************************************************
  27. * Function Name : LSM303C_MAG_WriteReg
  28. * Description : Generic Writing function. It must be fullfilled with either
  29. * : I2C or SPI writing function
  30. * Input : Register Address, Data to be written
  31. * Output : None
  32. * Return : None
  33. *******************************************************************************/
  34. u8_t LSM303C_MAG_WriteReg(void *handle, u8_t Reg, u8_t *Bufp, u16_t len)
  35. {
  36. /*Example*/
  37. //HAL_I2C_Mem_Write(&hi2c1, LSM303C_MAG_I2C_ADDRESS, Reg, I2C_MEMADD_SIZE_8BIT,
  38. // Bufp, len, 1000);
  39. // OperationI2C(LSM303C_MAG_I2C_ADDRESS,&Reg,1);
  40. #if 0
  41. IIC_Start();
  42. IIC_Send_Byte(LSM303C_MAG_I2C_ADDRESS);
  43. if(IIC_Wait_Ack())
  44. {
  45. IIC_Stop();//释放总线
  46. return 1;//没应答则退出
  47. }
  48. IIC_Send_Byte(Reg);
  49. IIC_Wait_Ack();
  50. IIC_Send_Byte(Bufp[0]);
  51. IIC_Wait_Ack();
  52. IIC_Stop();
  53. return MEMS_SUCCESS;
  54. #else
  55. unsigned short addr;
  56. unsigned short i;
  57. MOTION_CS_MAG_LOW;
  58. DelayNs(5);
  59. //写0x23
  60. addr=Reg;
  61. addr<<=8;
  62. addr|=*Bufp;
  63. for(i=0;i<16;i++)
  64. {
  65. MOTION_CLK_LOW;
  66. DelayNs(2);
  67. if((addr<<i)&0x8000) MOTION_SDA_HIGH;
  68. else MOTION_SDA_LOW;
  69. DelayNs(2);
  70. MOTION_CLK_HIGH;
  71. DelayNs(5);
  72. }
  73. DelayNs(5);
  74. MOTION_CS_MAG_HIGH;
  75. return MEMS_SUCCESS;
  76. #endif
  77. }
  78. /*******************************************************************************
  79. * Function Name : LSM303C_MAG_ReadReg
  80. * Description : Generic Reading function. It must be fullfilled with either
  81. * : I2C or SPI reading functions
  82. * Input : Register Address
  83. * Output : Data REad
  84. * Return : None
  85. *******************************************************************************/
  86. u8_t LSM303C_MAG_ReadReg(void *handle, u8_t Reg, u8_t *Bufp, u16_t len)
  87. {
  88. /*Example*/
  89. //HAL_I2C_Mem_Read(&hi2c1, LSM303C_MAG_I2C_ADDRESS, Reg, I2C_MEMADD_SIZE_8BIT,
  90. // Bufp, len, 1000);
  91. // OperationI2C(LSM303C_MAG_I2C_ADDRESS,&Reg,1);
  92. // return OperationI2C(LSM303C_MAG_I2C_ADDRESS|0x01,Bufp,1);
  93. #if 0
  94. IIC_Start();
  95. IIC_Send_Byte(LSM303C_MAG_I2C_ADDRESS);//发写命令
  96. if(IIC_Wait_Ack())
  97. {
  98. IIC_Stop();//释放总线
  99. return 0;//没应答则退出
  100. }
  101. IIC_Send_Byte(Reg);
  102. IIC_Wait_Ack();
  103. IIC_Start();
  104. IIC_Send_Byte(LSM303C_MAG_I2C_ADDRESS|0x01);//发读命令
  105. IIC_Wait_Ack();
  106. Bufp[0] = IIC_Read_Byte(0);
  107. IIC_Stop();
  108. return MEMS_SUCCESS;
  109. #else
  110. unsigned short addr;
  111. unsigned short dat=0,i;
  112. if(Reg>0x7F)return 0;
  113. addr=0x80|Reg;
  114. MOTION_CS_MAG_LOW;
  115. DelayNs(5);
  116. //写
  117. for(i=0;i<8;i++)
  118. {
  119. DelayNs(2);
  120. MOTION_CLK_LOW;
  121. DelayNs(2);
  122. if((addr<<i)&0x80) MOTION_SDA_HIGH;
  123. else MOTION_SDA_LOW;
  124. DelayNs(2);
  125. MOTION_CLK_HIGH;
  126. DelayNs(2);
  127. }
  128. //读
  129. for(i=0;i<8;i++)
  130. {
  131. dat<<=1;
  132. DelayNs(2);
  133. MOTION_CLK_LOW;
  134. DelayNs(5);
  135. MOTION_CLK_HIGH;
  136. DelayNs(2);
  137. if(MOTION_SDA_READ) dat|=0x01;
  138. }
  139. DelayNs(5);
  140. *Bufp = dat;
  141. MOTION_CS_MAG_HIGH;
  142. return MEMS_SUCCESS;
  143. #endif
  144. }
  145. /**************** Base Function *******************/
  146. /*******************************************************************************
  147. * Function Name : LSM303C_MAG_R_WHO_AM_I_
  148. * Description : Read WHO_AM_I_BIT
  149. * Input : Pointer to u8_t
  150. * Output : Status of WHO_AM_I_BIT
  151. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  152. *******************************************************************************/
  153. status_t LSM303C_MAG_R_WHO_AM_I_(void *handle, u8_t *value)
  154. {
  155. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_WHO_AM_I_REG, (u8_t *)value, 1) )
  156. return MEMS_ERROR;
  157. *value &= LSM303C_MAG_WHO_AM_I_BIT_MASK; //coerce
  158. *value = *value >> LSM303C_MAG_WHO_AM_I_BIT_POSITION; //mask
  159. return MEMS_SUCCESS;
  160. }
  161. /*******************************************************************************
  162. * Function Name : LSM303C_MAG_W_SystemOperatingMode
  163. * Description : Write MD
  164. * Input : LSM303C_MAG_MD_t
  165. * Output : None
  166. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  167. *******************************************************************************/
  168. status_t LSM303C_MAG_W_SystemOperatingMode(void *handle, LSM303C_MAG_MD_t newValue)
  169. {
  170. u8_t value;
  171. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) )
  172. return MEMS_ERROR;
  173. value &= ~LSM303C_MAG_MD_MASK;
  174. value |= newValue;
  175. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) )
  176. return MEMS_ERROR;
  177. return MEMS_SUCCESS;
  178. }
  179. /*******************************************************************************
  180. * Function Name : LSM303C_MAG_R_SystemOperatingMode
  181. * Description : Read MD
  182. * Input : Pointer to LSM303C_MAG_MD_t
  183. * Output : Status of MD see LSM303C_MAG_MD_t
  184. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  185. *******************************************************************************/
  186. status_t LSM303C_MAG_R_SystemOperatingMode(void *handle, LSM303C_MAG_MD_t *value)
  187. {
  188. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, (u8_t *)value, 1) )
  189. return MEMS_ERROR;
  190. *value &= LSM303C_MAG_MD_MASK; //mask
  191. return MEMS_SUCCESS;
  192. }
  193. /*******************************************************************************
  194. * Function Name : LSM303C_MAG_W_BlockDataUpdate
  195. * Description : Write BDU
  196. * Input : LSM303C_MAG_BDU_t
  197. * Output : None
  198. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  199. *******************************************************************************/
  200. status_t LSM303C_MAG_W_BlockDataUpdate(void *handle, LSM303C_MAG_BDU_t newValue)
  201. {
  202. u8_t value;
  203. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG5, &value, 1) )
  204. return MEMS_ERROR;
  205. value &= ~LSM303C_MAG_BDU_MASK;
  206. value |= newValue;
  207. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG5, &value, 1) )
  208. return MEMS_ERROR;
  209. return MEMS_SUCCESS;
  210. }
  211. /*******************************************************************************
  212. * Function Name : LSM303C_MAG_R_BlockDataUpdate
  213. * Description : Read BDU
  214. * Input : Pointer to LSM303C_MAG_BDU_t
  215. * Output : Status of BDU see LSM303C_MAG_BDU_t
  216. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  217. *******************************************************************************/
  218. status_t LSM303C_MAG_R_BlockDataUpdate(void *handle, LSM303C_MAG_BDU_t *value)
  219. {
  220. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG5, (u8_t *)value, 1) )
  221. return MEMS_ERROR;
  222. *value &= LSM303C_MAG_BDU_MASK; //mask
  223. return MEMS_SUCCESS;
  224. }
  225. /*******************************************************************************
  226. * Function Name : LSM303C_MAG_W_FullScale
  227. * Description : Write FS
  228. * Input : LSM303C_MAG_FS_t
  229. * Output : None
  230. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  231. *******************************************************************************/
  232. status_t LSM303C_MAG_W_FullScale(void *handle, LSM303C_MAG_FS_t newValue)
  233. {
  234. u8_t value;
  235. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) )
  236. return MEMS_ERROR;
  237. value &= ~LSM303C_MAG_FS_MASK;
  238. value |= newValue;
  239. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) )
  240. return MEMS_ERROR;
  241. return MEMS_SUCCESS;
  242. }
  243. /*******************************************************************************
  244. * Function Name : LSM303C_MAG_R_FullScale
  245. * Description : Read FS
  246. * Input : Pointer to LSM303C_MAG_FS_t
  247. * Output : Status of FS see LSM303C_MAG_FS_t
  248. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  249. *******************************************************************************/
  250. status_t LSM303C_MAG_R_FullScale(void *handle, LSM303C_MAG_FS_t *value)
  251. {
  252. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, (u8_t *)value, 1) )
  253. return MEMS_ERROR;
  254. *value &= LSM303C_MAG_FS_MASK; //mask
  255. return MEMS_SUCCESS;
  256. }
  257. /*******************************************************************************
  258. * Function Name : LSM303C_MAG_W_OutputDataRate
  259. * Description : Write DO
  260. * Input : LSM303C_MAG_DO_t
  261. * Output : None
  262. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  263. *******************************************************************************/
  264. status_t LSM303C_MAG_W_OutputDataRate(void *handle, LSM303C_MAG_DO_t newValue)
  265. {
  266. u8_t value;
  267. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  268. return MEMS_ERROR;
  269. value &= ~LSM303C_MAG_DO_MASK;
  270. value |= newValue;
  271. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  272. return MEMS_ERROR;
  273. return MEMS_SUCCESS;
  274. }
  275. /*******************************************************************************
  276. * Function Name : LSM303C_MAG_R_OutputDataRate
  277. * Description : Read DO
  278. * Input : Pointer to LSM303C_MAG_DO_t
  279. * Output : Status of DO see LSM303C_MAG_DO_t
  280. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  281. *******************************************************************************/
  282. status_t LSM303C_MAG_R_OutputDataRate(void *handle, LSM303C_MAG_DO_t *value)
  283. {
  284. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, (u8_t *)value, 1) )
  285. return MEMS_ERROR;
  286. *value &= LSM303C_MAG_DO_MASK; //mask
  287. return MEMS_SUCCESS;
  288. }
  289. /*******************************************************************************
  290. * Function Name : status_t LSM303C_MAG_Get_Magnetic(u8_t *buff)
  291. * Description : Read Magnetic output register
  292. * Input : pointer to [u8_t]
  293. * Output : Magnetic buffer u8_t
  294. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  295. *******************************************************************************/
  296. status_t LSM303C_MAG_Get_Magnetic(void *handle, u8_t *buff)
  297. {
  298. u8_t i, j, k;
  299. u8_t numberOfByteForDimension;
  300. numberOfByteForDimension=6/3;
  301. k=0;
  302. for (i=0; i<3;i++ )
  303. {
  304. for (j=0; j<numberOfByteForDimension;j++ )
  305. {
  306. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_OUTX_L+k, &buff[k], 1))
  307. return MEMS_ERROR;
  308. k++;
  309. }
  310. }
  311. return MEMS_SUCCESS;
  312. }
  313. /**************** Advanced Function *******************/
  314. /*******************************************************************************
  315. * Function Name : LSM303C_MAG_W_SelfTest
  316. * Description : Write ST
  317. * Input : LSM303C_MAG_ST_t
  318. * Output : None
  319. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  320. *******************************************************************************/
  321. status_t LSM303C_MAG_W_SelfTest(void *handle, LSM303C_MAG_ST_t newValue)
  322. {
  323. u8_t value;
  324. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  325. return MEMS_ERROR;
  326. value &= ~LSM303C_MAG_ST_MASK;
  327. value |= newValue;
  328. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  329. return MEMS_ERROR;
  330. return MEMS_SUCCESS;
  331. }
  332. /*******************************************************************************
  333. * Function Name : LSM303C_MAG_R_SelfTest
  334. * Description : Read ST
  335. * Input : Pointer to LSM303C_MAG_ST_t
  336. * Output : Status of ST see LSM303C_MAG_ST_t
  337. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  338. *******************************************************************************/
  339. status_t LSM303C_MAG_R_SelfTest(void *handle, LSM303C_MAG_ST_t *value)
  340. {
  341. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, (u8_t *)value, 1) )
  342. return MEMS_ERROR;
  343. *value &= LSM303C_MAG_ST_MASK; //mask
  344. return MEMS_SUCCESS;
  345. }
  346. /*******************************************************************************
  347. * Function Name : LSM303C_MAG_W_OperatingModeXY
  348. * Description : Write OM
  349. * Input : LSM303C_MAG_OM_t
  350. * Output : None
  351. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  352. *******************************************************************************/
  353. status_t LSM303C_MAG_W_OperatingModeXY(void *handle, LSM303C_MAG_OM_t newValue)
  354. {
  355. u8_t value;
  356. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  357. return MEMS_ERROR;
  358. value &= ~LSM303C_MAG_OM_MASK;
  359. value |= newValue;
  360. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  361. return MEMS_ERROR;
  362. return MEMS_SUCCESS;
  363. }
  364. /*******************************************************************************
  365. * Function Name : LSM303C_MAG_R_OperatingModeXY
  366. * Description : Read OM
  367. * Input : Pointer to LSM303C_MAG_OM_t
  368. * Output : Status of OM see LSM303C_MAG_OM_t
  369. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  370. *******************************************************************************/
  371. status_t LSM303C_MAG_R_OperatingModeXY(void *handle, LSM303C_MAG_OM_t *value)
  372. {
  373. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, (u8_t *)value, 1) )
  374. return MEMS_ERROR;
  375. *value &= LSM303C_MAG_OM_MASK; //mask
  376. return MEMS_SUCCESS;
  377. }
  378. /*******************************************************************************
  379. * Function Name : LSM303C_MAG_W_TemperatureSensor
  380. * Description : Write TEMP_EN
  381. * Input : LSM303C_MAG_TEMP_EN_t
  382. * Output : None
  383. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  384. *******************************************************************************/
  385. status_t LSM303C_MAG_W_TemperatureSensor(void *handle, LSM303C_MAG_TEMP_EN_t newValue)
  386. {
  387. u8_t value;
  388. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  389. return MEMS_ERROR;
  390. value &= ~LSM303C_MAG_TEMP_EN_MASK;
  391. value |= newValue;
  392. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG1, &value, 1) )
  393. return MEMS_ERROR;
  394. return MEMS_SUCCESS;
  395. }
  396. /*******************************************************************************
  397. * Function Name : LSM303C_MAG_R_TemperatureSensor
  398. * Description : Read TEMP_EN
  399. * Input : Pointer to LSM303C_MAG_TEMP_EN_t
  400. * Output : Status of TEMP_EN see LSM303C_MAG_TEMP_EN_t
  401. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  402. *******************************************************************************/
  403. status_t LSM303C_MAG_R_TemperatureSensor(void *handle, LSM303C_MAG_TEMP_EN_t *value)
  404. {
  405. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG1, (u8_t *)value, 1) )
  406. return MEMS_ERROR;
  407. *value &= LSM303C_MAG_TEMP_EN_MASK; //mask
  408. return MEMS_SUCCESS;
  409. }
  410. /*******************************************************************************
  411. * Function Name : LSM303C_MAG_W_SoftRST
  412. * Description : Write SOFT_RST
  413. * Input : LSM303C_MAG_SOFT_RST_t
  414. * Output : None
  415. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  416. *******************************************************************************/
  417. status_t LSM303C_MAG_W_SoftRST(void *handle, LSM303C_MAG_SOFT_RST_t newValue)
  418. {
  419. u8_t value;
  420. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) )
  421. return MEMS_ERROR;
  422. value &= ~LSM303C_MAG_SOFT_RST_MASK;
  423. value |= newValue;
  424. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) )
  425. return MEMS_ERROR;
  426. return MEMS_SUCCESS;
  427. }
  428. /*******************************************************************************
  429. * Function Name : LSM303C_MAG_R_SoftRST
  430. * Description : Read SOFT_RST
  431. * Input : Pointer to LSM303C_MAG_SOFT_RST_t
  432. * Output : Status of SOFT_RST see LSM303C_MAG_SOFT_RST_t
  433. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  434. *******************************************************************************/
  435. status_t LSM303C_MAG_R_SoftRST(void *handle, LSM303C_MAG_SOFT_RST_t *value)
  436. {
  437. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, (u8_t *)value, 1) )
  438. return MEMS_ERROR;
  439. *value &= LSM303C_MAG_SOFT_RST_MASK; //mask
  440. return MEMS_SUCCESS;
  441. }
  442. /*******************************************************************************
  443. * Function Name : LSM303C_MAG_W_Reboot
  444. * Description : Write REBOOT
  445. * Input : LSM303C_MAG_REBOOT_t
  446. * Output : None
  447. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  448. *******************************************************************************/
  449. status_t LSM303C_MAG_W_Reboot(void *handle, LSM303C_MAG_REBOOT_t newValue)
  450. {
  451. u8_t value;
  452. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) )
  453. return MEMS_ERROR;
  454. value &= ~LSM303C_MAG_REBOOT_MASK;
  455. value |= newValue;
  456. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG2, &value, 1) )
  457. return MEMS_ERROR;
  458. return MEMS_SUCCESS;
  459. }
  460. /*******************************************************************************
  461. * Function Name : LSM303C_MAG_R_Reboot
  462. * Description : Read REBOOT
  463. * Input : Pointer to LSM303C_MAG_REBOOT_t
  464. * Output : Status of REBOOT see LSM303C_MAG_REBOOT_t
  465. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  466. *******************************************************************************/
  467. status_t LSM303C_MAG_R_Reboot(void *handle, LSM303C_MAG_REBOOT_t *value)
  468. {
  469. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG2, (u8_t *)value, 1) )
  470. return MEMS_ERROR;
  471. *value &= LSM303C_MAG_REBOOT_MASK; //mask
  472. return MEMS_SUCCESS;
  473. }
  474. /*******************************************************************************
  475. * Function Name : LSM303C_MAG_W_SerialInterfaceMode
  476. * Description : Write SIM
  477. * Input : LSM303C_MAG_SIM_t
  478. * Output : None
  479. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  480. *******************************************************************************/
  481. status_t LSM303C_MAG_W_SerialInterfaceMode(void *handle, LSM303C_MAG_SIM_t newValue)
  482. {
  483. u8_t value;
  484. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) )
  485. return MEMS_ERROR;
  486. value &= ~LSM303C_MAG_SIM_MASK;
  487. value |= newValue;
  488. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) )
  489. return MEMS_ERROR;
  490. return MEMS_SUCCESS;
  491. }
  492. /*******************************************************************************
  493. * Function Name : LSM303C_MAG_R_SerialInterfaceMode
  494. * Description : Read SIM
  495. * Input : Pointer to LSM303C_MAG_SIM_t
  496. * Output : Status of SIM see LSM303C_MAG_SIM_t
  497. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  498. *******************************************************************************/
  499. status_t LSM303C_MAG_R_SerialInterfaceMode(void *handle, LSM303C_MAG_SIM_t *value)
  500. {
  501. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, (u8_t *)value, 1) )
  502. return MEMS_ERROR;
  503. *value &= LSM303C_MAG_SIM_MASK; //mask
  504. return MEMS_SUCCESS;
  505. }
  506. /*******************************************************************************
  507. * Function Name : LSM303C_MAG_W_FastLowPowerXYZ
  508. * Description : Write LP
  509. * Input : LSM303C_MAG_LP_t
  510. * Output : None
  511. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  512. *******************************************************************************/
  513. status_t LSM303C_MAG_W_FastLowPowerXYZ(void *handle, LSM303C_MAG_LP_t newValue)
  514. {
  515. u8_t value;
  516. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) )
  517. return MEMS_ERROR;
  518. value &= ~LSM303C_MAG_LP_MASK;
  519. value |= newValue;
  520. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG3, &value, 1) )
  521. return MEMS_ERROR;
  522. return MEMS_SUCCESS;
  523. }
  524. /*******************************************************************************
  525. * Function Name : LSM303C_MAG_R_FastLowPowerXYZ
  526. * Description : Read LP
  527. * Input : Pointer to LSM303C_MAG_LP_t
  528. * Output : Status of LP see LSM303C_MAG_LP_t
  529. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  530. *******************************************************************************/
  531. status_t LSM303C_MAG_R_FastLowPowerXYZ(void *handle, LSM303C_MAG_LP_t *value)
  532. {
  533. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG3, (u8_t *)value, 1) )
  534. return MEMS_ERROR;
  535. *value &= LSM303C_MAG_LP_MASK; //mask
  536. return MEMS_SUCCESS;
  537. }
  538. /*******************************************************************************
  539. * Function Name : LSM303C_MAG_W_LittleBigEndianInversion
  540. * Description : Write BLE
  541. * Input : LSM303C_MAG_BLE_t
  542. * Output : None
  543. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  544. *******************************************************************************/
  545. status_t LSM303C_MAG_W_LittleBigEndianInversion(void *handle, LSM303C_MAG_BLE_t newValue)
  546. {
  547. u8_t value;
  548. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG4, &value, 1) )
  549. return MEMS_ERROR;
  550. value &= ~LSM303C_MAG_BLE_MASK;
  551. value |= newValue;
  552. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG4, &value, 1) )
  553. return MEMS_ERROR;
  554. return MEMS_SUCCESS;
  555. }
  556. /*******************************************************************************
  557. * Function Name : LSM303C_MAG_R_LittleBigEndianInversion
  558. * Description : Read BLE
  559. * Input : Pointer to LSM303C_MAG_BLE_t
  560. * Output : Status of BLE see LSM303C_MAG_BLE_t
  561. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  562. *******************************************************************************/
  563. status_t LSM303C_MAG_R_LittleBigEndianInversion(void *handle, LSM303C_MAG_BLE_t *value)
  564. {
  565. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG4, (u8_t *)value, 1) )
  566. return MEMS_ERROR;
  567. *value &= LSM303C_MAG_BLE_MASK; //mask
  568. return MEMS_SUCCESS;
  569. }
  570. /*******************************************************************************
  571. * Function Name : LSM303C_MAG_W_OperatingModeZ
  572. * Description : Write OMZ
  573. * Input : LSM303C_MAG_OMZ_t
  574. * Output : None
  575. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  576. *******************************************************************************/
  577. status_t LSM303C_MAG_W_OperatingModeZ(void *handle, LSM303C_MAG_OMZ_t newValue)
  578. {
  579. u8_t value;
  580. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG4, &value, 1) )
  581. return MEMS_ERROR;
  582. value &= ~LSM303C_MAG_OMZ_MASK;
  583. value |= newValue;
  584. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_CTRL_REG4, &value, 1) )
  585. return MEMS_ERROR;
  586. return MEMS_SUCCESS;
  587. }
  588. /*******************************************************************************
  589. * Function Name : LSM303C_MAG_R_OperatingModeZ
  590. * Description : Read OMZ
  591. * Input : Pointer to LSM303C_MAG_OMZ_t
  592. * Output : Status of OMZ see LSM303C_MAG_OMZ_t
  593. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  594. *******************************************************************************/
  595. status_t LSM303C_MAG_R_OperatingModeZ(void *handle, LSM303C_MAG_OMZ_t *value)
  596. {
  597. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_CTRL_REG4, (u8_t *)value, 1) )
  598. return MEMS_ERROR;
  599. *value &= LSM303C_MAG_OMZ_MASK; //mask
  600. return MEMS_SUCCESS;
  601. }
  602. /*******************************************************************************
  603. * Function Name : LSM303C_MAG_R_NewXData
  604. * Description : Read XDA
  605. * Input : Pointer to LSM303C_MAG_XDA_t
  606. * Output : Status of XDA see LSM303C_MAG_XDA_t
  607. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  608. *******************************************************************************/
  609. status_t LSM303C_MAG_R_NewXData(void *handle, LSM303C_MAG_XDA_t *value)
  610. {
  611. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  612. return MEMS_ERROR;
  613. *value &= LSM303C_MAG_XDA_MASK; //mask
  614. return MEMS_SUCCESS;
  615. }
  616. /*******************************************************************************
  617. * Function Name : LSM303C_MAG_R_NewYData
  618. * Description : Read YDA
  619. * Input : Pointer to LSM303C_MAG_YDA_t
  620. * Output : Status of YDA see LSM303C_MAG_YDA_t
  621. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  622. *******************************************************************************/
  623. status_t LSM303C_MAG_R_NewYData(void *handle, LSM303C_MAG_YDA_t *value)
  624. {
  625. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  626. return MEMS_ERROR;
  627. *value &= LSM303C_MAG_YDA_MASK; //mask
  628. return MEMS_SUCCESS;
  629. }
  630. /*******************************************************************************
  631. * Function Name : LSM303C_MAG_R_NewZData
  632. * Description : Read ZDA
  633. * Input : Pointer to LSM303C_MAG_ZDA_t
  634. * Output : Status of ZDA see LSM303C_MAG_ZDA_t
  635. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  636. *******************************************************************************/
  637. status_t LSM303C_MAG_R_NewZData(void *handle, LSM303C_MAG_ZDA_t *value)
  638. {
  639. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  640. return MEMS_ERROR;
  641. *value &= LSM303C_MAG_ZDA_MASK; //mask
  642. return MEMS_SUCCESS;
  643. }
  644. /*******************************************************************************
  645. * Function Name : LSM303C_MAG_R_NewXYZData
  646. * Description : Read ZYXDA
  647. * Input : Pointer to LSM303C_MAG_ZYXDA_t
  648. * Output : Status of ZYXDA see LSM303C_MAG_ZYXDA_t
  649. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  650. *******************************************************************************/
  651. status_t LSM303C_MAG_R_NewXYZData(void *handle, LSM303C_MAG_ZYXDA_t *value)
  652. {
  653. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  654. return MEMS_ERROR;
  655. *value &= LSM303C_MAG_ZYXDA_MASK; //mask
  656. return MEMS_SUCCESS;
  657. }
  658. /*******************************************************************************
  659. * Function Name : LSM303C_MAG_R_DataXOverrun
  660. * Description : Read XOR
  661. * Input : Pointer to LSM303C_MAG_XOR_t
  662. * Output : Status of XOR see LSM303C_MAG_XOR_t
  663. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  664. *******************************************************************************/
  665. status_t LSM303C_MAG_R_DataXOverrun(void *handle, LSM303C_MAG_XOR_t *value)
  666. {
  667. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  668. return MEMS_ERROR;
  669. *value &= LSM303C_MAG_XOR_MASK; //mask
  670. return MEMS_SUCCESS;
  671. }
  672. /*******************************************************************************
  673. * Function Name : LSM303C_MAG_R_DataYOverrun
  674. * Description : Read YOR
  675. * Input : Pointer to LSM303C_MAG_YOR_t
  676. * Output : Status of YOR see LSM303C_MAG_YOR_t
  677. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  678. *******************************************************************************/
  679. status_t LSM303C_MAG_R_DataYOverrun(void *handle, LSM303C_MAG_YOR_t *value)
  680. {
  681. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  682. return MEMS_ERROR;
  683. *value &= LSM303C_MAG_YOR_MASK; //mask
  684. return MEMS_SUCCESS;
  685. }
  686. /*******************************************************************************
  687. * Function Name : LSM303C_MAG_R_DataZOverrun
  688. * Description : Read ZOR
  689. * Input : Pointer to LSM303C_MAG_ZOR_t
  690. * Output : Status of ZOR see LSM303C_MAG_ZOR_t
  691. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  692. *******************************************************************************/
  693. status_t LSM303C_MAG_R_DataZOverrun(void *handle, LSM303C_MAG_ZOR_t *value)
  694. {
  695. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  696. return MEMS_ERROR;
  697. *value &= LSM303C_MAG_ZOR_MASK; //mask
  698. return MEMS_SUCCESS;
  699. }
  700. /*******************************************************************************
  701. * Function Name : LSM303C_MAG_R_DataXYZOverrun
  702. * Description : Read ZYXOR
  703. * Input : Pointer to LSM303C_MAG_ZYXOR_t
  704. * Output : Status of ZYXOR see LSM303C_MAG_ZYXOR_t
  705. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  706. *******************************************************************************/
  707. status_t LSM303C_MAG_R_DataXYZOverrun(void *handle, LSM303C_MAG_ZYXOR_t *value)
  708. {
  709. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_STATUS_REG, (u8_t *)value, 1) )
  710. return MEMS_ERROR;
  711. *value &= LSM303C_MAG_ZYXOR_MASK; //mask
  712. return MEMS_SUCCESS;
  713. }
  714. /*******************************************************************************
  715. * Function Name : LSM303C_MAG_W_InterruptEnable
  716. * Description : Write IEN
  717. * Input : LSM303C_MAG_IEN_t
  718. * Output : None
  719. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  720. *******************************************************************************/
  721. status_t LSM303C_MAG_W_InterruptEnable(void *handle, LSM303C_MAG_IEN_t newValue)
  722. {
  723. u8_t value;
  724. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  725. return MEMS_ERROR;
  726. value &= ~LSM303C_MAG_IEN_MASK;
  727. value |= newValue;
  728. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  729. return MEMS_ERROR;
  730. return MEMS_SUCCESS;
  731. }
  732. /*******************************************************************************
  733. * Function Name : LSM303C_MAG_R_InterruptEnable
  734. * Description : Read IEN
  735. * Input : Pointer to LSM303C_MAG_IEN_t
  736. * Output : Status of IEN see LSM303C_MAG_IEN_t
  737. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  738. *******************************************************************************/
  739. status_t LSM303C_MAG_R_InterruptEnable(void *handle, LSM303C_MAG_IEN_t *value)
  740. {
  741. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, (u8_t *)value, 1) )
  742. return MEMS_ERROR;
  743. *value &= LSM303C_MAG_IEN_MASK; //mask
  744. return MEMS_SUCCESS;
  745. }
  746. /*******************************************************************************
  747. * Function Name : LSM303C_MAG_W_LatchInterruptRq
  748. * Description : Write LIR
  749. * Input : LSM303C_MAG_LIR_t
  750. * Output : None
  751. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  752. *******************************************************************************/
  753. status_t LSM303C_MAG_W_LatchInterruptRq(void *handle, LSM303C_MAG_LIR_t newValue)
  754. {
  755. u8_t value;
  756. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  757. return MEMS_ERROR;
  758. value &= ~LSM303C_MAG_LIR_MASK;
  759. value |= newValue;
  760. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  761. return MEMS_ERROR;
  762. return MEMS_SUCCESS;
  763. }
  764. /*******************************************************************************
  765. * Function Name : LSM303C_MAG_R_LatchInterruptRq
  766. * Description : Read LIR
  767. * Input : Pointer to LSM303C_MAG_LIR_t
  768. * Output : Status of LIR see LSM303C_MAG_LIR_t
  769. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  770. *******************************************************************************/
  771. status_t LSM303C_MAG_R_LatchInterruptRq(void *handle, LSM303C_MAG_LIR_t *value)
  772. {
  773. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, (u8_t *)value, 1) )
  774. return MEMS_ERROR;
  775. *value &= LSM303C_MAG_LIR_MASK; //mask
  776. return MEMS_SUCCESS;
  777. }
  778. /*******************************************************************************
  779. * Function Name : LSM303C_MAG_W_InterruptActive
  780. * Description : Write IEA
  781. * Input : LSM303C_MAG_IEA_t
  782. * Output : None
  783. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  784. *******************************************************************************/
  785. status_t LSM303C_MAG_W_InterruptActive(void *handle, LSM303C_MAG_IEA_t newValue)
  786. {
  787. u8_t value;
  788. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  789. return MEMS_ERROR;
  790. value &= ~LSM303C_MAG_IEA_MASK;
  791. value |= newValue;
  792. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  793. return MEMS_ERROR;
  794. return MEMS_SUCCESS;
  795. }
  796. /*******************************************************************************
  797. * Function Name : LSM303C_MAG_R_InterruptActive
  798. * Description : Read IEA
  799. * Input : Pointer to LSM303C_MAG_IEA_t
  800. * Output : Status of IEA see LSM303C_MAG_IEA_t
  801. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  802. *******************************************************************************/
  803. status_t LSM303C_MAG_R_InterruptActive(void *handle, LSM303C_MAG_IEA_t *value)
  804. {
  805. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, (u8_t *)value, 1) )
  806. return MEMS_ERROR;
  807. *value &= LSM303C_MAG_IEA_MASK; //mask
  808. return MEMS_SUCCESS;
  809. }
  810. /*******************************************************************************
  811. * Function Name : LSM303C_MAG_W_InterruptOnZ
  812. * Description : Write ZIEN
  813. * Input : LSM303C_MAG_ZIEN_t
  814. * Output : None
  815. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  816. *******************************************************************************/
  817. status_t LSM303C_MAG_W_InterruptOnZ(void *handle, LSM303C_MAG_ZIEN_t newValue)
  818. {
  819. u8_t value;
  820. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  821. return MEMS_ERROR;
  822. value &= ~LSM303C_MAG_ZIEN_MASK;
  823. value |= newValue;
  824. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  825. return MEMS_ERROR;
  826. return MEMS_SUCCESS;
  827. }
  828. /*******************************************************************************
  829. * Function Name : LSM303C_MAG_R_InterruptOnZ
  830. * Description : Read ZIEN
  831. * Input : Pointer to LSM303C_MAG_ZIEN_t
  832. * Output : Status of ZIEN see LSM303C_MAG_ZIEN_t
  833. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  834. *******************************************************************************/
  835. status_t LSM303C_MAG_R_InterruptOnZ(void *handle, LSM303C_MAG_ZIEN_t *value)
  836. {
  837. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, (u8_t *)value, 1) )
  838. return MEMS_ERROR;
  839. *value &= LSM303C_MAG_ZIEN_MASK; //mask
  840. return MEMS_SUCCESS;
  841. }
  842. /*******************************************************************************
  843. * Function Name : LSM303C_MAG_W_InterruptOnY
  844. * Description : Write YIEN
  845. * Input : LSM303C_MAG_YIEN_t
  846. * Output : None
  847. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  848. *******************************************************************************/
  849. status_t LSM303C_MAG_W_InterruptOnY(void *handle, LSM303C_MAG_YIEN_t newValue)
  850. {
  851. u8_t value;
  852. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  853. return MEMS_ERROR;
  854. value &= ~LSM303C_MAG_YIEN_MASK;
  855. value |= newValue;
  856. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  857. return MEMS_ERROR;
  858. return MEMS_SUCCESS;
  859. }
  860. /*******************************************************************************
  861. * Function Name : LSM303C_MAG_R_InterruptOnY
  862. * Description : Read YIEN
  863. * Input : Pointer to LSM303C_MAG_YIEN_t
  864. * Output : Status of YIEN see LSM303C_MAG_YIEN_t
  865. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  866. *******************************************************************************/
  867. status_t LSM303C_MAG_R_InterruptOnY(void *handle, LSM303C_MAG_YIEN_t *value)
  868. {
  869. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, (u8_t *)value, 1) )
  870. return MEMS_ERROR;
  871. *value &= LSM303C_MAG_YIEN_MASK; //mask
  872. return MEMS_SUCCESS;
  873. }
  874. /*******************************************************************************
  875. * Function Name : LSM303C_MAG_W_InterruptOnX
  876. * Description : Write XIEN
  877. * Input : LSM303C_MAG_XIEN_t
  878. * Output : None
  879. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  880. *******************************************************************************/
  881. status_t LSM303C_MAG_W_InterruptOnX(void *handle, LSM303C_MAG_XIEN_t newValue)
  882. {
  883. u8_t value;
  884. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  885. return MEMS_ERROR;
  886. value &= ~LSM303C_MAG_XIEN_MASK;
  887. value |= newValue;
  888. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_CFG, &value, 1) )
  889. return MEMS_ERROR;
  890. return MEMS_SUCCESS;
  891. }
  892. /*******************************************************************************
  893. * Function Name : LSM303C_MAG_R_InterruptOnX
  894. * Description : Read XIEN
  895. * Input : Pointer to LSM303C_MAG_XIEN_t
  896. * Output : Status of XIEN see LSM303C_MAG_XIEN_t
  897. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  898. *******************************************************************************/
  899. status_t LSM303C_MAG_R_InterruptOnX(void *handle, LSM303C_MAG_XIEN_t *value)
  900. {
  901. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_CFG, (u8_t *)value, 1) )
  902. return MEMS_ERROR;
  903. *value &= LSM303C_MAG_XIEN_MASK; //mask
  904. return MEMS_SUCCESS;
  905. }
  906. /*******************************************************************************
  907. * Function Name : LSM303C_MAG_W_InterruptFlag
  908. * Description : Write INT
  909. * Input : LSM303C_MAG_INT_t
  910. * Output : None
  911. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  912. *******************************************************************************/
  913. status_t LSM303C_MAG_W_InterruptFlag(void *handle, LSM303C_MAG_INT_t newValue)
  914. {
  915. u8_t value;
  916. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  917. return MEMS_ERROR;
  918. value &= ~LSM303C_MAG_INT_MASK;
  919. value |= newValue;
  920. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  921. return MEMS_ERROR;
  922. return MEMS_SUCCESS;
  923. }
  924. /*******************************************************************************
  925. * Function Name : LSM303C_MAG_R_InterruptFlag
  926. * Description : Read INT
  927. * Input : Pointer to LSM303C_MAG_INT_t
  928. * Output : Status of INT see LSM303C_MAG_INT_t
  929. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  930. *******************************************************************************/
  931. status_t LSM303C_MAG_R_InterruptFlag(void *handle, LSM303C_MAG_INT_t *value)
  932. {
  933. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  934. return MEMS_ERROR;
  935. *value &= LSM303C_MAG_INT_MASK; //mask
  936. return MEMS_SUCCESS;
  937. }
  938. /*******************************************************************************
  939. * Function Name : LSM303C_MAG_W_MagneticFieldOverflow
  940. * Description : Write MROI
  941. * Input : LSM303C_MAG_MROI_t
  942. * Output : None
  943. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  944. *******************************************************************************/
  945. status_t LSM303C_MAG_W_MagneticFieldOverflow(void *handle, LSM303C_MAG_MROI_t newValue)
  946. {
  947. u8_t value;
  948. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  949. return MEMS_ERROR;
  950. value &= ~LSM303C_MAG_MROI_MASK;
  951. value |= newValue;
  952. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  953. return MEMS_ERROR;
  954. return MEMS_SUCCESS;
  955. }
  956. /*******************************************************************************
  957. * Function Name : LSM303C_MAG_R_MagneticFieldOverflow
  958. * Description : Read MROI
  959. * Input : Pointer to LSM303C_MAG_MROI_t
  960. * Output : Status of MROI see LSM303C_MAG_MROI_t
  961. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  962. *******************************************************************************/
  963. status_t LSM303C_MAG_R_MagneticFieldOverflow(void *handle, LSM303C_MAG_MROI_t *value)
  964. {
  965. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  966. return MEMS_ERROR;
  967. *value &= LSM303C_MAG_MROI_MASK; //mask
  968. return MEMS_SUCCESS;
  969. }
  970. /*******************************************************************************
  971. * Function Name : LSM303C_MAG_W_NegativeThresholdFlagZ
  972. * Description : Write NTH_Z
  973. * Input : LSM303C_MAG_NTH_Z_t
  974. * Output : None
  975. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  976. *******************************************************************************/
  977. status_t LSM303C_MAG_W_NegativeThresholdFlagZ(void *handle, LSM303C_MAG_NTH_Z_t newValue)
  978. {
  979. u8_t value;
  980. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  981. return MEMS_ERROR;
  982. value &= ~LSM303C_MAG_NTH_Z_MASK;
  983. value |= newValue;
  984. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  985. return MEMS_ERROR;
  986. return MEMS_SUCCESS;
  987. }
  988. /*******************************************************************************
  989. * Function Name : LSM303C_MAG_R_NegativeThresholdFlagZ
  990. * Description : Read NTH_Z
  991. * Input : Pointer to LSM303C_MAG_NTH_Z_t
  992. * Output : Status of NTH_Z see LSM303C_MAG_NTH_Z_t
  993. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  994. *******************************************************************************/
  995. status_t LSM303C_MAG_R_NegativeThresholdFlagZ(void *handle, LSM303C_MAG_NTH_Z_t *value)
  996. {
  997. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  998. return MEMS_ERROR;
  999. *value &= LSM303C_MAG_NTH_Z_MASK; //mask
  1000. return MEMS_SUCCESS;
  1001. }
  1002. /*******************************************************************************
  1003. * Function Name : LSM303C_MAG_W_NegativeThresholdFlagY
  1004. * Description : Write NTH_Y
  1005. * Input : LSM303C_MAG_NTH_Y_t
  1006. * Output : None
  1007. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1008. *******************************************************************************/
  1009. status_t LSM303C_MAG_W_NegativeThresholdFlagY(void *handle, LSM303C_MAG_NTH_Y_t newValue)
  1010. {
  1011. u8_t value;
  1012. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1013. return MEMS_ERROR;
  1014. value &= ~LSM303C_MAG_NTH_Y_MASK;
  1015. value |= newValue;
  1016. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1017. return MEMS_ERROR;
  1018. return MEMS_SUCCESS;
  1019. }
  1020. /*******************************************************************************
  1021. * Function Name : LSM303C_MAG_R_NegativeThresholdFlagY
  1022. * Description : Read NTH_Y
  1023. * Input : Pointer to LSM303C_MAG_NTH_Y_t
  1024. * Output : Status of NTH_Y see LSM303C_MAG_NTH_Y_t
  1025. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1026. *******************************************************************************/
  1027. status_t LSM303C_MAG_R_NegativeThresholdFlagY(void *handle, LSM303C_MAG_NTH_Y_t *value)
  1028. {
  1029. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  1030. return MEMS_ERROR;
  1031. *value &= LSM303C_MAG_NTH_Y_MASK; //mask
  1032. return MEMS_SUCCESS;
  1033. }
  1034. /*******************************************************************************
  1035. * Function Name : LSM303C_MAG_W_NegativeThresholdFlagX
  1036. * Description : Write NTH_X
  1037. * Input : LSM303C_MAG_NTH_X_t
  1038. * Output : None
  1039. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1040. *******************************************************************************/
  1041. status_t LSM303C_MAG_W_NegativeThresholdFlagX(void *handle, LSM303C_MAG_NTH_X_t newValue)
  1042. {
  1043. u8_t value;
  1044. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1045. return MEMS_ERROR;
  1046. value &= ~LSM303C_MAG_NTH_X_MASK;
  1047. value |= newValue;
  1048. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1049. return MEMS_ERROR;
  1050. return MEMS_SUCCESS;
  1051. }
  1052. /*******************************************************************************
  1053. * Function Name : LSM303C_MAG_R_NegativeThresholdFlagX
  1054. * Description : Read NTH_X
  1055. * Input : Pointer to LSM303C_MAG_NTH_X_t
  1056. * Output : Status of NTH_X see LSM303C_MAG_NTH_X_t
  1057. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1058. *******************************************************************************/
  1059. status_t LSM303C_MAG_R_NegativeThresholdFlagX(void *handle, LSM303C_MAG_NTH_X_t *value)
  1060. {
  1061. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  1062. return MEMS_ERROR;
  1063. *value &= LSM303C_MAG_NTH_X_MASK; //mask
  1064. return MEMS_SUCCESS;
  1065. }
  1066. /*******************************************************************************
  1067. * Function Name : LSM303C_MAG_W_PositiveThresholdFlagZ
  1068. * Description : Write PTH_Z
  1069. * Input : LSM303C_MAG_PTH_Z_t
  1070. * Output : None
  1071. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1072. *******************************************************************************/
  1073. status_t LSM303C_MAG_W_PositiveThresholdFlagZ(void *handle, LSM303C_MAG_PTH_Z_t newValue)
  1074. {
  1075. u8_t value;
  1076. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1077. return MEMS_ERROR;
  1078. value &= ~LSM303C_MAG_PTH_Z_MASK;
  1079. value |= newValue;
  1080. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1081. return MEMS_ERROR;
  1082. return MEMS_SUCCESS;
  1083. }
  1084. /*******************************************************************************
  1085. * Function Name : LSM303C_MAG_R_PositiveThresholdFlagZ
  1086. * Description : Read PTH_Z
  1087. * Input : Pointer to LSM303C_MAG_PTH_Z_t
  1088. * Output : Status of PTH_Z see LSM303C_MAG_PTH_Z_t
  1089. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1090. *******************************************************************************/
  1091. status_t LSM303C_MAG_R_PositiveThresholdFlagZ(void *handle, LSM303C_MAG_PTH_Z_t *value)
  1092. {
  1093. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  1094. return MEMS_ERROR;
  1095. *value &= LSM303C_MAG_PTH_Z_MASK; //mask
  1096. return MEMS_SUCCESS;
  1097. }
  1098. /*******************************************************************************
  1099. * Function Name : LSM303C_MAG_W_PositiveThresholdFlagY
  1100. * Description : Write PTH_Y
  1101. * Input : LSM303C_MAG_PTH_Y_t
  1102. * Output : None
  1103. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1104. *******************************************************************************/
  1105. status_t LSM303C_MAG_W_PositiveThresholdFlagY(void *handle, LSM303C_MAG_PTH_Y_t newValue)
  1106. {
  1107. u8_t value;
  1108. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1109. return MEMS_ERROR;
  1110. value &= ~LSM303C_MAG_PTH_Y_MASK;
  1111. value |= newValue;
  1112. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1113. return MEMS_ERROR;
  1114. return MEMS_SUCCESS;
  1115. }
  1116. /*******************************************************************************
  1117. * Function Name : LSM303C_MAG_R_PositiveThresholdFlagY
  1118. * Description : Read PTH_Y
  1119. * Input : Pointer to LSM303C_MAG_PTH_Y_t
  1120. * Output : Status of PTH_Y see LSM303C_MAG_PTH_Y_t
  1121. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1122. *******************************************************************************/
  1123. status_t LSM303C_MAG_R_PositiveThresholdFlagY(void *handle, LSM303C_MAG_PTH_Y_t *value)
  1124. {
  1125. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  1126. return MEMS_ERROR;
  1127. *value &= LSM303C_MAG_PTH_Y_MASK; //mask
  1128. return MEMS_SUCCESS;
  1129. }
  1130. /*******************************************************************************
  1131. * Function Name : LSM303C_MAG_W_PositiveThresholdFlagX
  1132. * Description : Write PTH_X
  1133. * Input : LSM303C_MAG_PTH_X_t
  1134. * Output : None
  1135. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1136. *******************************************************************************/
  1137. status_t LSM303C_MAG_W_PositiveThresholdFlagX(void *handle, LSM303C_MAG_PTH_X_t newValue)
  1138. {
  1139. u8_t value;
  1140. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1141. return MEMS_ERROR;
  1142. value &= ~LSM303C_MAG_PTH_X_MASK;
  1143. value |= newValue;
  1144. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_SRC, &value, 1) )
  1145. return MEMS_ERROR;
  1146. return MEMS_SUCCESS;
  1147. }
  1148. /*******************************************************************************
  1149. * Function Name : LSM303C_MAG_R_PositiveThresholdFlagX
  1150. * Description : Read PTH_X
  1151. * Input : Pointer to LSM303C_MAG_PTH_X_t
  1152. * Output : Status of PTH_X see LSM303C_MAG_PTH_X_t
  1153. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1154. *******************************************************************************/
  1155. status_t LSM303C_MAG_R_PositiveThresholdFlagX(void *handle, LSM303C_MAG_PTH_X_t *value)
  1156. {
  1157. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_SRC, (u8_t *)value, 1) )
  1158. return MEMS_ERROR;
  1159. *value &= LSM303C_MAG_PTH_X_MASK; //mask
  1160. return MEMS_SUCCESS;
  1161. }
  1162. /*******************************************************************************
  1163. * Function Name : status_t LSM303C_MAG_Get_Temperature(u8_t *buff)
  1164. * Description : Read Temperature output register
  1165. * Input : pointer to [u8_t]
  1166. * Output : Temperature buffer u8_t
  1167. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1168. *******************************************************************************/
  1169. status_t LSM303C_MAG_Get_Temperature(void *handle, u8_t *buff)
  1170. {
  1171. u8_t i, j, k;
  1172. u8_t numberOfByteForDimension;
  1173. numberOfByteForDimension=2/1;
  1174. k=0;
  1175. for (i=0; i<1;i++ )
  1176. {
  1177. for (j=0; j<numberOfByteForDimension;j++ )
  1178. {
  1179. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_TEMP_OUT_L+k, &buff[k], 1))
  1180. return MEMS_ERROR;
  1181. k++;
  1182. }
  1183. }
  1184. return MEMS_SUCCESS;
  1185. }
  1186. /*******************************************************************************
  1187. * Function Name : status_t LSM303C_MAG_Set_MagneticThreshold(u8_t *buff)
  1188. * Description : Set MagneticThreshold data row
  1189. * Input : pointer to [u8_t]
  1190. * Output : None
  1191. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1192. *******************************************************************************/
  1193. status_t LSM303C_MAG_Set_MagneticThreshold(void *handle, u8_t *buff)
  1194. {
  1195. u8_t i;
  1196. for (i=0; i<2;i++ )
  1197. {
  1198. if( !LSM303C_MAG_WriteReg(handle, LSM303C_MAG_INT_THS_L+i, &buff[i], 1) )
  1199. return MEMS_ERROR;
  1200. }
  1201. return MEMS_SUCCESS;
  1202. }
  1203. /*******************************************************************************
  1204. * Function Name : status_t LSM303C_MAG_Get_MagneticThreshold(u8_t *buff)
  1205. * Description : Read MagneticThreshold output register
  1206. * Input : pointer to [u8_t]
  1207. * Output : MagneticThreshold buffer u8_t
  1208. * Return : Status [MEMS_ERROR, MEMS_SUCCESS]
  1209. *******************************************************************************/
  1210. status_t LSM303C_MAG_Get_MagneticThreshold(void *handle, u8_t *buff)
  1211. {
  1212. u8_t i, j, k;
  1213. u8_t numberOfByteForDimension;
  1214. numberOfByteForDimension=2/1;
  1215. k=0;
  1216. for (i=0; i<1;i++ )
  1217. {
  1218. for (j=0; j<numberOfByteForDimension;j++ )
  1219. {
  1220. if( !LSM303C_MAG_ReadReg(handle, LSM303C_MAG_INT_THS_L+k, &buff[k], 1))
  1221. return MEMS_ERROR;
  1222. k++;
  1223. }
  1224. }
  1225. return MEMS_SUCCESS;
  1226. }
  1227. /*******************************************************************************
  1228. * Function Name : SwapHighLowByte
  1229. * Description : Swap High/low byte in multiple byte values
  1230. * It works with minimum 2 byte for every dimension.
  1231. * Example x,y,z with 2 byte for every dimension
  1232. *
  1233. * Input : bufferToSwap -> buffer to swap
  1234. * numberOfByte -> the buffer length in byte
  1235. * dimension -> number of dimension
  1236. *
  1237. * Output : bufferToSwap -> buffer swapped
  1238. * Return : None
  1239. *******************************************************************************/
  1240. void LSM303C_MAG_SwapHighLowByte(u8_t *bufferToSwap, u8_t numberOfByte, u8_t dimension)
  1241. {
  1242. u8_t numberOfByteForDimension, i, j;
  1243. u8_t tempValue[10];
  1244. numberOfByteForDimension=numberOfByte/dimension;
  1245. for (i=0; i<dimension;i++ )
  1246. {
  1247. for (j=0; j<numberOfByteForDimension;j++ )
  1248. tempValue[j]=bufferToSwap[j+i*numberOfByteForDimension];
  1249. for (j=0; j<numberOfByteForDimension;j++ )
  1250. *(bufferToSwap+i*(numberOfByteForDimension)+j)=*(tempValue+(numberOfByteForDimension-1)-j);
  1251. }
  1252. }