1 //*****************************************************************************
3 // qei.c - Driver for the Quadrature Encoder with Index.
5 // Copyright (c) 2005,2006 Luminary Micro, Inc. All rights reserved.
7 // Software License Agreement
9 // Luminary Micro, Inc. (LMI) is supplying this software for use solely and
10 // exclusively on LMI's Stellaris Family of microcontroller products.
12 // The software is owned by LMI and/or its suppliers, and is protected under
13 // applicable copyright laws. All rights are reserved. Any use in violation
14 // of the foregoing restrictions may subject the user to criminal sanctions
15 // under applicable laws, as well as to civil liability for the breach of the
16 // terms and conditions of this license.
18 // THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
19 // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
21 // LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
22 // CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
24 // This is part of revision 991 of the Stellaris Driver Library.
26 //*****************************************************************************
28 //*****************************************************************************
30 //! \addtogroup qei_api
33 //*****************************************************************************
35 #include "../hw_ints.h"
36 #include "../hw_memmap.h"
37 #include "../hw_qei.h"
38 #include "../hw_types.h"
40 #include "interrupt.h"
43 //*****************************************************************************
45 //! Enables the quadrature encoder.
47 //! \param ulBase is the base address of the quadrature encoder module.
49 //! This will enable operation of the quadrature encoder module. It must be
50 //! configured before it is enabled.
52 //! \sa QEIConfigure()
56 //*****************************************************************************
57 #if defined(GROUP_enable) || defined(BUILD_ALL) || defined(DOXYGEN)
59 QEIEnable(unsigned long ulBase)
62 // Check the arguments.
64 ASSERT(ulBase == QEI_BASE);
67 // Enable the QEI module.
69 HWREG(ulBase + QEI_O_CTL) |= QEI_CTL_ENABLE;
73 //*****************************************************************************
75 //! Disables the quadrature encoder.
77 //! \param ulBase is the base address of the quadrature encoder module.
79 //! This will disable operation of the quadrature encoder module.
83 //*****************************************************************************
84 #if defined(GROUP_disable) || defined(BUILD_ALL) || defined(DOXYGEN)
86 QEIDisable(unsigned long ulBase)
89 // Check the arguments.
91 ASSERT(ulBase == QEI_BASE);
94 // Disable the QEI module.
96 HWREG(ulBase + QEI_O_CTL) &= ~(QEI_CTL_ENABLE);
100 //*****************************************************************************
102 //! Configures the quadrature encoder.
104 //! \param ulBase is the base address of the quadrature encoder module.
105 //! \param ulConfig is the configuration for the quadrature encoder. See below
106 //! for a description of this parameter.
107 //! \param ulMaxPosition specifies the maximum position value.
109 //! This will configure the operation of the quadrature encoder. The
110 //! \e ulConfig parameter provides the configuration of the encoder and is the
111 //! logical OR of several values:
113 //! - \b QEI_CONFIG_CAPTURE_A or \b QEI_CONFIG_CAPTURE_A_B to specify if edges
114 //! on channel A or on both channels A and B should be counted by the
115 //! position integrator and velocity accumulator.
116 //! - \b QEI_CONFIG_NO_RESET or \b QEI_CONFIG_RESET_IDX to specify if the
117 //! position integrator should be reset when the index pulse is detected.
118 //! - \b QEI_CONFIG_QUADRATURE or \b QEI_CONFIG_CLOCK_DIR to specify if
119 //! quadrature signals are being provided on ChA and ChB, or if a direction
120 //! signal and a clock are being provided instead.
121 //! - \b QEI_CONFIG_NO_SWAP or \b QEI_CONFIG_SWAP to specify if the signals
122 //! provided on ChA and ChB should be swapped before being processed.
124 //! \e ulMaxPosition is the maximum value of the position integrator, and is
125 //! the value used to reset the position capture when in index reset mode and
126 //! moving in the reverse (negative) direction.
130 //*****************************************************************************
131 #if defined(GROUP_configure) || defined(BUILD_ALL) || defined(DOXYGEN)
133 QEIConfigure(unsigned long ulBase, unsigned long ulConfig,
134 unsigned long ulMaxPosition)
137 // Check the arguments.
139 ASSERT(ulBase == QEI_BASE);
142 // Write the new configuration to the hardware.
144 HWREG(ulBase + QEI_O_CTL) = ((HWREG(ulBase + QEI_O_CTL) &
145 ~(QEI_CTL_CAPMODE | QEI_CTL_RESMODE |
146 QEI_CTL_SIGMODE | QEI_CTL_SWAP)) |
150 // Set the maximum position.
152 HWREG(ulBase + QEI_O_MAXPOS) = ulMaxPosition;
156 //*****************************************************************************
158 //! Gets the current encoder position.
160 //! \param ulBase is the base address of the quadrature encoder module.
162 //! This returns the current position of the encoder. Depending upon the
163 //! configuration of the encoder, and the incident of an index pulse, this
164 //! value may or may not contain the expected data (i.e. if in reset on index
165 //! mode, if an index pulse has not been encountered, the position counter will
166 //! not be aligned with the index pulse yet).
168 //! \return The current position of the encoder.
170 //*****************************************************************************
171 #if defined(GROUP_positionget) || defined(BUILD_ALL) || defined(DOXYGEN)
173 QEIPositionGet(unsigned long ulBase)
176 // Check the arguments.
178 ASSERT(ulBase == QEI_BASE);
181 // Return the current position counter.
183 return(HWREG(ulBase + QEI_O_POS));
187 //*****************************************************************************
189 //! Sets the current encoder position.
191 //! \param ulBase is the base address of the quadrature encoder module.
192 //! \param ulPosition is the new position for the encoder.
194 //! This sets the current position of the encoder; the encoder position will
195 //! then be measured relative to this value.
199 //*****************************************************************************
200 #if defined(GROUP_positionset) || defined(BUILD_ALL) || defined(DOXYGEN)
202 QEIPositionSet(unsigned long ulBase, unsigned long ulPosition)
205 // Check the arguments.
207 ASSERT(ulBase == QEI_BASE);
210 // Set the position counter.
212 HWREG(ulBase + QEI_O_POS) = ulPosition;
216 //*****************************************************************************
218 //! Gets the current direction of rotation.
220 //! \param ulBase is the base address of the quadrature encoder module.
222 //! This returns the current direction of rotation. In this case, current
223 //! means the most recently detected direction of the encoder; it may not be
224 //! presently moving but this is the direction it last moved before it stopped.
226 //! \return 1 if moving in the forward direction or -1 if moving in the reverse
229 //*****************************************************************************
230 #if defined(GROUP_directionget) || defined(BUILD_ALL) || defined(DOXYGEN)
232 QEIDirectionGet(unsigned long ulBase)
235 // Check the arguments.
237 ASSERT(ulBase == QEI_BASE);
240 // Return the direction of rotation.
242 return((HWREG(ulBase + QEI_O_STAT) & QEI_STAT_DIRECTION) ? -1 : 1);
246 //*****************************************************************************
248 //! Gets the encoder error indicator.
250 //! \param ulBase is the base address of the quadrature encoder module.
252 //! This returns the error indicator for the quadrature encoder. It is an
253 //! error for both of the signals of the quadrature input to change at the same
256 //! \return true if an error has occurred and false otherwise.
258 //*****************************************************************************
259 #if defined(GROUP_errorget) || defined(BUILD_ALL) || defined(DOXYGEN)
261 QEIErrorGet(unsigned long ulBase)
264 // Check the arguments.
266 ASSERT(ulBase == QEI_BASE);
269 // Return the error indicator.
271 return((HWREG(ulBase + QEI_O_STAT) & QEI_STAT_ERROR) ? true : false);
275 //*****************************************************************************
277 //! Enables the velocity capture.
279 //! \param ulBase is the base address of the quadrature encoder module.
281 //! This will enable operation of the velocity capture in the quadrature
282 //! encoder module. It must be configured before it is enabled. Velocity
283 //! capture will not occur if the quadrature encoder is not enabled.
285 //! \sa QEIVelocityConfigure() and QEIEnable()
289 //*****************************************************************************
290 #if defined(GROUP_velocityenable) || defined(BUILD_ALL) || defined(DOXYGEN)
292 QEIVelocityEnable(unsigned long ulBase)
295 // Check the arguments.
297 ASSERT(ulBase == QEI_BASE);
300 // Enable the velocity capture.
302 HWREG(ulBase + QEI_O_CTL) |= QEI_CTL_VELEN;
306 //*****************************************************************************
308 //! Disables the velocity capture.
310 //! \param ulBase is the base address of the quadrature encoder module.
312 //! This will disable operation of the velocity capture in the quadrature
317 //*****************************************************************************
318 #if defined(GROUP_velocitydisable) || defined(BUILD_ALL) || defined(DOXYGEN)
320 QEIVelocityDisable(unsigned long ulBase)
323 // Check the arguments.
325 ASSERT(ulBase == QEI_BASE);
328 // Disable the velocity capture.
330 HWREG(ulBase + QEI_O_CTL) &= ~(QEI_CTL_VELEN);
334 //*****************************************************************************
336 //! Configures the velocity capture.
338 //! \param ulBase is the base address of the quadrature encoder module.
339 //! \param ulPreDiv specifies the predivider applied to the input quadrature
340 //! signal before it is counted; can be one of QEI_VELDIV_1, QEI_VELDIV_2,
341 //! QEI_VELDIV_4, QEI_VELDIV_8, QEI_VELDIV_16, QEI_VELDIV_32, QEI_VELDIV_64, or
343 //! \param ulPeriod specifies the number of clock ticks over which to measure
344 //! the velocity; must be non-zero.
346 //! This will configure the operation of the velocity capture portion of the
347 //! quadrature encoder. The position increment signal is predivided as
348 //! specified by \e ulPreDiv before being accumulated by the velocity capture.
349 //! The divided signal is accumulated over \e ulPeriod system clock before
350 //! being saved and resetting the accumulator.
354 //*****************************************************************************
355 #if defined(GROUP_velocityconfigure) || defined(BUILD_ALL) || defined(DOXYGEN)
357 QEIVelocityConfigure(unsigned long ulBase, unsigned long ulPreDiv,
358 unsigned long ulPeriod)
361 // Check the arguments.
363 ASSERT(ulBase == QEI_BASE);
364 ASSERT(!(ulPreDiv & ~(QEI_CTL_VELDIV_M)));
365 ASSERT(ulPeriod != 0);
368 // Set the velocity predivider.
370 HWREG(ulBase + QEI_O_CTL) = ((HWREG(ulBase + QEI_O_CTL) &
371 ~(QEI_CTL_VELDIV_M)) | ulPreDiv);
374 // Set the timer period.
376 HWREG(ulBase + QEI_O_LOAD) = ulPeriod - 1;
380 //*****************************************************************************
382 //! Gets the current encoder speed.
384 //! \param ulBase is the base address of the quadrature encoder module.
386 //! This returns the current speed of the encoder. The value returned is the
387 //! number of pulses detected in the specified time period; this number can be
388 //! multiplied by the number of time periods per second and divided by the
389 //! number of pulses per revolution to obtain the number of revolutions per
392 //! \return The number of pulses captured in the given time period.
394 //*****************************************************************************
395 #if defined(GROUP_velocityget) || defined(BUILD_ALL) || defined(DOXYGEN)
397 QEIVelocityGet(unsigned long ulBase)
400 // Check the arguments.
402 ASSERT(ulBase == QEI_BASE);
405 // Return the speed capture value.
407 return(HWREG(ulBase + QEI_O_SPEED));
411 //*****************************************************************************
413 //! Registers an interrupt handler for the quadrature encoder interrupt.
415 //! \param ulBase is the base address of the quadrature encoder module.
416 //! \param pfnHandler is a pointer to the function to be called when the
417 //! quadrature encoder interrupt occurs.
419 //! This sets the handler to be called when a quadrature encoder interrupt
420 //! occurs. This will enable the global interrupt in the interrupt controller;
421 //! specific quadrature encoder interrupts must be enabled via QEIIntEnable().
422 //! It is the interrupt handler's responsibility to clear the interrupt source
423 //! via QEIIntClear().
425 //! \sa IntRegister() for important information about registering interrupt
430 //*****************************************************************************
431 #if defined(GROUP_intregister) || defined(BUILD_ALL) || defined(DOXYGEN)
433 QEIIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
436 // Check the arguments.
438 ASSERT(ulBase == QEI_BASE);
441 // Register the interrupt handler, returning an error if an error occurs.
443 IntRegister(INT_QEI, pfnHandler);
446 // Enable the quadrature encoder interrupt.
452 //*****************************************************************************
454 //! Unregisters an interrupt handler for the quadrature encoder interrupt.
456 //! \param ulBase is the base address of the quadrature encoder module.
458 //! This function will clear the handler to be called when a quadrature encoder
459 //! interrupt occurs. This will also mask off the interrupt in the interrupt
460 //! controller so that the interrupt handler no longer is called.
462 //! \sa IntRegister() for important information about registering interrupt
467 //*****************************************************************************
468 #if defined(GROUP_intunregister) || defined(BUILD_ALL) || defined(DOXYGEN)
470 QEIIntUnregister(unsigned long ulBase)
473 // Check the arguments.
475 ASSERT(ulBase == QEI_BASE);
478 // Disable the interrupt.
483 // Unregister the interrupt handler.
485 IntUnregister(INT_QEI);
489 //*****************************************************************************
491 //! Enables individual quadrature encoder interrupt sources.
493 //! \param ulBase is the base address of the quadrature encoder module.
494 //! \param ulIntFlags is a bit mask of the interrupt sources to be enabled.
495 //! Can be any of the QEI_INTERROR, QEI_INTDIR, QEI_INTTIMER, or QEI_INTINDEX
498 //! Enables the indicated quadrature encoder interrupt sources. Only the
499 //! sources that are enabled can be reflected to the processor interrupt;
500 //! disabled sources have no effect on the processor.
504 //*****************************************************************************
505 #if defined(GROUP_intenable) || defined(BUILD_ALL) || defined(DOXYGEN)
507 QEIIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
510 // Check the arguments.
512 ASSERT(ulBase == QEI_BASE);
515 // Enable the specified interrupts.
517 HWREG(ulBase + QEI_O_INTEN) |= ulIntFlags;
521 //*****************************************************************************
523 //! Disables individual quadrature encoder interrupt sources.
525 //! \param ulBase is the base address of the quadrature encoder module.
526 //! \param ulIntFlags is a bit mask of the interrupt sources to be disabled.
527 //! Can be any of the QEI_INTERROR, QEI_INTDIR, QEI_INTTIMER, or QEI_INTINDEX
530 //! Disables the indicated quadrature encoder interrupt sources. Only the
531 //! sources that are enabled can be reflected to the processor interrupt;
532 //! disabled sources have no effect on the processor.
536 //*****************************************************************************
537 #if defined(GROUP_intdisable) || defined(BUILD_ALL) || defined(DOXYGEN)
539 QEIIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
542 // Check the arguments.
544 ASSERT(ulBase == QEI_BASE);
547 // Disable the specified interrupts.
549 HWREG(ulBase + QEI_O_INTEN) &= ~(ulIntFlags);
553 //*****************************************************************************
555 //! Gets the current interrupt status.
557 //! \param ulBase is the base address of the quadrature encoder module.
558 //! \param bMasked is false if the raw interrupt status is required and true if
559 //! the masked interrupt status is required.
561 //! This returns the interrupt status for the quadrature encoder module.
562 //! Either the raw interrupt status or the status of interrupts that are
563 //! allowed to reflect to the processor can be returned.
565 //! \return The current interrupt status, enumerated as a bit field of
566 //! QEI_INTERROR, QEI_INTDIR, QEI_INTTIMER, and QEI_INTINDEX.
568 //*****************************************************************************
569 #if defined(GROUP_intstatus) || defined(BUILD_ALL) || defined(DOXYGEN)
571 QEIIntStatus(unsigned long ulBase, tBoolean bMasked)
574 // Check the arguments.
576 ASSERT(ulBase == QEI_BASE);
579 // Return either the interrupt status or the raw interrupt status as
584 return(HWREG(ulBase + QEI_O_ISC));
588 return(HWREG(ulBase + QEI_O_RIS));
593 //*****************************************************************************
595 //! Clears quadrature encoder interrupt sources.
597 //! \param ulBase is the base address of the quadrature encoder module.
598 //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
599 //! Can be any of the QEI_INTERROR, QEI_INTDIR, QEI_INTTIMER, or QEI_INTINDEX
602 //! The specified quadrature encoder interrupt sources are cleared, so that
603 //! they no longer assert. This must be done in the interrupt handler to keep
604 //! it from being called again immediately upon exit.
608 //*****************************************************************************
609 #if defined(GROUP_intclear) || defined(BUILD_ALL) || defined(DOXYGEN)
611 QEIIntClear(unsigned long ulBase, unsigned long ulIntFlags)
614 // Check the arguments.
616 ASSERT(ulBase == QEI_BASE);
619 // Clear the requested interrupt sources.
621 HWREG(ulBase + QEI_O_ISC) = ulIntFlags;
625 //*****************************************************************************
627 // Close the Doxygen group.
630 //*****************************************************************************