Output Toggle

该示例演示使用 GPIO 输出功能驱动LED进行翻转。 首先将GPIO配置为输出来驱动LED。然后进入while循环,每1000毫秒翻转一次LED状态。

环境需求

该示例支持以下开发套件:

开发套件

Hardware Platforms

Board Name

RTL87x2G HDK

RTL87x2G EVB

更多信息请参考 快速入门

硬件连线

连接P1_0和LED0。 LED驱动电路如下图所示。

这里应该是LED驱动电路图片

LED驱动电路图

编译和下载

该示例的工程路径如下:

Project file: samples\peripheral\gpio\output_toggle\proj\rtl87x2g\mdk

Project file: samples\peripheral\gpio\output_toggle\proj\rtl87x2g\gcc

请按照以下步骤操作构建并运行该示例:

  1. 打开工程文件。

  2. 按照 快速入门编译APP Image 给出的步骤构建目标文件。

  3. 编译成功后,在路径 mdk\bingcc\bin 下会生成 app bin app_MP_xxx.bin 文件。

  4. 按照 快速入门MPTool 给出的步骤将app bin烧录至EVB内。

  5. 按下复位按键,开始运行。

测试验证

  1. 当EVB启动后,在DebugAnalyzer工具内观察如下LOG。

    Start gpio toggle test!
    
  2. 观察LED灯闪烁情况。

代码介绍

该章节分为以下几个部分:

  1. 源码路径.

  2. 初始化函数将在 初始化 章节介绍

  3. 初始化后的功能实现将在 功能实现 章节介绍

源码路径

  1. 工程路径: sdk\samples\peripheral\gpio\output_toggle\proj

  2. 源码路径: sdk\samples\peripheral\gpio\output_toggle\src

该工程的工程文件代码结构如下:

└── Project: output_toggle
    └── secure_only_app
        └── Device                   includes startup code
            ├── startup_rtl.c
            └── system_rtl.c
        ├── CMSIS                    includes CMSIS header files
        ├── CMSE Library             Non-secure callable lib
        ├── lib                      includes all binary symbol files that user application is built on
            └── rtl87x2g_io.lib
        ├── peripheral               includes all peripheral drivers and module code used by the application
            ├── rtl_gpio.c
            ├── rtl_pinmux.c
            └── rtl_rcc.c
        └── APP                      includes the ble_peripheral user application implementation
            ├── io_gpio.c
            └── main_ns.c

初始化

初始化流程包括了 board_gpio_initdriver_gpio_init


board_gpio_init 中包含了PAD与PINMUX设置:

  1. 配置PAD:设置引脚、PINMUX模式、PowerOn、无内部上拉。

  2. 配置PINMUX:分配引脚为GPIO功能。


driver_gpio_init 包含了对GPIO外设的初始化。

  1. 使能 PCC 时钟源。

  2. 设置 GPIO_Mode 为输出模式。

RCC_PeriphClockCmd(APBPERIPH_GPIO, APBPERIPH_GPIO_CLOCK, ENABLE);
......
GPIO_InitStruct.GPIO_Mode   = GPIO_MODE_OUT;

功能实现

初始化后执行while循环,循环体内执行 GPIO_WriteBit() 函数,控制GPIO输出高低电平。

while (1)
{
    /* Set GPIO_PIN_OUTPUT */
    GPIO_WriteBit(GPIO_PORT, GPIO_PIN, (BitAction)(1));
    platform_delay_ms(1000);
    /* Reset GPIO_PIN_OUTPUT */
    GPIO_WriteBit(GPIO_PORT, GPIO_PIN, (BitAction)(0));
    platform_delay_ms(1000);
}