vTaskNotifyGiveIndexedFromISR brakes execuion

Hi everyone!
I am implementing power supply control functionality. I am utilizing adc measurements with dma, triggered by a timer at fixed frequency (2k5 Hz). I wan’t to execute the control loop after every measurement by using vTaskNotifyGiveIndexedFromISR. However using it in the ISR causes hardfault but so hard, there haven’t been any SEGGER messages received from anywhere within the system.

void ADC_DMA_ISR(void) {
    BaseType_t yield = pdFALSE;
    dma_clear_interrupt_flags(ADC_DMA, ADC_DMA_CHANNEL, DMA_TCIF);
    vTaskNotifyGiveIndexedFromISR(task_handle, 0, &yield);
    portYIELD_FROM_ISR(yield);
}
SemaphoreHandle_t mutex;
void task(void* pvParameters) {
    strat_adc();
    mutex = xSemaphoreCreateMutex();
    while (1) {
        notification |= ulTaskNotifyTakeIndexed(0, pdTRUE, portMAX_DELAY);
        // do the stuff
    }
}
Prioirties:
task:    4 // set as the highest priority task in the system
dam isr: 1

I feel I’m missing something fundamental. What could cause this issue?

Did you verify that the interrupt prio is set correctly ? See e.g. FreeRTOS - The Free RTOS configuration constants and configuration options - FREE Open Source RTOS for small real time embedded systems and/or the forum for further details.

Thanks, I found a big mistake. My config looks like this:

#define configMAX_PRIORITIES                (5)

#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY         15
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY    5

#define configKERNEL_INTERRUPT_PRIORITY                 (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))

#define configMAX_SYSCALL_INTERRUPT_PRIORITY            (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))

I changed the irq priority to 5, where I am sending the notification to the task:

Prioirties:
task:    4 // set as the highest priority task in the system
dam isr: 5

However the issue is persisting.

Experience 1:

void ADC_DMA_ISR(void) {
    BaseType_t yield = pdFALSE;
    dma_clear_interrupt_flags(ADC_DMA, ADC_DMA_CHANNEL, DMA_TCIF);
    // vTaskNotifyGiveIndexedFromISR(task_handle, 0, &yield);
    // portYIELD_FROM_ISR(yield);
}
SemaphoreHandle_t mutex;
void task(void* pvParameters) {
    strat_adc();
    mutex = xSemaphoreCreateMutex();
    while (1) {
        notification |= ulTaskNotifyTakeIndexed(0, pdTRUE, portMAX_DELAY);
        // do the stuff
    }
}

Result: No RTT logs received even from before the creation of the task.

Experience 2:

void ADC_DMA_ISR(void) {
    BaseType_t yield = pdFALSE;
    dma_clear_interrupt_flags(ADC_DMA, ADC_DMA_CHANNEL, DMA_TCIF);
    vTaskNotifyGiveIndexedFromISR(task_handle, 0, &yield);
    portYIELD_FROM_ISR(yield);
}
SemaphoreHandle_t mutex;
void task(void* pvParameters) {
    strat_adc();
    mutex = xSemaphoreCreateMutex();
    while (1) {
        // notification |= ulTaskNotifyTakeIndexed(0, pdTRUE, portMAX_DELAY);
        // do the stuff
        task_delay(x);
    }
}

Result: In this case RTT logs are received, and I see hardfault in the logs, when hitting the ADC_DMA_ISR

@cseha-mark Would it be possible to share which version of FreeRTOS is used an the hardware details?

In 10.6.0 version of FreeRTOS-Kernel FreeRTOS-Kernel/History.txt at main · FreeRTOS/FreeRTOS-Kernel · GitHub, configKERNEL_INTERRUPT_PRIORITY is deprecated for Armv7-M based targets and the kernel interrupts run at lowest priority FreeRTOS-Kernel/portable/GCC/ARM_CM4F/port.c at main · FreeRTOS/FreeRTOS-Kernel · GitHub.

If an older version of the kernel is used, setting configKERNEL_INTERRUPT_PRIORITY to (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS)) with configLIBRARY_LOWEST_INTERRUPT_PRIORITY set to 15 would only work if the number of implemented priority bits is 4. Can you please check this? As a quick check, if you set configKERNEL_INTERRUPT_PRIORITY to 255, does it work?

Have you defined configASSERT in your FreeRTOSConfig.h?

@aggarg Yes, it is defined

@urutva

  • FreeRTOS Kernel V10.5.1
  • STM32G474
    Implemented priority bits is 4. Setting configKERNEL_INTERRUPT_PRIORITY to 255 did not work.

Can you share your complete FreeRTOSConfig.h?

@aggarg

/*
 * FreeRTOS V202212.01
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

/*-----------------------------------------------------------
 * Application specific definitions.
 *
 * These definitions should be adjusted for your particular hardware and
 * application requirements.
 *
 * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
 * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
 *
 * See http://www.freertos.org/a00110.html
 *----------------------------------------------------------*/

/* Ensure stdint is only used by the compiler, and not the assembler. */
#ifdef __ICCARM__
#include <stdint.h>
extern uint32_t SystemCoreClock;
#endif

// clang-format off

#define configUSE_PREEMPTION                1
#define configUSE_IDLE_HOOK                 1
#define configUSE_TICK_HOOK                 1
#define configCPU_CLOCK_HZ                  (170000000)
#define configTICK_RATE_HZ                  ((TickType_t)1000)
#define configMAX_PRIORITIES                (5)
#define configMINIMAL_STACK_SIZE            ((unsigned short)128)   // for idle task
#define configTOTAL_HEAP_SIZE               ((size_t)(32 * 1024))   // the big one!
#define configMAX_TASK_NAME_LEN             (32)
#define configUSE_TRACE_FACILITY            0
#define configUSE_16_BIT_TICKS              0
#define configIDLE_SHOULD_YIELD             1
#define configUSE_MUTEXES                   1
#define configQUEUE_REGISTRY_SIZE           8
#define configCHECK_FOR_STACK_OVERFLOW      2
#define configUSE_RECURSIVE_MUTEXES         0
#define configUSE_MALLOC_FAILED_HOOK        1
#define configUSE_APPLICATION_TASK_TAG      0
#define configUSE_COUNTING_SEMAPHORES       0
#define configGENERATE_RUN_TIME_STATS       0
#define configSUPPORT_STATIC_ALLOCATION     1
#define configSUPPORT_DYNAMIC_ALLOCATION    1
/* Required to achieve a proper sleep state. */
#define configUSE_TICKLESS_IDLE             1

/* Hook our sleep processing in sleep.c */
#define configSLEEP_ROUTINE                 freertos_sleep

/* Software timer definitions. */
#define configUSE_TIMERS                    1
#define configTIMER_TASK_PRIORITY           (4)
#define configTIMER_QUEUE_LENGTH            10
#define configTIMER_TASK_STACK_DEPTH        (configMINIMAL_STACK_SIZE * 2)

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet            1
#define INCLUDE_uxTaskPriorityGet           1
#define INCLUDE_vTaskDelete                 1
#define INCLUDE_vTaskCleanUpResources       1
#define INCLUDE_vTaskSuspend                1
#define INCLUDE_vTaskDelayUntil             1
#define INCLUDE_vTaskDelay                  1
#define INCLUDE_xTaskGetSchedulerState      1

/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 4 /* 15 priority levels */
#endif

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY         15

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY    5

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY                 (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))

/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY            (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))

/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#include "main.h"
#define configASSERT(x)         if( ( x ) == 0 ) { freertos_assert_fail_handler(); }

/* Map the FreeRTOS port interrupt handlers to their libopencm3 names. */
#define vPortSVCHandler         sv_call_handler
#define xPortPendSVHandler      pend_sv_handler
#define xPortSysTickHandler     sys_tick_handler

// clang-format on

#endif /* FREERTOS_CONFIG_H */

@cseha-mark Just for the sake of completeness:

  • Can you share your definition of freertos_assert_fail_handler()?
  • Can you also share your definitions of vApplicationMallocFailedHook() and vApplicationStackOverflowHook()?
  • Can you tell us which version of the kernel you are running?

@jefftenney vApplicationMallocFailedHook() and vApplicationStackOverflowHook() are not defined.

void freertos_assert_fail_handler(void) {
    SEGGER_RTT_WriteString(0, "fatal error\n");
    // If FreeRTOS was running, disable interrupts
    if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
        taskDISABLE_INTERRUPTS();
    }

    // Red LED on
    red_was_on = 1;
   led_red_on();

    // If debugging, trigger a breakpoint
    // If you are debugging a hardfault and only have the address, use e.g.
    //      info line *0x080045D6
    // in the gdb console to find the source line that triggered the fault.
    if (debugger_is_attached()) {
        __asm("bkpt 1");
    }

    // Reset
    micro_reset();
}

Since you have both configUSE_MALLOC_FAILED_HOOK and configCHECK_FOR_STACK_OVERFLOW enabled in FreeRTOSConfig.h, then both vApplicationMallocFailedHook() and vApplicationStackOverflowHook() must exist in your codebase somewhere or else you’d be getting a build error from the linker.

Temporarily, for diagnostic purposes, can you use these:

#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }

static volatile uint8_t mallocFailedMarker = pdFALSE;
void vApplicationMallocFailedHook( void )
{
   configASSERT(mallocFailedMarker);
}

static volatile uint8_t stackOverflowDetectedMarker = pdFALSE;
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
   ( void ) pcTaskName;
   ( void ) pxTask;

   configASSERT(stackOverflowDetectedMarker);
}

And then re-test.

In addition to what @jefftenney mentioned, can you also share your interrupt able and startup code. What I want to make sure is that sv_call_handler and pend_sv_handler are directly installed as handlers.

We had progress with this issue and now i can block the task on the notification and execute upon the measurements.
Solution:

  1. We had a race condition in the ISR, it got executed before the task was created, now I am checking if the handle is NULL.
  2. notification |= ulTaskNotifyTakeIndexed(0, pdTRUE, portMAX_DELAY); started working when we set configUSE_TICKLESS_IDLE to 0.

Why configUSE_TICKLESS_IDLE makes notification not working?

Was this not caught by configASSERT?

Can you tell more about what you mean by not working? Does the ISR not fire when you use tickless IDLE?