AnsweredAssumed Answered

Hardfault when clicking restart during debugging using mcuxpresso (LPC4357 + FreeRTOS + interrupt)

Question asked by uaz on Dec 6, 2018
Latest reply on Dec 9, 2018 by xiangjun.rong

Hi,

I wonder if anyone else get the same problem as me. When running in debug, I am not able to restart the program because clicking restart will jump to hardfault (INVPC). I need to stop and restart debugging in order to restart my program and this consumes a lot of time.

 

I found out that this happens when I use an interrupt. When I disable the interrupt, I can restart the program during debugging as many time as I wants. This is my example code (I added a timer interrupt to Freertos blinky example):

 

#include "board.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

/*****************************************************************************
* Private types/enumerations/variables
****************************************************************************/

#define TICKRATE_HZ 1
/*****************************************************************************
* Public types/enumerations/variables
****************************************************************************/


/*****************************************************************************
* Private functions
****************************************************************************/


/* Sets up system hardware */
static void prvSetupHardware(void)
{
     SystemCoreClockUpdate();
     Board_Init();

     /* Initial LED0 state is off */
     Board_LED_Set(0, false);
}

/* LED1 toggle thread */
static void vLEDTask1(void *pvParameters) {
     bool LedState = false;

     while (1) {
          Board_LED_Set(0, LedState);
          LedState = (bool) !LedState;

          /* About a 3Hz on/off toggle rate */
          vTaskDelay(configTICK_RATE_HZ / 6);
     }
}

/* LED2 toggle thread */
static void vLEDTask2(void *pvParameters) {
     bool LedState = false;

     while (1) {
          Board_LED_Set(1, LedState);
          LedState = (bool) !LedState;

          /* About a 7Hz on/off toggle rate */
          vTaskDelay(configTICK_RATE_HZ / 14);
     }
}

/* UART (or output) thread */
static void vUARTTask(void *pvParameters) {
     int tickCnt = 0;

     while (1) {
          DEBUGOUT("Tick: %d \r\n", tickCnt);
          tickCnt++;

          /* About a 1s delay here */
          vTaskDelay(configTICK_RATE_HZ);
     }
}

/*****************************************************************************
* Public functions
****************************************************************************/


/**
* @brief     main routine for FreeRTOS blinky example
* @return     Nothing, function should not exit
*/

int main(void)
{
     uint32_t timerFreq;
     prvSetupHardware();

     /* Enable timer 1 clock and reset it */
     Chip_TIMER_Init(LPC_TIMER1);
     Chip_RGU_TriggerReset(RGU_TIMER1_RST);
     while (Chip_RGU_InReset(RGU_TIMER1_RST)) {}

     /* Get timer 1 peripheral clock rate */
     timerFreq = Chip_Clock_GetRate(CLK_MX_TIMER1);

     /* Timer setup for match and interrupt at TICKRATE_HZ */
     Chip_TIMER_Reset(LPC_TIMER1);
     Chip_TIMER_MatchEnableInt(LPC_TIMER1, 1);
     Chip_TIMER_SetMatch(LPC_TIMER1, 1, (timerFreq / TICKRATE_HZ));
     Chip_TIMER_ResetOnMatchEnable(LPC_TIMER1, 1);
     Chip_TIMER_Enable(LPC_TIMER1);

     /* Enable timer interrupt */
     NVIC_EnableIRQ(TIMER1_IRQn);
     NVIC_ClearPendingIRQ(TIMER1_IRQn);

     /* LED1 toggle thread */
     xTaskCreate(vLEDTask1, "vTaskLed1", configMINIMAL_STACK_SIZE,
               NULL, (tskIDLE_PRIORITY + 1UL), (TaskHandle_t *) NULL);

     /* LED2 toggle thread */
     xTaskCreate(vLEDTask2, "vTaskLed2", configMINIMAL_STACK_SIZE,
               NULL, (tskIDLE_PRIORITY + 1UL), (TaskHandle_t *) NULL);

     /* UART output thread, simply counts seconds */
     xTaskCreate(vUARTTask, "vTaskUart", configMINIMAL_STACK_SIZE,
               NULL, (tskIDLE_PRIORITY + 1UL), (TaskHandle_t *) NULL);

     /* Start the scheduler */
     vTaskStartScheduler();

     /* Should never arrive here */
     return 1;
}

void TIMER1_IRQHandler(void)
{
     static BaseType_t xHigherPriorityTaskWoken;
     static int interruptCount = 0;

     if (Chip_TIMER_MatchPending(LPC_TIMER1, 1)) {
          Chip_TIMER_ClearMatch(LPC_TIMER1, 1);
          interruptCount++;
     }
}

I also found that the rtos will crash after running it for several hours.

 

Can someone enlighten how this hardfault occurs? It will not occur if the timer interrupt is not enabled.

Does anyone ever succeeded to have stable freertos build with nxp?

 

Thanks

UAZ

Outcomes