lps25hb.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. /**
  2. ******************************************************************************
  3. * @file lps25hb.c
  4. * @author HESA Application Team
  5. * @version 1.0.0
  6. * @date 07/04/2014
  7. * @brief LPS25HB driver file
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  10. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  11. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  12. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  13. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *
  15. * THIS SOURCE CODE IS PROTECTED BY A LICENSE.
  16. * FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
  17. * IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "hal/lps25hb.h"
  24. #include "driver/i2c_master.h"
  25. #ifdef USE_FULL_ASSERT_LPS25HB
  26. #include <stdio.h>
  27. #endif
  28. /** @addtogroup Environmental_Sensor
  29. * @{
  30. */
  31. /** @defgroup LPS25HB_DRIVER
  32. * @brief LPS25HB DRIVER
  33. * @{
  34. */
  35. #define LPS25HB_WRITE_ADDRESS 0xB8
  36. #define LPS25HB_READ_ADDRESS 0xB9
  37. uint8_t ICACHE_FLASH_ATTR Sensor_IO_Write(uint8_t WriteAddr, uint16_t nBytesToWrite, uint8_t *pBuffer)
  38. {
  39. uint16_t i;
  40. i2c_master_init();
  41. // write device address
  42. i2c_master_start();
  43. i2c_master_writeByte(LPS25HB_WRITE_ADDRESS);
  44. if (!i2c_master_checkAck()) {
  45. i2c_master_stop();
  46. return 1;
  47. }
  48. // write initial address for reg to be written to
  49. i2c_master_writeByte(WriteAddr);
  50. if (!i2c_master_checkAck()) {
  51. i2c_master_stop();
  52. return 1;
  53. }
  54. // write bytes
  55. for (i=0; i<(nBytesToWrite); i++) {
  56. i2c_master_writeByte(pBuffer[i]);
  57. if (!i2c_master_checkAck()) {
  58. i2c_master_stop();
  59. return 1;
  60. }
  61. }
  62. i2c_master_stop();
  63. return 0;
  64. // enable LSM6DS3 for SPI communication
  65. //lsm6ds3_i2c_short(false);
  66. }
  67. uint8_t ICACHE_FLASH_ATTR Sensor_IO_Read(uint8_t ReadAddr, uint16_t nBytesToRead, uint8_t *pBuffer)
  68. {
  69. uint16_t i;
  70. i2c_master_init();
  71. // enable LSM6DS3 for I2C communication
  72. //lsm6ds3_i2c_short(true);
  73. // open connection to devive to write
  74. i2c_master_start();
  75. i2c_master_writeByte(LPS25HB_WRITE_ADDRESS);
  76. if (!i2c_master_checkAck()) {
  77. i2c_master_stop();
  78. return 1;
  79. }
  80. // write read address
  81. i2c_master_writeByte(ReadAddr);
  82. if (!i2c_master_checkAck()) {
  83. i2c_master_stop();
  84. return 1;
  85. }
  86. // open connection to device to read
  87. i2c_master_start();
  88. i2c_master_writeByte(LPS25HB_READ_ADDRESS);
  89. if (!i2c_master_checkAck()) {
  90. i2c_master_stop();
  91. return 1;
  92. }
  93. // read bytes
  94. for (i=0; i<nBytesToRead; i++)
  95. {
  96. pBuffer[i] = i2c_master_readByte();
  97. //os_printf("Byte %x\r\n", pBuffer[i]);
  98. if (i == (nBytesToRead - 1))
  99. i2c_master_send_nack();
  100. else
  101. i2c_master_send_ack();
  102. }
  103. i2c_master_stop();
  104. // enable LSM6DS3 for SPI communication
  105. // lsm6ds3_i2c_short(false);
  106. return 0;
  107. }
  108. /** @defgroup LPS25HB_Public_Functions
  109. * @{
  110. */
  111. /**
  112. * @brief Read identification code by WHO_AM_I register
  113. * @param Buffer to empty by Device identification Value.
  114. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  115. */
  116. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_DeviceID(uint8_t* deviceid)
  117. {
  118. if(LPS25HB_ReadReg(LPS25HB_WHO_AM_I_REG, 1, deviceid))
  119. return LPS25HB_ERROR;
  120. return LPS25HB_OK;
  121. }
  122. /**
  123. * @brief Get the LPS25HB driver version.
  124. * @param None
  125. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  126. */
  127. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_DriverVersion(LPS25HB_DriverVersion_st *Version)
  128. {
  129. Version->Major = LPS25HB_DriverVersion_Major;
  130. Version->Minor = LPS25HB_DriverVersion_Minor;
  131. Version->Point = LPS25HB_DriverVersion_Point;
  132. return LPS25HB_OK;
  133. }
  134. /**
  135. * @brief Set LPS25HB Pressure and Temperature Resolution Mode
  136. * @param Pressure Resolution
  137. * @param Temperature Resolution
  138. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  139. */
  140. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_Avg(LPS25HB_Avgp_et avgp,LPS25HB_Avgt_et avgt )
  141. {
  142. uint8_t tmp;
  143. LPS25HB_assert_param(IS_LPS25HB_AVGT(avgt));
  144. LPS25HB_assert_param(IS_LPS25HB_AVGP(avgp));
  145. if(LPS25HB_ReadReg(LPS25HB_RES_CONF_REG, 1, &tmp))
  146. return LPS25HB_ERROR;
  147. tmp &= ~(LPS25HB_AVGT_MASK | LPS25HB_AVGP_MASK);
  148. tmp |= (uint8_t)avgp;
  149. tmp |= (uint8_t)avgt;
  150. if(LPS25HB_WriteReg(LPS25HB_RES_CONF_REG, 1, &tmp) )
  151. return LPS25HB_ERROR;
  152. return LPS25HB_OK;
  153. }
  154. /**
  155. * @brief Exit the shutdown mode for LPS25HB.
  156. * @param None
  157. * @retval None
  158. */
  159. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Activate(void)
  160. {
  161. if(LPS25HB_Set_PowerDownMode(LPS25HB_SET))
  162. return LPS25HB_ERROR;
  163. return LPS25HB_OK;
  164. }
  165. /**
  166. * @brief Enter the shutdown mode for LPS25HB.
  167. * @param None
  168. * @retval None
  169. */
  170. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_DeActivate(void)
  171. {
  172. if(LPS25HB_Set_PowerDownMode(LPS25HB_RESET))
  173. return LPS25HB_ERROR;
  174. return LPS25HB_OK;
  175. }
  176. /**
  177. * @brief Set LPS25HB Pressure Resolution Mode
  178. * @param Pressure Resolution
  179. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  180. */
  181. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_AvgP(LPS25HB_Avgp_et avgp)
  182. {
  183. uint8_t tmp;
  184. LPS25HB_assert_param(IS_LPS25HB_AVGP(avgp));
  185. if(LPS25HB_ReadReg(LPS25HB_RES_CONF_REG, 1, &tmp))
  186. return LPS25HB_ERROR;
  187. tmp &= ~LPS25HB_AVGP_MASK;
  188. tmp |= (uint8_t)avgp;
  189. if(LPS25HB_WriteReg(LPS25HB_RES_CONF_REG, 1, &tmp))
  190. return LPS25HB_ERROR;
  191. return LPS25HB_OK;
  192. }
  193. /**
  194. * @brief Set LPS25HB Temperature Resolution Mode
  195. * @param Temperature Resolution
  196. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  197. */
  198. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_AvgT(LPS25HB_Avgt_et avgt)
  199. {
  200. uint8_t tmp;
  201. LPS25HB_assert_param(IS_LPS25HB_AVGT(avgt));
  202. if(LPS25HB_ReadReg(LPS25HB_RES_CONF_REG, 1, &tmp))
  203. return LPS25HB_ERROR;
  204. tmp &= ~LPS25HB_AVGT_MASK;
  205. tmp |= (uint8_t)avgt;
  206. if(LPS25HB_WriteReg(LPS25HB_RES_CONF_REG, 1, &tmp))
  207. return LPS25HB_ERROR;
  208. return LPS25HB_OK;
  209. }
  210. /**
  211. * @brief Get LPS25HB Pressure Resolution Mode
  212. * @param Pressure Resolution
  213. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  214. */
  215. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_AvgP(LPS25HB_Avgp_et* avgp)
  216. {
  217. uint8_t tmp;
  218. if(LPS25HB_ReadReg(LPS25HB_RES_CONF_REG, 1, &tmp))
  219. return LPS25HB_ERROR;
  220. *avgp = (LPS25HB_Avgp_et)(tmp & LPS25HB_AVGP_MASK);
  221. return LPS25HB_OK;
  222. }
  223. /**
  224. * @brief Get LPS25HB Temperature Resolution Mode
  225. * @param Temperature Resolution
  226. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  227. */
  228. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_AvgT(LPS25HB_Avgt_et* avgt)
  229. {
  230. uint8_t tmp;
  231. if(LPS25HB_ReadReg(LPS25HB_RES_CONF_REG, 1, &tmp) )
  232. return LPS25HB_ERROR;
  233. *avgt = (LPS25HB_Avgt_et)(tmp & LPS25HB_AVGT_MASK);
  234. return LPS25HB_OK;
  235. }
  236. /**
  237. * @brief Set LPS25HB Output Data Rate
  238. * @param Output Data Rate
  239. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  240. */
  241. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_Odr(LPS25HB_Odr_et odr)
  242. {
  243. uint8_t tmp;
  244. LPS25HB_assert_param(IS_LPS25HB_ODR(odr));
  245. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  246. return LPS25HB_ERROR;
  247. tmp &= ~LPS25HB_ODR_MASK;
  248. tmp |= (uint8_t)odr;
  249. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG1, 1, &tmp))
  250. return LPS25HB_ERROR;
  251. return LPS25HB_OK;
  252. }
  253. /**
  254. * @brief Get LPS25HB Output Data Rate
  255. * @param Buffer to empty with Output Data Rate
  256. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  257. */
  258. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_Odr(LPS25HB_Odr_et* odr)
  259. {
  260. uint8_t tmp;
  261. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  262. return LPS25HB_ERROR;
  263. *odr = (LPS25HB_Odr_et)(tmp & LPS25HB_ODR_MASK);
  264. return LPS25HB_OK;
  265. }
  266. /**
  267. * @brief SET/RESET LPS25HB Power Down Mode bit
  268. * @param LPS25HB_SET (Active Mode)/LPS25HB_RESET (Power Down Mode)
  269. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  270. */
  271. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_PowerDownMode(LPS25HB_BitStatus_et pd)
  272. {
  273. uint8_t tmp;
  274. LPS25HB_assert_param(IS_LPS25HB_BitStatus(pd));
  275. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  276. return LPS25HB_ERROR;
  277. tmp &= ~LPS25HB_PD_MASK;
  278. tmp |= ((uint8_t)pd)<<LPS25HB_PD_BIT;
  279. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG1, 1, &tmp))
  280. return LPS25HB_ERROR;
  281. return LPS25HB_OK;
  282. }
  283. /**
  284. * @brief Set Block Data Mode
  285. * @detail It is recommended to set BDU bit to �1�.
  286. * @detail This feature avoids reading LSB and MSB related to different samples.
  287. * @param LPS25HB_BDU_CONTINUOS_UPDATE, LPS25HB_BDU_NO_UPDATE
  288. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  289. */
  290. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_Bdu(LPS25HB_Bdu_et bdu)
  291. {
  292. uint8_t tmp;
  293. LPS25HB_assert_param(IS_LPS25HB_BDUMode(bdu));
  294. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  295. return LPS25HB_ERROR;
  296. tmp &= ~LPS25HB_BDU_MASK;
  297. tmp |= ((uint8_t)bdu);
  298. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG1, 1, &tmp))
  299. return LPS25HB_ERROR;
  300. return LPS25HB_OK;
  301. }
  302. /**
  303. * @brief Get Block Data Mode
  304. * @param Buffer to empty whit thw bdu mode read from sensor
  305. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  306. */
  307. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_Bdu(LPS25HB_Bdu_et* bdu)
  308. {
  309. uint8_t tmp;
  310. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  311. return LPS25HB_ERROR;
  312. *bdu = (LPS25HB_Bdu_et)(tmp & LPS25HB_BDU_MASK);
  313. return LPS25HB_OK;
  314. }
  315. /**
  316. * @brief Enable/Disable Interrupt Circuit.
  317. * @param LPS25HB_ENABLE/LPS25HB_DISABLE
  318. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  319. */
  320. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_InterruptCircuitEnable(LPS25HB_State_et diff_en)
  321. {
  322. uint8_t tmp;
  323. LPS25HB_assert_param(IS_LPS25HB_State(diff_en));
  324. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  325. return LPS25HB_ERROR;
  326. tmp &= ~LPS25HB_DIFF_EN_MASK;
  327. tmp |= ((uint8_t)diff_en)<<LPS25HB_DIFF_EN_BIT;
  328. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG1, 1, &tmp))
  329. return LPS25HB_ERROR;
  330. return LPS25HB_OK;
  331. }
  332. /**
  333. * @brief Get the Interrupt Circuit bit.
  334. * @param Buffer to empty whit thw diff_en mode read from sensor
  335. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  336. */
  337. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_InterruptCircuitEnable(LPS25HB_State_et* diff_en)
  338. {
  339. uint8_t tmp;
  340. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  341. return LPS25HB_ERROR;
  342. *diff_en= (LPS25HB_State_et)((tmp & LPS25HB_DIFF_EN_MASK)>>LPS25HB_DIFF_EN_BIT);
  343. return LPS25HB_OK;
  344. }
  345. /**
  346. * @brief Set ResetAutoZero Function bit
  347. * @details REF_P reg (@0x08..0A) set pressure reference to default tmp RPDS reg (0x39/3A).
  348. * @param None
  349. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  350. */
  351. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_ResetAZ(void)
  352. {
  353. uint8_t tmp;
  354. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  355. return LPS25HB_ERROR;
  356. /* Set the RESET_AZ bit*/
  357. /* RESET_AZ is self cleared*/
  358. tmp |= LPS25HB_RESET_AZ_MASK;
  359. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG1, 1, &tmp))
  360. return LPS25HB_ERROR;
  361. return LPS25HB_OK;
  362. }
  363. /**
  364. * @brief Set SPI mode: 3 Wire Interface or 4 Wire Interface
  365. * @param LPS25HB_SPI_3_WIRE, LPS25HB_SPI_4_WIRE
  366. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  367. */
  368. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_SpiInterface(LPS25HB_SPIMode_et spimode)
  369. {
  370. uint8_t tmp;
  371. LPS25HB_assert_param(IS_LPS25HB_SPIMode(spimode));
  372. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  373. return LPS25HB_ERROR;
  374. tmp &= ~LPS25HB_SIM_MASK;
  375. tmp |= (uint8_t)spimode;
  376. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG1, 1, &tmp))
  377. return LPS25HB_ERROR;
  378. return LPS25HB_OK;
  379. }
  380. /**
  381. * @brief Get SPI mode: 3 Wire Interface or 4 Wire Interface
  382. * @param Buffer to empty with spi mode read from Sensor
  383. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  384. */
  385. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_SpiInterface(LPS25HB_SPIMode_et* spimode)
  386. {
  387. uint8_t tmp;
  388. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  389. return LPS25HB_ERROR;
  390. *spimode = (LPS25HB_SPIMode_et)(tmp & LPS25HB_SIM_MASK);
  391. return LPS25HB_OK;
  392. }
  393. /**
  394. * @brief Enable/Disable I2C Mode
  395. * @param State: Enable (reset bit)/ Disable (set bit)
  396. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  397. */
  398. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_I2C(LPS25HB_State_et statei2c)
  399. {
  400. uint8_t tmp;
  401. LPS25HB_assert_param(IS_LPS25HB_State(statei2c));
  402. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  403. return LPS25HB_ERROR;
  404. tmp &= ~LPS25HB_I2C_MASK;
  405. if(statei2c == LPS25HB_DISABLE) {
  406. tmp |= LPS25HB_I2C_MASK;
  407. }
  408. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  409. return LPS25HB_ERROR;
  410. return LPS25HB_OK;
  411. }
  412. /**
  413. * @brief Set the one-shot bit in order to start acquisition when the ONE SHOT mode
  414. has been selected by the ODR configuration.
  415. * @detail Once the measurement is done, ONE_SHOT bit will self-clear.
  416. * @param None
  417. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  418. */
  419. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_StartOneShotMeasurement(void)
  420. {
  421. uint8_t tmp;
  422. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  423. return LPS25HB_ERROR;
  424. /* Set the one shot bit */
  425. /* Once the measurement is done, one shot bit will self-clear*/
  426. tmp |= LPS25HB_ONE_SHOT_MASK;
  427. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  428. return LPS25HB_ERROR;
  429. return LPS25HB_OK;
  430. }
  431. /**
  432. * @brief Set AutoZero Function bit
  433. * @detail When set to �1�, the actual pressure output is copied in the REF_P reg (@0x08..0A)
  434. * @param LPS25HB_SET/LPS25HB_RESET
  435. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  436. */
  437. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_AutoZeroFunction(LPS25HB_BitStatus_et autozero)
  438. {
  439. uint8_t tmp;
  440. LPS25HB_assert_param(IS_LPS25HB_BitStatus(autozero));
  441. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  442. return LPS25HB_ERROR;
  443. tmp &= ~LPS25HB_AUTO_ZERO_MASK;
  444. tmp |= ((uint8_t)autozero)<<LPS25HB_AUTO_ZERO_BIT;
  445. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  446. return LPS25HB_ERROR;
  447. return LPS25HB_OK;
  448. }
  449. /**
  450. * @brief Software Reset. Self-clearing upon completion
  451. * @param None
  452. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  453. */
  454. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_SwReset(void)
  455. {
  456. uint8_t tmp;
  457. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  458. return LPS25HB_ERROR;
  459. tmp |= (0x01<<LPS25HB_SW_RESET_BIT);
  460. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  461. return LPS25HB_ERROR;
  462. return LPS25HB_OK;
  463. }
  464. /**
  465. * @brief Reboot Memory Content
  466. * @param None
  467. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  468. */
  469. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_MemoryBoot(void)
  470. {
  471. uint8_t tmp;
  472. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  473. return LPS25HB_ERROR;
  474. tmp |= (0x01<<LPS25HB_BOOT_BIT);
  475. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  476. return LPS25HB_ERROR;
  477. return LPS25HB_OK;
  478. }
  479. /**
  480. * @brief Software Reset ann Reboot Memory Content.
  481. * @detail The device is reset to the power on configuration if the SWRESET bit is set to �1�
  482. and BOOT is set to �1�; Self-clearing upon completion.
  483. * @param None
  484. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  485. */
  486. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_SwResetAndMemoryBoot(void)
  487. {
  488. uint8_t tmp;
  489. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  490. return LPS25HB_ERROR;
  491. tmp |= ((0x01<<LPS25HB_SW_RESET_BIT) | (0x01<<LPS25HB_BOOT_BIT));
  492. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  493. return LPS25HB_ERROR;
  494. return LPS25HB_OK;
  495. }
  496. /**
  497. * @brief Enable/Disable FIFO Mode
  498. * @param LPS25HB_ENABLE/LPS25HB_DISABLE
  499. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  500. */
  501. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_FifoModeUse(LPS25HB_State_et status)
  502. {
  503. uint8_t tmp;
  504. LPS25HB_assert_param(IS_LPS25HB_State(status));
  505. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  506. return LPS25HB_ERROR;
  507. tmp &= ~LPS25HB_FIFO_EN_MASK;
  508. tmp |= ((uint8_t)status)<<LPS25HB_FIFO_EN_BIT;
  509. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  510. return LPS25HB_ERROR;
  511. return LPS25HB_OK;
  512. }
  513. /**
  514. * @brief Enable/Disable FIFO Watermark Level Use
  515. * @param LPS25HB_ENABLE/LPS25HB_DISABLE
  516. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  517. */
  518. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_FifoWatermarkLevelUse(LPS25HB_State_et status)
  519. {
  520. uint8_t tmp;
  521. LPS25HB_assert_param(IS_LPS25HB_State(status));
  522. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  523. return LPS25HB_ERROR;
  524. tmp &= ~LPS25HB_WTM_EN_MASK;
  525. tmp |= ((uint8_t)status)<<LPS25HB_WTM_EN_BIT;
  526. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  527. return LPS25HB_ERROR;
  528. return LPS25HB_OK;
  529. }
  530. /**
  531. * @brief Enable/Disable 1 HZ ODR Decimation
  532. * @param LPS25HB_ENABLE/LPS25HB_DISABLE
  533. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  534. */
  535. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_FifoMeanDecUse(LPS25HB_State_et status)
  536. {
  537. uint8_t tmp;
  538. LPS25HB_assert_param(IS_LPS25HB_State(status));
  539. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  540. return LPS25HB_ERROR;
  541. tmp &= ~LPS25HB_FIFO_MEAN_MASK;
  542. tmp |= ((uint8_t)status)<<LPS25HB_FIFO_MEAN_BIT;
  543. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG2, 1, &tmp))
  544. return LPS25HB_ERROR;
  545. return LPS25HB_OK;
  546. }
  547. /**
  548. * @brief Enable/Disable Interrupt Active High (default tmp 0) or Low(tmp 1)
  549. * @param LPS25HB_ENABLE/LPS25HB_DISABLE
  550. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  551. */
  552. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_InterruptActiveLevel(LPS25HB_State_et status)
  553. {
  554. uint8_t tmp;
  555. LPS25HB_assert_param(IS_LPS25HB_State(status));
  556. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG3, 1, &tmp))
  557. return LPS25HB_ERROR;
  558. tmp &= ~LPS25HB_INT_H_L_MASK;
  559. tmp |= ((uint8_t)status)<<LPS25HB_INT_H_L_BIT;
  560. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG3, 1, &tmp))
  561. return LPS25HB_ERROR;
  562. return LPS25HB_OK;
  563. }
  564. /**
  565. * @brief Push-pull/open drain selection on interrupt pads. Default tmp: 0
  566. * @param LPS25HB_PushPull/LPS25HB_OpenDrain
  567. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  568. */
  569. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_InterruptOutputType(LPS25HB_OutputType_et output)
  570. {
  571. uint8_t tmp;
  572. LPS25HB_assert_param(IS_LPS25HB_OutputType(output));
  573. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG3, 1, &tmp))
  574. return LPS25HB_ERROR;
  575. tmp &= ~LPS25HB_PP_OD_MASK;
  576. tmp |= (uint8_t)output;
  577. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG3, 1, &tmp))
  578. return LPS25HB_ERROR;
  579. return LPS25HB_OK;
  580. }
  581. /**
  582. * @brief Set Data signal on INT1 pad control bits. Default tmp: 00
  583. * @param LPS25HB_DATA,LPS25HB_P_HIGH_LPS25HB_P_LOW,LPS25HB_P_LOW_HIGH
  584. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  585. */
  586. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_InterruptControlConfig(LPS25HB_OutputSignalConfig_et config)
  587. {
  588. uint8_t tmp;
  589. LPS25HB_assert_param(IS_LPS25HB_OutputSignal(config));
  590. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG3, 1, &tmp))
  591. return LPS25HB_ERROR;
  592. tmp &= ~(LPS25HB_INT1_S2_MASK | LPS25HB_INT1_S1_MASK);
  593. tmp |= (uint8_t)config;
  594. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG3, 1, &tmp))
  595. return LPS25HB_ERROR;
  596. return LPS25HB_OK;
  597. }
  598. /**
  599. * @brief Set INT1 interrupt pins configuration
  600. * @param LPS25HB_EMPTY,LPS25HB_WTM,LPS25HB_OVR,LPS25HB_DATA_READY
  601. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  602. */
  603. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_InterruptDataConfig(LPS25HB_DataSignalType_et signal)
  604. {
  605. uint8_t tmp;
  606. LPS25HB_assert_param(IS_LPS25HB_DataSignal(signal));
  607. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG4, 1, &tmp))
  608. return LPS25HB_ERROR;
  609. tmp &= ~(LPS25HB_P1_EMPTY_MASK | LPS25HB_P1_WTM_MASK \
  610. | LPS25HB_P1_OVERRUN_MASK | LPS25HB_P1_DRDY_MASK );
  611. tmp |= (uint8_t)signal;
  612. if(LPS25HB_WriteReg(LPS25HB_CTRL_REG4, 1, &tmp))
  613. return LPS25HB_ERROR;
  614. return LPS25HB_OK;
  615. }
  616. /**
  617. * @brief Enable\Disable Interrupt Generation on differential pressure low and/or high event
  618. * @param LPS25HB_DISABLE_INT, LPS25HB_PHE,LPS25HB_PLE,LPS25HB_PLE_PHE
  619. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  620. */
  621. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_InterruptDifferentialConfig(LPS25HB_InterruptDiffConfig_et config)
  622. {
  623. uint8_t tmp;
  624. LPS25HB_assert_param(IS_LPS25HB_InterruptDiff(config));
  625. if(LPS25HB_ReadReg(LPS25HB_INTERRUPT_CFG_REG, 1, &tmp))
  626. return LPS25HB_ERROR;
  627. tmp &= ~(LPS25HB_PL_E_MASK | LPS25HB_PH_E_MASK);
  628. tmp |= (uint8_t)config;
  629. if(config!=LPS25HB_DISABLE_INT){
  630. /* Enable DIFF_EN bit in CTRL_REG1 */
  631. if(LPS25HB_Set_InterruptCircuitEnable(LPS25HB_ENABLE))
  632. return LPS25HB_ERROR;
  633. }
  634. if(LPS25HB_WriteReg(LPS25HB_INTERRUPT_CFG_REG, 1, &tmp))
  635. return LPS25HB_ERROR;
  636. return LPS25HB_OK;
  637. }
  638. /**
  639. * @brief Enable/Disable Latch Interrupt request into INT_SOURCE register.
  640. * @param LPS25HB_ENABLE/LPS25HB_DISABLE
  641. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  642. */
  643. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_LatchInterruptRequest(LPS25HB_State_et status)
  644. {
  645. uint8_t tmp;
  646. LPS25HB_assert_param(IS_LPS25HB_State(status));
  647. if(LPS25HB_ReadReg(LPS25HB_INTERRUPT_CFG_REG, 1, &tmp))
  648. return LPS25HB_ERROR;
  649. tmp &= ~LPS25HB_LIR_MASK;
  650. tmp |= (((uint8_t)status)<<LPS25HB_LIR_BIT);
  651. if(LPS25HB_WriteReg(LPS25HB_INTERRUPT_CFG_REG, 1, &tmp))
  652. return LPS25HB_ERROR;
  653. return LPS25HB_OK;
  654. }
  655. /**
  656. * @brief Get the Interrupt Generation on differential pressure status event.
  657. * @detail The INT_SOURCE register is cleared by reading it.
  658. * @param Status Event Flag: PH,PL,IA
  659. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  660. */
  661. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_InterruptDifferentialEventStatus(LPS25HB_InterruptDiffStatus_st* interruptsource)
  662. {
  663. uint8_t tmp;
  664. if(LPS25HB_ReadReg(LPS25HB_INTERRUPT_SOURCE_REG, 1, &tmp))
  665. return LPS25HB_ERROR;
  666. interruptsource->PH = (uint8_t)(tmp & LPS25HB_PH_MASK);
  667. interruptsource->PL = (uint8_t)((tmp & LPS25HB_PL_MASK)>>LPS25HB_PL_BIT);
  668. interruptsource->IA = (uint8_t)((tmp & LPS25HB_IA_MASK)>>LPS25HB_IA_BIT);
  669. return LPS25HB_OK;
  670. }
  671. /**
  672. * @brief Get the status of Pressure and Temperature data
  673. * @param Data Status Flag: TempDataAvailable, TempDataOverrun, PressDataAvailable, PressDataOverrun
  674. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  675. */
  676. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_DataStatus(LPS25HB_DataStatus_st* datastatus)
  677. {
  678. uint8_t tmp;
  679. if(LPS25HB_ReadReg(LPS25HB_STATUS_REG, 1, &tmp))
  680. return LPS25HB_ERROR;
  681. datastatus->TempDataAvailable = (tmp & LPS25HB_TDA_MASK);
  682. datastatus->PressDataAvailable = (uint8_t)((tmp & LPS25HB_PDA_MASK)>>LPS25HB_PDA_BIT);
  683. datastatus->TempDataOverrun = (uint8_t)((tmp & LPS25HB_TOR_MASK)>>LPS25HB_TOR_BIT);
  684. datastatus->PressDataOverrun = (uint8_t)((tmp & LPS25HB_POR_MASK)>>LPS25HB_POR_BIT);
  685. return LPS25HB_OK;
  686. }
  687. /**
  688. * @brief Get the raw pressure tmp
  689. * @detail The data are expressed as PRESS_OUT_H/_L/_XL in 2�s complement.
  690. Pout(hPA)=PRESS_OUT / 4096
  691. * @param The buffer to empty with the pressure raw tmp
  692. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  693. */
  694. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_RawPressure(int32_t *raw_press)
  695. {
  696. uint8_t buffer[3];
  697. uint32_t tmp = 0;
  698. uint8_t i;
  699. if(LPS25HB_ReadReg(LPS25HB_PRESS_OUT_XL_REG, 3, buffer))
  700. return LPS25HB_ERROR;
  701. /* Build the raw data */
  702. for(i=0; i<3; i++)
  703. tmp |= (((uint32_t)buffer[i]) << (8*i));
  704. /* convert the 2's complement 24 bit to 2's complement 32 bit */
  705. if(tmp & 0x00800000)
  706. tmp |= 0xFF000000;
  707. *raw_press = ((int32_t)tmp);
  708. return LPS25HB_OK;
  709. }
  710. /**
  711. * @brief Get the Pressure value in hPA.
  712. * @detail The data are expressed as PRESS_OUT_H/_L/_XL in 2�s complement.
  713. Pout(hPA)=PRESS_OUT / 4096
  714. * @param The buffer to empty with the pressure value that must be divided by 100 to get the value in hPA
  715. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  716. */
  717. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_Pressure(int32_t* Pout)
  718. {
  719. int32_t raw_press;
  720. if(LPS25HB_Get_RawPressure(&raw_press))
  721. return LPS25HB_ERROR;
  722. *Pout = (raw_press*100)/4096;
  723. return LPS25HB_OK;
  724. }
  725. /**
  726. * @brief Get the Raw Temperature tmp.
  727. * @detail Temperature data are expressed as TEMP_OUT_H&TEMP_OUT_L as 2�s complement number.
  728. Tout(degC)=42.5+ (TEMP_OUT/480)
  729. * @param Buffer to empty with the temperature raw tmp.
  730. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  731. */
  732. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_RawTemperature(int16_t* raw_data)
  733. {
  734. uint8_t buffer[2];
  735. uint16_t tmp;
  736. if(LPS25HB_ReadReg(LPS25HB_TEMP_OUT_L_REG, 2, buffer))
  737. return LPS25HB_ERROR;
  738. /* Build the raw tmp */
  739. tmp = (((uint16_t)buffer[1]) << 8) + (uint16_t)buffer[0];
  740. *raw_data = ((int16_t)tmp);
  741. return LPS25HB_OK;
  742. }
  743. /**
  744. * @brief Get the Temperature value in �C.
  745. * @detail Temperature data are expressed as TEMP_OUT_H&TEMP_OUT_L as 2�s complement number.
  746. * Tout(degC)=42.5+ (TEMP_OUT/480)
  747. * @param Buffer to empty with the temperature value that must be divided by 10 to get the value in �C
  748. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  749. */
  750. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_Temperature(int16_t* Tout)
  751. {
  752. int16_t raw_data;
  753. if(LPS25HB_Get_RawTemperature(&raw_data))
  754. return LPS25HB_ERROR;
  755. *Tout = raw_data/48 + 425;
  756. return LPS25HB_OK;
  757. }
  758. /**
  759. * @brief Get the threshold tmp used for pressure interrupt generation (hPA).
  760. * @detail THS_P=THS_P_H&THS_P_L and is expressed as unsigned number.
  761. P_ths(hPA)=(THS_P)/16.
  762. * @param Buffer to empty with the pressure threshold in hPA
  763. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  764. */
  765. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_PressureThreshold(int16_t* P_ths)
  766. {
  767. uint8_t tempReg[2];
  768. if(LPS25HB_ReadReg(LPS25HB_THS_P_LOW_REG, 2, tempReg))
  769. return LPS25HB_ERROR;
  770. *P_ths= (((((uint16_t)tempReg[1])<<8) + tempReg[0])/16);
  771. return LPS25HB_OK;
  772. }
  773. /**
  774. * @brief Set the threshold tmp used for pressure interrupt generation (hPA).
  775. * @detail THS_P=THS_P_H&THS_P_L and is expressed as unsigned number.
  776. P_ths(hPA)=(THS_P)/16.
  777. * @param Pressure threshold in hPA
  778. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  779. */
  780. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_PressureThreshold(int16_t P_ths)
  781. {
  782. uint8_t buffer[2];
  783. buffer[0] = (uint8_t)(16 * P_ths);
  784. buffer[1] = (uint8_t)(((uint16_t)(16 * P_ths))>>8);
  785. if(LPS25HB_WriteReg(LPS25HB_THS_P_LOW_REG, 2, buffer))
  786. return LPS25HB_ERROR;
  787. return LPS25HB_OK;
  788. }
  789. /**
  790. * @brief Set Fifo Mode
  791. * @param LPS25HB_FIFO_BYPASS_MODE, LPS25HB_FIFO_MODE, LPS25HB_FIFO_STREAM_MODE, LPS25HB_FIFO_TRIGGER_STREAMTOFIFO_MODE,
  792. LPS25HB_FIFO_TRIGGER_BYPASSTOSTREAM_MODE,LPS25HB_FIFO_MEAN_MODE, LPS25HB_FIFO_TRIGGER_BYPASSTOFIFO_MODE
  793. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  794. */
  795. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_FifoMode(LPS25HB_FifoMode_et fifomode)
  796. {
  797. uint8_t tmp;
  798. LPS25HB_assert_param(IS_LPS25HB_FifoMode(fifomode));
  799. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  800. return LPS25HB_ERROR;
  801. tmp &= ~LPS25HB_FMODE_MASK;
  802. tmp |= (uint8_t)fifomode;
  803. if(LPS25HB_WriteReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  804. return LPS25HB_ERROR;
  805. return LPS25HB_OK;
  806. }
  807. /**
  808. * @brief Get Fifo Mode
  809. * @param buffer to empty with fifo mode tmp
  810. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  811. */
  812. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_FifoMode(LPS25HB_FifoMode_et* fifomode)
  813. {
  814. uint8_t tmp;
  815. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  816. return LPS25HB_ERROR;
  817. tmp &= ~LPS25HB_WTM_POINT_MASK;
  818. *fifomode = (LPS25HB_FifoMode_et)tmp;
  819. return LPS25HB_OK;
  820. }
  821. /**
  822. * @brief Get the Fifo Status
  823. * @param Status Flag: FIFO_WTM,FIFO_EMPTY,FIFO_FULL,FIFO_LEVEL
  824. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  825. */
  826. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_FifoStatus(LPS25HB_FifoStatus_st* status)
  827. {
  828. uint8_t tmp;
  829. if(LPS25HB_ReadReg(LPS25HB_STATUS_FIFO_REG, 1, &tmp))
  830. return LPS25HB_ERROR;
  831. status->FIFO_WTM = (uint8_t)((tmp & LPS25HB_WTM_FIFO_MASK)>>LPS25HB_WTM_FIFO_BIT);
  832. status->FIFO_FULL = (uint8_t)((tmp & LPS25HB_FULL_FIFO_MASK)>>LPS25HB_FULL_FIFO_BIT);
  833. status->FIFO_EMPTY = (uint8_t)((tmp & LPS25HB_EMPTY_FIFO_MASK)>>LPS25HB_EMPTY_FIFO_BIT);
  834. status->FIFO_LEVEL = (uint8_t)(tmp & LPS25HB_DIFF_POINT_MASK);
  835. return LPS25HB_OK;
  836. }
  837. /**
  838. * @brief Set Watermark Value
  839. * @param wtmlevel = [0,31]
  840. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  841. */
  842. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_FifoWatermarkLevel(uint8_t wtmlevel)
  843. {
  844. uint8_t tmp;
  845. LPS25HB_assert_param(IS_LPS25HB_WtmLevel(wtmlevel));
  846. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  847. return LPS25HB_ERROR;
  848. tmp &= ~LPS25HB_WTM_POINT_MASK;
  849. tmp |= wtmlevel;
  850. if(LPS25HB_WriteReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  851. return LPS25HB_ERROR;
  852. return LPS25HB_OK;
  853. }
  854. /**
  855. * @brief Get Watermark Value
  856. * @param wtmlevel tmp read from sensor
  857. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  858. */
  859. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_FifoWatermarkLevel(uint8_t *wtmlevel)
  860. {
  861. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, wtmlevel))
  862. return LPS25HB_ERROR;
  863. *wtmlevel &= LPS25HB_WTM_POINT_MASK;
  864. return LPS25HB_OK;
  865. }
  866. /**
  867. * @brief Set the number of sample to perform moving average when FIFO_MEAN_MODE is used
  868. * @param LPS25HB_FIFO_SAMPLE_2,LPS25HB_FIFO_SAMPLE_4,LPS25HB_FIFO_SAMPLE_8,LPS25HB_FIFO_SAMPLE_16,LPS25HB_FIFO_SAMPLE_32
  869. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  870. */
  871. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_FifoSampleSize(LPS25HB_FifoMeanModeSample_et samplesize)
  872. {
  873. uint8_t tmp;
  874. LPS25HB_assert_param(IS_LPS25HB_FifoMeanModeSample(samplesize));
  875. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  876. return LPS25HB_ERROR;
  877. tmp &= ~LPS25HB_WTM_POINT_MASK;
  878. tmp |= (uint8_t)samplesize;
  879. if(LPS25HB_WriteReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  880. return LPS25HB_ERROR;
  881. return LPS25HB_OK;
  882. }
  883. /**
  884. * @brief Get the number of sample to perform moving average when FIFO_MEAN_MODE is used
  885. * @param buffer to empty with sample size tmp
  886. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  887. */
  888. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_FifoSampleSize(LPS25HB_FifoMeanModeSample_et* samplesize)
  889. {
  890. uint8_t tmp;
  891. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  892. return LPS25HB_ERROR;
  893. tmp &= LPS25HB_WTM_POINT_MASK;
  894. *samplesize = (LPS25HB_FifoMeanModeSample_et)tmp;
  895. return LPS25HB_OK;
  896. }
  897. /**
  898. * @brief Get the reference pressure after soldering for computing differential pressure (hPA)
  899. * @param buffer to empty with the he pressure tmp (hPA)
  900. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  901. */
  902. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_PressureOffsetValue(int16_t *pressoffset)
  903. {
  904. uint8_t buffer[2];
  905. int16_t raw_press;
  906. if(LPS25HB_ReadReg(LPS25HB_RPDS_L_REG, 2, buffer))
  907. return LPS25HB_ERROR;
  908. raw_press = (int16_t)((((uint16_t)buffer[1]) << 8) + (uint16_t)buffer[0]);
  909. *pressoffset = (raw_press*100)/4096;
  910. return LPS25HB_OK;
  911. }
  912. /**
  913. * @brief Set Generic Configuration
  914. * @param Struct to empty with the chosen tmp
  915. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  916. */
  917. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_GenericConfig(LPS25HB_ConfigTypeDef_st* pxLPS25HBInit)
  918. {
  919. /* Step 1. Init REF_P register*/
  920. /* The REF_P is the Reference Pressure. Its reset tmp is 0x00*/
  921. /* The REF_P will be set to the defualt RPDS (0x39h) tmp if Reset_AZ is enabled.*/
  922. /* The REF_P will be set the actual pressure output if AutoZero is enabled*/
  923. if((pxLPS25HBInit->Reset_AZ)==LPS25HB_ENABLE){
  924. if(LPS25HB_ResetAZ())
  925. return LPS25HB_ERROR;
  926. }
  927. else if((pxLPS25HBInit->AutoZero)==LPS25HB_ENABLE){
  928. if(LPS25HB_Set_AutoZeroFunction(LPS25HB_SET))
  929. return LPS25HB_ERROR;
  930. }
  931. /* Step 2. Init the Pressure and Temperature Resolution*/
  932. if(LPS25HB_Set_Avg(pxLPS25HBInit->PressResolution,pxLPS25HBInit->TempResolution))
  933. return LPS25HB_ERROR;
  934. /* Step 3. Init the Output Data Rate*/
  935. if(LPS25HB_Set_Odr(pxLPS25HBInit->OutputDataRate))
  936. return LPS25HB_ERROR;
  937. /*Step 4. BDU bit is used to inhibit the output registers update between the reading of upper and
  938. lower register parts. In default mode (BDU = �0�), the lower and upper register parts are
  939. updated continuously. If it is not sure to read faster than output data rate, it is recommended
  940. to set BDU bit to �1�. In this way, after the reading of the lower (upper) register part, the
  941. content of that output registers is not updated until the upper (lower) part is read too.
  942. This feature avoids reading LSB and MSB related to different samples.*/
  943. if(LPS25HB_Set_Bdu(pxLPS25HBInit->BDU))
  944. return LPS25HB_ERROR;
  945. /*Step 5. SIM bit selects the SPI serial interface mode.*/
  946. /* This feature has effect only if SPI interface is used*/
  947. if(LPS25HB_Set_SpiInterface(pxLPS25HBInit->Sim))
  948. return LPS25HB_ERROR;
  949. return LPS25HB_OK;
  950. }
  951. /**
  952. * @brief Get Generic Configuration
  953. * @param Struct to empty with the tmp read from sensor
  954. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  955. */
  956. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_GenericConfig(LPS25HB_ConfigTypeDef_st* pxLPS25HBInit)
  957. {
  958. uint8_t tmp;
  959. if(LPS25HB_ReadReg(LPS25HB_RES_CONF_REG, 1, &tmp))
  960. return LPS25HB_ERROR;
  961. pxLPS25HBInit->PressResolution=(LPS25HB_Avgp_et)(tmp&LPS25HB_AVGP_MASK);
  962. pxLPS25HBInit->TempResolution=(LPS25HB_Avgt_et)(tmp& LPS25HB_AVGT_MASK);
  963. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG1, 1, &tmp))
  964. return LPS25HB_ERROR;
  965. pxLPS25HBInit->OutputDataRate= (LPS25HB_Odr_et)(tmp & LPS25HB_ODR_MASK);
  966. pxLPS25HBInit->BDU=(LPS25HB_Bdu_et)(tmp & LPS25HB_BDU_MASK);
  967. pxLPS25HBInit->Sim=(LPS25HB_SPIMode_et)(tmp& LPS25HB_SIM_MASK);
  968. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  969. return LPS25HB_ERROR;
  970. pxLPS25HBInit->AutoZero=(LPS25HB_State_et)((tmp&LPS25HB_RESET_AZ_MASK)>>LPS25HB_AUTO_ZERO_BIT);
  971. return LPS25HB_OK;
  972. }
  973. /**
  974. * @brief Set Interrupt Configuration
  975. * @param Struct to empty with the chosen tmp
  976. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  977. */
  978. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_InterruptConfig(LPS25HB_InterruptTypeDef_st* pLPS25HBInt)
  979. {
  980. if(LPS25HB_Set_InterruptActiveLevel(pLPS25HBInt->INT_H_L))
  981. return LPS25HB_ERROR;
  982. if(LPS25HB_Set_InterruptOutputType(pLPS25HBInt->PP_OD))
  983. return LPS25HB_ERROR;
  984. if(LPS25HB_Set_InterruptControlConfig(pLPS25HBInt->OutputSignal_INT1))
  985. return LPS25HB_ERROR;
  986. if(pLPS25HBInt->OutputSignal_INT1==LPS25HB_DATA){
  987. if(LPS25HB_Set_InterruptDataConfig(pLPS25HBInt->DataInterrupt_INT1))
  988. return LPS25HB_ERROR;
  989. }
  990. if(LPS25HB_LatchInterruptRequest(pLPS25HBInt->LatchIRQ))
  991. return LPS25HB_ERROR;
  992. if(LPS25HB_Set_PressureThreshold(pLPS25HBInt->fP_threshold))
  993. return LPS25HB_ERROR;
  994. /*DIFF_EN bit is used to enable the circuitry for the computing of differential pressure output.*/
  995. /*It is suggested to turn on the circuitry only after the configuration of REF_P_x and THS_P_x.*/
  996. if(LPS25HB_Set_InterruptDifferentialConfig(pLPS25HBInt->PressureInterrupt))
  997. return LPS25HB_ERROR;
  998. return LPS25HB_OK;
  999. }
  1000. /**
  1001. * @brief Get Interrupt Configuration
  1002. * @param Struct to empty with the tmp read from sensor
  1003. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  1004. */
  1005. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_InterruptConfig(LPS25HB_InterruptTypeDef_st* pLPS25HBInt)
  1006. {
  1007. uint8_t tmp;
  1008. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG3, 1, &tmp))
  1009. return LPS25HB_ERROR;
  1010. pLPS25HBInt->INT_H_L=(LPS25HB_State_et)((tmp&LPS25HB_INT_H_L_MASK)>>LPS25HB_INT_H_L_BIT);
  1011. pLPS25HBInt->PP_OD =(LPS25HB_OutputType_et)(tmp&LPS25HB_PP_OD_MASK);
  1012. pLPS25HBInt->OutputSignal_INT1=(LPS25HB_OutputSignalConfig_et)((tmp&0x03));
  1013. if(pLPS25HBInt->OutputSignal_INT1==LPS25HB_DATA){
  1014. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG4, 1, &tmp))
  1015. return LPS25HB_ERROR;
  1016. pLPS25HBInt->DataInterrupt_INT1=(LPS25HB_DataSignalType_et)(tmp &=0x0F);
  1017. }
  1018. if(LPS25HB_ReadReg(LPS25HB_INTERRUPT_CFG_REG, 1, &tmp))
  1019. return LPS25HB_ERROR;
  1020. pLPS25HBInt->LatchIRQ=(LPS25HB_State_et)((tmp &LPS25HB_LIR_MASK)>>LPS25HB_LIR_BIT);
  1021. pLPS25HBInt->PressureInterrupt=(LPS25HB_InterruptDiffConfig_et)(tmp &LPS25HB_PE_MASK);
  1022. if(LPS25HB_Get_PressureThreshold(&pLPS25HBInt->fP_threshold))
  1023. return LPS25HB_ERROR;
  1024. return LPS25HB_OK;
  1025. }
  1026. /**
  1027. * @brief Set Fifo Configuration
  1028. * @param Struct to empty with the chosen tmp
  1029. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  1030. */
  1031. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Set_FifoConfig(LPS25HB_FIFOTypeDef_st* pLPS25HBFIFO)
  1032. {
  1033. if(pLPS25HBFIFO->FIFO_MODE == LPS25HB_FIFO_BYPASS_MODE) {
  1034. /* FIFO Disable-> FIFO_EN bit=0 in CTRL_REG2*/
  1035. if(LPS25HB_Set_FifoModeUse(LPS25HB_DISABLE))
  1036. return LPS25HB_ERROR;
  1037. }
  1038. else {
  1039. /* FIFO Enable-> FIFO_EN bit=1 in CTRL_REG2*/
  1040. if(LPS25HB_Set_FifoModeUse(LPS25HB_ENABLE))
  1041. return LPS25HB_ERROR;
  1042. if(pLPS25HBFIFO->FIFO_MODE==LPS25HB_FIFO_MEAN_MODE){
  1043. if(LPS25HB_Set_FifoSampleSize(pLPS25HBFIFO->MEAN_MODE_SAMPLE))
  1044. return LPS25HB_ERROR;
  1045. if(pLPS25HBFIFO->FIFO_MEAN_DEC)
  1046. if(LPS25HB_Set_FifoMeanDecUse(LPS25HB_ENABLE))
  1047. return LPS25HB_ERROR;
  1048. }
  1049. else{
  1050. if (pLPS25HBFIFO->WTM_INT){
  1051. if(LPS25HB_Set_FifoWatermarkLevelUse(LPS25HB_ENABLE))
  1052. return LPS25HB_ERROR;
  1053. if(LPS25HB_Set_FifoWatermarkLevel(pLPS25HBFIFO->WTM_LEVEL))
  1054. return LPS25HB_ERROR;
  1055. }
  1056. }
  1057. }
  1058. if(LPS25HB_Set_FifoMode(pLPS25HBFIFO->FIFO_MODE))
  1059. return LPS25HB_ERROR;
  1060. return LPS25HB_OK;
  1061. }
  1062. /**
  1063. * @brief Get Fifo Configuration
  1064. * @param Struct to empty with the tmp read from sensor
  1065. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  1066. */
  1067. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_FifoConfig(LPS25HB_FIFOTypeDef_st* pLPS25HBFIFO)
  1068. {
  1069. uint8_t tmp;
  1070. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  1071. return LPS25HB_ERROR;
  1072. pLPS25HBFIFO->FIFO_MODE=(LPS25HB_FifoMode_et)(tmp&LPS25HB_FMODE_MASK);
  1073. if(pLPS25HBFIFO->FIFO_MODE==LPS25HB_FIFO_MEAN_MODE){
  1074. pLPS25HBFIFO->MEAN_MODE_SAMPLE=(LPS25HB_FifoMeanModeSample_et)(tmp&LPS25HB_WTM_POINT_MASK);
  1075. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  1076. return LPS25HB_ERROR;
  1077. pLPS25HBFIFO->FIFO_MEAN_DEC=(LPS25HB_State_et)((tmp&LPS25HB_FIFO_MEAN_MASK)>>LPS25HB_FIFO_MEAN_BIT);
  1078. }
  1079. else{
  1080. if(pLPS25HBFIFO->FIFO_MODE != LPS25HB_FIFO_BYPASS_MODE) {
  1081. if(LPS25HB_ReadReg(LPS25HB_CTRL_REG2, 1, &tmp))
  1082. return LPS25HB_ERROR;
  1083. pLPS25HBFIFO->WTM_INT=(LPS25HB_State_et)((tmp&LPS25HB_WTM_EN_MASK)>>LPS25HB_WTM_EN_BIT);
  1084. if (pLPS25HBFIFO->WTM_INT){
  1085. if(LPS25HB_ReadReg(LPS25HB_CTRL_FIFO_REG, 1, &tmp))
  1086. return LPS25HB_ERROR;
  1087. pLPS25HBFIFO->WTM_LEVEL=(uint8_t)(tmp&LPS25HB_WTM_POINT_MASK);
  1088. }
  1089. }
  1090. }
  1091. return LPS25HB_OK;
  1092. }
  1093. /**
  1094. * @brief Get the Reference Pressure tmp that is sum to the sensor output pressure
  1095. * @param Buffer to empty with reference pressure tmp
  1096. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  1097. */
  1098. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_ReferencePressure(int32_t* RefP)
  1099. {
  1100. uint8_t buffer[3];
  1101. uint32_t tempVal=0;
  1102. int32_t raw_press;
  1103. uint8_t i;
  1104. if(LPS25HB_ReadReg(LPS25HB_REF_P_XL_REG, 3, buffer))
  1105. return LPS25HB_ERROR;
  1106. /* Build the raw data */
  1107. for(i=0; i<3; i++)
  1108. tempVal |= (((uint32_t)buffer[i]) << (8*i));
  1109. /* convert the 2's complement 24 bit to 2's complement 32 bit */
  1110. if(tempVal & 0x00800000)
  1111. tempVal |= 0xFF000000;
  1112. raw_press =((int32_t)tempVal);
  1113. *RefP = (raw_press*100)/4096;
  1114. return LPS25HB_OK;
  1115. }
  1116. /**
  1117. * @brief Check if the single measurement has completed.
  1118. * @param tmp is set to 1, when the measure is completed
  1119. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  1120. */
  1121. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_IsMeasurementCompleted(uint8_t* Is_Measurement_Completed)
  1122. {
  1123. uint8_t tmp;
  1124. LPS25HB_DataStatus_st datastatus;
  1125. if(LPS25HB_ReadReg(LPS25HB_STATUS_REG, 1, &tmp))
  1126. return LPS25HB_ERROR;
  1127. datastatus.TempDataAvailable=(uint8_t)(tmp&0x01);
  1128. datastatus.PressDataAvailable= (uint8_t)((tmp&0x02)>>LPS25HB_PDA_BIT);
  1129. *Is_Measurement_Completed=(uint8_t)((datastatus.PressDataAvailable) & (datastatus.TempDataAvailable));
  1130. return LPS25HB_OK;
  1131. }
  1132. /**
  1133. * @brief Get the values of the last single measurement.
  1134. * @param Pressure and temperature tmp
  1135. * @retval Status [LPS25HB_ERROR, LPS25HB_OK]
  1136. */
  1137. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_Get_Measurement(LPS25HB_MeasureTypeDef_st *Measurement_Value)
  1138. {
  1139. int16_t Tout;
  1140. int32_t Pout;
  1141. if(LPS25HB_Get_Temperature(&Tout))
  1142. return LPS25HB_ERROR;
  1143. Measurement_Value->Tout=Tout;
  1144. if(LPS25HB_Get_Pressure(&Pout))
  1145. return LPS25HB_ERROR;
  1146. Measurement_Value->Pout=Pout;
  1147. return LPS25HB_OK;
  1148. }
  1149. /**
  1150. * @brief De initialization function for LPS25HB.
  1151. * This function put the LPS25HB in power down, make a memory boot and clear the data output flags.
  1152. * @param None.
  1153. * @retval Error code [LPS25HB_OK, LPS25HB_ERROR].
  1154. */
  1155. LPS25HB_Error_et ICACHE_FLASH_ATTR LPS25HB_DeInit(void)
  1156. {
  1157. LPS25HB_MeasureTypeDef_st Measurement_Value;
  1158. /* LPS25HB in power down */
  1159. if(LPS25HB_Set_PowerDownMode(LPS25HB_RESET))
  1160. return LPS25HB_ERROR;
  1161. /* Make LPS25HB Reset and Reboot */
  1162. if(LPS25HB_SwResetAndMemoryBoot())
  1163. return LPS25HB_ERROR;
  1164. /* Dump of data output */
  1165. if(LPS25HB_Get_Measurement(& Measurement_Value))
  1166. return LPS25HB_ERROR;
  1167. return LPS25HB_OK;
  1168. }
  1169. #ifdef USE_FULL_ASSERT_LPS25HB
  1170. /**
  1171. * @brief Reports the name of the source file and the source line number
  1172. * where the assert_param error has occurred.
  1173. * @param file: pointer to the source file name
  1174. * @param line: assert_param error line source number
  1175. * @retval : None
  1176. */
  1177. void ICACHE_FLASH_ATTR LPS25HB_assert_failed(uint8_t* file, uint32_t line)
  1178. {
  1179. /* User can add his own implementation to report the file name and line number */
  1180. printf("Wrong parameters tmp: file %s on line %d\r\n", file, line);
  1181. /* Infinite loop */
  1182. while (1)
  1183. {
  1184. }
  1185. }
  1186. #endif
  1187. /**
  1188. * @}
  1189. */
  1190. /**
  1191. * @}
  1192. */
  1193. /**
  1194. * @}
  1195. */
  1196. /******************* (C) COPYRIGHT 2013 STMicroelectronics *****END OF FILE****/