123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #ifndef MBED_PWMOUT_H
- #define MBED_PWMOUT_H
- #include "platform/platform.h"
- #if defined (DEVICE_PWMOUT) || defined(DOXYGEN_ONLY)
- #include "hal/pwmout_api.h"
- #include "platform/mbed_critical.h"
- #include "platform/mbed_power_mgmt.h"
- namespace mbed {
- class PwmOut {
- public:
-
- PwmOut(PinName pin) : _deep_sleep_locked(false)
- {
- core_util_critical_section_enter();
- pwmout_init(&_pwm, pin);
- core_util_critical_section_exit();
- }
- ~PwmOut()
- {
- core_util_critical_section_enter();
- unlock_deep_sleep();
- core_util_critical_section_exit();
- }
-
- void write(float value)
- {
- core_util_critical_section_enter();
- lock_deep_sleep();
- pwmout_write(&_pwm, value);
- core_util_critical_section_exit();
- }
-
- float read()
- {
- core_util_critical_section_enter();
- float val = pwmout_read(&_pwm);
- core_util_critical_section_exit();
- return val;
- }
-
- void period(float seconds)
- {
- core_util_critical_section_enter();
- pwmout_period(&_pwm, seconds);
- core_util_critical_section_exit();
- }
-
- void period_ms(int ms)
- {
- core_util_critical_section_enter();
- pwmout_period_ms(&_pwm, ms);
- core_util_critical_section_exit();
- }
-
- void period_us(int us)
- {
- core_util_critical_section_enter();
- pwmout_period_us(&_pwm, us);
- core_util_critical_section_exit();
- }
-
- void pulsewidth(float seconds)
- {
- core_util_critical_section_enter();
- pwmout_pulsewidth(&_pwm, seconds);
- core_util_critical_section_exit();
- }
-
- void pulsewidth_ms(int ms)
- {
- core_util_critical_section_enter();
- pwmout_pulsewidth_ms(&_pwm, ms);
- core_util_critical_section_exit();
- }
-
- void pulsewidth_us(int us)
- {
- core_util_critical_section_enter();
- pwmout_pulsewidth_us(&_pwm, us);
- core_util_critical_section_exit();
- }
-
- PwmOut &operator= (float value)
- {
-
- write(value);
- return *this;
- }
-
- PwmOut &operator= (PwmOut &rhs)
- {
-
- write(rhs.read());
- return *this;
- }
-
- operator float()
- {
-
- return read();
- }
- protected:
-
- void lock_deep_sleep()
- {
- if (_deep_sleep_locked == false) {
- sleep_manager_lock_deep_sleep();
- _deep_sleep_locked = true;
- }
- }
-
- void unlock_deep_sleep()
- {
- if (_deep_sleep_locked == true) {
- sleep_manager_unlock_deep_sleep();
- _deep_sleep_locked = false;
- }
- }
- pwmout_t _pwm;
- bool _deep_sleep_locked;
- };
- }
- #endif
- #endif
|