This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123BH6PGE: Using Port B as GPIO

Part Number: TM4C123BH6PGE
Other Parts Discussed in Thread: EK-TM4C123GXL

Good afternoon,

I am trying to set up my TM4C micro to use port B (pins 0 - 2) as a digital input. The datasheet says Pin 2 is special which may or may not have anything to do with this error.

It seems no matter what I try, the next instruction after any access to a port B register causes a hard fault.

Following the indicated directions in the manual, I've tried:

- Configuring the SYSCTRL_RCGCGPIO register to include port B

- Configuring port B as input by writing 0 to the DIR register

- Disabling PORTB_LOCK by writing 0x4C4F434B

- Writing to the commit register (seems necessary but I do it anyways)

- Setting AFSEL to 0

- Configuring as I/O by writing appropriate bit to the DEN register.

In just about every order and combination that is possible. Still it seems to always throw a hard fault after anything to do with PORT B is executed. I can set the SYSCTRL_RCGCGPIO register and watch the registers fine, but any instruction that has to do with PORT B seems to cause a Hard Fault. I am not sure if there is some way to get more information about what is actually causing it.

Here is a code snippet:

#define PORT_A          (1U << 0)
#define PORT_B          (1U << 1)
#define PORT_C          (1U << 2)
#define PORT_D          (1U << 3)
#define PORT_E          (1U << 4)
#define PORT_F          (1U << 5)
#define PORT_G          (1U << 6)
#define PORT_H          (1U << 7)
#define PORT_J          (1U << 8)
#define PORT_K          (1U << 9)
#define PORT_L          (1U << 10)
#define PORT_M          (1U << 11)
#define PORT_N          (1U << 12)
#define PORT_P          (1U << 13)

#define PIN_0           (1U << 0)
#define PIN_1           (1U << 1)
#define PIN_2           (1U << 2)
#define PIN_3           (1U << 3)
#define PIN_4           (1U << 4)
#define PIN_5           (1U << 5)
#define PIN_6           (1U << 6)
#define PIN_7           (1U << 7)
#define PIN_ALL         0xFF  

...

  SYSCTL_RCGCGPIO_R   |= (PORT_A|PORT_B|PORT_D|PORT_G|PORT_H|PORT_J|PORT_L|PORT_M|PORT_N);
  SYSCTL_GPIOHBCTL_R  |= (PORT_J|PORT_L|PORT_M|PORT_N);
 
 
/*PORT A initializations ... */

 
  //PORT B         
  GPIO_PORTB_LOCK_R = 0x4C4F434B;
  GPIO_PORTB_CR_R = 0x07; //will always hard fault here, no matter what order these are in
  GPIO_PORTB_AFSEL_R = 0x0;  
  GPIO_PORTB_DEN_R  = (PIN_0|PIN_1|PIN_2);
  GPIO_PORTB_DIR_R  = 0x0;
  GPIO_PORTB_PCTL_R = 0x0;

Any assistance is appreciated.

  • Hi,

      Do you have any hard reason to not use TivaWare SDK? You are coding in DRM style rather than using the provided APIs. If you refer to #4 in the FAQ https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/695568/faq-faqs-for-tm4c-arm-cortex-m4f-microcontrollers, we actually don't support DRM style of coding. 

    Here is a simple piece of program that toggle PB(2:0). As stated by the FAQ, if you must use DRM for whatever reason, please refer to the source code for the API that carries out the DRM programming. 

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "driverlib/debug.h"
    #include "driverlib/gpio.h"
    #include "driverlib/sysctl.h"
    
    //*****************************************************************************
    //
    //! \addtogroup example_list
    //! <h1>Blinky (blinky)</h1>
    //!
    //! A very simple example that blinks the on-board LED using direct register
    //! access.
    //
    //*****************************************************************************
    
    //*****************************************************************************
    //
    // The error routine that is called if the driver library encounters an error.
    //
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
        while(1);
    }
    #endif
    
    //*****************************************************************************
    //
    // Blink the on-board LED.
    //
    //*****************************************************************************
    int
    main(void)
    {
        volatile uint32_t ui32Loop;
    
        //
        // Enable the GPIO port that is used for the on-board LED.
        //
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    
        //
        // Check if the peripheral access is enabled.
        //
        while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB))
        {
        }
    
        //
        // Enable the GPIO pin PB[2:0].  Set the direction as output, and
        // enable the GPIO pin for digital function.
        //
        GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0);
    
        //
        // Loop forever.
        //
        while(1)
        {
            //
            // Set the pins high.
            //
            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0, GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0);
    
            //
            // Delay for a bit.
            //
            for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
            {
            }
    
            //
            // Set the pins low.
            //
            GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0, 0x0);
    
            //
            // Delay for a bit.
            //
            for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++)
            {
            }
        }
    }
    

    This below post shows how to unlock some TM4C123 special pins but you don't need that for PB. 

    https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1020814/faq-how-to-get-locked-gpio-pins-on-tm4c123-devices-to-work

  • Thanks for the reply Charles,

    DRM = Direct Register Manipulation is that right?

    I will try the SDK and see if that works.

    Cheers

  • Hi,

      Yes, DRM stands for Direct Register Manipulation. The TivaWare SDK provides all of the components necessary to develop applications for the TM4C MCU. The ease of use of the TivaWare SDK allows developers to quickly start software development. The APIs abstract users from needing to manipulate the hardware registers. The SDK is a proven tool which has been tested and maintained. There are examples that you can find in the SDK. Please go to the below directories for many of the ready to use examples. Once you get the hang of it, just modify and adapt to your application requirements. 

    C:\ti\TivaWare_C_Series-2.2.0.295\examples\boards\ek-tm4c123gxl

    C:\ti\TivaWare_C_Series-2.2.0.295\examples\peripherals