Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ src/device/renesas/*.go
src/device/renesas/*.s
src/device/rp/*.go
src/device/rp/*.s
src/device/py32/*.go
src/device/py32/*.s
./vendor
llvm-build
llvm-project
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@
[submodule "lib/bdwgc"]
path = lib/bdwgc
url = https://github.com/ivmai/bdwgc.git
[submodule "lib/py32-svd"]
path = lib/py32-svd
url = https://github.com/burgrp/py32-svd.git
6 changes: 5 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fmt-check: ## Warn if any source needs reformatting
@unformatted=$$(gofmt -l $(FMT_PATHS)); [ -z "$$unformatted" ] && exit 0; echo "Unformatted:"; for fn in $$unformatted; do echo " $$fn"; done; exit 1


gen-device: gen-device-avr gen-device-esp gen-device-nrf gen-device-sam gen-device-sifive gen-device-kendryte gen-device-nxp gen-device-rp ## Generate microcontroller-specific sources
gen-device: gen-device-avr gen-device-esp gen-device-nrf gen-device-sam gen-device-sifive gen-device-kendryte gen-device-nxp gen-device-rp gen-device-py32 ## Generate microcontroller-specific sources
ifneq ($(RENESAS), 0)
gen-device: gen-device-renesas
endif
Expand Down Expand Up @@ -242,6 +242,10 @@ gen-device-renesas: build/gen-device-svd
./build/gen-device-svd -source=https://github.com/cmsis-svd/cmsis-svd-data/tree/master/data/Renesas lib/cmsis-svd/data/Renesas/ src/device/renesas/
GO111MODULE=off $(GO) fmt ./src/device/renesas

gen-device-py32: build/gen-device-svd
./build/gen-device-svd -source=https://github.com/tinygo-org/py32-svd lib/py32-svd/svd src/device/py32/
GO111MODULE=off $(GO) fmt ./src/device/py32

$(LLVM_PROJECTDIR)/llvm:
git clone -b xtensa_release_19.1.2 --depth=1 https://github.com/espressif/llvm-project $(LLVM_PROJECTDIR)
llvm-source: $(LLVM_PROJECTDIR)/llvm ## Get LLVM sources
Expand Down
1 change: 1 addition & 0 deletions lib/py32-svd
Submodule py32-svd added at d98c75
27 changes: 27 additions & 0 deletions src/machine/board_embedfire_py32f002b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build embedfire_py32f002b

// Pin mappings for the Embedfire PY32F002B board.

package machine

// LEDs
const (
LED1 = PA1
LED2 = PA5
LED3 = PA4
LED = LED2
)

// Buttons
const (
KEY1 = PA3
KEY2 = PA0
)

// UART
const (
DEFAULT_UART_TX_PIN = PA6
DEFAULT_UART_RX_PIN = PA7
DEFAULT_UART_TX_PIN_AF = 1
DEFAULT_UART_RX_PIN_AF = 3
)
27 changes: 27 additions & 0 deletions src/machine/board_embedfire_py32f030.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build embedfire_py32f030

// Pin mappings for the Embedfire PY32F030 board.

package machine

// LEDs
const (
LED1 = PA2
LED2 = PA3
LED3 = PA4
LED = LED2
)

// Buttons
const (
KEY1 = PA5
KEY2 = PA6
)

// UART
const (
DEFAULT_UART_TX_PIN = PA7
DEFAULT_UART_RX_PIN = PA8
DEFAULT_UART_TX_PIN_AF = 8
DEFAULT_UART_RX_PIN_AF = 8
)
220 changes: 220 additions & 0 deletions src/machine/machine_py32_pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
//go:build py32

package machine

import (
"device/py32"
"unsafe"
)

const deviceName = py32.Device

// Peripheral port offsets.
// Keep the same spacing used on other MCUs so helpers like Pin.getPortNumber
// can keep using simple division by 16.
const (
portA Pin = iota * 16
portB
portC
portD
portE
portF
)

const (
PA0 Pin = portA + iota
PA1
PA2
PA3
PA4
PA5
PA6
PA7
PA8
PA9
PA10
PA11
PA12
PA13
PA14
PA15
)

const (
PB0 Pin = portB + iota
PB1
PB2
PB3
PB4
PB5
PB6
PB7
PB8
PB9
PB10
PB11
PB12
PB13
PB14
PB15
)

const (
PC0 Pin = portC + iota
PC1
PC2
PC3
PC4
PC5
PC6
PC7
PC8
PC9
PC10
PC11
PC12
PC13
PC14
PC15
)

const (
PD0 Pin = portD + iota
PD1
PD2
PD3
PD4
PD5
PD6
PD7
PD8
PD9
PD10
PD11
PD12
PD13
PD14
PD15
)

const (
PE0 Pin = portE + iota
PE1
PE2
PE3
PE4
PE5
PE6
PE7
PE8
PE9
PE10
PE11
PE12
PE13
PE14
PE15
)

const (
PF0 Pin = portF + iota
PF1
PF2
PF3
PF4
PF5
PF6
PF7
PF8
PF9
PF10
PF11
PF12
PF13
PF14
PF15
)

const (
PinOutput PinMode = iota
PinInputFloating
PinInputPulldown
PinInputPullup
PinInputAnalog
)
const PinInput PinMode = PinInputFloating

const (
gpioModeInput = 0
gpioModeOutput = 1
gpioModeAnalog = 3
gpioModeMask = 0x3

gpioPullFloating = 0
gpioPullUp = 1
gpioPullDown = 2
gpioPullMask = 0x3

gpioOutputSpeedHigh = 2
gpioOutputSpeedMask = 0x3
)

func (p Pin) getPortNumber() uint8 {
return uint8(p) >> 4

}

func (p Pin) getPinNumber() uint8 {
return uint8(p) & 0x0F
}

func (p Pin) getPort() (*py32.GPIO_Type, uint8) {
offset := uintptr(p.getPortNumber()) * (uintptr(unsafe.Pointer(py32.GPIOB)) - uintptr(unsafe.Pointer(py32.GPIOA)))
return (*py32.GPIO_Type)(unsafe.Pointer(uintptr(unsafe.Pointer(py32.GPIOA)) + offset)), p.getPinNumber()
}

func (p Pin) Set(high bool) {
port, pin := p.getPort()
if high {
port.BSRR.Set(1 << pin)
} else {
port.BSRR.Set(1 << (pin + 16))
}
}

func (p Pin) Get() bool {
port, pin := p.getPort()
val := port.IDR.Get() & (1 << pin)
return val > 0
}

func (p Pin) Configure(config PinConfig) {
p.enableClock()
port, pin := p.getPort()
pos := (pin % 16) * 2

switch config.Mode {

case PinInputFloating:
port.MODER.ReplaceBits(gpioModeInput, gpioModeMask, pos)
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
case PinInputPulldown:
port.MODER.ReplaceBits(gpioModeInput, gpioModeMask, pos)
port.PUPDR.ReplaceBits(gpioPullDown, gpioPullMask, pos)
case PinInputPullup:
port.MODER.ReplaceBits(gpioModeInput, gpioModeMask, pos)
port.PUPDR.ReplaceBits(gpioPullUp, gpioPullMask, pos)
case PinOutput:
port.MODER.ReplaceBits(gpioModeOutput, gpioModeMask, pos)
port.OTYPER.ReplaceBits(py32.GPIO_OTYPER_OT0_PushPull, py32.GPIO_OTYPER_OT0_Msk, pos>>1)
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
case PinInputAnalog:
port.MODER.ReplaceBits(gpioModeAnalog, gpioModeMask, pos)
port.PUPDR.ReplaceBits(gpioPullFloating, gpioPullMask, pos)
}
}

func (p Pin) enableClock() {
portNo := p.getPortNumber()
py32.RCC.IOPENR.SetBits(1 << portNo)
}
12 changes: 12 additions & 0 deletions src/machine/machine_py32_pin_afrh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build py32 && !no_gpio_afrh

package machine

func (p Pin) SetAltFunc(af uint8) {
port, pin := p.getPort()
if pin >= 8 {
port.AFRH.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
} else {
port.AFRL.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
}
}
8 changes: 8 additions & 0 deletions src/machine/machine_py32_pin_no_afrh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build py32 && no_gpio_afrh

package machine

func (p Pin) SetAltFunc(af uint8) {
port, pin := p.getPort()
port.AFRL.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
}
Loading
Loading