Skip to content

Commit 3d859ca

Browse files
author
Cruz Monrreal
authored
Merge pull request #8272 from NXPmicro/Ensure_RTC_OSC_Start
MCUXpresso: Ensure the RTC OSC is running at bootup on Kinetis platforms
2 parents 03f4623 + ebf31c6 commit 3d859ca

File tree

17 files changed

+376
-116
lines changed

17 files changed

+376
-116
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/TARGET_FRDM/mbed_overrides.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,37 @@
1717

1818
#define CRC16
1919
#include "crc.h"
20+
#include "fsl_rtc.h"
2021
#include "fsl_clock_config.h"
2122

2223
// called before main
2324
void mbed_sdk_init()
2425
{
26+
rtc_config_t rtc_basic_config;
27+
uint32_t u32cTPR_counter = 0;
28+
2529
BOARD_BootClockRUN();
30+
31+
CLOCK_EnableClock(kCLOCK_Rtc0);
32+
33+
/* Check if the Rtc oscillator is enabled */
34+
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u) {
35+
/* Setup the 32K RTC OSC */
36+
RTC_Init(RTC, &rtc_basic_config);
37+
38+
/* Enable the RTC 32KHz oscillator */
39+
RTC->CR |= RTC_CR_OSCE_MASK;
40+
41+
/* Start the RTC time counter */
42+
RTC_StartTimer(RTC);
43+
44+
/* Verify TPR register reaches 4096 counts */
45+
while (u32cTPR_counter < 4096) {
46+
u32cTPR_counter = RTC->TPR;
47+
}
48+
/* 32kHz Oscillator is ready. */
49+
RTC_Deinit(RTC);
50+
}
2651
}
2752

2853
// Change the NMI pin to an input. This allows NMI pin to
@@ -34,13 +59,6 @@ void NMI_Handler(void)
3459
gpio_init_in(&gpio, PTA4);
3560
}
3661

37-
// Enable the RTC oscillator if available on the board
38-
void rtc_setup_oscillator(RTC_Type *base)
39-
{
40-
/* Enable the RTC oscillator */
41-
RTC->CR |= RTC_CR_OSCE_MASK;
42-
}
43-
4462
// Provide ethernet devices with a semi-unique MAC address from the UUID
4563
void mbed_mac_address(char *mac)
4664
{
@@ -55,7 +73,7 @@ void mbed_mac_address(char *mac)
5573

5674
// generate three CRC16's using different slices of the UUID
5775
MAC[0] = crcSlow((const uint8_t *)UID, 8); // most significant half-word
58-
MAC[1] = crcSlow((const uint8_t *)UID, 12);
76+
MAC[1] = crcSlow((const uint8_t *)UID, 12);
5977
MAC[2] = crcSlow((const uint8_t *)UID, 16); // least significant half word
6078

6179
// The network stack expects an array of 6 bytes

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/TARGET_FRDM/mbed_overrides.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,37 @@
1414
* limitations under the License.
1515
*/
1616
#include "gpio_api.h"
17+
#include "fsl_rtc.h"
1718
#include "fsl_clock_config.h"
1819

1920
// called before main
2021
void mbed_sdk_init()
2122
{
23+
rtc_config_t rtc_basic_config;
24+
uint32_t u32cTPR_counter = 0;
25+
2226
BOARD_BootClockRUN();
27+
28+
CLOCK_EnableClock(kCLOCK_Rtc0);
29+
30+
/* Check if the Rtc oscillator is enabled */
31+
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u) {
32+
/* Setup the 32K RTC OSC */
33+
RTC_Init(RTC, &rtc_basic_config);
34+
35+
/* Enable the RTC 32KHz oscillator */
36+
RTC->CR |= RTC_CR_OSCE_MASK;
37+
38+
/* Start the RTC time counter */
39+
RTC_StartTimer(RTC);
40+
41+
/* Verify TPR register reaches 4096 counts */
42+
while (u32cTPR_counter < 4096) {
43+
u32cTPR_counter = RTC->TPR;
44+
}
45+
/* 32kHz Oscillator is ready. */
46+
RTC_Deinit(RTC);
47+
}
2348
}
2449

2550
// Change the NMI pin to an input. This allows NMI pin to
@@ -31,13 +56,6 @@ void NMI_Handler(void)
3156
gpio_init_in(&gpio, PTA4);
3257
}
3358

34-
// Enable the RTC oscillator if available on the board
35-
void rtc_setup_oscillator(RTC_Type *base)
36-
{
37-
/* Enable the RTC oscillator */
38-
RTC->CR |= RTC_CR_OSCE_MASK;
39-
}
40-
4159
// Set the UART clock source
4260
void serial_clock_init(void)
4361
{

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/TARGET_UBRIDGE/mbed_overrides.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
#include "fsl_smc.h"
1818
#include "fsl_rcm.h"
1919
#include "fsl_pmc.h"
20+
#include "fsl_rtc.h"
2021
#include "fsl_clock_config.h"
2122

2223
//!< this contains the wakeup source
2324
rcm_reset_source_t kinetisResetSource;
2425

2526
// called before main
26-
void mbed_sdk_init() {
27+
void mbed_sdk_init()
28+
{
29+
rtc_config_t rtc_basic_config;
30+
uint32_t u32cTPR_counter = 0;
31+
2732
SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
2833

2934
// check the power mode source
@@ -36,6 +41,26 @@ void mbed_sdk_init() {
3641

3742
BOARD_BootClockRUN();
3843

44+
CLOCK_EnableClock(kCLOCK_Rtc0);
45+
46+
/* Check if the Rtc oscillator is enabled */
47+
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u) {
48+
/* Setup the 32K RTC OSC */
49+
RTC_Init(RTC, &rtc_basic_config);
50+
51+
/* Enable the RTC 32KHz oscillator */
52+
RTC->CR |= RTC_CR_OSCE_MASK;
53+
54+
/* Start the RTC time counter */
55+
RTC_StartTimer(RTC);
56+
57+
/* Verify TPR register reaches 4096 counts */
58+
while (u32cTPR_counter < 4096) {
59+
u32cTPR_counter = RTC->TPR;
60+
}
61+
/* 32kHz Oscillator is ready. */
62+
RTC_Deinit(RTC);
63+
}
3964
}
4065

4166
// Change the NMI pin to an input. This allows NMI pin to
@@ -47,13 +72,6 @@ void NMI_Handler(void)
4772
gpio_init_in(&gpio, PTA4);
4873
}
4974

50-
// Enable the RTC oscillator if available on the board
51-
void rtc_setup_oscillator(RTC_Type *base)
52-
{
53-
/* Enable the RTC oscillator */
54-
RTC->CR |= RTC_CR_OSCE_MASK;
55-
}
56-
5775
// Set the UART clock source
5876
void serial_clock_init(void)
5977
{

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/TARGET_FRDM/mbed_overrides.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,40 @@
1515
*/
1616
#include "gpio_api.h"
1717
#include "pinmap.h"
18+
#include "fsl_rtc.h"
1819
#include "fsl_clock_config.h"
1920

2021
// called before main - implement here if board needs it otherwise, let
2122
// the application override this if necessary
2223
void mbed_sdk_init()
2324
{
25+
rtc_config_t rtc_basic_config;
26+
uint32_t u32cTPR_counter = 0;
27+
2428
BOARD_BootClockRUN();
2529
/* Set the TPM clock source to be IRC48M, do not change as TPM2 is used for the usticker */
2630
CLOCK_SetTpmClock(1U);
27-
}
2831

29-
// Enable the RTC oscillator if available on the board
30-
void rtc_setup_oscillator(RTC_Type *base)
31-
{
32-
/* Enable the RTC oscillator */
33-
RTC->CR |= RTC_CR_OSCE_MASK;
32+
CLOCK_EnableClock(kCLOCK_Rtc0);
33+
34+
/* Check if the Rtc oscillator is enabled */
35+
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u) {
36+
/* Setup the 32K RTC OSC */
37+
RTC_Init(RTC, &rtc_basic_config);
38+
39+
/* Enable the RTC 32KHz oscillator */
40+
RTC->CR |= RTC_CR_OSCE_MASK;
41+
42+
/* Start the RTC time counter */
43+
RTC_StartTimer(RTC);
44+
45+
/* Verify TPR register reaches 4096 counts */
46+
while (u32cTPR_counter < 4096) {
47+
u32cTPR_counter = RTC->TPR;
48+
}
49+
/* 32kHz Oscillator is ready. */
50+
RTC_Deinit(RTC);
51+
}
3452
}
3553

3654
// Change the NMI pin to an input. This allows NMI pin to

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/TARGET_FRDM/mbed_overrides.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,40 @@
1515
*/
1616
#include "gpio_api.h"
1717
#include "pinmap.h"
18+
#include "fsl_rtc.h"
1819
#include "fsl_clock_config.h"
1920

2021
// called before main - implement here if board needs it otherwise, let
2122
// the application override this if necessary
2223
void mbed_sdk_init()
2324
{
25+
rtc_config_t rtc_basic_config;
26+
uint32_t u32cTPR_counter = 0;
27+
2428
BOARD_BootClockRUN();
2529
/* Set the TPM clock source to be IRC48M, do not change as TPM2 is used for the usticker */
2630
CLOCK_SetTpmClock(1U);
27-
}
2831

29-
// Enable the RTC oscillator if available on the board
30-
void rtc_setup_oscillator(RTC_Type *base)
31-
{
32-
/* Enable the RTC oscillator */
33-
RTC->CR |= RTC_CR_OSCE_MASK;
32+
CLOCK_EnableClock(kCLOCK_Rtc0);
33+
34+
/* Check if the Rtc oscillator is enabled */
35+
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u) {
36+
/* Setup the 32K RTC OSC */
37+
RTC_Init(RTC, &rtc_basic_config);
38+
39+
/* Enable the RTC 32KHz oscillator */
40+
RTC->CR |= RTC_CR_OSCE_MASK;
41+
42+
/* Start the RTC time counter */
43+
RTC_StartTimer(RTC);
44+
45+
/* Verify TPR register reaches 4096 counts */
46+
while (u32cTPR_counter < 4096) {
47+
u32cTPR_counter = RTC->TPR;
48+
}
49+
/* 32kHz Oscillator is ready. */
50+
RTC_Deinit(RTC);
51+
}
3452
}
3553

3654
// Change the NMI pin to an input. This allows NMI pin to

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/TARGET_FRDM/mbed_overrides.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,38 @@
1515
*/
1616
#include "gpio_api.h"
1717
#include "pinmap.h"
18+
#include "fsl_rtc.h"
1819
#include "fsl_clock_config.h"
1920

2021
// called before main - implement here if board needs it otherwise, let
2122
// the application override this if necessary
2223
void mbed_sdk_init()
2324
{
25+
rtc_config_t rtc_basic_config;
26+
uint32_t u32cTPR_counter = 0;
27+
2428
BOARD_BootClockRUN();
25-
}
2629

27-
// Enable the RTC oscillator if available on the board
28-
void rtc_setup_oscillator(RTC_Type *base)
29-
{
30-
/* Enable the RTC oscillator */
31-
RTC->CR |= RTC_CR_OSCE_MASK;
30+
CLOCK_EnableClock(kCLOCK_Rtc0);
31+
32+
/* Check if the Rtc oscillator is enabled */
33+
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u) {
34+
/* Setup the 32K RTC OSC */
35+
RTC_Init(RTC, &rtc_basic_config);
36+
37+
/* Enable the RTC 32KHz oscillator */
38+
RTC->CR |= RTC_CR_OSCE_MASK;
39+
40+
/* Start the RTC time counter */
41+
RTC_StartTimer(RTC);
42+
43+
/* Verify TPR register reaches 4096 counts */
44+
while (u32cTPR_counter < 4096) {
45+
u32cTPR_counter = RTC->TPR;
46+
}
47+
/* 32kHz Oscillator is ready. */
48+
RTC_Deinit(RTC);
49+
}
3250
}
3351

3452
// Change the NMI pin to an input. This allows NMI pin to

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/TARGET_USENSE/mbed_overrides.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
#include "fsl_smc.h"
1818
#include "fsl_rcm.h"
1919
#include "fsl_pmc.h"
20+
#include "fsl_rtc.h"
2021
#include "fsl_clock_config.h"
2122

2223
//!< this contains the wakeup source
2324
rcm_reset_source_t kinetisResetSource;
2425

2526
// called before main
26-
void mbed_sdk_init() {
27+
void mbed_sdk_init()
28+
{
29+
rtc_config_t rtc_basic_config;
30+
uint32_t u32cTPR_counter = 0;
31+
2732
SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
2833

2934
// check the power mode source
@@ -35,6 +40,27 @@ void mbed_sdk_init() {
3540
}
3641

3742
BOARD_BootClockRUN();
43+
44+
CLOCK_EnableClock(kCLOCK_Rtc0);
45+
46+
/* Check if the Rtc oscillator is enabled */
47+
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u) {
48+
/* Setup the 32K RTC OSC */
49+
RTC_Init(RTC, &rtc_basic_config);
50+
51+
/* Enable the RTC 32KHz oscillator */
52+
RTC->CR |= RTC_CR_OSCE_MASK;
53+
54+
/* Start the RTC time counter */
55+
RTC_StartTimer(RTC);
56+
57+
/* Verify TPR register reaches 4096 counts */
58+
while (u32cTPR_counter < 4096) {
59+
u32cTPR_counter = RTC->TPR;
60+
}
61+
/* 32kHz Oscillator is ready. */
62+
RTC_Deinit(RTC);
63+
}
3864
}
3965

4066
// Change the NMI pin to an input. This allows NMI pin to
@@ -46,15 +72,6 @@ void NMI_Handler(void)
4672
gpio_init_in(&gpio, PTA4);
4773
}
4874

49-
#if DEVICE_RTC || DEVICE_LPTICKER
50-
// Enable the RTC oscillator if available on the board
51-
void rtc_setup_oscillator(RTC_Type *base)
52-
{
53-
/* Enable the RTC oscillator */
54-
RTC->CR |= RTC_CR_OSCE_MASK;
55-
}
56-
#endif
57-
5875
// Set the UART clock source
5976
void serial_clock_init(void)
6077
{

0 commit comments

Comments
 (0)