avr: add support for the digispark

Blinking the on-board LED works. Nothing else has been tested yet.
This commit is contained in:
Ayke van Laethem
2018-11-20 18:34:14 +01:00
parent a96e2879b2
commit 9392ef900d
14 changed files with 421 additions and 291 deletions
+35
View File
@@ -0,0 +1,35 @@
// +build avr,atmega
package runtime
import (
"device/avr"
)
// Sleep for a given period. The period is defined by the WDT peripheral, and is
// on most chips (at least) 3 bits wide, in powers of two from 16ms to 2s
// (0=16ms, 1=32ms, 2=64ms...). Note that the WDT is not very accurate: it can
// be off by a large margin depending on temperature and supply voltage.
//
// TODO: disable more peripherals etc. to reduce sleep current.
func sleepWDT(period uint8) {
// Configure WDT
avr.Asm("cli")
avr.Asm("wdr")
// Start timed sequence.
*avr.WDTCSR |= avr.WDTCSR_WDCE | avr.WDTCSR_WDE
// Enable WDT and set new timeout
*avr.WDTCSR = avr.WDTCSR_WDIE | avr.RegValue(period)
avr.Asm("sei")
// Set sleep mode to idle and enable sleep mode.
// Note: when using something other than idle, the UART won't work
// correctly. This needs to be fixed, though, so we can truly sleep.
*avr.SMCR = (0 << 1) | avr.SMCR_SE
// go to sleep
avr.Asm("sleep")
// disable sleep
*avr.SMCR = 0
}
+16
View File
@@ -0,0 +1,16 @@
// +build avr,attiny
package runtime
import (
"device/avr"
)
func sleepWDT(period uint8) {
// TODO: use the watchdog timer instead of a busy loop.
for i := 0x45; i != 0; i-- {
for i := 0xff; i != 0; i-- {
avr.Asm("nop")
}
}
}
-28
View File
@@ -83,34 +83,6 @@ func sleepTicks(d timeUnit) {
}
}
// Sleep for a given period. The period is defined by the WDT peripheral, and is
// on most chips (at least) 3 bits wide, in powers of two from 16ms to 2s
// (0=16ms, 1=32ms, 2=64ms...). Note that the WDT is not very accurate: it can
// be off by a large margin depending on temperature and supply voltage.
//
// TODO: disable more peripherals etc. to reduce sleep current.
func sleepWDT(period uint8) {
// Configure WDT
avr.Asm("cli")
avr.Asm("wdr")
// Start timed sequence.
*avr.WDTCSR |= avr.WDTCSR_WDCE | avr.WDTCSR_WDE
// Enable WDT and set new timeout (0.5s)
*avr.WDTCSR = avr.WDTCSR_WDIE | avr.RegValue(period)
avr.Asm("sei")
// Set sleep mode to idle and enable sleep mode.
// Note: when using something other than idle, the UART won't work
// correctly. This needs to be fixed, though, so we can truly sleep.
*avr.SMCR = (0 << 1) | avr.SMCR_SE
// go to sleep
avr.Asm("sleep")
// disable sleep
*avr.SMCR = 0
}
func ticks() timeUnit {
return currentTime
}