@@ -201,8 +201,8 @@ func initRTC() {
201
201
}
202
202
203
203
func 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
+ }
206
206
}
207
207
208
208
// treat all ticks params coming from runtime as being in microseconds
@@ -232,9 +232,7 @@ func sleepTicks(d timeUnit) {
232
232
233
233
// ticks returns number of microseconds since start.
234
234
func ticks () timeUnit {
235
- // request read of count
236
- // sam.RTC_MODE0.READREQ.Set(sam.RTC_MODE0_READREQ_RREQ)
237
- // waitForSync()
235
+ waitForSync ()
238
236
239
237
rtcCounter := (uint64 (sam .RTC_MODE0 .COUNT .Get ()) * 305 ) / 10 // each counter tick == 30.5us
240
238
offset := (rtcCounter - timerLastCounter ) // change since last measurement
@@ -246,19 +244,21 @@ func ticks() timeUnit {
246
244
// ticks are in microseconds
247
245
func timerSleep (ticks uint32 ) {
248
246
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
252
253
}
253
254
254
255
// request read of count
255
- // sam.RTC_MODE0.READREQ.Set(sam.RTC_MODE0_READREQ_RREQ)
256
- // waitForSync()
256
+ waitForSync ()
257
257
258
258
// set compare value
259
259
cnt := sam .RTC_MODE0 .COUNT .Get ()
260
+
260
261
sam .RTC_MODE0 .COMP0 .Set (uint32 (cnt ) + (ticks * 10 / 305 )) // each counter tick == 30.5us
261
- waitForSync ()
262
262
263
263
// enable IRQ for CMP0 compare
264
264
sam .RTC_MODE0 .INTENSET .SetBits (sam .RTC_MODE0_INTENSET_CMP0 )
0 commit comments