Output Toggle

This sample demonstrates how to use the GPIO output function by toggling an LED.

This sample starts with configuring the GPIO as output to drive the LED. Then enter the while loop, toggling the LED state at each short interval.

Requirements

The sample supports the following development kits:

Development Kits

Hardware Platforms

Board Name

RTL8752H HDK

RTL8752H EVB

For more requirements, please refer to Quick Start.

Wiring

Connect P0_1 to LED0. The LED driver circuit is shown below.

Here should be a picture of the LED driver circuit

LED Driver Circuit Diagram

Building and Downloading

This sample can be found in the SDK folder:

Project file: board\evb\io_sample\GPIO\Output_led\mdk

Project file: board\evb\io_sample\GPIO\Output_led\gcc

Please follow these steps to build and run the example:

  1. Open sample project file.

  2. To build the target, follow the steps listed on the Generating App Image in Quick Start.

  3. After a successful compilation, the app bin app_MP_xxx.bin will be generated in the directory mdk\bin or gcc\bin.

  4. To download app bin into EVB board, follow the steps listed on the MP Tool Download in Quick Start.

  5. Press reset button on EVB board and it will start running.

Experimental Verification

Observe that the LED are Blinking.

Code Overview

This chapter will be introduced according to the following several parts:

  1. Source Code Directory.

  2. Peripheral initialization will be introduced in chapter Initialization.

  3. Functional implementation after initialization will be introduced in chapter Function Implementation.

Source Code Directory

  • Project directory: sdk\board\evb\io_sample\GPIO\Output_led

  • Source code directory: sdk\src\sample\io_sample\GPIO\Output_led

Source files are currently categorized into several groups as below.

└── Project: input_polling
    └── secure_only_app
        └── include
            ├── app_define.h
            └── rom_uuid.h
        ├── cmsis                    includes CMSIS header files and startup files
            ├── overlay_mgr.c
            ├── system_rtl876x.c
            └── startup_rtl876x.s
        ├── lib                      includes all binary symbol files that user application is built on
            ├── rtl8752h_sdk.lib
            ├── gap_utils.lib
            └── ROM.lib
        ├── peripheral               includes all peripheral drivers and module code used by the application
            ├── rtl876x_rcc.c
            ├── rtl876x_pinmux.c
            ├── rtl876x_nvic.c
            └── rtl876x_gpio.c
        ├── profile
        └── app                      includes the ble_peripheral user application implementation
            └── main.c

Initialization

When the EVB reset, the main function is executed, following these steps:

int main(void)
{
    extern uint32_t random_seed_value;
    srand(random_seed_value);
    gpio_demo();

    ...
}

In gpio_demo, it includes the PAD/PINMUX setup, and GPIO peripheral initialization.

void gpio_demo(void)
{
    /* Configure pad and pinmux firstly! */
    board_gpio_init();

    /* Initialize gpio peripheral */
    driver_gpio_init();

}

board_gpio_init sets up the PAD/PINMUX with the following process:

  1. Configure PAD: Set the pins, PINMUX mode, PowerOn, internal pull-up, and disable output.

  2. Configure PINMUX: Assign the pins to the GPIO function.

driver_gpio_init initializes the GPIO peripheral with the following process:

  1. Enable the RCC clock.

  2. Configure the GPIO mode to output mode.

  3. Disable GPIO interrupts.

void driver_gpio_init(void)
{
    /* Initialize GPIO peripheral */
    RCC_PeriphClockCmd(APBPeriph_GPIO, APBPeriph_GPIO_CLOCK, ENABLE);

    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin    = GPIO_PIN_OUTPUT;
    GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_ITCmd  = DISABLE;
    GPIO_Init(&GPIO_InitStruct);
}

Functional Implementation

In the main function, after initialization, execute a while loop. Within the loop body, execute the GPIO_WriteBit() function to control the GPIO output high and low levels.

int main(void)
{
    extern uint32_t random_seed_value;
    srand(random_seed_value);
    gpio_demo();

    while (1)
    {
        /* Light up LED0 */
        GPIO_WriteBit(GPIO_PIN_OUTPUT, (BitAction)(1));
        for (uint32_t i = 0; i < 1000000; i++);
        /* Lights out LED0 */
        GPIO_WriteBit(GPIO_PIN_OUTPUT, (BitAction)(0));
        for (uint32_t i = 0; i < 1000000; i++);
    }
}