mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

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

Revision:
610:813dcc80987e
Parent:
596:abfb2833c9f5
--- a/targets/hal/TARGET_STM/TARGET_STM32F7/pwmout_api.c	Fri Aug 14 12:45:09 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32F7/pwmout_api.c	Fri Aug 14 13:15:17 2015 +0100
@@ -42,24 +42,21 @@
 {
     // 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);
 
     // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object
     uint32_t function = pinmap_function(pin, PinMap_PWM);
     MBED_ASSERT(function != (uint32_t)NC);
-    obj->channel  = STM_PIN_CHANNEL(function);
+    obj->channel = STM_PIN_CHANNEL(function);
     obj->inverted = STM_PIN_INVERTED(function);
 
-    if (obj->pwm == (PWMName)NC) {
-        error("PWM error: pinout mapping failed.");
-    }
-
     // Enable TIM clock
-    if (obj->pwm ==  PWM_1) __HAL_RCC_TIM1_CLK_ENABLE();
-    if (obj->pwm ==  PWM_2) __HAL_RCC_TIM2_CLK_ENABLE();
-    if (obj->pwm ==  PWM_3) __HAL_RCC_TIM3_CLK_ENABLE();
-    if (obj->pwm ==  PWM_4) __HAL_RCC_TIM4_CLK_ENABLE();
-    if (obj->pwm ==  PWM_8) __HAL_RCC_TIM8_CLK_ENABLE();
-    if (obj->pwm ==  PWM_9) __HAL_RCC_TIM9_CLK_ENABLE();
+    if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE();
+    if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE();
+    if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE();
+    if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE();
+    if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE();
+    if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE();
     if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE();
     if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE();
     if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE();
@@ -124,7 +121,7 @@
     }
 
     if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) {
-        error("Cannot configure PWM channel");
+        error("Cannot configure PWM channel\n");
     }
 
     if (obj->inverted) {
@@ -156,14 +153,21 @@
 void pwmout_period_us(pwmout_t* obj, int us)
 {
     TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
+    RCC_ClkInitTypeDef RCC_ClkInitStruct;
     uint32_t PclkFreq;
-
+    uint32_t APBxCLKDivider;
     float dc = pwmout_read(obj);
 
     __HAL_TIM_DISABLE(&TimHandle);
 
-    // Get the PCLK used by the timer
+    // Get clock configuration
+    // Note: PclkFreq contains here the Latency (not used after)
+    HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
+
+    // Get the PCLK and APBCLK divider related to the timer
     switch (obj->pwm) {
+
+        // APB1 clock
         case PWM_2:
         case PWM_3:
         case PWM_4:
@@ -172,26 +176,33 @@
         case PWM_13:
         case PWM_14:
             PclkFreq = HAL_RCC_GetPCLK1Freq();
+            APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider;
             break;
+
+        // APB2 clock
         case PWM_1:
         case PWM_8:
         case PWM_9:
         case PWM_10:
         case PWM_11:
             PclkFreq = HAL_RCC_GetPCLK2Freq();
+            APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider;
             break;
         default:
             return;
     }
 
     TimHandle.Init.Period        = us - 1;
-    // TIMxCLK = 2 x PCLKx when the APB prescaler is not equal to 1 (DIV4 or DIV2 in our case)
-    TimHandle.Init.Prescaler     = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
+    // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
+    if (APBxCLKDivider == RCC_HCLK_DIV1)
+      TimHandle.Init.Prescaler   = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 µs tick
+    else
+      TimHandle.Init.Prescaler   = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 µs tick
     TimHandle.Init.ClockDivision = 0;
     TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
 
     if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
-        error("Cannot initialize PWM");
+        error("Cannot initialize PWM\n");
     }
 
     // Set duty cycle again