mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 19:23:25 +00:00
Working on NXP/Teensy support
This commit is contained in:
@@ -7,19 +7,7 @@ import (
|
||||
"runtime/volatile"
|
||||
)
|
||||
|
||||
const (
|
||||
PortControlRegisterSRE = nxp.PORT_PCR0_SRE
|
||||
PortControlRegisterDSE = nxp.PORT_PCR0_DSE
|
||||
PortControlRegisterODE = nxp.PORT_PCR0_ODE
|
||||
)
|
||||
|
||||
func PortControlRegisterMUX(v uint8) uint32 {
|
||||
return (uint32(v) << nxp.PORT_PCR0_MUX_Pos) & nxp.PORT_PCR0_MUX_Msk
|
||||
}
|
||||
|
||||
type pinRegisters struct {
|
||||
Bit uintptr
|
||||
PCR *volatile.Register32
|
||||
type FastPin struct {
|
||||
PDOR *volatile.BitRegister
|
||||
PSOR *volatile.BitRegister
|
||||
PCOR *volatile.BitRegister
|
||||
@@ -28,6 +16,12 @@ type pinRegisters struct {
|
||||
PDDR *volatile.BitRegister
|
||||
}
|
||||
|
||||
type pin struct {
|
||||
Bit uint8
|
||||
PCR *volatile.Register32
|
||||
GPIO *nxp.GPIO_Type
|
||||
}
|
||||
|
||||
// Configure this pin with the given configuration.
|
||||
func (p Pin) Configure(config PinConfig) {
|
||||
switch config.Mode {
|
||||
@@ -35,22 +29,47 @@ func (p Pin) Configure(config PinConfig) {
|
||||
panic("todo")
|
||||
|
||||
case PinOutput:
|
||||
p.registers().PDDR.Set()
|
||||
p.registers().PCR.SetBits(PortControlRegisterSRE | PortControlRegisterDSE | PortControlRegisterMUX(1))
|
||||
p.registers().PCR.ClearBits(PortControlRegisterODE)
|
||||
r := p.reg()
|
||||
r.GPIO.PDDR.SetBits(1 << r.Bit)
|
||||
r.PCR.SetBits(nxp.PORT_PCR0_SRE | nxp.PORT_PCR0_DSE | nxp.PORT_PCR0_MUX(1))
|
||||
r.PCR.ClearBits(nxp.PORT_PCR0_ODE)
|
||||
}
|
||||
}
|
||||
|
||||
// Set changes the value of the GPIO pin. The pin must be configured as output.
|
||||
func (p Pin) Set(value bool) {
|
||||
r := p.reg()
|
||||
if value {
|
||||
p.registers().PSOR.Set()
|
||||
r.GPIO.PSOR.Set(1 << r.Bit)
|
||||
} else {
|
||||
p.registers().PCOR.Set()
|
||||
r.GPIO.PCOR.Set(1 << r.Bit)
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the current value of a GPIO pin.
|
||||
func (p Pin) Get() bool {
|
||||
return p.registers().PDIR.Get()
|
||||
r := p.reg()
|
||||
return r.GPIO.PDIR.HasBits(1 << r.Bit)
|
||||
}
|
||||
|
||||
func (p Pin) Control() *volatile.Register32 {
|
||||
return p.reg().PCR
|
||||
}
|
||||
|
||||
func (p Pin) Fast() FastPin {
|
||||
r := p.reg()
|
||||
return FastPin{
|
||||
PDOR: r.GPIO.PDOR.Bit(r.Bit),
|
||||
PSOR: r.GPIO.PSOR.Bit(r.Bit),
|
||||
PCOR: r.GPIO.PCOR.Bit(r.Bit),
|
||||
PTOR: r.GPIO.PTOR.Bit(r.Bit),
|
||||
PDIR: r.GPIO.PDIR.Bit(r.Bit),
|
||||
PDDR: r.GPIO.PDDR.Bit(r.Bit),
|
||||
}
|
||||
}
|
||||
|
||||
func (p FastPin) Set() { p.PSOR.Set(true) }
|
||||
func (p FastPin) Clear() { p.PCOR.Set(true) }
|
||||
func (p FastPin) Toggle() { p.PTOR.Set(true) }
|
||||
func (p FastPin) Write(v bool) { p.PDOR.Set(v) }
|
||||
func (p FastPin) Read() bool { return p.PDIR.Get() }
|
||||
|
||||
Reference in New Issue
Block a user