PwmOut.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_PWMOUT_H
  17. #define MBED_PWMOUT_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_PWMOUT) || defined(DOXYGEN_ONLY)
  20. #include "hal/pwmout_api.h"
  21. #include "platform/mbed_critical.h"
  22. #include "platform/mbed_power_mgmt.h"
  23. namespace mbed {
  24. /** \addtogroup drivers */
  25. /** A pulse-width modulation digital output
  26. *
  27. * @note Synchronization level: Interrupt safe
  28. *
  29. * Example
  30. * @code
  31. * // Fade a led on.
  32. * #include "mbed.h"
  33. *
  34. * PwmOut led(LED1);
  35. *
  36. * int main() {
  37. * while(1) {
  38. * led = led + 0.01;
  39. * wait(0.2);
  40. * if(led == 1.0) {
  41. * led = 0;
  42. * }
  43. * }
  44. * }
  45. * @endcode
  46. * @ingroup drivers
  47. */
  48. class PwmOut {
  49. public:
  50. /** Create a PwmOut connected to the specified pin
  51. *
  52. * @param pin PwmOut pin to connect to
  53. */
  54. PwmOut(PinName pin) : _deep_sleep_locked(false)
  55. {
  56. core_util_critical_section_enter();
  57. pwmout_init(&_pwm, pin);
  58. core_util_critical_section_exit();
  59. }
  60. ~PwmOut()
  61. {
  62. core_util_critical_section_enter();
  63. unlock_deep_sleep();
  64. core_util_critical_section_exit();
  65. }
  66. /** Set the ouput duty-cycle, specified as a percentage (float)
  67. *
  68. * @param value A floating-point value representing the output duty-cycle,
  69. * specified as a percentage. The value should lie between
  70. * 0.0f (representing on 0%) and 1.0f (representing on 100%).
  71. * Values outside this range will be saturated to 0.0f or 1.0f.
  72. */
  73. void write(float value)
  74. {
  75. core_util_critical_section_enter();
  76. lock_deep_sleep();
  77. pwmout_write(&_pwm, value);
  78. core_util_critical_section_exit();
  79. }
  80. /** Return the current output duty-cycle setting, measured as a percentage (float)
  81. *
  82. * @returns
  83. * A floating-point value representing the current duty-cycle being output on the pin,
  84. * measured as a percentage. The returned value will lie between
  85. * 0.0f (representing on 0%) and 1.0f (representing on 100%).
  86. *
  87. * @note
  88. * This value may not match exactly the value set by a previous write().
  89. */
  90. float read()
  91. {
  92. core_util_critical_section_enter();
  93. float val = pwmout_read(&_pwm);
  94. core_util_critical_section_exit();
  95. return val;
  96. }
  97. /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
  98. *
  99. * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
  100. * @note
  101. * The resolution is currently in microseconds; periods smaller than this
  102. * will be set to zero.
  103. */
  104. void period(float seconds)
  105. {
  106. core_util_critical_section_enter();
  107. pwmout_period(&_pwm, seconds);
  108. core_util_critical_section_exit();
  109. }
  110. /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
  111. * @param ms Change the period of a PWM signal in milli-seconds without modifying the duty cycle
  112. */
  113. void period_ms(int ms)
  114. {
  115. core_util_critical_section_enter();
  116. pwmout_period_ms(&_pwm, ms);
  117. core_util_critical_section_exit();
  118. }
  119. /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
  120. * @param us Change the period of a PWM signal in micro-seconds without modifying the duty cycle
  121. */
  122. void period_us(int us)
  123. {
  124. core_util_critical_section_enter();
  125. pwmout_period_us(&_pwm, us);
  126. core_util_critical_section_exit();
  127. }
  128. /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
  129. * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
  130. */
  131. void pulsewidth(float seconds)
  132. {
  133. core_util_critical_section_enter();
  134. pwmout_pulsewidth(&_pwm, seconds);
  135. core_util_critical_section_exit();
  136. }
  137. /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
  138. * @param ms Change the pulse width of a PWM signal specified in milli-seconds
  139. */
  140. void pulsewidth_ms(int ms)
  141. {
  142. core_util_critical_section_enter();
  143. pwmout_pulsewidth_ms(&_pwm, ms);
  144. core_util_critical_section_exit();
  145. }
  146. /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
  147. * @param us Change the pulse width of a PWM signal specified in micro-seconds
  148. */
  149. void pulsewidth_us(int us)
  150. {
  151. core_util_critical_section_enter();
  152. pwmout_pulsewidth_us(&_pwm, us);
  153. core_util_critical_section_exit();
  154. }
  155. /** A operator shorthand for write()
  156. * \sa PwmOut::write()
  157. */
  158. PwmOut &operator= (float value)
  159. {
  160. // Underlying call is thread safe
  161. write(value);
  162. return *this;
  163. }
  164. /** A operator shorthand for write()
  165. * \sa PwmOut::write()
  166. */
  167. PwmOut &operator= (PwmOut &rhs)
  168. {
  169. // Underlying call is thread safe
  170. write(rhs.read());
  171. return *this;
  172. }
  173. /** An operator shorthand for read()
  174. * \sa PwmOut::read()
  175. */
  176. operator float()
  177. {
  178. // Underlying call is thread safe
  179. return read();
  180. }
  181. protected:
  182. /** Lock deep sleep only if it is not yet locked */
  183. void lock_deep_sleep()
  184. {
  185. if (_deep_sleep_locked == false) {
  186. sleep_manager_lock_deep_sleep();
  187. _deep_sleep_locked = true;
  188. }
  189. }
  190. /** Unlock deep sleep in case it is locked */
  191. void unlock_deep_sleep()
  192. {
  193. if (_deep_sleep_locked == true) {
  194. sleep_manager_unlock_deep_sleep();
  195. _deep_sleep_locked = false;
  196. }
  197. }
  198. pwmout_t _pwm;
  199. bool _deep_sleep_locked;
  200. };
  201. } // namespace mbed
  202. #endif
  203. #endif