|
| 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 | +} |
0 commit comments