lzbp li / mbed-stm32l0/l1-src

Dependents:   SANFAN_read_analog_value nucleo-wdg Nucleo_sleep_copy

Fork of mbed-src by mbed official

Revision:
354:e67efb2aab0e
Parent:
285:31249416b6f9
Child:
414:4ec4c5b614b0
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pinmap.c	Thu Oct 16 14:45:07 2014 +0100
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pinmap.c	Thu Oct 16 15:00:10 2014 +0100
@@ -32,32 +32,50 @@
 #include "PortNames.h"
 #include "mbed_error.h"
 
+// GPIO mode look-up table
+static const uint32_t gpio_mode[13] = {
+    0x00000000, //  0 = GPIO_MODE_INPUT
+    0x00000001, //  1 = GPIO_MODE_OUTPUT_PP
+    0x00000011, //  2 = GPIO_MODE_OUTPUT_OD
+    0x00000002, //  3 = GPIO_MODE_AF_PP
+    0x00000012, //  4 = GPIO_MODE_AF_OD
+    0x00000003, //  5 = GPIO_MODE_ANALOG
+    0x10110000, //  6 = GPIO_MODE_IT_RISING
+    0x10210000, //  7 = GPIO_MODE_IT_FALLING
+    0x10310000, //  8 = GPIO_MODE_IT_RISING_FALLING
+    0x10120000, //  9 = GPIO_MODE_EVT_RISING
+    0x10220000, // 10 = GPIO_MODE_EVT_FALLING
+    0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
+    0x10000000  // 12 = Reset IT and EVT (not in STM32Cube HAL)
+};
+
 // Enable GPIO clock and return GPIO base address
-uint32_t Set_GPIO_Clock(uint32_t port_idx) {
+uint32_t Set_GPIO_Clock(uint32_t port_idx)
+{
     uint32_t gpio_add = 0;
     switch (port_idx) {
         case PortA:
             gpio_add = GPIOA_BASE;
-            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
+            __GPIOA_CLK_ENABLE();
             break;
         case PortB:
             gpio_add = GPIOB_BASE;
-            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
+            __GPIOB_CLK_ENABLE();
             break;
         case PortC:
             gpio_add = GPIOC_BASE;
-            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
+            __GPIOC_CLK_ENABLE();
             break;
         case PortD:
             gpio_add = GPIOD_BASE;
-            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
+            __GPIOD_CLK_ENABLE();
             break;
         case PortH:
             gpio_add = GPIOH_BASE;
-            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOH, ENABLE);
+            __GPIOH_CLK_ENABLE();
             break;
         default:
-            error("Port number is not correct.");
+            error("Pinmap error: wrong port number.");
             break;
     }
     return gpio_add;
@@ -66,12 +84,11 @@
 /**
  * Configure pin (mode, speed, output type and pull-up/pull-down)
  */
-void pin_function(PinName pin, int data) {
+void pin_function(PinName pin, int data)
+{
     MBED_ASSERT(pin != (PinName)NC);
-
     // Get the pin informations
     uint32_t mode  = STM_PIN_MODE(data);
-    uint32_t otype = STM_PIN_OTYPE(data);
     uint32_t pupd  = STM_PIN_PUPD(data);
     uint32_t afnum = STM_PIN_AFNUM(data);
 
@@ -82,20 +99,14 @@
     uint32_t gpio_add = Set_GPIO_Clock(port_index);
     GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
 
-    // Configure Alternate Function
-    // Warning: Must be done before the GPIO is initialized
-    if (afnum != 0xFF) {
-        GPIO_PinAFConfig(gpio, (uint16_t)pin_index, afnum);
-    }
-
     // 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_40MHz;
-    GPIO_InitStructure.GPIO_OType = (GPIOOType_TypeDef)otype;
-    GPIO_InitStructure.GPIO_PuPd  = (GPIOPuPd_TypeDef)pupd;
-    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;
+    GPIO_InitStructure.Alternate = afnum;
+    HAL_GPIO_Init(gpio, &GPIO_InitStructure);
 
     // [TODO] Disconnect JTAG-DP + SW-DP signals.
     // Warning: Need to reconnect under reset
@@ -110,9 +121,9 @@
 /**
  * Configure pin pull-up/pull-down
  */
-void pin_mode(PinName pin, PinMode mode) {
+void pin_mode(PinName pin, PinMode mode)
+{
     MBED_ASSERT(pin != (PinName)NC);
-
     uint32_t port_index = STM_PORT(pin);
     uint32_t pin_index  = STM_PIN(pin);
 
@@ -122,8 +133,9 @@
 
     // Configure pull-up/pull-down resistors
     uint32_t pupd = (uint32_t)mode;
-    if (pupd > 2)
+    if (pupd > 2) {
         pupd = 0; // Open-drain = No pull-up/No pull-down
+    }
     gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
     gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));