Skip to content

Commit 83426ed

Browse files
authored
avr: add Arduino Mega 2560
1 parent 26aba72 commit 83426ed

File tree

7 files changed

+341
-48
lines changed

7 files changed

+341
-48
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// +build arduino_mega2560
2+
3+
package machine
4+
5+
// Return the current CPU frequency in hertz.
6+
func CPUFrequency() uint32 {
7+
return 16000000
8+
}
9+
10+
const (
11+
AREF Pin = NoPin
12+
LED Pin = PB7
13+
14+
A0 Pin = PF0
15+
A1 Pin = PF1
16+
A2 Pin = PF2
17+
A3 Pin = PF3
18+
A4 Pin = PF4
19+
A5 Pin = PF5
20+
A6 Pin = PF6
21+
A7 Pin = PF7
22+
A8 Pin = PK0
23+
A9 Pin = PK1
24+
A10 Pin = PK2
25+
A11 Pin = PK3
26+
A12 Pin = PK4
27+
A13 Pin = PK5
28+
A14 Pin = PK6
29+
A15 Pin = PK7
30+
31+
// Analog Input
32+
ADC0 Pin = PF0
33+
ADC1 Pin = PF1
34+
ADC2 Pin = PF2
35+
ADC3 Pin = PF3
36+
ADC4 Pin = PF4
37+
ADC5 Pin = PF5
38+
ADC6 Pin = PF6
39+
ADC7 Pin = PF7
40+
ADC8 Pin = PK0
41+
ADC9 Pin = PK1
42+
ADC10 Pin = PK2
43+
ADC11 Pin = PK3
44+
ADC12 Pin = PK4
45+
ADC13 Pin = PK5
46+
ADC14 Pin = PK6
47+
ADC15 Pin = PK7
48+
49+
// Digital pins
50+
D0 Pin = PE0
51+
D1 Pin = PE1
52+
D2 Pin = PE6
53+
D3 Pin = PE5
54+
D4 Pin = PG5
55+
D5 Pin = PE3
56+
D6 Pin = PH3
57+
D7 Pin = PH4
58+
D8 Pin = PH5
59+
D9 Pin = PH6
60+
D10 Pin = PB4
61+
D11 Pin = PB5
62+
D12 Pin = PB6
63+
D13 Pin = PB7
64+
D14 Pin = PJ1
65+
D15 Pin = PJ0
66+
D16 Pin = PH1
67+
D17 Pin = PH0
68+
D18 Pin = PD3
69+
D19 Pin = PD2
70+
D20 Pin = PD1
71+
D21 Pin = PD0
72+
D22 Pin = PA0
73+
D23 Pin = PA1
74+
D24 Pin = PA2
75+
D25 Pin = PA3
76+
D26 Pin = PA4
77+
D27 Pin = PA5
78+
D28 Pin = PA6
79+
D29 Pin = PA7
80+
D30 Pin = PC7
81+
D31 Pin = PC6
82+
D32 Pin = PC5
83+
D33 Pin = PC4
84+
D34 Pin = PC3
85+
D35 Pin = PC2
86+
D36 Pin = PC1
87+
D37 Pin = PC0
88+
D38 Pin = PD7
89+
D39 Pin = PG2
90+
D40 Pin = PG1
91+
D41 Pin = PG0
92+
D42 Pin = PL7
93+
D43 Pin = PL6
94+
D44 Pin = PL5
95+
D45 Pin = PL4
96+
D46 Pin = PL3
97+
D47 Pin = PL2
98+
D48 Pin = PL1
99+
D49 Pin = PL0
100+
D50 Pin = PB3
101+
D51 Pin = PB2
102+
D52 Pin = PB1
103+
D53 Pin = PB0
104+
)

src/machine/machine_atmega.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,8 @@ package machine
55
import (
66
"device/avr"
77
"runtime/interrupt"
8-
"runtime/volatile"
98
)
109

11-
// Configure sets the pin to input or output.
12-
func (p Pin) Configure(config PinConfig) {
13-
if config.Mode == PinOutput { // set output bit
14-
if p < 8 {
15-
avr.DDRD.SetBits(1 << uint8(p))
16-
} else if p < 14 {
17-
avr.DDRB.SetBits(1 << uint8(p-8))
18-
} else {
19-
avr.DDRC.SetBits(1 << uint8(p-14))
20-
}
21-
} else { // configure input: clear output bit
22-
if p < 8 {
23-
avr.DDRD.ClearBits(1 << uint8(p))
24-
} else if p < 14 {
25-
avr.DDRB.ClearBits(1 << uint8(p-8))
26-
} else {
27-
avr.DDRC.ClearBits(1 << uint8(p-14))
28-
}
29-
}
30-
}
31-
32-
// Get returns the current value of a GPIO pin.
33-
func (p Pin) Get() bool {
34-
if p < 8 {
35-
val := avr.PIND.Get() & (1 << uint8(p))
36-
return (val > 0)
37-
} else if p < 14 {
38-
val := avr.PINB.Get() & (1 << uint8(p-8))
39-
return (val > 0)
40-
} else {
41-
val := avr.PINC.Get() & (1 << uint8(p-14))
42-
return (val > 0)
43-
}
44-
}
45-
46-
func (p Pin) getPortMask() (*volatile.Register8, uint8) {
47-
if p < 8 {
48-
return avr.PORTD, 1 << uint8(p)
49-
} else if p < 14 {
50-
return avr.PORTB, 1 << uint8(p-8)
51-
} else {
52-
return avr.PORTC, 1 << uint8(p-14)
53-
}
54-
}
55-
5610
// InitPWM initializes the registers needed for PWM.
5711
func InitPWM() {
5812
// use waveform generation

src/machine/machine_atmega1284p.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22

33
package machine
44

5-
import "device/avr"
5+
import (
6+
"device/avr"
7+
"runtime/volatile"
8+
)
69

710
const irq_USART0_RX = avr.IRQ_USART0_RX
811

912
// Return the current CPU frequency in hertz.
1013
func CPUFrequency() uint32 {
1114
return 20000000
1215
}
16+
17+
func (p Pin) getPortMask() (*volatile.Register8, uint8) {
18+
if p < 8 {
19+
return avr.PORTD, 1 << uint8(p)
20+
} else if p < 14 {
21+
return avr.PORTB, 1 << uint8(p-8)
22+
} else {
23+
return avr.PORTC, 1 << uint8(p-14)
24+
}
25+
}

src/machine/machine_atmega2560.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// +build avr,atmega2560
2+
3+
package machine
4+
5+
import (
6+
"device/avr"
7+
"runtime/volatile"
8+
)
9+
10+
const irq_USART0_RX = avr.IRQ_USART0_RX
11+
12+
const (
13+
portA Pin = iota * 8
14+
portB
15+
portC
16+
portD
17+
portE
18+
portF
19+
portG
20+
portH
21+
portJ
22+
portK
23+
portL
24+
)
25+
26+
const (
27+
PA0 = portA + 0
28+
PA1 = portA + 1
29+
PA2 = portA + 2
30+
PA3 = portA + 3
31+
PA4 = portA + 4
32+
PA5 = portA + 5
33+
PA6 = portA + 6
34+
PA7 = portA + 7
35+
PB0 = portB + 0
36+
PB1 = portB + 1
37+
PB2 = portB + 2
38+
PB3 = portB + 3
39+
PB4 = portB + 4
40+
PB5 = portB + 5
41+
PB6 = portB + 6
42+
PB7 = portB + 7
43+
PC0 = portC + 0
44+
PC1 = portC + 1
45+
PC2 = portC + 2
46+
PC3 = portC + 3
47+
PC4 = portC + 4
48+
PC5 = portC + 5
49+
PC6 = portC + 6
50+
PC7 = portC + 7
51+
PD0 = portD + 0
52+
PD1 = portD + 1
53+
PD2 = portD + 2
54+
PD3 = portD + 3
55+
PD7 = portD + 7
56+
PE0 = portE + 0
57+
PE1 = portE + 1
58+
PE3 = portE + 2
59+
PE5 = portE + 5
60+
PE6 = portE + 6
61+
PF0 = portF + 0
62+
PF1 = portF + 1
63+
PF2 = portF + 2
64+
PF3 = portF + 3
65+
PF4 = portF + 4
66+
PF5 = portF + 5
67+
PF6 = portF + 6
68+
PF7 = portF + 7
69+
PG0 = portG + 0
70+
PG1 = portG + 1
71+
PG2 = portG + 2
72+
PG5 = portG + 5
73+
PH0 = portH + 0
74+
PH1 = portH + 1
75+
PH3 = portH + 3
76+
PH4 = portH + 4
77+
PH5 = portH + 5
78+
PH6 = portH + 6
79+
PJ0 = portJ + 0
80+
PJ1 = portJ + 1
81+
PK0 = portK + 0
82+
PK1 = portK + 1
83+
PK2 = portK + 2
84+
PK3 = portK + 3
85+
PK4 = portH + 4
86+
PK5 = portH + 5
87+
PK6 = portH + 6
88+
PK7 = portH + 7
89+
PL0 = portL + 0
90+
PL1 = portL + 1
91+
PL2 = portL + 2
92+
PL3 = portL + 3
93+
PL4 = portL + 4
94+
PL5 = portL + 5
95+
PL6 = portL + 6
96+
PL7 = portE + 7
97+
)
98+
99+
// Configure sets the pin to input or output.
100+
func (p Pin) Configure(config PinConfig) {
101+
register, _, mask := p.getRegisterPortMask()
102+
if config.Mode == PinOutput { // set output bit
103+
register.SetBits(mask)
104+
} else { // configure input: clear output bit
105+
register.ClearBits(mask)
106+
}
107+
}
108+
109+
// Get returns the current value of a GPIO pin.
110+
func (p Pin) Get() bool {
111+
_, port, mask := p.getRegisterPortMask()
112+
return (port.Get() & mask) > 0
113+
}
114+
115+
func (p Pin) getPortMask() (*volatile.Register8, uint8) {
116+
_, port, mask := p.getRegisterPortMask()
117+
return port, mask
118+
}
119+
120+
// getRegisterPortMask returns the register, port, and mask for the pin
121+
func (p Pin) getRegisterPortMask() (*volatile.Register8, *volatile.Register8, uint8) {
122+
switch {
123+
case p >= PA0 && p <= PA7:
124+
return avr.DDRA, avr.PORTA, 1 << uint8(p-portA)
125+
case p >= PB0 && p <= PB7:
126+
return avr.DDRB, avr.PORTB, 1 << uint8(p-portB)
127+
case p >= PC0 && p <= PC7:
128+
return avr.DDRC, avr.PORTC, 1 << uint8(p-portC)
129+
case p >= PD0 && p <= PD7:
130+
return avr.DDRD, avr.PORTD, 1 << uint8(p-portD)
131+
case p >= PE0 && p <= PE6:
132+
return avr.DDRE, avr.PORTE, 1 << uint8(p-portE)
133+
case p >= PF0 && p <= PF7:
134+
return avr.DDRF, avr.PORTF, 1 << uint8(p-portF)
135+
case p >= PG0 && p <= PG5:
136+
return avr.DDRG, avr.PORTG, 1 << uint8(p-portG)
137+
case p >= PH0 && p <= PH6:
138+
return avr.DDRH, avr.PORTH, 1 << uint8(p-portH)
139+
case p >= PJ0 && p <= PJ1:
140+
return avr.DDRJ, avr.PORTJ, 1 << uint8(p-portJ)
141+
case p >= PK0 && p <= PK7:
142+
return avr.DDRK, avr.PORTK, 1 << uint8(p-portK)
143+
case p >= PL0 && p <= PL7:
144+
return avr.DDRL, avr.PORTL, 1 << uint8(p-portL)
145+
default:
146+
return avr.DDRB, avr.PORTA, 255
147+
}
148+
}

src/machine/machine_atmega328p.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,54 @@
22

33
package machine
44

5-
import "device/avr"
5+
import (
6+
"device/avr"
7+
"runtime/volatile"
8+
)
69

710
const irq_USART0_RX = avr.IRQ_USART_RX
11+
12+
// Configure sets the pin to input or output.
13+
func (p Pin) Configure(config PinConfig) {
14+
if config.Mode == PinOutput { // set output bit
15+
if p < 8 {
16+
avr.DDRD.SetBits(1 << uint8(p))
17+
} else if p < 14 {
18+
avr.DDRB.SetBits(1 << uint8(p-8))
19+
} else {
20+
avr.DDRC.SetBits(1 << uint8(p-14))
21+
}
22+
} else { // configure input: clear output bit
23+
if p < 8 {
24+
avr.DDRD.ClearBits(1 << uint8(p))
25+
} else if p < 14 {
26+
avr.DDRB.ClearBits(1 << uint8(p-8))
27+
} else {
28+
avr.DDRC.ClearBits(1 << uint8(p-14))
29+
}
30+
}
31+
}
32+
33+
// Get returns the current value of a GPIO pin.
34+
func (p Pin) Get() bool {
35+
if p < 8 {
36+
val := avr.PIND.Get() & (1 << uint8(p))
37+
return (val > 0)
38+
} else if p < 14 {
39+
val := avr.PINB.Get() & (1 << uint8(p-8))
40+
return (val > 0)
41+
} else {
42+
val := avr.PINC.Get() & (1 << uint8(p-14))
43+
return (val > 0)
44+
}
45+
}
46+
47+
func (p Pin) getPortMask() (*volatile.Register8, uint8) {
48+
if p < 8 {
49+
return avr.PORTD, 1 << uint8(p)
50+
} else if p < 14 {
51+
return avr.PORTB, 1 << uint8(p-8)
52+
} else {
53+
return avr.PORTC, 1 << uint8(p-14)
54+
}
55+
}

targets/arduino-mega2560.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"inherits": ["atmega2560"],
3+
"build-tags": ["arduino_mega2560"],
4+
"ldflags": [
5+
"-Wl,--defsym=_bootloader_size=8192"
6+
],
7+
"flash-command":"avrdude -c wiring -b 115200 -p atmega2560 -P {port} -U flash:w:{hex} -v -D"
8+
}

0 commit comments

Comments
 (0)