nrf_qdec.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /**
  2. * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_QDEC_H__
  41. #define NRF_QDEC_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_qdec_hal QDEC HAL
  48. * @{
  49. * @ingroup nrf_qdec
  50. * @brief Hardware access layer for managing the Quadrature Decoder (QDEC) peripheral.
  51. */
  52. /**
  53. * @enum nrf_qdec_task_t
  54. * @brief QDEC tasks.
  55. */
  56. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  57. {
  58. NRF_QDEC_TASK_START = offsetof(NRF_QDEC_Type, TASKS_START), /**< Starting the quadrature decoder. */
  59. NRF_QDEC_TASK_STOP = offsetof(NRF_QDEC_Type, TASKS_STOP), /**< Stopping the quadrature decoder. */
  60. NRF_QDEC_TASK_READCLRACC = offsetof(NRF_QDEC_Type, TASKS_READCLRACC) /**< Reading and clearing ACC and ACCDBL registers. */
  61. } nrf_qdec_task_t;
  62. /**
  63. * @enum nrf_qdec_event_t
  64. * @brief QDEC events.
  65. */
  66. typedef enum
  67. {
  68. NRF_QDEC_EVENT_SAMPLERDY = offsetof(NRF_QDEC_Type, EVENTS_SAMPLERDY), /**< Event generated for every new sample. */
  69. NRF_QDEC_EVENT_REPORTRDY = offsetof(NRF_QDEC_Type, EVENTS_REPORTRDY), /**< Event generated for every new report. */
  70. NRF_QDEC_EVENT_ACCOF = offsetof(NRF_QDEC_Type, EVENTS_ACCOF) /**< Event generated for every accumulator overflow. */
  71. } nrf_qdec_event_t; /*lint -restore */
  72. /**
  73. * @enum nrf_qdec_short_mask_t
  74. * @brief QDEC shortcuts.
  75. */
  76. typedef enum
  77. {
  78. NRF_QDEC_SHORT_REPORTRDY_READCLRACC_MASK = QDEC_SHORTS_REPORTRDY_READCLRACC_Msk, /**< Shortcut between REPORTRDY event and READCLRACC task. */
  79. NRF_QDEC_SHORT_SAMPLERDY_STOP_MASK = QDEC_SHORTS_SAMPLERDY_STOP_Msk /**< Shortcut between SAMPLERDY event and STOP task. */
  80. } nrf_qdec_short_mask_t;
  81. /**
  82. * @enum nrf_qdec_int_mask_t
  83. * @brief QDEC interrupts.
  84. */
  85. typedef enum
  86. {
  87. NRF_QDEC_INT_SAMPLERDY_MASK = QDEC_INTENSET_SAMPLERDY_Msk, /**< Mask for enabling or disabling an interrupt on SAMPLERDY event. */
  88. NRF_QDEC_INT_REPORTRDY_MASK = QDEC_INTENSET_REPORTRDY_Msk, /**< Mask for enabling or disabling an interrupt on REPORTRDY event. */
  89. NRF_QDEC_INT_ACCOF_MASK = QDEC_INTENSET_ACCOF_Msk /**< Mask for enabling or disabling an interrupt on ACCOF event. */
  90. } nrf_qdec_int_mask_t;
  91. /**
  92. * @enum nrf_qdec_enable_t
  93. * @brief States of the enable bit.
  94. */
  95. typedef enum
  96. {
  97. NRF_QDEC_DISABLE = QDEC_ENABLE_ENABLE_Disabled, /**< Mask for disabling the QDEC periperal. When disabled, the QDEC decoder pins are not active. */
  98. NRF_QDEC_ENABLE = QDEC_ENABLE_ENABLE_Enabled /**< Mask for enabling the QDEC periperal. When enabled, the QDEC pins are active. */
  99. } nrf_qdec_enable_t;
  100. /**
  101. * @enum nrf_qdec_dbfen_t
  102. * @brief States of the debounce filter enable bit.
  103. */
  104. typedef enum
  105. {
  106. NRF_QDEC_DBFEN_DISABLE = QDEC_DBFEN_DBFEN_Disabled, /**< Mask for disabling the debounce filter. */
  107. NRF_QDEC_DBFEN_ENABLE = QDEC_DBFEN_DBFEN_Enabled /**< Mask for enabling the debounce filter. */
  108. } nrf_qdec_dbfen_t;
  109. /**
  110. * @enum nrf_qdec_ledpol_t
  111. * @brief Active LED polarity.
  112. */
  113. typedef enum
  114. {
  115. NRF_QDEC_LEPOL_ACTIVE_LOW = QDEC_LEDPOL_LEDPOL_ActiveLow, /**< QDEC LED active on output pin low. */
  116. NRF_QDEC_LEPOL_ACTIVE_HIGH = QDEC_LEDPOL_LEDPOL_ActiveHigh /**< QDEC LED active on output pin high. */
  117. } nrf_qdec_ledpol_t;
  118. /**
  119. * @enum nrf_qdec_sampleper_t
  120. * @brief Available sampling periods.
  121. */
  122. typedef enum
  123. {
  124. NRF_QDEC_SAMPLEPER_128us = QDEC_SAMPLEPER_SAMPLEPER_128us, /**< QDEC sampling period 128 microseconds. */
  125. NRF_QDEC_SAMPLEPER_256us = QDEC_SAMPLEPER_SAMPLEPER_256us, /**< QDEC sampling period 256 microseconds. */
  126. NRF_QDEC_SAMPLEPER_512us = QDEC_SAMPLEPER_SAMPLEPER_512us, /**< QDEC sampling period 512 microseconds. */
  127. NRF_QDEC_SAMPLEPER_1024us = QDEC_SAMPLEPER_SAMPLEPER_1024us, /**< QDEC sampling period 1024 microseconds. */
  128. NRF_QDEC_SAMPLEPER_2048us = QDEC_SAMPLEPER_SAMPLEPER_2048us, /**< QDEC sampling period 2048 microseconds. */
  129. NRF_QDEC_SAMPLEPER_4096us = QDEC_SAMPLEPER_SAMPLEPER_4096us, /**< QDEC sampling period 4096 microseconds. */
  130. NRF_QDEC_SAMPLEPER_8192us = QDEC_SAMPLEPER_SAMPLEPER_8192us, /**< QDEC sampling period 8192 microseconds. */
  131. NRF_QDEC_SAMPLEPER_16384us = QDEC_SAMPLEPER_SAMPLEPER_16384us /**< QDEC sampling period 16384 microseconds. */
  132. } nrf_qdec_sampleper_t;
  133. /**
  134. * @enum nrf_qdec_reportper_t
  135. * @brief Available report periods.
  136. */
  137. typedef enum
  138. {
  139. NRF_QDEC_REPORTPER_10 = QDEC_REPORTPER_REPORTPER_10Smpl, /**< QDEC report period 10 samples. */
  140. NRF_QDEC_REPORTPER_40 = QDEC_REPORTPER_REPORTPER_40Smpl, /**< QDEC report period 40 samples. */
  141. NRF_QDEC_REPORTPER_80 = QDEC_REPORTPER_REPORTPER_80Smpl, /**< QDEC report period 80 samples. */
  142. NRF_QDEC_REPORTPER_120 = QDEC_REPORTPER_REPORTPER_120Smpl, /**< QDEC report period 120 samples. */
  143. NRF_QDEC_REPORTPER_160 = QDEC_REPORTPER_REPORTPER_160Smpl, /**< QDEC report period 160 samples. */
  144. NRF_QDEC_REPORTPER_200 = QDEC_REPORTPER_REPORTPER_200Smpl, /**< QDEC report period 200 samples. */
  145. NRF_QDEC_REPORTPER_240 = QDEC_REPORTPER_REPORTPER_240Smpl, /**< QDEC report period 240 samples. */
  146. NRF_QDEC_REPORTPER_280 = QDEC_REPORTPER_REPORTPER_280Smpl, /**< QDEC report period 280 samples. */
  147. NRF_QDEC_REPORTPER_DISABLED /**< QDEC reporting disabled. */
  148. } nrf_qdec_reportper_t;
  149. /**
  150. * @brief Function for enabling QDEC.
  151. */
  152. __STATIC_INLINE void nrf_qdec_enable(void)
  153. {
  154. NRF_QDEC->ENABLE = NRF_QDEC_ENABLE;
  155. }
  156. /**
  157. * @brief Function for disabling QDEC.
  158. */
  159. __STATIC_INLINE void nrf_qdec_disable(void)
  160. {
  161. NRF_QDEC->ENABLE = NRF_QDEC_DISABLE;
  162. }
  163. /**
  164. * @brief Function for returning the enable state of QDEC.
  165. * @return State of the register.
  166. */
  167. __STATIC_INLINE uint32_t nrf_qdec_enable_get(void)
  168. {
  169. return NRF_QDEC->ENABLE;
  170. }
  171. /**
  172. * @brief Function for enabling QDEC interrupts by mask.
  173. * @param[in] qdec_int_mask Sources of the interrupts to enable.
  174. */
  175. __STATIC_INLINE void nrf_qdec_int_enable(uint32_t qdec_int_mask)
  176. {
  177. NRF_QDEC->INTENSET = qdec_int_mask; // writing 0 has no effect
  178. }
  179. /**
  180. * @brief Function for disabling QDEC interrupts by mask.
  181. * @param[in] qdec_int_mask Sources of the interrupts to disable.
  182. *
  183. */
  184. __STATIC_INLINE void nrf_qdec_int_disable(uint32_t qdec_int_mask)
  185. {
  186. NRF_QDEC->INTENCLR = qdec_int_mask; // writing 0 has no effect
  187. }
  188. /**
  189. * @brief Function for getting the enabled interrupts of the QDEC.
  190. */
  191. __STATIC_INLINE uint32_t nrf_qdec_int_enable_check(nrf_qdec_int_mask_t qdec_int_mask)
  192. {
  193. return NRF_QDEC->INTENSET & qdec_int_mask; // when read this register will return the value of INTEN.
  194. }
  195. /**
  196. * @brief Function for enabling the debouncing filter of the QED.
  197. */
  198. __STATIC_INLINE void nrf_qdec_dbfen_enable(void)
  199. {
  200. NRF_QDEC->DBFEN = NRF_QDEC_DBFEN_ENABLE;
  201. }
  202. /**
  203. * @brief Function for disabling the debouncing filter of the QED.
  204. */
  205. __STATIC_INLINE void nrf_qdec_dbfen_disable(void)
  206. {
  207. NRF_QDEC->DBFEN = NRF_QDEC_DBFEN_DISABLE;
  208. }
  209. /**
  210. * @brief Function for getting the state of the QDEC's debouncing filter.
  211. * @retval NRF_QDEC_DBFEN_DISABLE If the debouncing filter is disabled.
  212. * @retval NRF_QDEC_DBFEN_ENABLE If the debouncing filter is enabled.
  213. */
  214. __STATIC_INLINE uint32_t nrf_qdec_dbfen_get(void)
  215. {
  216. return NRF_QDEC->DBFEN;
  217. }
  218. /**
  219. * @brief Function for assigning QDEC pins.
  220. * @param[in] psela Pin number.
  221. * @param[in] pselb Pin number.
  222. * @param[in] pselled Pin number.
  223. */
  224. __STATIC_INLINE void nrf_qdec_pio_assign( uint32_t psela, uint32_t pselb, uint32_t pselled)
  225. {
  226. NRF_QDEC->PSELA = psela;
  227. NRF_QDEC->PSELB = pselb;
  228. NRF_QDEC->PSELLED = pselled;
  229. }
  230. /**
  231. * @brief Function for setting a specific QDEC task.
  232. * @param[in] qdec_task QDEC task to be set.
  233. */
  234. __STATIC_INLINE void nrf_qdec_task_trigger(nrf_qdec_task_t qdec_task)
  235. {
  236. *( (volatile uint32_t *)( (uint8_t *)NRF_QDEC + qdec_task) ) = 1;
  237. }
  238. /**
  239. * @brief Function for retrieving the address of a QDEC task register.
  240. * @param[in] qdec_task QDEC task.
  241. */
  242. __STATIC_INLINE uint32_t * nrf_qdec_task_address_get(nrf_qdec_task_t qdec_task)
  243. {
  244. return (uint32_t *)( (uint8_t *)NRF_QDEC + qdec_task);
  245. }
  246. /**
  247. * @brief Function for clearing a specific QDEC event.
  248. * @param[in] qdec_event QDEC event to clear.
  249. */
  250. __STATIC_INLINE void nrf_qdec_event_clear(nrf_qdec_event_t qdec_event)
  251. {
  252. *( (volatile uint32_t *)( (uint8_t *)NRF_QDEC + qdec_event) ) = 0;
  253. #if __CORTEX_M == 0x04
  254. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_QDEC + qdec_event));
  255. (void)dummy;
  256. #endif
  257. }
  258. /**
  259. * @brief Function for retrieving the state of a specific QDEC event.
  260. * @return State of the QDEC event.
  261. */
  262. __STATIC_INLINE uint32_t nrf_qdec_event_check(nrf_qdec_event_t qdec_event)
  263. {
  264. return *(volatile uint32_t *)( (uint8_t *)NRF_QDEC + qdec_event);
  265. }
  266. /**
  267. * @brief Function for retrieving the address of a specific QDEC event register.
  268. * @param[in] qdec_event QDEC event.
  269. * @return Address of the specified QDEC event.
  270. */
  271. __STATIC_INLINE uint32_t * nrf_qdec_event_address_get(nrf_qdec_event_t qdec_event)
  272. {
  273. return (uint32_t *)( (uint8_t *)NRF_QDEC + qdec_event);
  274. }
  275. /**
  276. * @brief Function for setting QDEC shortcuts.
  277. * @param[in] qdec_short_mask QDEC shortcut by mask.
  278. */
  279. __STATIC_INLINE void nrf_qdec_shorts_enable(uint32_t qdec_short_mask)
  280. {
  281. NRF_QDEC->SHORTS |= qdec_short_mask;
  282. }
  283. /**
  284. * @brief Function for clearing shortcuts of the QDEC by mask.
  285. * @param[in] qdec_short_mask QDEC shortcute to be cleared.
  286. */
  287. __STATIC_INLINE void nrf_qdec_shorts_disable(uint32_t qdec_short_mask)
  288. {
  289. NRF_QDEC->SHORTS &= ~qdec_short_mask;
  290. }
  291. /**
  292. * @brief Function for retrieving the value of QDEC's SAMPLEPER register.
  293. * @return Value of the SAMPLEPER register.
  294. */
  295. __STATIC_INLINE int32_t nrf_qdec_sampleper_reg_get(void)
  296. {
  297. return NRF_QDEC->SAMPLEPER;
  298. }
  299. /**
  300. * @brief Function for converting the value of QDEC's SAMPLE PERIOD to microseconds.
  301. * @retval sampling period in microseconds.
  302. */
  303. __STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(uint32_t sampleper)
  304. {
  305. return (1 << (7 + sampleper));
  306. }
  307. /**
  308. * @brief Function for setting the value of QDEC's SAMPLEPER register.
  309. * @param[in] sample_per Sampling period.
  310. */
  311. __STATIC_INLINE void nrf_qdec_sampleper_set(nrf_qdec_sampleper_t sample_per)
  312. {
  313. NRF_QDEC->SAMPLEPER = sample_per;
  314. }
  315. /**
  316. * @brief Function for retrieving the value of QDEC's SAMPLE register.
  317. * @return Value of the SAMPLE register.
  318. */
  319. __STATIC_INLINE int32_t nrf_qdec_sample_get(void)
  320. {
  321. return NRF_QDEC->SAMPLE;
  322. }
  323. /**
  324. * @brief Function for retrieving the value of QDEC's ACC register.
  325. * @return Value of the ACC register.
  326. */
  327. __STATIC_INLINE int32_t nrf_qdec_acc_get(void)
  328. {
  329. return NRF_QDEC->ACC;
  330. }
  331. /**
  332. * @brief Function for retrieving the value of QDEC's ACCREAD register.
  333. * @return Value of the ACCREAD register.
  334. */
  335. __STATIC_INLINE int32_t nrf_qdec_accread_get(void)
  336. {
  337. return NRF_QDEC->ACCREAD;
  338. }
  339. /**
  340. * @brief Function for retrieving the value of QDEC's ACCDBL register.
  341. * @return Value of the ACCDBL register.
  342. */
  343. __STATIC_INLINE uint32_t nrf_qdec_accdbl_get(void)
  344. {
  345. return NRF_QDEC->ACCDBL;
  346. }
  347. /**
  348. * @brief Function for retrieving the value of QDEC's ACCDBLREAD register.
  349. * @return Value of the ACCDBLREAD register.
  350. */
  351. __STATIC_INLINE uint32_t nrf_qdec_accdblread_get(void)
  352. {
  353. return NRF_QDEC->ACCDBLREAD;
  354. }
  355. /**
  356. * @brief Function for setting how long the LED is switched on before sampling.
  357. * @param[in] time_us Time (in microseconds) how long the LED is switched on before sampling.
  358. */
  359. __STATIC_INLINE void nrf_qdec_ledpre_set(uint32_t time_us)
  360. {
  361. NRF_QDEC->LEDPRE = time_us;
  362. }
  363. /**
  364. * @brief Function for retrieving how long the LED is switched on before sampling.
  365. * @retval time_us Time (in microseconds) how long the LED is switched on before sampling.
  366. */
  367. __STATIC_INLINE uint32_t nrf_qdec_ledpre_get(void)
  368. {
  369. return NRF_QDEC->LEDPRE;
  370. }
  371. /**
  372. * @brief Function for setting the report period (in samples).
  373. * @param[in] reportper Number of samples.
  374. */
  375. __STATIC_INLINE void nrf_qdec_reportper_set(nrf_qdec_reportper_t reportper)
  376. {
  377. NRF_QDEC->REPORTPER = reportper;
  378. }
  379. /**
  380. * @brief Function for retrieving the report period.
  381. * @retval reportper Number of samples as encoded in the register.
  382. */
  383. __STATIC_INLINE uint32_t nrf_qdec_reportper_reg_get(void)
  384. {
  385. return NRF_QDEC->REPORTPER;
  386. }
  387. /**
  388. * @brief Function for retrieving the value of QDEC's SAMPLEPER register.
  389. * @param [in] reportper Reportper to be converted to amount of samples per report.
  390. */
  391. __STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(uint32_t reportper)
  392. {
  393. return (reportper == NRF_QDEC_REPORTPER_10) ? 10 : reportper * 40;
  394. }
  395. /**
  396. * @brief Function for setting the active level for the LED.
  397. * @param[in] pol Active level for the LED.
  398. */
  399. __STATIC_INLINE void nrf_qdec_ledpol_set(nrf_qdec_ledpol_t pol)
  400. {
  401. NRF_QDEC->LEDPOL = pol;
  402. }
  403. /**
  404. * @brief Function for retrieving the active level for the LED.
  405. * @return Active level for the LED.
  406. */
  407. __STATIC_INLINE uint32_t nrf_qdec_ledpol_get(void)
  408. {
  409. return NRF_QDEC->LEDPOL;
  410. }
  411. /** @} */
  412. #ifdef __cplusplus
  413. }
  414. #endif
  415. #endif