The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Revision:
122:f9eeca106725
Parent:
65:5798e58a58b1
Child:
123:b0220dba8be7
--- a/PwmOut.h	Wed May 25 16:44:06 2016 +0100
+++ b/PwmOut.h	Thu Jul 07 14:34:11 2016 +0100
@@ -20,11 +20,14 @@
 
 #if DEVICE_PWMOUT
 #include "pwmout_api.h"
+#include "critical.h"
 
 namespace mbed {
 
 /** A pulse-width modulation digital output
  *
+ * @Note Synchronization level: Interrupt safe
+ *
  * Example
  * @code
  * // Fade a led on.
@@ -59,7 +62,9 @@
      *  @param pin PwmOut pin to connect to
      */
     PwmOut(PinName pin) {
+        core_util_critical_section_enter();
         pwmout_init(&_pwm, pin);
+        core_util_critical_section_exit();
     }
 
     /** Set the ouput duty-cycle, specified as a percentage (float)
@@ -70,7 +75,9 @@
      *    Values outside this range will be saturated to 0.0f or 1.0f.
      */
     void write(float value) {
+        core_util_critical_section_enter();
         pwmout_write(&_pwm, value);
+        core_util_critical_section_exit();
     }
 
     /** Return the current output duty-cycle setting, measured as a percentage (float)
@@ -84,7 +91,10 @@
      *  This value may not match exactly the value set by a previous <write>.
      */
     float read() {
-        return pwmout_read(&_pwm);
+        core_util_critical_section_enter();
+        float val = pwmout_read(&_pwm);
+        core_util_critical_section_exit();
+        return val;
     }
 
     /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
@@ -94,48 +104,62 @@
      *   will be set to zero.
      */
     void period(float seconds) {
+        core_util_critical_section_enter();
         pwmout_period(&_pwm, seconds);
+        core_util_critical_section_exit();
     }
 
     /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
      */
     void period_ms(int ms) {
+        core_util_critical_section_enter();
         pwmout_period_ms(&_pwm, ms);
+        core_util_critical_section_exit();
     }
 
     /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
      */
     void period_us(int us) {
+        core_util_critical_section_enter();
         pwmout_period_us(&_pwm, us);
+        core_util_critical_section_exit();
     }
 
     /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
      */
     void pulsewidth(float seconds) {
+        core_util_critical_section_enter();
         pwmout_pulsewidth(&_pwm, seconds);
+        core_util_critical_section_exit();
     }
 
     /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
      */
     void pulsewidth_ms(int ms) {
+        core_util_critical_section_enter();
         pwmout_pulsewidth_ms(&_pwm, ms);
+        core_util_critical_section_exit();
     }
 
     /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
      */
     void pulsewidth_us(int us) {
+        core_util_critical_section_enter();
         pwmout_pulsewidth_us(&_pwm, us);
+        core_util_critical_section_exit();
     }
 
 #ifdef MBED_OPERATORS
     /** A operator shorthand for write()
      */
     PwmOut& operator= (float value) {
+        // Underlying call is thread safe
         write(value);
         return *this;
     }
 
     PwmOut& operator= (PwmOut& rhs) {
+        // Underlying call is thread safe
         write(rhs.read());
         return *this;
     }
@@ -143,6 +167,7 @@
     /** An operator shorthand for read()
      */
     operator float() {
+        // Underlying call is thread safe
         return read();
     }
 #endif