An example to configure a peripheral timer in an FreeRTOS project

cancel
Showing results for 
Search instead for 
Did you mean: 

An example to configure a peripheral timer in an FreeRTOS project

An example to configure a peripheral timer in an FreeRTOS project

This article describes how to configure a peripheral timer in a FreeRTOS project with MCUXpresso config tools.

Software and Tools

In this article, I’m using the following:

MCUXpresso IDE 10.2.1   www.nxp.com/mcuxpresso

MCUXpresso SDK 2.4.1 for  Frdm-k66f  board.  With Amazon FreeRTOS v10 .

You can get it from https://mcuxpresso.nxp.com

FRDM-K66 board  www.nxp.com/frdm-k66f

First make sure you have a working FreeRTOS project. Please refer to below link for creating a new FreeRTOS project. 

https://community.nxp.com/docs/DOC-341972

To open the tool, simply right click on the project in Project Explorer and select the appropriate Open command:

pastedImage_2.png

With the peripheral Tool, we can use it to configure initialization of selected peripherals and generate code for them. we are picking the FTM0 timer ,of course we could use any other timer too.

pastedImage_3.png

pastedImage_4.png

Click OK, then configure the timer to 10KHz.

pastedImage_5.png

We will use the timer interrupt to increase a counter, so don’t forget to turn on the interrupt

pastedImage_6.png

To update the generated code in the related tool chain project, click the Update Project button.

pastedImage_7.png

pastedImage_9.png

Click OK, new code will be generated. Please check peripheral.c & peripheral.h

pastedImage_10.png

Timer ISR

Next we add the timer interrupt code to the application.

#include "fsl_ftm.h"

 

/*******************************************************************************

 * Variables

 ******************************************************************************/

volatile bool ftmIsrFlag = false;

volatile uint32_t milisecondCounts = 0U;

 

 

 

void FTM_1_IRQHANDLER(void)

{

    /* Clear interrupt flag.*/

    FTM_ClearStatusFlags(FTM0, kFTM_TimeOverflowFlag);

    ftmIsrFlag = true;

}

Adding Timer Drivers

The project won’t compile yet, because the necessary drivers are not part of the project yet. To add them, use the ‘Manage SDK components’ button:

pastedImage_11.png

Then check the ftm driver and press OK and get the extra driver sources added to the project.

pastedImage_12.png

pastedImage_13.png

We can see that fsl_ftm.c & fsl_ftm.h added into this project.

pastedImage_14.png

Adding timer to FreeRTOS application:

In this task, the FTM interrupt will trigger a message printed to the terminal

/*!

 * @brief Task responsible for printing message with FTM0.

 */

static void my_task(void *pvParameters)

{

 

    uint32_t cnt;

    uint32_t loop = 2U;

    uint32_t secondLoop = 1000U;

    const char *signals = "-|";

 

    cnt = 0;

 

    while (true)

    {

        if (ftmIsrFlag)

        {

            milisecondCounts++;

            ftmIsrFlag = false;

            if (milisecondCounts >= secondLoop)

            {

                PRINTF("%c", signals[cnt & 1]);

                cnt++;

                if (cnt >= loop)

                {

                    cnt = 0;

                }

                milisecondCounts = 0U;

            }

        }

        __WFI();

    }

}

Run the demo

console output:

pastedImage_1.png

For information about how to use MCUXpresso pins tool in a FreeRTOS project, please see the following document.

https://community.nxp.com/docs/DOC-341987

For information about how to create an FreeRTOS project with MCUXpresso IDE, please see the following document

https://community.nxp.com/docs/DOC-341972 

Comments

Hi Daniel,  when I click the 'open peripheral' , I can't see the peripherals view, as below, why?     

1.png

Thanks

Hi David:

Maybe you closed the peripherals view, please open it as below.

pastedImage_1.png

click other...

input peripherals, then click open, you will see the peripherals view.

pastedImage_2.png

Regards

Daniel

Version history
Revision #:
1 of 1
Last update:
‎11-06-2018 10:42 PM
Updated by: