ISSDK  1.8
IoT Sensing Software Development Kit
driver_pit.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /*! \file driver_pit.c
10  \brief Provides a simple abstraction for a periodic interval timer.
11 
12  Bare metal implementations of the sensor fusion library require at least
13  one periodic interrupt for use as a timebase for sensor fusion functions.
14  The Periodic Interval Timer (PIT) is one such module that is commonly
15  found on NXP Kinetis MCUs. The PIT functions are only referenced at the
16  main() level. There is no interaction within the fusion routines themselves.
17 */
18 #include "issdk_hal.h"
19 #include "board.h"
20 #include "fsl_pit.h"
21 #include "pin_mux.h"
22 #include "clock_config.h"
23 
24 /*******************************************************************************
25  * Definitions
26  ******************************************************************************/
27 #ifndef PIT_LED_HANDLER
28 #define PIT_LED_HANDLER PIT0_IRQHandler
29 #endif
30 #ifndef PIT_IRQ_ID
31 #define PIT_IRQ_ID PIT0_IRQn
32 #endif
33 #define PIT_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_BusClk)
34 
35 volatile bool pitIsrFlag = false;
36 
37 void PIT_LED_HANDLER(void)
38 {
39  /* Clear interrupt flag.*/
40  PIT_ClearStatusFlags(PIT, kPIT_Chnl_0, PIT_TFLG_TIF_MASK);
41  pitIsrFlag = true;
42 }
43 
44 void pit_init(uint32_t microseconds)
45 {
46  /* initialize PIT */
47  pit_config_t pitConfig; // Declare KSDK PIT configuration structure
48  PIT_GetDefaultConfig(&pitConfig); // Fill out that structure with defaults
49  PIT_Init(PIT, &pitConfig); // PIT is declared by the KSDK
50  // We choose to use Channel 0 of the PIT. That can obviously be changed
51  PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(microseconds, PIT_SOURCE_CLOCK));
52  PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);
53  EnableIRQ(PIT_IRQ_ID);
54  PIT_StartTimer(PIT, kPIT_Chnl_0);
55 }
volatile bool pitIsrFlag
Definition: driver_pit.c:35
#define PIT_IRQ_ID
Definition: driver_pit.c:31
#define PIT_SOURCE_CLOCK
Definition: driver_pit.c:33
void pit_init(uint32_t microseconds)
Definition: driver_pit.c:44
#define PIT_LED_HANDLER
Definition: driver_pit.c:28