PIT Timer example

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PIT Timer example

7,040 Views
abdulhayee
Contributor II

Hi guys

I am using KE02Z64VQH2 ARM Processor with Kinetic Design Studio. I want to use PIT Timer to interrupt after every 1sec and toggle an LED, Kindly give me working example program for it.

Thanks

Labels (1)
0 Kudos
7 Replies

3,870 Views
abdulhayee
Contributor II

I tried to write the code for PIT Timer but it is not working.can anyone modify the code so that the program can be run

#include "MKE02Z2.h"

#define FALSE     0
#define TRUE      1

#define LED0_Init()                GPIOB->PDDR |= (1<<25)    /*! RED Light */
#define LED0_Toggle()           GPIOB->PTOR = (1<<25)


__STATIC_INLINE void PIT_Enable(void)
{
    PIT->MCR &= ~PIT_MCR_MDIS_MASK;
}

__STATIC_INLINE void PIT_ChannelEnableInt(uint8_t u8channel)
{
    PIT->CHANNEL[u8channel].TCTRL |= PIT_TCTRL_TIE_MASK;
}

__STATIC_INLINE void PIT_ChannelEnableChain(uint8_t u8channel)
{
    PIT->CHANNEL[u8channel].TCTRL |= PIT_TCTRL_CHN_MASK;
}

__STATIC_INLINE void PIT_ChannelEnable(uint8_t u8channel)
{
    PIT->CHANNEL[u8channel].TCTRL |= PIT_TCTRL_TEN_MASK;
}

enum
{
    PIT_CHANNEL0=0,    //    PIT Channel 0
    PIT_CHANNEL1     //    PIT Channel 1
};

typedef struct
{
    uint8_t bFreeze            :1;
    uint8_t bModuleDis        :1;
    uint8_t bReversed0        :1;
    uint8_t bReversed1        :5;
    uint8_t bTimerEn        :1;
    uint8_t bInterruptEn    :1;
    uint8_t bChainMode        :1;
    uint8_t bReversed2        :5;
    uint8_t bFlag            :1;
    uint8_t bReversed3        :7;
    uint32_t u32LoadValue ;
}PIT_ConfigType, *PIT_ConfigPtr;

#ifndef NULL
#define NULL  (void *) 0
#endif

typedef void (*PIT_CallbackType)(void);

PIT_CallbackType PIT_Callback[2] = {(PIT_CallbackType)NULL};

void PIT_SetLoadVal(uint8_t u8channel, uint32_t u32LoadValue)
{
    PIT->CHANNEL[u8channel].LDVAL = u32LoadValue;
}

void PIT_SetCallback(uint8_t u8channel_No, PIT_CallbackType pfnCallback)
{
    PIT_Callback[u8channel_No] = pfnCallback;
}


void PIT_Init(uint8_t u8channel_No, PIT_ConfigType *pConfig)
{
    SIM->SCGC |= SIM_SCGC_PIT_MASK;        // enable clock to PIT

    if(pConfig->bModuleDis == 0)    PIT_Enable();                    // enable PIT Module

    PIT_SetLoadVal(u8channel_No, pConfig->u32LoadValue);

    if(pConfig->bInterruptEn)
    {
        if(u8channel_No)    NVIC_EnableIRQ(PIT_CH1_IRQn);
        else                NVIC_EnableIRQ(PIT_CH0_IRQn);

        PIT_ChannelEnableInt(u8channel_No);
    }
    else    NVIC_DisableIRQ(PIT_CH0_IRQn);

    if(pConfig->bChainMode)    PIT_ChannelEnableChain(u8channel_No);
    if(pConfig->bTimerEn)    PIT_ChannelEnable(u8channel_No);
}


void PIT_Task(void);

int main(void)
{
    uint32_t u32LoadValue0, u32LoadValue1;
    PIT_ConfigType sPITConfig0, sPITConfig1;
    PIT_ConfigType *pPIT_Config1 = &sPITConfig1;
    PIT_ConfigType *pPIT_Config0 = &sPITConfig0;

    LED0_Init();

    u32LoadValue0 = 0xF423F;
    u32LoadValue1 = 0x10;

    /*Configure PIT channel 1 in Chain Mode, enable interrupt and Timer*/
    pPIT_Config1->u32LoadValue    = u32LoadValue1;
    pPIT_Config1->bFreeze        = FALSE;
    pPIT_Config1->bModuleDis    = FALSE;
    pPIT_Config1->bInterruptEn    = TRUE;
    pPIT_Config1->bChainMode    = FALSE;
    pPIT_Config1->bTimerEn        = TRUE;

    /*Configure PIT channel 0, only enable Timer*/
    pPIT_Config0->u32LoadValue      = u32LoadValue0;
    pPIT_Config0->bFreeze           = FALSE;
    pPIT_Config0->bModuleDis        = FALSE;    /*!< enable PIT module */
    pPIT_Config0->bInterruptEn      = FALSE;
    pPIT_Config0->bChainMode        = FALSE;
    pPIT_Config0->bTimerEn          = TRUE;

    PIT_Init(PIT_CHANNEL0, pPIT_Config0);
    PIT_Init(PIT_CHANNEL1, pPIT_Config1);

    PIT_SetCallback(PIT_CHANNEL1,PIT_Task);
    /* This for loop should be replaced. By default this loop allows a single stepping. */
    for (;;) {

    }
    /* Never leave main */
    return 0;
}

void PIT_Task(void)
{
    LED0_Toggle();          /*!< toggle LED1 */
}

0 Kudos

3,870 Views
mjbcswitzerland
Specialist V

Hi

This is how I do it at the application level:

    PIT_SETUP pit_setup;                                 // PIT interrupt configuration parameters
    _CONFIG_DRIVE_PORT_OUTPUT_VALUE(B, (KE_PORTH_BIT1), (KE_PORTH_BIT1), (PORT_DSE_LOW)); // configure output to be used
    pit_setup.int_type = PIT_INTERRUPT;
    pit_setup.mode = PIT_PERIODIC;
    pit_setup.int_handler = pit_irq;
    pit_setup.int_priority = PIT0_INTERRUPT_PRIORITY;
    pit_setup.count_delay = PIT_US_DELAY(500);           // 500us period
    pit_setup.ucPIT = 0;                                 // use PIT0
    fnConfigureInterrupt((void *)&pit_setup);            // configure PIT and starts its operation

and the interrupt call back is

static void pit_irq(void)
{
    _TOGGLE_PORT(B, KE_PORTH_BIT1);
}

The (simplified) low level code for the KE02 (channel 0) is

        POWER_UP_ATOMIC(6, PIT);                         // ensure the PIT module is powered up
        PIT0_MCR = 0;                                    // ensure the PIT module is clocked
        fnEnterInterrupt(irq_PIT0_ID, PIT_settings->int_priority, pit_irq); // ensure that the handler for this PIT is entered, prioritised and enabled
        PIT0_LDVAL = PIT_settings->count_delay;          // load interval value
        WRITE_ONE_TO_CLEAR(PIT0_TFLG, PIT_TFLG_TIF);     // clear possible pending interrupt
        PIT0_TCTRL = (PIT_TCTRL_TEN | PIT_TCTRL_TIE);    // start PIT with interrupt enabled

In the interrupt handler (if using a raw interrupt) the interrupt must also be cleared with
WRITE_ONE_TO_CLEAR(PIT0_TFLG, PIT_TFLG_TIF);            // clear pending interrupts

As long as your environment has enabled interrupts and can enter a handler this is all that is needed.

Regards

Mark

0 Kudos

3,870 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Abdul,

Please test the attached 'PIT interrupt toggle PTH1 KE02 KDS3.2 PE' example.

PIT interrupt toggle PTH1 KE02 KDS3.2.png

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

3,870 Views
abdulhayee
Contributor II

Thanks Robin for your help..as i am learning ARM processor and pointers and structure make me so much confuse. Can you give me an example without using PE (processor expert) tool? simply by accessing the registers directly.

0 Kudos

3,870 Views
mjbcswitzerland
Specialist V

Hi Abdul

Reference the uTasker Open Source project on GIT hub:

http://www.utasker.com/kinetis/FRDM-KE02Z40M.html
http://www.utasker.com/kinetis/FRDM-KE02Z.html

PIT document: http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF

Attached is the PIT driver that works on all Kinetis parts with PIT or LPIT as reference, including all KE parts too.
PIT emulation is included in the project for approx. real-time testing of all project operation in Visual Studio (including interrupts and PIT DMA/ADC triggering).

Regards

Mark

0 Kudos

3,870 Views
abdulhayee
Contributor II

Thanks Mark Butcher.As i am new to ARM processor and know about it a little bit. Can you tell me what is UTasker? is it a simulator or something else? from where it can be downloaded?

0 Kudos

3,870 Views
mjbcswitzerland
Specialist V

Abdul

The uTasker project is described at http://www.utasker.com/index.html

It is a complete project that essentially runs on (virtually) all Kinetis processors including turn-key solution for most functions required in projects that is also IDE independent and allows complete Kinetis/project simulation. Below I have copied the features (also found at the link).

You can get the free Open Source version from https://github.com/uTasker/uTasker-Kinetis

There are various videos to get starting - eg.

https://www.youtube.com/watch?v=K8ScSgpgQ6M&index=9&list=PLWKlVb_MqDQFZAulrUywU30v869JBYi9Q

and tutorials and documents at http://www.utasker.com/docs/documentation.html

Regards

Mark


Features - what the µTasker project has to offer

    Small footprint - The µTasker solution is especially suited to projects where an easy to use, slim-line operating system and TCP/IP and/or USB stack are of importance. This makes it ideal for single chip applications where the control of resources is critical but a high level of developer/user comfort can't be compromised.
    µTasker operating system - efficient and easy to use operating system to enable very modular and manageable software. Inter-task queues with flexible timer support. Supports distributed processing features over an integrated fast Ethernet network protocol.
    Including option to operate together with FreeRTOS for perfect combination of pre-emptive and co-operative characteristics**.
    µFileSystem and µParameterSystem - Highly efficient internal or SPI FLASH based file system support, optimised for typical embedded applications.
    µtFAT - FAT12/16/32 compatible file system with SD card interface support, optionally integrated into FTP and HTTP servers supporting LFN (long file names) and optional data caching. Operates in internal Flash, NAND Flash, SD cards, external SPI Flash and/or USB memory sticks*.
    Emulated FAT - Allows data saved in linear memory (internal flash, external SPI Flash, etc. to be viewed as if it were FAT formatted and also its data formatted for direct importing to data processing application (eg. *.csv)
    User Files support - Allows embedded files in code to be used or mixed with µFileSystem files, including utility for creating an embedded set of user files for simple upload to targets.
    Device drivers - UARTs (interrupt, DMA, RS485), I2C, CAN, character LCD, graphical LCD and TFT, Matrix keyboard, ADC, DAC, RTC, PWM, HW timers, port interrupts, etc.
    USB device/host*/OTG* support - CDC, RNDIS, Audio, MSD, HMI classes, user friendly configuration and interfacing, with buffered streaming mode as well as firmware upload capability.
    Special support - DMA library routines (Kinetis, M5223X - uMemcpy, uMemset), Low power task, Time management (RTC, time zones, daylight saving).
    TCP/IP stack - ARP, RARP, ICMP, IPv4/IPv6, UDP, DHCP, DNS, TFTP, TCP, dynamic HTTP, HTTP post, FTP server, FTP client, SNMP V1/V2c, multicast with IGMP V1/V2, SNTP, SMTP, POP3, TELNET, NetBIOS and VLAN plus various useful utilities for real-world Internet work. Multiple interface, network (dual-homed) and VLAN capable. RNDIS operation over USB and PPP/Slip over serial**. Integrated with the µTasker operating system for optimal performance.
    DSP - Popular Digital Signal Processing functions integrated.
    Cryptography - Popular Cryptographic functions integrated.
    Graphical LCD support - µGLCDLib and graphical LCD simulator. Non-blocking FTDI FT800 graphical controller support with integrated emulation**.
    Boot Loader - The µTasker project has integrated support for the "Bare-Minimum" boot loader which enables complete and secure software uploads via Ethernet/Internet (FTP and HTTP POST) as well as USB, and occupies less than 2k of FLASH space! Optionally it allows uploads to external SPI FLASH as well as encryption (less than 4k of Flash space).
    Serial Boot Loader - The µTasker project includes a stand-alone boot loader allowing deleting and loading new applications over a UART, from an SD-card or a memory stick*, as USB-MSD device, USB-HID or via web server.
    Unique µTasker simulator - allows the supported devices and peripherals to be simulated in "real-time" on the PC which can dramatically reduce development times compared to classical development/debug methods on the target.
    Ready-to-run with pre-configured projects - The µTasker project is delivered with setups for GCC make file building, IAR, Keil uVision, Rowley CrossWorks, CodeWarrior, Kinetis Design Studio (KDS), CooCox CoIDE, Atollic, Green Hills and MCUXpress. The µTasker project is a high quality framework designed for immediate used as base for professional work.

0 Kudos