nrf_qdec.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_QDEC_H__
  13. #define NRF_QDEC_H__
  14. #include <stddef.h>
  15. #include "nrf_error.h"
  16. #include "nrf.h"
  17. /*lint ++flb "Enter library region" */
  18. /**
  19. * @defgroup nrf_qdec_hal QDEC HAL
  20. * @{
  21. * @ingroup nrf_qdec
  22. * @brief Hardware access layer for accessing the quadrature decoder (QDEC) peripheral.
  23. */
  24. /**
  25. * @enum nrf_qdec_task_t
  26. * @brief QDEC tasks.
  27. */
  28. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  29. {
  30. NRF_QDEC_TASK_START = offsetof(NRF_QDEC_Type, TASKS_START), /**< Starting the quadrature decoder. */
  31. NRF_QDEC_TASK_STOP = offsetof(NRF_QDEC_Type, TASKS_STOP), /**< Stopping the quadrature decoder. */
  32. NRF_QDEC_TASK_READCLRACC = offsetof(NRF_QDEC_Type, TASKS_READCLRACC) /**< Reading and clearing ACC and ACCDBL registers. */
  33. } nrf_qdec_task_t;
  34. /**
  35. * @enum nrf_qdec_event_t
  36. * @brief QDEC events.
  37. */
  38. typedef enum
  39. {
  40. NRF_QDEC_EVENT_SAMPLERDY = offsetof(NRF_QDEC_Type, EVENTS_SAMPLERDY), /**< Event generated for every new sample. */
  41. NRF_QDEC_EVENT_REPORTRDY = offsetof(NRF_QDEC_Type, EVENTS_REPORTRDY), /**< Event generated for every new report. */
  42. NRF_QDEC_EVENT_ACCOF = offsetof(NRF_QDEC_Type, EVENTS_ACCOF) /**< Event generated for every accumulator overflow. */
  43. } nrf_qdec_event_t; /*lint -restore */
  44. /**
  45. * @enum nrf_qdec_short_mask_t
  46. * @brief QDEC shortcuts.
  47. */
  48. typedef enum
  49. {
  50. NRF_QDEC_SHORT_REPORTRDY_READCLRACC_MASK = QDEC_SHORTS_REPORTRDY_READCLRACC_Msk, /**< Shortcut between REPORTRDY event and READCLRACC task. */
  51. NRF_QDEC_SHORT_SAMPLERDY_STOP_MASK = QDEC_SHORTS_SAMPLERDY_STOP_Msk /**< Shortcut between SAMPLERDY event and STOP task. */
  52. } nrf_qdec_short_mask_t;
  53. /**
  54. * @enum nrf_qdec_int_mask_t
  55. * @brief QDEC interrupts.
  56. */
  57. typedef enum
  58. {
  59. NRF_QDEC_INT_SAMPLERDY_MASK = QDEC_INTENSET_SAMPLERDY_Msk, /**< Mask for enabling or disabling an interrupt on SAMPLERDY event. */
  60. NRF_QDEC_INT_REPORTRDY_MASK = QDEC_INTENSET_REPORTRDY_Msk, /**< Mask for enabling or disabling an interrupt on REPORTRDY event. */
  61. NRF_QDEC_INT_ACCOF_MASK = QDEC_INTENSET_ACCOF_Msk /**< Mask for enabling or disabling an interrupt on ACCOF event. */
  62. } nrf_qdec_int_mask_t;
  63. /**
  64. * @enum nrf_qdec_enable_t
  65. * @brief States of the enable bit.
  66. */
  67. typedef enum
  68. {
  69. NRF_QDEC_DISABLE = QDEC_ENABLE_ENABLE_Disabled, /**< Mask for disabling the QDEC periperal. When disabled, the QDEC decoder pins are not active. */
  70. NRF_QDEC_ENABLE = QDEC_ENABLE_ENABLE_Enabled /**< Mask for enabling the QDEC periperal. When enabled, the QDEC pins are active. */
  71. } nrf_qdec_enable_t;
  72. /**
  73. * @enum nrf_qdec_dbfen_t
  74. * @brief States of the debounce filter enable bit.
  75. */
  76. typedef enum
  77. {
  78. NRF_QDEC_DBFEN_DISABLE = QDEC_DBFEN_DBFEN_Disabled, /**< Mask for disabling the debounce filter. */
  79. NRF_QDEC_DBFEN_ENABLE = QDEC_DBFEN_DBFEN_Enabled /**< Mask for enabling the debounce filter. */
  80. } nrf_qdec_dbfen_t;
  81. /**
  82. * @enum nrf_qdec_ledpol_t
  83. * @brief Active LED polarity.
  84. */
  85. typedef enum
  86. {
  87. NRF_QDEC_LEPOL_ACTIVE_LOW = QDEC_LEDPOL_LEDPOL_ActiveLow, /**< QDEC LED active on output pin low. */
  88. NRF_QDEC_LEPOL_ACTIVE_HIGH = QDEC_LEDPOL_LEDPOL_ActiveHigh /**< QDEC LED active on output pin high. */
  89. } nrf_qdec_ledpol_t;
  90. /**
  91. * @enum nrf_qdec_sampleper_t
  92. * @brief Available sampling periods.
  93. */
  94. typedef enum
  95. {
  96. NRF_QDEC_SAMPLEPER_128us = QDEC_SAMPLEPER_SAMPLEPER_128us, /**< QDEC sampling period 128 microseconds. */
  97. NRF_QDEC_SAMPLEPER_256us = QDEC_SAMPLEPER_SAMPLEPER_256us, /**< QDEC sampling period 256 microseconds. */
  98. NRF_QDEC_SAMPLEPER_512us = QDEC_SAMPLEPER_SAMPLEPER_512us, /**< QDEC sampling period 512 microseconds. */
  99. NRF_QDEC_SAMPLEPER_1024us = QDEC_SAMPLEPER_SAMPLEPER_1024us, /**< QDEC sampling period 1024 microseconds. */
  100. NRF_QDEC_SAMPLEPER_2048us = QDEC_SAMPLEPER_SAMPLEPER_2048us, /**< QDEC sampling period 2048 microseconds. */
  101. NRF_QDEC_SAMPLEPER_4096us = QDEC_SAMPLEPER_SAMPLEPER_4096us, /**< QDEC sampling period 4096 microseconds. */
  102. NRF_QDEC_SAMPLEPER_8192us = QDEC_SAMPLEPER_SAMPLEPER_8192us, /**< QDEC sampling period 8192 microseconds. */
  103. NRF_QDEC_SAMPLEPER_16384us = QDEC_SAMPLEPER_SAMPLEPER_16384us /**< QDEC sampling period 16384 microseconds. */
  104. } nrf_qdec_sampleper_t;
  105. /**
  106. * @enum nrf_qdec_reportper_t
  107. * @brief Available report periods.
  108. */
  109. typedef enum
  110. {
  111. NRF_QDEC_REPORTPER_10 = QDEC_REPORTPER_REPORTPER_10Smpl, /**< QDEC report period 10 samples. */
  112. NRF_QDEC_REPORTPER_40 = QDEC_REPORTPER_REPORTPER_40Smpl, /**< QDEC report period 40 samples. */
  113. NRF_QDEC_REPORTPER_80 = QDEC_REPORTPER_REPORTPER_80Smpl, /**< QDEC report period 80 samples. */
  114. NRF_QDEC_REPORTPER_120 = QDEC_REPORTPER_REPORTPER_120Smpl, /**< QDEC report period 120 samples. */
  115. NRF_QDEC_REPORTPER_160 = QDEC_REPORTPER_REPORTPER_160Smpl, /**< QDEC report period 160 samples. */
  116. NRF_QDEC_REPORTPER_200 = QDEC_REPORTPER_REPORTPER_200Smpl, /**< QDEC report period 200 samples. */
  117. NRF_QDEC_REPORTPER_240 = QDEC_REPORTPER_REPORTPER_240Smpl, /**< QDEC report period 240 samples. */
  118. NRF_QDEC_REPORTPER_280 = QDEC_REPORTPER_REPORTPER_280Smpl, /**< QDEC report period 280 samples. */
  119. NRF_QDEC_REPORTPER_DISABLED /**< QDEC reporting disabled. */
  120. } nrf_qdec_reportper_t;
  121. /**
  122. * @brief Function for enabling QDEC.
  123. */
  124. __STATIC_INLINE void nrf_qdec_enable(void)
  125. {
  126. NRF_QDEC->ENABLE = NRF_QDEC_ENABLE;
  127. }
  128. /**
  129. * @brief Function for disabling QDEC.
  130. */
  131. __STATIC_INLINE void nrf_qdec_disable(void)
  132. {
  133. NRF_QDEC->ENABLE = NRF_QDEC_DISABLE;
  134. }
  135. /**
  136. * @brief Function for returning the enable state of QDEC.
  137. * @return State of the register.
  138. */
  139. __STATIC_INLINE uint32_t nrf_qdec_enable_get(void)
  140. {
  141. return NRF_QDEC->ENABLE;
  142. }
  143. /**
  144. * @brief Function for enabling QDEC interrupts by mask.
  145. * @param[in] qdec_int_mask Sources of the interrupts to enable.
  146. */
  147. __STATIC_INLINE void nrf_qdec_int_enable(uint32_t qdec_int_mask)
  148. {
  149. NRF_QDEC->INTENSET = qdec_int_mask; // writing 0 has no effect
  150. }
  151. /**
  152. * @brief Function for disabling QDEC interrupts by mask.
  153. * @param[in] qdec_int_mask Sources of the interrupts to disable.
  154. *
  155. */
  156. __STATIC_INLINE void nrf_qdec_int_disable(uint32_t qdec_int_mask)
  157. {
  158. NRF_QDEC->INTENCLR = qdec_int_mask; // writing 0 has no effect
  159. }
  160. /**
  161. * @brief Function for getting the enabled interrupts of the QDEC.
  162. */
  163. __STATIC_INLINE uint32_t nrf_qdec_int_enable_check(nrf_qdec_int_mask_t qdec_int_mask)
  164. {
  165. return NRF_QDEC->INTENSET & qdec_int_mask; // when read this register will return the value of INTEN.
  166. }
  167. /**
  168. * @brief Function for enabling the debouncing filter of the QED.
  169. */
  170. __STATIC_INLINE void nrf_qdec_dbfen_enable(void)
  171. {
  172. NRF_QDEC->DBFEN = NRF_QDEC_DBFEN_ENABLE;
  173. }
  174. /**
  175. * @brief Function for disabling the debouncing filter of the QED.
  176. */
  177. __STATIC_INLINE void nrf_qdec_dbfen_disable(void)
  178. {
  179. NRF_QDEC->DBFEN = NRF_QDEC_DBFEN_DISABLE;
  180. }
  181. /**
  182. * @brief Function for getting the state of the QDEC's debouncing filter.
  183. * @retval NRF_QDEC_DBFEN_DISABLE If the debouncing filter is disabled.
  184. * @retval NRF_QDEC_DBFEN_ENABLE If the debouncing filter is enabled.
  185. */
  186. __STATIC_INLINE uint32_t nrf_qdec_dbfen_get(void)
  187. {
  188. return NRF_QDEC->DBFEN;
  189. }
  190. /**
  191. * @brief Function for assigning QDEC pins.
  192. * @param[in] psela Pin number.
  193. * @param[in] pselb Pin number.
  194. * @param[in] pselled Pin number.
  195. */
  196. __STATIC_INLINE void nrf_qdec_pio_assign( uint32_t psela, uint32_t pselb, uint32_t pselled)
  197. {
  198. #ifdef NRF51
  199. NRF_QDEC->PSELA = psela;
  200. NRF_QDEC->PSELB = pselb;
  201. NRF_QDEC->PSELLED = pselled;
  202. #elif defined NRF52
  203. NRF_QDEC->PSEL.A = psela;
  204. NRF_QDEC->PSEL.B = pselb;
  205. NRF_QDEC->PSEL.LED = pselled;
  206. #endif
  207. }
  208. /**
  209. * @brief Function for setting a specific QDEC task.
  210. * @param[in] qdec_task QDEC task to be set.
  211. */
  212. __STATIC_INLINE void nrf_qdec_task_trigger(nrf_qdec_task_t qdec_task)
  213. {
  214. *( (volatile uint32_t *)( (uint8_t *)NRF_QDEC + qdec_task) ) = 1;
  215. }
  216. /**
  217. * @brief Function for retrieving the address of a QDEC task register.
  218. * @param[in] qdec_task QDEC task.
  219. */
  220. __STATIC_INLINE uint32_t * nrf_qdec_task_address_get(nrf_qdec_task_t qdec_task)
  221. {
  222. return (uint32_t *)( (uint8_t *)NRF_QDEC + qdec_task);
  223. }
  224. /**
  225. * @brief Function for clearing a specific QDEC event.
  226. * @param[in] qdec_event QDEC event to clear.
  227. */
  228. __STATIC_INLINE void nrf_qdec_event_clear(nrf_qdec_event_t qdec_event)
  229. {
  230. *( (volatile uint32_t *)( (uint8_t *)NRF_QDEC + qdec_event) ) = 0;
  231. }
  232. /**
  233. * @brief Function for retrieving the state of a specific QDEC event.
  234. * @return State of the QDEC event.
  235. */
  236. __STATIC_INLINE uint32_t nrf_qdec_event_check(nrf_qdec_event_t qdec_event)
  237. {
  238. return *(volatile uint32_t *)( (uint8_t *)NRF_QDEC + qdec_event);
  239. }
  240. /**
  241. * @brief Function for retrieving the address of a specific QDEC event register.
  242. * @param[in] qdec_event QDEC event.
  243. * @return Address of the specified QDEC event.
  244. */
  245. __STATIC_INLINE uint32_t * nrf_qdec_event_address_get(nrf_qdec_event_t qdec_event)
  246. {
  247. return (uint32_t *)( (uint8_t *)NRF_QDEC + qdec_event);
  248. }
  249. /**
  250. * @brief Function for setting QDEC shortcuts.
  251. * @param[in] qdec_short_mask QDEC shortcut by mask.
  252. */
  253. __STATIC_INLINE void nrf_qdec_shorts_enable(uint32_t qdec_short_mask)
  254. {
  255. NRF_QDEC->SHORTS |= qdec_short_mask;
  256. }
  257. /**
  258. * @brief Function for clearing shortcuts of the QDEC by mask.
  259. * @param[in] qdec_short_mask QDEC shortcute to be cleared.
  260. */
  261. __STATIC_INLINE void nrf_qdec_shorts_disable(uint32_t qdec_short_mask)
  262. {
  263. NRF_QDEC->SHORTS &= ~qdec_short_mask;
  264. }
  265. /**
  266. * @brief Function for retrieving the value of QDEC's SAMPLEPER register.
  267. * @return Value of the SAMPLEPER register.
  268. */
  269. __STATIC_INLINE int32_t nrf_qdec_sampleper_reg_get(void)
  270. {
  271. return NRF_QDEC->SAMPLEPER;
  272. }
  273. /**
  274. * @brief Function for converting the value of QDEC's SAMPLE PERIOD to microseconds.
  275. * @retval sampling period in microseconds.
  276. */
  277. __STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(uint32_t sampleper)
  278. {
  279. return (1 << (7+sampleper));
  280. }
  281. /**
  282. * @brief Function for setting the value of QDEC's SAMPLEPER register.
  283. * @param[in] sample_per Sampling period.
  284. */
  285. __STATIC_INLINE void nrf_qdec_sampleper_set(nrf_qdec_sampleper_t sample_per)
  286. {
  287. NRF_QDEC->SAMPLEPER = sample_per;
  288. }
  289. /**
  290. * @brief Function for retrieving the value of QDEC's SAMPLE register.
  291. * @return Value of the SAMPLE register.
  292. */
  293. __STATIC_INLINE int32_t nrf_qdec_sample_get(void)
  294. {
  295. return NRF_QDEC->SAMPLE;
  296. }
  297. /**
  298. * @brief Function for retrieving the value of QDEC's ACC register.
  299. * @return Value of the ACC register.
  300. */
  301. __STATIC_INLINE int32_t nrf_qdec_acc_get(void)
  302. {
  303. return NRF_QDEC->ACC;
  304. }
  305. /**
  306. * @brief Function for retrieving the value of QDEC's ACCREAD register.
  307. * @return Value of the ACCREAD register.
  308. */
  309. __STATIC_INLINE int32_t nrf_qdec_accread_get(void)
  310. {
  311. return NRF_QDEC->ACCREAD;
  312. }
  313. /**
  314. * @brief Function for retrieving the value of QDEC's ACCDBL register.
  315. * @return Value of the ACCDBL register.
  316. */
  317. __STATIC_INLINE uint32_t nrf_qdec_accdbl_get(void)
  318. {
  319. return NRF_QDEC->ACCDBL;
  320. }
  321. /**
  322. * @brief Function for retrieving the value of QDEC's ACCDBLREAD register.
  323. * @return Value of the ACCDBLREAD register.
  324. */
  325. __STATIC_INLINE uint32_t nrf_qdec_accdblread_get(void)
  326. {
  327. return NRF_QDEC->ACCDBLREAD;
  328. }
  329. /**
  330. * @brief Function for setting how long the LED is switched on before sampling.
  331. * @param[in] time_us Time (in microseconds) how long the LED is switched on before sampling.
  332. */
  333. __STATIC_INLINE void nrf_qdec_ledpre_set(uint32_t time_us)
  334. {
  335. NRF_QDEC->LEDPRE = time_us;
  336. }
  337. /**
  338. * @brief Function for retrieving how long the LED is switched on before sampling.
  339. * @retval time_us Time (in microseconds) how long the LED is switched on before sampling.
  340. */
  341. __STATIC_INLINE uint32_t nrf_qdec_ledpre_get(void)
  342. {
  343. return NRF_QDEC->LEDPRE;
  344. }
  345. /**
  346. * @brief Function for setting the report period (in samples).
  347. * @param[in] reportper Number of samples.
  348. */
  349. __STATIC_INLINE void nrf_qdec_reportper_set(nrf_qdec_reportper_t reportper)
  350. {
  351. NRF_QDEC->REPORTPER = reportper;
  352. }
  353. /**
  354. * @brief Function for retrieving the report period.
  355. * @retval reportper Number of samples as encoded in the register.
  356. */
  357. __STATIC_INLINE uint32_t nrf_qdec_reportper_reg_get(void)
  358. {
  359. return NRF_QDEC->REPORTPER;
  360. }
  361. /**
  362. * @brief Function for retrieving the value of QDEC's SAMPLEPER register.
  363. * @param [in] reportper Reportper to be converted to amount of samples per report.
  364. */
  365. __STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(uint32_t reportper)
  366. {
  367. return (reportper == NRF_QDEC_REPORTPER_10) ? 10 : reportper*40;
  368. }
  369. /**
  370. * @brief Function for setting the active level for the LED.
  371. * @param[in] pol Active level for the LED.
  372. */
  373. __STATIC_INLINE void nrf_qdec_ledpol_set(nrf_qdec_ledpol_t pol)
  374. {
  375. NRF_QDEC->LEDPOL = pol;
  376. }
  377. /**
  378. * @brief Function for retrieving the active level for the LED.
  379. * @return Active level for the LED.
  380. */
  381. __STATIC_INLINE uint32_t nrf_qdec_ledpol_get(void)
  382. {
  383. return NRF_QDEC->LEDPOL;
  384. }
  385. /**
  386. *@}
  387. **/
  388. /*lint --flb "Leave library region" */
  389. #endif