Maclain Lobdell

How To: use Kinetis SDK drivers within MQX apps - General overview

Discussion created by Maclain Lobdell Employee on Sep 4, 2014

Last week I made a post about using Kinetis SDK drivers with MQX RTOS.  That focused on interrupts.

How To: use Kinetis SDK drivers within MQX apps - handling interrupts

 

It references the NEW version of MQX RTOS for Kinetis SDK

Beta version of MQX RTOS for Kinetis SDK - Now Available

 

Here is a more general overview of how to use a Kinetis SDK driver in an MQX app.

 

Step i

When starting a new application, I just copy an example.  See folder, C:\Freescale\MQX_KSDK_1.0.0\rtos\mqx\mqx\examples, for native MQX RTOS examples. 

 

NOTE: The MQX RTOS examples in the folder C:\Freescale\MQX_KSDK_1.0.0\rtos\mqx\mqx\examples, DO NOT yet contain examples for using the Kinetis SDK peripheral drivers within MQX RTOS applications.  A few examples are planned in the next release.  The following steps should help you get started.


Step ii

Find a Kinetis SDK example for a driver in the folder, C:\Freescale\MQX_KSDK_1.0.0\platform\drivers , or reference the driver API from the documentation in the C:\Freescale\MQX_KSDK_1.0.0\doc folder. 

 

The examples there weren't written for MQX RTOS, but it is easy to convert them.

 

I have chosen the Low Power Timer (lptmr) driver, which has an example in the folder C:\Freescale\MQX_KSDK_1.0.0\platform\drivers\lptmr.

 

Step 1

Back in your MQX RTOS application, include the header file for the driver.

 

#include <stdio.h>

#include <mqx.h>

#include <bsp.h>

#include "fsl_lptmr_driver.h"

 

Step 2

 

Copy over definitions, global variables used by the driver

 

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

* Definitions

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

#define LPTMR_INSTANCE 0

 

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

* Global Variables

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

static lptmr_state_t gLPTMRState;

uint8_t gLPTMR_counter;

 

Step 3

 

Add prototype for interrupt callback function to be used by the driver.

 

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

* Prototypes

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

void lptmr_isr_callback(void);

void main_task(uint32_t);

 

Step 4

Add interrupt callback function.

 

/*

* @brief LPTMR interrupt callback

*/

void lptmr_isr_callback(void)

{

    gLPTMR_counter++;

    printf("%d ",gLPTMR_counter);

}

 

Step 5

Copy over the driver initialization into your MQX task function.  Remove unnecessary items shown below in RED.

 

Step 6

Install the driver's ISR to MQX RTOS.  This is an extra step required that is shown in BLUE below.  See the driver header file for the ISR name. 


For the Low Power Timer, the ISR function name (LPTMR_DRV_IRQHandler) can be found in fsl_lptmr_driver.h.  As noted in my last post, this ISR function, clears the interrrupt and then calls the callback function which was copied into the application in step 4. 

 

/*TASK*-----------------------------------------------------

*

* Task Name    : main_task

* Comments     :

*

*END*-----------------------------------------------------*/

void main_task

    (

        uint32_t initial_data

    )

{

        _int_install_unexpected_isr();


        gLPTMR_counter=0;


        lptmr_user_config_t lptmrUserConfig =

        {

            .timerMode = kLptmrTimerModeTimeCounter, /* Use LPTMR in Time Counter mode*/

            .freeRunningEnable = false, /*When hit compare value, set counter back to zero */

            .prescalerEnable = false, /* bypass prescaler */

            .prescalerClockSource = kLptmrPrescalerClockSourceLpo, /* use 1kHz Low Power Clock */

            .isInterruptEnabled = true

        };

 

/* NOTE: The section highlighted in RED here is Not Required for MQX RTOS for Kinetis SDK v1.0 BETA.  This is already done in MQX initialization. */


#if 0 

        /* Initialize standard SDK demo application pins */

        hardware_init();


        /* Configure the UART TX/RX pins */

        configure_uart_pins(BOARD_DEBUG_UART_INSTANCE);

 

        /* Call this function to initialize the console UART.  This function

           enables the use of STDIO functions (printf, scanf, etc.) */

        dbg_uart_init();

#endif

 

        printf("Low Power Timer Example\n\r");

 

        /* Initialize LPTMR */

        LPTMR_DRV_Init(LPTMR_INSTANCE,&lptmrUserConfig,&gLPTMRState);

 

        /* Set the timer period for 1 second */

        LPTMR_DRV_SetTimerPeriodUs(LPTMR_INSTANCE,1000000);

 

        /* Specify the callback function when a LPTMR interrupt occurs */

        LPTMR_DRV_InstallCallback(LPTMR_INSTANCE,lptmr_isr_callback);

 

/* NOTE: The section highlighted in BLUE here is required for MQX RTOS for Kinetis SDK v1.0 BETA because it handles interrupts differently. */


        /* Install interrupt to MQX RTOS for Kinetis SDK v1.0 BETA */

        _int_install_isr(LPTimer_IRQn, LPTMR_DRV_IRQHandler,  0);

 

        /* Start counting */

        LPTMR_DRV_Start(LPTMR_INSTANCE);

 

        printf("Started LPTMR\n\r");

 

        /* Wait for LPTMR interrupt once every second */

        while(1)

        {}

}

 

Step 7

That's it.  You are done.  Build, download, and execute to see your example running.

 

Best of luck.  Please post comments if you find any error or have additional suggestions.

 

Thanks,

Mac L

Outcomes