TI ARM LaunchPad GPIO Unlock Tutorial

TI locks some GPIO pins because they’re required for programming interfaces like JTAG. SW2 on the LaunchPad is connected to PF0 (page 334), which is one of these locked up pins. We can’t use it as an input without unlocking it first. Here’s the code from my GPIO Input tutorial, but modified to use SW2. Let’s go over the changes from that tutorial’s code, then see the entire thing.

 

Screen_Shot_2015-05-25_at_12_59_07_PM

Include hw_gpio.h

#include "inc/hw_gpio.h"	// We need to include this. It's got the GPIO_LOCK_KEY macro, among others

 

We need to include this header to get access to the appropriate macros to unlock pins. This wasn’t needed in the GPIO input tutorial, but it makes to include it in every project that’s going to need GPIO.

 

Unlock the pin

HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;	// unlock the GPIOCR register for port F
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x01;		// Free up pin 0

 

GPIO_LOCK_KEY is equal to 0x4C4F434B – this value can be found on page 684 of the TM4C123GH6PM datasheet, as well as details about what’s going on. Essentially, writing this value to the GPIO Lock register will unlock the GPIO Commit register, where we specify which pins we need opened up for tinkering with. So in the second line we’re opening up pin 0 (they’re 1-indexed here, which is why we’re writing a 1). Also, the key value won’t necessarily be the same between microcontrollers. Check the datasheet to be sure.

 

Ready to go!

That’s all you need to do to unlock GPIOs. Here’s the entire example code. It’s exactly the same as the GPIO input tutorial, but includes the code to unlock SW2 that we just went over.

 

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_gpio.h"	// We need to include this. It's got the GPIO_LOCK_KEY macro, among others
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
int main(void)
{
  SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);	// set up the clock
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);	// enable port F

    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;	// unlock the GPIOCR register for port F
    HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x01;		// Free up pin 0

  GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);	// enable outputs on the launchpad LED pins
  GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0);	// make F4 an input

  GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_0,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);	// enable F0's pullup, the drive strength won't affect the input
  while(1)
  {
    uint32_t pinVal=0;	// variable to hold the pinRead
    pinVal= GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0);	// read F0

    if( (pinVal & GPIO_PIN_0)==0){	// AND to strip out anything but the value read from F0
      GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 2);	// turn on one LED
    }

    else{
      GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 4);	// turn on a different LED
    }

    //   SysCtlDelay(7000000); // uncomment me if you need debouncing

  }
}

 

Other considerations

In the example I was using to figure this out, you’ll notice that they set the AFSL register. Below are lines 146-148 of the gpio_jtag example, which will be installed in the Tivaware C series examples folder for the launchpad board. If you followed all steps of TI’s installation tutorials, this project should exist somewhere for you. Here was the path for me: C:TITivaWare_C_Series-1.1examplesboardsek-tm4c123gxlgpio_jtag.

 

HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x01;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) &= 0xfe;

 

This is the GPIO alternate function select register. In the JTAG example, they need to set this since they’re switching the pins back and forth between GPIO and JTAG. Not something we needed to do in this tutorial, but it’s good to be aware of. Actually, I set this in the otherwise identical code for this tutorial, and GPIO wouldn’t work. Go figure. More info on this register can be found on page 671 of the TM4C123GH6PM datasheet, or you can take a look at the gpio_jtag example.

 

There it is, a quick look on which registers to poke into when you need to unlock a GPIO using the Tivaware API. Thanks for reading!

2 thoughts on “TI ARM LaunchPad GPIO Unlock Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *