iwdg_init() and iwdg_feed()

Post here first, or if you can't find a relevant section!
smithy
Posts: 37
Joined: Sat Aug 13, 2016 7:02 pm
Location: Gießen, Germany

Re: iwdg_init() and iwdg_feed()

Post by smithy » Mon Aug 29, 2016 12:48 am

Since no one posted what came out of this i´m asking if there is a more precise way of using the WD timer ?

Code: Select all

#include <libmaple/iwdg.h>
#define iwdg_init_ms(N) iwdg_init(IWDG_PRE_256,((N)/5))

unsigned long temp;
void setup() {
  Serial.begin(19200);
  delay(4000); // allow a few secs to establish serial connection and open serial window
  Serial.println("Begin");
  //iwdg_init(IWDG_PRE_256, 1600); // init an 8 second wd timer by prescaling the ~50KHz clock and then calculating resulting ticks in 8 secs...
  iwdg_init_ms(200); // or init an 8s wd timer a bit more obviously using a wrapper
  temp = millis();
}

void loop() {
  Serial.println(millis()-temp);
  delay(10);
}
For 200ms i can measure around 245 ms.
Begin
0
13
24
35
46
57
68
79
90
101
113
125
137
149
161
173
185
197
209
221
233
245
For 8000 its 9800 or 10239 on my other test board.

User avatar
RogerClark
Posts: 6711
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: iwdg_init() and iwdg_feed()

Post by RogerClark » Mon Aug 29, 2016 1:21 am

The problem is that the watch dog clock is not crystal controlled , its an internal RC clock whose frequency will vary with temperature and can be different for different batches of chips.
The clock frequency is around 40 kHz (between 30 kHz and 60 kHz). For more details, refer to
the electrical characteristics section of the datasheets.

You would need to calibrate the WDT value based on the external crystal

To do this you would need to select the RTCCLOCK to run from the LSI, (as this is the same oscillator that the WDT uses).

Work out what speed its running at, and calculate the divider and preload values needed for the actual time you need.

You would need to re calibrate periodically, to ensure that value gets updated as the oscillator frequency changes as the device heats up (as it will when its in use)

A lot of systems do this not just for the WDT but often for the RTC.


I'm also a bit curious about why you need this to be so accurate, in my experience the WDT is not expected to be a precise timeout

smithy
Posts: 37
Joined: Sat Aug 13, 2016 7:02 pm
Location: Gießen, Germany

Re: iwdg_init() and iwdg_feed()

Post by smithy » Mon Aug 29, 2016 1:39 am

I need a watchdog because i´m developing an ebike computer which regulates throttle output. If something goes wrong i need to ensure that the throttle goes low. As long as its always longer than the period i set it will be ok i guess (depending on the spread). Since there are a lot of devices i interface with at the same time i don´t know yet how long a usual loop will take but the wdt should be as small as it can be (around 50-100ms)

User avatar
RogerClark
Posts: 6711
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: iwdg_init() and iwdg_feed()

Post by RogerClark » Mon Aug 29, 2016 2:02 am

Hi Smithy

Thats interesting, I'm building a eBike project as well. Mine doesn't effect the throttle so I hadn't considered using a WDT, but I think I will now implement it with WDT

Is yours for a Bosch eBike ?

Definitely just look at calibrating via the RTCCLOCK value, it should not be too hard to do.

smithy
Posts: 37
Joined: Sat Aug 13, 2016 7:02 pm
Location: Gießen, Germany

Re: iwdg_init() and iwdg_feed()

Post by smithy » Mon Aug 29, 2016 2:31 am

Its more for universal usage like the CA (http://www.ebikes.ca/product-info/cycle-analyst-3.html) did. Maybe it can work with bosh interfaces aswell don´t have one to test :( There are quite some controllers which support the CA plug right now i´m using it aswell. Some controllers can even vary the ampere drawn and so on, the possibilities are unlimited in such a project (dual drive etc.). Thats why we switched from the little nano (used 2 units before which were interfacing via i2c) to the stm32. Optionally we thought supporting the smartbms OZ890 for even more customizing on the fly :) I´ll pm you when i have a vid of my lcdmenu.

User avatar
RogerClark
Posts: 6711
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: iwdg_init() and iwdg_feed()

Post by RogerClark » Mon Aug 29, 2016 2:47 am

From what I've read the Bosch system is quite locked down.

I don't have an eBike, my dad as a Bocsh, hence why I have an interest.

The 8Fun system looks the best as its hackable via RS232 to reconfigure loads of settings ;-)

User avatar
GrumpyOldPizza
Posts: 181
Joined: Fri Apr 15, 2016 4:15 pm
Location: Denver, CO

Re: iwdg_init() and iwdg_feed()

Post by GrumpyOldPizza » Wed Feb 22, 2017 3:11 pm

Quick question. If LSI feeds IWDG, could IWDG be disabled by turning off LSI ? Do anybody try ?

bluesystems
Posts: 5
Joined: Sun Jan 29, 2017 3:48 pm

Re: iwdg_init() and iwdg_feed()

Post by bluesystems » Sun Mar 12, 2017 8:02 pm

I found this PDF file in ST micro's documents on the STM32 micro.
http://www.st.com/resource/en/product_t ... s_iwdg.pdf

This documents provides a good description of the STM32 hardware watchdog that iwdg is using.

The processor has a dedicated 32Khz clock for the watchdog hardware with a 8 bit pre-scaler and a 12 bit count down counter.
So with the 256 pre-scaler that would make each dog tick 32,000/256 = 125Hz or 8mS long.
Using 256 in the first field makes the second field 8mS ticks in decimal numbers.

The second field controls the STM32 count down counter that is 12 bits or 2^12 = 4096.
So the maximum watchdog timeout can be 4096 @ 8mS per tick making for 32.768 seconds as the longest time out possible.
I personally have not tried that long.

This example gives about a 16 second timeout and seems to work fine.
The first field is a 256 perscaller on the 32Khz clock and the second filed is then 2000 ticks at 8mS each.
iwdg_init(IWDG_PRE_256, 2000);

And remember once the dog is turned on there is no way to turn it off except for a power off reset.
So be careful to make sure you feed the dog in all your code once it is turned on or it will bite back!

Bob

bluesystems
Posts: 5
Joined: Sun Jan 29, 2017 3:48 pm

Re: iwdg_init() and iwdg_feed()

Post by bluesystems » Sun Mar 12, 2017 8:14 pm

I just noted the data sheet shows the watchdog timer between 30Khz to 60Khz with the typical of 40Khz.
Odd as the app note uses 32Khz in the examples.

So the 256 pre-scale typically gives 40,000/256 or 6.4mS ticks in the second field.
Can be as short as 60,000/256 or 4.2667mS per tick or a total time out of 17.476seconds

Bob

Post Reply