mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 06:23:39 +00:00
maixbit: support both GPIO and GPIOHS controllers
This commit is contained in:
+92
-20
@@ -15,39 +15,111 @@ type PinMode uint8
|
||||
|
||||
const (
|
||||
PinInput PinMode = iota
|
||||
PinInputPullUp
|
||||
PinInputPullDown
|
||||
PinOutput
|
||||
PinPWM
|
||||
PinSPI
|
||||
PinI2C = PinSPI
|
||||
)
|
||||
|
||||
type fpioaPullMode uint8
|
||||
|
||||
const (
|
||||
fpioaPullNone fpioaPullMode = iota
|
||||
fpioaPullDown
|
||||
fpioaPullUp
|
||||
)
|
||||
|
||||
func (p Pin) fpioaSetIOPull(pull fpioaPullMode) {
|
||||
switch pull {
|
||||
case fpioaPullNone:
|
||||
kendryte.FPIOA.IO[uint8(p)].ClearBits(kendryte.FPIOA_IO_PU & kendryte.FPIOA_IO_PD)
|
||||
case fpioaPullUp:
|
||||
kendryte.FPIOA.IO[uint8(p)].SetBits(kendryte.FPIOA_IO_PU)
|
||||
kendryte.FPIOA.IO[uint8(p)].ClearBits(kendryte.FPIOA_IO_PD)
|
||||
case fpioaPullDown:
|
||||
kendryte.FPIOA.IO[uint8(p)].ClearBits(kendryte.FPIOA_IO_PU)
|
||||
kendryte.FPIOA.IO[uint8(p)].SetBits(kendryte.FPIOA_IO_PD)
|
||||
}
|
||||
}
|
||||
|
||||
// Configure this pin with the given configuration.
|
||||
func (p Pin) Configure(config PinConfig) {
|
||||
var input bool
|
||||
|
||||
kendryte.FPIOA.IO[uint8(p)].SetBits(kendryte.FPIOA_IO_OE_EN | kendryte.FPIOA_IO_IE_EN | kendryte.FPIOA_IO_ST | kendryte.FPIOA_IO_DS_Msk)
|
||||
switch config.Mode {
|
||||
case PinInput:
|
||||
p.fpioaSetIOPull(fpioaPullNone)
|
||||
input = true
|
||||
|
||||
case PinInputPullUp:
|
||||
p.fpioaSetIOPull(fpioaPullUp)
|
||||
input = true
|
||||
|
||||
case PinInputPullDown:
|
||||
p.fpioaSetIOPull(fpioaPullDown)
|
||||
input = true
|
||||
|
||||
case PinOutput:
|
||||
p.fpioaSetIOPull(fpioaPullNone)
|
||||
input = false
|
||||
|
||||
}
|
||||
|
||||
if p >= P08 && p <= P15 {
|
||||
// Converts the IO pin number in the effective GPIO number (assuming default FPIOA function).
|
||||
gpioPin := uint8(p) - 8
|
||||
|
||||
if input {
|
||||
kendryte.GPIO.DIRECTION.ClearBits(1 << gpioPin)
|
||||
} else {
|
||||
kendryte.GPIO.DIRECTION.SetBits(1 << gpioPin)
|
||||
}
|
||||
} else if p >= P16 && p <= P47 {
|
||||
// Converts the IO pin number in the effective GPIOHS number (assuming default FPIOA function).
|
||||
gpioPin := uint8(p) - 16
|
||||
|
||||
if input {
|
||||
kendryte.GPIOHS.INPUT_EN.SetBits(1 << gpioPin)
|
||||
kendryte.GPIOHS.OUTPUT_EN.ClearBits(1 << gpioPin)
|
||||
} else {
|
||||
kendryte.GPIOHS.OUTPUT_EN.SetBits(1 << gpioPin)
|
||||
kendryte.GPIOHS.INPUT_EN.ClearBits(1 << gpioPin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set the pin to high or low.
|
||||
func (p Pin) Set(high bool) {
|
||||
if p >= P08 && p <= P15 {
|
||||
gpioPin := uint8(p) - 8
|
||||
|
||||
if high {
|
||||
kendryte.GPIO.DATA_OUTPUT.SetBits(1 << gpioPin)
|
||||
} else {
|
||||
kendryte.GPIO.DATA_OUTPUT.ClearBits(1 << gpioPin)
|
||||
}
|
||||
} else if p >= P16 && p <= P47 {
|
||||
gpioPin := uint8(p) - 16
|
||||
|
||||
if high {
|
||||
kendryte.GPIOHS.OUTPUT_VAL.SetBits(1 << gpioPin)
|
||||
} else {
|
||||
kendryte.GPIOHS.OUTPUT_VAL.ClearBits(1 << gpioPin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the current value of a GPIO pin.
|
||||
func (p Pin) Get() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type FPIOA struct {
|
||||
Bus *kendryte.FPIOA_Type
|
||||
}
|
||||
|
||||
var (
|
||||
FPIOA0 = FPIOA{Bus: kendryte.FPIOA}
|
||||
)
|
||||
|
||||
func (fpioa FPIOA) Init() {
|
||||
// Enable APB0 clock.
|
||||
kendryte.SYSCTL.CLK_EN_CENT.SetBits(kendryte.SYSCTL_CLK_EN_CENT_APB0_CLK_EN)
|
||||
|
||||
// Enable FPIOA peripheral.
|
||||
kendryte.SYSCTL.CLK_EN_PERI.SetBits(kendryte.SYSCTL_CLK_EN_PERI_FPIOA_CLK_EN)
|
||||
var val uint32
|
||||
if p >= P08 && p <= P15 {
|
||||
gpioPin := uint8(p) - 8
|
||||
val = kendryte.GPIO.DATA_INPUT.Get() & (1 << gpioPin)
|
||||
} else if p >= P16 && p <= P47 {
|
||||
gpioPin := uint8(p) - 16
|
||||
val = kendryte.GPIOHS.INPUT_VAL.Get() & (1 << gpioPin)
|
||||
}
|
||||
return (val > 0)
|
||||
}
|
||||
|
||||
type UART struct {
|
||||
|
||||
Reference in New Issue
Block a user