Skip to content

Commit 6b96d4e

Browse files
committed
us ticker - pit implementation (not functional yet)
- pit driver in KPSDK - added sdk prefix to needed functions
1 parent 098e60a commit 6b96d4e

File tree

3 files changed

+63
-45
lines changed

3 files changed

+63
-45
lines changed

libraries/mbed/targets/hal/TARGET_Freescale/KPSDK/drivers/pit/fsl_pit_driver.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
*/
3030
#ifndef __FSL_PIT_DRIVER_H__
3131
#define __FSL_PIT_DRIVER_H__
32-
32+
3333
#include <stdint.h>
3434
#include <stdbool.h>
3535
#include "fsl_pit_hal.h"
36-
36+
3737
/*!
3838
* @addtogroup pit_driver
3939
* @{
@@ -47,7 +47,7 @@
4747
* @brief PIT timer configuration structure
4848
*
4949
* Define structure PitConfig and use pit_init() to make necessary initializations.
50-
* You may also use remaining functions for PIT configuration. The timer chain and
50+
* You may also use remaining functions for PIT configuration. The timer chain and
5151
* lifetime timer features are only available in specific MCUs.
5252
*/
5353
typedef struct PitTimerConfig {
@@ -70,7 +70,7 @@ typedef void (*pit_isr_callback_t)(void);
7070
/*******************************************************************************
7171
* API
7272
******************************************************************************/
73-
73+
7474
#if defined(__cplusplus)
7575
extern "C" {
7676
#endif
@@ -82,21 +82,21 @@ extern "C" {
8282

8383
/*!
8484
* @brief Initialize PIT in peripheral driver level.
85-
*
86-
* This function un-gates the PIT clock and enables the PIT module automatically.
87-
* Users should define the config structure and pass it in. Timers do not
85+
*
86+
* This function un-gates the PIT clock and enables the PIT module automatically.
87+
* Users should define the config structure and pass it in. Timers do not
8888
* start counting after calling this function by default. Function pit_timer_start
8989
* must be called to start timer counting. Periods set in this function are
90-
* only in units of count. Call pit_set_timer_period_us to re-set the period in
90+
* only in units of count. Call pit_set_timer_period_us to re-set the period in
9191
* microsecond units before starting timers.
9292
*
9393
* Here is an example demonstrating how to define a PIT config structure:
9494
@code
9595
pit_config_t pitTestInit = {
96-
.isRunInDebug = false,
96+
.isRunInDebug = false,
9797
.timers[0].isInterruptEnabled = true,
9898
// Only valid when chain feature is available.
99-
.timers[0].isTimerChained = false,
99+
.timers[0].isTimerChained = false,
100100
.timers[0].period = 0x1234FFFF,
101101
.timers[1].isInterruptEnabled = false,
102102
.timers[1].isTimerChained = false,
@@ -106,7 +106,7 @@ extern "C" {
106106
*
107107
* @param config PIT configuration data.
108108
*/
109-
void pit_init(const pit_config_t * config);
109+
void sdk_pit_init(const pit_config_t * config);
110110

111111
/*!
112112
* @brief Disable PIT module and gate control.
@@ -119,7 +119,7 @@ void pit_shutdown(void);
119119
/* @} */
120120

121121
/*!
122-
* @name Timer Start and Stop
122+
* @name Timer Start and Stop
123123
* @{
124124
*/
125125

@@ -155,20 +155,20 @@ void pit_timer_stop(uint32_t timer);
155155
* @brief Set timer period in microsecond units.
156156
*
157157
* The period range depends on the frequency of PIT source clock. If the required period
158-
* is out of range, use the lifetime timer, if applicable.
159-
*
158+
* is out of range, use the lifetime timer, if applicable.
159+
*
160160
* @param timer Timer channel number.
161161
* @param us Timer period in microseconds.
162162
*/
163163
void pit_set_timer_period_us(uint32_t timer, uint32_t us);
164164

165165
/*!
166166
* @brief Read current timer value in microsecond units.
167-
*
167+
*
168168
* This function returns an absolute time stamp in microsecond units.
169169
* One common use of this function is to measure the running time of a part of
170170
* code. Call this function at both the beginning and end of code; the time
171-
* difference between these two time stamps is the running time (Make sure the
171+
* difference between these two time stamps is the running time (Make sure the
172172
* running time will not exceed the timer period). The time stamp returned is
173173
* up-counting.
174174
*
@@ -180,7 +180,7 @@ uint32_t pit_read_timer_us(uint32_t timer);
180180
#if FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
181181
/*!
182182
* @brief Set lifetime timer period.
183-
*
183+
*
184184
* Timer 1 must be chained with timer 0 before using the lifetime timer. The period
185185
* range is restricted by "period * pitSourceClock < max of an uint64_t integer",
186186
* or it may cause an overflow and be unable to set the correct period.
@@ -192,7 +192,7 @@ void pit_set_lifetime_timer_period_us(uint64_t us);
192192
/*!
193193
* @brief Read current lifetime value in microseconds.
194194
*
195-
* This feature returns an absolute time stamp in microsecond units. The time stamp
195+
* This feature returns an absolute time stamp in microsecond units. The time stamp
196196
* value will not exceed the timer period. The timer is up-counting.
197197
*
198198
* @return Current lifetime timer value in microseconds.
@@ -203,14 +203,14 @@ uint64_t pit_read_lifetime_timer_us(void);
203203
/* @} */
204204

205205
/*!
206-
* @name ISR Callback Function
206+
* @name ISR Callback Function
207207
* @{
208208
*/
209209

210210
/*!
211-
* @brief Register pit isr callback function.
211+
* @brief Register pit isr callback function.
212212
*
213-
* System default ISR interfaces are already defined in fsl_pit_irq.c. Users
213+
* System default ISR interfaces are already defined in fsl_pit_irq.c. Users
214214
* can either edit these ISRs or use this function to register a callback
215215
* function. The default ISR runs the callback function if there is one
216216
* installed.
@@ -225,9 +225,9 @@ void pit_register_isr_callback_function(uint32_t timer, pit_isr_callback_t funct
225225
#if defined(__cplusplus)
226226
}
227227
#endif
228-
228+
229229
/*! @}*/
230-
230+
231231
#endif /* __FSL_PIT_DRIVER_H__*/
232232
/*******************************************************************************
233233
* EOF

libraries/mbed/targets/hal/TARGET_Freescale/KPSDK/drivers/pit/src/fsl_pit_driver.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2828
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
30-
30+
3131
#include "fsl_pit_driver.h"
3232
#include "fsl_clock_manager.h"
3333

@@ -38,7 +38,7 @@
3838
/* pit source clock variable which will be updated in pit_init. */
3939
uint64_t pitSourceClock;
4040

41-
/* Table to save PIT IRQ enum numbers defined in CMSIS files. This table is
41+
/* Table to save PIT IRQ enum numbers defined in CMSIS files. This table is
4242
* defined in fsl_pit_irq.c */
4343
extern IRQn_Type pit_irq_ids[FSL_FEATURE_PIT_TIMER_COUNT];
4444

@@ -50,15 +50,15 @@ extern IRQn_Type pit_irq_ids[FSL_FEATURE_PIT_TIMER_COUNT];
5050
*
5151
* Function Name : pit_init
5252
* Description : Initialize PIT in peripheral level.
53-
* This function will un-gate the PIT clock and enable PIT module automatically.
54-
* Users should define the config structure and pass it in. Timers will not
53+
* This function will un-gate the PIT clock and enable PIT module automatically.
54+
* Users should define the config structure and pass it in. Timers will not
5555
* start counting after calling this function as default. Function pit_timer_start
5656
* need to be called to start timer counting. Periods set in this function are
5757
* only in unit of count. Call pit_set_timer_period_us to re-set the period in unit
5858
* of microseconds before starting timers.
5959
*
6060
*END**************************************************************************/
61-
void pit_init(const pit_config_t * config)
61+
void sdk_pit_init(const pit_config_t * config)
6262
{
6363
uint32_t i, busClock;
6464

@@ -70,7 +70,7 @@ void pit_init(const pit_config_t * config)
7070

7171
/* Set timer run or stop in debug mode*/
7272
pit_hal_configure_timer_run_in_debug(config->isRunInDebug);
73-
73+
7474
/* Configure timer channels*/
7575
for(i=0; i < FSL_FEATURE_PIT_TIMER_COUNT; i++)
7676
{
@@ -91,20 +91,20 @@ void pit_init(const pit_config_t * config)
9191
/* Enable PIT interrupt.*/
9292
NVIC_EnableIRQ(pit_irq_ids[i]);
9393
}
94-
}
95-
94+
}
95+
9696
/* Finally, update pit source clock frequency.*/
97-
clock_manager_get_frequency(kBusClock, &busClock);
97+
clock_manager_get_frequency(kBusClock, &busClock);
9898
pitSourceClock = (uint64_t)busClock;
9999
}
100100

101101
/*FUNCTION**********************************************************************
102102
*
103-
* Function Name : pit_shutdown
103+
* Function Name : pit_shutdown
104104
* Description : Disable PIT module and gate control
105105
* This function will disable all PIT interrupts and PIT clock. Then gate the
106106
* PIT clock control. pit_init must be called in order to use PIT again.
107-
*
107+
*
108108
*END**************************************************************************/
109109
void pit_shutdown(void)
110110
{
@@ -130,7 +130,7 @@ void pit_shutdown(void)
130130
* After calling this function, timers load period value, count down to 0 and
131131
* then load the respective start value again. Each time a timer reaches 0,
132132
* it will generate a trigger pulse and set the timeout interrupt flag.
133-
*
133+
*
134134
*END**************************************************************************/
135135
void pit_timer_start(uint32_t timer)
136136
{
@@ -155,7 +155,7 @@ void pit_timer_stop(uint32_t timer)
155155
* Function Name : pit_set_timer_period_us
156156
* Description : Set timer period in microseconds unit.
157157
* The period range depends on the frequency of PIT source clock. If required
158-
* period is out the range, try to use lifetime timer if applicable.
158+
* period is out the range, try to use lifetime timer if applicable.
159159
*
160160
*END**************************************************************************/
161161
void pit_set_timer_period_us(uint32_t timer, uint32_t us)
@@ -172,7 +172,7 @@ void pit_set_timer_period_us(uint32_t timer, uint32_t us)
172172
* This function will return an absolute time stamp in the unit of microseconds.
173173
* One common use of this function is to measure the running time of part of
174174
* code. Just call this function at both the beginning and end of code, the time
175-
* difference between these two time stamp will be the running time (Need to
175+
* difference between these two time stamp will be the running time (Need to
176176
* make sure the running time will not exceed the timer period). Also, the time
177177
* stamp returned is up-counting.
178178
*
@@ -200,7 +200,7 @@ uint32_t pit_read_timer_us(uint32_t timer)
200200
void pit_set_lifetime_timer_period_us(uint64_t us)
201201
{
202202
uint64_t lifeTimeCount;
203-
203+
204204
/* Calculate the counter value.*/
205205
lifeTimeCount = us * pitSourceClock / 1000000U - 1U;
206206

@@ -213,7 +213,7 @@ void pit_set_lifetime_timer_period_us(uint64_t us)
213213
*
214214
* Function Name : pit_read_lifetime_timer_us
215215
* Description : Read current lifetime value in microseconds unit.
216-
* Return an absolute time stamp in the unit of microseconds. The time stamp
216+
* Return an absolute time stamp in the unit of microseconds. The time stamp
217217
* value will not exceed the timer period. Also, the timer is up-counting.
218218
*
219219
*END**************************************************************************/

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K64F/us_ticker.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,47 @@
1616
#include <stddef.h>
1717
#include "us_ticker_api.h"
1818
#include "PeripheralNames.h"
19+
#include "fsl_pit_driver.h"
20+
#include "fsl_clock_manager.h"
1921

2022
static void pit_init(void);
2123
static void lptmr_init(void);
2224

2325
static int us_ticker_inited = 0;
2426

2527
void us_ticker_init(void) {
28+
if (us_ticker_inited)
29+
return;
30+
us_ticker_inited = 1;
2631

32+
pit_init();
33+
lptmr_init();
2734
}
2835

29-
static uint32_t pit_us_ticker_counter = 0;
30-
31-
void pit0_isr(void) {
32-
33-
}
3436

3537
uint32_t us_ticker_read() {
38+
if (!us_ticker_inited)
39+
us_ticker_init();
40+
41+
return ~(pit_hal_read_timer_count(1));
3642
}
3743
/******************************************************************************
3844
* Timer for us timing.
3945
******************************************************************************/
4046
static void pit_init(void) {
41-
47+
pit_config_t config = {0};
48+
uint32_t busClock;
49+
50+
clock_manager_get_frequency(kBusClock, &busClock);
51+
config.timers[0].period = busClock / 1000000 - 1;
52+
config.isRunInDebug = true;
53+
config.timers[0].isInterruptEnabled = false;
54+
config.timers[1].isInterruptEnabled = false;
55+
config.timers[1].period = 0xFFFFFFFF;
56+
config.timers[1].isTimerChained = true;
57+
sdk_pit_init((const pit_config_t *)&config);
58+
pit_timer_start(0);
59+
pit_timer_start(1);
4260
}
4361

4462
/******************************************************************************

0 commit comments

Comments
 (0)