Skip to content

Commit 6b89404

Browse files
aykevldeadprogram
authored andcommitted
avr: use standard pin numbering
This commit changes pin numbering for atmega328 based boards (Uno, Nano) to use the standard format, where pin number is determined by the pin/port. Previously, pin numbers were based on what the Uno uses, which does not seem to have a clear pattern. One difference is that counting starts at port B, as there is no port A. So PB0 is 0, PB1 is 1… PC0 is 8. This commit also moves PWM code to the atmega328 file, as it may not be generic to all ATmega chips.
1 parent 2c71f08 commit 6b89404

File tree

8 files changed

+195
-120
lines changed

8 files changed

+195
-120
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ tinygo-test:
182182
.PHONY: smoketest
183183
smoketest:
184184
$(TINYGO) version
185-
# test all examples
185+
# test all examples (except pwm)
186186
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/blinky1
187187
@$(MD5SUM) test.hex
188188
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/adc
@@ -203,8 +203,6 @@ smoketest:
203203
@$(MD5SUM) test.hex
204204
$(TINYGO) build -size short -o test.hex -target=microbit examples/microbit-blink
205205
@$(MD5SUM) test.hex
206-
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/pwm
207-
@$(MD5SUM) test.hex
208206
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/serial
209207
@$(MD5SUM) test.hex
210208
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/systick
@@ -296,6 +294,8 @@ ifneq ($(AVR), 0)
296294
@$(MD5SUM) test.hex
297295
$(TINYGO) build -size short -o test.hex -target=arduino examples/blinky1
298296
@$(MD5SUM) test.hex
297+
$(TINYGO) build -size short -o test.hex -target=arduino examples/pwm
298+
@$(MD5SUM) test.hex
299299
$(TINYGO) build -size short -o test.hex -target=arduino -scheduler=tasks examples/blinky1
300300
@$(MD5SUM) test.hex
301301
$(TINYGO) build -size short -o test.hex -target=arduino-nano examples/blinky1

src/examples/button/button.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import (
55
"time"
66
)
77

8-
// This example assumes that the button is connected to pin 8. Change the value
9-
// below to use a different pin.
108
const (
119
led = machine.LED
12-
button = machine.Pin(8)
10+
button = machine.BUTTON
1311
)
1412

1513
func main() {

src/examples/pwm/pwm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
// This example assumes that an RGB LED is connected to pins 3, 5 and 6 on an Arduino.
99
// Change the values below to use different pins.
1010
const (
11-
redPin = 3
12-
greenPin = 5
13-
bluePin = 6
11+
redPin = machine.D3
12+
greenPin = machine.D5
13+
bluePin = machine.D6
1414
)
1515

1616
// cycleColor is just a placeholder until math/rand or some equivalent is working.

src/machine/board_arduino.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,39 @@ func CPUFrequency() uint32 {
77
return 16000000
88
}
99

10+
// Digital pins, marked as plain numbers on the board.
11+
const (
12+
D0 = PD0 // RX
13+
D1 = PD1 // TX
14+
D2 = PD2
15+
D3 = PD3
16+
D4 = PD4
17+
D5 = PD5
18+
D6 = PD6
19+
D7 = PD7
20+
D8 = PB0
21+
D9 = PB1
22+
D10 = PB2
23+
D11 = PB3
24+
D12 = PB4
25+
D13 = PB5
26+
)
27+
1028
// LED on the Arduino
11-
const LED Pin = 13
29+
const LED Pin = D13
1230

1331
// ADC on the Arduino
1432
const (
15-
ADC0 Pin = 0
16-
ADC1 Pin = 1
17-
ADC2 Pin = 2
18-
ADC3 Pin = 3
19-
ADC4 Pin = 4 // Used by TWI for SDA
20-
ADC5 Pin = 5 // Used by TWI for SCL
33+
ADC0 Pin = PC0
34+
ADC1 Pin = PC1
35+
ADC2 Pin = PC2
36+
ADC3 Pin = PC3
37+
ADC4 Pin = PC4 // Used by TWI for SDA
38+
ADC5 Pin = PC5 // Used by TWI for SCL
2139
)
2240

2341
// UART pins
2442
const (
25-
UART_TX_PIN Pin = 1
26-
UART_RX_PIN Pin = 0
43+
UART_TX_PIN Pin = PD1
44+
UART_RX_PIN Pin = PD0
2745
)

src/machine/board_arduino_nano.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,39 @@ func CPUFrequency() uint32 {
77
return 16000000
88
}
99

10+
// Digital pins.
11+
const (
12+
D0 = PD0 // RX0
13+
D1 = PD1 // TX1
14+
D2 = PD2
15+
D3 = PD3
16+
D4 = PD4
17+
D5 = PD5
18+
D6 = PD6
19+
D7 = PD7
20+
D8 = PB0
21+
D9 = PB1
22+
D10 = PB2
23+
D11 = PB3
24+
D12 = PB4
25+
D13 = PB5
26+
)
27+
1028
// LED on the Arduino
11-
const LED Pin = 13
29+
const LED Pin = D13
1230

1331
// ADC on the Arduino
1432
const (
15-
ADC0 Pin = 0
16-
ADC1 Pin = 1
17-
ADC2 Pin = 2
18-
ADC3 Pin = 3
19-
ADC4 Pin = 4 // Used by TWI for SDA
20-
ADC5 Pin = 5 // Used by TWI for SCL
33+
ADC0 Pin = PC0
34+
ADC1 Pin = PC1
35+
ADC2 Pin = PC2
36+
ADC3 Pin = PC3
37+
ADC4 Pin = PC4 // Used by TWI for SDA
38+
ADC5 Pin = PC5 // Used by TWI for SCL
2139
)
2240

2341
// UART pins
2442
const (
25-
UART_TX_PIN Pin = 1
26-
UART_RX_PIN Pin = 0
43+
UART_TX_PIN Pin = PD1
44+
UART_RX_PIN Pin = PD0
2745
)

src/machine/board_atmega328p.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// +build avr,atmega328p arduino arduino_nano
2+
3+
package machine
4+
5+
const (
6+
// Note: start at port B because there is no port A.
7+
portB Pin = iota * 8
8+
portC
9+
portD
10+
)
11+
12+
const (
13+
PB0 = portB + 0
14+
PB1 = portB + 1
15+
PB2 = portB + 2
16+
PB3 = portB + 3
17+
PB4 = portB + 4
18+
PB5 = portB + 5
19+
PB6 = portB + 6
20+
PB7 = portB + 7
21+
PC0 = portC + 0
22+
PC1 = portC + 1
23+
PC2 = portC + 2
24+
PC3 = portC + 3
25+
PC4 = portC + 4
26+
PC5 = portC + 5
27+
PC6 = portC + 6
28+
PC7 = portC + 7
29+
PD0 = portD + 0
30+
PD1 = portD + 1
31+
PD2 = portD + 2
32+
PD3 = portD + 3
33+
PD4 = portD + 4
34+
PD5 = portD + 5
35+
PD6 = portD + 6
36+
PD7 = portD + 7
37+
)

src/machine/machine_atmega.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,6 @@ import (
77
"runtime/interrupt"
88
)
99

10-
// InitPWM initializes the registers needed for PWM.
11-
func InitPWM() {
12-
// use waveform generation
13-
avr.TCCR0A.SetBits(avr.TCCR0A_WGM00)
14-
15-
// set timer 0 prescale factor to 64
16-
avr.TCCR0B.SetBits(avr.TCCR0B_CS01 | avr.TCCR0B_CS00)
17-
18-
// set timer 1 prescale factor to 64
19-
avr.TCCR1B.SetBits(avr.TCCR1B_CS11)
20-
21-
// put timer 1 in 8-bit phase correct pwm mode
22-
avr.TCCR1A.SetBits(avr.TCCR1A_WGM10)
23-
24-
// set timer 2 prescale factor to 64
25-
avr.TCCR2B.SetBits(avr.TCCR2B_CS22)
26-
27-
// configure timer 2 for phase correct pwm (8-bit)
28-
avr.TCCR2A.SetBits(avr.TCCR2A_WGM20)
29-
}
30-
31-
// Configure configures a PWM pin for output.
32-
func (pwm PWM) Configure() {
33-
if pwm.Pin < 8 {
34-
avr.DDRD.SetBits(1 << uint8(pwm.Pin))
35-
} else {
36-
avr.DDRB.SetBits(1 << uint8(pwm.Pin-8))
37-
}
38-
}
39-
40-
// Set turns on the duty cycle for a PWM pin using the provided value. On the AVR this is normally a
41-
// 8-bit value ranging from 0 to 255.
42-
func (pwm PWM) Set(value uint16) {
43-
value8 := uint8(value >> 8)
44-
switch pwm.Pin {
45-
case 3:
46-
// connect pwm to pin on timer 2, channel B
47-
avr.TCCR2A.SetBits(avr.TCCR2A_COM2B1)
48-
avr.OCR2B.Set(value8) // set pwm duty
49-
case 5:
50-
// connect pwm to pin on timer 0, channel B
51-
avr.TCCR0A.SetBits(avr.TCCR0A_COM0B1)
52-
avr.OCR0B.Set(value8) // set pwm duty
53-
case 6:
54-
// connect pwm to pin on timer 0, channel A
55-
avr.TCCR0A.SetBits(avr.TCCR0A_COM0A1)
56-
avr.OCR0A.Set(value8) // set pwm duty
57-
case 9:
58-
// connect pwm to pin on timer 1, channel A
59-
avr.TCCR1A.SetBits(avr.TCCR1A_COM1A1)
60-
// this is a 16-bit value, but we only currently allow the low order bits to be set
61-
avr.OCR1AL.Set(value8) // set pwm duty
62-
case 10:
63-
// connect pwm to pin on timer 1, channel B
64-
avr.TCCR1A.SetBits(avr.TCCR1A_COM1B1)
65-
// this is a 16-bit value, but we only currently allow the low order bits to be set
66-
avr.OCR1BL.Set(value8) // set pwm duty
67-
case 11:
68-
// connect pwm to pin on timer 2, channel A
69-
avr.TCCR2A.SetBits(avr.TCCR2A_COM2A1)
70-
avr.OCR2A.Set(value8) // set pwm duty
71-
default:
72-
panic("Invalid PWM pin")
73-
}
74-
}
75-
7610
// I2CConfig is used to store config info for I2C.
7711
type I2CConfig struct {
7812
Frequency uint32

0 commit comments

Comments
 (0)