mbed library sources

Dependents:   Freedman_v2 Nucleo_i2c_OLED_BME280_copy

Fork of mbed-src by mbed official

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Thu Aug 13 10:30:09 2015 +0100
Parent:
602:421d040916fe
Child:
604:19d487c995ec
Commit message:
Synchronized with git revision 7cbf9896cc73d825b242028a5f95030a9e1016a4

Full URL: https://github.com/mbedmicro/mbed/commit/7cbf9896cc73d825b242028a5f95030a9e1016a4/

Added requirements.txt file

Changed in this revision

targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_Maxim/TARGET_MAX32600/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_Maxim/TARGET_MAX32600/pwmout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_Maxim/TARGET_MAX32610/PeripheralPins.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_Maxim/TARGET_MAX32610/pwmout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
--- a/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51.c	Wed Aug 05 14:30:08 2015 +0100
+++ b/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51.c	Thu Aug 13 10:30:09 2015 +0100
@@ -34,6 +34,7 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include "nrf.h"
+#include "nrf_delay.h"
 #include "system_nrf51.h"
 
 /*lint ++flb "Enter library region" */
@@ -42,6 +43,7 @@
 
 static bool is_manual_peripheral_setup_needed(void);
 static bool is_disabled_in_debug_needed(void);
+static void init_clock(void);
 
 
 #if defined ( __CC_ARM )
@@ -82,8 +84,18 @@
     }
 
     // Start the external 32khz crystal oscillator.
+    init_clock();
+}
 
-    /* for Nordic devices without an external LF crystal */
+void init_clock(void)
+{
+    /* For compatibility purpose, the default behaviour is to first attempt to initialise an
+       external clock, and after a timeout, use the internal RC one. To avoid this wait, boards that
+       don't have an external oscillator can set TARGET_NRF_LFCLK_RC directly. */
+    int i = 0;
+    const uint32_t polling_period = 200;
+    const uint32_t timeout = 1000000;
+
 #if defined(TARGET_NRF_LFCLK_RC)
     NRF_CLOCK->LFCLKSRC             = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
 #else
@@ -92,7 +104,26 @@
     NRF_CLOCK->EVENTS_LFCLKSTARTED  = 0;
     NRF_CLOCK->TASKS_LFCLKSTART     = 1;
 
-    // Wait for the external oscillator to start up.
+    /* Wait for the external oscillator to start up.
+       nRF51822 product specification (8.1.5) gives a typical value of 300ms for external clock
+       startup duration, and a maximum value of 1s. When using the internal RC source, typical delay
+       will be 390µs, so we use a polling period of 200µs.
+
+       We can't use us_ticker at this point, so we have to rely on a less precise method for
+       measuring our timeout. Because of this, the actual timeout will be slightly longer than 1
+       second, which isn't an issue at all, since this fallback should only be used as a safety net.
+       */
+    for (i = 0; i < (timeout / polling_period); i++) {
+        if (NRF_CLOCK->EVENTS_LFCLKSTARTED != 0)
+            return;
+        nrf_delay_us(polling_period);
+    }
+
+    /* Fallback to internal clock. Belt and braces, since the internal clock is used by default
+       whilst no external source is running. This is not only a sanity check, but it also allows
+       code down the road (e.g. ble initialisation) to directly know which clock is used. */
+    NRF_CLOCK->LFCLKSRC         = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
+    NRF_CLOCK->TASKS_LFCLKSTART = 1;
     while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) {
         // Do nothing.
     }
--- a/targets/hal/TARGET_Maxim/TARGET_MAX32600/PeripheralPins.c	Wed Aug 05 14:30:08 2015 +0100
+++ b/targets/hal/TARGET_Maxim/TARGET_MAX32600/PeripheralPins.c	Thu Aug 13 10:30:09 2015 +0100
@@ -185,49 +185,50 @@
 /************PWM***************/
 const PinMap PinMap_PWM[] = {
     {P0_0, PWM_0, 1}, {P0_0, PWM_0, 2}, {P0_0, PWM_4, 3},
-    {P0_1, PWM_0, 3}, {P0_1, PWM_1, 1}, {P0_1, PWM_4, 2},
-    {P0_2, PWM_1, 2}, {P0_2, PWM_2, 1}, {P0_2, PWM_5, 3},
-    {P0_3, PWM_1, 3}, {P0_3, PWM_3, 1}, {P0_3, PWM_5, 2},
-    {P0_4, PWM_2, 2}, {P0_4, PWM_4, 1}, {P0_4, PWM_6, 3},
-    {P0_5, PWM_2, 3}, {P0_5, PWM_5, 1}, {P0_5, PWM_6, 2},
-    {P0_6, PWM_3, 2}, {P0_6, PWM_6, 1}, {P0_6, PWM_7, 3},
-    {P0_7, PWM_3, 3}, {P0_7, PWM_7, 1}, {P0_7, PWM_7, 2},
+    {P0_1, PWM_1, 1}, {P0_1, PWM_4, 2}, {P0_1, PWM_0, 3},
+    {P0_2, PWM_2, 1}, {P0_2, PWM_1, 2}, {P0_2, PWM_5, 3},
+    {P0_3, PWM_3, 1}, {P0_3, PWM_5, 2}, {P0_3, PWM_1, 3},
+    {P0_4, PWM_4, 1}, {P0_4, PWM_2, 2}, {P0_4, PWM_6, 3},
+    {P0_5, PWM_5, 1}, {P0_5, PWM_6, 2}, {P0_5, PWM_2, 3},
+    {P0_6, PWM_6, 1}, {P0_6, PWM_3, 2}, {P0_6, PWM_7, 3},
+    {P0_7, PWM_7, 1}, {P0_7, PWM_7, 2}, {P0_7, PWM_3, 3},
 
     {P1_0, PWM_0, 1}, {P1_0, PWM_0, 2}, {P1_0, PWM_4, 3},
-    {P1_1, PWM_0, 3}, {P1_1, PWM_1, 1}, {P1_1, PWM_4, 2},
-    {P1_2, PWM_1, 2}, {P1_2, PWM_2, 1}, {P1_2, PWM_5, 3},
-    {P1_3, PWM_1, 3}, {P1_3, PWM_3, 1}, {P1_3, PWM_5, 2},
-    {P1_4, PWM_2, 2}, {P1_4, PWM_4, 1}, {P1_4, PWM_6, 3},
-    {P1_5, PWM_2, 3}, {P1_5, PWM_5, 1}, {P1_5, PWM_6, 2},
-    {P1_6, PWM_3, 2}, {P1_6, PWM_6, 1}, {P1_6, PWM_7, 3},
-    {P1_7, PWM_3, 3}, {P1_7, PWM_7, 1}, {P1_7, PWM_7, 2},
+    {P1_1, PWM_1, 1}, {P1_1, PWM_4, 2}, {P1_1, PWM_0, 3},
+    {P1_2, PWM_2, 1}, {P1_2, PWM_1, 2}, {P1_2, PWM_5, 3},
+    {P1_3, PWM_3, 1}, {P1_3, PWM_5, 2}, {P1_3, PWM_1, 3},
+    {P1_4, PWM_4, 1}, {P1_4, PWM_2, 2}, {P1_4, PWM_6, 3},
+    {P1_5, PWM_5, 1}, {P1_5, PWM_6, 2}, {P1_5, PWM_2, 3},
+    {P1_6, PWM_6, 1}, {P1_6, PWM_3, 2}, {P1_6, PWM_7, 3},
+    {P1_7, PWM_7, 1}, {P1_7, PWM_7, 2}, {P1_7, PWM_3, 3},
 
     {P2_0, PWM_0, 1}, {P2_0, PWM_0, 2}, {P2_0, PWM_4, 3},
-    {P2_1, PWM_0, 3}, {P2_1, PWM_1, 1}, {P2_1, PWM_4, 2},
-    {P2_2, PWM_1, 2}, {P2_2, PWM_2, 1}, {P2_2, PWM_5, 3},
-    {P2_3, PWM_1, 3}, {P2_3, PWM_3, 1}, {P2_3, PWM_5, 2},
-    {P2_4, PWM_2, 2}, {P2_4, PWM_4, 1}, {P2_4, PWM_6, 3},
-    {P2_5, PWM_2, 3}, {P2_5, PWM_5, 1}, {P2_5, PWM_6, 2},
-    {P2_6, PWM_3, 2}, {P2_6, PWM_6, 1}, {P2_6, PWM_7, 3},
-    {P2_7, PWM_3, 3}, {P2_7, PWM_7, 1}, {P2_7, PWM_7, 2},
+    {P2_1, PWM_1, 1}, {P2_1, PWM_4, 2}, {P2_1, PWM_0, 3},
+    {P2_2, PWM_2, 1}, {P2_2, PWM_1, 2}, {P2_2, PWM_5, 3},
+    {P2_3, PWM_3, 1}, {P2_3, PWM_5, 2}, {P2_3, PWM_1, 3},
+    {P2_4, PWM_4, 1}, {P2_4, PWM_2, 2}, {P2_4, PWM_6, 3},
+    {P2_5, PWM_5, 1}, {P2_5, PWM_6, 2}, {P2_5, PWM_2, 3},
+    {P2_6, PWM_6, 1}, {P2_6, PWM_3, 2}, {P2_6, PWM_7, 3},
+    {P2_7, PWM_7, 1}, {P2_7, PWM_7, 2}, {P2_7, PWM_3, 3},
 
     {P6_0, PWM_0, 1}, {P6_0, PWM_0, 2}, {P6_0, PWM_4, 3},
-    {P6_1, PWM_0, 3}, {P6_1, PWM_1, 1}, {P6_1, PWM_4, 2},
-    {P6_2, PWM_1, 2}, {P6_2, PWM_2, 1}, {P6_2, PWM_5, 3},
-    {P6_3, PWM_1, 3}, {P6_3, PWM_3, 1}, {P6_3, PWM_5, 2},
-    {P6_4, PWM_2, 2}, {P6_4, PWM_4, 1}, {P6_4, PWM_6, 3},
-    {P6_5, PWM_2, 3}, {P6_5, PWM_5, 1}, {P6_5, PWM_6, 2},
-    {P6_6, PWM_3, 2}, {P6_6, PWM_6, 1}, {P6_6, PWM_7, 3},
-    {P6_7, PWM_3, 3}, {P6_7, PWM_7, 1}, {P6_7, PWM_7, 2},
+    {P6_1, PWM_1, 1}, {P6_1, PWM_4, 2}, {P6_1, PWM_0, 3},
+    {P6_2, PWM_2, 1}, {P6_2, PWM_1, 2}, {P6_2, PWM_5, 3},
+    {P6_3, PWM_3, 1}, {P6_3, PWM_5, 2}, {P6_3, PWM_1, 3},
+    {P6_4, PWM_4, 1}, {P6_4, PWM_2, 2}, {P6_4, PWM_6, 3},
+    {P6_5, PWM_5, 1}, {P6_5, PWM_6, 2}, {P6_5, PWM_2, 3},
+    {P6_6, PWM_6, 1}, {P6_6, PWM_3, 2}, {P6_6, PWM_7, 3},
+    {P6_7, PWM_7, 1}, {P6_7, PWM_7, 2}, {P6_7, PWM_3, 3},
+
+    {P7_0, PWM_0, 1}, {P7_0, PWM_0, 2}, {P7_0, PWM_4, 3},
+    {P7_1, PWM_1, 1}, {P7_1, PWM_4, 2}, {P7_1, PWM_0, 3},
+    {P7_2, PWM_2, 1}, {P7_2, PWM_1, 2}, {P7_2, PWM_5, 3},
+    {P7_3, PWM_3, 1}, {P7_3, PWM_5, 2}, {P7_3, PWM_1, 3},
+    {P7_4, PWM_4, 1}, {P7_4, PWM_2, 2}, {P7_4, PWM_6, 3},
+    {P7_5, PWM_5, 1}, {P7_5, PWM_6, 2}, {P7_5, PWM_2, 3},
+    {P7_6, PWM_6, 1}, {P7_6, PWM_3, 2}, {P7_6, PWM_7, 3},
+    {P7_7, PWM_7, 1}, {P7_7, PWM_7, 2}, {P7_7, PWM_3, 3},
     
-    {P7_0, PWM_0, 1}, {P7_0, PWM_0, 2}, {P7_0, PWM_4, 3},
-    {P7_1, PWM_0, 3}, {P7_1, PWM_1, 1}, {P7_1, PWM_4, 2},
-    {P7_2, PWM_1, 2}, {P7_2, PWM_2, 1}, {P7_2, PWM_5, 3},
-    {P7_3, PWM_1, 3}, {P7_3, PWM_3, 1}, {P7_3, PWM_5, 2},
-    {P7_4, PWM_2, 2}, {P7_4, PWM_4, 1}, {P7_4, PWM_6, 3},
-    {P7_5, PWM_2, 3}, {P7_5, PWM_5, 1}, {P7_5, PWM_6, 2},
-    {P7_6, PWM_3, 2}, {P7_6, PWM_6, 1}, {P7_6, PWM_7, 3},
-    {P7_7, PWM_3, 3}, {P7_7, PWM_7, 1}, {P7_7, PWM_7, 2},
     {NC,   NC,    0}
 };
 
--- a/targets/hal/TARGET_Maxim/TARGET_MAX32600/pwmout_api.c	Wed Aug 05 14:30:08 2015 +0100
+++ b/targets/hal/TARGET_Maxim/TARGET_MAX32600/pwmout_api.c	Thu Aug 13 10:30:09 2015 +0100
@@ -52,55 +52,31 @@
 
     // Check if there is a pulse train already active on this port
     int pin_func = (MXC_GPIO->func_sel[port] & (0xF << (port_pin*4))) >> (port_pin*4);
-    if((pin_func > 0) && (pin_func < 4)) {
-        // Search through PinMap_PWM to find the active PT
-        while(pwm.pin != (PinName)NC) {
-            if((pwm.pin == pin) && (pwm.function == pin_func)) {
-                break;
-            }
-            pwm = PinMap_PWM[++i];
+    MBED_ASSERT((pin_func < 1) || (pin_func > 3));
+
+    // Search through PinMap_PWM to find the pin
+    while(pwm.pin != pin) {
+        pwm = PinMap_PWM[++i];
+    }
+
+    // Find a free PT instance on this pin
+    while(pwm.pin == pin) {
+
+        // Check to see if this PT instance is free
+        if((((mxc_pt_regs_t*)pwm.peripheral)->rate_length & 
+            MXC_F_PT_RATE_LENGTH_MODE)) {
+            break;
         }
 
-    } else {
-       // Search through PinMap_PWM to find an available PT
-        int i = 0;
-        while(pwm.pin != (PinName)NC && (i > -1)) {
-            pwm = PinMap_PWM[i++];
-            if(pwm.pin == pin) {
-                // Check each instance of PT
-                while(1) {
-                    // Check to see if this PT instance is already in use
-                    if((((mxc_pt_regs_t*)pwm.peripheral)->rate_length & 
-                        MXC_F_PT_RATE_LENGTH_MODE)) {
-                        i = -1;
-                        break;
-                    } 
+        pwm = PinMap_PWM[++i];
 
-                    // If all instances are in use, overwrite the last 
-                    pwm = PinMap_PWM[i++];
-                    if(pwm.pin != pin) {
-                        pwm = PinMap_PWM[(i-1)];
-                        i = -1; 
-                        break;
-                    }
-
-                }
-            }
-        } 
+        // Raise an assertion if we can not allocate another PT instance.
+        MBED_ASSERT(pwm.pin == pin);
     }
 
-    // Make sure we found an available PWM generator
-    MBED_ASSERT(pwm.pin != (PinName)NC);
-
-    // Disable all pwm output
-    MXC_PTG->ctrl = 0;
-
     // Enable the clock
     MXC_CLKMAN->clk_ctrl_2_pt = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
 
-    // Set the drive mode to normal
-    MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin*4)));
-
     // Set the obj pointer to the propper PWM instance
     obj->pwm = (mxc_pt_regs_t*)pwm.peripheral;
 
@@ -120,6 +96,9 @@
     pwmout_period_us(obj, 20000);
     pwmout_write    (obj, 0);
 
+    // Set the drive mode to normal
+    MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin*4)));
+
     // Enable the global pwm
     MXC_PTG->ctrl = MXC_F_PT_CTRL_ENABLE_ALL;
 }
--- a/targets/hal/TARGET_Maxim/TARGET_MAX32610/PeripheralPins.c	Wed Aug 05 14:30:08 2015 +0100
+++ b/targets/hal/TARGET_Maxim/TARGET_MAX32610/PeripheralPins.c	Thu Aug 13 10:30:09 2015 +0100
@@ -129,31 +129,32 @@
 /************PWM***************/
 const PinMap PinMap_PWM[] = {
     {P0_0, PWM_0, 1}, {P0_0, PWM_0, 2}, {P0_0, PWM_4, 3},
-    {P0_1, PWM_0, 3}, {P0_1, PWM_1, 1}, {P0_1, PWM_4, 2},
-    {P0_2, PWM_1, 2}, {P0_2, PWM_2, 1}, {P0_2, PWM_5, 3},
-    {P0_3, PWM_1, 3}, {P0_3, PWM_3, 1}, {P0_3, PWM_5, 2},
-    {P0_4, PWM_2, 2}, {P0_4, PWM_4, 1}, {P0_4, PWM_6, 3},
-    {P0_5, PWM_2, 3}, {P0_5, PWM_5, 1}, {P0_5, PWM_6, 2},
-    {P0_6, PWM_3, 2}, {P0_6, PWM_6, 1}, {P0_6, PWM_7, 3},
-    {P0_7, PWM_3, 3}, {P0_7, PWM_7, 1}, {P0_7, PWM_7, 2},
+    {P0_1, PWM_1, 1}, {P0_1, PWM_4, 2}, {P0_1, PWM_0, 3},
+    {P0_2, PWM_2, 1}, {P0_2, PWM_1, 2}, {P0_2, PWM_5, 3},
+    {P0_3, PWM_3, 1}, {P0_3, PWM_5, 2}, {P0_3, PWM_1, 3},
+    {P0_4, PWM_4, 1}, {P0_4, PWM_2, 2}, {P0_4, PWM_6, 3},
+    {P0_5, PWM_5, 1}, {P0_5, PWM_6, 2}, {P0_5, PWM_2, 3},
+    {P0_6, PWM_6, 1}, {P0_6, PWM_3, 2}, {P0_6, PWM_7, 3},
+    {P0_7, PWM_7, 1}, {P0_7, PWM_7, 2}, {P0_7, PWM_3, 3},
 
     {P1_0, PWM_0, 1}, {P1_0, PWM_0, 2}, {P1_0, PWM_4, 3},
-    {P1_1, PWM_0, 3}, {P1_1, PWM_1, 1}, {P1_1, PWM_4, 2},
-    {P1_2, PWM_1, 2}, {P1_2, PWM_2, 1}, {P1_2, PWM_5, 3},
-    {P1_3, PWM_1, 3}, {P1_3, PWM_3, 1}, {P1_3, PWM_5, 2},
-    {P1_4, PWM_2, 2}, {P1_4, PWM_4, 1}, {P1_4, PWM_6, 3},
-    {P1_5, PWM_2, 3}, {P1_5, PWM_5, 1}, {P1_5, PWM_6, 2},
-    {P1_6, PWM_3, 2}, {P1_6, PWM_6, 1}, {P1_6, PWM_7, 3},
-    {P1_7, PWM_3, 3}, {P1_7, PWM_7, 1}, {P1_7, PWM_7, 2},
+    {P1_1, PWM_1, 1}, {P1_1, PWM_4, 2}, {P1_1, PWM_0, 3},
+    {P1_2, PWM_2, 1}, {P1_2, PWM_1, 2}, {P1_2, PWM_5, 3},
+    {P1_3, PWM_3, 1}, {P1_3, PWM_5, 2}, {P1_3, PWM_1, 3},
+    {P1_4, PWM_4, 1}, {P1_4, PWM_2, 2}, {P1_4, PWM_6, 3},
+    {P1_5, PWM_5, 1}, {P1_5, PWM_6, 2}, {P1_5, PWM_2, 3},
+    {P1_6, PWM_6, 1}, {P1_6, PWM_3, 2}, {P1_6, PWM_7, 3},
+    {P1_7, PWM_7, 1}, {P1_7, PWM_7, 2}, {P1_7, PWM_3, 3},
 
     {P2_0, PWM_0, 1}, {P2_0, PWM_0, 2}, {P2_0, PWM_4, 3},
-    {P2_1, PWM_0, 3}, {P2_1, PWM_1, 1}, {P2_1, PWM_4, 2},
-    {P2_2, PWM_1, 2}, {P2_2, PWM_2, 1}, {P2_2, PWM_5, 3},
-    {P2_3, PWM_1, 3}, {P2_3, PWM_3, 1}, {P2_3, PWM_5, 2},
-    {P2_4, PWM_2, 2}, {P2_4, PWM_4, 1}, {P2_4, PWM_6, 3},
-    {P2_5, PWM_2, 3}, {P2_5, PWM_5, 1}, {P2_5, PWM_6, 2},
-    {P2_6, PWM_3, 2}, {P2_6, PWM_6, 1}, {P2_6, PWM_7, 3},
-    {P2_7, PWM_3, 3}, {P2_7, PWM_7, 1}, {P2_7, PWM_7, 2},
+    {P2_1, PWM_1, 1}, {P2_1, PWM_4, 2}, {P2_1, PWM_0, 3},
+    {P2_2, PWM_2, 1}, {P2_2, PWM_1, 2}, {P2_2, PWM_5, 3},
+    {P2_3, PWM_3, 1}, {P2_3, PWM_5, 2}, {P2_3, PWM_1, 3},
+    {P2_4, PWM_4, 1}, {P2_4, PWM_2, 2}, {P2_4, PWM_6, 3},
+    {P2_5, PWM_5, 1}, {P2_5, PWM_6, 2}, {P2_5, PWM_2, 3},
+    {P2_6, PWM_6, 1}, {P2_6, PWM_3, 2}, {P2_6, PWM_7, 3},
+    {P2_7, PWM_7, 1}, {P2_7, PWM_7, 2}, {P2_7, PWM_3, 3},
+    
     {NC,   NC,    0}
 };
 
--- a/targets/hal/TARGET_Maxim/TARGET_MAX32610/pwmout_api.c	Wed Aug 05 14:30:08 2015 +0100
+++ b/targets/hal/TARGET_Maxim/TARGET_MAX32610/pwmout_api.c	Thu Aug 13 10:30:09 2015 +0100
@@ -52,55 +52,31 @@
 
     // Check if there is a pulse train already active on this port
     int pin_func = (MXC_GPIO->func_sel[port] & (0xF << (port_pin*4))) >> (port_pin*4);
-    if((pin_func > 0) && (pin_func < 4)) {
-        // Search through PinMap_PWM to find the active PT
-        while(pwm.pin != (PinName)NC) {
-            if((pwm.pin == pin) && (pwm.function == pin_func)) {
-                break;
-            }
-            pwm = PinMap_PWM[++i];
+    MBED_ASSERT((pin_func < 1) || (pin_func > 3));
+
+    // Search through PinMap_PWM to find the pin
+    while(pwm.pin != pin) {
+        pwm = PinMap_PWM[++i];
+    }
+
+    // Find a free PT instance on this pin
+    while(pwm.pin == pin) {
+
+        // Check to see if this PT instance is free
+        if((((mxc_pt_regs_t*)pwm.peripheral)->rate_length & 
+            MXC_F_PT_RATE_LENGTH_MODE)) {
+            break;
         }
 
-    } else {
-       // Search through PinMap_PWM to find an available PT
-        int i = 0;
-        while(pwm.pin != (PinName)NC && (i > -1)) {
-            pwm = PinMap_PWM[i++];
-            if(pwm.pin == pin) {
-                // Check each instance of PT
-                while(1) {
-                    // Check to see if this PT instance is already in use
-                    if((((mxc_pt_regs_t*)pwm.peripheral)->rate_length & 
-                        MXC_F_PT_RATE_LENGTH_MODE)) {
-                        i = -1;
-                        break;
-                    } 
+        pwm = PinMap_PWM[++i];
 
-                    // If all instances are in use, overwrite the last 
-                    pwm = PinMap_PWM[i++];
-                    if(pwm.pin != pin) {
-                        pwm = PinMap_PWM[(i-1)];
-                        i = -1; 
-                        break;
-                    }
-
-                }
-            }
-        } 
+        // Raise an assertion if we can not allocate another PT instance.
+        MBED_ASSERT(pwm.pin == pin);
     }
 
-    // Make sure we found an available PWM generator
-    MBED_ASSERT(pwm.pin != (PinName)NC);
-
-    // Disable all pwm output
-    MXC_PTG->ctrl = 0;
-
     // Enable the clock
     MXC_CLKMAN->clk_ctrl_2_pt = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
 
-    // Set the drive mode to normal
-    MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin*4)));
-
     // Set the obj pointer to the propper PWM instance
     obj->pwm = (mxc_pt_regs_t*)pwm.peripheral;
 
@@ -120,6 +96,9 @@
     pwmout_period_us(obj, 20000);
     pwmout_write    (obj, 0);
 
+    // Set the drive mode to normal
+    MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin*4)));
+
     // Enable the global pwm
     MXC_PTG->ctrl = MXC_F_PT_CTRL_ENABLE_ALL;
 }
--- a/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c	Wed Aug 05 14:30:08 2015 +0100
+++ b/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c	Thu Aug 13 10:30:09 2015 +0100
@@ -159,10 +159,11 @@
         NRF_RTC1->EVENTS_OVRFLW = 0;
         NRF_RTC1->EVTENCLR      = RTC_EVTEN_OVRFLW_Msk;
     }
-    if (NRF_RTC1->EVENTS_COMPARE[0] && us_ticker_callbackPending && ((int)(us_ticker_callbackTimestamp - rtc1_getCounter()) <= 0)) {
+    if (NRF_RTC1->EVENTS_COMPARE[0]) {
         NRF_RTC1->EVENTS_COMPARE[0] = 0;
         NRF_RTC1->EVTENCLR          = RTC_EVTEN_COMPARE0_Msk;
-        invokeCallback();
+        if (us_ticker_callbackPending && ((int)(us_ticker_callbackTimestamp - rtc1_getCounter()) <= 0))
+            invokeCallback();
     }
 }