sct_pwm_8xx.h 6.0 KB

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