bma400.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909
  1. /**\mainpage
  2. * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the copyright holder nor the names of the
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  23. * OR CONTRIBUTORS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  33. *
  34. * The information provided is believed to be accurate and reliable.
  35. * The copyright holder assumes no responsibility
  36. * for the consequences of use
  37. * of such information nor for any infringement of patents or
  38. * other rights of third parties which may result from its use.
  39. * No license is granted by implication or otherwise under any patent or
  40. * patent rights of the copyright holder.
  41. *
  42. * File bma400.c
  43. * Date 25 Sep 2018
  44. * Version 1.5.0
  45. *
  46. */
  47. /*! @file bma400.c
  48. * @brief Sensor driver for BMA400 sensor
  49. */
  50. #include "bma400.h"
  51. /************************** Internal macros *******************************/
  52. /********************** Static function declarations ************************/
  53. /*!
  54. * @brief This internal API is used to validate the device pointer for
  55. * null conditions.
  56. *
  57. * @param[in] dev : Structure instance of bma400_dev.
  58. *
  59. * @return Result of API execution status
  60. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  61. */
  62. static int8_t null_ptr_check(const struct bma400_dev *dev);
  63. /*!
  64. * @brief This internal API is used to set the accel configurations in sensor
  65. *
  66. * @param[in] accel_conf : Structure instance with accel configurations
  67. * @param[in] dev : Structure instance of bma400_dev
  68. *
  69. * @return Result of API execution status
  70. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  71. */
  72. static int8_t set_accel_conf(const struct bma400_acc_conf *accel_conf, const struct bma400_dev *dev);
  73. /*!
  74. * @brief This API reads accel data along with sensor time
  75. *
  76. * @param[in] data_sel : Variable to select the data to be read
  77. * @param[in,out] accel : Structure instance to store the accel data
  78. * @param[in] dev : Structure instance of bma400_dev
  79. *
  80. * Assignable values for data_sel:
  81. * - BMA400_DATA_ONLY
  82. * - BMA400_DATA_SENSOR_TIME
  83. *
  84. * @return Result of API execution status
  85. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  86. */
  87. static int8_t get_accel_data(uint8_t data_sel, struct bma400_sensor_data *accel, const struct bma400_dev *dev);
  88. /*!
  89. * @brief This API enables the auto-wakeup feature
  90. * of the sensor using a timeout value
  91. *
  92. * @param[in] wakeup_conf : Structure instance of wakeup configurations
  93. * @param[in] dev : Structure instance of bma400_dev
  94. *
  95. * @return Result of API execution status
  96. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  97. */
  98. static int8_t set_autowakeup_timeout(const struct bma400_auto_wakeup_conf *wakeup_conf, const struct bma400_dev *dev);
  99. /*!
  100. * @brief This API enables the auto-wakeup feature of the sensor
  101. *
  102. * @param[in] conf : Configuration value to enable/disable
  103. * auto-wakeup interrupt
  104. * @param[in] dev : Structure instance of bma400_dev
  105. *
  106. * @return Result of API execution status
  107. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  108. */
  109. static int8_t set_auto_wakeup(uint8_t conf, const struct bma400_dev *dev);
  110. /*!
  111. * @brief This API sets the parameters for auto-wakeup feature
  112. * of the sensor
  113. *
  114. * @param[in] wakeup_conf : Structure instance of wakeup configurations
  115. * @param[in] dev : Structure instance of bma400_dev
  116. *
  117. * @return Result of API execution status
  118. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  119. */
  120. static int8_t set_autowakeup_interrupt(const struct bma400_wakeup_conf *wakeup_conf, const struct bma400_dev *dev);
  121. /*!
  122. * @brief This API sets the sensor to enter low power mode
  123. * automatically based on the configurations
  124. *
  125. * @param[in] auto_lp_conf : Structure instance of auto-low power settings
  126. * @param[in] dev : Structure instance of bma400_dev
  127. *
  128. * @return Result of API execution status
  129. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  130. */
  131. static int8_t set_auto_low_power(const struct bma400_auto_lp_conf *auto_lp_conf, const struct bma400_dev *dev);
  132. /*!
  133. * @brief This API sets the tap setting parameters
  134. *
  135. * @param[in] tap_set : Structure instance of tap configurations
  136. * @param[in] dev : Structure instance of bma400_dev
  137. *
  138. * @return Result of API execution status
  139. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  140. */
  141. static int8_t set_tap_conf(const struct bma400_tap_conf *tap_set, const struct bma400_dev *dev);
  142. /*!
  143. * @brief This API sets the parameters for activity change detection
  144. *
  145. * @param[in] act_ch_set : Structure instance of activity change
  146. * configurations
  147. * @param[in] dev : Structure instance of bma400_dev
  148. *
  149. * @return Result of API execution status
  150. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  151. */
  152. static int8_t set_activity_change_conf(const struct bma400_act_ch_conf *act_ch_set, const struct bma400_dev *dev);
  153. /*!
  154. * @brief This API sets the parameters for generic interrupt1 configuration
  155. *
  156. * @param[in] gen_int_set : Structure instance of generic interrupt
  157. * configurations
  158. * @param[in] dev : Structure instance of bma400_dev
  159. *
  160. * @return Result of API execution status
  161. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  162. */
  163. static int8_t set_gen1_int(const struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev);
  164. /*!
  165. * @brief This API sets the parameters for generic interrupt2 configuration
  166. *
  167. * @param[in] gen_int_set : Structure instance of generic interrupt
  168. * configurations
  169. * @param[in] dev : Structure instance of bma400_dev
  170. *
  171. * @return Result of API execution status
  172. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  173. */
  174. static int8_t set_gen2_int(const struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev);
  175. /*!
  176. * @brief This API sets the parameters for orientation interrupt
  177. *
  178. * @param[in] orient_conf : Structure instance of orient interrupt
  179. * configurations
  180. * @param[in] dev : Structure instance of bma400_dev
  181. *
  182. * @return Result of API execution status
  183. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  184. */
  185. static int8_t set_orient_int(const struct bma400_orient_int_conf *orient_conf, const struct bma400_dev *dev);
  186. /*!
  187. * @brief This internal API is used to get the accel configurations in sensor
  188. *
  189. * @param[in,out] accel_conf : Structure instance of basic
  190. * accelerometer configuration
  191. * @param[in] dev : Structure instance of bma400_dev
  192. *
  193. * @return Result of API execution status
  194. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  195. */
  196. static int8_t get_accel_conf(struct bma400_acc_conf *accel_conf, const struct bma400_dev *dev);
  197. /*!
  198. * @brief This API gets the set sensor settings for auto-wakeup timeout feature
  199. *
  200. * @param[in,out] wakeup_conf : Structure instance of wake-up configurations
  201. * @param[in] dev : Structure instance of bma400_dev
  202. *
  203. * @return Result of API execution status
  204. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  205. */
  206. static int8_t get_autowakeup_timeout(struct bma400_auto_wakeup_conf *wakeup_conf, const struct bma400_dev *dev);
  207. /*!
  208. * @brief This API gets the set sensor settings for
  209. * auto-wakeup interrupt feature
  210. *
  211. * @param[in,out] wakeup_conf : Structure instance of wake-up configurations
  212. * @param[in] dev : Structure instance of bma400_dev
  213. *
  214. * @return Result of API execution status
  215. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  216. */
  217. static int8_t get_autowakeup_interrupt(struct bma400_wakeup_conf *wakeup_conf, const struct bma400_dev *dev);
  218. /*!
  219. * @brief This API gets the sensor to get the auto-low
  220. * power mode configuration settings
  221. *
  222. * @param[in,out] auto_lp_conf : Structure instance of low power
  223. * configurations
  224. * @param[in] dev : Structure instance of bma400_dev
  225. *
  226. * @return Result of API execution status
  227. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  228. */
  229. static int8_t get_auto_low_power(struct bma400_auto_lp_conf *auto_lp_conf, const struct bma400_dev *dev);
  230. /*!
  231. * @brief This API sets the tap setting parameters
  232. *
  233. * @param[in,out] tap_set : Structure instance of tap configurations
  234. * @param[in] dev : Structure instance of bma400_dev
  235. *
  236. * @return Result of API execution status
  237. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  238. */
  239. static int8_t get_tap_conf(struct bma400_tap_conf *tap_set, const struct bma400_dev *dev);
  240. /*!
  241. * @brief This API gets the parameters for activity change detection
  242. *
  243. * @param[in,out] act_ch_set : Structure instance of activity
  244. * change configurations
  245. * @param[in] dev : Structure instance of bma400_dev
  246. *
  247. * @return Result of API execution status
  248. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  249. */
  250. static int8_t get_activity_change_conf(struct bma400_act_ch_conf *act_ch_set, const struct bma400_dev *dev);
  251. /*!
  252. * @brief This API gets the generic interrupt1 configuration
  253. *
  254. * @param[in,out] gen_int_set : Structure instance of generic
  255. * interrupt configurations
  256. * @param[in] dev : Structure instance of bma400_dev
  257. *
  258. * @return Result of API execution status
  259. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  260. */
  261. static int8_t get_gen1_int(struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev);
  262. /*!
  263. * @brief This API gets the generic interrupt2 configuration
  264. *
  265. * @param[in,out] gen_int_set : Structure instance of generic
  266. * interrupt configurations
  267. * @param[in] dev : Structure instance of bma400_dev
  268. *
  269. * @return Result of API execution status
  270. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  271. */
  272. static int8_t get_gen2_int(struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev);
  273. /*!
  274. * @brief This API gets the parameters for orientation interrupt
  275. *
  276. * @param[in,out] orient_conf : Structure instance of orient
  277. * interrupt configurations
  278. * @param[in] dev : Structure instance of bma400_dev
  279. *
  280. * @return Result of API execution status
  281. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  282. */
  283. static int8_t get_orient_int(struct bma400_orient_int_conf *orient_conf, const struct bma400_dev *dev);
  284. /*!
  285. * @brief This API sets the selected interrupt to be mapped to
  286. * the hardware interrupt pin of the sensor
  287. *
  288. * @param[in,out] data_array : Data array of interrupt pin configurations
  289. * @param[in] int_enable : Interrupt selected for pin mapping
  290. * @param[in] int_map : Interrupt channel to be mapped
  291. *
  292. * @return Nothing
  293. */
  294. static void map_int_pin(uint8_t *data_array, uint8_t int_enable, enum bma400_int_chan int_map);
  295. /*!
  296. * @brief This API checks whether the interrupt is mapped to the INT pin1
  297. * or INT pin2 of the sensor
  298. *
  299. * @param[in] int_1_map : Variable to denote whether the interrupt is
  300. * mapped to INT1 pin or not
  301. * @param[in] int_2_map : Variable to denote whether the interrupt is
  302. * mapped to INT2 pin or not
  303. * @param[in,out] int_map : Interrupt channel which is mapped
  304. * INT1/INT2/NONE/BOTH
  305. *
  306. * @return Nothing
  307. */
  308. static void check_mapped_interrupts(uint8_t int_1_map, uint8_t int_2_map, enum bma400_int_chan *int_map);
  309. /*!
  310. * @brief This API gets the selected interrupt and its mapping to
  311. * the hardware interrupt pin of the sensor
  312. *
  313. * @param[in,out] data_array : Data array of interrupt pin configurations
  314. * @param[in] int_enable : Interrupt selected for pin mapping
  315. * @param[out] int_map : Interrupt channel which is mapped
  316. *
  317. * @return Nothing
  318. */
  319. static void get_int_pin_map(const uint8_t *data_array, uint8_t int_enable, enum bma400_int_chan *int_map);
  320. /*!
  321. * @brief This API is used to set the interrupt pin configurations
  322. *
  323. * @param[in] int_conf : Interrupt pin configuration
  324. * @param[in] dev : Structure instance of bma400_dev
  325. *
  326. * @return Result of API execution status
  327. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  328. */
  329. static int8_t set_int_pin_conf(struct bma400_int_pin_conf int_conf, const struct bma400_dev *dev);
  330. /*!
  331. * @brief This API is used to set the interrupt pin configurations
  332. *
  333. * @param[in,out] int_conf : Interrupt pin configuration
  334. * @param[in] dev : Structure instance of bma400_dev
  335. *
  336. * @return Result of API execution status
  337. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  338. */
  339. static int8_t get_int_pin_conf(struct bma400_int_pin_conf *int_conf, const struct bma400_dev *dev);
  340. /*!
  341. * @brief This API is used to set the FIFO configurations
  342. *
  343. * @param[in,out] fifo_conf : Structure instance containing the FIFO
  344. * configuration set in the sensor
  345. * @param[in] dev : Structure instance of bma400_dev
  346. *
  347. * @return Result of API execution status
  348. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  349. */
  350. static int8_t set_fifo_conf(const struct bma400_fifo_conf *fifo_conf, const struct bma400_dev *dev);
  351. /*!
  352. * @brief This API is used to get the FIFO configurations
  353. *
  354. * @param[in,out] fifo_conf : Structure instance containing the FIFO
  355. * configuration set in the sensor
  356. * @param[in] dev : Structure instance of bma400_dev
  357. *
  358. * @return Result of API execution status
  359. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  360. */
  361. static int8_t get_fifo_conf(struct bma400_fifo_conf *fifo_conf, const struct bma400_dev *dev);
  362. /*!
  363. * @brief This API is used to get the number of bytes filled in FIFO
  364. *
  365. * @param[in,out] fifo_byte_cnt : Number of bytes in the FIFO buffer
  366. * actually filled by the sensor
  367. * @param[in] dev : Structure instance of bma400_dev
  368. *
  369. * @return Result of API execution status
  370. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  371. */
  372. static int8_t get_fifo_length(uint16_t *fifo_byte_cnt, const struct bma400_dev *dev);
  373. /*!
  374. * @brief This API is used to read the FIFO of BMA400
  375. *
  376. * @param[in,out] fifo : Pointer to the fifo structure.
  377. *
  378. * @param[in] dev : Structure instance of bma400_dev
  379. *
  380. * @return Result of API execution status
  381. * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
  382. */
  383. static int8_t read_fifo(struct bma400_fifo_data *fifo, const struct bma400_dev *dev);
  384. /*!
  385. * @brief This API is used to unpack the accelerometer frames from the FIFO
  386. *
  387. * @param[in,out] fifo : Pointer to the fifo structure.
  388. * @param[in,out] accel_data : Structure instance to store the accel data
  389. * @param[in,out] frame_count : Number of frames requested by user as input
  390. * Number of frames actually parsed as output
  391. * @param[in] dev : Structure instance of bma400_dev
  392. *
  393. * @return Nothing
  394. */
  395. static void unpack_accel_frame(struct bma400_fifo_data *fifo,
  396. struct bma400_sensor_data *accel_data,
  397. uint16_t *frame_count,
  398. const struct bma400_dev *dev);
  399. /*!
  400. * @brief This API is used to check for a frame availability in FIFO
  401. *
  402. * @param[in,out] fifo : Pointer to the fifo structure.
  403. * @param[in,out] frame_available : Variable to denote availability of a frame
  404. * @param[in] accel_width : Variable to denote 12/8 bit accel data
  405. * @param[in] data_en : Data enabled in FIFO
  406. * @param[in,out] data_index : Index of the currently parsed FIFO data
  407. *
  408. * @return Nothing
  409. */
  410. static void check_frame_available(struct bma400_fifo_data *fifo,
  411. uint8_t *frame_available,
  412. uint8_t accel_width,
  413. uint8_t data_en,
  414. uint16_t *data_index);
  415. /*!
  416. * @brief This API is used to unpack the accelerometer xyz data from the FIFO
  417. * and store it in the user defined buffer
  418. *
  419. * @param[in,out] fifo : Pointer to the fifo structure.
  420. * @param[in,out] accel_data : Structure instance to store the accel data
  421. * @param[in,out] data_index : Index of the currently parsed FIFO data
  422. * @param[in] accel_width : Variable to denote 12/8 bit accel data
  423. * @param[in] frame_header : Variable to get the data enabled
  424. *
  425. * @return Nothing
  426. */
  427. static void unpack_accel(struct bma400_fifo_data *fifo,
  428. struct bma400_sensor_data *accel_data,
  429. uint16_t *data_index,
  430. uint8_t accel_width,
  431. uint8_t frame_header);
  432. /*!
  433. * @brief This API is used to parse and store the sensor time from the
  434. * FIFO data in the structure instance dev
  435. *
  436. * @param[in,out] fifo : Pointer to the fifo structure.
  437. * @param[in,out] data_index : Index of the FIFO data which has sensor time
  438. *
  439. * @return Nothing
  440. */
  441. static void unpack_sensortime_frame(struct bma400_fifo_data *fifo, uint16_t *data_index);
  442. /*!
  443. * @brief This API validates the self test results
  444. *
  445. * @param[in] accel_pos : Structure pointer to store accel data
  446. * for positive excitation
  447. * @param[in] accel_neg : Structure pointer to store accel data
  448. * for negative excitation
  449. *
  450. * @return Result of API execution status
  451. * @retval zero -> Success / -ve value -> Error / +ve value -> Self test fail
  452. */
  453. static int8_t validate_accel_self_test(const struct bma400_sensor_data *accel_pos,
  454. const struct bma400_sensor_data *accel_neg);
  455. /*!
  456. * @brief This API performs self test with positive excitation
  457. *
  458. * @param[in] accel_pos : Structure pointer to store accel data
  459. * for positive excitation
  460. * @param[in] dev : structure instance of bma400_dev
  461. *
  462. * @return Result of API execution status
  463. * @retval zero -> Success / -ve value -> Error
  464. */
  465. static int8_t positive_excited_accel(struct bma400_sensor_data *accel_pos, const struct bma400_dev *dev);
  466. /*!
  467. * @brief This API performs self test with negative excitation
  468. *
  469. * @param[in] accel_neg : Structure pointer to store accel data
  470. * for negative excitation
  471. * @param[in] dev : structure instance of bma400_dev
  472. *
  473. * @return Result of API execution status
  474. * @retval zero -> Success / -ve value -> Error
  475. */
  476. static int8_t negative_excited_accel(struct bma400_sensor_data *accel_neg, const struct bma400_dev *dev);
  477. /*!
  478. * @brief This API performs the pre-requisites needed to perform the self test
  479. *
  480. * @param[in] dev : structure instance of bma400_dev
  481. *
  482. * @return Result of API execution status
  483. * @retval zero -> Success / -ve value -> Error
  484. */
  485. static int8_t enable_self_test(const struct bma400_dev *dev);
  486. /********************** Global function definitions ************************/
  487. /*!
  488. * @brief This API is the entry point, Call this API before using other APIs.
  489. * This API reads the chip-id of the sensor which is the first step to
  490. * verify the sensor and updates the trim parameters of the sensor.
  491. */
  492. int8_t bma400_init(struct bma400_dev *dev)
  493. {
  494. int8_t rslt;
  495. uint8_t chip_id = 0;
  496. /* Check for null pointer in the device structure*/
  497. rslt = null_ptr_check(dev);
  498. /* Proceed if null check is fine */
  499. if (rslt == BMA400_OK) {
  500. /* Initial power-up time */
  501. dev->delay_ms(5);
  502. /* Assigning dummy byte value */
  503. if (dev->intf == BMA400_SPI_INTF) {
  504. /* Dummy Byte availability */
  505. dev->dummy_byte = 1;
  506. /* Dummy read of Chip-ID in SPI mode */
  507. rslt = bma400_get_regs(BMA400_CHIP_ID_ADDR, &chip_id, 1, dev);
  508. } else {
  509. dev->dummy_byte = 0;
  510. }
  511. if (rslt == BMA400_OK) {
  512. /* Chip ID of the sensor is read */
  513. rslt = bma400_get_regs(BMA400_CHIP_ID_ADDR, &chip_id, 1, dev);
  514. /* Proceed if everything is fine until now */
  515. if (rslt == BMA400_OK) {
  516. /* Check for chip id validity */
  517. if (chip_id == BMA400_CHIP_ID) {
  518. /* Store the chip ID in dev structure */
  519. dev->chip_id = chip_id;
  520. } else {
  521. rslt = BMA400_E_DEV_NOT_FOUND;
  522. }
  523. }
  524. }
  525. }
  526. return rslt;
  527. }
  528. /*!
  529. * @brief This API writes the given data to the register address
  530. * of the sensor.
  531. */
  532. int8_t bma400_set_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, const struct bma400_dev *dev)
  533. {
  534. int8_t rslt;
  535. uint8_t count;
  536. /* Check for null pointer in the device structure */
  537. rslt = null_ptr_check(dev);
  538. /* Proceed if null check is fine */
  539. if ((rslt == BMA400_OK) && (reg_data != NULL)) {
  540. /* Write the data to the reg_addr */
  541. /* SPI write requires to set The MSB of reg_addr as 0
  542. but in default the MSB is always 0 */
  543. if (len == 1) {
  544. rslt = dev->write(dev->intf_ptr, dev->dev_id, reg_addr, reg_data, len);
  545. if (rslt != BMA400_OK) {
  546. /* Failure case */
  547. rslt = BMA400_E_COM_FAIL;
  548. }
  549. }
  550. /* Burst write is not allowed thus we split burst case write
  551. * into single byte writes Thus user can write multiple bytes
  552. * with ease */
  553. if (len > 1) {
  554. for (count = 0; count < len; count++) {
  555. rslt = dev->write(dev->intf_ptr, dev->dev_id, reg_addr, &reg_data[count], 1);
  556. reg_addr++;
  557. }
  558. }
  559. } else {
  560. rslt = BMA400_E_NULL_PTR;
  561. }
  562. return rslt;
  563. }
  564. /*!
  565. * @brief This API reads the data from the given register address of the sensor.
  566. */
  567. int8_t bma400_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, const struct bma400_dev *dev)
  568. {
  569. int8_t rslt;
  570. uint16_t index;
  571. uint16_t temp_len = len + dev->dummy_byte;
  572. uint8_t temp_buff[temp_len];
  573. /* Check for null pointer in the device structure */
  574. rslt = null_ptr_check(dev);
  575. /* Proceed if null check is fine */
  576. if ((rslt == BMA400_OK) && (reg_data != NULL)) {
  577. if (dev->intf != BMA400_I2C_INTF) {
  578. /* If interface selected is SPI */
  579. reg_addr = reg_addr | BMA400_SPI_RD_MASK;
  580. }
  581. /* Read the data from the reg_addr */
  582. rslt = dev->read(dev->intf_ptr, dev->dev_id, reg_addr, temp_buff, temp_len);
  583. if (rslt == BMA400_OK) {
  584. for (index = 0; index < len; index++) {
  585. /* Parse the data read and store in "reg_data"
  586. * buffer so that the dummy byte is removed
  587. * and user will get only valid data
  588. */
  589. reg_data[index] = temp_buff[index + dev->dummy_byte];
  590. }
  591. }
  592. if (rslt != BMA400_OK) {
  593. /* Failure case */
  594. rslt = BMA400_E_COM_FAIL;
  595. }
  596. } else {
  597. rslt = BMA400_E_NULL_PTR;
  598. }
  599. return rslt;
  600. }
  601. /*!
  602. * @brief This API is used to perform soft-reset of the sensor
  603. * where all the registers are reset to their default values.
  604. */
  605. int8_t bma400_soft_reset(const struct bma400_dev *dev)
  606. {
  607. int8_t rslt;
  608. uint8_t data = BMA400_SOFT_RESET_CMD;
  609. /* Null-pointer check */
  610. rslt = null_ptr_check(dev);
  611. if (rslt == BMA400_OK) {
  612. /* Reset the device */
  613. rslt = bma400_set_regs(BMA400_COMMAND_REG_ADDR, &data, 1, dev);
  614. dev->delay_ms(BMA400_SOFT_RESET_DELAY_MS);
  615. if ((rslt == BMA400_OK) && (dev->intf == BMA400_SPI_INTF)) {
  616. /* Dummy read of 0x7F register to enable SPI Interface
  617. * if SPI is used
  618. */
  619. rslt = bma400_get_regs(0x7F, &data, 1, dev);
  620. }
  621. }
  622. return rslt;
  623. }
  624. /*!
  625. * @brief This API is used to set the power mode of the sensor.
  626. */
  627. int8_t bma400_set_power_mode(uint8_t power_mode, const struct bma400_dev *dev)
  628. {
  629. int8_t rslt;
  630. uint8_t reg_data = 0;
  631. rslt = null_ptr_check(dev);
  632. if (rslt == BMA400_OK) {
  633. rslt = bma400_get_regs(BMA400_ACCEL_CONFIG_0_ADDR, &reg_data, 1, dev);
  634. }
  635. if (rslt == BMA400_OK) {
  636. reg_data = BMA400_SET_BITS_POS_0(reg_data, BMA400_POWER_MODE, power_mode);
  637. /* Set the power mode of sensor */
  638. rslt = bma400_set_regs(BMA400_ACCEL_CONFIG_0_ADDR, &reg_data, 1, dev);
  639. if (power_mode == BMA400_LOW_POWER_MODE) {
  640. /* A delay of 1/ODR is required to switch power modes
  641. * Low power mode has 25Hz frequency and hence it needs
  642. * 40ms delay to enter low power mode */
  643. dev->delay_ms(40);
  644. } else {
  645. dev->delay_ms(10); /* TBC */
  646. }
  647. }
  648. return rslt;
  649. }
  650. /*!
  651. * @brief This API is used to get the power mode of the sensor.
  652. */
  653. int8_t bma400_get_power_mode(uint8_t *power_mode, const struct bma400_dev *dev)
  654. {
  655. int8_t rslt;
  656. uint8_t reg_data;
  657. /* Check for null pointer in the device structure*/
  658. rslt = null_ptr_check(dev);
  659. /* Proceed if null check is fine */
  660. if (rslt == BMA400_OK) {
  661. rslt = bma400_get_regs(BMA400_STATUS_ADDR, &reg_data, 1, dev);
  662. *power_mode = BMA400_GET_BITS(reg_data, BMA400_POWER_MODE_STATUS);
  663. }
  664. return rslt;
  665. }
  666. /*!
  667. * @brief This API is used to get the accel data along with the sensor-time
  668. */
  669. int8_t bma400_get_accel_data(uint8_t data_sel, struct bma400_sensor_data *accel, const struct bma400_dev *dev)
  670. {
  671. int8_t rslt;
  672. /* Check for null pointer in the device structure*/
  673. rslt = null_ptr_check(dev);
  674. /* Proceed if null check is fine */
  675. if ((rslt == BMA400_OK) || (accel != NULL)) {
  676. /* Read and store the accel data */
  677. rslt = get_accel_data(data_sel, accel, dev);
  678. } else {
  679. rslt = BMA400_E_NULL_PTR;
  680. }
  681. return rslt;
  682. }
  683. /*!
  684. * @brief This API is used to set the sensor settings like sensor
  685. * configurations and interrupt configurations
  686. */
  687. int8_t bma400_set_sensor_conf(const struct bma400_sensor_conf *conf, uint16_t n_sett, const struct bma400_dev *dev)
  688. {
  689. int8_t rslt;
  690. uint16_t idx = 0;
  691. uint8_t data_array[3] = { 0 };
  692. /* Check for null pointer in the device structure*/
  693. rslt = null_ptr_check(dev);
  694. /* Proceed if null check is fine */
  695. if (rslt == BMA400_OK) {
  696. /* Read the interrupt pin mapping configurations */
  697. rslt = bma400_get_regs(BMA400_INT_MAP_ADDR, data_array, 3, dev);
  698. if (rslt == BMA400_OK) {
  699. for (idx = 0; idx < n_sett; idx++) {
  700. switch (conf[idx].type) {
  701. case BMA400_ACCEL:
  702. /* Setting Accel configurations */
  703. rslt = set_accel_conf(&conf[idx].param.accel, dev);
  704. if (rslt == BMA400_OK) {
  705. /* Int pin mapping settings */
  706. map_int_pin(data_array, BMA400_DATA_READY_INT_MAP,
  707. conf[idx].param.accel.int_chan);
  708. }
  709. break;
  710. case BMA400_TAP_INT:
  711. /* Setting TAP configurations */
  712. rslt = set_tap_conf(&conf[idx].param.tap, dev);
  713. if (rslt == BMA400_OK) {
  714. /* Int pin mapping settings */
  715. map_int_pin(data_array, BMA400_TAP_INT_MAP,
  716. conf[idx].param.tap.int_chan);
  717. }
  718. break;
  719. case BMA400_ACTIVITY_CHANGE_INT:
  720. /* Setting activity change config */
  721. rslt = set_activity_change_conf(&conf[idx].param.act_ch, dev);
  722. if (rslt == BMA400_OK) {
  723. /* Int pin mapping settings */
  724. map_int_pin(data_array, BMA400_ACT_CH_INT_MAP,
  725. conf[idx].param.act_ch.int_chan);
  726. }
  727. break;
  728. case BMA400_GEN1_INT:
  729. /* Setting Generic int 1 config */
  730. rslt = set_gen1_int(&conf[idx].param.gen_int, dev);
  731. if (rslt == BMA400_OK) {
  732. /* Int pin mapping settings */
  733. map_int_pin(data_array, BMA400_GEN1_INT_MAP,
  734. conf[idx].param.gen_int.int_chan);
  735. }
  736. break;
  737. case BMA400_GEN2_INT:
  738. /* Setting Generic int 2 config */
  739. rslt = set_gen2_int(&conf[idx].param.gen_int, dev);
  740. if (rslt == BMA400_OK) {
  741. /* Int pin mapping settings */
  742. map_int_pin(data_array, BMA400_GEN2_INT_MAP,
  743. conf[idx].param.gen_int.int_chan);
  744. }
  745. break;
  746. case BMA400_ORIENT_CHANGE_INT:
  747. /* Setting orient int config */
  748. rslt = set_orient_int(&conf[idx].param.orient, dev);
  749. if (rslt == BMA400_OK) {
  750. /* Int pin mapping settings */
  751. map_int_pin(data_array, BMA400_ORIENT_CH_INT_MAP,
  752. conf[idx].param.orient.int_chan);
  753. }
  754. break;
  755. case BMA400_STEP_COUNTER_INT:
  756. /* Int pin mapping settings */
  757. map_int_pin(data_array, BMA400_STEP_INT_MAP, conf[idx].param.step_cnt.int_chan);
  758. break;
  759. }
  760. }
  761. if (rslt == BMA400_OK) {
  762. /* Set the interrupt pin mapping configurations */
  763. rslt = bma400_set_regs(BMA400_INT_MAP_ADDR, data_array, 3, dev);
  764. }
  765. }
  766. }
  767. return rslt;
  768. }
  769. /*!
  770. * @brief This API is used to get the sensor settings like sensor
  771. * configurations and interrupt configurations and store
  772. * them in the corresponding structure instance
  773. */
  774. int8_t bma400_get_sensor_conf(struct bma400_sensor_conf *conf, uint16_t n_sett, const struct bma400_dev *dev)
  775. {
  776. int8_t rslt = BMA400_OK;
  777. uint16_t idx = 0;
  778. uint8_t data_array[3] = { 0 };
  779. if (conf == NULL) {
  780. rslt = BMA400_E_NULL_PTR;
  781. }
  782. if (rslt == BMA400_OK) {
  783. /* Read the interrupt pin mapping configurations */
  784. rslt = bma400_get_regs(BMA400_INT_MAP_ADDR, data_array, 3, dev);
  785. }
  786. for (idx = 0; (idx < n_sett) && (rslt == BMA400_OK); idx++) {
  787. switch (conf[idx].type) {
  788. case BMA400_ACCEL:
  789. /* Accel configuration settings */
  790. rslt = get_accel_conf(&conf[idx].param.accel, dev);
  791. if (rslt == BMA400_OK) {
  792. /* Get the INT pin mapping */
  793. get_int_pin_map(data_array, BMA400_DATA_READY_INT_MAP, &conf[idx].param.accel.int_chan);
  794. }
  795. break;
  796. case BMA400_TAP_INT:
  797. /* TAP configuration settings */
  798. rslt = get_tap_conf(&conf[idx].param.tap, dev);
  799. if (rslt == BMA400_OK) {
  800. /* Get the INT pin mapping */
  801. get_int_pin_map(data_array, BMA400_TAP_INT_MAP, &conf[idx].param.tap.int_chan);
  802. }
  803. break;
  804. case BMA400_ACTIVITY_CHANGE_INT:
  805. /* Activity change configurations */
  806. rslt = get_activity_change_conf(&conf[idx].param.act_ch, dev);
  807. if (rslt == BMA400_OK) {
  808. /* Get the INT pin mapping */
  809. get_int_pin_map(data_array, BMA400_ACT_CH_INT_MAP, &conf[idx].param.act_ch.int_chan);
  810. }
  811. break;
  812. case BMA400_GEN1_INT:
  813. /* Generic int1 configurations */
  814. rslt = get_gen1_int(&conf[idx].param.gen_int, dev);
  815. if (rslt == BMA400_OK) {
  816. /* Get the INT pin mapping */
  817. get_int_pin_map(data_array, BMA400_GEN1_INT_MAP, &conf[idx].param.gen_int.int_chan);
  818. }
  819. break;
  820. case BMA400_GEN2_INT:
  821. /* Generic int2 configurations */
  822. rslt = get_gen2_int(&conf[idx].param.gen_int, dev);
  823. if (rslt == BMA400_OK) {
  824. /* Get the INT pin mapping */
  825. get_int_pin_map(data_array, BMA400_GEN2_INT_MAP, &conf[idx].param.gen_int.int_chan);
  826. }
  827. break;
  828. case BMA400_ORIENT_CHANGE_INT:
  829. /* Orient int configurations */
  830. rslt = get_orient_int(&conf[idx].param.orient, dev);
  831. if (rslt == BMA400_OK) {
  832. /* Get the INT pin mapping */
  833. get_int_pin_map(data_array, BMA400_ORIENT_CH_INT_MAP, &conf[idx].param.orient.int_chan);
  834. }
  835. break;
  836. case BMA400_STEP_COUNTER_INT:
  837. /* Get int pin mapping settings */
  838. get_int_pin_map(data_array, BMA400_STEP_INT_MAP, &conf[idx].param.step_cnt.int_chan);
  839. break;
  840. default:
  841. rslt = BMA400_E_INVALID_CONFIG;
  842. }
  843. }
  844. return rslt;
  845. }
  846. /*!
  847. * @brief This API is used to set the device specific settings
  848. */
  849. int8_t bma400_set_device_conf(const struct bma400_device_conf *conf, uint8_t n_sett, const struct bma400_dev *dev)
  850. {
  851. int8_t rslt = BMA400_OK;
  852. uint16_t idx = 0;
  853. uint8_t data_array[3] = { 0 };
  854. if (conf == NULL) {
  855. rslt = BMA400_E_NULL_PTR;
  856. }
  857. if (rslt == BMA400_OK) {
  858. /* Read the interrupt pin mapping configurations */
  859. rslt = bma400_get_regs(BMA400_INT_MAP_ADDR, data_array, 3, dev);
  860. }
  861. for (idx = 0; (idx < n_sett) && (rslt == BMA400_OK); idx++) {
  862. switch (conf[idx].type) {
  863. case BMA400_AUTOWAKEUP_TIMEOUT:
  864. rslt = set_autowakeup_timeout(&conf[idx].param.auto_wakeup, dev);
  865. break;
  866. case BMA400_AUTOWAKEUP_INT:
  867. rslt = set_autowakeup_interrupt(&conf[idx].param.wakeup, dev);
  868. if (rslt == BMA400_OK) {
  869. /* Interrupt pin mapping */
  870. map_int_pin(data_array, BMA400_WAKEUP_INT_MAP, conf[idx].param.wakeup.int_chan);
  871. }
  872. break;
  873. case BMA400_AUTO_LOW_POWER:
  874. rslt = set_auto_low_power(&conf[idx].param.auto_lp, dev);
  875. break;
  876. case BMA400_INT_PIN_CONF:
  877. rslt = set_int_pin_conf(conf[idx].param.int_conf, dev);
  878. break;
  879. case BMA400_INT_OVERRUN_CONF:
  880. /* Interrupt pin mapping */
  881. map_int_pin(data_array, BMA400_INT_OVERRUN_MAP, conf[idx].param.overrun_int.int_chan);
  882. break;
  883. case BMA400_FIFO_CONF:
  884. rslt = set_fifo_conf(&conf[idx].param.fifo_conf, dev);
  885. if (rslt == BMA400_OK) {
  886. /* Interrupt pin mapping */
  887. map_int_pin(data_array, BMA400_FIFO_WM_INT_MAP,
  888. conf[idx].param.fifo_conf.fifo_wm_channel);
  889. map_int_pin(data_array, BMA400_FIFO_FULL_INT_MAP,
  890. conf[idx].param.fifo_conf.fifo_full_channel);
  891. }
  892. break;
  893. default:
  894. rslt = BMA400_E_INVALID_CONFIG;
  895. }
  896. }
  897. if (rslt == BMA400_OK) {
  898. /* Set the interrupt pin mapping configurations */
  899. rslt = bma400_set_regs(BMA400_INT_MAP_ADDR, data_array, 3, dev);
  900. }
  901. return rslt;
  902. }
  903. /*!
  904. * @brief This API is used to get the device specific settings and store
  905. * them in the corresponding structure instance
  906. */
  907. int8_t bma400_get_device_conf(struct bma400_device_conf *conf, uint8_t n_sett, const struct bma400_dev *dev)
  908. {
  909. int8_t rslt;
  910. uint16_t idx = 0;
  911. uint8_t data_array[3] = { 0 };
  912. /* Check for null pointer in the device structure*/
  913. rslt = null_ptr_check(dev);
  914. /* Proceed if null check is fine */
  915. if (rslt == BMA400_OK) {
  916. /* Read the interrupt pin mapping configurations */
  917. rslt = bma400_get_regs(BMA400_INT_MAP_ADDR, data_array, 3, dev);
  918. if (rslt == BMA400_OK) {
  919. for (idx = 0; idx < n_sett; idx++) {
  920. switch (conf[idx].type) {
  921. case BMA400_AUTOWAKEUP_TIMEOUT:
  922. rslt = get_autowakeup_timeout(&conf[idx].param.auto_wakeup, dev);
  923. break;
  924. case BMA400_AUTOWAKEUP_INT:
  925. rslt = get_autowakeup_interrupt(&conf[idx].param.wakeup, dev);
  926. if (rslt == BMA400_OK) {
  927. /* Get the INT pin mapping */
  928. get_int_pin_map(data_array, BMA400_WAKEUP_INT_MAP,
  929. &conf[idx].param.wakeup.int_chan);
  930. }
  931. break;
  932. case BMA400_AUTO_LOW_POWER:
  933. rslt = get_auto_low_power(&conf[idx].param.auto_lp, dev);
  934. break;
  935. case BMA400_INT_PIN_CONF:
  936. rslt = get_int_pin_conf(&conf[idx].param.int_conf, dev);
  937. break;
  938. case BMA400_INT_OVERRUN_CONF:
  939. get_int_pin_map(data_array, BMA400_INT_OVERRUN_MAP,
  940. &conf[idx].param.overrun_int.int_chan);
  941. break;
  942. case BMA400_FIFO_CONF:
  943. rslt = get_fifo_conf(&conf[idx].param.fifo_conf, dev);
  944. if (rslt == BMA400_OK) {
  945. get_int_pin_map(data_array,
  946. BMA400_FIFO_FULL_INT_MAP,
  947. &conf[idx].param.fifo_conf.fifo_full_channel);
  948. get_int_pin_map(data_array, BMA400_FIFO_WM_INT_MAP,
  949. &conf[idx].param.fifo_conf.fifo_wm_channel);
  950. }
  951. break;
  952. }
  953. }
  954. }
  955. }
  956. return rslt;
  957. }
  958. /*!
  959. * @brief This API is used to get the status of all the interrupts
  960. * whether they are asserted or not
  961. */
  962. int8_t bma400_get_interrupt_status(uint16_t *int_status, const struct bma400_dev *dev)
  963. {
  964. int8_t rslt;
  965. uint8_t reg_data[3];
  966. /* Check for null pointer in the device structure*/
  967. rslt = null_ptr_check(dev);
  968. /* Proceed if null check is fine */
  969. if (rslt == BMA400_OK) {
  970. /* Read the interrupt status registers */
  971. rslt = bma400_get_regs(BMA400_INT_STAT0_ADDR, reg_data, 3, dev);
  972. reg_data[1] = BMA400_SET_BITS(reg_data[1], BMA400_INT_STATUS, reg_data[2]);
  973. /* Concatenate the interrupt status to the output */
  974. *int_status = ((uint16_t)reg_data[1] << 8) | reg_data[0];
  975. }
  976. return rslt;
  977. }
  978. /*!
  979. * @brief This API is used to set the step counter's configuration
  980. * parameters from the registers 0x59 to 0x71
  981. */
  982. int8_t bma400_set_step_counter_param(uint8_t *sccr_conf, const struct bma400_dev *dev)
  983. {
  984. int8_t rslt;
  985. /* Check for null pointer in the device structure*/
  986. rslt = null_ptr_check(dev);
  987. /* Proceed if null check is fine */
  988. if (rslt == BMA400_OK) {
  989. /* Set the step counter parameters in the sensor */
  990. rslt = bma400_set_regs(0x59, sccr_conf, 25, dev);
  991. }
  992. return rslt;
  993. }
  994. /*!
  995. * @brief This API is used to get the step counter output in form
  996. * of number of steps in the step_count value
  997. */
  998. int8_t bma400_get_steps_counted(uint32_t *step_count, uint8_t *activity_data, const struct bma400_dev *dev)
  999. {
  1000. int8_t rslt;
  1001. uint8_t data_arrray[4];
  1002. /* Check for null pointer in the device structure*/
  1003. rslt = null_ptr_check(dev);
  1004. /* Proceed if null check is fine */
  1005. if (rslt == BMA400_OK) {
  1006. rslt = bma400_get_regs(BMA400_STEP_CNT_0_ADDR, data_arrray, 4, dev);
  1007. *step_count = ((uint32_t)data_arrray[2] << 16) | ((uint16_t)data_arrray[1] << 8) | data_arrray[0];
  1008. *activity_data = data_arrray[3];
  1009. }
  1010. return rslt;
  1011. }
  1012. /*!
  1013. * @brief This API is used to get the temperature data output
  1014. */
  1015. int8_t bma400_get_temperature_data(int16_t *temperature_data, const struct bma400_dev *dev)
  1016. {
  1017. int8_t rslt;
  1018. uint8_t reg_data;
  1019. /* Check for null pointer in the device structure*/
  1020. rslt = null_ptr_check(dev);
  1021. /* Proceed if null check is fine */
  1022. if (rslt == BMA400_OK) {
  1023. rslt = bma400_get_regs(BMA400_TEMP_DATA_ADDR, &reg_data, 1, dev);
  1024. /* Temperature data calculations */
  1025. *temperature_data = (((int16_t)((int8_t)reg_data)) - 2) * 5 + 250;
  1026. }
  1027. return rslt;
  1028. }
  1029. /*!
  1030. * @brief This API is used to get the enable/disable
  1031. * status of the various interrupts
  1032. */
  1033. int8_t bma400_get_interrupts_enabled(struct bma400_int_enable *int_select, uint8_t n_sett, const struct bma400_dev *dev)
  1034. {
  1035. int8_t rslt;
  1036. uint8_t idx = 0;
  1037. uint8_t reg_data[2];
  1038. uint8_t wkup_int;
  1039. /* Check for null pointer in the device structure */
  1040. rslt = null_ptr_check(dev);
  1041. /* Proceed if null check is fine */
  1042. if (rslt == BMA400_OK) {
  1043. rslt = bma400_get_regs(BMA400_INT_CONF_0_ADDR, reg_data, 2, dev);
  1044. if (rslt == BMA400_OK) {
  1045. for (idx = 0; idx < n_sett; idx++) {
  1046. /* Read the enable/disable of interrupts
  1047. * based on user selection */
  1048. switch (int_select[idx].type) {
  1049. case BMA400_DRDY_INT_EN:
  1050. int_select[idx].conf = BMA400_GET_BITS(reg_data[0], BMA400_EN_DRDY);
  1051. break;
  1052. case BMA400_FIFO_WM_INT_EN:
  1053. int_select[idx].conf = BMA400_GET_BITS(reg_data[0], BMA400_EN_FIFO_WM);
  1054. break;
  1055. case BMA400_FIFO_FULL_INT_EN:
  1056. int_select[idx].conf = BMA400_GET_BITS(reg_data[0], BMA400_EN_FIFO_FULL);
  1057. break;
  1058. case BMA400_GEN2_INT_EN:
  1059. int_select[idx].conf = BMA400_GET_BITS(reg_data[0], BMA400_EN_GEN2);
  1060. break;
  1061. case BMA400_GEN1_INT_EN:
  1062. int_select[idx].conf = BMA400_GET_BITS(reg_data[0], BMA400_EN_GEN1);
  1063. break;
  1064. case BMA400_ORIENT_CHANGE_INT_EN:
  1065. int_select[idx].conf = BMA400_GET_BITS(reg_data[0], BMA400_EN_ORIENT_CH);
  1066. break;
  1067. case BMA400_LATCH_INT_EN:
  1068. int_select[idx].conf = BMA400_GET_BITS(reg_data[1], BMA400_EN_LATCH);
  1069. break;
  1070. case BMA400_ACTIVITY_CHANGE_INT_EN:
  1071. int_select[idx].conf = BMA400_GET_BITS(reg_data[1], BMA400_EN_ACTCH);
  1072. break;
  1073. case BMA400_DOUBLE_TAP_INT_EN:
  1074. int_select[idx].conf = BMA400_GET_BITS(reg_data[1], BMA400_EN_D_TAP);
  1075. break;
  1076. case BMA400_SINGLE_TAP_INT_EN:
  1077. int_select[idx].conf = BMA400_GET_BITS(reg_data[1], BMA400_EN_S_TAP);
  1078. break;
  1079. case BMA400_STEP_COUNTER_INT_EN:
  1080. int_select[idx].conf = BMA400_GET_BITS_POS_0(reg_data[1], BMA400_EN_STEP_INT);
  1081. break;
  1082. case BMA400_AUTO_WAKEUP_EN:
  1083. rslt = bma400_get_regs(BMA400_AUTOWAKEUP_1_ADDR, &wkup_int, 1, dev);
  1084. if (rslt == BMA400_OK) {
  1085. /* Auto-Wakeup int status */
  1086. int_select[idx].conf = BMA400_GET_BITS(wkup_int,
  1087. BMA400_WAKEUP_INTERRUPT);
  1088. }
  1089. break;
  1090. default:
  1091. rslt = BMA400_E_INVALID_CONFIG;
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. }
  1097. return rslt;
  1098. }
  1099. /*!
  1100. * @brief This API is used to enable the various interrupts
  1101. */
  1102. int8_t bma400_enable_interrupt(const struct bma400_int_enable *int_select, uint8_t n_sett, const struct bma400_dev *dev)
  1103. {
  1104. int8_t rslt;
  1105. uint8_t conf, idx = 0;
  1106. uint8_t reg_data[2];
  1107. /* Check for null pointer in the device structure */
  1108. rslt = null_ptr_check(dev);
  1109. /* Proceed if null check is fine */
  1110. if (rslt == BMA400_OK) {
  1111. rslt = bma400_get_regs(BMA400_INT_CONF_0_ADDR, reg_data, 2, dev);
  1112. if (rslt == BMA400_OK) {
  1113. for (idx = 0; idx < n_sett; idx++) {
  1114. conf = int_select[idx].conf;
  1115. /* Enable the interrupt based on user selection */
  1116. switch (int_select[idx].type) {
  1117. case BMA400_DRDY_INT_EN:
  1118. reg_data[0] = BMA400_SET_BITS(reg_data[0], BMA400_EN_DRDY, conf);
  1119. break;
  1120. case BMA400_FIFO_WM_INT_EN:
  1121. reg_data[0] = BMA400_SET_BITS(reg_data[0], BMA400_EN_FIFO_WM, conf);
  1122. break;
  1123. case BMA400_FIFO_FULL_INT_EN:
  1124. reg_data[0] = BMA400_SET_BITS(reg_data[0], BMA400_EN_FIFO_FULL, conf);
  1125. break;
  1126. case BMA400_GEN2_INT_EN:
  1127. reg_data[0] = BMA400_SET_BITS(reg_data[0], BMA400_EN_GEN2, conf);
  1128. break;
  1129. case BMA400_GEN1_INT_EN:
  1130. reg_data[0] = BMA400_SET_BITS(reg_data[0], BMA400_EN_GEN1, conf);
  1131. break;
  1132. case BMA400_ORIENT_CHANGE_INT_EN:
  1133. reg_data[0] = BMA400_SET_BITS(reg_data[0], BMA400_EN_ORIENT_CH, conf);
  1134. break;
  1135. case BMA400_LATCH_INT_EN:
  1136. reg_data[1] = BMA400_SET_BITS(reg_data[1], BMA400_EN_LATCH, conf);
  1137. break;
  1138. case BMA400_ACTIVITY_CHANGE_INT_EN:
  1139. reg_data[1] = BMA400_SET_BITS(reg_data[1], BMA400_EN_ACTCH, conf);
  1140. break;
  1141. case BMA400_DOUBLE_TAP_INT_EN:
  1142. reg_data[1] = BMA400_SET_BITS(reg_data[1], BMA400_EN_D_TAP, conf);
  1143. break;
  1144. case BMA400_SINGLE_TAP_INT_EN:
  1145. reg_data[1] = BMA400_SET_BITS(reg_data[1], BMA400_EN_S_TAP, conf);
  1146. break;
  1147. case BMA400_STEP_COUNTER_INT_EN:
  1148. reg_data[1] = BMA400_SET_BITS_POS_0(reg_data[1], BMA400_EN_STEP_INT, conf);
  1149. break;
  1150. case BMA400_AUTO_WAKEUP_EN:
  1151. rslt = set_auto_wakeup(conf, dev);
  1152. break;
  1153. default:
  1154. rslt = BMA400_E_INVALID_CONFIG;
  1155. break;
  1156. }
  1157. }
  1158. if (rslt == BMA400_OK) {
  1159. /* Set the configurations in the sensor */
  1160. rslt = bma400_set_regs(BMA400_INT_CONF_0_ADDR, reg_data, 2, dev);
  1161. }
  1162. }
  1163. }
  1164. return rslt;
  1165. }
  1166. /*!
  1167. * @brief This API reads the FIFO data from the sensor
  1168. */
  1169. int8_t bma400_get_fifo_data(struct bma400_fifo_data *fifo, const struct bma400_dev *dev)
  1170. {
  1171. int8_t rslt;
  1172. uint8_t data;
  1173. uint16_t fifo_byte_cnt = 0;
  1174. uint16_t user_fifo_len = 0;
  1175. /* Check for null pointer in the device structure */
  1176. rslt = null_ptr_check(dev);
  1177. /* Proceed if null check is fine */
  1178. if (rslt == BMA400_OK) {
  1179. /* Resetting the FIFO data byte index */
  1180. fifo->accel_byte_start_idx = 0;
  1181. /* Reading the FIFO length */
  1182. rslt = get_fifo_length(&fifo_byte_cnt, dev);
  1183. if (rslt == BMA400_OK) {
  1184. /* Get the FIFO configurations
  1185. * from the sensor */
  1186. rslt = bma400_get_regs(BMA400_FIFO_CONFIG_0_ADDR, &data, 1, dev);
  1187. if (rslt == BMA400_OK) {
  1188. /* Get the data from FIFO_CONFIG0 register */
  1189. fifo->fifo_8_bit_en = BMA400_GET_BITS(data, BMA400_FIFO_8_BIT_EN);
  1190. fifo->fifo_data_enable = BMA400_GET_BITS(data, BMA400_FIFO_AXES_EN);
  1191. fifo->fifo_time_enable = BMA400_GET_BITS(data, BMA400_FIFO_TIME_EN);
  1192. fifo->fifo_sensor_time = 0;
  1193. user_fifo_len = fifo->length;
  1194. if (fifo->length > fifo_byte_cnt) {
  1195. /* Handling case where user requests
  1196. * more data than available in FIFO */
  1197. fifo->length = fifo_byte_cnt;
  1198. }
  1199. /* Reading extra bytes as per the macro
  1200. * "BMA400_FIFO_BYTES_OVERREAD"
  1201. * when FIFO time is enabled */
  1202. if ((fifo->fifo_time_enable == BMA400_ENABLE) &&
  1203. (fifo_byte_cnt + BMA400_FIFO_BYTES_OVERREAD <= user_fifo_len)) {
  1204. /* Handling sensor time availability*/
  1205. fifo->length = fifo->length + BMA400_FIFO_BYTES_OVERREAD;
  1206. }
  1207. /* Read the FIFO data */
  1208. rslt = read_fifo(fifo, dev);
  1209. }
  1210. }
  1211. }
  1212. return rslt;
  1213. }
  1214. /*!
  1215. * @brief This API parses and extracts the accelerometer frames, FIFO time
  1216. * and control frames from FIFO data read by the "bma400_get_fifo_data" API
  1217. * and stores it in the "accel_data" structure instance.
  1218. */
  1219. int8_t bma400_extract_accel(struct bma400_fifo_data *fifo,
  1220. struct bma400_sensor_data *accel_data,
  1221. uint16_t *frame_count,
  1222. const struct bma400_dev *dev)
  1223. {
  1224. int8_t rslt;
  1225. /* Check for null pointer in the device structure */
  1226. rslt = null_ptr_check(dev);
  1227. /* Proceed if null check is fine */
  1228. if (rslt == BMA400_OK) {
  1229. /* Parse the FIFO data */
  1230. unpack_accel_frame(fifo, accel_data, frame_count, dev);
  1231. }
  1232. return rslt;
  1233. }
  1234. /*!
  1235. * @brief This API writes fifo_flush command to command register.This
  1236. * action clears all data in the FIFO
  1237. */
  1238. int8_t bma400_set_fifo_flush(const struct bma400_dev *dev)
  1239. {
  1240. int8_t rslt;
  1241. uint8_t data = BMA400_FIFO_FLUSH_CMD;
  1242. /* Check for null pointer in the device structure */
  1243. rslt = null_ptr_check(dev);
  1244. /* Proceed if null check is fine */
  1245. if (rslt == BMA400_OK) {
  1246. /* FIFO flush command is set */
  1247. rslt = bma400_set_regs(BMA400_COMMAND_REG_ADDR, &data, 1, dev);
  1248. }
  1249. return rslt;
  1250. }
  1251. /*!
  1252. * @brief This is used to perform self test of accelerometer in BMA400
  1253. */
  1254. int8_t bma400_perform_self_test(const struct bma400_dev *dev)
  1255. {
  1256. int8_t rslt;
  1257. int8_t self_test_rslt = 0;
  1258. struct bma400_sensor_data accel_pos, accel_neg;
  1259. /* Check for null pointer in the device structure */
  1260. rslt = null_ptr_check(dev);
  1261. /* Proceed if null check is fine */
  1262. if (rslt == BMA400_OK) {
  1263. /* pre-requisites for self test*/
  1264. rslt = enable_self_test(dev);
  1265. if (rslt == BMA400_OK) {
  1266. rslt = positive_excited_accel(&accel_pos, dev);
  1267. if (rslt == BMA400_OK) {
  1268. rslt = negative_excited_accel(&accel_neg, dev);
  1269. if (rslt == BMA400_OK) {
  1270. /* Validate the self test result */
  1271. rslt = validate_accel_self_test(&accel_pos, &accel_neg);
  1272. }
  1273. }
  1274. }
  1275. }
  1276. /* Check to ensure bus error does not occur */
  1277. if (rslt >= BMA400_OK) {
  1278. /* Store the status of self test result */
  1279. self_test_rslt = rslt;
  1280. /* Perform soft reset */
  1281. rslt = bma400_soft_reset(dev);
  1282. }
  1283. /* Check to ensure bus operations are success */
  1284. if (rslt == BMA400_OK) {
  1285. /* Restore self_test_rslt as return value */
  1286. rslt = self_test_rslt;
  1287. }
  1288. return rslt;
  1289. }
  1290. /****************************************************************************/
  1291. /**\name INTERNAL APIs */
  1292. /*!
  1293. * @brief This internal API is used to validate the device structure pointer for
  1294. * null conditions.
  1295. */
  1296. static int8_t null_ptr_check(const struct bma400_dev *dev)
  1297. {
  1298. int8_t rslt;
  1299. if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL)) {
  1300. /* Device structure pointer is not valid */
  1301. rslt = BMA400_E_NULL_PTR;
  1302. } else {
  1303. /* Device structure is fine */
  1304. rslt = BMA400_OK;
  1305. }
  1306. return rslt;
  1307. }
  1308. /*!
  1309. * @brief This internal API is used to set the accel configurations in sensor
  1310. */
  1311. static int8_t set_accel_conf(const struct bma400_acc_conf *accel_conf, const struct bma400_dev *dev)
  1312. {
  1313. int8_t rslt;
  1314. uint8_t data_array[3] = { 0, 0, 0xE0 };
  1315. /* Update the accel configurations from the user structure
  1316. * accel_conf */
  1317. rslt = bma400_get_regs(BMA400_ACCEL_CONFIG_0_ADDR, data_array, 3, dev);
  1318. if (rslt == BMA400_OK) {
  1319. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_FILT_1_BW, accel_conf->filt1_bw);
  1320. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_OSR_LP, accel_conf->osr_lp);
  1321. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_ACCEL_RANGE, accel_conf->range);
  1322. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_OSR, accel_conf->osr);
  1323. data_array[1] = BMA400_SET_BITS_POS_0(data_array[1], BMA400_ACCEL_ODR, accel_conf->odr);
  1324. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_DATA_FILTER, accel_conf->data_src);
  1325. /* Set the accel configurations in the sensor */
  1326. rslt = bma400_set_regs(BMA400_ACCEL_CONFIG_0_ADDR, data_array, 3, dev);
  1327. }
  1328. return rslt;
  1329. }
  1330. /*!
  1331. * @brief This internal API is used to set the accel configurations in sensor
  1332. */
  1333. static int8_t get_accel_conf(struct bma400_acc_conf *accel_conf, const struct bma400_dev *dev)
  1334. {
  1335. int8_t rslt;
  1336. uint8_t data_array[3];
  1337. rslt = bma400_get_regs(BMA400_ACCEL_CONFIG_0_ADDR, data_array, 3, dev);
  1338. if (rslt == BMA400_OK) {
  1339. accel_conf->filt1_bw = BMA400_GET_BITS(data_array[0], BMA400_FILT_1_BW);
  1340. accel_conf->osr_lp = BMA400_GET_BITS(data_array[0], BMA400_OSR_LP);
  1341. accel_conf->range = BMA400_GET_BITS(data_array[1], BMA400_ACCEL_RANGE);
  1342. accel_conf->osr = BMA400_GET_BITS(data_array[1], BMA400_OSR);
  1343. accel_conf->odr = BMA400_GET_BITS_POS_0(data_array[1], BMA400_ACCEL_ODR);
  1344. accel_conf->data_src = BMA400_GET_BITS(data_array[2], BMA400_DATA_FILTER);
  1345. }
  1346. return rslt;
  1347. }
  1348. /*!
  1349. * @brief This API reads accel data along with sensor time
  1350. */
  1351. static int8_t get_accel_data(uint8_t data_sel, struct bma400_sensor_data *accel, const struct bma400_dev *dev)
  1352. {
  1353. int8_t rslt;
  1354. uint8_t data_array[9] = { 0 };
  1355. uint16_t lsb;
  1356. uint8_t msb;
  1357. uint8_t time_0;
  1358. uint16_t time_1;
  1359. uint32_t time_2;
  1360. if (data_sel == BMA400_DATA_ONLY) {
  1361. /* Read the sensor data registers only */
  1362. rslt = bma400_get_regs(BMA400_ACCEL_DATA_ADDR, data_array, 6, dev);
  1363. } else if (data_sel == BMA400_DATA_SENSOR_TIME) {
  1364. /* Read the sensor data along with sensor time */
  1365. rslt = bma400_get_regs(BMA400_ACCEL_DATA_ADDR, data_array, 9, dev);
  1366. } else {
  1367. /* Invalid use of "data_sel" */
  1368. rslt = BMA400_E_INVALID_CONFIG;
  1369. }
  1370. if (rslt == BMA400_OK) {
  1371. lsb = data_array[0];
  1372. msb = data_array[1];
  1373. /* accel X axis data */
  1374. accel->x = (int16_t)(((uint16_t)msb * 256) + lsb);
  1375. if (accel->x > 2047) {
  1376. /* Computing accel data negative value */
  1377. accel->x = accel->x - 4096;
  1378. }
  1379. lsb = data_array[2];
  1380. msb = data_array[3];
  1381. /* accel Y axis data */
  1382. accel->y = (int16_t)(((uint16_t)msb * 256) | lsb);
  1383. if (accel->y > 2047) {
  1384. /* Computing accel data negative value */
  1385. accel->y = accel->y - 4096;
  1386. }
  1387. lsb = data_array[4];
  1388. msb = data_array[5];
  1389. /* accel Z axis data */
  1390. accel->z = (int16_t)(((uint16_t)msb * 256) | lsb);
  1391. if (accel->z > 2047) {
  1392. /* Computing accel data negative value */
  1393. accel->z = accel->z - 4096;
  1394. }
  1395. if (data_sel == BMA400_DATA_ONLY) {
  1396. /* Update sensortime as 0 */
  1397. accel->sensortime = 0;
  1398. }
  1399. if (data_sel == BMA400_DATA_SENSOR_TIME) {
  1400. /* Sensor-time data*/
  1401. time_0 = data_array[6];
  1402. time_1 = ((uint16_t)data_array[7] << 8);
  1403. time_2 = ((uint32_t)data_array[8] << 16);
  1404. accel->sensortime = (uint32_t)(time_2 + time_1 + time_0);
  1405. }
  1406. }
  1407. return rslt;
  1408. }
  1409. /*!
  1410. * @brief This API enables the auto-wakeup feature
  1411. * of the sensor using a timeout value
  1412. */
  1413. static int8_t set_autowakeup_timeout(const struct bma400_auto_wakeup_conf *wakeup_conf, const struct bma400_dev *dev)
  1414. {
  1415. int8_t rslt;
  1416. uint8_t data_array[2];
  1417. uint8_t lsb;
  1418. uint8_t msb;
  1419. rslt = bma400_get_regs(BMA400_AUTOWAKEUP_1_ADDR, &data_array[1], 1, dev);
  1420. if (rslt == BMA400_OK) {
  1421. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_WAKEUP_TIMEOUT, wakeup_conf->wakeup_timeout);
  1422. /* LSB of timeout threshold */
  1423. lsb = BMA400_GET_BITS_POS_0(wakeup_conf->timeout_thres, BMA400_WAKEUP_THRES_LSB);
  1424. /* MSB of timeout threshold */
  1425. msb = BMA400_GET_BITS(wakeup_conf->timeout_thres, BMA400_WAKEUP_THRES_MSB);
  1426. /* Set the value in the data_array */
  1427. data_array[0] = msb;
  1428. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_WAKEUP_TIMEOUT_THRES, lsb);
  1429. rslt = bma400_set_regs(BMA400_AUTOWAKEUP_0_ADDR, data_array, 2, dev);
  1430. }
  1431. return rslt;
  1432. }
  1433. /*!
  1434. * @brief This API gets the set sensor settings for auto-wakeup timeout feature
  1435. */
  1436. static int8_t get_autowakeup_timeout(struct bma400_auto_wakeup_conf *wakeup_conf, const struct bma400_dev *dev)
  1437. {
  1438. int8_t rslt;
  1439. uint8_t data_array[2];
  1440. uint8_t lsb;
  1441. uint8_t msb;
  1442. rslt = bma400_get_regs(BMA400_AUTOWAKEUP_0_ADDR, data_array, 2, dev);
  1443. if (rslt == BMA400_OK) {
  1444. wakeup_conf->wakeup_timeout = BMA400_GET_BITS(data_array[1], BMA400_WAKEUP_TIMEOUT);
  1445. msb = data_array[0];
  1446. lsb = BMA400_GET_BITS(data_array[1], BMA400_WAKEUP_TIMEOUT_THRES);
  1447. /* Store the timeout value in the wakeup structure */
  1448. wakeup_conf->timeout_thres = msb << 4 | lsb;
  1449. }
  1450. return rslt;
  1451. }
  1452. /*!
  1453. * @brief This API enables the auto-wakeup feature of the sensor
  1454. */
  1455. static int8_t set_auto_wakeup(uint8_t conf, const struct bma400_dev *dev)
  1456. {
  1457. int8_t rslt;
  1458. uint8_t reg_data;
  1459. rslt = bma400_get_regs(BMA400_AUTOWAKEUP_1_ADDR, &reg_data, 1, dev);
  1460. if (rslt == BMA400_OK) {
  1461. reg_data = BMA400_SET_BITS(reg_data, BMA400_WAKEUP_INTERRUPT, conf);
  1462. /* Enabling the Auto wakeup interrupt */
  1463. rslt = bma400_set_regs(BMA400_AUTOWAKEUP_1_ADDR, &reg_data, 1, dev);
  1464. }
  1465. return rslt;
  1466. }
  1467. /*!
  1468. * @brief This API sets the parameters for auto-wakeup feature
  1469. * of the sensor
  1470. */
  1471. static int8_t set_autowakeup_interrupt(const struct bma400_wakeup_conf *wakeup_conf, const struct bma400_dev *dev)
  1472. {
  1473. int8_t rslt;
  1474. uint8_t data_array[5] = { 0 };
  1475. /* Set the wakeup reference update */
  1476. data_array[0] = BMA400_SET_BITS_POS_0(data_array[0], BMA400_WKUP_REF_UPDATE, wakeup_conf->wakeup_ref_update);
  1477. /* Set the number of samples for interrupt condition evaluation */
  1478. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_SAMPLE_COUNT, wakeup_conf->sample_count);
  1479. /* Enable low power wake-up interrupt for X,Y,Z axes*/
  1480. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_WAKEUP_EN_AXES, wakeup_conf->wakeup_axes_en);
  1481. /* Set interrupt threshold configuration */
  1482. data_array[1] = wakeup_conf->int_wkup_threshold;
  1483. /* Set the reference acceleration x-axis for the wake-up interrupt */
  1484. data_array[2] = wakeup_conf->int_wkup_ref_x;
  1485. /* Set the reference acceleration y-axis for the wake-up interrupt */
  1486. data_array[3] = wakeup_conf->int_wkup_ref_y;
  1487. /* Set the reference acceleration z-axis for the wake-up interrupt */
  1488. data_array[4] = wakeup_conf->int_wkup_ref_z;
  1489. /* Set the wakeup interrupt configurations in the sensor */
  1490. rslt = bma400_set_regs(BMA400_WAKEUP_INT_CONF_0_ADDR, data_array, 5, dev);
  1491. return rslt;
  1492. }
  1493. /*!
  1494. * @brief This API gets the set sensor settings for
  1495. * auto-wakeup interrupt feature
  1496. */
  1497. static int8_t get_autowakeup_interrupt(struct bma400_wakeup_conf *wakeup_conf, const struct bma400_dev *dev)
  1498. {
  1499. int8_t rslt;
  1500. uint8_t data_array[5];
  1501. rslt = bma400_get_regs(BMA400_WAKEUP_INT_CONF_0_ADDR, data_array, 5, dev);
  1502. if (rslt == BMA400_OK) {
  1503. /* get the wakeup reference update */
  1504. wakeup_conf->wakeup_ref_update = BMA400_GET_BITS_POS_0(data_array[0], BMA400_WKUP_REF_UPDATE);
  1505. /* Get the number of samples for interrupt condition evaluation */
  1506. wakeup_conf->sample_count = BMA400_GET_BITS(data_array[0], BMA400_SAMPLE_COUNT);
  1507. /* Get the axes enabled */
  1508. wakeup_conf->wakeup_axes_en = BMA400_GET_BITS(data_array[0], BMA400_WAKEUP_EN_AXES);
  1509. /* Get interrupt threshold configuration */
  1510. wakeup_conf->int_wkup_threshold = data_array[1];
  1511. /* Get the reference acceleration x-axis for the wake-up interrupt */
  1512. wakeup_conf->int_wkup_ref_x = data_array[2];
  1513. /* Get the reference acceleration y-axis for the wake-up interrupt */
  1514. wakeup_conf->int_wkup_ref_y = data_array[3];
  1515. /* Get the reference acceleration z-axis for the wake-up interrupt */
  1516. wakeup_conf->int_wkup_ref_z = data_array[4];
  1517. }
  1518. return rslt;
  1519. }
  1520. /*!
  1521. * @brief This API sets the sensor to enter low power mode
  1522. * automatically based on the configurations
  1523. */
  1524. static int8_t set_auto_low_power(const struct bma400_auto_lp_conf *auto_lp_conf, const struct bma400_dev *dev)
  1525. {
  1526. int8_t rslt;
  1527. uint8_t reg_data;
  1528. uint8_t timeout_msb;
  1529. uint8_t timeout_lsb;
  1530. rslt = bma400_get_regs(BMA400_AUTO_LOW_POW_1_ADDR, &reg_data, 1, dev);
  1531. if (rslt == BMA400_OK) {
  1532. reg_data = BMA400_SET_BITS_POS_0(reg_data, BMA400_AUTO_LOW_POW, auto_lp_conf->auto_low_power_trigger);
  1533. /* If auto Low power timeout threshold is enabled */
  1534. if (auto_lp_conf->auto_low_power_trigger & 0x0C) {
  1535. rslt = bma400_get_regs(BMA400_AUTO_LOW_POW_0_ADDR, &timeout_msb, 1, dev);
  1536. if (rslt == BMA400_OK) {
  1537. /* Compute the timeout threshold MSB value */
  1538. timeout_msb = BMA400_GET_BITS(auto_lp_conf->auto_lp_timeout_threshold,
  1539. BMA400_AUTO_LP_THRES);
  1540. /* Compute the timeout threshold LSB value */
  1541. timeout_lsb = BMA400_GET_BITS_POS_0(auto_lp_conf->auto_lp_timeout_threshold,
  1542. BMA400_AUTO_LP_THRES_LSB);
  1543. reg_data = BMA400_SET_BITS(reg_data, BMA400_AUTO_LP_TIMEOUT_LSB, timeout_lsb);
  1544. /* Set the timeout threshold MSB value */
  1545. rslt = bma400_set_regs(BMA400_AUTO_LOW_POW_0_ADDR, &timeout_msb, 1, dev);
  1546. }
  1547. }
  1548. if (rslt == BMA400_OK) {
  1549. /* Set the Auto low power configurations */
  1550. rslt = bma400_set_regs(BMA400_AUTO_LOW_POW_1_ADDR, &reg_data, 1, dev);
  1551. }
  1552. }
  1553. return rslt;
  1554. }
  1555. /*!
  1556. * @brief This API gets the sensor to get the auto-low
  1557. * power mode configuration settings
  1558. */
  1559. static int8_t get_auto_low_power(struct bma400_auto_lp_conf *auto_lp_conf, const struct bma400_dev *dev)
  1560. {
  1561. int8_t rslt;
  1562. uint8_t data_array[2];
  1563. uint8_t timeout_msb;
  1564. uint8_t timeout_lsb;
  1565. rslt = bma400_get_regs(BMA400_AUTO_LOW_POW_0_ADDR, data_array, 2, dev);
  1566. if (rslt == BMA400_OK) {
  1567. /* Get the auto low power trigger */
  1568. auto_lp_conf->auto_low_power_trigger = BMA400_GET_BITS_POS_0(data_array[1], BMA400_AUTO_LOW_POW);
  1569. timeout_msb = data_array[0];
  1570. timeout_lsb = BMA400_GET_BITS(data_array[1], BMA400_AUTO_LP_TIMEOUT_LSB);
  1571. /* Get the auto low power timeout threshold */
  1572. auto_lp_conf->auto_lp_timeout_threshold = timeout_msb << 4 | timeout_lsb;
  1573. }
  1574. return rslt;
  1575. }
  1576. /*!
  1577. * @brief This API sets the tap setting parameters
  1578. */
  1579. static int8_t set_tap_conf(const struct bma400_tap_conf *tap_set, const struct bma400_dev *dev)
  1580. {
  1581. int8_t rslt;
  1582. uint8_t reg_data[2] = { 0, 0 };
  1583. rslt = bma400_get_regs(BMA400_TAP_CONFIG_ADDR, reg_data, 2, dev);
  1584. if (rslt == BMA400_OK) {
  1585. /* Set the axis to sense for tap */
  1586. reg_data[0] = BMA400_SET_BITS(reg_data[0], BMA400_TAP_AXES_EN, tap_set->axes_sel);
  1587. /* Set the threshold for tap sensing */
  1588. reg_data[0] = BMA400_SET_BITS_POS_0(reg_data[0], BMA400_TAP_SENSITIVITY, tap_set->sensitivity);
  1589. /* Set the Quiet_dt setting */
  1590. reg_data[1] = BMA400_SET_BITS(reg_data[1], BMA400_TAP_QUIET_DT, tap_set->quiet_dt);
  1591. /* Set the Quiet setting */
  1592. reg_data[1] = BMA400_SET_BITS(reg_data[1], BMA400_TAP_QUIET, tap_set->quiet);
  1593. /* Set the tics_th setting */
  1594. reg_data[1] = BMA400_SET_BITS_POS_0(reg_data[1], BMA400_TAP_TICS_TH, tap_set->tics_th);
  1595. /* Set the TAP configuration in the sensor*/
  1596. rslt = bma400_set_regs(BMA400_TAP_CONFIG_ADDR, reg_data, 2, dev);
  1597. }
  1598. return rslt;
  1599. }
  1600. /*!
  1601. * @brief This API gets the tap setting parameters
  1602. */
  1603. static int8_t get_tap_conf(struct bma400_tap_conf *tap_set, const struct bma400_dev *dev)
  1604. {
  1605. int8_t rslt;
  1606. uint8_t reg_data[2];
  1607. rslt = bma400_get_regs(BMA400_TAP_CONFIG_ADDR, reg_data, 2, dev);
  1608. if (rslt == BMA400_OK) {
  1609. /* Get the axis enabled for tap sensing */
  1610. tap_set->axes_sel = BMA400_GET_BITS(reg_data[0], BMA400_TAP_AXES_EN);
  1611. /* Get the threshold for tap sensing */
  1612. tap_set->sensitivity = BMA400_GET_BITS_POS_0(reg_data[0], BMA400_TAP_SENSITIVITY);
  1613. /* Get the Quiet_dt setting */
  1614. tap_set->quiet_dt = BMA400_GET_BITS(reg_data[1], BMA400_TAP_QUIET_DT);
  1615. /* Get the Quiet setting */
  1616. tap_set->quiet = BMA400_GET_BITS(reg_data[1], BMA400_TAP_QUIET);
  1617. /* Get the tics_th setting */
  1618. tap_set->tics_th = BMA400_GET_BITS_POS_0(reg_data[1], BMA400_TAP_TICS_TH);
  1619. }
  1620. return rslt;
  1621. }
  1622. /*!
  1623. * @brief This API sets the parameters for activity change detection
  1624. */
  1625. static int8_t set_activity_change_conf(const struct bma400_act_ch_conf *act_ch_set, const struct bma400_dev *dev)
  1626. {
  1627. int8_t rslt;
  1628. uint8_t data_array[2] = { 0 };
  1629. /* Set the activity change threshold */
  1630. data_array[0] = act_ch_set->act_ch_thres;
  1631. /* Set the axis to sense for activity change */
  1632. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_ACT_CH_AXES_EN, act_ch_set->axes_sel);
  1633. /* Set the data source for activity change */
  1634. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_ACT_CH_DATA_SRC, act_ch_set->data_source);
  1635. /* Set the Number of sample points(NPTS)
  1636. * for sensing activity change */
  1637. data_array[1] = BMA400_SET_BITS_POS_0(data_array[1], BMA400_ACT_CH_NPTS, act_ch_set->act_ch_ntps);
  1638. /* Set the Activity change configuration in the sensor*/
  1639. rslt = bma400_set_regs(BMA400_ACT_CH_CONFIG_0_ADDR, data_array, 2, dev);
  1640. return rslt;
  1641. }
  1642. /*!
  1643. * @brief This API gets the parameters for activity change detection
  1644. */
  1645. static int8_t get_activity_change_conf(struct bma400_act_ch_conf *act_ch_set, const struct bma400_dev *dev)
  1646. {
  1647. int8_t rslt;
  1648. uint8_t data_array[2];
  1649. rslt = bma400_get_regs(BMA400_ACT_CH_CONFIG_0_ADDR, data_array, 2, dev);
  1650. if (rslt == BMA400_OK) {
  1651. /* Get the activity change threshold */
  1652. act_ch_set->act_ch_thres = data_array[0];
  1653. /* Get the axis enabled for activity change detection */
  1654. act_ch_set->axes_sel = BMA400_GET_BITS(data_array[1], BMA400_ACT_CH_AXES_EN);
  1655. /* Get the data source for activity change */
  1656. act_ch_set->data_source = BMA400_GET_BITS(data_array[1], BMA400_ACT_CH_DATA_SRC);
  1657. /* Get the Number of sample points(NPTS)
  1658. * for sensing activity change */
  1659. act_ch_set->act_ch_ntps = BMA400_GET_BITS_POS_0(data_array[1], BMA400_ACT_CH_NPTS);
  1660. }
  1661. return rslt;
  1662. }
  1663. /*!
  1664. * @brief This API sets the parameters for generic interrupt1 configuration
  1665. */
  1666. static int8_t set_gen1_int(const struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev)
  1667. {
  1668. int8_t rslt;
  1669. uint8_t data_array[11] = { 0 };
  1670. /* Set the axes to sense for interrupt */
  1671. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_AXES_EN, gen_int_set->axes_sel);
  1672. /* Set the data source for interrupt */
  1673. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_DATA_SRC, gen_int_set->data_src);
  1674. /* Set the reference update mode */
  1675. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_REFU, gen_int_set->ref_update);
  1676. /* Set the hysteresis for interrupt calculation */
  1677. data_array[0] = BMA400_SET_BITS_POS_0(data_array[0], BMA400_INT_HYST, gen_int_set->hysteresis);
  1678. /* Set the criterion to generate interrupt on either
  1679. * ACTIVITY OR INACTIVITY */
  1680. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_GEN_INT_CRITERION, gen_int_set->criterion_sel);
  1681. /* Set the interrupt axes logic (AND/OR) for the
  1682. * enabled axes to generate interrupt */
  1683. data_array[1] = BMA400_SET_BITS_POS_0(data_array[1], BMA400_GEN_INT_COMB, gen_int_set->evaluate_axes);
  1684. /* Set the interrupt threshold */
  1685. data_array[2] = gen_int_set->gen_int_thres;
  1686. /* Set the MSB of gen int dur */
  1687. data_array[3] = BMA400_GET_MSB(gen_int_set->gen_int_dur);
  1688. /* Set the LSB of gen int dur */
  1689. data_array[4] = BMA400_GET_LSB(gen_int_set->gen_int_dur);
  1690. /* Handling case of manual reference update */
  1691. if (gen_int_set->ref_update == BMA400_MANUAL_UPDATE) {
  1692. /* Set the LSB of reference x threshold */
  1693. data_array[5] = BMA400_GET_LSB(gen_int_set->int_thres_ref_x);
  1694. /* Set the MSB of reference x threshold */
  1695. data_array[6] = BMA400_GET_MSB(gen_int_set->int_thres_ref_x);
  1696. /* Set the LSB of reference y threshold */
  1697. data_array[7] = BMA400_GET_LSB(gen_int_set->int_thres_ref_y);
  1698. /* Set the MSB of reference y threshold */
  1699. data_array[8] = BMA400_GET_MSB(gen_int_set->int_thres_ref_y);
  1700. /* Set the LSB of reference z threshold */
  1701. data_array[9] = BMA400_GET_LSB(gen_int_set->int_thres_ref_z);
  1702. /* Set the MSB of reference z threshold */
  1703. data_array[10] = BMA400_GET_MSB(gen_int_set->int_thres_ref_z);
  1704. /* Set the GEN1 INT configuration in the sensor */
  1705. rslt = bma400_set_regs(BMA400_GEN1_INT_CONFIG_ADDR, data_array, 11, dev);
  1706. } else {
  1707. /* Set the GEN1 INT configuration in the sensor */
  1708. rslt = bma400_set_regs(BMA400_GEN1_INT_CONFIG_ADDR, data_array, 5, dev);
  1709. }
  1710. return rslt;
  1711. }
  1712. /*!
  1713. * @brief This API gets the generic interrupt1 configuration
  1714. */
  1715. static int8_t get_gen1_int(struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev)
  1716. {
  1717. int8_t rslt;
  1718. uint8_t data_array[11];
  1719. rslt = bma400_get_regs(BMA400_GEN1_INT_CONFIG_ADDR, data_array, 11, dev);
  1720. if (rslt == BMA400_OK) {
  1721. /* Get the axes to sense for interrupt */
  1722. gen_int_set->axes_sel = BMA400_GET_BITS(data_array[0], BMA400_INT_AXES_EN);
  1723. /* Get the data source for interrupt */
  1724. gen_int_set->data_src = BMA400_GET_BITS(data_array[0], BMA400_INT_DATA_SRC);
  1725. /* Get the reference update mode */
  1726. gen_int_set->ref_update = BMA400_GET_BITS(data_array[0], BMA400_INT_REFU);
  1727. /* Get the hysteresis for interrupt calculation */
  1728. gen_int_set->hysteresis = BMA400_GET_BITS_POS_0(data_array[0], BMA400_INT_HYST);
  1729. /* Get the interrupt axes logic (AND/OR) to generate interrupt */
  1730. gen_int_set->evaluate_axes = BMA400_GET_BITS_POS_0(data_array[1], BMA400_GEN_INT_COMB);
  1731. /* Get the criterion to generate interrupt ACTIVITY/INACTIVITY */
  1732. gen_int_set->criterion_sel = BMA400_GET_BITS(data_array[1], BMA400_GEN_INT_CRITERION);
  1733. /* Get the interrupt threshold */
  1734. gen_int_set->gen_int_thres = data_array[2];
  1735. /* Get the interrupt duration */
  1736. gen_int_set->gen_int_dur = ((uint16_t)data_array[3] << 8) | data_array[4];
  1737. /* Get the interrupt threshold */
  1738. data_array[6] = data_array[6] & 0x0F;
  1739. gen_int_set->int_thres_ref_x = ((uint16_t)data_array[6] << 8) | data_array[5];
  1740. data_array[8] = data_array[8] & 0x0F;
  1741. gen_int_set->int_thres_ref_y = ((uint16_t)data_array[8] << 8) | data_array[7];
  1742. data_array[10] = data_array[10] & 0x0F;
  1743. gen_int_set->int_thres_ref_z = ((uint16_t)data_array[10] << 8) | data_array[9];
  1744. }
  1745. return rslt;
  1746. }
  1747. /*!
  1748. * @brief This API sets the parameters for generic interrupt2 configuration
  1749. */
  1750. static int8_t set_gen2_int(const struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev)
  1751. {
  1752. int8_t rslt;
  1753. uint8_t data_array[11] = { 0 };
  1754. /* Set the axes to sense for interrupt */
  1755. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_AXES_EN, gen_int_set->axes_sel);
  1756. /* Set the data source for interrupt */
  1757. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_DATA_SRC, gen_int_set->data_src);
  1758. /* Set the reference update mode */
  1759. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_REFU, gen_int_set->ref_update);
  1760. /* Set the hysteresis for interrupt calculation */
  1761. data_array[0] = BMA400_SET_BITS_POS_0(data_array[0], BMA400_INT_HYST, gen_int_set->hysteresis);
  1762. /* Set the criterion to generate interrupt on either
  1763. * ACTIVITY OR INACTIVITY */
  1764. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_GEN_INT_CRITERION, gen_int_set->criterion_sel);
  1765. /* Set the interrupt axes logic (AND/OR) for the
  1766. * enabled axes to generate interrupt */
  1767. data_array[1] = BMA400_SET_BITS_POS_0(data_array[1], BMA400_GEN_INT_COMB, gen_int_set->evaluate_axes);
  1768. /* Set the interrupt threshold */
  1769. data_array[2] = gen_int_set->gen_int_thres;
  1770. /* Set the MSB of gen int dur */
  1771. data_array[3] = BMA400_GET_MSB(gen_int_set->gen_int_dur);
  1772. /* Set the LSB of gen int dur */
  1773. data_array[4] = BMA400_GET_LSB(gen_int_set->gen_int_dur);
  1774. /* Handling case of manual reference update */
  1775. if (gen_int_set->ref_update == BMA400_MANUAL_UPDATE) {
  1776. /* Set the LSB of reference x threshold */
  1777. data_array[5] = BMA400_GET_LSB(gen_int_set->int_thres_ref_x);
  1778. /* Set the MSB of reference x threshold */
  1779. data_array[6] = BMA400_GET_MSB(gen_int_set->int_thres_ref_x);
  1780. /* Set the LSB of reference y threshold */
  1781. data_array[7] = BMA400_GET_LSB(gen_int_set->int_thres_ref_y);
  1782. /* Set the MSB of reference y threshold */
  1783. data_array[8] = BMA400_GET_MSB(gen_int_set->int_thres_ref_y);
  1784. /* Set the LSB of reference z threshold */
  1785. data_array[9] = BMA400_GET_LSB(gen_int_set->int_thres_ref_z);
  1786. /* Set the MSB of reference z threshold */
  1787. data_array[10] = BMA400_GET_MSB(gen_int_set->int_thres_ref_z);
  1788. /* Set the GEN2 INT configuration in the sensor */
  1789. rslt = bma400_set_regs(BMA400_GEN2_INT_CONFIG_ADDR, data_array, 11, dev);
  1790. } else {
  1791. /* Set the GEN2 INT configuration in the sensor */
  1792. rslt = bma400_set_regs(BMA400_GEN2_INT_CONFIG_ADDR, data_array, 5, dev);
  1793. }
  1794. return rslt;
  1795. }
  1796. /*!
  1797. * @brief This API gets the generic interrupt2 configuration
  1798. */
  1799. static int8_t get_gen2_int(struct bma400_gen_int_conf *gen_int_set, const struct bma400_dev *dev)
  1800. {
  1801. int8_t rslt;
  1802. uint8_t data_array[11];
  1803. rslt = bma400_get_regs(BMA400_GEN2_INT_CONFIG_ADDR, data_array, 11, dev);
  1804. if (rslt == BMA400_OK) {
  1805. /* Get the axes to sense for interrupt */
  1806. gen_int_set->axes_sel = BMA400_GET_BITS(data_array[0], BMA400_INT_AXES_EN);
  1807. /* Get the data source for interrupt */
  1808. gen_int_set->data_src = BMA400_GET_BITS(data_array[0], BMA400_INT_DATA_SRC);
  1809. /* Get the reference update mode */
  1810. gen_int_set->ref_update = BMA400_GET_BITS(data_array[0], BMA400_INT_REFU);
  1811. /* Get the hysteresis for interrupt calculation */
  1812. gen_int_set->hysteresis = BMA400_GET_BITS_POS_0(data_array[0], BMA400_INT_HYST);
  1813. /* Get the interrupt axes logic (AND/OR) to generate interrupt */
  1814. gen_int_set->evaluate_axes = BMA400_GET_BITS_POS_0(data_array[1], BMA400_GEN_INT_COMB);
  1815. /* Get the criterion to generate interrupt ACTIVITY/INACTIVITY */
  1816. gen_int_set->criterion_sel = BMA400_GET_BITS(data_array[1], BMA400_GEN_INT_CRITERION);
  1817. /* Get the interrupt threshold */
  1818. gen_int_set->gen_int_thres = data_array[2];
  1819. /* Get the interrupt duration */
  1820. gen_int_set->gen_int_dur = ((uint16_t)data_array[3] << 8) | data_array[4];
  1821. /* Get the interrupt threshold */
  1822. data_array[6] = data_array[6] & 0x0F;
  1823. gen_int_set->int_thres_ref_x = ((uint16_t)data_array[6] << 8) | data_array[5];
  1824. data_array[8] = data_array[8] & 0x0F;
  1825. gen_int_set->int_thres_ref_y = ((uint16_t)data_array[8] << 8) | data_array[7];
  1826. data_array[10] = data_array[10] & 0x0F;
  1827. gen_int_set->int_thres_ref_z = ((uint16_t)data_array[10] << 8) | data_array[9];
  1828. }
  1829. return rslt;
  1830. }
  1831. /*!
  1832. * @brief This API sets the parameters for orientation interrupt
  1833. */
  1834. static int8_t set_orient_int(const struct bma400_orient_int_conf *orient_conf, const struct bma400_dev *dev)
  1835. {
  1836. int8_t rslt;
  1837. uint8_t data_array[10] = { 0 };
  1838. /* Set the axes to sense for interrupt */
  1839. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_AXES_EN, orient_conf->axes_sel);
  1840. /* Set the data source for interrupt */
  1841. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_DATA_SRC, orient_conf->data_src);
  1842. /* Set the reference update mode */
  1843. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_INT_REFU, orient_conf->ref_update);
  1844. /* Set the stability_mode for interrupt calculation */
  1845. data_array[0] = BMA400_SET_BITS_POS_0(data_array[0], BMA400_STABILITY_MODE, orient_conf->stability_mode);
  1846. /* Set the threshold for interrupt calculation */
  1847. data_array[1] = orient_conf->orient_thres;
  1848. /* Set the stability threshold */
  1849. data_array[2] = orient_conf->stability_thres;
  1850. /* Set the interrupt duration */
  1851. data_array[3] = orient_conf->orient_int_dur;
  1852. /* Handling case of manual reference update */
  1853. if (orient_conf->ref_update == BMA400_MANUAL_UPDATE) {
  1854. /* Set the LSB of reference x threshold */
  1855. data_array[4] = BMA400_GET_LSB(orient_conf->orient_ref_x);
  1856. /* Set the MSB of reference x threshold */
  1857. data_array[5] = BMA400_GET_MSB(orient_conf->orient_ref_x);
  1858. /* Set the MSB of reference x threshold */
  1859. data_array[6] = BMA400_GET_LSB(orient_conf->orient_ref_y);
  1860. /* Set the LSB of reference y threshold */
  1861. data_array[7] = BMA400_GET_MSB(orient_conf->orient_ref_y);
  1862. /* Set the MSB of reference y threshold */
  1863. data_array[8] = BMA400_GET_LSB(orient_conf->orient_ref_z);
  1864. /* Set the LSB of reference z threshold */
  1865. data_array[9] = BMA400_GET_MSB(orient_conf->orient_ref_z);
  1866. /* Set the orient configurations in the sensor */
  1867. rslt = bma400_set_regs(BMA400_ORIENTCH_INT_CONFIG_ADDR, data_array, 10, dev);
  1868. } else {
  1869. /* Set the orient configurations in the sensor excluding
  1870. * reference values of x,y,z */
  1871. rslt = bma400_set_regs(BMA400_ORIENTCH_INT_CONFIG_ADDR, data_array, 4, dev);
  1872. }
  1873. return rslt;
  1874. }
  1875. /*!
  1876. * @brief This API gets the parameters for orientation interrupt
  1877. */
  1878. static int8_t get_orient_int(struct bma400_orient_int_conf *orient_conf, const struct bma400_dev *dev)
  1879. {
  1880. int8_t rslt;
  1881. uint8_t data_array[10];
  1882. rslt = bma400_get_regs(BMA400_ORIENTCH_INT_CONFIG_ADDR, data_array, 10, dev);
  1883. if (rslt == BMA400_OK) {
  1884. /* Get the axes to sense for interrupt */
  1885. orient_conf->axes_sel = BMA400_GET_BITS(data_array[0], BMA400_INT_AXES_EN);
  1886. /* Get the data source for interrupt */
  1887. orient_conf->data_src = BMA400_GET_BITS(data_array[0], BMA400_INT_DATA_SRC);
  1888. /* Get the reference update mode */
  1889. orient_conf->ref_update = BMA400_GET_BITS(data_array[0], BMA400_INT_REFU);
  1890. /* Get the stability_mode for interrupt calculation */
  1891. orient_conf->stability_mode = BMA400_GET_BITS_POS_0(data_array[0], BMA400_STABILITY_MODE);
  1892. /* Get the threshold for interrupt calculation */
  1893. orient_conf->orient_thres = data_array[1];
  1894. /* Get the stability threshold */
  1895. orient_conf->stability_thres = data_array[2];
  1896. /* Get the interrupt duration */
  1897. orient_conf->orient_int_dur = data_array[3];
  1898. /* Get the interrupt reference values */
  1899. data_array[5] = data_array[5] & 0x0F;
  1900. orient_conf->orient_ref_x = ((uint16_t)data_array[5] << 8) | data_array[4];
  1901. data_array[5] = data_array[7] & 0x0F;
  1902. orient_conf->orient_ref_y = ((uint16_t)data_array[7] << 8) | data_array[6];
  1903. data_array[5] = data_array[9] & 0x0F;
  1904. orient_conf->orient_ref_z = ((uint16_t)data_array[9] << 8) | data_array[8];
  1905. }
  1906. return rslt;
  1907. }
  1908. /*!
  1909. * @brief This API sets the selected interrupt to be mapped to
  1910. * the hardware interrupt pin of the sensor
  1911. */
  1912. static void map_int_pin(uint8_t *data_array, uint8_t int_enable, enum bma400_int_chan int_map)
  1913. {
  1914. switch (int_enable) {
  1915. case BMA400_DATA_READY_INT_MAP:
  1916. if (int_map == BMA400_INT_CHANNEL_1) {
  1917. /* Mapping interrupt to INT pin 1*/
  1918. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_DRDY, BMA400_ENABLE);
  1919. }
  1920. if (int_map == BMA400_INT_CHANNEL_2) {
  1921. /* Mapping interrupt to INT pin 2*/
  1922. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_DRDY, BMA400_ENABLE);
  1923. }
  1924. if (int_map == BMA400_UNMAP_INT_PIN) {
  1925. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_DRDY);
  1926. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_DRDY);
  1927. }
  1928. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  1929. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_DRDY, BMA400_ENABLE);
  1930. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_DRDY, BMA400_ENABLE);
  1931. }
  1932. break;
  1933. case BMA400_FIFO_WM_INT_MAP:
  1934. if (int_map == BMA400_INT_CHANNEL_1) {
  1935. /* Mapping interrupt to INT pin 1*/
  1936. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_FIFO_WM, BMA400_ENABLE);
  1937. }
  1938. if (int_map == BMA400_INT_CHANNEL_2) {
  1939. /* Mapping interrupt to INT pin 2*/
  1940. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_FIFO_WM, BMA400_ENABLE);
  1941. }
  1942. if (int_map == BMA400_UNMAP_INT_PIN) {
  1943. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_FIFO_WM);
  1944. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_FIFO_WM);
  1945. }
  1946. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  1947. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_FIFO_WM, BMA400_ENABLE);
  1948. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_FIFO_WM, BMA400_ENABLE);
  1949. }
  1950. break;
  1951. case BMA400_FIFO_FULL_INT_MAP:
  1952. if (int_map == BMA400_INT_CHANNEL_1) {
  1953. /* Mapping interrupt to INT pin 1 */
  1954. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_FIFO_FULL, BMA400_ENABLE);
  1955. }
  1956. if (int_map == BMA400_INT_CHANNEL_2) {
  1957. /* Mapping interrupt to INT pin 2 */
  1958. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_FIFO_FULL, BMA400_ENABLE);
  1959. }
  1960. if (int_map == BMA400_UNMAP_INT_PIN) {
  1961. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_FIFO_FULL);
  1962. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_FIFO_FULL);
  1963. }
  1964. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  1965. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_FIFO_FULL, BMA400_ENABLE);
  1966. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_FIFO_FULL, BMA400_ENABLE);
  1967. }
  1968. break;
  1969. case BMA400_INT_OVERRUN_MAP:
  1970. if (int_map == BMA400_INT_CHANNEL_1) {
  1971. /* Mapping interrupt to INT pin 1 */
  1972. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_INT_OVERRUN, BMA400_ENABLE);
  1973. }
  1974. if (int_map == BMA400_INT_CHANNEL_2) {
  1975. /* Mapping interrupt to INT pin 2 */
  1976. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_INT_OVERRUN, BMA400_ENABLE);
  1977. }
  1978. if (int_map == BMA400_UNMAP_INT_PIN) {
  1979. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_INT_OVERRUN);
  1980. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_INT_OVERRUN);
  1981. }
  1982. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  1983. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_INT_OVERRUN, BMA400_ENABLE);
  1984. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_INT_OVERRUN, BMA400_ENABLE);
  1985. }
  1986. break;
  1987. case BMA400_GEN2_INT_MAP:
  1988. if (int_map == BMA400_INT_CHANNEL_1) {
  1989. /* Mapping interrupt to INT pin 1 */
  1990. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_GEN2, BMA400_ENABLE);
  1991. }
  1992. if (int_map == BMA400_INT_CHANNEL_2) {
  1993. /* Mapping interrupt to INT pin 2 */
  1994. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_GEN2, BMA400_ENABLE);
  1995. }
  1996. if (int_map == BMA400_UNMAP_INT_PIN) {
  1997. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_GEN2);
  1998. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_GEN2);
  1999. }
  2000. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  2001. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_GEN2, BMA400_ENABLE);
  2002. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_GEN2, BMA400_ENABLE);
  2003. }
  2004. break;
  2005. case BMA400_GEN1_INT_MAP:
  2006. if (int_map == BMA400_INT_CHANNEL_1) {
  2007. /* Mapping interrupt to INT pin 1 */
  2008. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_GEN1, BMA400_ENABLE);
  2009. }
  2010. if (int_map == BMA400_INT_CHANNEL_2) {
  2011. /* Mapping interrupt to INT pin 2 */
  2012. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_GEN1, BMA400_ENABLE);
  2013. }
  2014. if (int_map == BMA400_UNMAP_INT_PIN) {
  2015. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_GEN1);
  2016. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_GEN1);
  2017. }
  2018. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  2019. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_GEN1, BMA400_ENABLE);
  2020. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_GEN1, BMA400_ENABLE);
  2021. }
  2022. break;
  2023. case BMA400_ORIENT_CH_INT_MAP:
  2024. if (int_map == BMA400_INT_CHANNEL_1) {
  2025. /* Mapping interrupt to INT pin 1 */
  2026. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_ORIENT_CH, BMA400_ENABLE);
  2027. }
  2028. if (int_map == BMA400_INT_CHANNEL_2) {
  2029. /* Mapping interrupt to INT pin 2 */
  2030. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_ORIENT_CH, BMA400_ENABLE);
  2031. }
  2032. if (int_map == BMA400_UNMAP_INT_PIN) {
  2033. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_ORIENT_CH);
  2034. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_ORIENT_CH);
  2035. }
  2036. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  2037. data_array[0] = BMA400_SET_BITS(data_array[0], BMA400_EN_ORIENT_CH, BMA400_ENABLE);
  2038. data_array[1] = BMA400_SET_BITS(data_array[1], BMA400_EN_ORIENT_CH, BMA400_ENABLE);
  2039. }
  2040. break;
  2041. case BMA400_WAKEUP_INT_MAP:
  2042. if (int_map == BMA400_INT_CHANNEL_1) {
  2043. /* Mapping interrupt to INT pin 1 */
  2044. data_array[0] = BMA400_SET_BITS_POS_0(data_array[0], BMA400_EN_WAKEUP_INT, BMA400_ENABLE);
  2045. }
  2046. if (int_map == BMA400_INT_CHANNEL_2) {
  2047. /* Mapping interrupt to INT pin 2 */
  2048. data_array[1] = BMA400_SET_BITS_POS_0(data_array[1], BMA400_EN_WAKEUP_INT, BMA400_ENABLE);
  2049. }
  2050. if (int_map == BMA400_UNMAP_INT_PIN) {
  2051. data_array[0] = BMA400_SET_BIT_VAL_0(data_array[0], BMA400_EN_WAKEUP_INT);
  2052. data_array[1] = BMA400_SET_BIT_VAL_0(data_array[1], BMA400_EN_WAKEUP_INT);
  2053. }
  2054. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  2055. data_array[0] = BMA400_SET_BITS_POS_0(data_array[0], BMA400_EN_WAKEUP_INT, BMA400_ENABLE);
  2056. data_array[1] = BMA400_SET_BITS_POS_0(data_array[1], BMA400_EN_WAKEUP_INT, BMA400_ENABLE);
  2057. }
  2058. break;
  2059. case BMA400_ACT_CH_INT_MAP:
  2060. if (int_map == BMA400_INT_CHANNEL_1) {
  2061. /* Mapping interrupt to INT pin 1 */
  2062. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_ACTCH_MAP_INT1, BMA400_ENABLE);
  2063. }
  2064. if (int_map == BMA400_INT_CHANNEL_2) {
  2065. /* Mapping interrupt to INT pin 2 */
  2066. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_ACTCH_MAP_INT2, BMA400_ENABLE);
  2067. }
  2068. if (int_map == BMA400_UNMAP_INT_PIN) {
  2069. data_array[2] = BMA400_SET_BIT_VAL_0(data_array[2], BMA400_ACTCH_MAP_INT1);
  2070. data_array[2] = BMA400_SET_BIT_VAL_0(data_array[2], BMA400_ACTCH_MAP_INT2);
  2071. }
  2072. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  2073. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_ACTCH_MAP_INT1, BMA400_ENABLE);
  2074. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_ACTCH_MAP_INT2, BMA400_ENABLE);
  2075. }
  2076. break;
  2077. case BMA400_TAP_INT_MAP:
  2078. if (int_map == BMA400_INT_CHANNEL_1) {
  2079. /* Mapping interrupt to INT pin 1 */
  2080. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_TAP_MAP_INT1, BMA400_ENABLE);
  2081. }
  2082. if (int_map == BMA400_INT_CHANNEL_2) {
  2083. /* Mapping interrupt to INT pin 2 */
  2084. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_TAP_MAP_INT2, BMA400_ENABLE);
  2085. }
  2086. if (int_map == BMA400_UNMAP_INT_PIN) {
  2087. data_array[2] = BMA400_SET_BIT_VAL_0(data_array[2], BMA400_TAP_MAP_INT1);
  2088. data_array[2] = BMA400_SET_BIT_VAL_0(data_array[2], BMA400_TAP_MAP_INT2);
  2089. }
  2090. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  2091. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_TAP_MAP_INT1, BMA400_ENABLE);
  2092. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_TAP_MAP_INT2, BMA400_ENABLE);
  2093. }
  2094. break;
  2095. case BMA400_STEP_INT_MAP:
  2096. if (int_map == BMA400_INT_CHANNEL_1) {
  2097. /* Mapping interrupt to INT pin 1 */
  2098. data_array[2] = BMA400_SET_BITS_POS_0(data_array[2], BMA400_EN_STEP_INT, BMA400_ENABLE);
  2099. }
  2100. if (int_map == BMA400_INT_CHANNEL_2) {
  2101. /* Mapping interrupt to INT pin 2 */
  2102. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_STEP_MAP_INT2, BMA400_ENABLE);
  2103. }
  2104. if (int_map == BMA400_UNMAP_INT_PIN) {
  2105. data_array[2] = BMA400_SET_BIT_VAL_0(data_array[2], BMA400_EN_STEP_INT);
  2106. data_array[2] = BMA400_SET_BIT_VAL_0(data_array[2], BMA400_STEP_MAP_INT2);
  2107. }
  2108. if (int_map == BMA400_MAP_BOTH_INT_PINS) {
  2109. data_array[2] = BMA400_SET_BITS_POS_0(data_array[2], BMA400_EN_STEP_INT, BMA400_ENABLE);
  2110. data_array[2] = BMA400_SET_BITS(data_array[2], BMA400_STEP_MAP_INT2, BMA400_ENABLE);
  2111. }
  2112. break;
  2113. default:
  2114. break;
  2115. }
  2116. }
  2117. /*!
  2118. * @brief This API checks whether the interrupt is mapped to the INT pin1
  2119. * or INT pin2 of the sensor
  2120. */
  2121. static void check_mapped_interrupts(uint8_t int_1_map, uint8_t int_2_map, enum bma400_int_chan *int_map)
  2122. {
  2123. if ((int_1_map == BMA400_ENABLE) && (int_2_map == BMA400_DISABLE)) {
  2124. /* INT 1 mapped INT 2 not mapped */
  2125. *int_map = BMA400_INT_CHANNEL_1;
  2126. }
  2127. if ((int_1_map == BMA400_DISABLE) && (int_2_map == BMA400_ENABLE)) {
  2128. /* INT 1 not mapped INT 2 mapped */
  2129. *int_map = BMA400_INT_CHANNEL_2;
  2130. }
  2131. if ((int_1_map == BMA400_ENABLE) && (int_2_map == BMA400_ENABLE)) {
  2132. /* INT 1 ,INT 2 both mapped */
  2133. *int_map = BMA400_MAP_BOTH_INT_PINS;
  2134. }
  2135. if ((int_1_map == BMA400_DISABLE) && (int_2_map == BMA400_DISABLE)) {
  2136. /* INT 1 ,INT 2 not mapped */
  2137. *int_map = BMA400_UNMAP_INT_PIN;
  2138. }
  2139. }
  2140. /*!
  2141. * @brief This API gets the selected interrupt and its mapping to
  2142. * the hardware interrupt pin of the sensor
  2143. */
  2144. static void get_int_pin_map(const uint8_t *data_array, uint8_t int_enable, enum bma400_int_chan *int_map)
  2145. {
  2146. uint8_t int_1_map;
  2147. uint8_t int_2_map;
  2148. switch (int_enable) {
  2149. case BMA400_DATA_READY_INT_MAP:
  2150. /* Interrupt 1 pin mapping status */
  2151. int_1_map = BMA400_GET_BITS(data_array[0], BMA400_EN_DRDY);
  2152. /* Interrupt 2 pin mapping status */
  2153. int_2_map = BMA400_GET_BITS(data_array[1], BMA400_EN_DRDY);
  2154. /* Check the mapped interrupt pins */
  2155. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2156. break;
  2157. case BMA400_FIFO_WM_INT_MAP:
  2158. /* Interrupt 1 pin mapping status */
  2159. int_1_map = BMA400_GET_BITS(data_array[0], BMA400_EN_FIFO_WM);
  2160. /* Interrupt 2 pin mapping status */
  2161. int_2_map = BMA400_GET_BITS(data_array[1], BMA400_EN_FIFO_WM);
  2162. /* Check the mapped interrupt pins */
  2163. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2164. break;
  2165. case BMA400_FIFO_FULL_INT_MAP:
  2166. /* Interrupt 1 pin mapping status */
  2167. int_1_map = BMA400_GET_BITS(data_array[0], BMA400_EN_FIFO_FULL);
  2168. /* Interrupt 2 pin mapping status */
  2169. int_2_map = BMA400_GET_BITS(data_array[1], BMA400_EN_FIFO_FULL);
  2170. /* Check the mapped interrupt pins */
  2171. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2172. break;
  2173. case BMA400_GEN2_INT_MAP:
  2174. /* Interrupt 1 pin mapping status */
  2175. int_1_map = BMA400_GET_BITS(data_array[0], BMA400_EN_GEN2);
  2176. /* Interrupt 2 pin mapping status */
  2177. int_2_map = BMA400_GET_BITS(data_array[1], BMA400_EN_GEN2);
  2178. /* Check the mapped interrupt pins */
  2179. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2180. break;
  2181. case BMA400_GEN1_INT_MAP:
  2182. /* Interrupt 1 pin mapping status */
  2183. int_1_map = BMA400_GET_BITS(data_array[0], BMA400_EN_GEN1);
  2184. /* Interrupt 2 pin mapping status */
  2185. int_2_map = BMA400_GET_BITS(data_array[1], BMA400_EN_GEN1);
  2186. /* Check the mapped interrupt pins */
  2187. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2188. break;
  2189. case BMA400_ORIENT_CH_INT_MAP:
  2190. /* Interrupt 1 pin mapping status */
  2191. int_1_map = BMA400_GET_BITS(data_array[0], BMA400_EN_ORIENT_CH);
  2192. /* Interrupt 2 pin mapping status */
  2193. int_2_map = BMA400_GET_BITS(data_array[1], BMA400_EN_ORIENT_CH);
  2194. /* Check the mapped interrupt pins */
  2195. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2196. break;
  2197. case BMA400_WAKEUP_INT_MAP:
  2198. /* Interrupt 1 pin mapping status */
  2199. int_1_map = BMA400_GET_BITS_POS_0(data_array[0], BMA400_EN_WAKEUP_INT);
  2200. /* Interrupt 2 pin mapping status */
  2201. int_2_map = BMA400_GET_BITS_POS_0(data_array[1], BMA400_EN_WAKEUP_INT);
  2202. /* Check the mapped interrupt pins */
  2203. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2204. break;
  2205. case BMA400_ACT_CH_INT_MAP:
  2206. /* Interrupt 1 pin mapping status */
  2207. int_1_map = BMA400_GET_BITS(data_array[2], BMA400_ACTCH_MAP_INT1);
  2208. /* Interrupt 2 pin mapping status */
  2209. int_2_map = BMA400_GET_BITS(data_array[2], BMA400_ACTCH_MAP_INT2);
  2210. /* Check the mapped interrupt pins */
  2211. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2212. break;
  2213. case BMA400_TAP_INT_MAP:
  2214. /* Interrupt 1 pin mapping status */
  2215. int_1_map = BMA400_GET_BITS(data_array[2], BMA400_TAP_MAP_INT1);
  2216. /* Interrupt 2 pin mapping status */
  2217. int_2_map = BMA400_GET_BITS(data_array[2], BMA400_TAP_MAP_INT2);
  2218. /* Check the mapped interrupt pins */
  2219. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2220. break;
  2221. case BMA400_STEP_INT_MAP:
  2222. /* Interrupt 1 pin mapping status */
  2223. int_1_map = BMA400_GET_BITS_POS_0(data_array[2], BMA400_EN_STEP_INT);
  2224. /* Interrupt 2 pin mapping status */
  2225. int_2_map = BMA400_GET_BITS(data_array[2], BMA400_STEP_MAP_INT2);
  2226. /* Check the mapped interrupt pins */
  2227. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2228. break;
  2229. case BMA400_INT_OVERRUN_MAP:
  2230. /* Interrupt 1 pin mapping status */
  2231. int_1_map = BMA400_GET_BITS(data_array[0], BMA400_EN_INT_OVERRUN);
  2232. /* Interrupt 2 pin mapping status */
  2233. int_2_map = BMA400_GET_BITS(data_array[1], BMA400_EN_INT_OVERRUN);
  2234. /* Check the mapped interrupt pins */
  2235. check_mapped_interrupts(int_1_map, int_2_map, int_map);
  2236. break;
  2237. default:
  2238. break;
  2239. }
  2240. }
  2241. /*!
  2242. * @brief This API is used to set the interrupt pin configurations
  2243. */
  2244. static int8_t set_int_pin_conf(struct bma400_int_pin_conf int_conf, const struct bma400_dev *dev)
  2245. {
  2246. int8_t rslt;
  2247. uint8_t reg_data;
  2248. rslt = bma400_get_regs(BMA400_INT_12_IO_CTRL_ADDR, &reg_data, 1, dev);
  2249. if (rslt == BMA400_OK) {
  2250. if (int_conf.int_chan == BMA400_INT_CHANNEL_1) {
  2251. /* Setting interrupt pin configurations */
  2252. reg_data = BMA400_SET_BITS(reg_data, BMA400_INT_PIN1_CONF, int_conf.pin_conf);
  2253. }
  2254. if (int_conf.int_chan == BMA400_INT_CHANNEL_2) {
  2255. /* Setting interrupt pin configurations */
  2256. reg_data = BMA400_SET_BITS(reg_data, BMA400_INT_PIN2_CONF, int_conf.pin_conf);
  2257. }
  2258. /* Set the configurations in the sensor */
  2259. rslt = bma400_set_regs(BMA400_INT_12_IO_CTRL_ADDR, &reg_data, 1, dev);
  2260. }
  2261. return rslt;
  2262. }
  2263. /*!
  2264. * @brief This API is used to get the interrupt pin configurations
  2265. */
  2266. static int8_t get_int_pin_conf(struct bma400_int_pin_conf *int_conf, const struct bma400_dev *dev)
  2267. {
  2268. int8_t rslt;
  2269. uint8_t reg_data;
  2270. rslt = bma400_get_regs(BMA400_INT_12_IO_CTRL_ADDR, &reg_data, 1, dev);
  2271. if (rslt == BMA400_OK) {
  2272. if (int_conf->int_chan == BMA400_INT_CHANNEL_1) {
  2273. /* reading Interrupt pin configurations */
  2274. int_conf->pin_conf = BMA400_GET_BITS(reg_data, BMA400_INT_PIN1_CONF);
  2275. }
  2276. if (int_conf->int_chan == BMA400_INT_CHANNEL_2) {
  2277. /* Setting interrupt pin configurations */
  2278. int_conf->pin_conf = BMA400_GET_BITS(reg_data, BMA400_INT_PIN2_CONF);
  2279. }
  2280. }
  2281. return rslt;
  2282. }
  2283. /*!
  2284. * @brief This API is used to get the FIFO configurations
  2285. */
  2286. static int8_t get_fifo_conf(struct bma400_fifo_conf *fifo_conf, const struct bma400_dev *dev)
  2287. {
  2288. int8_t rslt;
  2289. uint8_t data_array[3];
  2290. /* Check for null pointer in the device structure */
  2291. rslt = null_ptr_check(dev);
  2292. /* Proceed if null check is fine */
  2293. if (rslt == BMA400_OK) {
  2294. /* Get the FIFO configurations and water-mark
  2295. * values from the sensor */
  2296. rslt = bma400_get_regs(BMA400_FIFO_CONFIG_0_ADDR, data_array, 3, dev);
  2297. if (rslt == BMA400_OK) {
  2298. /* Get the data of FIFO_CONFIG0 register */
  2299. fifo_conf->conf_regs = data_array[0];
  2300. /* Get the MSB of FIFO water-mark */
  2301. data_array[2] = BMA400_GET_BITS_POS_0(data_array[2], BMA400_FIFO_BYTES_CNT);
  2302. /* FIFO water-mark value is stored */
  2303. fifo_conf->fifo_watermark = ((uint16_t)data_array[2] << 8) | ((uint16_t)data_array[1]);
  2304. }
  2305. }
  2306. return rslt;
  2307. }
  2308. /*!
  2309. * @brief This API is used to set the FIFO configurations
  2310. */
  2311. static int8_t set_fifo_conf(const struct bma400_fifo_conf *fifo_conf, const struct bma400_dev *dev)
  2312. {
  2313. int8_t rslt;
  2314. uint8_t data_array[3];
  2315. uint8_t sens_data[3];
  2316. /* Check for null pointer in the device structure */
  2317. rslt = null_ptr_check(dev);
  2318. /* Proceed if null check is fine */
  2319. if (rslt == BMA400_OK) {
  2320. /* Get the FIFO configurations and water-mark
  2321. * values from the sensor */
  2322. rslt = bma400_get_regs(BMA400_FIFO_CONFIG_0_ADDR, sens_data, 3, dev);
  2323. if (rslt == BMA400_OK) {
  2324. /* FIFO configurations */
  2325. data_array[0] = fifo_conf->conf_regs;
  2326. if (fifo_conf->conf_status == BMA400_DISABLE) {
  2327. /* Disable the selected interrupt status */
  2328. data_array[0] = sens_data[0] & (~data_array[0]);
  2329. }
  2330. /* FIFO water-mark values */
  2331. data_array[1] = BMA400_GET_LSB(fifo_conf->fifo_watermark);
  2332. data_array[2] = BMA400_GET_MSB(fifo_conf->fifo_watermark);
  2333. data_array[2] = BMA400_GET_BITS_POS_0(data_array[2], BMA400_FIFO_BYTES_CNT);
  2334. if ((data_array[1] == sens_data[1]) && (data_array[2] == sens_data[2])) {
  2335. /* Set the FIFO configurations in the
  2336. * sensor excluding the watermark value */
  2337. rslt = bma400_set_regs(BMA400_FIFO_CONFIG_0_ADDR, data_array, 1, dev);
  2338. } else {
  2339. /* Set the FIFO configurations in the sensor*/
  2340. rslt = bma400_set_regs(BMA400_FIFO_CONFIG_0_ADDR, data_array, 3, dev);
  2341. }
  2342. }
  2343. }
  2344. return rslt;
  2345. }
  2346. /*!
  2347. * @brief This API is used to get the number of bytes filled in FIFO
  2348. */
  2349. static int8_t get_fifo_length(uint16_t *fifo_byte_cnt, const struct bma400_dev *dev)
  2350. {
  2351. int8_t rslt;
  2352. uint8_t data_array[2] = { 0 };
  2353. rslt = bma400_get_regs(BMA400_FIFO_LENGTH_ADDR, data_array, 2, dev);
  2354. if (rslt == BMA400_OK) {
  2355. data_array[1] = BMA400_GET_BITS_POS_0(data_array[1], BMA400_FIFO_BYTES_CNT);
  2356. /* Available data in FIFO is stored in fifo_byte_cnt*/
  2357. *fifo_byte_cnt = ((uint16_t)data_array[1] << 8) | ((uint16_t)data_array[0]);
  2358. }
  2359. return rslt;
  2360. }
  2361. /*!
  2362. * @brief This API is used to read the FIFO of BMA400
  2363. */
  2364. static int8_t read_fifo(struct bma400_fifo_data *fifo, const struct bma400_dev *dev)
  2365. {
  2366. int8_t rslt;
  2367. uint8_t reg_data;
  2368. uint8_t fifo_addr = BMA400_FIFO_DATA_ADDR;
  2369. if (dev->intf == BMA400_SPI_INTF) {
  2370. /* SPI mask is added */
  2371. fifo_addr = fifo_addr | BMA400_SPI_RD_MASK;
  2372. }
  2373. /* Read the FIFO enable bit */
  2374. rslt = bma400_get_regs(BMA400_FIFO_READ_EN_ADDR, &reg_data, 1, dev);
  2375. if (rslt == BMA400_OK) {
  2376. /* FIFO read disable bit */
  2377. if (reg_data == 0) {
  2378. /* Read FIFO Buffer since FIFO read is enabled */
  2379. rslt = dev->read(dev->intf_ptr, dev->dev_id, fifo_addr, fifo->data, fifo->length);
  2380. if (rslt != 0) {
  2381. rslt = BMA400_E_COM_FAIL;
  2382. }
  2383. } else {
  2384. /* Enable FIFO reading */
  2385. reg_data = 0;
  2386. rslt = bma400_set_regs(BMA400_FIFO_READ_EN_ADDR, &reg_data, 1, dev);
  2387. if (rslt == BMA400_OK) {
  2388. /* Delay to enable the FIFO */
  2389. dev->delay_ms(1);
  2390. /* Read FIFO Buffer since FIFO read is enabled*/
  2391. rslt = dev->read(dev->intf_ptr, dev->dev_id, fifo_addr, fifo->data, fifo->length);
  2392. if (rslt == BMA400_OK) {
  2393. /* Disable FIFO reading */
  2394. reg_data = 1;
  2395. rslt = bma400_set_regs(BMA400_FIFO_READ_EN_ADDR, &reg_data, 1, dev);
  2396. }
  2397. }
  2398. }
  2399. }
  2400. return rslt;
  2401. }
  2402. /*!
  2403. * @brief This API is used to unpack the accelerometer frames from the FIFO
  2404. */
  2405. static void unpack_accel_frame(struct bma400_fifo_data *fifo,
  2406. struct bma400_sensor_data *accel_data,
  2407. uint16_t *frame_count,
  2408. const struct bma400_dev *dev)
  2409. {
  2410. /* Frame header information is stored */
  2411. uint8_t frame_header = 0;
  2412. /* Accel data width is stored */
  2413. uint8_t accel_width;
  2414. /* Data index of the parsed byte from FIFO */
  2415. uint16_t data_index;
  2416. /* Number of accel frames parsed */
  2417. uint16_t accel_index = 0;
  2418. /* Variable to check frame availability */
  2419. uint8_t frame_available = BMA400_ENABLE;
  2420. /* Check if this is the first iteration of data unpacking
  2421. * if yes, then consider dummy byte on SPI */
  2422. if (fifo->accel_byte_start_idx == 0) {
  2423. /* Dummy byte included */
  2424. fifo->accel_byte_start_idx = dev->dummy_byte;
  2425. }
  2426. for (data_index = fifo->accel_byte_start_idx; data_index < fifo->length;) {
  2427. /*Header byte is stored in the variable frame_header*/
  2428. frame_header = fifo->data[data_index];
  2429. /* Store the Accel 8 bit or 12 bit mode */
  2430. accel_width = BMA400_GET_BITS(frame_header, BMA400_FIFO_8_BIT_EN);
  2431. /* Exclude the 8/12 bit mode data from frame header */
  2432. frame_header = frame_header & BMA400_AWIDTH_MASK;
  2433. /*Index is moved to next byte where the data is starting*/
  2434. data_index++;
  2435. switch (frame_header) {
  2436. case BMA400_FIFO_XYZ_ENABLE:
  2437. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_XYZ_ENABLE, &data_index);
  2438. if (frame_available != BMA400_DISABLE) {
  2439. /* Extract and store accel xyz data */
  2440. unpack_accel(fifo, &accel_data[accel_index], &data_index, accel_width, frame_header);
  2441. accel_index++;
  2442. }
  2443. break;
  2444. case BMA400_FIFO_X_ENABLE:
  2445. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_X_ENABLE, &data_index);
  2446. if (frame_available != BMA400_DISABLE) {
  2447. /* Extract and store accel x data */
  2448. unpack_accel(fifo, &accel_data[accel_index], &data_index, accel_width, frame_header);
  2449. accel_index++;
  2450. }
  2451. break;
  2452. case BMA400_FIFO_Y_ENABLE:
  2453. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_Y_ENABLE, &data_index);
  2454. if (frame_available != BMA400_DISABLE) {
  2455. /* Extract and store accel y data */
  2456. unpack_accel(fifo, &accel_data[accel_index], &data_index, accel_width, frame_header);
  2457. accel_index++;
  2458. }
  2459. break;
  2460. case BMA400_FIFO_Z_ENABLE:
  2461. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_Z_ENABLE, &data_index);
  2462. if (frame_available != BMA400_DISABLE) {
  2463. /* Extract and store accel z data */
  2464. unpack_accel(fifo, &accel_data[accel_index], &data_index, accel_width, frame_header);
  2465. accel_index++;
  2466. }
  2467. break;
  2468. case BMA400_FIFO_XY_ENABLE:
  2469. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_XY_ENABLE, &data_index);
  2470. if (frame_available != BMA400_DISABLE) {
  2471. /* Extract and store accel xy data */
  2472. unpack_accel(fifo, &accel_data[accel_index], &data_index, accel_width, frame_header);
  2473. accel_index++;
  2474. }
  2475. break;
  2476. case BMA400_FIFO_YZ_ENABLE:
  2477. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_YZ_ENABLE, &data_index);
  2478. if (frame_available != BMA400_DISABLE) {
  2479. /* Extract and store accel yz data */
  2480. unpack_accel(fifo, &accel_data[accel_index], &data_index, accel_width, frame_header);
  2481. accel_index++;
  2482. }
  2483. break;
  2484. case BMA400_FIFO_XZ_ENABLE:
  2485. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_YZ_ENABLE, &data_index);
  2486. if (frame_available != BMA400_DISABLE) {
  2487. /* Extract and store accel xz data */
  2488. unpack_accel(fifo, &accel_data[accel_index], &data_index, accel_width, frame_header);
  2489. accel_index++;
  2490. }
  2491. break;
  2492. case BMA400_FIFO_SENSOR_TIME:
  2493. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_SENSOR_TIME, &data_index);
  2494. if (frame_available != BMA400_DISABLE) {
  2495. /* Unpack and store the sensor time data */
  2496. unpack_sensortime_frame(fifo, &data_index);
  2497. }
  2498. break;
  2499. case BMA400_FIFO_EMPTY_FRAME:
  2500. /* Update the data index as complete */
  2501. data_index = fifo->length;
  2502. break;
  2503. case BMA400_FIFO_CONTROL_FRAME:
  2504. check_frame_available(fifo, &frame_available, accel_width, BMA400_FIFO_CONTROL_FRAME,
  2505. &data_index);
  2506. if (frame_available != BMA400_DISABLE) {
  2507. /* Store the configuration change data from FIFO */
  2508. fifo->conf_change = fifo->data[data_index++];
  2509. }
  2510. break;
  2511. default:
  2512. /* Update the data index as complete */
  2513. data_index = fifo->length;
  2514. break;
  2515. }
  2516. if (*frame_count == accel_index) {
  2517. /* Frames read completely*/
  2518. break;
  2519. }
  2520. }
  2521. /* Update the data index */
  2522. fifo->accel_byte_start_idx = data_index;
  2523. /* Update number of accel frame index */
  2524. *frame_count = accel_index;
  2525. }
  2526. /*!
  2527. * @brief This API is used to check for a frame availability in FIFO
  2528. */
  2529. static void check_frame_available(struct bma400_fifo_data *fifo,
  2530. uint8_t *frame_available,
  2531. uint8_t accel_width,
  2532. uint8_t data_en,
  2533. uint16_t *data_index)
  2534. {
  2535. switch (data_en) {
  2536. case BMA400_FIFO_XYZ_ENABLE:
  2537. /* Handling case of 12 bit/ 8 bit data available in FIFO */
  2538. if (accel_width == BMA400_12_BIT_FIFO_DATA) {
  2539. if ((*data_index + 6) > fifo->length) {
  2540. /* Partial frame available */
  2541. *data_index = fifo->length;
  2542. *frame_available = BMA400_DISABLE;
  2543. }
  2544. } else {
  2545. if ((*data_index + 3) > fifo->length) {
  2546. /* Partial frame available */
  2547. *data_index = fifo->length;
  2548. *frame_available = BMA400_DISABLE;
  2549. }
  2550. }
  2551. break;
  2552. case BMA400_FIFO_X_ENABLE:
  2553. case BMA400_FIFO_Y_ENABLE:
  2554. case BMA400_FIFO_Z_ENABLE:
  2555. /* Handling case of 12 bit/ 8 bit data available in FIFO */
  2556. if (accel_width == BMA400_12_BIT_FIFO_DATA) {
  2557. if ((*data_index + 2) > fifo->length) {
  2558. /* Partial frame available */
  2559. *data_index = fifo->length;
  2560. *frame_available = BMA400_DISABLE;
  2561. }
  2562. } else {
  2563. if ((*data_index + 1) > fifo->length) {
  2564. /* Partial frame available */
  2565. *data_index = fifo->length;
  2566. *frame_available = BMA400_DISABLE;
  2567. }
  2568. }
  2569. break;
  2570. case BMA400_FIFO_XY_ENABLE:
  2571. case BMA400_FIFO_YZ_ENABLE:
  2572. case BMA400_FIFO_XZ_ENABLE:
  2573. /* Handling case of 12 bit/ 8 bit data available in FIFO */
  2574. if (accel_width == BMA400_12_BIT_FIFO_DATA) {
  2575. if ((*data_index + 4) > fifo->length) {
  2576. /* Partial frame available */
  2577. *data_index = fifo->length;
  2578. *frame_available = BMA400_DISABLE;
  2579. }
  2580. } else {
  2581. if ((*data_index + 2) > fifo->length) {
  2582. /* Partial frame available */
  2583. *data_index = fifo->length;
  2584. *frame_available = BMA400_DISABLE;
  2585. }
  2586. }
  2587. break;
  2588. case BMA400_FIFO_SENSOR_TIME:
  2589. if ((*data_index + 3) > fifo->length) {
  2590. /* Partial frame available */
  2591. *data_index = fifo->length;
  2592. *frame_available = BMA400_DISABLE;
  2593. }
  2594. break;
  2595. case BMA400_FIFO_CONTROL_FRAME:
  2596. if ((*data_index + 1) > fifo->length) {
  2597. /* Partial frame available */
  2598. *data_index = fifo->length;
  2599. *frame_available = BMA400_DISABLE;
  2600. }
  2601. break;
  2602. default:
  2603. break;
  2604. }
  2605. }
  2606. /*!
  2607. * @brief This API is used to unpack the accelerometer xyz data from the FIFO
  2608. * and store it in the user defined buffer
  2609. */
  2610. static void unpack_accel(struct bma400_fifo_data *fifo,
  2611. struct bma400_sensor_data *accel_data,
  2612. uint16_t *data_index,
  2613. uint8_t accel_width,
  2614. uint8_t frame_header)
  2615. {
  2616. uint8_t data_lsb;
  2617. uint8_t data_msb;
  2618. /* Header information of enabled axes */
  2619. frame_header = frame_header & BMA400_FIFO_DATA_EN_MASK;
  2620. if (accel_width == BMA400_12_BIT_FIFO_DATA) {
  2621. if (frame_header & BMA400_FIFO_X_ENABLE) {
  2622. /* Accel x data */
  2623. data_lsb = fifo->data[(*data_index)++];
  2624. data_msb = fifo->data[(*data_index)++];
  2625. accel_data->x = (int16_t)(((uint16_t)(data_msb << 4)) | data_lsb);
  2626. if (accel_data->x > 2047) {
  2627. /* Computing accel x data negative value */
  2628. accel_data->x = accel_data->x - 4096;
  2629. }
  2630. } else {
  2631. /* Accel x not available */
  2632. accel_data->x = 0;
  2633. }
  2634. if (frame_header & BMA400_FIFO_Y_ENABLE) {
  2635. /* Accel y data */
  2636. data_lsb = fifo->data[(*data_index)++];
  2637. data_msb = fifo->data[(*data_index)++];
  2638. accel_data->y = (int16_t)(((uint16_t)(data_msb << 4)) | data_lsb);
  2639. if (accel_data->y > 2047) {
  2640. /* Computing accel y data negative value */
  2641. accel_data->y = accel_data->y - 4096;
  2642. }
  2643. } else {
  2644. /* Accel y not available */
  2645. accel_data->y = 0;
  2646. }
  2647. if (frame_header & BMA400_FIFO_Z_ENABLE) {
  2648. /* Accel z data */
  2649. data_lsb = fifo->data[(*data_index)++];
  2650. data_msb = fifo->data[(*data_index)++];
  2651. accel_data->z = (int16_t)(((uint16_t)(data_msb << 4)) | data_lsb);
  2652. if (accel_data->z > 2047) {
  2653. /* Computing accel z data negative value */
  2654. accel_data->z = accel_data->z - 4096;
  2655. }
  2656. } else {
  2657. /* Accel z not available */
  2658. accel_data->z = 0;
  2659. }
  2660. } else {
  2661. if (frame_header & BMA400_FIFO_X_ENABLE) {
  2662. /* Accel x data */
  2663. data_msb = fifo->data[(*data_index)++];
  2664. accel_data->x = (int16_t)((uint16_t)(data_msb << 4));
  2665. if (accel_data->x > 2047) {
  2666. /* Computing accel x data negative value */
  2667. accel_data->x = accel_data->x - 4096;
  2668. }
  2669. } else {
  2670. /* Accel x not available */
  2671. accel_data->x = 0;
  2672. }
  2673. if (frame_header & BMA400_FIFO_Y_ENABLE) {
  2674. /* Accel y data */
  2675. data_msb = fifo->data[(*data_index)++];
  2676. accel_data->y = (int16_t)((uint16_t)(data_msb << 4));
  2677. if (accel_data->y > 2047) {
  2678. /* Computing accel y data negative value */
  2679. accel_data->y = accel_data->y - 4096;
  2680. }
  2681. } else {
  2682. /* Accel y not available */
  2683. accel_data->y = 0;
  2684. }
  2685. if (frame_header & BMA400_FIFO_Z_ENABLE) {
  2686. /* Accel z data */
  2687. data_msb = fifo->data[(*data_index)++];
  2688. accel_data->z = (int16_t)((uint16_t)(data_msb << 4));
  2689. if (accel_data->z > 2047) {
  2690. /* Computing accel z data negative value */
  2691. accel_data->z = accel_data->z - 4096;
  2692. }
  2693. } else {
  2694. /* Accel z not available */
  2695. accel_data->z = 0;
  2696. }
  2697. }
  2698. }
  2699. /*!
  2700. * @brief This API is used to parse and store the sensor time from the
  2701. * FIFO data in the structure instance dev
  2702. */
  2703. static void unpack_sensortime_frame(struct bma400_fifo_data *fifo, uint16_t *data_index)
  2704. {
  2705. uint32_t time_msb;
  2706. uint16_t time_lsb;
  2707. uint8_t time_xlsb;
  2708. time_msb = fifo->data[(*data_index) + 2] << 16;
  2709. time_lsb = fifo->data[(*data_index) + 1] << 8;
  2710. time_xlsb = fifo->data[(*data_index)];
  2711. /* Sensor time */
  2712. fifo->fifo_sensor_time = (uint32_t)(time_msb | time_lsb | time_xlsb);
  2713. *data_index = (*data_index) + 3;
  2714. }
  2715. /*!
  2716. * @brief This API validates the self test results
  2717. */
  2718. static int8_t validate_accel_self_test(const struct bma400_sensor_data *accel_pos,
  2719. const struct bma400_sensor_data *accel_neg)
  2720. {
  2721. int8_t rslt;
  2722. /* Validate the results of self test
  2723. * Self test value of x,y axes should be 800mg
  2724. * and z axes should be 400 mg */
  2725. if (((accel_pos->x - accel_neg->x) > 205) && ((accel_pos->y - accel_neg->y) > 205) &&
  2726. ((accel_pos->z - accel_neg->z) > 103)) {
  2727. /* Self test pass condition */
  2728. rslt = BMA400_OK;
  2729. } else {
  2730. /* Self test failed */
  2731. rslt = BMA400_W_SELF_TEST_FAIL;
  2732. }
  2733. return rslt;
  2734. }
  2735. /*!
  2736. * @brief This API performs self test with positive excitation
  2737. */
  2738. static int8_t positive_excited_accel(struct bma400_sensor_data *accel_pos, const struct bma400_dev *dev)
  2739. {
  2740. int8_t rslt;
  2741. uint8_t reg_data = BMA400_ENABLE_POSITIVE_SELF_TEST;
  2742. /* Enable positive excitation for all 3 axes */
  2743. rslt = bma400_set_regs(BMA400_SELF_TEST_ADDR, &reg_data, 1, dev);
  2744. if (rslt == BMA400_OK) {
  2745. /* Read accel data after 50ms delay */
  2746. dev->delay_ms(BMA400_SELF_TEST_DATA_READ_MS);
  2747. rslt = bma400_get_accel_data(BMA400_DATA_ONLY, accel_pos, dev);
  2748. }
  2749. return rslt;
  2750. }
  2751. /*!
  2752. * @brief This API performs self test with negative excitation
  2753. */
  2754. static int8_t negative_excited_accel(struct bma400_sensor_data *accel_neg, const struct bma400_dev *dev)
  2755. {
  2756. int8_t rslt;
  2757. uint8_t reg_data = BMA400_ENABLE_NEGATIVE_SELF_TEST;
  2758. /* Enable negative excitation for all 3 axes */
  2759. rslt = bma400_set_regs(BMA400_SELF_TEST_ADDR, &reg_data, 1, dev);
  2760. if (rslt == BMA400_OK) {
  2761. /* Read accel data after 50ms delay */
  2762. dev->delay_ms(BMA400_SELF_TEST_DATA_READ_MS);
  2763. rslt = bma400_get_accel_data(BMA400_DATA_ONLY, accel_neg, dev);
  2764. if (rslt == BMA400_OK) {
  2765. /* Disable self test */
  2766. reg_data = BMA400_DISABLE_SELF_TEST;
  2767. rslt = bma400_set_regs(BMA400_SELF_TEST_ADDR, &reg_data, 1, dev);
  2768. }
  2769. }
  2770. return rslt;
  2771. }
  2772. /*!
  2773. * @brief This API performs the pre-requisites needed to perform the self test
  2774. */
  2775. static int8_t enable_self_test(const struct bma400_dev *dev)
  2776. {
  2777. int8_t rslt;
  2778. /* Accelerometer setting structure */
  2779. struct bma400_sensor_conf accel_setting;
  2780. /* Select the type of configuration to be modified */
  2781. accel_setting.type = BMA400_ACCEL;
  2782. /* Get the accel configurations which are set in the sensor */
  2783. rslt = bma400_get_sensor_conf(&accel_setting, 1, dev);
  2784. if (rslt == BMA400_OK) {
  2785. /* Modify to the desired configurations */
  2786. accel_setting.param.accel.odr = BMA400_ODR_100HZ;
  2787. accel_setting.param.accel.range = BMA400_8G_RANGE;
  2788. accel_setting.param.accel.osr = BMA400_ACCEL_OSR_SETTING_3;
  2789. accel_setting.param.accel.data_src = BMA400_DATA_SRC_ACCEL_FILT_1;
  2790. /* Set the desired configurations in the sensor */
  2791. rslt = bma400_set_sensor_conf(&accel_setting, 1, dev);
  2792. if (rslt == BMA400_OK) {
  2793. /* self test enabling delay */
  2794. dev->delay_ms(BMA400_SELF_TEST_DELAY_MS);
  2795. }
  2796. if (rslt == BMA400_OK) {
  2797. rslt = bma400_set_power_mode(BMA400_NORMAL_MODE, dev);
  2798. }
  2799. }
  2800. return rslt;
  2801. }