RTC  - Page 1
Forum section: electronics / general

author: CLARAAA
author_rank: Contributor
timestamp: June 28, 2016, 06:20:16 pm
content: Hello, I would implement the RTC modules within a program would you code examples that do not use ASF ?

author: CLARAAA
author_rank: Contributor
timestamp: June 28, 2016, 06:26:26 pm
content: RTC, I have a function of initialization I would like code samples where the RTC module is used.

author: CLARAAA
author_rank: Contributor
timestamp: June 28, 2016, 06:38:57 pm
content: Examples of programming without using ASF me would be helpful

author: ataradov
author_rank: Super Contributor
timestamp: June 28, 2016, 11:52:19 pm
content: Quote from: CLARAAA on June 28, 2016, 06:33:43 pmYou may want to rephrase this. I don't understand what you want.You are showing us good pieces of code. Presumably if you wrote them, then you would know how to do the rest. If you did not write them, then where they are from?You may also want to separate tasks and solve them one by one. And WDT would be the last thing to implement.

author: Sal Ammoniac
author_rank: Super Contributor
timestamp: June 29, 2016, 12:37:04 am
content: http://www.ganssle.com/watchdogs.htm

author: MarkF
author_rank: Super Contributor
timestamp: June 29, 2016, 01:02:31 am
content: To implement a Watch Dog timer, I can think of two possibilities:1) Setup an interrupt (via an interrupt) with an integer variable that counts down to zero. When the variable reaches zero, reset the processor. Your main routine will need to periodically reset the variable to a value representing the Watch Dog delay before reset. The interrupt could also do your Real Time Clock.2) Setup up a timer to generate the delay before reset. When the timer rolls over, reset the processor. Again, your main routine would periodically reset the timer prevent the reset.I leave the details of implementing either option to you.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 01:04:15 am
content: There is a dedicated WD timer in Atmel SAM MCUs.OP needs to be clear what is needed exactly, I'm pretty sure there is no need for WDT, especially considering how basic other questions are.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 06:42:05 am
content: Quote from: CLARAAA on June 29, 2016, 06:35:04 amWhy? There is a dedicated watchdog timer that has nothing to do with RTC.You did not specify your device, but assuming you are using SAM D21, then WDT operation is described in chapter "17. WDT - Watchdog Timer"Its configuration is extremely simple, so it hard to screw up. Once you configure the timer, you just need to periodically write a proper value into CLEAR register, so WDT does not reset your MCU.EDIT: Actually your code with Config_WDT() and others already shows how to use WDT.

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 06:44:57 am
content: Did I think to something like this that my main :Code:Init_WDT ();( Config_WDT period= 1000 );uint32_t tp = time_RTC ;if ( tp > periode){reset_WDT ();RESET_SYSTEM ( ) ;}// Disable WDTWDT- > CTRL.reg = 0;// Wait synchronizationBut for my limited time I have to run my module RTC

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 06:51:14 am
content: Quote from: CLARAAA on June 29, 2016, 06:44:57 amThis code makes no sense and won't even compile.You need to call Config_WDT(1024) in the beginning of the program. This will configure WDT to reset the micro in 1024 ms (~1 second).Then you need to call Reset_WDT() at least once a second. Not calling this function will indicate that there is a bug and WDT will reset the micro.Quote from: CLARAAA on June 29, 2016, 06:44:57 amI don't understand what you mean here.Here is a simple example of using RTC:Code://-----------------------------------------------------------------------------void rtc_init(void){GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(8);GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_SRC(GCLK_SOURCE_OSC8M) |GCLK_GENCTRL_IDC | GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN;while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);// Configure RTCGCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(RTC_GCLK_ID) |GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0/*2*/);RTC->MODE1.CTRL.reg = RTC_MODE1_CTRL_MODE_COUNT16;while (RTC->MODE1.STATUS.bit.SYNCBUSY);// Prescaler needs to be enabled separately from the mode for some reasonRTC->MODE1.CTRL.reg |= RTC_MODE1_CTRL_PRESCALER_DIV8;while (RTC->MODE1.STATUS.bit.SYNCBUSY);RTC->MODE1.PER.reg = 999;while (RTC->MODE1.STATUS.bit.SYNCBUSY);RTC->MODE1.READREQ.reg |= RTC_READREQ_RCONT | RTC_READREQ_ADDR(0x10);RTC->MODE1.INTENSET.reg = RTC_MODE1_INTENSET_OVF;RTC->MODE1.CTRL.bit.ENABLE = 1;while (RTC->MODE1.STATUS.bit.SYNCBUSY);NVIC_EnableIRQ(RTC_IRQn);}//-----------------------------------------------------------------------------void irq_handler_rtc(void){HAL_GPIO_LED_toggle();time_ms += 1000;RTC->MODE1.INTFLAG.bit.OVF = 1;}You will need to rename irq_handler_rtc() to whatever it is called in your startup file.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 06:52:16 am
content: Quote from: CLARAAA on June 29, 2016, 06:50:42 amHow does your main() looks right now?

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 07:01:23 am
content: If I call my Conf_WDT () function early in the program with my period the watchdog is not supposed compared to the period fixed and realize the reset_WDT and reset_System? Maybe I Have to call these focntions in Config_WDT ()?

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 07:03:57 am
content: Code:Config_WDT(2048); // initialize and activate WDT with maximum perioduint32_t tps_since_act_WDT;if(tps_since_act_WDT <= 2048){Reset_WDT();Reset_System();}WDT->CTRL.reg = 0;// disable watchdogwhile (WDT->STATUS.bit.SYNCBUSY);//WAIT SYNCHRONIZATIONHere is my code in case of crash, but the problem is how to test it? This requires that I simulate a beugue

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 07:08:35 am
content: You don't need to call Reset_System(). This is just a convenience function in case you actually want to force MCU reset.WDT will reset MCU itself if you don't call Reset_WDT() often enough. That's how WDT works.

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 07:12:33 am
content: So, I have to do a loop that calls the reset_WDT in main?

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 07:14:19 am
content: Presumably you already have some sort of a loop, otherwise what is your program doing? Just add Reset_WDT() there.

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 07:17:13 am
content: So, I just call my function in my hand while the Init_WDT and Config_WDT but outside this while loopCode:void main(void){Init_WDT();Config_WDT(4096); // initialize and activate WDT with maximum periodwhile(){//dans la boucle while du mainReset_WDT();//Reset WDTWDT->CTRL.reg = 0;// disable watchdogwhile (WDT->STATUS.bit.SYNCBUSY);//WAIT SYNCHRONIZATION//Following programme}}

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 07:21:07 am
content: Quote from: CLARAAA on June 29, 2016, 07:17:13 amYou really need to put more effort into English.But if I understand correctly, then yes Config_WDT() must be called once outside of that loop. And Init_WDT() is already called by the Config_WDT().I don't get it, why do you bother with WDT. Is your main application logic already debugged and working?

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 07:53:29 am
content: Code:void main(void){Init_WDT();Config_WDT(4096); // initialize and activate WDT with maximum periodwhile(){//dans la boucle while du mainReset_WDT();//Reset WDTWDT->CTRL.reg = 0;// disable watchdogwhile (WDT->STATUS.bit.SYNCBUSY);//WAIT SYNCHRONIZATION//Following programme}}

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 07:58:44 am
content: Why do you disable WDT?Also, this program will not compile. Please check that your code at least compiles before posting.And also, waiting on synchronization can take a long time if your clock source for the WDT peripheral is low (it is typical to run WDT from internal 32 kHz oscillator). I would not call Reset_WDT() on every iteration, it can significantly slow down your system.

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 08:09:28 am
content: For setting up the RTC module such a way that every second a conversion on the ADC is done, I must Init_RTC () out of hand, but then how can I make him understand that ADC should take action every second

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 08:11:57 am
content: I'm sorry, I can't teach you how to do basic embedded development.I've already posted RTC example that will call periodic interrupt. It is actually set up to fire every second, just as you need. So start your ADC measurement in that interrupt handler.But you may want to start with simpler programs - blink an LED, read the button, etc.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 06:18:28 pm
content: I don't understand why you reset RTC every single iteration of the cycle. How do you expect any of this to work?Typically you set up all the registers in the beginning and then look at flags in the loop.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 06:30:58 pm
content: You are clearly not understanding what I'm saying.On each iteration of the while (1) loop you call Config_RTC(), which completely resets the state of the timer and then in the next statement you check if flag is set. It will never be set, since you just reset the peripheral.This is very basic programming, it does not even have anything to do with particular MCU type.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 06:43:00 pm
content: I'm not going to proof-read your code.Code:RTC->MODE0.READREQ.reg |= RTC_READREQ_RCONT;//Read continuouslyRTC_READREQ_ADDR(0x10);Here you need to replace first ';' with '|'. Not that this matters here. And I'm pretty sure compiler will issus a warning here. READ ALL COMPILER WARNINGS, especially when you don't know what you are doing.Why are you reading RTC->MODE0.INTENSET.reg and comparing it to 1? This register will reflect your settings. You need to analyze INTFLAG register and check for actual flags.Also, why do you set direction 10 times in a row?

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 06:53:36 pm
content: My COMPILER WARNINGS are only no previous prototype for my two functionsI modified byCode:if(RTC->MODE0.INTFLAG.reg==0)But I don't understand :Quote

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 06:57:23 pm
content: Quote from: CLARAAA on June 29, 2016, 06:53:36 pmIt should complain about statement with no effect on this line "RTC_READREQ_ADDR(0x10);". If it does not, then go into compiler setting and enable all the warnings you can.Quote from: CLARAAA on June 29, 2016, 06:53:36 pmOk, so why are you checking for 0?Quote from: CLARAAA on June 29, 2016, 06:53:36 pmWhat is this for?Code:for(int i=0; i<10;i++)PORT->Group.DIRSET.reg = PORT_PA09;

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 07:08:07 pm
content: The loop was kind of delay but I can take it off and put it this way

author: CLARAAA
author_rank: Contributor
timestamp: June 29, 2016, 07:10:26 pm
content: But the major problem is in the COnfig_RTC () function when I would do:Code:RTC->MODE0.READREQ.reg |= RTC_READREQ_RCONT|//Read continuouslyRTC_READREQ_ADDR(0x10);

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 08:34:13 pm
content: Quote from: CLARAAA on June 29, 2016, 07:10:26 pmWhat's the problem?Have you looked at my MCU starter projects -https://github.com/ataradov/mcu-starter-projects/tree/master/samd21? There is a working timer implementation. It is nt based on RTC, but I don't see why it would matter in this case.

author: MarkF
author_rank: Super Contributor
timestamp: June 29, 2016, 10:57:48 pm
content: I can't figure out what the program suppose to do.Maybe, the OP can describe what he's trying to do and the requirements for the program. (With OUT talking about WDT and RTC). A general description would really help.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 11:00:03 pm
content: He wants to read ADC sample every second based on a timer. At the moment he tries to make timer work. The next stage will be ADC.

author: MarkF
author_rank: Super Contributor
timestamp: June 29, 2016, 11:24:37 pm
content: So, all he needs to do is setup a loop in the main routine where he reads the ADC and then has a 1 sec delay at the end.If he really needs it to sample at exactly 1Hz, then he could setup a 1Hz timer interrupt that would sample the ADC and set a flag for an infinite loop in the main function where he can check the flag and do whatever processing needs done. He could read the ADC in the main function instead of the interrupt time in order to get into and out of the interrupt as fast as possible.

author: ataradov
author_rank: Super Contributor
timestamp: June 29, 2016, 11:26:23 pm
content: I agree, but it is hard when you have no  programming experience. And that's exactly what is going on in this thread.

author: MarkF
author_rank: Super Contributor
timestamp: June 29, 2016, 11:29:05 pm
content: Okay. Plus the language barrier. I have 35+ years of professional software development experience so this all seems trivial to me.

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 05:42:10 am
content: You still have not fixed RTC_READREQ_ADDR(0x10); thing. I'm not going to help you if you are not going to listen.I gave you a working code, use it.Also, start giving more information. If you say "stuck", tell us exactly where it is stuck.

author: CLARAAA
author_rank: Contributor
timestamp: June 30, 2016, 05:47:05 am
content: I don't understand because in the datasheet :ADDR is read-only constant of 0x10

author: CLARAAA
author_rank: Contributor
timestamp: June 30, 2016, 05:51:36 am
content: In your link it is a function with TC , not a module RTC

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 05:52:04 am
content: So what? Your C is wrong.You have two statements:Code:RTC->MODE0.READREQ.reg |= RTC_READREQ_RCONT;//Read continuouslyandCode:RTC_READREQ_ADDR(0x10);The first one is fine, the second one has no effect and compiler should complain about it. If it does not, you need to enable warnings. At this point compiler is smarter than you, so you should listen to it and fix any warnings it gives.It is a valid point that this field is constant fore this specific peripheral, on other peripherals it is not constant. So author of the original code (which you copy-pasted with errors) had intention to indicate that this filed is set to 0x10. This is done for code readability and maintainability.

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 05:55:49 am
content: Quote from: CLARAAA on June 30, 2016, 05:51:36 amI gave you code for RTC as well in this thread.Why do you need RTC? Is there a real reason for this?

author: CLARAAA
author_rank: Contributor
timestamp: June 30, 2016, 06:00:29 am
content: RTC->MODE0.READREQ.reg |= RTC_READREQ_RCONT; //Read continuouslyI tried to compile by removing this line of code, when I removed it remains blocked in the following line of code allows activate the RTC and if I let it rest stop on this lineCode:RTC->MODE0.READREQ.reg |= RTC_READREQ_RCONT;//Read continuouslyEven when I delete the statement with ADDR. I find it very strange

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 06:04:27 am
content: Quote from: CLARAAA on June 30, 2016, 06:00:29 amAgain, you are not making any sense.This entire read request thing is completely inconsequential to anything here. It is just a nice optimization.Quote from: CLARAAA on June 30, 2016, 06:00:29 amThe line with ADDR did absolutely nothing. Removing or adding it would not change anything.

author: CLARAAA
author_rank: Contributor
timestamp: June 30, 2016, 06:35:51 am
content: so I do not see why it remains locked in this function

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 06:37:28 am
content: Quote from: CLARAAA on June 30, 2016, 06:35:51 amToo bad you would not tell us where it is locked exactly.And since you won't reply why you need RTC and regular timer will not work, I'll let you deal with this on your own.

author: CLARAAA
author_rank: Contributor
timestamp: June 30, 2016, 06:57:26 am
content: it is locked exactly here:Code:RTC->MODE0.READREQ.reg |= RTC_READREQ_RCONT;//Read continuouslyI would like to use the RTC module to use another mode of this module. But first, I'd like to use the RTC in mode 0 .

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 07:00:52 am
content: There is nothing to lock in this instruction. Furthermore, you can completely remove it with no harm to actual operation of the module.How do you check that it is locked?

author: CLARAAA
author_rank: Contributor
timestamp: June 30, 2016, 07:33:28 am
content: At the launch of the step mode remains locked in this statement and if I delete it blocks the next line concerning the activation of the module RTC

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 07:37:59 am
content: Switch to disassembly view and step through individual instructions to find out what is happening exactly.

author: CLARAAA
author_rank: Contributor
timestamp: June 30, 2016, 07:41:57 am
content: Activation of the RTC module does not happen, but no indication is given me suddenly I do not know what to do

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 07:48:18 am
content: I told you exactly what to do - "Switch to disassembly view and step through individual instructions to find out what is happening exactly."This is very basic stuff. If you can't get it to work, then start with known working projects and modify them. There are a lot of thins than can go wrong, and step by step modifications will make it much easier to debug.

author: ataradov
author_rank: Super Contributor
timestamp: June 30, 2016, 03:40:29 pm
content: Quote from: CLARAAA on June 30, 2016, 08:57:10 amThat's because you are not doing what I tell you to do. The only way for you to achieve anything is stat with guaranteed working project and modify it bit by bit, checking that it still works.You are jumping right into an unfamiliar MCU and unfamiliar peripheral from scratch. Something, I personally won't do myself.