NXP MCXA Microcontroller: How to use GPIO Output

Once your development environment is setup, you learned how to open, compile, flash and debug code. It time to start learn all the interfaces on the MCXA Microcontroller. First and the easiest interface is GPIO as an output. In this article we will learn how to write an application to blink and LED connected to a GPIO of the MCXA Microcontroller.

NXP MCXA Microcontroller: How to use GPIO Output 1

DEMO Example of GPIO Toggle LED on MCXA Microcontroller

You can import the official example given. Go to File>New>Import SDK Examples>Double clock on the FRDM development board and you will get a list of examples. You can choose driver examples>gpio_led_outout

NXP MCXA Microcontroller: How to use GPIO Output 2

This example shows how to toggle an LED(one of the RGB LEDs) on the FRDM Development board.

NXP MCXA Microcontroller: How to use GPIO Output 3
From FRDM Development Board Schematic

The code flow is that you need to initialize the system clock, which is mainly the MCU core clock, and then the Port clock, which will enable the GPIO Port and PIN port. Once you allow the clock, you need to configure the pins and then use a function to toggle the GPIO pin.

In the example, you can see that function BOARD_InitBootClocks() is called, which in turn calls function BOARD_BootClockFRO96M(), which basically sets the clock frequency for the core and other peripherals.

MCUXpressoIDE has an inbuilt Clock configurator where you can enable or disable various clocks. This basically allows you to control each clock source. After configuring, on the right handle side pane, you will be able to see the clock frequency set for each peripheral and whether it is active or inactive.

If you can, play around with it and see how it works and how it impacts the system or peripherals’ clock.

NXP MCXA Microcontroller: How to use GPIO Output 4

After the clock is configured, the initialization of IOs is required, which will enable their clocks and port settings.

For example, for our RED LED, which is connected to P3_12, we will need to enable the clock of GPIO3 and Port3 and release reset for both GPIO3 and Port3.

See the code below

    /* Write to GPIO3: Peripheral clock is enabled */
    CLOCK_EnableClock(kCLOCK_GateGPIO3);

    /* Write to PORT3: Peripheral clock is enabled */
    CLOCK_EnableClock(kCLOCK_GatePORT3);

    /* GPIO3 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kGPIO3_RST_SHIFT_RSTn);

    /* PORT3 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kPORT3_RST_SHIFT_RSTn);

After the clock is enabled and the reset is released, we need to configure for GPIO3 port and Port3

The port configuration will be the electrical specification for the selected Pin, and the GPIO configuration will be for the mode of the GPIO to be used. Here is how you do both configurations:

gpio_pin_config_t LED_RED_config = {
        .pinDirection = kGPIO_DigitalOutput,
        .outputLogic = 0U // default logic level of the GPIO
    };

    /* Initialize GPIO functionality on pin PIO3_12 (pin 38)  */
    GPIO_PinInit(BOARD_INITPINS_LED_RED_GPIO, BOARD_INITPINS_LED_RED_PIN, &LED_RED_config);
const port_pin_config_t LED_RED = {/* Internal pull-up/down resistor is disabled */
                                       kPORT_PullDisable,
                                       /* Low internal pull resistor value is selected. */
                                       kPORT_LowPullResistor,
                                       /* Fast slew rate is configured */
                                       kPORT_FastSlewRate,
                                       /* Passive input filter is disabled */
                                       kPORT_PassiveFilterDisable,
                                       /* Open drain output is disabled */
                                       kPORT_OpenDrainDisable,
                                       /* Low drive strength is configured */
                                       kPORT_LowDriveStrength,
                                       /* Normal drive strength is configured */
                                       kPORT_NormalDriveStrength,
                                       /* Pin is configured as P3_12 */
                                       kPORT_MuxAlt0,
                                       /* Digital input enabled */
                                       kPORT_InputBufferEnable,
                                       /* Digital input is not inverted */
                                       kPORT_InputNormal,
                                       /* Pin Control Register fields [15:0] are not locked */
                                       kPORT_UnlockRegister};
    /* PORT3_12 (pin 38) is configured as P3_12 */
    PORT_SetPinConfig(BOARD_INITPINS_LED_RED_PORT, BOARD_INITPINS_LED_RED_PIN, &LED_RED);

I also tried to explore the Pin configuration tool, but I wasn’t able to understand how it works. I could not make it work. I tried enabling/disabling GPIO, and I wasn’t able to see the code getting changes. Maybe I need to watch a video to gain some knowledge. I might be missing something.

Anyways, once the configuration is done, we can then toggle the function in the super loop while(1)

I suggest reading the fsl_gpio.h file to learn which HAL functions are available to configure and control GPIOs.

Toggling GPIO can be done using the GPIO_PortSet function. Alternatively, GPIO_PortSet and GPIO_PortClear functions can also be used to create GPIO toggle functionality.

The delay function gives a few milliseconds delay so that eyes can see the toggling of the GPIO/LED.

The Code will look like this:

void delay(void)
{
    volatile uint32_t i = 0;
    for (i = 0; i < 800000; ++i)
    {
        __asm("NOP"); /* delay */
    }
}

/*!
 * @brief Main function
 */
int main(void)
{

    BOARD_InitPins();
    BOARD_InitBootClocks();

    while (1)
    {
        delay();
        delay();
        GPIO_PortToggle(BOARD_LED_GPIO, 1u << BOARD_LED_GPIO_PIN);
	delay();
	delay();
        GPIO_PortToggle(BOARD_LED_GPIO, 1u << BOARD_LED_GPIO_PIN);

    }
}

What I have observed is that the examples provided are not very well written. For example, the GPIO settings are done in the BOARD_InitPins(), and then again in the main loop, these are configured. The same thing is true for the GPIO3 clock configuration.

I don’t think configuration needs to be done twice, as in the example. Please ignore that. To keep the code well organized, the best would be to keep all pin configurations in a function.

I have also observed that after code compilation is done, the IDE gets stuck somewhere and becomes responsive only a couple of seconds later. The same thing happens when you try to enter debug mode, etc. I’m not sure why, but I will figure it out. See the video below to know what I mean.

Toggle LED on MCXA Microcontroller

The output of the Toggle LED example is shown in the video below:

That’s all in this tutorial blog for MXA Microcontroller, I hope you got to learn something.

I will get back with a new blog on how to use GPIO as input for both the polling and interrupt methods on the MCXA microcontroller.


I am running an Embedded Design House, CAPUF Embedded Pvt. Ltd, located in Bangalore, India. At CAPUF, we help companies build embedded products with our hardware and firmware design services.

We also help in design optimizations for power consumption, cost, mass manufacturing, and performance.

Additionally, we develop PCB testing jigs and provide cloud-based monitoring solutions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.