qei.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. //*****************************************************************************
  2. //
  3. // qei.c - Driver for the Quadrature Encoder with Index.
  4. //
  5. // Copyright (c) 2005-2017 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //*****************************************************************************
  37. //*****************************************************************************
  38. //
  39. //! \addtogroup qei_api
  40. //! @{
  41. //
  42. //*****************************************************************************
  43. #include <ti/devices/msp432e4/inc/msp432e411y.h>
  44. #include "types.h"
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_qei.h"
  48. #include "inc/hw_sysctl.h"
  49. #include "debug.h"
  50. #include "interrupt.h"
  51. #include "qei.h"
  52. //*****************************************************************************
  53. //
  54. //! Enables the quadrature encoder.
  55. //!
  56. //! \param ui32Base is the base address of the quadrature encoder module.
  57. //!
  58. //! This function enables operation of the quadrature encoder module. The
  59. //! module must be configured before it is enabled.
  60. //!
  61. //! \sa QEIConfigure()
  62. //!
  63. //! \return None.
  64. //
  65. //*****************************************************************************
  66. void
  67. QEIEnable(uint32_t ui32Base)
  68. {
  69. //
  70. // Check the arguments.
  71. //
  72. ASSERT(ui32Base == QEI0_BASE);
  73. //
  74. // Enable the QEI module.
  75. //
  76. HWREG(ui32Base + QEI_O_CTL) |= QEI_CTL_ENABLE;
  77. }
  78. //*****************************************************************************
  79. //
  80. //! Disables the quadrature encoder.
  81. //!
  82. //! \param ui32Base is the base address of the quadrature encoder module.
  83. //!
  84. //! This function disables operation of the quadrature encoder module.
  85. //!
  86. //! \return None.
  87. //
  88. //*****************************************************************************
  89. void
  90. QEIDisable(uint32_t ui32Base)
  91. {
  92. //
  93. // Check the arguments.
  94. //
  95. ASSERT(ui32Base == QEI0_BASE);
  96. //
  97. // Disable the QEI module.
  98. //
  99. HWREG(ui32Base + QEI_O_CTL) &= ~(QEI_CTL_ENABLE);
  100. }
  101. //*****************************************************************************
  102. //
  103. //! Configures the quadrature encoder.
  104. //!
  105. //! \param ui32Base is the base address of the quadrature encoder module.
  106. //! \param ui32Config is the configuration for the quadrature encoder. See
  107. //! below for a description of this parameter.
  108. //! \param ui32MaxPosition specifies the maximum position value.
  109. //!
  110. //! This function configures the operation of the quadrature encoder. The
  111. //! \e ui32Config parameter provides the configuration of the encoder and is
  112. //! the logical OR of several values:
  113. //!
  114. //! - \b QEI_CONFIG_CAPTURE_A or \b QEI_CONFIG_CAPTURE_A_B specify if edges
  115. //! on channel A or on both channels A and B should be counted by the
  116. //! position integrator and velocity accumulator.
  117. //! - \b QEI_CONFIG_NO_RESET or \b QEI_CONFIG_RESET_IDX specify if the
  118. //! position integrator should be reset when the index pulse is detected.
  119. //! - \b QEI_CONFIG_QUADRATURE or \b QEI_CONFIG_CLOCK_DIR specify if
  120. //! quadrature signals are being provided on ChA and ChB, or if a direction
  121. //! signal and a clock are being provided instead.
  122. //! - \b QEI_CONFIG_NO_SWAP or \b QEI_CONFIG_SWAP to specify if the signals
  123. //! provided on ChA and ChB should be swapped before being processed.
  124. //!
  125. //! \e ui32MaxPosition is the maximum value of the position integrator and is
  126. //! the value used to reset the position capture when in index reset mode and
  127. //! moving in the reverse (negative) direction.
  128. //!
  129. //! \return None.
  130. //
  131. //*****************************************************************************
  132. void
  133. QEIConfigure(uint32_t ui32Base, uint32_t ui32Config,
  134. uint32_t ui32MaxPosition)
  135. {
  136. //
  137. // Check the arguments.
  138. //
  139. ASSERT(ui32Base == QEI0_BASE);
  140. //
  141. // Write the new configuration to the hardware.
  142. //
  143. HWREG(ui32Base + QEI_O_CTL) = ((HWREG(ui32Base + QEI_O_CTL) &
  144. ~(QEI_CTL_CAPMODE | QEI_CTL_RESMODE |
  145. QEI_CTL_SIGMODE | QEI_CTL_SWAP)) |
  146. ui32Config);
  147. //
  148. // Set the maximum position.
  149. //
  150. HWREG(ui32Base + QEI_O_MAXPOS) = ui32MaxPosition;
  151. }
  152. //*****************************************************************************
  153. //
  154. //! Gets the current encoder position.
  155. //!
  156. //! \param ui32Base is the base address of the quadrature encoder module.
  157. //!
  158. //! This function returns the current position of the encoder. Depending upon
  159. //! the configuration of the encoder, and the incident of an index pulse, this
  160. //! value may or may not contain the expected data (that is, if in reset on
  161. //! index mode, if an index pulse has not been encountered, the position
  162. //! counter is not yet aligned with the index pulse).
  163. //!
  164. //! \return The current position of the encoder.
  165. //
  166. //*****************************************************************************
  167. uint32_t
  168. QEIPositionGet(uint32_t ui32Base)
  169. {
  170. //
  171. // Check the arguments.
  172. //
  173. ASSERT(ui32Base == QEI0_BASE);
  174. //
  175. // Return the current position counter.
  176. //
  177. return (HWREG(ui32Base + QEI_O_POS));
  178. }
  179. //*****************************************************************************
  180. //
  181. //! Sets the current encoder position.
  182. //!
  183. //! \param ui32Base is the base address of the quadrature encoder module.
  184. //! \param ui32Position is the new position for the encoder.
  185. //!
  186. //! This function sets the current position of the encoder; the encoder
  187. //! position is then measured relative to this value.
  188. //!
  189. //! \return None.
  190. //
  191. //*****************************************************************************
  192. void
  193. QEIPositionSet(uint32_t ui32Base, uint32_t ui32Position)
  194. {
  195. //
  196. // Check the arguments.
  197. //
  198. ASSERT(ui32Base == QEI0_BASE);
  199. //
  200. // Set the position counter.
  201. //
  202. HWREG(ui32Base + QEI_O_POS) = ui32Position;
  203. }
  204. //*****************************************************************************
  205. //
  206. //! Gets the current direction of rotation.
  207. //!
  208. //! \param ui32Base is the base address of the quadrature encoder module.
  209. //!
  210. //! This function returns the current direction of rotation. In this case,
  211. //! current means the most recently detected direction of the encoder; it may
  212. //! not be presently moving but this is the direction it last moved before it
  213. //! stopped.
  214. //!
  215. //! \return Returns 1 if moving in the forward direction or -1 if moving in the
  216. //! reverse direction.
  217. //
  218. //*****************************************************************************
  219. int32_t
  220. QEIDirectionGet(uint32_t ui32Base)
  221. {
  222. //
  223. // Check the arguments.
  224. //
  225. ASSERT(ui32Base == QEI0_BASE);
  226. //
  227. // Return the direction of rotation.
  228. //
  229. return ((HWREG(ui32Base + QEI_O_STAT) & QEI_STAT_DIRECTION) ? -1 : 1);
  230. }
  231. //*****************************************************************************
  232. //
  233. //! Gets the encoder error indicator.
  234. //!
  235. //! \param ui32Base is the base address of the quadrature encoder module.
  236. //!
  237. //! This function returns the error indicator for the quadrature encoder. It
  238. //! is an error for both of the signals of the quadrature input to change at
  239. //! the same time.
  240. //!
  241. //! \return Returns \b true if an error has occurred and \b false otherwise.
  242. //
  243. //*****************************************************************************
  244. bool
  245. QEIErrorGet(uint32_t ui32Base)
  246. {
  247. //
  248. // Check the arguments.
  249. //
  250. ASSERT(ui32Base == QEI0_BASE);
  251. //
  252. // Return the error indicator.
  253. //
  254. return ((HWREG(ui32Base + QEI_O_STAT) & QEI_STAT_ERROR) ? true : false);
  255. }
  256. //*****************************************************************************
  257. //
  258. //! Enables the input filter.
  259. //!
  260. //! \param ui32Base is the base address of the quadrature encoder module.
  261. //!
  262. //! This function enables operation of the input filter in the quadrature
  263. //! encoder module. The module must be configured before input filter is
  264. //! enabled.
  265. //!
  266. //! \sa QEIFilterConfigure() and QEIEnable()
  267. //!
  268. //! \return None.
  269. //
  270. //*****************************************************************************
  271. void
  272. QEIFilterEnable(uint32_t ui32Base)
  273. {
  274. //
  275. // Check the arguments.
  276. //
  277. ASSERT(ui32Base == QEI0_BASE);
  278. //
  279. // Enable the input filter.
  280. //
  281. HWREG(ui32Base + QEI_O_CTL) |= QEI_CTL_FILTEN;
  282. }
  283. //*****************************************************************************
  284. //
  285. //! Disables the input filter.
  286. //!
  287. //! \param ui32Base is the base address of the quadrature encoder module.
  288. //!
  289. //! This function disables operation of the input filter in the quadrature
  290. //! encoder module.
  291. //!
  292. //! \return None.
  293. //
  294. //*****************************************************************************
  295. void
  296. QEIFilterDisable(uint32_t ui32Base)
  297. {
  298. //
  299. // Check the arguments.
  300. //
  301. ASSERT(ui32Base == QEI0_BASE);
  302. //
  303. // Disable the input filter.
  304. //
  305. HWREG(ui32Base + QEI_O_CTL) &= ~(QEI_CTL_FILTEN);
  306. }
  307. //*****************************************************************************
  308. //
  309. //! Configures the input filter.
  310. //!
  311. //! \param ui32Base is the base address of the quadrature encoder module.
  312. //! \param ui32FiltCnt specifies the filter count applied to the input quadrature
  313. //! signal before it is counted; can be one of \b QEI_FILTCNT_2,
  314. //! \b QEI_FILTCNT_3, \b QEI_FILTCNT_4, \b QEI_FILTCNT_5, \b QEI_FILTCNT_6,
  315. //! \b QEI_FILTCNT_7, \b QEI_FILTCNT_8, \b QEI_FILTCNT_9, \b QEI_FILTCNT_10,
  316. //! \b QEI_FILTCNT_11, \b QEI_FILTCNT_12, \b QEI_FILTCNT_13, \b QEI_FILTCNT_14,
  317. //! \b QEI_FILTCNT_15, \b QEI_FILTCNT_16 or \b QEI_FILTCNT_17
  318. //!
  319. //! This function configures the operation of the input filter prescale count.
  320. //! as specified by \e ui32FiltCnt before the input signals are sent to the
  321. //! quadrature encoder module.
  322. //!
  323. //! \return None.
  324. //
  325. //*****************************************************************************
  326. void
  327. QEIFilterConfigure(uint32_t ui32Base, uint32_t ui32FiltCnt)
  328. {
  329. //
  330. // Check the arguments.
  331. //
  332. ASSERT(ui32Base == QEI0_BASE);
  333. ASSERT(!(ui32FiltCnt & ~(QEI_CTL_FILTCNT_M)));
  334. //
  335. // Set the input filter prescale count.
  336. //
  337. HWREG(ui32Base + QEI_O_CTL) = ((HWREG(ui32Base + QEI_O_CTL) &
  338. ~(QEI_CTL_FILTCNT_M)) | ui32FiltCnt);
  339. }
  340. //*****************************************************************************
  341. //
  342. //! Enables the velocity capture.
  343. //!
  344. //! \param ui32Base is the base address of the quadrature encoder module.
  345. //!
  346. //! This function enables operation of the velocity capture in the quadrature
  347. //! encoder module. The module must be configured before velocity capture is
  348. //! enabled.
  349. //!
  350. //! \sa QEIVelocityConfigure() and QEIEnable()
  351. //!
  352. //! \return None.
  353. //
  354. //*****************************************************************************
  355. void
  356. QEIVelocityEnable(uint32_t ui32Base)
  357. {
  358. //
  359. // Check the arguments.
  360. //
  361. ASSERT(ui32Base == QEI0_BASE);
  362. //
  363. // Enable the velocity capture.
  364. //
  365. HWREG(ui32Base + QEI_O_CTL) |= QEI_CTL_VELEN;
  366. }
  367. //*****************************************************************************
  368. //
  369. //! Disables the velocity capture.
  370. //!
  371. //! \param ui32Base is the base address of the quadrature encoder module.
  372. //!
  373. //! This function disables operation of the velocity capture in the quadrature
  374. //! encoder module.
  375. //!
  376. //! \return None.
  377. //
  378. //*****************************************************************************
  379. void
  380. QEIVelocityDisable(uint32_t ui32Base)
  381. {
  382. //
  383. // Check the arguments.
  384. //
  385. ASSERT(ui32Base == QEI0_BASE);
  386. //
  387. // Disable the velocity capture.
  388. //
  389. HWREG(ui32Base + QEI_O_CTL) &= ~(QEI_CTL_VELEN);
  390. }
  391. //*****************************************************************************
  392. //
  393. //! Configures the velocity capture.
  394. //!
  395. //! \param ui32Base is the base address of the quadrature encoder module.
  396. //! \param ui32PreDiv specifies the predivider applied to the input quadrature
  397. //! signal before it is counted; can be one of \b QEI_VELDIV_1,
  398. //! \b QEI_VELDIV_2, \b QEI_VELDIV_4, \b QEI_VELDIV_8, \b QEI_VELDIV_16,
  399. //! \b QEI_VELDIV_32, \b QEI_VELDIV_64, or \b QEI_VELDIV_128.
  400. //! \param ui32Period specifies the number of clock ticks over which to measure
  401. //! the velocity; must be non-zero.
  402. //!
  403. //! This function configures the operation of the velocity capture portion of
  404. //! the quadrature encoder. The position increment signal is predivided as
  405. //! specified by \e ui32PreDiv before being accumulated by the velocity
  406. //! capture. The divided signal is accumulated over \e ui32Period system clock
  407. //! before being saved and resetting the accumulator.
  408. //!
  409. //! \return None.
  410. //
  411. //*****************************************************************************
  412. void
  413. QEIVelocityConfigure(uint32_t ui32Base, uint32_t ui32PreDiv,
  414. uint32_t ui32Period)
  415. {
  416. //
  417. // Check the arguments.
  418. //
  419. ASSERT(ui32Base == QEI0_BASE);
  420. ASSERT(!(ui32PreDiv & ~(QEI_CTL_VELDIV_M)));
  421. ASSERT(ui32Period != 0);
  422. //
  423. // Set the velocity predivider.
  424. //
  425. HWREG(ui32Base + QEI_O_CTL) = ((HWREG(ui32Base + QEI_O_CTL) &
  426. ~(QEI_CTL_VELDIV_M)) | ui32PreDiv);
  427. //
  428. // Set the timer period.
  429. //
  430. HWREG(ui32Base + QEI_O_LOAD) = ui32Period - 1;
  431. }
  432. //*****************************************************************************
  433. //
  434. //! Gets the current encoder speed.
  435. //!
  436. //! \param ui32Base is the base address of the quadrature encoder module.
  437. //!
  438. //! This function returns the current speed of the encoder. The value returned
  439. //! is the number of pulses detected in the specified time period; this number
  440. //! can be multiplied by the number of time periods per second and divided by
  441. //! the number of pulses per revolution to obtain the number of revolutions per
  442. //! second.
  443. //!
  444. //! \return Returns the number of pulses captured in the given time period.
  445. //
  446. //*****************************************************************************
  447. uint32_t
  448. QEIVelocityGet(uint32_t ui32Base)
  449. {
  450. //
  451. // Check the arguments.
  452. //
  453. ASSERT(ui32Base == QEI0_BASE);
  454. //
  455. // Return the speed capture value.
  456. //
  457. return (HWREG(ui32Base + QEI_O_SPEED));
  458. }
  459. //*****************************************************************************
  460. //
  461. //! Returns the quadrature encoder interrupt number.
  462. //!
  463. //! \param ui32Base is the base address of the selected quadrature encoder
  464. //!
  465. //! This function returns the interrupt number for the quadrature encoder with
  466. //! the base address passed in the \e ui32Base parameter.
  467. //!
  468. //! \return Returns a quadrature encoder interrupt number or 0 if the interrupt
  469. //! does not exist.
  470. //
  471. //*****************************************************************************
  472. static uint32_t
  473. _QEIIntNumberGet(uint32_t ui32Base)
  474. {
  475. uint32_t ui32Int;
  476. ASSERT(ui32Base == QEI0_BASE);
  477. //
  478. // Find the valid interrupt number for this quadrature encoder.
  479. //
  480. if (ui32Base == QEI0_BASE)
  481. {
  482. ui32Int = INT_QEI0;
  483. }
  484. else
  485. {
  486. ui32Int = 0;
  487. }
  488. return (ui32Int);
  489. }
  490. //*****************************************************************************
  491. //
  492. //! Registers an interrupt handler for the quadrature encoder interrupt.
  493. //!
  494. //! \param ui32Base is the base address of the quadrature encoder module.
  495. //! \param pfnHandler is a pointer to the function to be called when the
  496. //! quadrature encoder interrupt occurs.
  497. //!
  498. //! This function registers the handler to be called when a quadrature encoder
  499. //! interrupt occurs. This function enables the global interrupt in the
  500. //! interrupt controller; specific quadrature encoder interrupts must be
  501. //! enabled via QEIIntEnable(). It is the interrupt handler's responsibility
  502. //! to clear the interrupt source via QEIIntClear().
  503. //!
  504. //! \sa IntRegister() for important information about registering interrupt
  505. //! handlers.
  506. //!
  507. //! \return None.
  508. //
  509. //*****************************************************************************
  510. void
  511. QEIIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
  512. {
  513. uint32_t ui32Int;
  514. //
  515. // Check the arguments.
  516. //
  517. ASSERT(ui32Base == QEI0_BASE);
  518. //
  519. // Determine the interrupt number based on the QEI module.
  520. //
  521. ui32Int = _QEIIntNumberGet(ui32Base);
  522. ASSERT(ui32Int != 0);
  523. //
  524. // Register the interrupt handler, returning an error if an error occurs.
  525. //
  526. IntRegister(ui32Int, pfnHandler);
  527. //
  528. // Enable the quadrature encoder interrupt.
  529. //
  530. IntEnable(ui32Int);
  531. }
  532. //*****************************************************************************
  533. //
  534. //! Unregisters an interrupt handler for the quadrature encoder interrupt.
  535. //!
  536. //! \param ui32Base is the base address of the quadrature encoder module.
  537. //!
  538. //! This function unregisters the handler to be called when a quadrature
  539. //! encoder interrupt occurs. This function also masks off the interrupt in
  540. //! the interrupt controller so that the interrupt handler no longer is called.
  541. //!
  542. //! \sa IntRegister() for important information about registering interrupt
  543. //! handlers.
  544. //!
  545. //! \return None.
  546. //
  547. //*****************************************************************************
  548. void
  549. QEIIntUnregister(uint32_t ui32Base)
  550. {
  551. uint32_t ui32Int;
  552. //
  553. // Check the arguments.
  554. //
  555. ASSERT(ui32Base == QEI0_BASE);
  556. //
  557. // Determine the interrupt number based on the QEI module.
  558. //
  559. ui32Int = _QEIIntNumberGet(ui32Base);
  560. ASSERT(ui32Int != 0);
  561. //
  562. // Disable the interrupt.
  563. //
  564. IntDisable(ui32Int);
  565. //
  566. // Unregister the interrupt handler.
  567. //
  568. IntUnregister(ui32Int);
  569. }
  570. //*****************************************************************************
  571. //
  572. //! Enables individual quadrature encoder interrupt sources.
  573. //!
  574. //! \param ui32Base is the base address of the quadrature encoder module.
  575. //! \param ui32IntFlags is a bit mask of the interrupt sources to be enabled.
  576. //! Can be any of the \b QEI_INTERROR, \b QEI_INTDIR, \b QEI_INTTIMER, or
  577. //! \b QEI_INTINDEX values.
  578. //!
  579. //! This function enables the indicated quadrature encoder interrupt sources.
  580. //! Only the sources that are enabled can be reflected to the processor
  581. //! interrupt; disabled sources have no effect on the processor.
  582. //!
  583. //! \return None.
  584. //
  585. //*****************************************************************************
  586. void
  587. QEIIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
  588. {
  589. //
  590. // Check the arguments.
  591. //
  592. ASSERT(ui32Base == QEI0_BASE);
  593. //
  594. // Enable the specified interrupts.
  595. //
  596. HWREG(ui32Base + QEI_O_INTEN) |= ui32IntFlags;
  597. }
  598. //*****************************************************************************
  599. //
  600. //! Disables individual quadrature encoder interrupt sources.
  601. //!
  602. //! \param ui32Base is the base address of the quadrature encoder module.
  603. //! \param ui32IntFlags is a bit mask of the interrupt sources to be disabled.
  604. //! This parameter can be any of the \b QEI_INTERROR, \b QEI_INTDIR,
  605. //! \b QEI_INTTIMER, or \b QEI_INTINDEX values.
  606. //!
  607. //! This function disables the indicated quadrature encoder interrupt sources.
  608. //! Only the sources that are enabled can be reflected to the processor
  609. //! interrupt; disabled sources have no effect on the processor.
  610. //!
  611. //! \return None.
  612. //
  613. //*****************************************************************************
  614. void
  615. QEIIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
  616. {
  617. //
  618. // Check the arguments.
  619. //
  620. ASSERT(ui32Base == QEI0_BASE);
  621. //
  622. // Disable the specified interrupts.
  623. //
  624. HWREG(ui32Base + QEI_O_INTEN) &= ~(ui32IntFlags);
  625. }
  626. //*****************************************************************************
  627. //
  628. //! Gets the current interrupt status.
  629. //!
  630. //! \param ui32Base is the base address of the quadrature encoder module.
  631. //! \param bMasked is false if the raw interrupt status is required and true if
  632. //! the masked interrupt status is required.
  633. //!
  634. //! This function returns the interrupt status for the quadrature encoder
  635. //! module. Either the raw interrupt status or the status of interrupts that
  636. //! are allowed to reflect to the processor can be returned.
  637. //!
  638. //! \return Returns the current interrupt status, enumerated as a bit field of
  639. //! \b QEI_INTERROR, \b QEI_INTDIR, \b QEI_INTTIMER, and \b QEI_INTINDEX.
  640. //
  641. //*****************************************************************************
  642. uint32_t
  643. QEIIntStatus(uint32_t ui32Base, bool bMasked)
  644. {
  645. //
  646. // Check the arguments.
  647. //
  648. ASSERT(ui32Base == QEI0_BASE);
  649. //
  650. // Return either the interrupt status or the raw interrupt status as
  651. // requested.
  652. //
  653. if (bMasked)
  654. {
  655. return (HWREG(ui32Base + QEI_O_ISC));
  656. }
  657. else
  658. {
  659. return (HWREG(ui32Base + QEI_O_RIS));
  660. }
  661. }
  662. //*****************************************************************************
  663. //
  664. //! Clears quadrature encoder interrupt sources.
  665. //!
  666. //! \param ui32Base is the base address of the quadrature encoder module.
  667. //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
  668. //! This parameter can be any of the \b QEI_INTERROR, \b QEI_INTDIR,
  669. //! \b QEI_INTTIMER, or \b QEI_INTINDEX values.
  670. //!
  671. //! The specified quadrature encoder interrupt sources are cleared, so that
  672. //! they no longer assert. This function must be called in the interrupt
  673. //! handler to keep the interrupt from being triggered again immediately upon
  674. //! exit.
  675. //!
  676. //! \note Because there is a write buffer in the Cortex-M processor, it may
  677. //! take several clock cycles before the interrupt source is actually cleared.
  678. //! Therefore, it is recommended that the interrupt source be cleared early in
  679. //! the interrupt handler (as opposed to the very last action) to avoid
  680. //! returning from the interrupt handler before the interrupt source is
  681. //! actually cleared. Failure to do so may result in the interrupt handler
  682. //! being immediately reentered (because the interrupt controller still sees
  683. //! the interrupt source asserted).
  684. //!
  685. //! \return None.
  686. //
  687. //*****************************************************************************
  688. void
  689. QEIIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
  690. {
  691. //
  692. // Check the arguments.
  693. //
  694. ASSERT(ui32Base == QEI0_BASE);
  695. //
  696. // Clear the requested interrupt sources.
  697. //
  698. HWREG(ui32Base + QEI_O_ISC) = ui32IntFlags;
  699. }
  700. //*****************************************************************************
  701. //
  702. // Close the Doxygen group.
  703. //! @}
  704. //
  705. //*****************************************************************************