You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/07/27 16:57:40 UTC

[GitHub] utzig commented on a change in pull request #1299: [RFC] [NRF52] Remove uint64_t div in watchdog init

utzig commented on a change in pull request #1299: [RFC] [NRF52] Remove uint64_t div in watchdog init
URL: https://github.com/apache/mynewt-core/pull/1299#discussion_r205836933
 
 

 ##########
 File path: hw/mcu/nordic/nrf52xxx/src/hal_watchdog.c
 ##########
 @@ -44,13 +44,20 @@ nrf52_wdt_irq_handler(void)
 int
 hal_watchdog_init(uint32_t expire_msecs)
 {
-    uint64_t expiration;
-
     NRF_WDT->CONFIG = WDT_CONFIG_SLEEP_Msk;
 
-    /* Convert msec timeout to counts of a 32768 crystal */
-    expiration = ((uint64_t)expire_msecs * 32768) / 1000;
-    NRF_WDT->CRV = (uint32_t)expiration;
+    if (expire_msecs >= 1048576) {
+        /* watchdog with more than 17 minutes? */
+        assert(0);
+    } else {
+        uint32_t e = expire_msecs, rtc = 32768, d = 1000;
+        while (e >= 131072) {
+            e /= 2;
+            rtc /= 2;
+            d /= 2;
 
 Review comment:
   Cool!
   
   ```
   #include <stdint.h>
   #include <stdio.h>
   
   int main(void)
   {
       uint64_t r;
       uint32_t s;
   
       for (uint32_t u = 0; u < 0xffffffff; u++) {
           r = ((uint64_t)u * 32768) / 1000;
           s = (u * 32) + ((u * 96) / 125);
   
           if (r != s) {
               printf("u=%u, r=%lu, s=%u\n", u, r, s);
               break;
           }
       }
   
       return 0;
   }
   ```
   
   ```
   $ ./test
   u=44739243, r=1466015514, s=1431655776
   ```
   
   I guess you just made it work upto 12 hours, you're a genius @wes3!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services