EE445M RTOS
Taken at the University of Texas Spring 2015
qei.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // qei.c - Driver for the Quadrature Encoder with Index.
4 //
5 // Copyright (c) 2005-2014 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.1.0.12573 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 //*****************************************************************************
41 //
44 //
45 //*****************************************************************************
46 
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "inc/hw_ints.h"
50 #include "inc/hw_memmap.h"
51 #include "inc/hw_qei.h"
52 #include "inc/hw_types.h"
53 #include "inc/hw_sysctl.h"
54 #include "driverlib/debug.h"
55 #include "driverlib/interrupt.h"
56 #include "driverlib/qei.h"
57 
58 //*****************************************************************************
59 //
70 //
71 //*****************************************************************************
72 void
73 QEIEnable(uint32_t ui32Base)
74 {
75  //
76  // Check the arguments.
77  //
78  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
79 
80  //
81  // Enable the QEI module.
82  //
83  HWREG(ui32Base + QEI_O_CTL) |= QEI_CTL_ENABLE;
84 }
85 
86 //*****************************************************************************
87 //
95 //
96 //*****************************************************************************
97 void
98 QEIDisable(uint32_t ui32Base)
99 {
100  //
101  // Check the arguments.
102  //
103  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
104 
105  //
106  // Disable the QEI module.
107  //
108  HWREG(ui32Base + QEI_O_CTL) &= ~(QEI_CTL_ENABLE);
109 }
110 
111 //*****************************************************************************
112 //
140 //
141 //*****************************************************************************
142 void
143 QEIConfigure(uint32_t ui32Base, uint32_t ui32Config,
144  uint32_t ui32MaxPosition)
145 {
146  //
147  // Check the arguments.
148  //
149  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
150 
151  //
152  // Write the new configuration to the hardware.
153  //
154  HWREG(ui32Base + QEI_O_CTL) = ((HWREG(ui32Base + QEI_O_CTL) &
157  ui32Config);
158 
159  //
160  // Set the maximum position.
161  //
162  HWREG(ui32Base + QEI_O_MAXPOS) = ui32MaxPosition;
163 }
164 
165 //*****************************************************************************
166 //
178 //
179 //*****************************************************************************
180 uint32_t
181 QEIPositionGet(uint32_t ui32Base)
182 {
183  //
184  // Check the arguments.
185  //
186  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
187 
188  //
189  // Return the current position counter.
190  //
191  return(HWREG(ui32Base + QEI_O_POS));
192 }
193 
194 //*****************************************************************************
195 //
205 //
206 //*****************************************************************************
207 void
208 QEIPositionSet(uint32_t ui32Base, uint32_t ui32Position)
209 {
210  //
211  // Check the arguments.
212  //
213  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
214 
215  //
216  // Set the position counter.
217  //
218  HWREG(ui32Base + QEI_O_POS) = ui32Position;
219 }
220 
221 //*****************************************************************************
222 //
234 //
235 //*****************************************************************************
236 int32_t
237 QEIDirectionGet(uint32_t ui32Base)
238 {
239  //
240  // Check the arguments.
241  //
242  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
243 
244  //
245  // Return the direction of rotation.
246  //
247  return((HWREG(ui32Base + QEI_O_STAT) & QEI_STAT_DIRECTION) ? -1 : 1);
248 }
249 
250 //*****************************************************************************
251 //
261 //
262 //*****************************************************************************
263 bool
264 QEIErrorGet(uint32_t ui32Base)
265 {
266  //
267  // Check the arguments.
268  //
269  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
270 
271  //
272  // Return the error indicator.
273  //
274  return((HWREG(ui32Base + QEI_O_STAT) & QEI_STAT_ERROR) ? true : false);
275 }
276 
277 //*****************************************************************************
278 //
290 //
291 //*****************************************************************************
292 void
293 QEIVelocityEnable(uint32_t ui32Base)
294 {
295  //
296  // Check the arguments.
297  //
298  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
299 
300  //
301  // Enable the velocity capture.
302  //
303  HWREG(ui32Base + QEI_O_CTL) |= QEI_CTL_VELEN;
304 }
305 
306 //*****************************************************************************
307 //
316 //
317 //*****************************************************************************
318 void
319 QEIVelocityDisable(uint32_t ui32Base)
320 {
321  //
322  // Check the arguments.
323  //
324  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
325 
326  //
327  // Disable the velocity capture.
328  //
329  HWREG(ui32Base + QEI_O_CTL) &= ~(QEI_CTL_VELEN);
330 }
331 
332 //*****************************************************************************
333 //
351 //
352 //*****************************************************************************
353 void
354 QEIVelocityConfigure(uint32_t ui32Base, uint32_t ui32PreDiv,
355  uint32_t ui32Period)
356 {
357  //
358  // Check the arguments.
359  //
360  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
361  ASSERT(!(ui32PreDiv & ~(QEI_CTL_VELDIV_M)));
362  ASSERT(ui32Period != 0);
363 
364  //
365  // Set the velocity predivider.
366  //
367  HWREG(ui32Base + QEI_O_CTL) = ((HWREG(ui32Base + QEI_O_CTL) &
368  ~(QEI_CTL_VELDIV_M)) | ui32PreDiv);
369 
370  //
371  // Set the timer period.
372  //
373  HWREG(ui32Base + QEI_O_LOAD) = ui32Period - 1;
374 }
375 
376 //*****************************************************************************
377 //
389 //
390 //*****************************************************************************
391 uint32_t
392 QEIVelocityGet(uint32_t ui32Base)
393 {
394  //
395  // Check the arguments.
396  //
397  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
398 
399  //
400  // Return the speed capture value.
401  //
402  return(HWREG(ui32Base + QEI_O_SPEED));
403 }
404 
405 //*****************************************************************************
406 //
416 //
417 //*****************************************************************************
418 static uint32_t
419 _QEIIntNumberGet(uint32_t ui32Base)
420 {
421  uint32_t ui32Int;
422 
423  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
424 
425  //
426  // Find the valid interrupt number for this quadrature encoder.
427  //
428  if(CLASS_IS_TM4C123)
429  {
430  if(ui32Base == QEI0_BASE)
431  {
432  ui32Int = INT_QEI0_TM4C123;
433  }
434  else
435  {
436  ui32Int = INT_QEI1_TM4C123;
437  }
438  }
439  else if(CLASS_IS_TM4C129)
440  {
441  if(ui32Base == QEI0_BASE)
442  {
443  ui32Int = INT_QEI0_TM4C129;
444  }
445  else
446  {
447  ui32Int = 0;
448  }
449  }
450  else
451  {
452  ui32Int = 0;
453  }
454 
455  return(ui32Int);
456 }
457 
458 //*****************************************************************************
459 //
476 //
477 //*****************************************************************************
478 void
479 QEIIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
480 {
481  uint32_t ui32Int;
482 
483  //
484  // Check the arguments.
485  //
486  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
487 
488  //
489  // Determine the interrupt number based on the QEI module.
490  //
491  ui32Int = _QEIIntNumberGet(ui32Base);
492 
493  ASSERT(ui32Int != 0);
494 
495  //
496  // Register the interrupt handler, returning an error if an error occurs.
497  //
498  IntRegister(ui32Int, pfnHandler);
499 
500  //
501  // Enable the quadrature encoder interrupt.
502  //
503  IntEnable(ui32Int);
504 }
505 
506 //*****************************************************************************
507 //
520 //
521 //*****************************************************************************
522 void
523 QEIIntUnregister(uint32_t ui32Base)
524 {
525  uint32_t ui32Int;
526 
527  //
528  // Check the arguments.
529  //
530  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
531 
532  //
533  // Determine the interrupt number based on the QEI module.
534  //
535  ui32Int = _QEIIntNumberGet(ui32Base);
536 
537  ASSERT(ui32Int != 0);
538 
539  //
540  // Disable the interrupt.
541  //
542  IntDisable(ui32Int);
543 
544  //
545  // Unregister the interrupt handler.
546  //
547  IntUnregister(ui32Int);
548 }
549 
550 //*****************************************************************************
551 //
564 //
565 //*****************************************************************************
566 void
567 QEIIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
568 {
569  //
570  // Check the arguments.
571  //
572  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
573 
574  //
575  // Enable the specified interrupts.
576  //
577  HWREG(ui32Base + QEI_O_INTEN) |= ui32IntFlags;
578 }
579 
580 //*****************************************************************************
581 //
594 //
595 //*****************************************************************************
596 void
597 QEIIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
598 {
599  //
600  // Check the arguments.
601  //
602  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
603 
604  //
605  // Disable the specified interrupts.
606  //
607  HWREG(ui32Base + QEI_O_INTEN) &= ~(ui32IntFlags);
608 }
609 
610 //*****************************************************************************
611 //
624 //
625 //*****************************************************************************
626 uint32_t
627 QEIIntStatus(uint32_t ui32Base, bool bMasked)
628 {
629  //
630  // Check the arguments.
631  //
632  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
633 
634  //
635  // Return either the interrupt status or the raw interrupt status as
636  // requested.
637  //
638  if(bMasked)
639  {
640  return(HWREG(ui32Base + QEI_O_ISC));
641  }
642  else
643  {
644  return(HWREG(ui32Base + QEI_O_RIS));
645  }
646 }
647 
648 //*****************************************************************************
649 //
672 //
673 //*****************************************************************************
674 void
675 QEIIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
676 {
677  //
678  // Check the arguments.
679  //
680  ASSERT((ui32Base == QEI0_BASE) || (ui32Base == QEI1_BASE));
681 
682  //
683  // Clear the requested interrupt sources.
684  //
685  HWREG(ui32Base + QEI_O_ISC) = ui32IntFlags;
686 }
687 
688 //*****************************************************************************
689 //
690 // Close the Doxygen group.
692 //
693 //*****************************************************************************
#define QEI_O_RIS
Definition: hw_qei.h:57
int32_t QEIDirectionGet(uint32_t ui32Base)
Definition: qei.c:237
void QEIPositionSet(uint32_t ui32Base, uint32_t ui32Position)
Definition: qei.c:208
#define QEI_O_STAT
Definition: hw_qei.h:49
#define QEI_O_LOAD
Definition: hw_qei.h:52
#define QEI0_BASE
Definition: hw_memmap.h:79
void QEIIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: qei.c:567
#define HWREG(x)
Definition: hw_types.h:48
void QEIIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: qei.c:675
void QEIIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: qei.c:597
#define QEI_O_CTL
Definition: hw_qei.h:48
#define QEI_CTL_VELEN
Definition: hw_qei.h:80
void QEIIntUnregister(uint32_t ui32Base)
Definition: qei.c:523
#define ASSERT(expr)
Definition: debug.h:67
uint32_t QEIIntStatus(uint32_t ui32Base, bool bMasked)
Definition: qei.c:627
static uint32_t _QEIIntNumberGet(uint32_t ui32Base)
Definition: qei.c:419
#define QEI_CTL_ENABLE
Definition: hw_qei.h:85
void QEIIntRegister(uint32_t ui32Base, void(*pfnHandler)(void))
Definition: qei.c:479
uint32_t QEIPositionGet(uint32_t ui32Base)
Definition: qei.c:181
#define QEI_CTL_SIGMODE
Definition: hw_qei.h:83
void QEIVelocityEnable(uint32_t ui32Base)
Definition: qei.c:293
void QEIVelocityDisable(uint32_t ui32Base)
Definition: qei.c:319
void QEIDisable(uint32_t ui32Base)
Definition: qei.c:98
#define QEI_O_MAXPOS
Definition: hw_qei.h:51
#define INT_QEI0_TM4C123
Definition: hw_ints.h:77
#define INT_QEI0_TM4C129
Definition: hw_ints.h:189
#define QEI_CTL_VELDIV_M
Definition: hw_qei.h:71
void QEIVelocityConfigure(uint32_t ui32Base, uint32_t ui32PreDiv, uint32_t ui32Period)
Definition: qei.c:354
#define CLASS_IS_TM4C123
Definition: hw_types.h:93
#define QEI_O_POS
Definition: hw_qei.h:50
#define QEI1_BASE
Definition: hw_memmap.h:80
#define QEI_O_INTEN
Definition: hw_qei.h:56
#define INT_QEI1_TM4C123
Definition: hw_ints.h:103
#define QEI_O_ISC
Definition: hw_qei.h:58
void IntUnregister(uint32_t ui32Interrupt)
Definition: interrupt.c:381
#define QEI_STAT_DIRECTION
Definition: hw_qei.h:93
#define QEI_STAT_ERROR
Definition: hw_qei.h:94
void QEIConfigure(uint32_t ui32Base, uint32_t ui32Config, uint32_t ui32MaxPosition)
Definition: qei.c:143
#define QEI_CTL_SWAP
Definition: hw_qei.h:84
#define QEI_O_SPEED
Definition: hw_qei.h:55
#define CLASS_IS_TM4C129
Definition: hw_types.h:99
#define QEI_CTL_CAPMODE
Definition: hw_qei.h:82
uint32_t QEIVelocityGet(uint32_t ui32Base)
Definition: qei.c:392
#define QEI_CTL_RESMODE
Definition: hw_qei.h:81
void IntDisable(uint32_t ui32Interrupt)
Definition: interrupt.c:684
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Definition: interrupt.c:309
bool QEIErrorGet(uint32_t ui32Base)
Definition: qei.c:264
void IntEnable(uint32_t ui32Interrupt)
Definition: interrupt.c:610
void QEIEnable(uint32_t ui32Base)
Definition: qei.c:73