Skip to content

Commit fb622a2

Browse files
mmahadevan108bulislaw
authored andcommitted
MIMXRT1050_EVK: Update lpticker implementation
Use only the GPT module and avoid using RTC. Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 3b7b0f1 commit fb622a2

File tree

2 files changed

+36
-55
lines changed

2 files changed

+36
-55
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/lp_ticker.c

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2016 ARM Limited
2+
* Copyright (c) 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.
@@ -17,20 +17,24 @@
1717
#if DEVICE_LPTICKER
1818

1919
#include "lp_ticker_api.h"
20-
#include "fsl_snvs_hp.h"
2120
#include "fsl_gpt.h"
2221
#include "cmsis.h"
23-
#include "rtc_api.h"
2422

25-
#define LOWFREQ_REF_CLK_HZ (32768)
23+
const ticker_info_t* lp_ticker_get_info()
24+
{
25+
static const ticker_info_t info = {
26+
32768, // 32kHz
27+
32 // 32 bit counter
28+
};
29+
return &info;
30+
}
2631

2732
static bool lp_ticker_inited = false;
2833

2934
static void gpt_isr(void)
3035
{
3136
GPT_ClearStatusFlags(GPT2, kGPT_OutputCompare1Flag);
32-
GPT_StopTimer(GPT2);
33-
37+
GPT_DisableInterrupts(GPT2, kGPT_OutputCompare1InterruptEnable);
3438
lp_ticker_irq_handler();
3539
}
3640

@@ -41,71 +45,48 @@ void lp_ticker_init(void)
4145
{
4246
gpt_config_t gptConfig;
4347

44-
if (lp_ticker_inited) {
45-
return;
46-
}
47-
lp_ticker_inited = true;
48-
49-
/* Setup low resolution clock - RTC */
50-
if (!rtc_isenabled()) {
51-
rtc_init();
48+
if (!lp_ticker_inited) {
49+
/* Setup GPT */
50+
GPT_GetDefaultConfig(&gptConfig);
51+
/* Use 32kHz drive */
52+
gptConfig.clockSource = kGPT_ClockSource_LowFreq;
53+
gptConfig.enableFreeRun = true;
54+
gptConfig.enableMode = false;
55+
56+
GPT_Init(GPT2, &gptConfig);
57+
GPT_EnableInterrupts(GPT2, kGPT_OutputCompare1InterruptEnable);
58+
NVIC_ClearPendingIRQ(GPT2_IRQn);
59+
NVIC_SetVector(GPT2_IRQn, (uint32_t)gpt_isr);
60+
EnableIRQ(GPT2_IRQn);
61+
GPT_StartTimer(GPT2);
62+
lp_ticker_inited = true;
63+
} else {
64+
GPT_DisableInterrupts(GPT2, kGPT_OutputCompare1InterruptEnable);
5265
}
53-
54-
/* Setup GPT */
55-
GPT_GetDefaultConfig(&gptConfig);
56-
/* Use 32kHz drive */
57-
gptConfig.clockSource = kGPT_ClockSource_LowFreq;
58-
GPT_Init(GPT2, &gptConfig);
59-
GPT_EnableInterrupts(GPT2, kGPT_OutputCompare1InterruptEnable);
60-
NVIC_ClearPendingIRQ(GPT2_IRQn);
61-
NVIC_SetVector(GPT2_IRQn, (uint32_t)gpt_isr);
62-
EnableIRQ(GPT2_IRQn);
6366
}
6467

6568
/** Read the current counter
6669
*
67-
* @return The current timer's counter value in microseconds
70+
* @return The current timer's counter value in ticks
6871
*/
6972
uint32_t lp_ticker_read(void)
7073
{
71-
uint32_t ticks = 0;
72-
uint64_t tmp = 0;
73-
74-
if (!lp_ticker_inited) {
75-
lp_ticker_init();
76-
}
77-
78-
/* Do consecutive reads until value is correct */
79-
do
80-
{
81-
ticks = tmp;
82-
tmp = SNVS->HPRTCLR;
83-
} while (tmp != ticks);
84-
85-
return COUNT_TO_USEC(ticks, LOWFREQ_REF_CLK_HZ);;
74+
return GPT_GetCurrentTimerCount(GPT2);
8675
}
8776

8877
/** Set interrupt for specified timestamp
8978
*
90-
* @param timestamp The time in microseconds to be set
79+
* @param timestamp The time in ticks to be set
9180
*/
9281
void lp_ticker_set_interrupt(timestamp_t timestamp)
9382
{
94-
uint32_t now_us, delta_us, delta_ticks;
95-
96-
if (!lp_ticker_inited) {
97-
lp_ticker_init();
83+
if (timestamp == 0) {
84+
timestamp = 1;
9885
}
9986

100-
now_us = lp_ticker_read();
101-
delta_us = timestamp > now_us ? timestamp - now_us : (uint32_t)((uint64_t)timestamp + 0xFFFFFFFF - now_us);
102-
103-
delta_ticks = USEC_TO_COUNT(delta_us, LOWFREQ_REF_CLK_HZ);
104-
if (delta_ticks == 0) {
105-
delta_ticks = 1;
106-
}
107-
108-
GPT_SetOutputCompareValue(GPT2, kGPT_OutputCompare_Channel1, delta_ticks);
87+
GPT_StopTimer(GPT2);
88+
GPT_SetOutputCompareValue(GPT2, kGPT_OutputCompare_Channel1, timestamp);
89+
GPT_ClearStatusFlags(GPT2, kGPT_OutputCompare1Flag);
10990
GPT_EnableInterrupts(GPT2, kGPT_OutputCompare1InterruptEnable);
11091
GPT_StartTimer(GPT2);
11192
}

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@
753753
"macros": ["CPU_MIMXRT1052DVL6A", "FSL_RTOS_MBED"],
754754
"inherits": ["Target"],
755755
"detect_code": ["0227"],
756-
"device_has": ["USTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "ERROR_RED", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
756+
"device_has": ["USTICKER", "LPTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "ERROR_RED", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
757757
"release_versions": ["2", "5"],
758758
"device_name": "MIMXRT1052"
759759
},

0 commit comments

Comments
 (0)