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.

RTOS/TM4C1292NCPDT: How to write critical section code to prevent the task preemption

Part Number: TM4C1292NCPDT

Tool/software: TI-RTOS

Hi,

I am using tirtos_tivac_2_16_01_14 and TivaWare_C_Series-2.1.4.178 for project. I am using one of the flash segment for storing non-volatile data for my application and this data will be written from one of the task in my application. There are other higher priority tasks and ISRs running in my application.

i want to prevent the below block of code should not be preempted by any other task or ISR before completing it.

void flashWrite(BOOT_STRUCT *stFlashData)
{
    int ret;

    ret = FlashErase((uint32_t ) FLASH_COMMON_MEMORY_ADDRESS);
    if(ret != NULL)
    {
        System_printf("FLash memory is not erased successfully\n");
    }

    ret = FlashProgram((uint32_t *)stFlashData,
            FLASH_COMMON_MEMORY_ADDRESS, 2048);
    if(ret != NULL)
    {
        System_printf("FLash memory is not programmed successfully\n");
    }

    System_flush();
}

anybody have suggestion, how to do it?

some time, after flash erase is completed, the task is preempted by other task/ISR before completing flash program and these flash data is needed for other tasks to run.

Regards

Bala

  • Hi Bala,

    What other threads are going to access that non-volatile memory?
    1. No one: then you don't need any protection
    2. Other tasks: I'd use a GateMutex or GateMutexPri as the blocking mechanism. So do a GateMutex_enter before the FlashErase above and GateMutex_leave after the programming has occurred. Then use the same enter/leave in the other task's code that accesses that memory.
    3. Swi and/ Hwi: While you could disable interrupts, that could cause too larger of a interrupt latency. I'd look at changing the design to limit access of the memory by the Hwi/Swi via a global variable or a Semaphore and have the Hwi/Swi use a timeout of zero and act accordingly if the semaphore was not obtained.

    Todd
  • Hi Todd,

    No other task accessing this flash memory region and even this piece of code. i thought, there may task preemption between flash erase and flash program. so i thought of bringing this section to critical region so that other task will not preempt.

    as per your suggestion i have used GateMutex feature to do that.

    The problem is flash erase always hang at below code at flah.c line no 152 from tivaware driver library after few back to back flash write.

        //
        // Wait until the block has been erased.
        //
        while(HWREG(FLASH_FMC) & FLASH_FMC_ERASE)
        {
        }
    

    This is the routine called from my task context based on external event.

    void flashWrite(BOOT_STRUCT *stFlashData)
    {
        int ret;
    
        /* Disable hardware interrupt */
        ret = FlashErase((uint32_t ) FLASH_COMMON_MEMORY_ADDRESS);
        if(ret != NULL)
        {
            System_printf("FLash memory is not erased successfully\n");
        }
    
        ret = FlashProgram((uint32_t *)stFlashData,
                FLASH_COMMON_MEMORY_ADDRESS, 2048);
        if(ret != NULL)
        {
            System_printf("FLash memory is not programmed successfully\n");
        }
    }

    i have gone through this forum link. 

    https://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/671683?TM4C1294NCPDT-TM4C1294NCPDT-flash-write-issue

    in that there was suggestion that adding some NOP instruction its working correctly.

    in similar way i added Task sleep about 10 msec(didn't checked by reducing the sleep time further) between flash access, its working fine. we want to know the exact root cause of this issue.

    Regards

    Bala

  • Hi Bala,

    Does the problem happen when you add the GateMutex or all the time? Obviously, you cannot act on a flash sector if there is executing code in it. I think the flash sectors for TM4C are pretty large if I remember correctly.

    Or even better, can you attach a small CCS project that has the problem.

    Todd

  • Hi Todd,

    ToddMullanix said:
    Does the problem happen when you add the GateMutex or all the time?

    No. The problem is earlier, we thought this flash erase/write may be preempted by other task. that is the reason we asked the code should be executed in critical section. After adding GateMutex does not solve the issue
    ToddMullanix said:
    Obviously, you cannot act on a flash sector if there is executing code in it.
    . Currently the code is programmed into the flash and its executing from the flash only and not from the RAM. Do you meant this should not be?

    ToddMullanix said:
    I think the flash sectors for TM4C are pretty large if I remember correctly.

    Yes. its 1MB.

    ToddMullanix said:
    even better, can you attach a small CCS project that has the problem

    Let me check whether i can recreate it in Evaluvauation board with minimal CCS project.

    Regards

    Bala

  • Hi Todd,

    ToddMullanix said:
    you cannot act on a flash sector if there is executing code in it.

    The execution code is available in different flash sectors.

    Bala

  • Hi Bala,

    Sorry, this fell through the cracks on our side. Is this still an issue.

    Todd

    [9/24 Update: Marking this as TI Thinks Resolved due to no activity from original poster.]

  • Hi Todd,

    This is not yet solved. we just added delay and its working... but we are not able to find the root cause yet... working with local FAE from TI.

    Regards

    Bala