timer_5410x.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * @brief LPC5410X 32-bit Timer/PWM driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "chip.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. struct TBASE_TO_TMRBITS {
  36. uint32_t base;
  37. uint8_t clockID;
  38. uint8_t resetID;
  39. };
  40. #define LAST_TIMER (4)
  41. static const struct TBASE_TO_TMRBITS tbaseToTimerIDs[LAST_TIMER + 1] = {
  42. {LPC_TIMER0_BASE, (uint8_t) SYSCON_CLOCK_TIMER0, (uint8_t) RESET_TIMER0},
  43. {LPC_TIMER1_BASE, (uint8_t) SYSCON_CLOCK_TIMER1, (uint8_t) RESET_TIMER1},
  44. {LPC_TIMER2_BASE, (uint8_t) SYSCON_CLOCK_TIMER2, (uint8_t) RESET_TIMER2},
  45. {LPC_TIMER3_BASE, (uint8_t) SYSCON_CLOCK_TIMER3, (uint8_t) RESET_TIMER3},
  46. {LPC_TIMER4_BASE, (uint8_t) SYSCON_CLOCK_TIMER4, (uint8_t) RESET_TIMER4}
  47. };
  48. /*****************************************************************************
  49. * Public types/enumerations/variables
  50. ****************************************************************************/
  51. /*****************************************************************************
  52. * Private functions
  53. ****************************************************************************/
  54. /* Return index into tbaseToTimerIDs for timers 0-4 */
  55. static int GetClockID(LPC_TIMER_T *pTMR)
  56. {
  57. int timerId = LAST_TIMER;
  58. while (timerId >= 0) {
  59. if (pTMR == (LPC_TIMER_T *) tbaseToTimerIDs[timerId].base) {
  60. return timerId;
  61. }
  62. timerId--;
  63. }
  64. /* Waill return timer 0 if no timer match */
  65. return 0;
  66. }
  67. /*****************************************************************************
  68. * Public functions
  69. ****************************************************************************/
  70. /* Initialize a timer */
  71. void Chip_TIMER_Init(LPC_TIMER_T *pTMR)
  72. {
  73. int clockId = GetClockID(pTMR);
  74. Chip_Clock_EnablePeriphClock((CHIP_SYSCON_CLOCK_T) tbaseToTimerIDs[clockId].clockID);
  75. Chip_SYSCON_PeriphReset((CHIP_SYSCON_PERIPH_RESET_T) tbaseToTimerIDs[clockId].resetID);
  76. }
  77. /* Shutdown a timer */
  78. void Chip_TIMER_DeInit(LPC_TIMER_T *pTMR)
  79. {
  80. int clockId = GetClockID(pTMR);
  81. Chip_Clock_DisablePeriphClock((CHIP_SYSCON_CLOCK_T) tbaseToTimerIDs[clockId].clockID);
  82. }
  83. /* Resets the timer counter and prescale counts to 0 */
  84. void Chip_TIMER_Reset(LPC_TIMER_T *pTMR)
  85. {
  86. uint32_t reg;
  87. /* Disable timer, set terminal count to non-0 */
  88. reg = pTMR->TCR;
  89. pTMR->TCR = 0;
  90. pTMR->TC = 1;
  91. /* Reset timer counter */
  92. pTMR->TCR = TIMER_RESET;
  93. /* Wait for terminal count to clear */
  94. while (pTMR->TC != 0) {}
  95. /* Restore timer state */
  96. pTMR->TCR = reg;
  97. }
  98. /* Sets external match control (MATn.matchnum) pin control */
  99. void Chip_TIMER_ExtMatchControlSet(LPC_TIMER_T *pTMR, int8_t initial_state,
  100. TIMER_PIN_MATCH_STATE_T matchState, int8_t matchnum)
  101. {
  102. uint32_t mask, reg;
  103. /* Clear bits corresponding to selected match register */
  104. mask = (1 << matchnum) | (0x03 << (4 + (matchnum * 2)));
  105. /* Also mask reserved bits */
  106. reg = (pTMR->EMR & TIMER_EMR_MASK) & ~mask;
  107. /* Set new configuration for selected match register */
  108. pTMR->EMR = reg | (((uint32_t) initial_state) << matchnum) |
  109. (((uint32_t) matchState) << (4 + (matchnum * 2)));
  110. }