mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 14:33:41 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| df8add7b60 |
@@ -236,6 +236,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
make ASSERT=1
|
make ASSERT=1
|
||||||
echo "$(pwd)/build" >> $GITHUB_PATH
|
echo "$(pwd)/build" >> $GITHUB_PATH
|
||||||
|
- name: Test machine package
|
||||||
|
run: make check-machine
|
||||||
- name: Test stdlib packages
|
- name: Test stdlib packages
|
||||||
run: make tinygo-test
|
run: make tinygo-test
|
||||||
- name: Install Xtensa toolchain
|
- name: Install Xtensa toolchain
|
||||||
|
|||||||
@@ -267,6 +267,18 @@ tinygo:
|
|||||||
test: wasi-libc
|
test: wasi-libc
|
||||||
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -timeout=20m -buildmode exe -tags "byollvm osusergo" ./builder ./cgo ./compileopts ./compiler ./interp ./transform .
|
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -timeout=20m -buildmode exe -tags "byollvm osusergo" ./builder ./cgo ./compileopts ./compiler ./interp ./transform .
|
||||||
|
|
||||||
|
# Check whether the machine package matches the documentation.
|
||||||
|
# TODO: improve `tinygo targets` so it doesn't return these invalid targets.
|
||||||
|
CHECK_MACHINE_EXCULDE = \
|
||||||
|
cortex-m-qemu \
|
||||||
|
particle-3rd-gen \
|
||||||
|
riscv-qemu \
|
||||||
|
riscv64-qemu \
|
||||||
|
rp2040 \
|
||||||
|
$(nil)
|
||||||
|
check-machine:
|
||||||
|
$(GO) run ./tools/machinecheck $(filter-out $(CHECK_MACHINE_EXCULDE),$(shell $(TINYGO) targets))
|
||||||
|
|
||||||
# Standard library packages that pass tests on darwin, linux, wasi, and windows, but take over a minute in wasi
|
# Standard library packages that pass tests on darwin, linux, wasi, and windows, but take over a minute in wasi
|
||||||
TEST_PACKAGES_SLOW = \
|
TEST_PACKAGES_SLOW = \
|
||||||
compress/bzip2 \
|
compress/bzip2 \
|
||||||
|
|||||||
@@ -17,20 +17,20 @@ import (
|
|||||||
const deviceName = sam.Device
|
const deviceName = sam.Device
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
PinAnalog PinMode = 1
|
||||||
|
PinSERCOM PinMode = 2
|
||||||
|
PinSERCOMAlt PinMode = 3
|
||||||
|
PinTimer PinMode = 4
|
||||||
|
PinTimerAlt PinMode = 5
|
||||||
|
PinCom PinMode = 6
|
||||||
|
//PinAC_CLK PinMode = 7
|
||||||
|
PinDigital PinMode = 8
|
||||||
PinInput PinMode = 9
|
PinInput PinMode = 9
|
||||||
PinInputPullup PinMode = 10
|
PinInputPullup PinMode = 10
|
||||||
PinOutput PinMode = 11
|
PinOutput PinMode = 11
|
||||||
|
PinTCC PinMode = PinTimer
|
||||||
|
PinTCCAlt PinMode = PinTimerAlt
|
||||||
PinInputPulldown PinMode = 12
|
PinInputPulldown PinMode = 12
|
||||||
|
|
||||||
// internal pin modes
|
|
||||||
pinAnalog PinMode = 1
|
|
||||||
pinSERCOM PinMode = 2
|
|
||||||
pinSERCOMAlt PinMode = 3
|
|
||||||
pinTCC PinMode = 4
|
|
||||||
pinTCCAlt PinMode = 5
|
|
||||||
pinCom PinMode = 6
|
|
||||||
//PinAC_CLK PinMode = 7
|
|
||||||
pinDigital PinMode = 8
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type PinChange uint8
|
type PinChange uint8
|
||||||
@@ -149,7 +149,7 @@ func findPinPadMapping(sercom uint8, pin Pin) (pinMode PinMode, pad uint32, ok b
|
|||||||
if upper != 0 {
|
if upper != 0 {
|
||||||
// SERCOM
|
// SERCOM
|
||||||
if (upper>>1)-1 == sercom {
|
if (upper>>1)-1 == sercom {
|
||||||
pinMode = pinSERCOM
|
pinMode = PinSERCOM
|
||||||
pad |= uint32((upper & 1) << 1)
|
pad |= uint32((upper & 1) << 1)
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
@@ -157,7 +157,7 @@ func findPinPadMapping(sercom uint8, pin Pin) (pinMode PinMode, pad uint32, ok b
|
|||||||
if lower != 0 {
|
if lower != 0 {
|
||||||
// SERCOM-ALT
|
// SERCOM-ALT
|
||||||
if (lower>>1)-1 == sercom {
|
if (lower>>1)-1 == sercom {
|
||||||
pinMode = pinSERCOMAlt
|
pinMode = PinSERCOMAlt
|
||||||
pad |= uint32((lower & 1) << 1)
|
pad |= uint32((lower & 1) << 1)
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
@@ -381,7 +381,7 @@ func (a ADC) Configure(config ADCConfig) {
|
|||||||
// 1/2 VDDANA = 0.5 * 3V3 = 1.65V
|
// 1/2 VDDANA = 0.5 * 3V3 = 1.65V
|
||||||
sam.ADC.REFCTRL.SetBits(sam.ADC_REFCTRL_REFSEL_INTVCC1 << sam.ADC_REFCTRL_REFSEL_Pos)
|
sam.ADC.REFCTRL.SetBits(sam.ADC_REFCTRL_REFSEL_INTVCC1 << sam.ADC_REFCTRL_REFSEL_Pos)
|
||||||
|
|
||||||
a.Pin.Configure(PinConfig{Mode: pinAnalog})
|
a.Pin.Configure(PinConfig{Mode: PinAnalog})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1000,11 +1000,11 @@ func (i2s I2S) Configure(config I2SConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// configure pin for clock
|
// configure pin for clock
|
||||||
config.SCK.Configure(PinConfig{Mode: pinCom})
|
config.SCK.Configure(PinConfig{Mode: PinCom})
|
||||||
|
|
||||||
// configure pin for WS, if needed
|
// configure pin for WS, if needed
|
||||||
if config.WS != NoPin {
|
if config.WS != NoPin {
|
||||||
config.WS.Configure(PinConfig{Mode: pinCom})
|
config.WS.Configure(PinConfig{Mode: PinCom})
|
||||||
}
|
}
|
||||||
|
|
||||||
// now set serializer data size.
|
// now set serializer data size.
|
||||||
@@ -1043,7 +1043,7 @@ func (i2s I2S) Configure(config I2SConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// configure data pin
|
// configure data pin
|
||||||
config.SD.Configure(PinConfig{Mode: pinCom})
|
config.SD.Configure(PinConfig{Mode: PinCom})
|
||||||
|
|
||||||
// re-enable
|
// re-enable
|
||||||
i2s.Bus.CTRLA.SetBits(sam.I2S_CTRLA_ENABLE)
|
i2s.Bus.CTRLA.SetBits(sam.I2S_CTRLA_ENABLE)
|
||||||
@@ -1636,12 +1636,12 @@ func findPinTimerMapping(timer uint8, pin Pin) (PinMode, uint8) {
|
|||||||
if mapping&0x07 == timer+1 {
|
if mapping&0x07 == timer+1 {
|
||||||
// PWM output is on peripheral function E.
|
// PWM output is on peripheral function E.
|
||||||
evenChannel := ((mapping >> 3) & 1) * 2
|
evenChannel := ((mapping >> 3) & 1) * 2
|
||||||
return pinTCC, evenChannel + uint8(pin&1)
|
return PinTCC, evenChannel + uint8(pin&1)
|
||||||
}
|
}
|
||||||
if (mapping&0x70)>>4 == timer+1 {
|
if (mapping&0x70)>>4 == timer+1 {
|
||||||
// PWM output is on peripheral function F.
|
// PWM output is on peripheral function F.
|
||||||
evenChannel := ((mapping >> 7) & 1) * 2
|
evenChannel := ((mapping >> 7) & 1) * 2
|
||||||
return pinTCCAlt, evenChannel + uint8(pin&1)
|
return PinTCCAlt, evenChannel + uint8(pin&1)
|
||||||
}
|
}
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ func (dev *USBDevice) Configure(config UARTConfig) {
|
|||||||
sam.USB_DEVICE.DESCADD.Set(uint32(uintptr(unsafe.Pointer(&usbEndpointDescriptors))))
|
sam.USB_DEVICE.DESCADD.Set(uint32(uintptr(unsafe.Pointer(&usbEndpointDescriptors))))
|
||||||
|
|
||||||
// configure pins
|
// configure pins
|
||||||
USBCDC_DM_PIN.Configure(PinConfig{Mode: pinCom})
|
USBCDC_DM_PIN.Configure(PinConfig{Mode: PinCom})
|
||||||
USBCDC_DP_PIN.Configure(PinConfig{Mode: pinCom})
|
USBCDC_DP_PIN.Configure(PinConfig{Mode: PinCom})
|
||||||
|
|
||||||
// performs pad calibration from store fuses
|
// performs pad calibration from store fuses
|
||||||
handlePadCalibration()
|
handlePadCalibration()
|
||||||
|
|||||||
@@ -86,53 +86,53 @@ func (p Pin) Configure(config PinConfig) {
|
|||||||
sam.PORT.OUTSET0.Set(1 << uint8(p))
|
sam.PORT.OUTSET0.Set(1 << uint8(p))
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
|
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
|
||||||
|
|
||||||
case pinSERCOM:
|
case PinSERCOM:
|
||||||
if uint8(p)&1 > 0 {
|
if uint8(p)&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOM) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOM) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN)
|
||||||
|
|
||||||
case pinSERCOMAlt:
|
case PinSERCOMAlt:
|
||||||
if uint8(p)&1 > 0 {
|
if uint8(p)&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOMAlt) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOMAlt) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
||||||
|
|
||||||
case pinCom:
|
case PinCom:
|
||||||
if uint8(p)&1 > 0 {
|
if uint8(p)&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinCom) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinCom) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN)
|
||||||
case pinAnalog:
|
case PinAnalog:
|
||||||
if uint8(p)&1 > 0 {
|
if uint8(p)&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinAnalog) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinAnalog) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
||||||
|
|||||||
@@ -137,53 +137,53 @@ func (p Pin) Configure(config PinConfig) {
|
|||||||
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
|
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
|
||||||
}
|
}
|
||||||
|
|
||||||
case pinSERCOM:
|
case PinSERCOM:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOM) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOM) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN)
|
||||||
|
|
||||||
case pinSERCOMAlt:
|
case PinSERCOMAlt:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOMAlt) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOMAlt) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
||||||
|
|
||||||
case pinCom:
|
case PinCom:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinCom) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinCom) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN)
|
||||||
case pinAnalog:
|
case PinAnalog:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinAnalog) << sam.PORT_PMUX0_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinAnalog) << sam.PORT_PMUX0_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
|
||||||
|
|||||||
@@ -21,29 +21,30 @@ func CPUFrequency() uint32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
PinAnalog PinMode = 1
|
||||||
|
PinSERCOM PinMode = 2
|
||||||
|
PinSERCOMAlt PinMode = 3
|
||||||
|
PinTimer PinMode = 4
|
||||||
|
PinTimerAlt PinMode = 5
|
||||||
|
PinTCCPDEC PinMode = 6
|
||||||
|
PinCom PinMode = 7
|
||||||
|
PinSDHC PinMode = 8
|
||||||
|
PinI2S PinMode = 9
|
||||||
|
PinPCC PinMode = 10
|
||||||
|
PinGMAC PinMode = 11
|
||||||
|
PinACCLK PinMode = 12
|
||||||
|
PinCCL PinMode = 13
|
||||||
|
PinDigital PinMode = 14
|
||||||
PinInput PinMode = 15
|
PinInput PinMode = 15
|
||||||
PinInputPullup PinMode = 16
|
PinInputPullup PinMode = 16
|
||||||
PinOutput PinMode = 17
|
PinOutput PinMode = 17
|
||||||
|
PinTCCE PinMode = PinTimer
|
||||||
|
PinTCCF PinMode = PinTimerAlt
|
||||||
|
PinTCCG PinMode = PinTCCPDEC
|
||||||
PinInputPulldown PinMode = 18
|
PinInputPulldown PinMode = 18
|
||||||
|
PinCAN PinMode = 19
|
||||||
// internal pin modes
|
PinCAN0 PinMode = PinSDHC
|
||||||
pinAnalog PinMode = 1
|
PinCAN1 PinMode = PinCom
|
||||||
pinSERCOM PinMode = 2
|
|
||||||
pinSERCOMAlt PinMode = 3
|
|
||||||
pinTCCE PinMode = 4
|
|
||||||
pinTCCF PinMode = 5
|
|
||||||
pinTCCG PinMode = 6
|
|
||||||
pinCom PinMode = 7
|
|
||||||
pinSDHC PinMode = 8
|
|
||||||
pinI2S PinMode = 9
|
|
||||||
pinPCC PinMode = 10
|
|
||||||
pinGMAC PinMode = 11
|
|
||||||
pinACCLK PinMode = 12
|
|
||||||
pinCCL PinMode = 13
|
|
||||||
pinDigital PinMode = 14
|
|
||||||
pinCAN PinMode = 19
|
|
||||||
pinCAN0 PinMode = pinSDHC
|
|
||||||
pinCAN1 PinMode = pinCom
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type PinChange uint8
|
type PinChange uint8
|
||||||
@@ -337,7 +338,7 @@ func findPinPadMapping(sercom uint8, pin Pin) (pinMode PinMode, pad uint32, ok b
|
|||||||
if upper != 0 {
|
if upper != 0 {
|
||||||
// SERCOM
|
// SERCOM
|
||||||
if (upper>>4)-1 == sercom {
|
if (upper>>4)-1 == sercom {
|
||||||
pinMode = pinSERCOM
|
pinMode = PinSERCOM
|
||||||
pad |= uint32(upper % 4)
|
pad |= uint32(upper % 4)
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
@@ -345,7 +346,7 @@ func findPinPadMapping(sercom uint8, pin Pin) (pinMode PinMode, pad uint32, ok b
|
|||||||
if lower != 0 {
|
if lower != 0 {
|
||||||
// SERCOM-ALT
|
// SERCOM-ALT
|
||||||
if (lower>>4)-1 == sercom {
|
if (lower>>4)-1 == sercom {
|
||||||
pinMode = pinSERCOMAlt
|
pinMode = PinSERCOMAlt
|
||||||
pad |= uint32(lower % 4)
|
pad |= uint32(lower % 4)
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
@@ -579,65 +580,65 @@ func (p Pin) Configure(config PinConfig) {
|
|||||||
sam.PORT.GROUP[group].OUTSET.Set(1 << pin_in_group)
|
sam.PORT.GROUP[group].OUTSET.Set(1 << pin_in_group)
|
||||||
p.setPinCfg(sam.PORT_GROUP_PINCFG_INEN | sam.PORT_GROUP_PINCFG_PULLEN)
|
p.setPinCfg(sam.PORT_GROUP_PINCFG_INEN | sam.PORT_GROUP_PINCFG_PULLEN)
|
||||||
|
|
||||||
case pinSERCOM:
|
case PinSERCOM:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOM) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOM) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN | sam.PORT_GROUP_PINCFG_DRVSTR | sam.PORT_GROUP_PINCFG_INEN)
|
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN | sam.PORT_GROUP_PINCFG_DRVSTR | sam.PORT_GROUP_PINCFG_INEN)
|
||||||
|
|
||||||
case pinSERCOMAlt:
|
case PinSERCOMAlt:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOMAlt) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinSERCOMAlt) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN | sam.PORT_GROUP_PINCFG_DRVSTR)
|
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN | sam.PORT_GROUP_PINCFG_DRVSTR)
|
||||||
|
|
||||||
case pinCom:
|
case PinCom:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinCom) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinCom) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinCom) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinCom) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN)
|
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN)
|
||||||
case pinAnalog:
|
case PinAnalog:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinAnalog) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinAnalog) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinAnalog) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinAnalog) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN | sam.PORT_GROUP_PINCFG_DRVSTR)
|
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN | sam.PORT_GROUP_PINCFG_DRVSTR)
|
||||||
case pinSDHC:
|
case PinSDHC:
|
||||||
if p&1 > 0 {
|
if p&1 > 0 {
|
||||||
// odd pin, so save the even pins
|
// odd pin, so save the even pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXE_Msk
|
||||||
p.setPMux(val | (uint8(pinSDHC) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
p.setPMux(val | (uint8(PinSDHC) << sam.PORT_GROUP_PMUX_PMUXO_Pos))
|
||||||
} else {
|
} else {
|
||||||
// even pin, so save the odd pins
|
// even pin, so save the odd pins
|
||||||
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
val := p.getPMux() & sam.PORT_GROUP_PMUX_PMUXO_Msk
|
||||||
p.setPMux(val | (uint8(pinSDHC) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
p.setPMux(val | (uint8(PinSDHC) << sam.PORT_GROUP_PMUX_PMUXE_Pos))
|
||||||
}
|
}
|
||||||
// enable port config
|
// enable port config
|
||||||
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN)
|
p.setPinCfg(sam.PORT_GROUP_PINCFG_PMUXEN)
|
||||||
@@ -813,7 +814,7 @@ func (a ADC) Configure(config ADCConfig) {
|
|||||||
adc.REFCTRL.SetBits(sam.ADC_REFCTRL_REFSEL_INTVCC1)
|
adc.REFCTRL.SetBits(sam.ADC_REFCTRL_REFSEL_INTVCC1)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.Pin.Configure(PinConfig{Mode: pinAnalog})
|
a.Pin.Configure(PinConfig{Mode: PinAnalog})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the current value of a ADC pin, in the range 0..0xffff.
|
// Get returns the current value of a ADC pin, in the range 0..0xffff.
|
||||||
@@ -1866,7 +1867,7 @@ var pinTimerMapping = [...]struct{ F, G uint8 }{
|
|||||||
PB02 / 2: {pinTCC2_2, 0},
|
PB02 / 2: {pinTCC2_2, 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
// findPinPadMapping returns the pin mode (pinTCCF or pinTCCG) and the channel
|
// findPinPadMapping returns the pin mode (PinTCCF or PinTCCG) and the channel
|
||||||
// number for a given timer and pin. A zero PinMode is returned if no mapping
|
// number for a given timer and pin. A zero PinMode is returned if no mapping
|
||||||
// could be found.
|
// could be found.
|
||||||
func findPinTimerMapping(timer uint8, pin Pin) (PinMode, uint8) {
|
func findPinTimerMapping(timer uint8, pin Pin) (PinMode, uint8) {
|
||||||
@@ -1878,12 +1879,12 @@ func findPinTimerMapping(timer uint8, pin Pin) (PinMode, uint8) {
|
|||||||
|
|
||||||
// Check for column F in the datasheet.
|
// Check for column F in the datasheet.
|
||||||
if mapping.F>>4-1 == timer {
|
if mapping.F>>4-1 == timer {
|
||||||
return pinTCCF, mapping.F&0x0f + uint8(pin)&1
|
return PinTCCF, mapping.F&0x0f + uint8(pin)&1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for column G in the datasheet.
|
// Check for column G in the datasheet.
|
||||||
if mapping.G>>4-1 == timer {
|
if mapping.G>>4-1 == timer {
|
||||||
return pinTCCG, mapping.G&0x0f + uint8(pin)&1
|
return PinTCCG, mapping.G&0x0f + uint8(pin)&1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nothing found.
|
// Nothing found.
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ func (dev *USBDevice) Configure(config UARTConfig) {
|
|||||||
sam.USB_DEVICE.DESCADD.Set(uint32(uintptr(unsafe.Pointer(&usbEndpointDescriptors))))
|
sam.USB_DEVICE.DESCADD.Set(uint32(uintptr(unsafe.Pointer(&usbEndpointDescriptors))))
|
||||||
|
|
||||||
// configure pins
|
// configure pins
|
||||||
USBCDC_DM_PIN.Configure(PinConfig{Mode: pinCom})
|
USBCDC_DM_PIN.Configure(PinConfig{Mode: PinCom})
|
||||||
USBCDC_DP_PIN.Configure(PinConfig{Mode: pinCom})
|
USBCDC_DP_PIN.Configure(PinConfig{Mode: PinCom})
|
||||||
|
|
||||||
// performs pad calibration from store fuses
|
// performs pad calibration from store fuses
|
||||||
handlePadCalibration()
|
handlePadCalibration()
|
||||||
|
|||||||
@@ -68,9 +68,9 @@ func (can *CAN) Configure(config CANConfig) error {
|
|||||||
config.Standby.Low()
|
config.Standby.Low()
|
||||||
}
|
}
|
||||||
|
|
||||||
mode := pinCAN0
|
mode := PinCAN0
|
||||||
if can.instance() == 1 {
|
if can.instance() == 1 {
|
||||||
mode = pinCAN1
|
mode = PinCAN1
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Rx.Configure(PinConfig{Mode: mode})
|
config.Rx.Configure(PinConfig{Mode: mode})
|
||||||
|
|||||||
@@ -18,11 +18,9 @@ func CPUFrequency() uint32 {
|
|||||||
const (
|
const (
|
||||||
PinInput PinMode = iota
|
PinInput PinMode = iota
|
||||||
PinOutput
|
PinOutput
|
||||||
|
PinPWM
|
||||||
// internal pin modes
|
PinSPI
|
||||||
pinPWM
|
PinI2C = PinSPI
|
||||||
pinSPI
|
|
||||||
pinI2C = pinSPI
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Configure this pin with the given configuration.
|
// Configure this pin with the given configuration.
|
||||||
@@ -31,10 +29,10 @@ func (p Pin) Configure(config PinConfig) {
|
|||||||
switch config.Mode {
|
switch config.Mode {
|
||||||
case PinOutput:
|
case PinOutput:
|
||||||
sifive.GPIO0.OUTPUT_EN.SetBits(1 << uint8(p))
|
sifive.GPIO0.OUTPUT_EN.SetBits(1 << uint8(p))
|
||||||
case pinPWM:
|
case PinPWM:
|
||||||
sifive.GPIO0.IOF_EN.SetBits(1 << uint8(p))
|
sifive.GPIO0.IOF_EN.SetBits(1 << uint8(p))
|
||||||
sifive.GPIO0.IOF_SEL.SetBits(1 << uint8(p))
|
sifive.GPIO0.IOF_SEL.SetBits(1 << uint8(p))
|
||||||
case pinSPI:
|
case PinSPI:
|
||||||
sifive.GPIO0.IOF_EN.SetBits(1 << uint8(p))
|
sifive.GPIO0.IOF_EN.SetBits(1 << uint8(p))
|
||||||
sifive.GPIO0.IOF_SEL.ClearBits(1 << uint8(p))
|
sifive.GPIO0.IOF_SEL.ClearBits(1 << uint8(p))
|
||||||
}
|
}
|
||||||
@@ -147,9 +145,9 @@ func (spi SPI) Configure(config SPIConfig) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// enable pins for SPI
|
// enable pins for SPI
|
||||||
config.SCK.Configure(PinConfig{Mode: pinSPI})
|
config.SCK.Configure(PinConfig{Mode: PinSPI})
|
||||||
config.SDO.Configure(PinConfig{Mode: pinSPI})
|
config.SDO.Configure(PinConfig{Mode: PinSPI})
|
||||||
config.SDI.Configure(PinConfig{Mode: pinSPI})
|
config.SDI.Configure(PinConfig{Mode: PinSPI})
|
||||||
|
|
||||||
// set default frequency
|
// set default frequency
|
||||||
if config.Frequency == 0 {
|
if config.Frequency == 0 {
|
||||||
@@ -253,8 +251,8 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
|||||||
// enable controller
|
// enable controller
|
||||||
i2c.Bus.CTR.SetBits(sifive.I2C_CTR_EN)
|
i2c.Bus.CTR.SetBits(sifive.I2C_CTR_EN)
|
||||||
|
|
||||||
config.SDA.Configure(PinConfig{Mode: pinI2C})
|
config.SDA.Configure(PinConfig{Mode: PinI2C})
|
||||||
config.SCL.Configure(PinConfig{Mode: pinI2C})
|
config.SCL.Configure(PinConfig{Mode: PinI2C})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,21 +28,21 @@ const (
|
|||||||
PinDisable
|
PinDisable
|
||||||
|
|
||||||
// ADC
|
// ADC
|
||||||
pinInputAnalog
|
PinInputAnalog
|
||||||
|
|
||||||
// UART
|
// UART
|
||||||
pinModeUARTTX
|
PinModeUARTTX
|
||||||
pinModeUARTRX
|
PinModeUARTRX
|
||||||
|
|
||||||
// SPI
|
// SPI
|
||||||
pinModeSPISDI
|
PinModeSPISDI
|
||||||
pinModeSPISDO
|
PinModeSPISDO
|
||||||
pinModeSPICLK
|
PinModeSPICLK
|
||||||
pinModeSPICS
|
PinModeSPICS
|
||||||
|
|
||||||
// I2C
|
// I2C
|
||||||
pinModeI2CSDA
|
PinModeI2CSDA
|
||||||
pinModeI2CSCL
|
PinModeI2CSCL
|
||||||
)
|
)
|
||||||
|
|
||||||
// Deprecated: use PinInputPullup and PinInputPulldown instead.
|
// Deprecated: use PinInputPullup and PinInputPulldown instead.
|
||||||
@@ -283,29 +283,29 @@ func (p Pin) Configure(config PinConfig) {
|
|||||||
gpio.GDIR.ClearBits(p.getMask())
|
gpio.GDIR.ClearBits(p.getMask())
|
||||||
pad.Set(dse(7) | hys)
|
pad.Set(dse(7) | hys)
|
||||||
|
|
||||||
case pinInputAnalog:
|
case PinInputAnalog:
|
||||||
gpio.GDIR.ClearBits(p.getMask())
|
gpio.GDIR.ClearBits(p.getMask())
|
||||||
pad.Set(dse(7))
|
pad.Set(dse(7))
|
||||||
|
|
||||||
case pinModeUARTTX:
|
case PinModeUARTTX:
|
||||||
pad.Set(sre | dse(3) | spd(3))
|
pad.Set(sre | dse(3) | spd(3))
|
||||||
|
|
||||||
case pinModeUARTRX:
|
case PinModeUARTRX:
|
||||||
pad.Set(dse(7) | pke | pue | pup(3) | hys)
|
pad.Set(dse(7) | pke | pue | pup(3) | hys)
|
||||||
|
|
||||||
case pinModeSPISDI:
|
case PinModeSPISDI:
|
||||||
pad.Set(dse(7) | spd(2))
|
pad.Set(dse(7) | spd(2))
|
||||||
|
|
||||||
case pinModeSPISDO:
|
case PinModeSPISDO:
|
||||||
pad.Set(dse(7) | spd(2))
|
pad.Set(dse(7) | spd(2))
|
||||||
|
|
||||||
case pinModeSPICLK:
|
case PinModeSPICLK:
|
||||||
pad.Set(dse(7) | spd(2))
|
pad.Set(dse(7) | spd(2))
|
||||||
|
|
||||||
case pinModeSPICS:
|
case PinModeSPICS:
|
||||||
pad.Set(dse(7))
|
pad.Set(dse(7))
|
||||||
|
|
||||||
case pinModeI2CSDA, pinModeI2CSCL:
|
case PinModeI2CSDA, PinModeI2CSCL:
|
||||||
pad.Set(ode | sre | dse(4) | spd(1) | pke | pue | pup(3))
|
pad.Set(ode | sre | dse(4) | spd(1) | pke | pue | pup(3))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -759,7 +759,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// ADC
|
// ADC
|
||||||
case pinInputAnalog:
|
case PinInputAnalog:
|
||||||
mode := uint32(0x5) // use alternate function 5 (GPIO)
|
mode := uint32(0x5) // use alternate function 5 (GPIO)
|
||||||
if forcePath {
|
if forcePath {
|
||||||
mode |= 0x10 // SION bit
|
mode |= 0x10 // SION bit
|
||||||
@@ -767,7 +767,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// UART RX/TX
|
// UART RX/TX
|
||||||
case pinModeUARTRX, pinModeUARTTX:
|
case PinModeUARTRX, PinModeUARTTX:
|
||||||
mode := uint32(0x2) // UART is usually alternate function 2 on Teensy 4.x
|
mode := uint32(0x2) // UART is usually alternate function 2 on Teensy 4.x
|
||||||
// Teensy 4.1 has a UART (LPUART5) with alternate function 1
|
// Teensy 4.1 has a UART (LPUART5) with alternate function 1
|
||||||
if p == PB28 || p == PB29 {
|
if p == PB28 || p == PB29 {
|
||||||
@@ -776,7 +776,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// SPI SDI
|
// SPI SDI
|
||||||
case pinModeSPISDI:
|
case PinModeSPISDI:
|
||||||
var mode uint32
|
var mode uint32
|
||||||
switch p {
|
switch p {
|
||||||
case PC15: // LPSPI1 SDI on PC15 alternate function 4
|
case PC15: // LPSPI1 SDI on PC15 alternate function 4
|
||||||
@@ -794,7 +794,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// SPI SDO
|
// SPI SDO
|
||||||
case pinModeSPISDO:
|
case PinModeSPISDO:
|
||||||
var mode uint32
|
var mode uint32
|
||||||
switch p {
|
switch p {
|
||||||
case PC14: // LPSPI1 SDO on PC14 alternate function 4
|
case PC14: // LPSPI1 SDO on PC14 alternate function 4
|
||||||
@@ -812,7 +812,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// SPI SCK
|
// SPI SCK
|
||||||
case pinModeSPICLK:
|
case PinModeSPICLK:
|
||||||
var mode uint32
|
var mode uint32
|
||||||
switch p {
|
switch p {
|
||||||
case PC12: // LPSPI1 SCK on PC12 alternate function 4
|
case PC12: // LPSPI1 SCK on PC12 alternate function 4
|
||||||
@@ -830,7 +830,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// SPI CS
|
// SPI CS
|
||||||
case pinModeSPICS:
|
case PinModeSPICS:
|
||||||
var mode uint32
|
var mode uint32
|
||||||
switch p {
|
switch p {
|
||||||
case PC13: // LPSPI1 CS on PC13 alternate function 4
|
case PC13: // LPSPI1 CS on PC13 alternate function 4
|
||||||
@@ -848,7 +848,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// I2C SDA
|
// I2C SDA
|
||||||
case pinModeI2CSDA:
|
case PinModeI2CSDA:
|
||||||
var mode uint32
|
var mode uint32
|
||||||
switch p {
|
switch p {
|
||||||
case PA13: // LPI2C4 SDA on PA13 alternate function 0
|
case PA13: // LPI2C4 SDA on PA13 alternate function 0
|
||||||
@@ -866,7 +866,7 @@ func (p Pin) getMuxMode(config PinConfig) uint32 {
|
|||||||
return mode
|
return mode
|
||||||
|
|
||||||
// I2C SCL
|
// I2C SCL
|
||||||
case pinModeI2CSCL:
|
case PinModeI2CSCL:
|
||||||
var mode uint32
|
var mode uint32
|
||||||
switch p {
|
switch p {
|
||||||
case PA12: // LPI2C4 SCL on PA12 alternate function 0
|
case PA12: // LPI2C4 SCL on PA12 alternate function 0
|
||||||
@@ -902,7 +902,7 @@ func (a ADC) Configure(config ADCConfig) {
|
|||||||
defaultSamples = uint32(4)
|
defaultSamples = uint32(4)
|
||||||
)
|
)
|
||||||
|
|
||||||
a.Pin.Configure(PinConfig{Mode: pinInputAnalog})
|
a.Pin.Configure(PinConfig{Mode: PinInputAnalog})
|
||||||
|
|
||||||
resolution, samples := config.Resolution, config.Samples
|
resolution, samples := config.Resolution, config.Samples
|
||||||
if 0 == resolution {
|
if 0 == resolution {
|
||||||
|
|||||||
@@ -156,8 +156,8 @@ func (i2c *I2C) Configure(config I2CConfig) {
|
|||||||
sda, scl := i2c.setPins(config)
|
sda, scl := i2c.setPins(config)
|
||||||
|
|
||||||
// configure the mux and pad control registers
|
// configure the mux and pad control registers
|
||||||
sda.Configure(PinConfig{Mode: pinModeI2CSDA})
|
sda.Configure(PinConfig{Mode: PinModeI2CSDA})
|
||||||
scl.Configure(PinConfig{Mode: pinModeI2CSCL})
|
scl.Configure(PinConfig{Mode: PinModeI2CSCL})
|
||||||
|
|
||||||
// configure the mux input selector
|
// configure the mux input selector
|
||||||
i2c.muxSDA.connect()
|
i2c.muxSDA.connect()
|
||||||
|
|||||||
@@ -77,10 +77,10 @@ func (spi *SPI) Configure(config SPIConfig) {
|
|||||||
spi.sdi, spi.sdo, spi.sck, spi.cs = config.getPins()
|
spi.sdi, spi.sdo, spi.sck, spi.cs = config.getPins()
|
||||||
|
|
||||||
// configure the mux and pad control registers
|
// configure the mux and pad control registers
|
||||||
spi.sdi.Configure(PinConfig{Mode: pinModeSPISDI})
|
spi.sdi.Configure(PinConfig{Mode: PinModeSPISDI})
|
||||||
spi.sdo.Configure(PinConfig{Mode: pinModeSPISDO})
|
spi.sdo.Configure(PinConfig{Mode: PinModeSPISDO})
|
||||||
spi.sck.Configure(PinConfig{Mode: pinModeSPICLK})
|
spi.sck.Configure(PinConfig{Mode: PinModeSPICLK})
|
||||||
spi.cs.Configure(PinConfig{Mode: pinModeSPICS})
|
spi.cs.Configure(PinConfig{Mode: PinModeSPICS})
|
||||||
|
|
||||||
// configure the mux input selector
|
// configure the mux input selector
|
||||||
spi.muxSDI.connect()
|
spi.muxSDI.connect()
|
||||||
|
|||||||
@@ -68,8 +68,8 @@ func (uart *UART) Configure(config UARTConfig) {
|
|||||||
uart.tx = config.TX
|
uart.tx = config.TX
|
||||||
|
|
||||||
// configure the mux and pad control registers
|
// configure the mux and pad control registers
|
||||||
uart.rx.Configure(PinConfig{Mode: pinModeUARTRX})
|
uart.rx.Configure(PinConfig{Mode: PinModeUARTRX})
|
||||||
uart.tx.Configure(PinConfig{Mode: pinModeUARTTX})
|
uart.tx.Configure(PinConfig{Mode: PinModeUARTTX})
|
||||||
|
|
||||||
// configure the mux input selector
|
// configure the mux input selector
|
||||||
uart.muxRX.connect()
|
uart.muxRX.connect()
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ func (c ADCChannel) Configure(config ADCConfig) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.Configure(PinConfig{Mode: pinAnalog})
|
p.Configure(PinConfig{Mode: PinAnalog})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -81,13 +81,11 @@ const (
|
|||||||
PinInput
|
PinInput
|
||||||
PinInputPulldown
|
PinInputPulldown
|
||||||
PinInputPullup
|
PinInputPullup
|
||||||
|
PinAnalog
|
||||||
// internal pin modes
|
PinUART
|
||||||
pinAnalog
|
PinPWM
|
||||||
pinUART
|
PinI2C
|
||||||
pinPWM
|
PinSPI
|
||||||
pinI2C
|
|
||||||
pinSPI
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p Pin) PortMaskSet() (*uint32, uint32) {
|
func (p Pin) PortMaskSet() (*uint32, uint32) {
|
||||||
@@ -190,20 +188,20 @@ func (p Pin) Configure(config PinConfig) {
|
|||||||
case PinInputPullup:
|
case PinInputPullup:
|
||||||
p.setFunc(fnSIO)
|
p.setFunc(fnSIO)
|
||||||
p.pullup()
|
p.pullup()
|
||||||
case pinAnalog:
|
case PinAnalog:
|
||||||
p.setFunc(fnNULL)
|
p.setFunc(fnNULL)
|
||||||
p.pulloff()
|
p.pulloff()
|
||||||
case pinUART:
|
case PinUART:
|
||||||
p.setFunc(fnUART)
|
p.setFunc(fnUART)
|
||||||
case pinPWM:
|
case PinPWM:
|
||||||
p.setFunc(fnPWM)
|
p.setFunc(fnPWM)
|
||||||
case pinI2C:
|
case PinI2C:
|
||||||
// IO config according to 4.3.1.3 of rp2040 datasheet.
|
// IO config according to 4.3.1.3 of rp2040 datasheet.
|
||||||
p.setFunc(fnI2C)
|
p.setFunc(fnI2C)
|
||||||
p.pullup()
|
p.pullup()
|
||||||
p.setSchmitt(true)
|
p.setSchmitt(true)
|
||||||
p.setSlew(false)
|
p.setSlew(false)
|
||||||
case pinSPI:
|
case PinSPI:
|
||||||
p.setFunc(fnSPI)
|
p.setFunc(fnSPI)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,8 +105,8 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
|||||||
if config.Frequency == 0 {
|
if config.Frequency == 0 {
|
||||||
config.Frequency = defaultBaud
|
config.Frequency = defaultBaud
|
||||||
}
|
}
|
||||||
config.SDA.Configure(PinConfig{pinI2C})
|
config.SDA.Configure(PinConfig{PinI2C})
|
||||||
config.SCL.Configure(PinConfig{pinI2C})
|
config.SCL.Configure(PinConfig{PinI2C})
|
||||||
return i2c.init(config)
|
return i2c.init(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ func (pwm *pwmGroup) Channel(pin Pin) (channel uint8, err error) {
|
|||||||
if pin > maxPWMPins || pwmGPIOToSlice(pin) != pwm.peripheral() {
|
if pin > maxPWMPins || pwmGPIOToSlice(pin) != pwm.peripheral() {
|
||||||
return 3, ErrInvalidOutputPin
|
return 3, ErrInvalidOutputPin
|
||||||
}
|
}
|
||||||
pin.Configure(PinConfig{pinPWM})
|
pin.Configure(PinConfig{PinPWM})
|
||||||
return pwmGPIOToChannel(pin), nil
|
return pwmGPIOToChannel(pin), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ func (uart *UART) Configure(config UARTConfig) error {
|
|||||||
rp.UART0_UARTCR_TXE)
|
rp.UART0_UARTCR_TXE)
|
||||||
|
|
||||||
// set GPIO mux to UART for the pins
|
// set GPIO mux to UART for the pins
|
||||||
config.TX.Configure(PinConfig{Mode: pinUART})
|
config.TX.Configure(PinConfig{Mode: PinUART})
|
||||||
config.RX.Configure(PinConfig{Mode: pinUART})
|
config.RX.Configure(PinConfig{Mode: PinUART})
|
||||||
|
|
||||||
// Enable RX IRQ.
|
// Enable RX IRQ.
|
||||||
uart.Interrupt.SetPriority(0x80)
|
uart.Interrupt.SetPriority(0x80)
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func InitADC() {
|
|||||||
|
|
||||||
// Configure configures an ADC pin to be able to read analog data.
|
// Configure configures an ADC pin to be able to read analog data.
|
||||||
func (a ADC) Configure(ADCConfig) {
|
func (a ADC) Configure(ADCConfig) {
|
||||||
a.Pin.Configure(PinConfig{Mode: pinInputModeAnalog})
|
a.Pin.Configure(PinConfig{Mode: PinInputModeAnalog})
|
||||||
|
|
||||||
// set sample time
|
// set sample time
|
||||||
ch := a.getChannel()
|
ch := a.getChannel()
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func InitADC() {
|
|||||||
|
|
||||||
// Configure configures an ADC pin to be able to read analog data.
|
// Configure configures an ADC pin to be able to read analog data.
|
||||||
func (a ADC) Configure(ADCConfig) {
|
func (a ADC) Configure(ADCConfig) {
|
||||||
a.Pin.ConfigureAltFunc(PinConfig{Mode: pinInputAnalog}, 0)
|
a.Pin.ConfigureAltFunc(PinConfig{Mode: PinInputAnalog}, 0)
|
||||||
|
|
||||||
// set sample time
|
// set sample time
|
||||||
ch := a.getChannel()
|
ch := a.getChannel()
|
||||||
|
|||||||
@@ -102,8 +102,8 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i2c *I2C) configurePins(config I2CConfig) {
|
func (i2c *I2C) configurePins(config I2CConfig) {
|
||||||
config.SCL.ConfigureAltFunc(PinConfig{Mode: pinModeI2CSCL}, i2c.AltFuncSelector)
|
config.SCL.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSCL}, i2c.AltFuncSelector)
|
||||||
config.SDA.ConfigureAltFunc(PinConfig{Mode: pinModeI2CSDA}, i2c.AltFuncSelector)
|
config.SDA.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSDA}, i2c.AltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i2c *I2C) controllerTransmit(addr uint16, w []byte) error {
|
func (i2c *I2C) controllerTransmit(addr uint16, w []byte) error {
|
||||||
|
|||||||
@@ -14,28 +14,29 @@ import (
|
|||||||
const (
|
const (
|
||||||
// Mode Flag
|
// Mode Flag
|
||||||
PinOutput PinMode = 0
|
PinOutput PinMode = 0
|
||||||
PinInput PinMode = 1
|
PinInput PinMode = PinInputFloating
|
||||||
|
PinInputFloating PinMode = 1
|
||||||
PinInputPulldown PinMode = 2
|
PinInputPulldown PinMode = 2
|
||||||
PinInputPullup PinMode = 3
|
PinInputPullup PinMode = 3
|
||||||
|
|
||||||
// for UART
|
// for UART
|
||||||
pinModeUARTTX PinMode = 4
|
PinModeUARTTX PinMode = 4
|
||||||
pinModeUARTRX PinMode = 5
|
PinModeUARTRX PinMode = 5
|
||||||
|
|
||||||
// for I2C
|
// for I2C
|
||||||
pinModeI2CSCL PinMode = 6
|
PinModeI2CSCL PinMode = 6
|
||||||
pinModeI2CSDA PinMode = 7
|
PinModeI2CSDA PinMode = 7
|
||||||
|
|
||||||
// for SPI
|
// for SPI
|
||||||
pinModeSPICLK PinMode = 8
|
PinModeSPICLK PinMode = 8
|
||||||
pinModeSPISDO PinMode = 9
|
PinModeSPISDO PinMode = 9
|
||||||
pinModeSPISDI PinMode = 10
|
PinModeSPISDI PinMode = 10
|
||||||
|
|
||||||
// for analog/ADC
|
// for analog/ADC
|
||||||
pinInputAnalog PinMode = 11
|
PinInputAnalog PinMode = 11
|
||||||
|
|
||||||
// for PWM
|
// for PWM
|
||||||
pinModePWMOutput PinMode = 12
|
PinModePWMOutput PinMode = 12
|
||||||
)
|
)
|
||||||
|
|
||||||
// Define several bitfields that have different names across chip families but
|
// Define several bitfields that have different names across chip families but
|
||||||
@@ -82,7 +83,7 @@ func (p Pin) ConfigureAltFunc(config PinConfig, altFunc uint8) {
|
|||||||
switch config.Mode {
|
switch config.Mode {
|
||||||
|
|
||||||
// GPIO
|
// GPIO
|
||||||
case PinInput:
|
case PinInputFloating:
|
||||||
port.MODER.ReplaceBits(gpioModeInput, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeInput, gpioModeMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
||||||
case PinInputPulldown:
|
case PinInputPulldown:
|
||||||
@@ -96,24 +97,24 @@ func (p Pin) ConfigureAltFunc(config PinConfig, altFunc uint8) {
|
|||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
||||||
|
|
||||||
// UART
|
// UART
|
||||||
case pinModeUARTTX:
|
case PinModeUARTTX:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullUp, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullUp, gpioPullMask, pos)
|
||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
case pinModeUARTRX:
|
case PinModeUARTRX:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
|
|
||||||
// I2C
|
// I2C
|
||||||
case pinModeI2CSCL:
|
case PinModeI2CSCL:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.OTYPER.ReplaceBits(stm32.GPIO_OTYPER_OT0_OpenDrain, stm32.GPIO_OTYPER_OT0_Msk, pos/2)
|
port.OTYPER.ReplaceBits(stm32.GPIO_OTYPER_OT0_OpenDrain, stm32.GPIO_OTYPER_OT0_Msk, pos/2)
|
||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullUp, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullUp, gpioPullMask, pos)
|
||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
case pinModeI2CSDA:
|
case PinModeI2CSDA:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.OTYPER.ReplaceBits(stm32.GPIO_OTYPER_OT0_OpenDrain, stm32.GPIO_OTYPER_OT0_Msk, pos/2)
|
port.OTYPER.ReplaceBits(stm32.GPIO_OTYPER_OT0_OpenDrain, stm32.GPIO_OTYPER_OT0_Msk, pos/2)
|
||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
||||||
@@ -121,31 +122,31 @@ func (p Pin) ConfigureAltFunc(config PinConfig, altFunc uint8) {
|
|||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
|
|
||||||
// SPI
|
// SPI
|
||||||
case pinModeSPICLK:
|
case PinModeSPICLK:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
case pinModeSPISDO:
|
case PinModeSPISDO:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
case pinModeSPISDI:
|
case PinModeSPISDI:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedLow, gpioOutputSpeedMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
|
|
||||||
// PWM
|
// PWM
|
||||||
case pinModePWMOutput:
|
case PinModePWMOutput:
|
||||||
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAlternate, gpioModeMask, pos)
|
||||||
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
|
||||||
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
|
||||||
p.SetAltFunc(altFunc)
|
p.SetAltFunc(altFunc)
|
||||||
|
|
||||||
// ADC
|
// ADC
|
||||||
case pinInputAnalog:
|
case PinInputAnalog:
|
||||||
port.MODER.ReplaceBits(gpioModeAnalog, gpioModeMask, pos)
|
port.MODER.ReplaceBits(gpioModeAnalog, gpioModeMask, pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ func (t *TIM) Channel(pin Pin) (uint8, error) {
|
|||||||
for _, p := range ch.Pins {
|
for _, p := range ch.Pins {
|
||||||
if p.Pin == pin {
|
if p.Pin == pin {
|
||||||
t.configurePin(uint8(chi), p)
|
t.configurePin(uint8(chi), p)
|
||||||
//p.Pin.ConfigureAltFunc(PinConfig{Mode: pinModePWMOutput}, p.AltFunc)
|
//p.Pin.ConfigureAltFunc(PinConfig{Mode: PinModePWMOutput}, p.AltFunc)
|
||||||
return uint8(chi), nil
|
return uint8(chi), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ package machine
|
|||||||
// register (most MCUs except STM32F1 series).
|
// register (most MCUs except STM32F1 series).
|
||||||
|
|
||||||
func (t *TIM) configurePin(channel uint8, pf PinFunction) {
|
func (t *TIM) configurePin(channel uint8, pf PinFunction) {
|
||||||
pf.Pin.ConfigureAltFunc(PinConfig{Mode: pinModePWMOutput}, pf.AltFunc)
|
pf.Pin.ConfigureAltFunc(PinConfig{Mode: PinModePWMOutput}, pf.AltFunc)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,20 +29,20 @@ const (
|
|||||||
PinOutput50MHz PinMode = 3 // Output mode, max speed 50MHz
|
PinOutput50MHz PinMode = 3 // Output mode, max speed 50MHz
|
||||||
PinOutput PinMode = PinOutput2MHz
|
PinOutput PinMode = PinOutput2MHz
|
||||||
|
|
||||||
pinInputModeAnalog PinMode = 0 // Input analog mode
|
PinInputModeAnalog PinMode = 0 // Input analog mode
|
||||||
pinInputModeFloating PinMode = 4 // Input floating mode
|
PinInputModeFloating PinMode = 4 // Input floating mode
|
||||||
pinInputModePullUpDown PinMode = 8 // Input pull up/down mode
|
PinInputModePullUpDown PinMode = 8 // Input pull up/down mode
|
||||||
pinInputModeReserved PinMode = 12 // Input mode (reserved)
|
PinInputModeReserved PinMode = 12 // Input mode (reserved)
|
||||||
|
|
||||||
pinOutputModeGPPushPull PinMode = 0 // Output mode general purpose push/pull
|
PinOutputModeGPPushPull PinMode = 0 // Output mode general purpose push/pull
|
||||||
pinOutputModeGPOpenDrain PinMode = 4 // Output mode general purpose open drain
|
PinOutputModeGPOpenDrain PinMode = 4 // Output mode general purpose open drain
|
||||||
pinOutputModeAltPushPull PinMode = 8 // Output mode alt. purpose push/pull
|
PinOutputModeAltPushPull PinMode = 8 // Output mode alt. purpose push/pull
|
||||||
pinOutputModeAltOpenDrain PinMode = 12 // Output mode alt. purpose open drain
|
PinOutputModeAltOpenDrain PinMode = 12 // Output mode alt. purpose open drain
|
||||||
|
|
||||||
// Pull-up vs Pull down is not part of the CNF0 / CNF1 bits, but is
|
// Pull-up vs Pull down is not part of the CNF0 / CNF1 bits, but is
|
||||||
// controlled by PxODR. Encoded using the 'spare' bit 5.
|
// controlled by PxODR. Encoded using the 'spare' bit 5.
|
||||||
PinInputPulldown PinMode = pinInputModePullUpDown
|
PinInputPulldown PinMode = PinInputModePullUpDown
|
||||||
PinInputPullup PinMode = pinInputModePullUpDown | 0x10
|
PinInputPullup PinMode = PinInputModePullUpDown | 0x10
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pin constants for all stm32f103 package sizes
|
// Pin constants for all stm32f103 package sizes
|
||||||
@@ -166,7 +166,7 @@ func (p Pin) Configure(config PinConfig) {
|
|||||||
|
|
||||||
// If configured for input pull-up or pull-down, set ODR
|
// If configured for input pull-up or pull-down, set ODR
|
||||||
// for desired pull-up or pull-down.
|
// for desired pull-up or pull-down.
|
||||||
if (config.Mode & 0xf) == pinInputModePullUpDown {
|
if (config.Mode & 0xf) == PinInputModePullUpDown {
|
||||||
var pullup uint32
|
var pullup uint32
|
||||||
if config.Mode == PinInputPullup {
|
if config.Mode == PinInputPullup {
|
||||||
pullup = 1
|
pullup = 1
|
||||||
@@ -290,8 +290,8 @@ func (uart *UART) configurePins(config UARTConfig) {
|
|||||||
default:
|
default:
|
||||||
// use standard TX/RX pins PA9 and PA10
|
// use standard TX/RX pins PA9 and PA10
|
||||||
}
|
}
|
||||||
config.TX.Configure(PinConfig{Mode: PinOutput50MHz + pinOutputModeAltPushPull})
|
config.TX.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltPushPull})
|
||||||
config.RX.Configure(PinConfig{Mode: pinInputModeFloating})
|
config.RX.Configure(PinConfig{Mode: PinInputModeFloating})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the divisor for USARTs to get the given baudrate
|
// Determine the divisor for USARTs to get the given baudrate
|
||||||
@@ -363,9 +363,9 @@ func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
|||||||
|
|
||||||
// Configure SPI pins for input output and clock
|
// Configure SPI pins for input output and clock
|
||||||
func (spi SPI) configurePins(config SPIConfig) {
|
func (spi SPI) configurePins(config SPIConfig) {
|
||||||
config.SCK.Configure(PinConfig{Mode: PinOutput50MHz + pinOutputModeAltPushPull})
|
config.SCK.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltPushPull})
|
||||||
config.SDO.Configure(PinConfig{Mode: PinOutput50MHz + pinOutputModeAltPushPull})
|
config.SDO.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltPushPull})
|
||||||
config.SDI.Configure(PinConfig{Mode: pinInputModeFloating})
|
config.SDI.Configure(PinConfig{Mode: PinInputModeFloating})
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------- I2C related types and code
|
//---------- I2C related types and code
|
||||||
@@ -390,8 +390,8 @@ func (i2c *I2C) configurePins(config I2CConfig) {
|
|||||||
stm32.AFIO.MAPR.SetBits(stm32.AFIO_MAPR_I2C1_REMAP)
|
stm32.AFIO.MAPR.SetBits(stm32.AFIO_MAPR_I2C1_REMAP)
|
||||||
}
|
}
|
||||||
|
|
||||||
config.SDA.Configure(PinConfig{Mode: PinOutput50MHz + pinOutputModeAltOpenDrain})
|
config.SDA.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltOpenDrain})
|
||||||
config.SCL.Configure(PinConfig{Mode: PinOutput50MHz + pinOutputModeAltOpenDrain})
|
config.SCL.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltOpenDrain})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||||
@@ -717,7 +717,7 @@ func (t *TIM) configurePin(channel uint8, pf PinFunction) {
|
|||||||
stm32.AFIO.MAPR.ReplaceBits(remap<<stm32.AFIO_MAPR2_TIM14_REMAP_Pos, stm32.AFIO_MAPR2_TIM14_REMAP_Msk, 0)
|
stm32.AFIO.MAPR.ReplaceBits(remap<<stm32.AFIO_MAPR2_TIM14_REMAP_Pos, stm32.AFIO_MAPR2_TIM14_REMAP_Msk, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pf.Pin.Configure(PinConfig{Mode: PinOutput + pinOutputModeAltPushPull})
|
pf.Pin.Configure(PinConfig{Mode: PinOutput + PinOutputModeAltPushPull})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TIM) enableMainOutput() {
|
func (t *TIM) enableMainOutput() {
|
||||||
|
|||||||
@@ -640,8 +640,8 @@ const (
|
|||||||
|
|
||||||
func (uart *UART) configurePins(config UARTConfig) {
|
func (uart *UART) configurePins(config UARTConfig) {
|
||||||
// enable the alternate functions on the TX and RX pins
|
// enable the alternate functions on the TX and RX pins
|
||||||
config.TX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTTX}, uart.TxAltFuncSelector)
|
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||||
config.RX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTRX}, uart.RxAltFuncSelector)
|
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
|
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
|
||||||
@@ -674,9 +674,9 @@ func (spi SPI) config8Bits() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (spi SPI) configurePins(config SPIConfig) {
|
func (spi SPI) configurePins(config SPIConfig) {
|
||||||
config.SCK.ConfigureAltFunc(PinConfig{Mode: pinModeSPICLK}, spi.AltFuncSelector)
|
config.SCK.ConfigureAltFunc(PinConfig{Mode: PinModeSPICLK}, spi.AltFuncSelector)
|
||||||
config.SDO.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDO}, spi.AltFuncSelector)
|
config.SDO.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDO}, spi.AltFuncSelector)
|
||||||
config.SDI.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDI}, spi.AltFuncSelector)
|
config.SDI.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDI}, spi.AltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
||||||
@@ -723,8 +723,8 @@ type I2C struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i2c *I2C) configurePins(config I2CConfig) {
|
func (i2c *I2C) configurePins(config I2CConfig) {
|
||||||
config.SCL.ConfigureAltFunc(PinConfig{Mode: pinModeI2CSCL}, i2c.AltFuncSelector)
|
config.SCL.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSCL}, i2c.AltFuncSelector)
|
||||||
config.SDA.ConfigureAltFunc(PinConfig{Mode: pinModeI2CSDA}, i2c.AltFuncSelector)
|
config.SDA.ConfigureAltFunc(PinConfig{Mode: PinModeI2CSDA}, i2c.AltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
func (i2c *I2C) getFreqRange(config I2CConfig) uint32 {
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ const APB2_TIM_FREQ = 216e6 // 216MHz
|
|||||||
// Configure the UART.
|
// Configure the UART.
|
||||||
func (uart *UART) configurePins(config UARTConfig) {
|
func (uart *UART) configurePins(config UARTConfig) {
|
||||||
// enable the alternate functions on the TX and RX pins
|
// enable the alternate functions on the TX and RX pins
|
||||||
config.TX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTTX}, uart.TxAltFuncSelector)
|
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||||
config.RX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTRX}, uart.RxAltFuncSelector)
|
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UART baudrate calc based on the bus and clockspeed
|
// UART baudrate calc based on the bus and clockspeed
|
||||||
|
|||||||
@@ -195,8 +195,8 @@ func (p Pin) registerInterrupt() interrupt.Interrupt {
|
|||||||
// Configure the UART.
|
// Configure the UART.
|
||||||
func (uart *UART) configurePins(config UARTConfig) {
|
func (uart *UART) configurePins(config UARTConfig) {
|
||||||
// enable the alternate functions on the TX and RX pins
|
// enable the alternate functions on the TX and RX pins
|
||||||
config.TX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTTX}, uart.TxAltFuncSelector)
|
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||||
config.RX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTRX}, uart.RxAltFuncSelector)
|
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UART baudrate calc based on the bus and clockspeed
|
// UART baudrate calc based on the bus and clockspeed
|
||||||
@@ -289,9 +289,9 @@ func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
|||||||
|
|
||||||
// Configure SPI pins for input output and clock
|
// Configure SPI pins for input output and clock
|
||||||
func (spi SPI) configurePins(config SPIConfig) {
|
func (spi SPI) configurePins(config SPIConfig) {
|
||||||
config.SCK.ConfigureAltFunc(PinConfig{Mode: pinModeSPICLK}, spi.AltFuncSelector)
|
config.SCK.ConfigureAltFunc(PinConfig{Mode: PinModeSPICLK}, spi.AltFuncSelector)
|
||||||
config.SDO.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDO}, spi.AltFuncSelector)
|
config.SDO.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDO}, spi.AltFuncSelector)
|
||||||
config.SDI.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDI}, spi.AltFuncSelector)
|
config.SDI.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDI}, spi.AltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------- I2C related types and code
|
//---------- I2C related types and code
|
||||||
|
|||||||
@@ -280,8 +280,8 @@ func (p Pin) registerInterrupt() interrupt.Interrupt {
|
|||||||
// Configure the UART.
|
// Configure the UART.
|
||||||
func (uart *UART) configurePins(config UARTConfig) {
|
func (uart *UART) configurePins(config UARTConfig) {
|
||||||
// enable the alternate functions on the TX and RX pins
|
// enable the alternate functions on the TX and RX pins
|
||||||
config.TX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTTX}, uart.TxAltFuncSelector)
|
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||||
config.RX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTRX}, uart.RxAltFuncSelector)
|
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UART baudrate calc based on the bus and clockspeed
|
// UART baudrate calc based on the bus and clockspeed
|
||||||
@@ -357,9 +357,9 @@ func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
|||||||
|
|
||||||
// Configure SPI pins for input output and clock
|
// Configure SPI pins for input output and clock
|
||||||
func (spi SPI) configurePins(config SPIConfig) {
|
func (spi SPI) configurePins(config SPIConfig) {
|
||||||
config.SCK.ConfigureAltFunc(PinConfig{Mode: pinModeSPICLK}, spi.AltFuncSelector)
|
config.SCK.ConfigureAltFunc(PinConfig{Mode: PinModeSPICLK}, spi.AltFuncSelector)
|
||||||
config.SDO.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDO}, spi.AltFuncSelector)
|
config.SDO.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDO}, spi.AltFuncSelector)
|
||||||
config.SDI.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDI}, spi.AltFuncSelector)
|
config.SDI.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDI}, spi.AltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------- Timer related code
|
//---------- Timer related code
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ func (uart *UART) configurePins(config UARTConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// enable the alternate functions on the TX and RX pins
|
// enable the alternate functions on the TX and RX pins
|
||||||
config.TX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTTX}, uart.TxAltFuncSelector)
|
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||||
config.RX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTRX}, uart.RxAltFuncSelector)
|
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UART baudrate calc based on the bus and clockspeed
|
// UART baudrate calc based on the bus and clockspeed
|
||||||
|
|||||||
@@ -239,9 +239,9 @@ func (spi SPI) config8Bits() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (spi SPI) configurePins(config SPIConfig) {
|
func (spi SPI) configurePins(config SPIConfig) {
|
||||||
config.SCK.ConfigureAltFunc(PinConfig{Mode: pinModeSPICLK}, spi.AltFuncSelector)
|
config.SCK.ConfigureAltFunc(PinConfig{Mode: PinModeSPICLK}, spi.AltFuncSelector)
|
||||||
config.SDO.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDO}, spi.AltFuncSelector)
|
config.SDO.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDO}, spi.AltFuncSelector)
|
||||||
config.SDI.ConfigureAltFunc(PinConfig{Mode: pinModeSPISDI}, spi.AltFuncSelector)
|
config.SDI.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDI}, spi.AltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
||||||
@@ -298,8 +298,8 @@ func (i2c *I2C) getFreqRange() uint32 {
|
|||||||
// Configure the UART.
|
// Configure the UART.
|
||||||
func (uart UART) configurePins(config UARTConfig) {
|
func (uart UART) configurePins(config UARTConfig) {
|
||||||
// enable the alternate functions on the TX and RX pins
|
// enable the alternate functions on the TX and RX pins
|
||||||
config.TX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTTX}, uart.TxAltFuncSelector)
|
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
|
||||||
config.RX.ConfigureAltFunc(PinConfig{Mode: pinModeUARTRX}, uart.RxAltFuncSelector)
|
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UART baudrate calc based on the bus and clockspeed
|
// UART baudrate calc based on the bus and clockspeed
|
||||||
|
|||||||
@@ -0,0 +1,225 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// This file runs some checks on the machine package, to make sure it matches
|
||||||
|
// the documentation and that it is internally consistent between targets.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/ast"
|
||||||
|
"go/scanner"
|
||||||
|
"go/token"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/tinygo-org/tinygo/compileopts"
|
||||||
|
"github.com/tinygo-org/tinygo/loader"
|
||||||
|
"golang.org/x/tools/go/packages"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Constants that are documented and always have a particular type.
|
||||||
|
var knownConsts = map[string]string{
|
||||||
|
// Chip name, something like "nrf51" or "STM32F429".
|
||||||
|
"Device": "untyped string",
|
||||||
|
|
||||||
|
// Generic constants.
|
||||||
|
"KHz": "untyped int",
|
||||||
|
"MHz": "untyped int",
|
||||||
|
"GHz": "untyped int",
|
||||||
|
|
||||||
|
// Pin mode types for machine.PinConfig.
|
||||||
|
"PinInput": "machine.PinMode",
|
||||||
|
"PinOutput": "machine.PinMode",
|
||||||
|
"PinInputPullup": "machine.PinMode",
|
||||||
|
"PinInputPulldown": "machine.PinMode",
|
||||||
|
|
||||||
|
// Pin change events for pin interrupts.
|
||||||
|
"PinRising": "machine.PinChange",
|
||||||
|
"PinFalling": "machine.PinChange",
|
||||||
|
"PinToggle": "machine.PinChange",
|
||||||
|
|
||||||
|
// UART parity
|
||||||
|
"ParityNone": "machine.UARTParity",
|
||||||
|
"ParityEven": "machine.UARTParity",
|
||||||
|
"ParityOdd": "machine.UARTParity",
|
||||||
|
|
||||||
|
// SPI modes
|
||||||
|
"Mode0": "untyped int",
|
||||||
|
"Mode1": "untyped int",
|
||||||
|
"Mode2": "untyped int",
|
||||||
|
"Mode3": "untyped int",
|
||||||
|
|
||||||
|
// Board specific constants.
|
||||||
|
"HasLowFrequencyCrystal": "untyped bool", // for nrf boards
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
targets := os.Args[1:]
|
||||||
|
|
||||||
|
// Start worker goroutines to run checkTarget.
|
||||||
|
results := make(map[string]chan []error)
|
||||||
|
for _, target := range targets {
|
||||||
|
results[target] = make(chan []error, 1)
|
||||||
|
}
|
||||||
|
targetsChan := make(chan string)
|
||||||
|
for i := 0; i < runtime.NumCPU(); i++ {
|
||||||
|
go func() {
|
||||||
|
for target := range targetsChan {
|
||||||
|
errs := checkTarget(target)
|
||||||
|
if len(errs) != 0 {
|
||||||
|
results[target] <- errs
|
||||||
|
}
|
||||||
|
close(results[target])
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check all targets.
|
||||||
|
go func() {
|
||||||
|
for _, target := range targets {
|
||||||
|
targetsChan <- target
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Read the result of each check.
|
||||||
|
failedTargets := 0
|
||||||
|
for _, target := range targets {
|
||||||
|
errs := <-results[target]
|
||||||
|
if len(errs) != 0 {
|
||||||
|
failedTargets++
|
||||||
|
fmt.Printf("found errors for %s:\n", target)
|
||||||
|
for _, err := range errs {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if failedTargets != 0 {
|
||||||
|
fmt.Printf("Failed checks for %d out of %d targets.\n", failedTargets, len(targets))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether the machine package of the given targets is valid.
|
||||||
|
func checkTarget(target string) []error {
|
||||||
|
// Load target configuration.
|
||||||
|
options := &compileopts.Options{
|
||||||
|
Target: target,
|
||||||
|
}
|
||||||
|
spec, err := compileopts.LoadTarget(options)
|
||||||
|
if err != nil {
|
||||||
|
return []error{err}
|
||||||
|
}
|
||||||
|
config := &compileopts.Config{Options: options, Target: spec}
|
||||||
|
|
||||||
|
// Load the package.
|
||||||
|
goroot, err := loader.GetCachedGoroot(config)
|
||||||
|
if err != nil {
|
||||||
|
return []error{err}
|
||||||
|
}
|
||||||
|
cfg := &packages.Config{
|
||||||
|
Env: append(os.Environ(),
|
||||||
|
"GOOS="+config.GOOS(),
|
||||||
|
"GOARCH="+config.GOARCH(),
|
||||||
|
"GOROOT="+goroot,
|
||||||
|
),
|
||||||
|
BuildFlags: []string{
|
||||||
|
"-tags=" + strings.Join(spec.BuildTags, " "),
|
||||||
|
},
|
||||||
|
Mode: packages.NeedSyntax | packages.NeedTypes,
|
||||||
|
}
|
||||||
|
pkgs, err := packages.Load(cfg, "machine")
|
||||||
|
if err != nil {
|
||||||
|
return []error{err}
|
||||||
|
}
|
||||||
|
pkg := pkgs[0]
|
||||||
|
if len(pkg.Errors) != 0 {
|
||||||
|
var errors []error
|
||||||
|
for _, err := range pkg.Errors {
|
||||||
|
errors = append(errors, err)
|
||||||
|
}
|
||||||
|
return errors
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the package!
|
||||||
|
checker := Checker{
|
||||||
|
goroot: goroot,
|
||||||
|
pkg: pkg,
|
||||||
|
}
|
||||||
|
for _, file := range pkg.Syntax {
|
||||||
|
if checker.getPosition(file.Package).Filename == "i2s.go" {
|
||||||
|
// TODO: check I2S.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, decl := range file.Decls {
|
||||||
|
switch decl := decl.(type) {
|
||||||
|
case *ast.GenDecl:
|
||||||
|
if isDeprecated(decl.Doc.Text()) {
|
||||||
|
// Don't check deprecated declarations. They will be removed
|
||||||
|
// with TinyGo 1.0.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch decl.Tok {
|
||||||
|
case token.CONST:
|
||||||
|
for _, spec := range decl.Specs {
|
||||||
|
spec := spec.(*ast.ValueSpec)
|
||||||
|
for _, name := range spec.Names {
|
||||||
|
if !ast.IsExported(name.Name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
checker.checkConst(spec, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return checker.errors
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return whether the comment is for a deprecated declaration.
|
||||||
|
// The convention is explained here:
|
||||||
|
// https://github.com/golang/go/wiki/Deprecated
|
||||||
|
func isDeprecated(comment string) bool {
|
||||||
|
for _, line := range strings.Split(comment, "\n") {
|
||||||
|
if strings.HasPrefix(line, "Deprecated: ") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type Checker struct {
|
||||||
|
goroot string
|
||||||
|
pkg *packages.Package
|
||||||
|
errors []error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether a constant declaration follows the documentation.
|
||||||
|
func (c *Checker) checkConst(spec *ast.ValueSpec, name *ast.Ident) {
|
||||||
|
t := c.pkg.Types.Scope().Lookup(name.Name).Type()
|
||||||
|
if knownConsts[name.Name] == t.String() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case t.String() == "machine.Pin":
|
||||||
|
// Pins have all kinds of names. They might need some checking, but
|
||||||
|
// right now all pin names are fine.
|
||||||
|
default:
|
||||||
|
c.errors = append(c.errors, scanner.Error{
|
||||||
|
Pos: c.getPosition(name.NamePos),
|
||||||
|
Msg: fmt.Sprintf("unknown name: %-20s (type %s)", name.Name, t.String()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) getPosition(pos token.Pos) token.Position {
|
||||||
|
position := c.pkg.Fset.Position(pos)
|
||||||
|
if newpath, err := filepath.Rel(filepath.Join(c.goroot, "src/machine"), position.Filename); err == nil {
|
||||||
|
position.Filename = newpath
|
||||||
|
}
|
||||||
|
return position
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user