ISSDK  1.8
IoT Sensing Software Development Kit
driver_ctimer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 2016, 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_ctimer.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 CTIMER module on the LPC is one such module. The timer functions are
15  only referenced at the main() level. There is no interaction within the
16  fusion routines themselves.
17 */
18 #include "issdk_hal.h"
19 #include "board.h"
20 #include "fsl_ctimer.h"
21 #include "pin_mux.h"
22 #include "clock_config.h"
23 
24 /*******************************************************************************
25  * Definitions
26  ******************************************************************************/
27 #define CTIMER CTIMER0 /* Timer 0 */
28 #define CTIMER_MAT0_OUT kCTIMER_Match_0 /* Match output 0 */
29 #define BUS_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk)
30 
31 /*******************************************************************************
32  * Prototypes
33  ******************************************************************************/
34 void ctimer_callback(uint32_t flags);
35 
36 /* Array of function pointers for callback for each channel */
37 ctimer_callback_t ctimer_callback_table[] =
38 {
40  NULL,
41  NULL,
42  NULL,
43  NULL,
44  NULL,
45  NULL,
46  NULL
47 };
48 
49 volatile bool pitIsrFlag = false;
50 
51 void ctimer_callback(uint32_t flags)
52 {
53  CTIMER_ClearStatusFlags(CTIMER, kCTIMER_Match0Flag);
54  pitIsrFlag = true;
55  GPIO_TogglePinsOutput(GPIO, 0U, 1U << 30); // Arduino Pin A0 for LPC54114, for freq debug purpose only
56 }
57 
58 /*******************************************************************************
59 * Variables
60 ******************************************************************************/
61 /* Match Configuration for Channel 0 */
62 static ctimer_match_config_t matchConfig0;
63 
64 void pit_init(uint32_t microseconds)
65 {
66  long busFreq = BUS_CLK_FREQ;
67  ctimer_config_t config;
68 
69  long fusionHz = 1000000/microseconds;
70  // Program Arduino A0 pin as output
71  GPIO_PinInit(GPIO, 0U, 30, &(gpio_pin_config_t){kGPIO_DigitalOutput, 1U}); // Arduino Pin A0 for LPC54114
72 
73  CTIMER_GetDefaultConfig(&config);
74 
75  CTIMER_Init(CTIMER, &config);
76 
77  /* Configuration 0 */
78  matchConfig0.enableCounterReset = true;
79  matchConfig0.enableCounterStop = false;
80  matchConfig0.matchValue = busFreq / fusionHz;
81  matchConfig0.outControl = kCTIMER_Output_Toggle;
82  matchConfig0.outPinInitState = false;
83  matchConfig0.enableInterrupt = true;
84 
85  /* initialize CTIMER */
86  CTIMER_RegisterCallBack(CTIMER, &ctimer_callback_table[0], kCTIMER_MultipleCallback);
87  CTIMER_SetupMatch(CTIMER, CTIMER_MAT0_OUT, &matchConfig0);
88  CTIMER_StartTimer(CTIMER);
89 }
#define CTIMER_MAT0_OUT
Definition: driver_ctimer.c:28
ctimer_callback_t ctimer_callback_table[]
Definition: driver_ctimer.c:37
#define CTIMER
Definition: driver_ctimer.c:27
void pit_init(uint32_t microseconds)
Definition: driver_ctimer.c:64
#define BUS_CLK_FREQ
Definition: driver_ctimer.c:29
volatile bool pitIsrFlag
Definition: driver_ctimer.c:49
void ctimer_callback(uint32_t flags)
Definition: driver_ctimer.c:51