Skip to content

Commit d321c11

Browse files
authored
Merge pull request #8872 from NXPmicro/Fix_RTC_Startup
MCUXpresso: Ensure the RTC OSC is running at bootup on Kinetis platforms
2 parents b52c164 + dcc8b65 commit d321c11

File tree

18 files changed

+456
-116
lines changed

18 files changed

+456
-116
lines changed

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,42 @@
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+
/*Init the RTC with default configuration*/
36+
RTC_GetDefaultConfig(&rtc_basic_config);
37+
38+
/* Setup the 32K RTC OSC */
39+
RTC_Init(RTC, &rtc_basic_config);
40+
41+
/* Enable the RTC 32KHz oscillator */
42+
RTC->CR |= RTC_CR_OSCE_MASK;
43+
44+
/* Start the RTC time counter */
45+
RTC_StartTimer(RTC);
46+
47+
/* Verify TPR register reaches 4096 counts */
48+
while (u32cTPR_counter < 4096) {
49+
u32cTPR_counter = RTC->TPR;
50+
}
51+
/* 32kHz Oscillator is ready. */
52+
RTC_Deinit(RTC);
53+
}
54+
55+
CLOCK_DisableClock(kCLOCK_Rtc0);
2656
}
2757

2858
// Change the NMI pin to an input. This allows NMI pin to
@@ -34,13 +64,6 @@ void NMI_Handler(void)
3464
gpio_init_in(&gpio, PTA4);
3565
}
3666

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-
4467
// Provide ethernet devices with a semi-unique MAC address from the UUID
4568
void mbed_mac_address(char *mac)
4669
{
@@ -55,7 +78,7 @@ void mbed_mac_address(char *mac)
5578

5679
// generate three CRC16's using different slices of the UUID
5780
MAC[0] = crcSlow((const uint8_t *)UID, 8); // most significant half-word
58-
MAC[1] = crcSlow((const uint8_t *)UID, 12);
81+
MAC[1] = crcSlow((const uint8_t *)UID, 12);
5982
MAC[2] = crcSlow((const uint8_t *)UID, 16); // least significant half word
6083

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

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,42 @@
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+
/*Init the RTC with default configuration*/
33+
RTC_GetDefaultConfig(&rtc_basic_config);
34+
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+
}
51+
52+
CLOCK_DisableClock(kCLOCK_Rtc0);
2353
}
2454

2555
// Change the NMI pin to an input. This allows NMI pin to
@@ -31,13 +61,6 @@ void NMI_Handler(void)
3161
gpio_init_in(&gpio, PTA4);
3262
}
3363

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-
4164
// Set the UART clock source
4265
void serial_clock_init(void)
4366
{

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

Lines changed: 31 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,31 @@ 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+
/*Init the RTC with default configuration*/
49+
RTC_GetDefaultConfig(&rtc_basic_config);
50+
51+
/* Setup the 32K RTC OSC */
52+
RTC_Init(RTC, &rtc_basic_config);
53+
54+
/* Enable the RTC 32KHz oscillator */
55+
RTC->CR |= RTC_CR_OSCE_MASK;
56+
57+
/* Start the RTC time counter */
58+
RTC_StartTimer(RTC);
59+
60+
/* Verify TPR register reaches 4096 counts */
61+
while (u32cTPR_counter < 4096) {
62+
u32cTPR_counter = RTC->TPR;
63+
}
64+
/* 32kHz Oscillator is ready. */
65+
RTC_Deinit(RTC);
66+
}
67+
68+
CLOCK_DisableClock(kCLOCK_Rtc0);
3969
}
4070

4171
// Change the NMI pin to an input. This allows NMI pin to
@@ -47,13 +77,6 @@ void NMI_Handler(void)
4777
gpio_init_in(&gpio, PTA4);
4878
}
4979

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-
5780
// Set the UART clock source
5881
void serial_clock_init(void)
5982
{

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,45 @@
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+
/*Init the RTC with default configuration*/
37+
RTC_GetDefaultConfig(&rtc_basic_config);
38+
39+
/* Setup the 32K RTC OSC */
40+
RTC_Init(RTC, &rtc_basic_config);
41+
42+
/* Enable the RTC 32KHz oscillator */
43+
RTC->CR |= RTC_CR_OSCE_MASK;
44+
45+
/* Start the RTC time counter */
46+
RTC_StartTimer(RTC);
47+
48+
/* Verify TPR register reaches 4096 counts */
49+
while (u32cTPR_counter < 4096) {
50+
u32cTPR_counter = RTC->TPR;
51+
}
52+
/* 32kHz Oscillator is ready. */
53+
RTC_Deinit(RTC);
54+
}
55+
56+
CLOCK_DisableClock(kCLOCK_Rtc0);
3457
}
3558

3659
// 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: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,45 @@
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+
/*Init the RTC with default configuration*/
37+
RTC_GetDefaultConfig(&rtc_basic_config);
38+
39+
/* Setup the 32K RTC OSC */
40+
RTC_Init(RTC, &rtc_basic_config);
41+
42+
/* Enable the RTC 32KHz oscillator */
43+
RTC->CR |= RTC_CR_OSCE_MASK;
44+
45+
/* Start the RTC time counter */
46+
RTC_StartTimer(RTC);
47+
48+
/* Verify TPR register reaches 4096 counts */
49+
while (u32cTPR_counter < 4096) {
50+
u32cTPR_counter = RTC->TPR;
51+
}
52+
/* 32kHz Oscillator is ready. */
53+
RTC_Deinit(RTC);
54+
}
55+
56+
CLOCK_DisableClock(kCLOCK_Rtc0);
3457
}
3558

3659
// 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: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,43 @@
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+
/*Init the RTC with default configuration*/
35+
RTC_GetDefaultConfig(&rtc_basic_config);
36+
37+
/* Setup the 32K RTC OSC */
38+
RTC_Init(RTC, &rtc_basic_config);
39+
40+
/* Enable the RTC 32KHz oscillator */
41+
RTC->CR |= RTC_CR_OSCE_MASK;
42+
43+
/* Start the RTC time counter */
44+
RTC_StartTimer(RTC);
45+
46+
/* Verify TPR register reaches 4096 counts */
47+
while (u32cTPR_counter < 4096) {
48+
u32cTPR_counter = RTC->TPR;
49+
}
50+
/* 32kHz Oscillator is ready. */
51+
RTC_Deinit(RTC);
52+
}
53+
54+
CLOCK_DisableClock(kCLOCK_Rtc0);
3255
}
3356

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

0 commit comments

Comments
 (0)