Fawwaz Nadzmy / mbed-src

Fork of mbed-src by mbed official

Revision:
531:47d2b67c511f
Parent:
491:b97b6b70036c
--- a/targets/hal/TARGET_STM/TARGET_STM32L0/pwmout_api.c	Thu Apr 30 09:15:06 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32L0/pwmout_api.c	Thu Apr 30 13:00:08 2015 +0100
@@ -1,6 +1,6 @@
 /* mbed Microcontroller Library
  *******************************************************************************
- * Copyright (c) 2014, STMicroelectronics
+ * Copyright (c) 2015, STMicroelectronics
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -42,10 +42,13 @@
 {
     // Get the peripheral name from the pin and assign it to the object
     obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
+    MBED_ASSERT(obj->pwm != (PWMName)NC);
 
-    if (obj->pwm == (PWMName)NC) {
-        error("PWM error: pinout mapping failed.");
-    }
+    // Get the pin function and assign the used channel to the object
+    uint32_t function = pinmap_function(pin, PinMap_PWM);
+    MBED_ASSERT(function != (uint32_t)NC);
+    obj->channel = STM_PIN_CHANNEL(function);
+    obj->inverted = STM_PIN_INVERTED(function);
 
     // Enable TIM clock
     if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
@@ -92,43 +95,27 @@
     sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
     sConfig.OCFastMode   = TIM_OCFAST_ENABLE;
 
-    switch (obj->pin) {
-        // Channels 1
-        case PA_0:
-        case PA_5:
-        case PA_6:
-        case PA_15:
-        case PB_4:
-        case PC_6:
+    switch (obj->channel) {
+        case 1:
             channel = TIM_CHANNEL_1;
             break;
-        // Channels 2
-        case PA_1:
-        case PA_7:
-        case PB_3:
-        case PB_5:
-        case PC_7:
+        case 2:
             channel = TIM_CHANNEL_2;
             break;
-        // Channels 3
-        case PA_2:
-        case PB_0:
-        case PB_10:
-        case PC_8:
+        case 3:
             channel = TIM_CHANNEL_3;
             break;
-        // Channels 4
-        case PA_3:
-        case PB_1:
-        case PB_11:
-        case PC_9:
+        case 4:
             channel = TIM_CHANNEL_4;
             break;
         default:
             return;
     }
 
-    HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
+    if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
+        error("Cannot initialize PWM");
+    }
+
     HAL_TIM_PWM_Start(&TimHandle, channel);
 }
 
@@ -163,7 +150,10 @@
     TimHandle.Init.Prescaler     = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
     TimHandle.Init.ClockDivision = 0;
     TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
-    HAL_TIM_PWM_Init(&TimHandle);
+
+    if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
+        error("Cannot initialize PWM");
+    }
 
     // Set duty cycle again
     pwmout_write(obj, dc);