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