stm32.cube(三)——HAL.GPIO
HAL層里的庫(kù)函數(shù)和大家熟悉的stm32lib基本上是一致的。一般來(lái)說(shuō)一個(gè)cpu外設(shè)的操作函數(shù)分成兩部分,分別是init和operation。
來(lái)看一個(gè)cube里的例子:
static GPIO_InitTypeDef GPIO_InitStruct;
int main(void)
{
/* This sample code shows how to use GPIO HAL API to toggle LED2 IO
in an infinite loop. */
/* STM32F103xB HAL library initialization:
- Configure the Flash prefetch
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();
/* Configure the system clock to 64 MHz */
SystemClock_Config();
/* -1- Enable GPIO Clock (to be able to program the configuration registers) */
LED2_GPIO_CLK_ENABLE();
/* -2- Configure IO in output push-pull mode to drive external LEDs */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Pin = LED2_PIN;
HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStruct);
/* -3- Toggle IO in an infinite loop */
while (1)
{
HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
/* Insert delay 100 ms */
HAL_Delay(100);
}
}
注釋里的內(nèi)容說(shuō)明了之前一篇文章里的提到的HAL_INIT()函數(shù)的作用。
在使用GPIO前要先初始化GPIO的時(shí)鐘,因?yàn)樵S多外設(shè)都被設(shè)計(jì)成時(shí)序邏輯電路,所以必須要為外設(shè)提供時(shí)鐘源,否則外設(shè)的電路狀態(tài)就不會(huì)改變。
GPIO時(shí)鐘源設(shè)定后,通過(guò)GPIO_initTypeDef結(jié)構(gòu)體,初始化指定Pin腳的模式、速度、初始輸出。
最后調(diào)用 HAL_GPIO_TogglePin()和HAL_Delay()將GPIO定時(shí)取反以達(dá)到使驅(qū)動(dòng)的led閃爍的效果。
二、GPIO的初始化
GPIO_InitTypeDef結(jié)構(gòu)體用于對(duì)一個(gè)或者一組GPIO來(lái)進(jìn)行初始化,它的四個(gè)成員分別是,Pin、Mode、Pull、Speed。
GPIO_InitTypeDef原型:
/**
* @brief GPIO Init structure definition
*/
typedef struct
{
uint32_t Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
uint32_t Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIO_mode_define */
uint32_t Pull; /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.
This parameter can be a value of @ref GPIO_pull_define */
uint32_t Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIO_speed_define */
}GPIO_InitTypeDef;
Pin
#define GPIO_PIN_0 ((uint16_t)0x0001) /* Pin 0 selected */
#define GPIO_PIN_1 ((uint16_t)0x0002) /* Pin 1 selected */
#define GPIO_PIN_2 ((uint16_t)0x0004) /* Pin 2 selected */
#define GPIO_PIN_3 ((uint16_t)0x0008) /* Pin 3 selected */
#define GPIO_PIN_4 ((uint16_t)0x0010) /* Pin 4 selected */
#define GPIO_PIN_5 ((uint16_t)0x0020) /* Pin 5 selected */
#define GPIO_PIN_6 ((uint16_t)0x0040) /* Pin 6 selected */
#define GPIO_PIN_7 ((uint16_t)0x0080) /* Pin 7 selected */
#define GPIO_PIN_8 ((uint16_t)0x0100) /* Pin 8 selected */
#define GPIO_PIN_9 ((uint16_t)0x0200) /* Pin 9 selected */
#define GPIO_PIN_10 ((uint16_t)0x0400) /* Pin 10 selected */
#define GPIO_PIN_11 ((uint16_t)0x0800) /* Pin 11 selected */
#define GPIO_PIN_12 ((uint16_t)0x1000) /* Pin 12 selected */
#define GPIO_PIN_13 ((uint16_t)0x2000) /* Pin 13 selected */
#define GPIO_PIN_14 ((uint16_t)0x4000) /* Pin 14 selected */
#define GPIO_PIN_15 ((uint16_t)0x8000) /* Pin 15 selected */
#define GPIO_PIN_All ((uint16_t)0xFFFF) /* All pins selected */
選擇多個(gè)Pin的時(shí)候,可以用位選的宏定義相或得到。
Mode
#define GPIO_MODE_INPUT ((uint32_t)0x00000000) /*!< Input Floating Mode */
#define GPIO_MODE_OUTPUT_PP ((uint32_t)0x00000001) /*!< Output Push Pull Mode */
#define GPIO_MODE_OUTPUT_OD ((uint32_t)0x00000011) /*!< Output Open Drain Mode */
#define GPIO_MODE_AF_PP ((uint32_t)0x00000002) /*!< Alternate Function Push Pull Mode */
#define GPIO_MODE_AF_OD ((uint32_t)0x00000012) /*!< Alternate Function Open Drain Mode */
#define GPIO_MODE_AF_INPUT GPIO_MODE_INPUT /*!< Alternate Function Input Mode */
#define GPIO_MODE_ANALOG ((uint32_t)0x00000003) /*!< Analog Mode */
#define GPIO_MODE_IT_RISING ((uint32_t)0x10110000) /*!< External Interrupt Mode with Rising edge trigger detection */
#define GPIO_MODE_IT_FALLING ((uint32_t)0x10210000) /*!< External Interrupt Mode with Falling edge trigger detection */
#define GPIO_MODE_IT_RISING_FALLING ((uint32_t)0x10310000) /*!< External Interrupt Mode with Rising/Falling edge trigger detection */
#define GPIO_MODE_EVT_RISING ((uint32_t)0x10120000) /*!< External Event Mode with Rising edge trigger detection */
#define GPIO_MODE_EVT_FALLING ((uint32_t)0x10220000) /*!< External Event Mode with Falling edge trigger detection */
#define GPIO_MODE_EVT_RISING_FALLING ((uint32_t)0x10320000) /*!< External Event Mode with Rising/Falling edge trigger detection */
GPIO的模式理解起來(lái)有點(diǎn)復(fù)雜。
GPIO可以被用作中斷源(GPIO_MODE_IT)和事件源(GPIO_MODE_EVT),這以后再敘述。
GPIO也可以用來(lái)接收模擬量(GPIO_MODE_ANALOG)。
如果GPIO原本是一個(gè)復(fù)用I/O,比如可以被作為串口模塊使用,要用GPIO_MODE_AF_x來(lái)設(shè)置。
GPIO可以被設(shè)置成輸入或者輸出。
當(dāng)被設(shè)置成輸出時(shí),有open-drain和push-pull兩者模式。我覺(jué)得理解這兩種模式對(duì)于像我一樣的模電知識(shí)較薄弱的人來(lái)說(shuō)有點(diǎn)困難。而且此類知識(shí)的理解需要做實(shí)驗(yàn)來(lái)輔助,在沒(méi)有實(shí)驗(yàn)環(huán)境的情況下,還要學(xué)習(xí)電路仿真軟件的使用,學(xué)習(xí)成本較高,就不做敘述,直接說(shuō)結(jié)論了:
push-pull模式,速度快,但功耗大。
open-drain模式,功耗低,同時(shí)有線與功能。如果此模式芯片內(nèi)部沒(méi)有上拉電路,需要外接上拉電阻。
Pull
#define GPIO_NOPULL ((uint32_t)0x00000000) /*!< No Pull-up or Pull-down activation */
#define GPIO_PULLUP ((uint32_t)0x00000001) /*!< Pull-up activation */
#define GPIO_PULLDOWN