mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 19:17:47 +00:00
17b5b6ec5b
Don't store addresses in the values of registers, this leads to problems with char arrays (among others). Instead, do it like it's done in C with raw addresses cast to struct pointers. This commit also splits gen-device.py, as AVR and ARM have very different ideas of what a register is. It's easier to just keep them separate.
50 lines
770 B
Go
50 lines
770 B
Go
// +build avr
|
|
|
|
package machine
|
|
|
|
import (
|
|
"device/avr"
|
|
)
|
|
|
|
type GPIOMode uint8
|
|
|
|
const (
|
|
GPIO_INPUT = iota
|
|
GPIO_OUTPUT
|
|
)
|
|
|
|
// LED on the Arduino
|
|
const LED = 13
|
|
|
|
func (p GPIO) Configure(config GPIOConfig) {
|
|
if config.Mode == GPIO_OUTPUT { // set output bit
|
|
if p.Pin < 8 {
|
|
*avr.DDRD |= 1 << p.Pin
|
|
} else {
|
|
*avr.DDRB |= 1 << (p.Pin - 8)
|
|
}
|
|
} else { // configure input: clear output bit
|
|
if p.Pin < 8 {
|
|
*avr.DDRD &^= 1 << p.Pin
|
|
} else {
|
|
*avr.DDRB &^= 1 << (p.Pin - 8)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p GPIO) Set(value bool) {
|
|
if value { // set bits
|
|
if p.Pin < 8 {
|
|
*avr.PORTD |= 1 << p.Pin
|
|
} else {
|
|
*avr.PORTB |= 1 << (p.Pin - 8)
|
|
}
|
|
} else { // clear bits
|
|
if p.Pin < 8 {
|
|
*avr.PORTB &^= 1 << p.Pin
|
|
} else {
|
|
*avr.PORTB &^= 1 << (p.Pin - 8)
|
|
}
|
|
}
|
|
}
|