@@ -201,8 +201,8 @@ func initRTC() {
201201}
202202
203203func waitForSync () {
204- // for sam.GCLK.STATUS .HasBits(sam.GCLK_STATUS_SYNCBUSY ) {
205- // }
204+ for sam .RTC_MODE0 . SYNCBUSY .HasBits (sam .RTC_MODE0_SYNCBUSY_COUNT ) {
205+ }
206206}
207207
208208// treat all ticks params coming from runtime as being in microseconds
@@ -232,9 +232,7 @@ func sleepTicks(d timeUnit) {
232232
233233// ticks returns number of microseconds since start.
234234func ticks () timeUnit {
235- // request read of count
236- // sam.RTC_MODE0.READREQ.Set(sam.RTC_MODE0_READREQ_RREQ)
237- // waitForSync()
235+ waitForSync ()
238236
239237 rtcCounter := (uint64 (sam .RTC_MODE0 .COUNT .Get ()) * 305 ) / 10 // each counter tick == 30.5us
240238 offset := (rtcCounter - timerLastCounter ) // change since last measurement
@@ -246,19 +244,21 @@ func ticks() timeUnit {
246244// ticks are in microseconds
247245func timerSleep (ticks uint32 ) {
248246 timerWakeup = false
249- if ticks < 30 {
250- // have to have at least one clock count
251- ticks = 30
247+ if ticks < 260 {
248+ // due to delay waiting for the register value to sync, the minimum sleep value
249+ // for the SAMD51 is 260us.
250+ // For related info for SAMD21, see:
251+ // https://community.atmel.com/comment/2507091#comment-2507091
252+ ticks = 260
252253 }
253254
254255 // request read of count
255- // sam.RTC_MODE0.READREQ.Set(sam.RTC_MODE0_READREQ_RREQ)
256- // waitForSync()
256+ waitForSync ()
257257
258258 // set compare value
259259 cnt := sam .RTC_MODE0 .COUNT .Get ()
260+
260261 sam .RTC_MODE0 .COMP0 .Set (uint32 (cnt ) + (ticks * 10 / 305 )) // each counter tick == 30.5us
261- waitForSync ()
262262
263263 // enable IRQ for CMP0 compare
264264 sam .RTC_MODE0 .INTENSET .SetBits (sam .RTC_MODE0_INTENSET_CMP0 )
0 commit comments