sct_pwm_5410x.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * @brief LPC5410x State Configurable Timer (SCT/PWM) Chip 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 licenser 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. #ifndef __SCT_PWM_5410X_H_
  32. #define __SCT_PWM_5410X_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup SCT_PWM_5410X CHIP: LPC5410X State Configurable Timer PWM driver
  37. *
  38. * For more information on how to use the driver please visit the FAQ page at
  39. * <a href="http://www.lpcware.com/content/faq/how-use-sct-standard-pwm-using-lpcopen">
  40. * www.lpcware.com</a>
  41. *
  42. * @ingroup CHIP_5410X_DRIVERS
  43. * @{
  44. */
  45. /**
  46. * @brief Get number of ticks per PWM cycle
  47. * @param pSCT : The base of SCT peripheral on the chip
  48. * @return Number ot ticks that will be counted per cycle
  49. * @note Return value of this function will be vaild only
  50. * after calling Chip_SCTPWM_SetRate()
  51. */
  52. STATIC INLINE uint32_t Chip_SCTPWM_GetTicksPerCycle(LPC_SCT_T *pSCT)
  53. {
  54. return pSCT->MATCHREL[0].U;
  55. }
  56. /**
  57. * @brief Converts a percentage to ticks
  58. * @param pSCT : The base of SCT peripheral on the chip
  59. * @param percent : Percentage to convert (0 - 100)
  60. * @return Number ot ticks corresponding to given percentage
  61. * @note Do not use this function when using very low
  62. * pwm rate (like 100Hz or less), on a chip that has
  63. * very high frequency as the calculation might
  64. * cause integer overflow
  65. */
  66. STATIC INLINE uint32_t Chip_SCTPWM_PercentageToTicks(LPC_SCT_T *pSCT, uint8_t percent)
  67. {
  68. return (Chip_SCTPWM_GetTicksPerCycle(pSCT) * percent) / 100;
  69. }
  70. /**
  71. * @brief Get number of ticks on per PWM cycle
  72. * @param pSCT : The base of SCT peripheral on the chip
  73. * @param index : Index of the PWM 1 to N (see notes)
  74. * @return Number ot ticks for which the output will be ON per cycle
  75. * @note @a index will be 1 to N where N is the "Number of
  76. * match registers available in the SCT - 1" or
  77. * "Number of OUTPUT pins available in the SCT" whichever
  78. * is minimum.
  79. */
  80. STATIC INLINE uint32_t Chip_SCTPWM_GetDutyCycle(LPC_SCT_T *pSCT, uint8_t index)
  81. {
  82. return pSCT->MATCHREL[index].U;
  83. }
  84. /**
  85. * @brief Get number of ticks on per PWM cycle
  86. * @param pSCT : The base of SCT peripheral on the chip
  87. * @param index : Index of the PWM 1 to N (see notes)
  88. * @param ticks : Number of ticks the output should say ON
  89. * @return None
  90. * @note @a index will be 1 to N where N is the "Number of
  91. * match registers available in the SCT - 1" or
  92. * "Number of OUTPUT pins available in the SCT" whichever
  93. * is minimum. The new duty cycle will be effective only
  94. * after completion of current PWM cycle.
  95. */
  96. STATIC INLINE void Chip_SCTPWM_SetDutyCycle(LPC_SCT_T *pSCT, uint8_t index, uint32_t ticks)
  97. {
  98. Chip_SCT_SetMatchReload(pSCT, (CHIP_SCT_MATCH_REG_T) index, ticks);
  99. }
  100. /**
  101. * @brief Initialize the SCT/PWM clock and reset
  102. * @param pSCT : The base of SCT peripheral on the chip
  103. * @return None
  104. */
  105. STATIC INLINE void Chip_SCTPWM_Init(LPC_SCT_T *pSCT)
  106. {
  107. Chip_SCT_Init(pSCT);
  108. }
  109. /**
  110. * @brief Start the SCT PWM
  111. * @param pSCT : The base of SCT peripheral on the chip
  112. * @return None
  113. * @note This function must be called after all the
  114. * configuration is completed. Do not call Chip_SCTPWM_SetRate()
  115. * or Chip_SCTPWM_SetOutPin() after the SCT/PWM is started. Use
  116. * Chip_SCTPWM_Stop() to stop the SCT/PWM before reconfiguring,
  117. * Chip_SCTPWM_SetDutyCycle() can be called when the SCT/PWM is
  118. * running to change the DutyCycle.
  119. */
  120. STATIC INLINE void Chip_SCTPWM_Start(LPC_SCT_T *pSCT)
  121. {
  122. Chip_SCT_ClearControl(pSCT, SCT_CTRL_HALT_L | SCT_CTRL_HALT_H);
  123. }
  124. /**
  125. * @brief Stop the SCT PWM
  126. * @param pSCT : The base of SCT peripheral on the chip
  127. * @return None
  128. */
  129. STATIC INLINE void Chip_SCTPWM_Stop(LPC_SCT_T *pSCT)
  130. {
  131. /* Stop SCT */
  132. Chip_SCT_SetControl(pSCT, SCT_CTRL_HALT_L | SCT_CTRL_HALT_H);
  133. /* Clear the counter */
  134. Chip_SCT_SetControl(pSCT, SCT_CTRL_CLRCTR_L | SCT_CTRL_CLRCTR_H);
  135. }
  136. /**
  137. * @brief Sets the frequency of the generated PWM wave
  138. * @param pSCT : The base of SCT peripheral on the chip
  139. * @param freq : Frequency in Hz
  140. * @return None
  141. */
  142. void Chip_SCTPWM_SetRate(LPC_SCT_T *pSCT, uint32_t freq);
  143. /**
  144. * @brief Setup the OUTPUT pin and associate it with an index
  145. * @param pSCT : The base of the SCT peripheral on the chip
  146. * @param index : Index of PWM 1 to N (see notes)
  147. * @param pin : COUT pin to be associated with the index
  148. * @return None
  149. * @note @a index will be 1 to N where N is the "Number of
  150. * match registers available in the SCT - 1" or
  151. * "Number of OUTPUT pins available in the SCT" whichever
  152. * is minimum.
  153. */
  154. void Chip_SCTPWM_SetOutPin(LPC_SCT_T *pSCT, uint8_t index, uint8_t pin);
  155. /**
  156. * @}
  157. */
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #endif /* __SCT_PWM_5410X_H_ */