Shop OBEX P1 Docs P2 Docs Learn Events
GETSEC() is dope! — Parallax Forums

GETSEC() is dope!

Just used GETSEC() for the first time. I needed a 5 second timout for my DNS resolver. That's so sweet!

     ' Set timeout to 5 seconds
     l := GETSEC() + 5
     repeat until W6100.ReceiveData(W6100.IDM_BSR_Socket0_Register,1) == 0
      if GETSEC() >= l
         debug("DNS Server timed out.")
         return -1

I now have designs to couple GETSEC() with a NTP client to calculate and update system time.

Thanks for adding that in! Very convenient!

Comments

  • ElectrodudeElectrodude Posts: 1,657
    edited 2023-01-05 14:20

    @ke4pjw said:
    Just used GETSEC() for the first time. I needed a 5 second timout for my DNS resolver. That's so sweet!

         ' Set timeout to 5 seconds
         l := GETSEC() + 5
         repeat until W6100.ReceiveData(W6100.IDM_BSR_Socket0_Register,1) == 0
          if GETSEC() >= l
             debug("DNS Server timed out.")
             return -1
    

    I now have designs to couple GETSEC() with a NTP client to calculate and update system time.

    Thanks for adding that in! Very convenient!

    You could get a more precise timeout, and with slightly less computation, if you just did it the old-fashioned way:

         ' Set timeout to 5 seconds
         l := cnt + clkfreq*5
         repeat until W6100.ReceiveData(W6100.IDM_BSR_Socket0_Register,1) == 0
          if cnt - l > 0
             debug("DNS Server timed out.")
             return -1
    

    This is also guaranteed to never overflow - your way will hang for 2^32 seconds if called at just the wrong time (which happens only every 2^32 seconds, which is almost never, but still, why write slower, less robust code if you don't have to).

  • JonnyMacJonnyMac Posts: 9,102
    edited 2023-01-05 17:02

    I do the same thing with getms() -- this is my timeout code in a Ping))) object for the P2. The combination of smart pins and getms() and getsec() is kind of neat.

      timeout := getms() + 25                                       ' start time-out timing (25ms)
      pinstart(pin, P_HIGH_TICKS, 0, 0)                             ' measure return pulse (system ticks)
      repeat
        if (getms() > timeout)                                      ' if > 25ms, abort
          return 0
      until pinread(pin)                                            ' wait for end of pulse
    
  • So my thinking was, when GETSEC() overflows 32 bits, that will be about 136 years from P2 boot time. So I am not worried about it.

    cnt will roll over once every 15 seconds or so at 270MHz, right? So you have to take that into account, which you have in your code. It just takes more calculations. The same rollover could be applied to what I did, but none of us will be around to test it :)

    Thank you for sharing! I will implement your logic, for it to properly deal with the rollover at 136 years after boot time.

    I was mostly excited about the prospect of being able to back into the epoch of the boot time via a network clock and know the real time based on that. (And periodically adjusting for drift.) A one second counter is very handy, specifically for that.

Sign In or Register to comment.