diff --git a/Makefile b/Makefile index d057cbbf..31556bc2 100644 --- a/Makefile +++ b/Makefile @@ -680,6 +680,8 @@ endif @$(MD5SUM) test.hex $(TINYGO) build -size short -o test.hex -target=arduino examples/blinky1 @$(MD5SUM) test.hex + $(TINYGO) build -size short -o test.hex -target=arduino-leonardo examples/blinky1 + @$(MD5SUM) test.hex $(TINYGO) build -size short -o test.hex -target=arduino examples/pwm @$(MD5SUM) test.hex $(TINYGO) build -size short -o test.hex -target=arduino -scheduler=tasks examples/blinky1 diff --git a/src/machine/board_arduino_leonardo.go b/src/machine/board_arduino_leonardo.go new file mode 100644 index 00000000..01f9570b --- /dev/null +++ b/src/machine/board_arduino_leonardo.go @@ -0,0 +1,39 @@ +//go:build arduino_leonardo + +package machine + +// Return the current CPU frequency in hertz. +func CPUFrequency() uint32 { + return 16000000 +} + +// Digital pins, marked as plain numbers on the board. +const ( + D0 = PD2 // RX + D1 = PD3 // TX + D2 = PD1 + D3 = PD0 + D4 = PD4 + D5 = PC6 + D6 = PD7 + D7 = PE6 + D8 = PB4 + D9 = PB5 + D10 = PB6 + D11 = PB7 + D12 = PD6 + D13 = PC7 +) + +// LED on the Arduino +const LED Pin = D13 + +// ADC on the Arduino +const ( + ADC0 Pin = PF7 + ADC1 Pin = PF6 + ADC2 Pin = PF5 + ADC3 Pin = PF4 + ADC4 Pin = PF1 + ADC5 Pin = PF0 +) diff --git a/src/machine/machine_atmega32u4.go b/src/machine/machine_atmega32u4.go new file mode 100644 index 00000000..8b3faabb --- /dev/null +++ b/src/machine/machine_atmega32u4.go @@ -0,0 +1,76 @@ +//go:build avr && atmega32u4 + +package machine + +import ( + "device/avr" + "runtime/volatile" +) + +const ( + // Note: start at port B because there is no port A. + portB Pin = iota * 8 + portC + portD + portE + portF +) + +const ( + PB0 = portB + 0 + PB1 = portB + 1 // peripherals: Timer1 channel A + PB2 = portB + 2 // peripherals: Timer1 channel B + PB3 = portB + 3 // peripherals: Timer2 channel A + PB4 = portB + 4 + PB5 = portB + 5 + PB6 = portB + 6 + PB7 = portB + 7 + PC0 = portC + 0 + PC1 = portC + 1 + PC2 = portC + 2 + PC3 = portC + 3 + PC4 = portC + 4 + PC5 = portC + 5 + PC6 = portC + 6 + PC7 = portC + 7 + PD0 = portD + 0 + PD1 = portD + 1 + PD2 = portD + 2 + PD3 = portD + 3 + PD4 = portD + 4 + PD5 = portD + 5 + PD6 = portD + 6 + PD7 = portD + 7 + PE0 = portE + 0 + PE1 = portE + 1 + PE2 = portE + 2 + PE3 = portE + 3 + PE4 = portE + 4 + PE5 = portE + 5 + PE6 = portE + 6 + PE7 = portE + 7 + PF0 = portF + 0 + PF1 = portF + 1 + PF2 = portF + 2 + PF3 = portF + 3 + PF4 = portF + 4 + PF5 = portF + 5 + PF6 = portF + 6 + PF7 = portF + 7 +) + +// getPortMask returns the PORTx register and mask for the pin. +func (p Pin) getPortMask() (*volatile.Register8, uint8) { + switch { + case p >= PB0 && p <= PB7: // port B + return avr.PORTB, 1 << uint8(p-portB) + case p >= PC0 && p <= PC7: // port C + return avr.PORTC, 1 << uint8(p-portC) + case p >= PD0 && p <= PD7: // port D + return avr.PORTD, 1 << uint8(p-portD) + case p >= PE0 && p <= PE7: // port E + return avr.PORTE, 1 << uint8(p-portE) + default: // port F + return avr.PORTF, 1 << uint8(p-portF) + } +} diff --git a/src/runtime/runtime_atmega.go b/src/runtime/runtime_atmega.go index b2e02445..af547545 100644 --- a/src/runtime/runtime_atmega.go +++ b/src/runtime/runtime_atmega.go @@ -1,4 +1,4 @@ -//go:build avr && atmega +//go:build avr && (atmega || atmega32u4) package runtime diff --git a/targets/arduino-leonardo.json b/targets/arduino-leonardo.json new file mode 100644 index 00000000..17652d85 --- /dev/null +++ b/targets/arduino-leonardo.json @@ -0,0 +1,10 @@ +{ + "inherits": ["atmega32u4"], + "build-tags": ["arduino_leonardo"], + "ldflags": [ + "--defsym=_bootloader_size=512", + "--defsym=_stack_size=512" + ], + "flash-command": "avrdude -c avr109 -p atmega32u4 -b 57600 -P {port} -U flash:w:{hex}:i", + "emulator": "simavr -m atmega32u4 -f 16000000 {}" +} diff --git a/targets/atmega32u4.json b/targets/atmega32u4.json new file mode 100644 index 00000000..7aa2c08b --- /dev/null +++ b/targets/atmega32u4.json @@ -0,0 +1,11 @@ +{ + "inherits": ["avr"], + "cpu": "atmega32u4", + "build-tags": ["atmega32u4", "avr5"], + "serial": "none", + "linkerscript": "src/device/avr/atmega32u4.ld", + "extra-files": [ + "targets/avr.S", + "src/device/avr/atmega32u4.s" + ] +}