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.

Tiva TM4C123G strange behaviour

Hello Guys. 

Debugging this code I encountered strange behaviour

CODE:

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.c"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.c"
#include "driverlib/gpio.h"

#define LED_PERIPH SYSCTL_PERIPH_GPIOF
#define LED_BASE GPIO_PORTF_BASE
#define RED_LED GPIO_PIN_1

#define Button_PERIPH SYSCTL_PERIPH_GPIOF
#define ButtonBase GPIO_PORTF_BASE
#define Button GPIO_PIN_4
#define Button2 GPIO_PIN_0

int main(void)
{
 //Set the clock to 80Mhz
  SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
  HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
  HWREG(GPIO_PORTF_BASE+GPIO_O_CR) |= GPIO_PIN_0;

  SysCtlPeripheralEnable(LED_PERIPH);
  SysCtlDelay(9);

  GPIOPinTypeGPIOInput(ButtonBase, Button);
  GPIOPinTypeGPIOInput(ButtonBase, Button2);
  GPIOPadConfigSet(ButtonBase ,Button,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
  GPIOPadConfigSet(ButtonBase ,Button2,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); //When I comment out this line the LED blinks faster

  GPIOPinTypeGPIOOutput(LED_BASE, RED_LED);

  uint32_t value=0;
  uint8_t state=0;
  while(1){
    value= GPIOPinRead(ButtonBase,Button);
    if( (value & GPIO_PIN_4)==0)
      state^=RED_LED;

    GPIOPinWrite(LED_BASE,RED_LED, state);
    SysCtlDelay(7000000);
  }
}

When I comment out this line:

  GPIOPadConfigSet(ButtonBase ,Button2,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);

The LED is blinking faster then when the line is not commented.

Another think that bothers me - When I press the reset button of the Target Board the code crashes and never runs properly again. What might cause this? With the blinking led I don't have such a problem.

Thank you.

  • Hello Radoslav,

    I confirm the behavior with your code. For the second issue the problem is that the peripheral is being enabled after the register access

    HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE+GPIO_O_CR) |= GPIO_PIN_0;
    
    SysCtlPeripheralEnable(LED_PERIPH);
    SysCtlDelay(9);

    must be

    SysCtlPeripheralEnable(LED_PERIPH);
    SysCtlDelay(9);
    HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE+GPIO_O_CR) |= GPIO_PIN_0;
    

    Regards

    Amit

  • Yes it is. Amit when I see you I will get You a BEER :)

  • Hello Radoslav,

    I found the issue with the blink rate issue as well. The issue is that at 80MHz the prefetch buffer is used and the SysCtlDelay API call is in flash. By removing the line you are making the code move across the prefetch buffer causing the CPU to stall when the prefetch buffer needs to be refilled.

    By moving the code to 40MHz, the delay is gone and the LED blinks at a constant rate,

    Regards

    Amit