Skip to content

Commit 3a48afa

Browse files
committed
Adapt K64F us ticker driver to the new standards.
- provide ticker configuration: 1MHz/32 bit counter. - us_ticker_init() routine disables interrupts. - adapt comments.
1 parent f35f932 commit 3a48afa

File tree

1 file changed

+62
-27
lines changed
  • targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F

1 file changed

+62
-27
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker.c

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2013 ARM Limited
2+
* Copyright (c) 2006-2018 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,16 @@
1919
#include "fsl_pit.h"
2020
#include "fsl_clock_config.h"
2121

22-
static int us_ticker_inited = 0;
22+
const ticker_info_t* us_ticker_get_info()
23+
{
24+
static const ticker_info_t info = {
25+
1000000, // 1 MHz
26+
32 // 32 bit counter
27+
};
28+
return &info;
29+
}
30+
31+
static bool us_ticker_inited = false;
2332

2433
static void pit_isr(void)
2534
{
@@ -31,72 +40,98 @@ static void pit_isr(void)
3140
us_ticker_irq_handler();
3241
}
3342

43+
/** Initialize the high frequency ticker
44+
*
45+
*/
3446
void us_ticker_init(void)
3547
{
36-
if (us_ticker_inited) {
37-
return;
38-
}
39-
us_ticker_inited = 1;
40-
//Common for ticker/timer
48+
/* Common for ticker/timer. */
4149
uint32_t busClock;
42-
// Structure to initialize PIT
50+
/* Structure to initialize PIT. */
4351
pit_config_t pitConfig;
4452

4553
PIT_GetDefaultConfig(&pitConfig);
4654
PIT_Init(PIT, &pitConfig);
4755

4856
busClock = CLOCK_GetFreq(kCLOCK_BusClk);
4957

50-
//Timer
51-
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1);
52-
PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF);
53-
PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true);
54-
PIT_StartTimer(PIT, kPIT_Chnl_0);
55-
PIT_StartTimer(PIT, kPIT_Chnl_1);
58+
/* Let the timer to count if re-init. */
59+
if (!us_ticker_inited) {
5660

57-
//Ticker
61+
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1);
62+
PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF);
63+
PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true);
64+
PIT_StartTimer(PIT, kPIT_Chnl_0);
65+
PIT_StartTimer(PIT, kPIT_Chnl_1);
66+
}
67+
68+
/* Configure interrupt generation counters and disable ticker interrupts. */
69+
PIT_StopTimer(PIT, kPIT_Chnl_3);
70+
PIT_StopTimer(PIT, kPIT_Chnl_2);
5871
PIT_SetTimerPeriod(PIT, kPIT_Chnl_2, busClock / 1000000 - 1);
5972
PIT_SetTimerChainMode(PIT, kPIT_Chnl_3, true);
60-
NVIC_SetVector(PIT3_IRQn, (uint32_t)pit_isr);
73+
NVIC_SetVector(PIT3_IRQn, (uint32_t) pit_isr);
6174
NVIC_EnableIRQ(PIT3_IRQn);
62-
}
75+
PIT_DisableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable);
6376

77+
us_ticker_inited = true;
78+
}
6479

80+
/** Read the current counter
81+
*
82+
* @return The current timer's counter value in ticks
83+
*/
6584
uint32_t us_ticker_read()
6685
{
67-
if (!us_ticker_inited) {
68-
us_ticker_init();
69-
}
70-
7186
return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1));
7287
}
7388

89+
/** Disable us ticker interrupt
90+
*
91+
*/
7492
void us_ticker_disable_interrupt(void)
7593
{
7694
PIT_DisableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable);
7795
}
7896

97+
/** Clear us ticker interrupt
98+
*
99+
*/
79100
void us_ticker_clear_interrupt(void)
80101
{
81102
PIT_ClearStatusFlags(PIT, kPIT_Chnl_3, PIT_TFLG_TIF_MASK);
82103
}
83104

105+
/** Set interrupt for specified timestamp
106+
*
107+
* @param timestamp The time in ticks when interrupt should be generated
108+
*/
84109
void us_ticker_set_interrupt(timestamp_t timestamp)
85110
{
86-
uint32_t now_us, delta_us;
87-
88-
now_us = us_ticker_read();
89-
delta_us = timestamp >= now_us ? timestamp - now_us : (uint32_t)((uint64_t)timestamp + 0xFFFFFFFF - now_us);
111+
/* We get here absolute interrupt time which takes into account counter overflow.
112+
* Since we use additional count-down timer to generate interrupt we need to calculate
113+
* load value based on time-stamp.
114+
*/
115+
const uint32_t now_ticks = us_ticker_read();
116+
uint32_t delta_ticks =
117+
timestamp >= now_ticks ? timestamp - now_ticks : (uint32_t)((uint64_t) timestamp + 0xFFFFFFFF - now_ticks);
118+
119+
if (delta_ticks == 0) {
120+
/* The requested delay is less than the minimum resolution of this counter. */
121+
delta_ticks = 1;
122+
}
90123

91-
uint32_t delta = timestamp - us_ticker_read();
92124
PIT_StopTimer(PIT, kPIT_Chnl_3);
93125
PIT_StopTimer(PIT, kPIT_Chnl_2);
94-
PIT_SetTimerPeriod(PIT, kPIT_Chnl_3, (uint32_t)delta_us);
126+
PIT_SetTimerPeriod(PIT, kPIT_Chnl_3, delta_ticks);
95127
PIT_EnableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable);
96128
PIT_StartTimer(PIT, kPIT_Chnl_3);
97129
PIT_StartTimer(PIT, kPIT_Chnl_2);
98130
}
99131

132+
/** Fire us ticker interrupt
133+
*
134+
*/
100135
void us_ticker_fire_interrupt(void)
101136
{
102137
NVIC_SetPendingIRQ(PIT3_IRQn);

0 commit comments

Comments
 (0)