aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/drivers/inc/fsl_lptmr_driver.h
blob: 9ef2faefa23951328a3f5af6abfa7691f9d720a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef __FSL_LPTMR_DRIVER_H__
#define __FSL_LPTMR_DRIVER_H__

#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include "fsl_lptmr_hal.h"
#include "fsl_sim_hal.h"
#if FSL_FEATURE_SOC_LPTMR_COUNT

/*!
 * @addtogroup lptmr_driver
 * @{
 */

/*******************************************************************************
 * Definitions
 *******************************************************************************/
/*! @brief Table of base addresses for LPTMR instances. */
extern LPTMR_Type * const g_lptmrBase[];

/*! @brief Table to save LPTMR IRQ enumeration numbers defined in the CMSIS header file. */
extern const IRQn_Type g_lptmrIrqId[LPTMR_INSTANCE_COUNT];

/*!
 * @brief Data structure to initialize the LPTMR
 *
 * This structure is used when initializing the LPTMR during the LPTMR_DRV_Init function call.
 * @internal gui name="LPTMR configuration" id="lptmrCfg"
 */
typedef struct LptmrUserConfig {
    lptmr_timer_mode_t timerMode; /*!< Timer counter mode or pulse counter mode @internal gui name="Timer mode" */
    lptmr_pin_select_t pinSelect; /*!< LPTMR pulse input pin select @internal gui name="Pin select" */
    lptmr_pin_polarity_t pinPolarity; /*!< LPTMR pulse input pin polarity @internal gui name="Pin polarity" */
    bool freeRunningEnable; /*!< Free running configure. True means enable free running @internal gui name="Free running" */
    bool  prescalerEnable; /*!< Prescaler enable configure. True means enable prescaler @internal gui name="Prescaler" */
    clock_lptmr_src_t prescalerClockSource; /*!< LPTMR clock source @internal gui name="Prescaler clock source" */
    lptmr_prescaler_value_t prescalerValue; /*!< Prescaler value @internal gui name="Prescaler value" */
    bool isInterruptEnabled;  /*!< Timer interrupt 0-disable/1-enable @internal gui name="Interrupt" */
} lptmr_user_config_t;

/*!
 * @brief Defines a type of the user-defined callback function.
 */
typedef void (*lptmr_callback_t)(void);

/*!
 * @brief Internal driver state information.
 *
 * The contents of this structure are internal to the driver and should not be
 *  modified by users. Contents of the structure are subject to change in
 *  future releases.
 */
typedef struct LptmrState {
    lptmr_callback_t userCallbackFunc; /*!< Callback function that is executed in ISR. */
    uint32_t prescalerClockHz;
} lptmr_state_t;


/*******************************************************************************
 * API
 *******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @name LPTMR Driver
 * @{
 */

/*!
 * @brief Initializes the LPTMR driver.
 *
 * This function initializes the LPTMR. The LPTMR can be initialized as a time counter or pulse counter,
 * which is determined by the timerMode in the lptmr_user_config_t. pinSelect and pinPolarity do not need to be
 * configured while working as a time counter.
 *
 * @param instance The LPTMR peripheral instance number.
 * @param userStatePtr The pointer to the structure of the context memory, see #lptmr_state_t.
 * @param userConfigPtr The pointer to the LPTMR user configure structure, see #lptmr_user_config_t.
 * @return kStatus_LPTMR_Success means succeed, otherwise means failed.
 */
lptmr_status_t LPTMR_DRV_Init(uint32_t instance, lptmr_state_t *userStatePtr, const lptmr_user_config_t* userConfigPtr);

/*!
 * @brief De-initializes the LPTMR driver.
 *
 * This function  de-initializes the LPTMR. It disables the interrupt and turns off the LPTMR clock.
 *
 * @param instance The LPTMR peripheral instance number.
 * @return kStatus_LPTMR_Success means succeed, otherwise means failed.
 */
lptmr_status_t LPTMR_DRV_Deinit(uint32_t instance);

/*!
 * @brief Starts the LPTMR counter.
 *
 * This function starts the LPTMR counter. Ensure that all necessary
 * configurations are set before calling this function.
 *
 * @param instance The LPTMR peripheral instance number.
 * @return kStatus_LPTMR_Success means success. Otherwise, means failure.
 */
lptmr_status_t LPTMR_DRV_Start(uint32_t instance);

/*!
 * @brief Stops the LPTMR counter.
 *
 * This function stops the LPTMR counter.
 *
 * @param instance The LPTMR peripheral instance number.
 * @return kStatus_LPTMR_Success means success. Otherwise, means failure.
 */
lptmr_status_t LPTMR_DRV_Stop(uint32_t instance);

/*!
 * @brief Configures the LPTMR timer period in microseconds.
 *
 * This function configures the LPTMR time period while the LPTMR is working as a
 * time counter. After the time period in microseconds, the callback function is called.
 * This function cannot be called while the LPTMR is working as a pulse counter.
 * The value in microseconds (us) should be integer multiple of the clock source time slice. If the clock source
 * is 1 kHz, then both 2000 us and 3000 us are valid while 2500 us gets the same result as the 2000 µs,
 * because 2500 us cannot be generated in 1 kHz clock source.
 *
 * @param instance The LPTMR peripheral instance number.
 * @param us time period in microseconds.
 * @return kStatus_LPTMR_Success means success. Otherwise, means failure.
 */
lptmr_status_t LPTMR_DRV_SetTimerPeriodUs(uint32_t instance, uint32_t us);

 /*!
 * @brief Gets the current LPTMR time in microseconds.
 *
 * This function gets the current time while operating as a time counter.
 * This function cannot be called while operating as a pulse counter.
 *
 * @param instance The LPTMR peripheral instance number.
 * @return current time in microsecond unit.
 */
uint32_t LPTMR_DRV_GetCurrentTimeUs(uint32_t instance);

/*!
 * @brief Sets the pulse period value.
 *
 * This function configures the pulse period of the LPTMR while working as a
 * pulse counter. After the count of pulsePeriodValue pulse is captured, the callback function
 * is called.
 * This function cannot be called while operating as a time counter.
 *
 * @param instance The LPTMR peripheral instance number.
 * @param pulsePeriodCount pulse period value.
 * @return kStatus_LPTMR_Success means success. Otherwise, means failure.
 */
lptmr_status_t LPTMR_DRV_SetPulsePeriodCount(uint32_t instance, uint32_t pulsePeriodCount);

 /*!
 * @brief Gets the current pulse count.
 *
 * This function gets the current pulse count captured on the pulse input pin.
 * This function cannot be called while operating as a time counter.
 *
 * @param instance The LPTMR peripheral instance number.
 * @return pulse count captured on the pulse input pin.
 */
uint32_t LPTMR_DRV_GetCurrentPulseCount(uint32_t instance);

/*!
 * @brief Installs the user-defined callback in the LPTMR module.
 *
 * This function installs the user-defined callback in the LPTMR module.
 * When an LPTMR interrupt request is served, the callback is executed
 * inside the ISR.
 *
 * @param instance LPTMR instance ID.
 * @param userCallback User-defined callback function.
 * @return kStatus_LPTMR_Success means success. Otherwise, means failure.
 */
lptmr_status_t LPTMR_DRV_InstallCallback(uint32_t instance, lptmr_callback_t userCallback);

/*!
 * @brief Driver-defined ISR in the LPTMR module.
 *
 * This function is the driver-defined ISR in LPTMR module.
 * It includes the process for interrupt mode defined by driver. Currently, it
 * is called inside the system-defined ISR.
 *
 * @param instance LPTMR instance ID.
 */
void LPTMR_DRV_IRQHandler(uint32_t instance);

/*@}*/

#if defined(__cplusplus)
}
#endif

/*! @}*/

#endif
#endif /* __FSL_LPTMR_H__*/
/*******************************************************************************
 * EOF
 *******************************************************************************/