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:
489:119543c9f674
Parent:
414:4ec4c5b614b0
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/analogin_api.c	Thu Mar 05 13:15:07 2015 +0000
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/analogin_api.c	Thu Mar 12 14:30:49 2015 +0000
@@ -30,17 +30,18 @@
 
 #if DEVICE_ANALOGIN
 
+#include "wait_api.h"
 #include "cmsis.h"
 #include "pinmap.h"
-#include "wait_api.h"
 #include "PeripheralPins.h"
 
+ADC_HandleTypeDef AdcHandle;
+
 int adc_inited = 0;
 
 void analogin_init(analogin_t *obj, PinName pin)
 {
-    ADC_TypeDef *adc;
-    ADC_InitTypeDef ADC_InitStructure;
+    RCC_PeriphCLKInitTypeDef  PeriphClkInit;
 
     // Get the peripheral name from the pin and assign it to the object
     obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
@@ -56,101 +57,105 @@
     if (adc_inited == 0) {
         adc_inited = 1;
 
-        // Get ADC registers structure address
-        adc = (ADC_TypeDef *)(obj->adc);
+        // Enable ADC clock
+        __HAL_RCC_ADC1_CLK_ENABLE();
 
-        // Enable ADC clock (14 MHz maximum)
-        // PCLK2 = 64 MHz --> ADC clock = 64/6 = 10.666 MHz
-        RCC_ADCCLKConfig(RCC_PCLK2_Div6);
-        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
+        // Configure ADC clock prescaler
+        // Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet).
+        // Therefore, ADC clock prescaler must be configured in function
+        // of ADC clock source frequency to remain below this maximum frequency.
+        // with 8 MHz external xtal: PCLK2 = 72 MHz --> ADC clock = 72/6 = 12 MHz
+        // with internal clock     : PCLK2 = 64 MHz --> ADC clock = 64/6 = 10.67 MHz
+        PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
+        PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
+        HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
 
         // Configure ADC
-        ADC_InitStructure.ADC_Mode               = ADC_Mode_Independent;
-        ADC_InitStructure.ADC_ScanConvMode       = DISABLE;
-        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
-        ADC_InitStructure.ADC_ExternalTrigConv   = ADC_ExternalTrigConv_None;
-        ADC_InitStructure.ADC_DataAlign          = ADC_DataAlign_Right;
-        ADC_InitStructure.ADC_NbrOfChannel       = 1;
-        ADC_Init(adc, &ADC_InitStructure);
-
-        // Enable ADC
-        ADC_Cmd(adc, ENABLE);
-
-        // Calibrate ADC
-        ADC_ResetCalibration(adc);
-        while (ADC_GetResetCalibrationStatus(adc));
-        ADC_StartCalibration(adc);
-        while (ADC_GetCalibrationStatus(adc));
+        AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
+        AdcHandle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
+        AdcHandle.Init.ScanConvMode          = DISABLE;
+        AdcHandle.Init.ContinuousConvMode    = DISABLE;
+        AdcHandle.Init.NbrOfConversion       = 1;
+        AdcHandle.Init.DiscontinuousConvMode = DISABLE;
+        AdcHandle.Init.NbrOfDiscConversion   = 0;
+        AdcHandle.Init.ExternalTrigConv      = ADC_EXTERNALTRIGCONV_T1_CC1;
+        HAL_ADC_Init(&AdcHandle);
     }
 }
 
 static inline uint16_t adc_read(analogin_t *obj)
 {
-    // Get ADC registers structure address
-    ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc);
-    int channel = 0;
+    ADC_ChannelConfTypeDef sConfig;
+
+    AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
 
     // Configure ADC channel
+    sConfig.Rank         = 1;
+    sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
+
     switch (obj->pin) {
         case PA_0:
-            channel = 0;
+            sConfig.Channel = ADC_CHANNEL_0;
             break;
         case PA_1:
-            channel = 1;
+            sConfig.Channel = ADC_CHANNEL_1;
             break;
         case PA_2:
-            channel = 2;
+            sConfig.Channel = ADC_CHANNEL_2;
             break;
         case PA_3:
-            channel = 3;
+            sConfig.Channel = ADC_CHANNEL_3;
             break;
         case PA_4:
-            channel = 4;
+            sConfig.Channel = ADC_CHANNEL_4;
             break;
         case PA_5:
-            channel = 5;
+            sConfig.Channel = ADC_CHANNEL_5;
             break;
         case PA_6:
-            channel = 6;
+            sConfig.Channel = ADC_CHANNEL_6;
             break;
         case PA_7:
-            channel = 7;
+            sConfig.Channel = ADC_CHANNEL_7;
             break;
         case PB_0:
-            channel = 8;
+            sConfig.Channel = ADC_CHANNEL_8;
             break;
         case PB_1:
-            channel = 9;
+            sConfig.Channel = ADC_CHANNEL_9;
             break;
         case PC_0:
-            channel = 10;
+            sConfig.Channel = ADC_CHANNEL_10;
             break;
         case PC_1:
-            channel = 11;
+            sConfig.Channel = ADC_CHANNEL_11;
             break;
         case PC_2:
-            channel = 12;
+            sConfig.Channel = ADC_CHANNEL_12;
             break;
         case PC_3:
-            channel = 13;
+            sConfig.Channel = ADC_CHANNEL_13;
             break;
         case PC_4:
-            channel = 14;
+            sConfig.Channel = ADC_CHANNEL_14;
             break;
         case PC_5:
-            channel = 15;
+            sConfig.Channel = ADC_CHANNEL_15;
             break;
         default:
             return 0;
     }
 
-    ADC_RegularChannelConfig(adc, channel, 1, ADC_SampleTime_7Cycles5);
+    HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
 
-    ADC_SoftwareStartConvCmd(adc, ENABLE); // Start conversion
+    HAL_ADC_Start(&AdcHandle); // Start conversion
 
-    while (ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion
-
-    return (ADC_GetConversionValue(adc)); // Get conversion value
+    // Wait end of conversion and get value
+    if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
+        return (HAL_ADC_GetValue(&AdcHandle));
+    } else {
+        return 0;
+    }
 }
 
 uint16_t analogin_read_u16(analogin_t *obj)