hal_pwm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_pwm.h
  15. * @brief PWM Driver macros and structures.
  16. *
  17. * @addtogroup PWM
  18. * @{
  19. */
  20. #ifndef HAL_PWM_H
  21. #define HAL_PWM_H
  22. #if (HAL_USE_PWM == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @name PWM output mode macros
  28. * @{
  29. */
  30. /**
  31. * @brief Standard output modes mask.
  32. */
  33. #define PWM_OUTPUT_MASK 0x0FU
  34. /**
  35. * @brief Output not driven, callback only.
  36. */
  37. #define PWM_OUTPUT_DISABLED 0x00U
  38. /**
  39. * @brief Positive PWM logic, active is logic level one.
  40. */
  41. #define PWM_OUTPUT_ACTIVE_HIGH 0x01U
  42. /**
  43. * @brief Inverse PWM logic, active is logic level zero.
  44. */
  45. #define PWM_OUTPUT_ACTIVE_LOW 0x02U
  46. /** @} */
  47. /*===========================================================================*/
  48. /* Driver pre-compile time settings. */
  49. /*===========================================================================*/
  50. /*===========================================================================*/
  51. /* Derived constants and error checks. */
  52. /*===========================================================================*/
  53. /*===========================================================================*/
  54. /* Driver data structures and types. */
  55. /*===========================================================================*/
  56. /**
  57. * @brief Driver state machine possible states.
  58. */
  59. typedef enum {
  60. PWM_UNINIT = 0, /**< Not initialized. */
  61. PWM_STOP = 1, /**< Stopped. */
  62. PWM_READY = 2 /**< Ready. */
  63. } pwmstate_t;
  64. /**
  65. * @brief Type of a structure representing a PWM driver.
  66. */
  67. typedef struct PWMDriver PWMDriver;
  68. /**
  69. * @brief Type of a PWM notification callback.
  70. *
  71. * @param[in] pwmp pointer to a @p PWMDriver object
  72. */
  73. typedef void (*pwmcallback_t)(PWMDriver *pwmp);
  74. #include "hal_pwm_lld.h"
  75. /*===========================================================================*/
  76. /* Driver macros. */
  77. /*===========================================================================*/
  78. /**
  79. * @name PWM duty cycle conversion
  80. * @{
  81. */
  82. /**
  83. * @brief Converts from fraction to pulse width.
  84. * @note Be careful with rounding errors, this is integer math not magic.
  85. * You can specify tenths of thousandth but make sure you have the
  86. * proper hardware resolution by carefully choosing the clock source
  87. * and prescaler settings, see @p PWM_COMPUTE_PSC.
  88. *
  89. * @param[in] pwmp pointer to a @p PWMDriver object
  90. * @param[in] denominator denominator of the fraction
  91. * @param[in] numerator numerator of the fraction
  92. * @return The pulse width to be passed to @p pwmEnableChannel().
  93. *
  94. * @api
  95. */
  96. #define PWM_FRACTION_TO_WIDTH(pwmp, denominator, numerator) \
  97. ((pwmcnt_t)((((pwmcnt_t)(pwmp)->period) * \
  98. (pwmcnt_t)(numerator)) / (pwmcnt_t)(denominator)))
  99. /**
  100. * @brief Converts from degrees to pulse width.
  101. * @note Be careful with rounding errors, this is integer math not magic.
  102. * You can specify hundredths of degrees but make sure you have the
  103. * proper hardware resolution by carefully choosing the clock source
  104. * and prescaler settings, see @p PWM_COMPUTE_PSC.
  105. *
  106. * @param[in] pwmp pointer to a @p PWMDriver object
  107. * @param[in] degrees degrees as an integer between 0 and 36000
  108. * @return The pulse width to be passed to @p pwmEnableChannel().
  109. *
  110. * @api
  111. */
  112. #define PWM_DEGREES_TO_WIDTH(pwmp, degrees) \
  113. PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees)
  114. /**
  115. * @brief Converts from percentage to pulse width.
  116. * @note Be careful with rounding errors, this is integer math not magic.
  117. * You can specify tenths of thousandth but make sure you have the
  118. * proper hardware resolution by carefully choosing the clock source
  119. * and prescaler settings, see @p PWM_COMPUTE_PSC.
  120. *
  121. * @param[in] pwmp pointer to a @p PWMDriver object
  122. * @param[in] percentage percentage as an integer between 0 and 10000
  123. * @return The pulse width to be passed to @p pwmEnableChannel().
  124. *
  125. * @api
  126. */
  127. #define PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) \
  128. PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage)
  129. /** @} */
  130. /**
  131. * @name Macro Functions
  132. * @{
  133. */
  134. /**
  135. * @brief Changes the period the PWM peripheral.
  136. * @details This function changes the period of a PWM unit that has already
  137. * been activated using @p pwmStart().
  138. * @pre The PWM unit must have been activated using @p pwmStart().
  139. * @post The PWM unit period is changed to the new value.
  140. * @note If a period is specified that is shorter than the pulse width
  141. * programmed in one of the channels then the behavior is not
  142. * guaranteed.
  143. *
  144. * @param[in] pwmp pointer to a @p PWMDriver object
  145. * @param[in] value new cycle time in ticks
  146. *
  147. * @iclass
  148. */
  149. #define pwmChangePeriodI(pwmp, value) { \
  150. (pwmp)->period = (value); \
  151. pwm_lld_change_period(pwmp, value); \
  152. }
  153. /**
  154. * @brief Enables a PWM channel.
  155. * @pre The PWM unit must have been activated using @p pwmStart().
  156. * @post The channel is active using the specified configuration.
  157. * @note Depending on the hardware implementation this function has
  158. * effect starting on the next cycle (recommended implementation)
  159. * or immediately (fallback implementation).
  160. *
  161. * @param[in] pwmp pointer to a @p PWMDriver object
  162. * @param[in] channel PWM channel identifier (0...channels-1)
  163. * @param[in] width PWM pulse width as clock pulses number
  164. *
  165. * @iclass
  166. */
  167. #define pwmEnableChannelI(pwmp, channel, width) do { \
  168. (pwmp)->enabled |= ((pwmchnmsk_t)1U << (pwmchnmsk_t)(channel)); \
  169. pwm_lld_enable_channel(pwmp, channel, width); \
  170. } while (false)
  171. /**
  172. * @brief Disables a PWM channel.
  173. * @pre The PWM unit must have been activated using @p pwmStart().
  174. * @post The channel is disabled and its output line returned to the
  175. * idle state.
  176. * @note Depending on the hardware implementation this function has
  177. * effect starting on the next cycle (recommended implementation)
  178. * or immediately (fallback implementation).
  179. *
  180. * @param[in] pwmp pointer to a @p PWMDriver object
  181. * @param[in] channel PWM channel identifier (0...channels-1)
  182. *
  183. * @iclass
  184. */
  185. #define pwmDisableChannelI(pwmp, channel) do { \
  186. (pwmp)->enabled &= ~((pwmchnmsk_t)1U << (pwmchnmsk_t)(channel)); \
  187. pwm_lld_disable_channel(pwmp, channel); \
  188. } while (false)
  189. /**
  190. * @brief Returns a PWM channel status.
  191. * @pre The PWM unit must have been activated using @p pwmStart().
  192. *
  193. * @param[in] pwmp pointer to a @p PWMDriver object
  194. * @param[in] channel PWM channel identifier (0...channels-1)
  195. *
  196. * @iclass
  197. */
  198. #define pwmIsChannelEnabledI(pwmp, channel) \
  199. (((pwmp)->enabled & ((pwmchnmsk_t)1U << (pwmchnmsk_t)(channel))) != 0U)
  200. /**
  201. * @brief Enables the periodic activation edge notification.
  202. * @pre The PWM unit must have been activated using @p pwmStart().
  203. * @note If the notification is already enabled then the call has no effect.
  204. *
  205. * @param[in] pwmp pointer to a @p PWMDriver object
  206. *
  207. * @iclass
  208. */
  209. #define pwmEnablePeriodicNotificationI(pwmp) \
  210. pwm_lld_enable_periodic_notification(pwmp)
  211. /**
  212. * @brief Disables the periodic activation edge notification.
  213. * @pre The PWM unit must have been activated using @p pwmStart().
  214. * @note If the notification is already disabled then the call has no effect.
  215. *
  216. * @param[in] pwmp pointer to a @p PWMDriver object
  217. *
  218. * @iclass
  219. */
  220. #define pwmDisablePeriodicNotificationI(pwmp) \
  221. pwm_lld_disable_periodic_notification(pwmp)
  222. /**
  223. * @brief Enables a channel de-activation edge notification.
  224. * @pre The PWM unit must have been activated using @p pwmStart().
  225. * @pre The channel must have been activated using @p pwmEnableChannel().
  226. * @note If the notification is already enabled then the call has no effect.
  227. *
  228. * @param[in] pwmp pointer to a @p PWMDriver object
  229. * @param[in] channel PWM channel identifier (0...channels-1)
  230. *
  231. * @iclass
  232. */
  233. #define pwmEnableChannelNotificationI(pwmp, channel) \
  234. pwm_lld_enable_channel_notification(pwmp, channel)
  235. /**
  236. * @brief Disables a channel de-activation edge notification.
  237. * @pre The PWM unit must have been activated using @p pwmStart().
  238. * @pre The channel must have been activated using @p pwmEnableChannel().
  239. * @note If the notification is already disabled then the call has no effect.
  240. *
  241. * @param[in] pwmp pointer to a @p PWMDriver object
  242. * @param[in] channel PWM channel identifier (0...channels-1)
  243. *
  244. * @iclass
  245. */
  246. #define pwmDisableChannelNotificationI(pwmp, channel) \
  247. pwm_lld_disable_channel_notification(pwmp, channel)
  248. /** @} */
  249. /*===========================================================================*/
  250. /* External declarations. */
  251. /*===========================================================================*/
  252. #ifdef __cplusplus
  253. extern "C" {
  254. #endif
  255. void pwmInit(void);
  256. void pwmObjectInit(PWMDriver *pwmp);
  257. void pwmStart(PWMDriver *pwmp, const PWMConfig *config);
  258. void pwmStop(PWMDriver *pwmp);
  259. void pwmChangePeriod(PWMDriver *pwmp, pwmcnt_t period);
  260. void pwmEnableChannel(PWMDriver *pwmp,
  261. pwmchannel_t channel,
  262. pwmcnt_t width);
  263. void pwmDisableChannel(PWMDriver *pwmp, pwmchannel_t channel);
  264. void pwmEnablePeriodicNotification(PWMDriver *pwmp);
  265. void pwmDisablePeriodicNotification(PWMDriver *pwmp);
  266. void pwmEnableChannelNotification(PWMDriver *pwmp, pwmchannel_t channel);
  267. void pwmDisableChannelNotification(PWMDriver *pwmp, pwmchannel_t channel);
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* HAL_USE_PWM == TRUE */
  272. #endif /* HAL_PWM_H */
  273. /** @} */