qei.c 25 KB

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