K64F RTC Hard fault during initialization

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

K64F RTC Hard fault during initialization

Jump to solution
2,067 Views
priyankb
Contributor III

 Hi, I am using MK64FN1M0VLL12 processor and working on RTC. But in the first line itself getting a hard fault. I tried to solve it but failed. can anyone help to solve this problem. External 32.768KHz crystal, Battery connected. Open to any suggestion.

This my initialization code. Thank you.

void rtc_init(void)

{

   volatile uint32_t i;

   SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;

   RTC->SR &= ~RTC_SR_TCE_MASK;

   RTC->CR &= ~RTC_CR_OSCE_MASK;

   RTC->CR |= RTC_CR_OSCE_MASK;

   for(i=0;i<0x60000;i++)

   { }

   RTC->SR |= RTC_SR_TCE_MASK;

}

Labels (1)
1 Solution
1,384 Views
mjbcswitzerland
Specialist V

Hi

A problem with a hard fault on attempted access to the RTC, even after its clock is unmasked in SCGC6, is likely to be a missing VBAT voltage; without the dedicated power supply the RTC is not powered and thus accesses hard-fault.

Regards

Mark

Kinetis: http://www.utasker.com/kinetis.html
Kinetis K64:
- http://www.utasker.com/kinetis/FRDM-K64F.html
- http://www.utasker.com/kinetis/TWR-K64F120M.html
- http://www.utasker.com/kinetis/TEENSY_3.5.html
- http://www.utasker.com/kinetis/Hexiwear-K64F.html
RTC and time keeping: http://www.utasker.com/docs/uTasker/uTasker_Time.pdf


Free Open Source solution: https://github.com/uTasker/uTasker-Kinetis
Working project in 15 minutes video: https://youtu.be/K8ScSgpgQ6M

Professional Kinetis support, one-on-one training and complete fast-track project solutions: http://www.utasker.com/support.html

View solution in original post

9 Replies
1,384 Views
mjbcswitzerland
Specialist V

Kevin

You can use an ADC input to measure the voltage.


But, you can also use the hard fault to detect the lack of VBAT too - your hard fault handler however will need to be able to allow the fault to recover when it occurs so that the code can continue and avoid further use of the RTC.
Since recovery from a hard fault made by the CPU is a little fiddly the simplest method is to set up a DMA transfer from or to it and check whether the DMA channel errors or not. If it doesn't error it can be read/written - if it errors it means that the DMA controller experienced a hard fault (this doesn't need to be recovered from and so is simple to use).

Regards

Mark

uTasker developer and supporter (+5'000 hours experience on +60 Kinetis derivatives in +80 product developments)
Kinetis: http://www.utasker.com/kinetis.html

1,384 Views
mjbcswitzerland
Specialist V

Here's the link to the verified uTasker solution:

http://www.utasker.com/forum/index.php?topic=2017.0


Regards

Mark

0 Kudos
1,384 Views
kevinyuthermo
Contributor III

How to detect VBAT? 

We would like to initialize RTC if VBAT is present, otherwise, skip to init RTC to avoid hard fault.

But there isn't a easy way to detect battery is on or missing, any suggestions?

0 Kudos
1,384 Views
priyankb
Contributor III

Kevin

@Mark suggested a great option.

Another one could be that you connect VDD and battery to Schottky  Diode. The output of diode to VBAT. This way even if your battery is down you will get voltage from Vdd. In this way there is never a problem of hard fault.

Detection: You can detect the battery(down) by reading RTC register. It will always start from 0 every time you start the board and battery is down.

If you aren't able to understand I am attaching a picture of the circuit.

VBAT circuit.jpg

1,384 Views
priyankb
Contributor III

Kevin

Mark suggested a great option.

Another one could be that you connect VDD and battery to Schottky  Diode. The output of diode to VBAT. This way even if your battery is down you will get voltage from Vdd. In this way there is never a problem of hard fault.

Detection: You can detect the battery(down) by reading RTC register. It will always start from 0 every time you start the board and battery is down.

0 Kudos
1,385 Views
mjbcswitzerland
Specialist V

Hi

A problem with a hard fault on attempted access to the RTC, even after its clock is unmasked in SCGC6, is likely to be a missing VBAT voltage; without the dedicated power supply the RTC is not powered and thus accesses hard-fault.

Regards

Mark

Kinetis: http://www.utasker.com/kinetis.html
Kinetis K64:
- http://www.utasker.com/kinetis/FRDM-K64F.html
- http://www.utasker.com/kinetis/TWR-K64F120M.html
- http://www.utasker.com/kinetis/TEENSY_3.5.html
- http://www.utasker.com/kinetis/Hexiwear-K64F.html
RTC and time keeping: http://www.utasker.com/docs/uTasker/uTasker_Time.pdf


Free Open Source solution: https://github.com/uTasker/uTasker-Kinetis
Working project in 15 minutes video: https://youtu.be/K8ScSgpgQ6M

Professional Kinetis support, one-on-one training and complete fast-track project solutions: http://www.utasker.com/support.html

1,384 Views
priyankb
Contributor III

Thank you Mark, you were correct.

VBAT wasn't getting adequate voltage. Changed some hardware, problem solved.

Thank you once again.

0 Kudos
1,384 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Priyank Bhatt,

   I suggest you try our official SDK RTC code for K64 at first, these code has been test, it works OK.

  Please download the KSDK code from this link:

Welcome | MCUXpresso SDK Builder 

Download the SDK_2.3_FRDM-K64F, then you can find the RTC project in this folder:

SDK_2.3_FRDM-K64F\boards\frdmk64f\driver_examples\rtc

If you still have problem after you test it, please kindly let me know!

Have a great day,
Kerry

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

1,384 Views
priyankb
Contributor III

Thank you Kerry Zhou for replying. Downloaded KSDK 2.3 but no luck.

I noticed one thing. Whenever I try to perform any operation (whether it is read or write) on RTC registers hard fault occurs. So basically clock enable using

SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;

doesn't give any error. But as soon as I try to read/write a register hard fault occur be it software reset  on RTC_CR ( RTC_CR |= RTC_CR_SWR_MASK; ) or trying to disable time counter in RTC_SR ( RTC_SR &= ~RTC_SR_TCE_MASK; ).

Now I have few doubts on hardware part:

1. Do I have to always keep the battery connected?

2. Do I need to use external capacitors and resistor while using 32.768KHz crystal oscillator?

One more thing, the default values of the registers associated to RTC are not same as described in the reference manual.

Thank you.

0 Kudos