×
单片机 > 单片机程序设计 > 详情

stm32.cube(三)——HAL.GPIO

发布时间:2020-08-28 发布时间:
|

一、总览

HAL层里的库函数和大家熟悉的stm32lib基本上是一致的。一般来说一个cpu外设的操作函数分成两部分,分别是init和operation。

来看一个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);

  }

}

注释里的内容说明了之前一篇文章里的提到的HAL_INIT()函数的作用。


在使用GPIO前要先初始化GPIO的时钟,因为许多外设都被设计成时序逻辑电路,所以必须要为外设提供时钟源,否则外设的电路状态就不会改变。


GPIO时钟源设定后,通过GPIO_initTypeDef结构体,初始化指定Pin脚的模式、速度、初始输出。


最后调用 HAL_GPIO_TogglePin()和HAL_Delay()将GPIO定时取反以达到使驱动的led闪烁的效果。


二、GPIO的初始化


GPIO_InitTypeDef结构体用于对一个或者一组GPIO来进行初始化,它的四个成员分别是,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 */


选择多个Pin的时候,可以用位选的宏定义相或得到。

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的模式理解起来有点复杂。

GPIO可以被用作中断源(GPIO_MODE_IT)和事件源(GPIO_MODE_EVT),这以后再叙述。

GPIO也可以用来接收模拟量(GPIO_MODE_ANALOG)。

如果GPIO原本是一个复用I/O,比如可以被作为串口模块使用,要用GPIO_MODE_AF_x来设置。

GPIO可以被设置成输入或者输出。

当被设置成输出时,有open-drain和push-pull两者模式。我觉得理解这两种模式对于像我一样的模电知识较薄弱的人来说有点困难。而且此类知识的理解需要做实验来辅助,在没有实验环境的情况下,还要学习电路仿真软件的使用,学习成本较高,就不做叙述,直接说结论了:

push-pull模式,速度快,但功耗大。

open-drain模式,功耗低,同时有线与功能。如果此模式芯片内部没有上拉电路,需要外接上拉电阻。


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      ((uint32_t)0x00000002)   /*!< Pull-down activation                */


拉高或拉低。


Speed


#define  GPIO_SPEED_LOW              (GPIO_CRL_MODE0_1) /*!< Low speed */

#define  GPIO_SPEED_MEDIUM           (GPIO_CRL_MODE0_0) /*!< Medium speed */

#define  GPIO_SPEED_HIGH             (GPIO_CRL_MODE0)   /*!< High speed */


不同的速率会产生不同的功耗和噪声,设置Speed的时候要根据实际应用来选取GPIO的工作频率。


设置好属性以后,调用HAL_GPIO_Init来初始化GPIO,函数的第一个参数是一个指向GPIO寄存器组的指针,这个结构体是在芯片手册里定义的。其他对GPIO的操作也都需要传递这个指针,方面在函数里直接访问寄存器,也可以使语义更加清晰。


三、GPIO的操作


GPIO operation函数


GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

void          HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState);

void          HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

void          HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin);

void          HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);


HAL里的GPIO操作函数,较之之前的stmlib库更加简单了,基础的操作是读、写、锁、取反,就不做叙述了。


HAL_GPIO_EXTI_x是GPIO用作外部中断时使用的函数。


四、总结


对任一外设,都要先进行初始化,然后再进行操作。通常初始化一个外设的方法就是建立一个描述该外设属性的结构体,用宏定义约定参数值代表的含义。然后将该外设的配置寄存器指针传给初始化函数,初始化函数解释各属性并写相应寄存器。







『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
如何为单片机选择合适的负载电容