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:
402:09075a3b15e3
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pinmap.c	Thu Mar 05 13:15:07 2015 +0000
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pinmap.c	Thu Mar 12 14:30:49 2015 +0000
@@ -32,19 +32,22 @@
 #include "PortNames.h"
 #include "mbed_error.h"
 
-// Alternate-function mapping
-#define AF_NUM (10)
-static const uint32_t AF_mapping[AF_NUM] = {
-    0,                        // 0 = No AF
-    GPIO_Remap_SPI1,          // 1
-    GPIO_Remap_I2C1,          // 2
-    GPIO_Remap_USART1,        // 3
-    GPIO_Remap_USART2,        // 4
-    GPIO_PartialRemap_USART3, // 5
-    GPIO_PartialRemap_TIM1,   // 6
-    GPIO_PartialRemap_TIM3,   // 7
-    GPIO_FullRemap_TIM2,      // 8
-    GPIO_FullRemap_TIM3       // 9
+// GPIO mode look-up table
+// Warning: the elements order must be the same as the one defined in PinNames.h
+static const uint32_t gpio_mode[13] = {
+    GPIO_MODE_INPUT,              //  0 = STM_MODE_INPUT
+    GPIO_MODE_OUTPUT_PP,          //  1 = STM_MODE_OUTPUT_PP
+    GPIO_MODE_OUTPUT_OD,          //  2 = STM_MODE_OUTPUT_OD
+    GPIO_MODE_AF_PP,              //  3 = STM_MODE_AF_PP
+    GPIO_MODE_AF_OD,              //  4 = STM_MODE_AF_OD
+    GPIO_MODE_ANALOG,             //  5 = STM_MODE_ANALOG
+    GPIO_MODE_IT_RISING,          //  6 = STM_MODE_IT_RISING
+    GPIO_MODE_IT_FALLING,         //  7 = STM_MODE_IT_FALLING
+    GPIO_MODE_IT_RISING_FALLING,  //  8 = STM_MODE_IT_RISING_FALLING
+    GPIO_MODE_EVT_RISING,         //  9 = STM_MODE_EVT_RISING
+    GPIO_MODE_EVT_FALLING,        // 10 = STM_MODE_EVT_FALLING
+    GPIO_MODE_EVT_RISING_FALLING, // 11 = STM_MODE_EVT_RISING_FALLING
+    0x10000000                    // 12 = STM_MODE_IT_EVT_RESET (not in STM32Cube HAL)
 };
 
 // Enable GPIO clock and return GPIO base address
@@ -54,22 +57,22 @@
     switch (port_idx) {
         case PortA:
             gpio_add = GPIOA_BASE;
-            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
+            __GPIOA_CLK_ENABLE();
             break;
         case PortB:
             gpio_add = GPIOB_BASE;
-            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
+            __GPIOB_CLK_ENABLE();
             break;
         case PortC:
             gpio_add = GPIOC_BASE;
-            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
+            __GPIOC_CLK_ENABLE();
             break;
         case PortD:
             gpio_add = GPIOD_BASE;
-            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
+            __GPIOD_CLK_ENABLE();
             break;
         default:
-            error("Port number is not correct.");
+            error("Pinmap error: wrong port number.");
             break;
     }
     return gpio_add;
@@ -83,6 +86,7 @@
     MBED_ASSERT(pin != (PinName)NC);
     // Get the pin informations
     uint32_t mode  = STM_PIN_MODE(data);
+    uint32_t pupd  = STM_PIN_PUPD(data);
     uint32_t afnum = STM_PIN_AFNUM(data);
 
     uint32_t port_index = STM_PORT(pin);
@@ -93,28 +97,59 @@
     GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
 
     // Enable AFIO clock
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
+    __HAL_RCC_AFIO_CLK_ENABLE();
 
     // Configure Alternate Function
     // Warning: Must be done before the GPIO is initialized
-    if ((afnum > 0) && (afnum < AF_NUM)) {
-        GPIO_PinRemapConfig(AF_mapping[afnum], ENABLE);
+    if (afnum > 0) {
+        switch (afnum) {
+            case 1: // Remap SPI1
+                __HAL_AFIO_REMAP_SPI1_ENABLE();
+                break;
+            case 2: // Remap I2C1
+                __HAL_AFIO_REMAP_I2C1_ENABLE();
+                break;
+            case 3: // Remap USART1
+                __HAL_AFIO_REMAP_USART1_ENABLE();
+                break;
+            case 4: // Remap USART2
+                __HAL_AFIO_REMAP_USART2_ENABLE();
+                break;
+            case 5: // Partial Remap USART3
+                __HAL_AFIO_REMAP_USART3_PARTIAL();
+                break;
+            case 6: // Partial Remap TIM1
+                __HAL_AFIO_REMAP_TIM1_PARTIAL();
+                break;
+            case 7: // Partial Remap TIM3
+                __HAL_AFIO_REMAP_TIM3_PARTIAL();
+                break;
+            case 8: // Full Remap TIM2
+                __HAL_AFIO_REMAP_TIM2_ENABLE();
+                break;
+            case 9: // Full Remap TIM3
+                __HAL_AFIO_REMAP_TIM3_ENABLE();
+                break;
+            default:
+                break;
+        }
     }
 
     // Configure GPIO
     GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.GPIO_Pin   = (uint16_t)(1 << pin_index);
-    GPIO_InitStructure.GPIO_Mode  = (GPIOMode_TypeDef)mode;
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-    GPIO_Init(gpio, &GPIO_InitStructure);
+    GPIO_InitStructure.Pin   = (uint32_t)(1 << pin_index);
+    GPIO_InitStructure.Mode  = gpio_mode[mode];
+    GPIO_InitStructure.Pull  = pupd;
+    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
+    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
 
     // Disconnect JTAG-DP + SW-DP signals.
     // Warning: Need to reconnect under reset
     if ((pin == PA_13) || (pin == PA_14)) {
-        GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
+        __HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled
     }
     if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
-        GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
+        __HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled
     }
 }
 
@@ -124,7 +159,6 @@
 void pin_mode(PinName pin, PinMode mode)
 {
     MBED_ASSERT(pin != (PinName)NC);
-    GPIO_InitTypeDef GPIO_InitStructure;
 
     uint32_t port_index = STM_PORT(pin);
     uint32_t pin_index  = STM_PIN(pin);
@@ -136,14 +170,22 @@
     // Configure open-drain and pull-up/down
     switch (mode) {
         case PullNone:
-            return;
+            break;
         case PullUp:
-            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
-            break;
         case PullDown:
-            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
+            // Set pull-up / pull-down for Input mode
+            if (pin_index < 8) {
+                if ((gpio->CRL & (0x03 << (pin_index * 4))) == 0) { // MODE bits = Input mode
+                    gpio->CRL |= (0x08 << (pin_index * 4)); // Set pull-up / pull-down
+                }
+            } else {
+                if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) == 0) { // MODE bits = Input mode
+                    gpio->CRH |= (0x08 << ((pin_index % 8) * 4)); // Set pull-up / pull-down
+                }
+            }
             break;
         case OpenDrain:
+            // Set open-drain for Output mode (General Purpose or Alternate Function)
             if (pin_index < 8) {
                 if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode
                     gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain
@@ -153,13 +195,8 @@
                     gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain
                 }
             }
-            return;
+            break;
         default:
             break;
     }
-
-    // Configure GPIO
-    GPIO_InitStructure.GPIO_Pin   = (uint16_t)(1 << pin_index);
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-    GPIO_Init(gpio, &GPIO_InitStructure);
 }