machine: refactor pins to be of Pin type

Этот коммит содержится в:
Ayke van Laethem 2019-04-28 18:49:17 +02:00 коммит произвёл Ron Evans
родитель 421ef04efb
коммит 94b8214529
38 изменённых файлов: 590 добавлений и 605 удалений

Просмотреть файл

@ -17,8 +17,8 @@ import (
) )
func main() { func main() {
led := machine.GPIO{machine.LED} led := machine.LED
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led.Configure(machine.PinConfig{Mode: machine.PinOutput})
for { for {
led.Low() led.Low()
time.Sleep(time.Millisecond * 1000) time.Sleep(time.Millisecond * 1000)

Просмотреть файл

@ -11,8 +11,8 @@ import (
func main() { func main() {
machine.InitADC() machine.InitADC()
led := machine.GPIO{machine.LED} led := machine.LED
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led.Configure(machine.PinConfig{Mode: machine.PinOutput})
sensor := machine.ADC{machine.ADC2} sensor := machine.ADC{machine.ADC2}
sensor.Configure() sensor.Configure()

Просмотреть файл

@ -8,8 +8,8 @@ import (
) )
func main() { func main() {
led := machine.GPIO{machine.LED} led := machine.LED
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led.Configure(machine.PinConfig{Mode: machine.PinOutput})
for { for {
led.Low() led.Low()
time.Sleep(time.Millisecond * 500) time.Sleep(time.Millisecond * 500)

Просмотреть файл

@ -16,8 +16,8 @@ func main() {
} }
func led1() { func led1() {
led := machine.GPIO{machine.LED} led := machine.LED1
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led.Configure(machine.PinConfig{Mode: machine.PinOutput})
for { for {
println("+") println("+")
led.Low() led.Low()
@ -30,8 +30,8 @@ func led1() {
} }
func led2() { func led2() {
led := machine.GPIO{machine.LED2} led := machine.LED2
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led.Configure(machine.PinConfig{Mode: machine.PinOutput})
for { for {
println(" +") println(" +")
led.Low() led.Low()

Просмотреть файл

@ -7,14 +7,14 @@ import (
// This example assumes that the button is connected to pin 8. Change the value // This example assumes that the button is connected to pin 8. Change the value
// below to use a different pin. // below to use a different pin.
const buttonPin = 8 const (
led = machine.LED
button = machine.Pin(8)
)
func main() { func main() {
led := machine.GPIO{machine.LED} led.Configure(machine.PinConfig{Mode: machine.PinOutput})
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) button.Configure(machine.PinConfig{Mode: machine.PinInput})
button := machine.GPIO{buttonPin}
button.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT})
for { for {
if button.Get() { if button.Get() {

Просмотреть файл

@ -8,29 +8,29 @@ import (
// This example assumes that you are using the pca10040 board // This example assumes that you are using the pca10040 board
func main() { func main() {
led1 := machine.GPIO{machine.LED1} led1 := machine.LED1
led1.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led1.Configure(machine.PinConfig{Mode: machine.PinOutput})
led2 := machine.GPIO{machine.LED2} led2 := machine.LED2
led2.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led2.Configure(machine.PinConfig{Mode: machine.PinOutput})
led3 := machine.GPIO{machine.LED3} led3 := machine.LED3
led3.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led3.Configure(machine.PinConfig{Mode: machine.PinOutput})
led4 := machine.GPIO{machine.LED4} led4 := machine.LED4
led4.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) led4.Configure(machine.PinConfig{Mode: machine.PinOutput})
button1 := machine.GPIO{machine.BUTTON1} button1 := machine.BUTTON1
button1.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP}) button1.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
button2 := machine.GPIO{machine.BUTTON2} button2 := machine.BUTTON2
button2.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP}) button2.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
button3 := machine.GPIO{machine.BUTTON3} button3 := machine.BUTTON3
button3.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP}) button3.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
button4 := machine.GPIO{machine.BUTTON4} button4 := machine.BUTTON4
button4.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP}) button4.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
for { for {
led1.Set(button1.Get()) led1.Set(button1.Get())

Просмотреть файл

@ -9,9 +9,9 @@ import (
// change these to test a different UART or pins if available // change these to test a different UART or pins if available
var ( var (
uart = machine.UART0 uart = machine.UART0
tx uint8 = machine.UART_TX_PIN tx = machine.UART_TX_PIN
rx uint8 = machine.UART_RX_PIN rx = machine.UART_RX_PIN
) )
func main() { func main() {

Просмотреть файл

@ -8,19 +8,17 @@ import (
"time" "time"
) )
// CS_PIN is the pin used for Chip Select (CS). Change to whatever is in use on your board. // cs is the pin used for Chip Select (CS). Change to whatever is in use on your board.
const CS_PIN = 3 const cs = machine.Pin(3)
var ( var (
tx []byte tx []byte
rx []byte rx []byte
val, result uint16 val, result uint16
cs machine.GPIO
) )
func main() { func main() {
cs = machine.GPIO{CS_PIN} cs.Configure(machine.PinConfig{Mode: machine.PinOutput})
cs.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
machine.SPI0.Configure(machine.SPIConfig{ machine.SPI0.Configure(machine.SPIConfig{
Frequency: 4000000, Frequency: 4000000,

Просмотреть файл

@ -9,10 +9,10 @@ import (
// The LED matrix in the micro:bit is a multiplexed display: https://en.wikipedia.org/wiki/Multiplexed_display // The LED matrix in the micro:bit is a multiplexed display: https://en.wikipedia.org/wiki/Multiplexed_display
// Driver for easier control: https://github.com/tinygo-org/drivers/tree/master/microbitmatrix // Driver for easier control: https://github.com/tinygo-org/drivers/tree/master/microbitmatrix
func main() { func main() {
ledrow := machine.GPIO{machine.LED_ROW_1} ledrow := machine.LED_ROW_1
ledrow.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) ledrow.Configure(machine.PinConfig{Mode: machine.PinOutput})
ledcol := machine.GPIO{machine.LED_COL_1} ledcol := machine.LED_COL_1
ledcol.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) ledcol.Configure(machine.PinConfig{Mode: machine.PinOutput})
ledcol.Low() ledcol.Low()
for { for {
ledrow.Low() ledrow.Low()

Просмотреть файл

@ -5,20 +5,20 @@ package machine
const CPU_FREQUENCY = 16000000 const CPU_FREQUENCY = 16000000
// LED on the Arduino // LED on the Arduino
const LED = 13 const LED Pin = 13
// ADC on the Arduino // ADC on the Arduino
const ( const (
ADC0 = 0 ADC0 Pin = 0
ADC1 = 1 ADC1 Pin = 1
ADC2 = 2 ADC2 Pin = 2
ADC3 = 3 ADC3 Pin = 3
ADC4 = 4 // Used by TWI for SDA ADC4 Pin = 4 // Used by TWI for SDA
ADC5 = 5 // Used by TWI for SCL ADC5 Pin = 5 // Used by TWI for SCL
) )
// UART pins // UART pins
const ( const (
UART_TX_PIN = 1 UART_TX_PIN Pin = 1
UART_RX_PIN = 0 UART_RX_PIN Pin = 0
) )

Просмотреть файл

@ -17,7 +17,7 @@ const (
D8 = PB23 D8 = PB23
D9 = PA06 D9 = PA06
D10 = PA07 D10 = PA07
D11 = 0xff // does not seem to exist D11 = NoPin // does not seem to exist
D12 = PA02 D12 = PA02
D13 = PA17 // PWM available D13 = PA17 // PWM available
) )
@ -80,12 +80,12 @@ var (
I2C0 = I2C{Bus: sam.SERCOM5_I2CM, I2C0 = I2C{Bus: sam.SERCOM5_I2CM,
SDA: SDA_PIN, SDA: SDA_PIN,
SCL: SCL_PIN, SCL: SCL_PIN,
PinMode: GPIO_SERCOM} PinMode: PinSERCOM}
// internal device // internal device
I2C1 = I2C{Bus: sam.SERCOM1_I2CM, I2C1 = I2C{Bus: sam.SERCOM1_I2CM,
SDA: SDA1_PIN, SDA: SDA1_PIN,
SCL: SCL1_PIN, SCL: SCL1_PIN,
PinMode: GPIO_SERCOM_ALT} PinMode: PinSERCOMAlt}
) )
// SPI pins (internal flash) // SPI pins (internal flash)
@ -104,7 +104,7 @@ var (
const ( const (
I2S_SCK_PIN = PA10 I2S_SCK_PIN = PA10
I2S_SD_PIN = PA08 I2S_SD_PIN = PA08
I2S_WS_PIN = 0xff // no WS, instead uses SCK to sync I2S_WS_PIN = NoPin // no WS, instead uses SCK to sync
) )
// I2S on the Circuit Playground Express. // I2S on the Circuit Playground Express.

Просмотреть файл

@ -3,5 +3,5 @@
package machine package machine
const ( const (
LED = 1 LED Pin = 1
) )

Просмотреть файл

@ -6,14 +6,14 @@ import "device/sam"
// GPIO Pins // GPIO Pins
const ( const (
D0 = PA11 // UART0 RX D0 = PA11 // UART0 RX
D1 = PA10 // UART0 TX D1 = PA10 // UART0 TX
D2 = 0xff // does not seem to exist D2 = NoPin // does not seem to exist
D3 = PA09 D3 = PA09
D4 = PA08 D4 = PA08
D5 = PA15 // PWM available D5 = PA15 // PWM available
D6 = PA20 // PWM available D6 = PA20 // PWM available
D7 = 0xff // does not seem to exist D7 = NoPin // does not seem to exist
D8 = PA06 D8 = PA06
D9 = PA07 // PWM available D9 = PA07 // PWM available
D10 = PA18 // can be used for PWM or UART1 TX D10 = PA18 // can be used for PWM or UART1 TX
@ -59,7 +59,7 @@ var (
I2C0 = I2C{Bus: sam.SERCOM3_I2CM, I2C0 = I2C{Bus: sam.SERCOM3_I2CM,
SDA: SDA_PIN, SDA: SDA_PIN,
SCL: SCL_PIN, SCL: SCL_PIN,
PinMode: GPIO_SERCOM} PinMode: PinSERCOM}
) )
// SPI pins // SPI pins
@ -78,5 +78,5 @@ var (
const ( const (
I2S_SCK_PIN = PA10 I2S_SCK_PIN = PA10
I2S_SD_PIN = PA08 I2S_SD_PIN = PA08
I2S_WS_PIN = 0xff // TODO: figure out what this is on Feather M0. I2S_WS_PIN = NoPin // TODO: figure out what this is on Feather M0.
) )

Просмотреть файл

@ -59,7 +59,7 @@ var (
I2C0 = I2C{Bus: sam.SERCOM3_I2CM, I2C0 = I2C{Bus: sam.SERCOM3_I2CM,
SDA: SDA_PIN, SDA: SDA_PIN,
SCL: SCL_PIN, SCL: SCL_PIN,
PinMode: GPIO_SERCOM} PinMode: PinSERCOM}
) )
// SPI pins // SPI pins
@ -78,7 +78,7 @@ var (
const ( const (
I2S_SCK_PIN = PA10 I2S_SCK_PIN = PA10
I2S_SD_PIN = PA08 I2S_SD_PIN = PA08
I2S_WS_PIN = 0xff // TODO: figure out what this is on ItsyBitsy M0. I2S_WS_PIN = NoPin // TODO: figure out what this is on ItsyBitsy M0.
) )
// I2S on the ItsyBitsy M0. // I2S on the ItsyBitsy M0.

Просмотреть файл

@ -7,70 +7,70 @@ const HasLowFrequencyCrystal = false
// Buttons on the micro:bit (A and B) // Buttons on the micro:bit (A and B)
const ( const (
BUTTON = BUTTONA BUTTON Pin = BUTTONA
BUTTONA = 17 BUTTONA Pin = 17
BUTTONB = 26 BUTTONB Pin = 26
) )
// UART pins // UART pins
const ( const (
UART_TX_PIN = 24 UART_TX_PIN Pin = 24
UART_RX_PIN = 25 UART_RX_PIN Pin = 25
) )
// ADC pins // ADC pins
const ( const (
ADC0 = 3 // P0 on the board ADC0 Pin = 3 // P0 on the board
ADC1 = 2 // P1 on the board ADC1 Pin = 2 // P1 on the board
ADC2 = 1 // P2 on the board ADC2 Pin = 1 // P2 on the board
) )
// I2C pins // I2C pins
const ( const (
SDA_PIN = 30 // P20 on the board SDA_PIN Pin = 30 // P20 on the board
SCL_PIN = 0 // P19 on the board SCL_PIN Pin = 0 // P19 on the board
) )
// SPI pins // SPI pins
const ( const (
SPI0_SCK_PIN = 23 // P13 on the board SPI0_SCK_PIN Pin = 23 // P13 on the board
SPI0_MOSI_PIN = 21 // P15 on the board SPI0_MOSI_PIN Pin = 21 // P15 on the board
SPI0_MISO_PIN = 22 // P14 on the board SPI0_MISO_PIN Pin = 22 // P14 on the board
) )
// GPIO/Analog pins // GPIO/Analog pins
const ( const (
P0 = 3 P0 Pin = 3
P1 = 2 P1 Pin = 2
P2 = 1 P2 Pin = 1
P3 = 4 P3 Pin = 4
P4 = 5 P4 Pin = 5
P5 = 17 P5 Pin = 17
P6 = 12 P6 Pin = 12
P7 = 11 P7 Pin = 11
P8 = 18 P8 Pin = 18
P9 = 10 P9 Pin = 10
P10 = 6 P10 Pin = 6
P11 = 26 P11 Pin = 26
P12 = 20 P12 Pin = 20
P13 = 23 P13 Pin = 23
P14 = 22 P14 Pin = 22
P15 = 21 P15 Pin = 21
P16 = 16 P16 Pin = 16
) )
// LED matrix pins // LED matrix pins
const ( const (
LED_COL_1 = 4 LED_COL_1 Pin = 4
LED_COL_2 = 5 LED_COL_2 Pin = 5
LED_COL_3 = 6 LED_COL_3 Pin = 6
LED_COL_4 = 7 LED_COL_4 Pin = 7
LED_COL_5 = 8 LED_COL_5 Pin = 8
LED_COL_6 = 9 LED_COL_6 Pin = 9
LED_COL_7 = 10 LED_COL_7 Pin = 10
LED_COL_8 = 11 LED_COL_8 Pin = 11
LED_COL_9 = 12 LED_COL_9 Pin = 12
LED_ROW_1 = 13 LED_ROW_1 Pin = 13
LED_ROW_2 = 14 LED_ROW_2 Pin = 14
LED_ROW_3 = 15 LED_ROW_3 Pin = 15
) )

Просмотреть файл

@ -6,27 +6,27 @@ const HasLowFrequencyCrystal = true
// LEDs on the nrf52840-mdk (nRF52840 dev board) // LEDs on the nrf52840-mdk (nRF52840 dev board)
const ( const (
LED = LED_GREEN LED Pin = LED_GREEN
LED_GREEN = 22 LED_GREEN Pin = 22
LED_RED = 23 LED_RED Pin = 23
LED_BLUE = 24 LED_BLUE Pin = 24
) )
// UART pins // UART pins
const ( const (
UART_TX_PIN = 20 UART_TX_PIN Pin = 20
UART_RX_PIN = 19 UART_RX_PIN Pin = 19
) )
// I2C pins (unused) // I2C pins (unused)
const ( const (
SDA_PIN = 0xff SDA_PIN = NoPin
SCL_PIN = 0xff SCL_PIN = NoPin
) )
// SPI pins (unused) // SPI pins (unused)
const ( const (
SPI0_SCK_PIN = 0 SPI0_SCK_PIN = NoPin
SPI0_MOSI_PIN = 0 SPI0_MOSI_PIN = NoPin
SPI0_MISO_PIN = 0 SPI0_MISO_PIN = NoPin
) )

Просмотреть файл

@ -10,30 +10,30 @@ const HasLowFrequencyCrystal = true
// LED on the pca10031 // LED on the pca10031
const ( const (
LED = LED_RED LED Pin = LED_RED
LED1 = LED_RED LED1 Pin = LED_RED
LED2 = LED_GREEN LED2 Pin = LED_GREEN
LED3 = LED_BLUE LED3 Pin = LED_BLUE
LED_RED = 21 LED_RED Pin = 21
LED_GREEN = 22 LED_GREEN Pin = 22
LED_BLUE = 23 LED_BLUE Pin = 23
) )
// UART pins // UART pins
const ( const (
UART_TX_PIN = 9 UART_TX_PIN Pin = 9
UART_RX_PIN = 11 UART_RX_PIN Pin = 11
) )
// I2C pins (disabled) // I2C pins (disabled)
const ( const (
SDA_PIN = 0xff SDA_PIN = NoPin
SCL_PIN = 0xff SCL_PIN = NoPin
) )
// SPI pins (unused) // SPI pins (unused)
const ( const (
SPI0_SCK_PIN = 0 SPI0_SCK_PIN = NoPin
SPI0_MOSI_PIN = 0 SPI0_MOSI_PIN = NoPin
SPI0_MISO_PIN = 0 SPI0_MISO_PIN = NoPin
) )

Просмотреть файл

@ -7,47 +7,47 @@ const HasLowFrequencyCrystal = true
// LEDs on the PCA10040 (nRF52832 dev board) // LEDs on the PCA10040 (nRF52832 dev board)
const ( const (
LED = LED1 LED Pin = LED1
LED1 = 17 LED1 Pin = 17
LED2 = 18 LED2 Pin = 18
LED3 = 19 LED3 Pin = 19
LED4 = 20 LED4 Pin = 20
) )
// Buttons on the PCA10040 (nRF52832 dev board) // Buttons on the PCA10040 (nRF52832 dev board)
const ( const (
BUTTON = BUTTON1 BUTTON Pin = BUTTON1
BUTTON1 = 13 BUTTON1 Pin = 13
BUTTON2 = 14 BUTTON2 Pin = 14
BUTTON3 = 15 BUTTON3 Pin = 15
BUTTON4 = 16 BUTTON4 Pin = 16
) )
// UART pins for NRF52840-DK // UART pins for NRF52840-DK
const ( const (
UART_TX_PIN = 6 UART_TX_PIN Pin = 6
UART_RX_PIN = 8 UART_RX_PIN Pin = 8
) )
// ADC pins // ADC pins
const ( const (
ADC0 = 3 ADC0 Pin = 3
ADC1 = 4 ADC1 Pin = 4
ADC2 = 28 ADC2 Pin = 28
ADC3 = 29 ADC3 Pin = 29
ADC4 = 30 ADC4 Pin = 30
ADC5 = 31 ADC5 Pin = 31
) )
// I2C pins // I2C pins
const ( const (
SDA_PIN = 26 SDA_PIN Pin = 26
SCL_PIN = 27 SCL_PIN Pin = 27
) )
// SPI pins // SPI pins
const ( const (
SPI0_SCK_PIN = 25 SPI0_SCK_PIN Pin = 25
SPI0_MOSI_PIN = 23 SPI0_MOSI_PIN Pin = 23
SPI0_MISO_PIN = 24 SPI0_MISO_PIN Pin = 24
) )

Просмотреть файл

@ -6,47 +6,47 @@ const HasLowFrequencyCrystal = true
// LEDs on the pca10056 // LEDs on the pca10056
const ( const (
LED = LED1 LED Pin = LED1
LED1 = 13 LED1 Pin = 13
LED2 = 14 LED2 Pin = 14
LED3 = 15 LED3 Pin = 15
LED4 = 16 LED4 Pin = 16
) )
// Buttons on the pca10056 // Buttons on the pca10056
const ( const (
BUTTON = BUTTON1 BUTTON Pin = BUTTON1
BUTTON1 = 11 BUTTON1 Pin = 11
BUTTON2 = 12 BUTTON2 Pin = 12
BUTTON3 = 24 BUTTON3 Pin = 24
BUTTON4 = 25 BUTTON4 Pin = 25
) )
// UART pins // UART pins
const ( const (
UART_TX_PIN = 6 UART_TX_PIN Pin = 6
UART_RX_PIN = 8 UART_RX_PIN Pin = 8
) )
// ADC pins // ADC pins
const ( const (
ADC0 = 3 ADC0 Pin = 3
ADC1 = 4 ADC1 Pin = 4
ADC2 = 28 ADC2 Pin = 28
ADC3 = 29 ADC3 Pin = 29
ADC4 = 30 ADC4 Pin = 30
ADC5 = 31 ADC5 Pin = 31
) )
// I2C pins // I2C pins
const ( const (
SDA_PIN = 26 // P0.26 SDA_PIN Pin = 26 // P0.26
SCL_PIN = 27 // P0.27 SCL_PIN Pin = 27 // P0.27
) )
// SPI pins // SPI pins
const ( const (
SPI0_SCK_PIN = 47 // P1.15 SPI0_SCK_PIN Pin = 47 // P1.15
SPI0_MOSI_PIN = 45 // P1.13 SPI0_MOSI_PIN Pin = 45 // P1.13
SPI0_MISO_PIN = 46 // P1.14 SPI0_MISO_PIN Pin = 46 // P1.14
) )

Просмотреть файл

@ -6,37 +6,37 @@ const HasLowFrequencyCrystal = true
// LEDs on the reel board // LEDs on the reel board
const ( const (
LED = LED1 LED Pin = LED1
LED1 = LED_YELLOW LED1 Pin = LED_YELLOW
LED2 = LED_RED LED2 Pin = LED_RED
LED3 = LED_GREEN LED3 Pin = LED_GREEN
LED4 = LED_BLUE LED4 Pin = LED_BLUE
LED_RED = 11 LED_RED Pin = 11
LED_GREEN = 12 LED_GREEN Pin = 12
LED_BLUE = 41 LED_BLUE Pin = 41
LED_YELLOW = 13 LED_YELLOW Pin = 13
) )
// User "a" button on the reel board // User "a" button on the reel board
const ( const (
BUTTON = 7 BUTTON Pin = 7
) )
// UART pins // UART pins
const ( const (
UART_TX_PIN = 6 UART_TX_PIN Pin = 6
UART_RX_PIN = 8 UART_RX_PIN Pin = 8
) )
// I2C pins // I2C pins
const ( const (
SDA_PIN = 26 SDA_PIN Pin = 26
SCL_PIN = 27 SCL_PIN Pin = 27
) )
// SPI pins // SPI pins
const ( const (
SPI0_SCK_PIN = 47 SPI0_SCK_PIN Pin = 47
SPI0_MOSI_PIN = 45 SPI0_MOSI_PIN Pin = 45
SPI0_MISO_PIN = 46 SPI0_MISO_PIN Pin = 46
) )

Просмотреть файл

@ -62,12 +62,12 @@ var (
I2C0 = I2C{Bus: sam.SERCOM2_I2CM, I2C0 = I2C{Bus: sam.SERCOM2_I2CM,
SDA: SDA_PIN, SDA: SDA_PIN,
SCL: SCL_PIN, SCL: SCL_PIN,
PinMode: GPIO_SERCOM_ALT} PinMode: PinSERCOMAlt}
) )
// I2S pins // I2S pins
const ( const (
I2S_SCK_PIN = PA10 I2S_SCK_PIN = PA10
I2S_SD_PIN = PA08 I2S_SD_PIN = PA08
I2S_WS_PIN = 0xff // TODO: figure out what this is on Trinket M0. I2S_WS_PIN = NoPin // TODO: figure out what this is on Trinket M0.
) )

Просмотреть файл

@ -41,9 +41,9 @@ const (
// All fields are optional and may not be required or used on a particular platform. // All fields are optional and may not be required or used on a particular platform.
type I2SConfig struct { type I2SConfig struct {
SCK uint8 SCK Pin
WS uint8 WS Pin
SD uint8 SD Pin
Mode I2SMode Mode I2SMode
Standard I2SStandard Standard I2SStandard
ClockSource I2SClockSource ClockSource I2SClockSource

Просмотреть файл

@ -1,25 +1,36 @@
package machine package machine
type GPIOConfig struct { type PinConfig struct {
Mode GPIOMode Mode PinMode
} }
type GPIO struct { // Pin is a single pin on a chip, which may be connected to other hardware
Pin uint8 // devices. It can either be used directly as GPIO pin or it can be used in
} // other peripherals like ADC, I2C, etc.
type Pin int8
func (p GPIO) High() { // NoPin explicitly indicates "not a pin". Use this pin if you want to leave one
// of the pins in a peripheral unconfigured (if supported by the hardware).
const NoPin = Pin(-1)
// High sets this GPIO pin to high, assuming it has been configured as an output
// pin. It is hardware dependent (and often undefined) what happens if you set a
// pin to high that is not configured as an output pin.
func (p Pin) High() {
p.Set(true) p.Set(true)
} }
func (p GPIO) Low() { // Low sets this GPIO pin to low, assuming it has been configured as an output
// pin. It is hardware dependent (and often undefined) what happens if you set a
// pin to low that is not configured as an output pin.
func (p Pin) Low() {
p.Set(false) p.Set(false)
} }
type PWM struct { type PWM struct {
Pin uint8 Pin Pin
} }
type ADC struct { type ADC struct {
Pin uint8 Pin Pin
} }

Просмотреть файл

@ -7,38 +7,38 @@ import (
) )
// Configure sets the pin to input or output. // Configure sets the pin to input or output.
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
if config.Mode == GPIO_OUTPUT { // set output bit if config.Mode == PinOutput { // set output bit
if p.Pin < 8 { if p < 8 {
avr.DDRD.SetBits(1 << p.Pin) avr.DDRD.SetBits(1 << uint8(p))
} else { } else {
avr.DDRB.SetBits(1 << (p.Pin - 8)) avr.DDRB.SetBits(1 << uint8(p-8))
} }
} else { // configure input: clear output bit } else { // configure input: clear output bit
if p.Pin < 8 { if p < 8 {
avr.DDRD.ClearBits(1 << p.Pin) avr.DDRD.ClearBits(1 << uint8(p))
} else { } else {
avr.DDRB.ClearBits(1 << (p.Pin - 8)) avr.DDRB.ClearBits(1 << uint8(p-8))
} }
} }
} }
// Get returns the current value of a GPIO pin. // Get returns the current value of a GPIO pin.
func (p GPIO) Get() bool { func (p Pin) Get() bool {
if p.Pin < 8 { if p < 8 {
val := avr.PIND.Get() & (1 << p.Pin) val := avr.PIND.Get() & (1 << uint8(p))
return (val > 0) return (val > 0)
} else { } else {
val := avr.PINB.Get() & (1 << (p.Pin - 8)) val := avr.PINB.Get() & (1 << uint8(p-8))
return (val > 0) return (val > 0)
} }
} }
func (p GPIO) getPortMask() (*avr.Register8, uint8) { func (p Pin) getPortMask() (*avr.Register8, uint8) {
if p.Pin < 8 { if p < 8 {
return avr.PORTD, 1 << p.Pin return avr.PORTD, 1 << uint8(p)
} else { } else {
return avr.PORTB, 1 << (p.Pin - 8) return avr.PORTB, 1 << uint8(p-8)
} }
} }
@ -66,9 +66,9 @@ func InitPWM() {
// Configure configures a PWM pin for output. // Configure configures a PWM pin for output.
func (pwm PWM) Configure() { func (pwm PWM) Configure() {
if pwm.Pin < 8 { if pwm.Pin < 8 {
avr.DDRD.SetBits(1 << pwm.Pin) avr.DDRD.SetBits(1 << uint8(pwm.Pin))
} else { } else {
avr.DDRB.SetBits(1 << (pwm.Pin - 8)) avr.DDRB.SetBits(1 << uint8(pwm.Pin-8))
} }
} }

Просмотреть файл

@ -18,113 +18,93 @@ import (
const CPU_FREQUENCY = 48000000 const CPU_FREQUENCY = 48000000
type GPIOMode uint8 type PinMode uint8
const ( const (
GPIO_ANALOG = 1 PinAnalog PinMode = 1
GPIO_SERCOM = 2 PinSERCOM PinMode = 2
GPIO_SERCOM_ALT = 3 PinSERCOMAlt PinMode = 3
GPIO_TIMER = 4 PinTimer PinMode = 4
GPIO_TIMER_ALT = 5 PinTimerAlt PinMode = 5
GPIO_COM = 6 PinCom PinMode = 6
GPIO_AC_CLK = 7 //PinAC_CLK PinMode = 7
GPIO_DIGITAL = 8 PinDigital PinMode = 8
GPIO_INPUT = 9 PinInput PinMode = 9
GPIO_INPUT_PULLUP = 10 PinInputPullup PinMode = 10
GPIO_OUTPUT = 11 PinOutput PinMode = 11
GPIO_PWM = GPIO_TIMER PinPWM PinMode = PinTimer
GPIO_PWM_ALT = GPIO_TIMER_ALT PinPWMAlt PinMode = PinTimerAlt
GPIO_INPUT_PULLDOWN = 12 PinInputPulldown PinMode = 12
) )
// Hardware pins // Hardware pins
const ( const (
PA00 = 0 PA00 Pin = 0
PA01 = 1 PA01 Pin = 1
PA02 = 2 PA02 Pin = 2
PA03 = 3 PA03 Pin = 3
PA04 = 4 PA04 Pin = 4
PA05 = 5 PA05 Pin = 5
PA06 = 6 PA06 Pin = 6
PA07 = 7 PA07 Pin = 7
PA08 = 8 PA08 Pin = 8
PA09 = 9 PA09 Pin = 9
PA10 = 10 PA10 Pin = 10
PA11 = 11 PA11 Pin = 11
PA12 = 12 PA12 Pin = 12
PA13 = 13 PA13 Pin = 13
PA14 = 14 PA14 Pin = 14
PA15 = 15 PA15 Pin = 15
PA16 = 16 PA16 Pin = 16
PA17 = 17 PA17 Pin = 17
PA18 = 18 PA18 Pin = 18
PA19 = 19 PA19 Pin = 19
PA20 = 20 PA20 Pin = 20
PA21 = 21 PA21 Pin = 21
PA22 = 22 PA22 Pin = 22
PA23 = 23 PA23 Pin = 23
PA24 = 24 PA24 Pin = 24
PA25 = 25 PA25 Pin = 25
PA26 = 26 PA26 Pin = 26
PA27 = 27 PA27 Pin = 27
PA28 = 28 PA28 Pin = 28
PA29 = 29 PA29 Pin = 29
PA30 = 30 PA30 Pin = 30
PA31 = 31 PA31 Pin = 31
PB00 = 32 PB00 Pin = 32
PB01 = 33 PB01 Pin = 33
PB02 = 34 PB02 Pin = 34
PB03 = 35 PB03 Pin = 35
PB04 = 36 PB04 Pin = 36
PB05 = 37 PB05 Pin = 37
PB06 = 38 PB06 Pin = 38
PB07 = 39 PB07 Pin = 39
PB08 = 40 PB08 Pin = 40
PB09 = 41 PB09 Pin = 41
PB10 = 42 PB10 Pin = 42
PB11 = 43 PB11 Pin = 43
PB12 = 44 PB12 Pin = 44
PB13 = 45 PB13 Pin = 45
PB14 = 46 PB14 Pin = 46
PB15 = 47 PB15 Pin = 47
PB16 = 48 PB16 Pin = 48
PB17 = 49 PB17 Pin = 49
PB18 = 50 PB18 Pin = 50
PB19 = 51 PB19 Pin = 51
PB20 = 52 PB20 Pin = 52
PB21 = 53 PB21 Pin = 53
PB22 = 54 PB22 Pin = 54
PB23 = 55 PB23 Pin = 55
PB24 = 56 PB24 Pin = 56
PB25 = 57 PB25 Pin = 57
PB26 = 58 PB26 Pin = 58
PB27 = 59 PB27 Pin = 59
PB28 = 60 PB28 Pin = 60
PB29 = 61 PB29 Pin = 61
PB30 = 62 PB30 Pin = 62
PB31 = 63 PB31 Pin = 63
) )
// getPMux returns the value for the correct PMUX register for this pin.
func (p GPIO) getPMux() uint8 {
return getPMux(p.Pin)
}
// setPMux sets the value for the correct PMUX register for this pin.
func (p GPIO) setPMux(val uint8) {
setPMux(p.Pin, val)
}
// getPinCfg returns the value for the correct PINCFG register for this pin.
func (p GPIO) getPinCfg() uint8 {
return getPinCfg(p.Pin)
}
// setPinCfg sets the value for the correct PINCFG register for this pin.
func (p GPIO) setPinCfg(val uint8) {
setPinCfg(p.Pin, val)
}
// InitADC initializes the ADC. // InitADC initializes the ADC.
func InitADC() { func InitADC() {
// ADC Bias Calibration // ADC Bias Calibration
@ -184,7 +164,7 @@ func InitADC() {
// Configure configures a ADCPin to be able to be used to read data. // Configure configures a ADCPin to be able to be used to read data.
func (a ADC) Configure() { func (a ADC) Configure() {
GPIO{a.Pin}.Configure(GPIOConfig{Mode: GPIO_ANALOG}) a.Pin.Configure(PinConfig{Mode: PinAnalog})
return return
} }
@ -340,8 +320,8 @@ func (uart UART) Configure(config UARTConfig) {
} }
// configure pins // configure pins
GPIO{config.TX}.Configure(GPIOConfig{Mode: GPIO_SERCOM}) config.TX.Configure(PinConfig{Mode: PinSERCOM})
GPIO{config.RX}.Configure(GPIOConfig{Mode: GPIO_SERCOM}) config.RX.Configure(PinConfig{Mode: PinSERCOM})
// reset SERCOM0 // reset SERCOM0
uart.Bus.CTRLA.SetBits(sam.SERCOM_USART_CTRLA_SWRST) uart.Bus.CTRLA.SetBits(sam.SERCOM_USART_CTRLA_SWRST)
@ -434,16 +414,16 @@ func handleUART1() {
// I2C on the SAMD21. // I2C on the SAMD21.
type I2C struct { type I2C struct {
Bus *sam.SERCOM_I2CM_Type Bus *sam.SERCOM_I2CM_Type
SCL uint8 SCL Pin
SDA uint8 SDA Pin
PinMode GPIOMode PinMode PinMode
} }
// I2CConfig is used to store config info for I2C. // I2CConfig is used to store config info for I2C.
type I2CConfig struct { type I2CConfig struct {
Frequency uint32 Frequency uint32
SCL uint8 SCL Pin
SDA uint8 SDA Pin
} }
const ( const (
@ -496,8 +476,8 @@ func (i2c I2C) Configure(config I2CConfig) {
} }
// enable pins // enable pins
GPIO{i2c.SDA}.Configure(GPIOConfig{Mode: i2c.PinMode}) i2c.SDA.Configure(PinConfig{Mode: i2c.PinMode})
GPIO{i2c.SCL}.Configure(GPIOConfig{Mode: i2c.PinMode}) i2c.SCL.Configure(PinConfig{Mode: i2c.PinMode})
} }
// SetBaudRate sets the communication speed for the I2C. // SetBaudRate sets the communication speed for the I2C.
@ -770,11 +750,11 @@ func (i2s I2S) Configure(config I2SConfig) {
} }
// configure pin for clock // configure pin for clock
GPIO{config.SCK}.Configure(GPIOConfig{Mode: GPIO_COM}) config.SCK.Configure(PinConfig{Mode: PinCom})
// configure pin for WS, if needed // configure pin for WS, if needed
if config.WS != 0xff { if config.WS != NoPin {
GPIO{config.WS}.Configure(GPIOConfig{Mode: GPIO_COM}) config.WS.Configure(PinConfig{Mode: PinCom})
} }
// now set serializer data size. // now set serializer data size.
@ -813,7 +793,7 @@ func (i2s I2S) Configure(config I2SConfig) {
} }
// configure data pin // configure data pin
GPIO{config.SD}.Configure(GPIOConfig{Mode: GPIO_COM}) config.SD.Configure(PinConfig{Mode: PinCom})
// re-enable // re-enable
i2s.Bus.CTRLA.SetBits(sam.I2S_CTRLA_ENABLE) i2s.Bus.CTRLA.SetBits(sam.I2S_CTRLA_ENABLE)
@ -900,9 +880,9 @@ type SPI struct {
// SPIConfig is used to store config info for SPI. // SPIConfig is used to store config info for SPI.
type SPIConfig struct { type SPIConfig struct {
Frequency uint32 Frequency uint32
SCK uint8 SCK Pin
MOSI uint8 MOSI Pin
MISO uint8 MISO Pin
LSBFirst bool LSBFirst bool
Mode uint8 Mode uint8
} }
@ -927,9 +907,9 @@ func (spi SPI) Configure(config SPIConfig) {
} }
// enable pins // enable pins
GPIO{config.SCK}.Configure(GPIOConfig{Mode: GPIO_SERCOM_ALT}) config.SCK.Configure(PinConfig{Mode: PinSERCOMAlt})
GPIO{config.MOSI}.Configure(GPIOConfig{Mode: GPIO_SERCOM_ALT}) config.MOSI.Configure(PinConfig{Mode: PinSERCOMAlt})
GPIO{config.MISO}.Configure(GPIOConfig{Mode: GPIO_SERCOM_ALT}) config.MISO.Configure(PinConfig{Mode: PinSERCOMAlt})
// reset SERCOM // reset SERCOM
spi.Bus.CTRLA.SetBits(sam.SERCOM_SPI_CTRLA_SWRST) spi.Bus.CTRLA.SetBits(sam.SERCOM_SPI_CTRLA_SWRST)
@ -1044,20 +1024,20 @@ func (pwm PWM) Configure() {
} }
// Set pin as output // Set pin as output
sam.PORT.DIRSET0.Set(1 << pwm.Pin) sam.PORT.DIRSET0.Set(1 << uint8(pwm.Pin))
// Set pin to low // Set pin to low
sam.PORT.OUTCLR0.Set(1 << pwm.Pin) sam.PORT.OUTCLR0.Set(1 << uint8(pwm.Pin))
// Enable the port multiplexer for pin // Enable the port multiplexer for pin
pwm.setPinCfg(sam.PORT_PINCFG0_PMUXEN) pwm.setPinCfg(sam.PORT_PINCFG0_PMUXEN)
// Connect TCCX timer to pin. // Connect TCCX timer to pin.
// we normally use the F channel aka ALT // we normally use the F channel aka ALT
pwmConfig := GPIO_PWM_ALT pwmConfig := PinPWMAlt
// in the case of PA6 or PA7 we have to use E channel // in the case of PA6 or PA7 we have to use E channel
if pwm.Pin == 6 || pwm.Pin == 7 { if pwm.Pin == 6 || pwm.Pin == 7 {
pwmConfig = GPIO_PWM pwmConfig = PinPWM
} }
if pwm.Pin&1 > 0 { if pwm.Pin&1 > 0 {
@ -1102,22 +1082,22 @@ func (pwm PWM) Set(value uint16) {
// getPMux returns the value for the correct PMUX register for this pin. // getPMux returns the value for the correct PMUX register for this pin.
func (pwm PWM) getPMux() uint8 { func (pwm PWM) getPMux() uint8 {
return getPMux(pwm.Pin) return pwm.Pin.getPMux()
} }
// setPMux sets the value for the correct PMUX register for this pin. // setPMux sets the value for the correct PMUX register for this pin.
func (pwm PWM) setPMux(val uint8) { func (pwm PWM) setPMux(val uint8) {
setPMux(pwm.Pin, val) pwm.Pin.setPMux(val)
} }
// getPinCfg returns the value for the correct PINCFG register for this pin. // getPinCfg returns the value for the correct PINCFG register for this pin.
func (pwm PWM) getPinCfg() uint8 { func (pwm PWM) getPinCfg() uint8 {
return getPinCfg(pwm.Pin) return pwm.Pin.getPinCfg()
} }
// setPinCfg sets the value for the correct PINCFG register for this pin. // setPinCfg sets the value for the correct PINCFG register for this pin.
func (pwm PWM) setPinCfg(val uint8) { func (pwm PWM) setPinCfg(val uint8) {
setPinCfg(pwm.Pin, val) pwm.Pin.setPinCfg(val)
} }
// getTimer returns the timer to be used for PWM on this pin // getTimer returns the timer to be used for PWM on this pin
@ -1260,8 +1240,8 @@ func (usbcdc USBCDC) Configure(config UARTConfig) {
sam.USB_DEVICE.DESCADD.Set(uint32(uintptr(unsafe.Pointer(&usbEndpointDescriptors)))) sam.USB_DEVICE.DESCADD.Set(uint32(uintptr(unsafe.Pointer(&usbEndpointDescriptors))))
// configure pins // configure pins
GPIO{USBCDC_DM_PIN}.Configure(GPIOConfig{Mode: GPIO_COM}) USBCDC_DM_PIN.Configure(PinConfig{Mode: PinCom})
GPIO{USBCDC_DP_PIN}.Configure(GPIOConfig{Mode: GPIO_COM}) USBCDC_DP_PIN.Configure(PinConfig{Mode: PinCom})
// performs pad calibration from store fuses // performs pad calibration from store fuses
handlePadCalibration() handlePadCalibration()

Просмотреть файл

@ -13,100 +13,100 @@ import (
// Return the register and mask to enable a given GPIO pin. This can be used to // Return the register and mask to enable a given GPIO pin. This can be used to
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskSet() (*uint32, uint32) { func (p Pin) PortMaskSet() (*uint32, uint32) {
return &sam.PORT.OUTSET0.Reg, 1 << p.Pin return &sam.PORT.OUTSET0.Reg, 1 << uint8(p)
} }
// Return the register and mask to disable a given port. This can be used to // Return the register and mask to disable a given port. This can be used to
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskClear() (*uint32, uint32) { func (p Pin) PortMaskClear() (*uint32, uint32) {
return &sam.PORT.OUTCLR0.Reg, 1 << p.Pin return &sam.PORT.OUTCLR0.Reg, 1 << uint8(p)
} }
// Set the pin to high or low. // Set the pin to high or low.
// Warning: only use this on an output pin! // Warning: only use this on an output pin!
func (p GPIO) Set(high bool) { func (p Pin) Set(high bool) {
if high { if high {
sam.PORT.OUTSET0.Set(1 << p.Pin) sam.PORT.OUTSET0.Set(1 << uint8(p))
} else { } else {
sam.PORT.OUTCLR0.Set(1 << p.Pin) sam.PORT.OUTCLR0.Set(1 << uint8(p))
} }
} }
// Get returns the current value of a GPIO pin. // Get returns the current value of a GPIO pin.
func (p GPIO) Get() bool { func (p Pin) Get() bool {
return (sam.PORT.IN0.Get()>>p.Pin)&1 > 0 return (sam.PORT.IN0.Get()>>uint8(p))&1 > 0
} }
// Configure this pin with the given configuration. // Configure this pin with the given configuration.
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
switch config.Mode { switch config.Mode {
case GPIO_OUTPUT: case PinOutput:
sam.PORT.DIRSET0.Set(1 << p.Pin) sam.PORT.DIRSET0.Set(1 << uint8(p))
// output is also set to input enable so pin can read back its own value // output is also set to input enable so pin can read back its own value
p.setPinCfg(sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_INEN)
case GPIO_INPUT: case PinInput:
sam.PORT.DIRCLR0.Set(1 << p.Pin) sam.PORT.DIRCLR0.Set(1 << uint8(p))
p.setPinCfg(sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_INEN)
case GPIO_INPUT_PULLDOWN: case PinInputPulldown:
sam.PORT.DIRCLR0.Set(1 << p.Pin) sam.PORT.DIRCLR0.Set(1 << uint8(p))
sam.PORT.OUTCLR0.Set(1 << p.Pin) sam.PORT.OUTCLR0.Set(1 << uint8(p))
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN) p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
case GPIO_INPUT_PULLUP: case PinInputPullup:
sam.PORT.DIRCLR0.Set(1 << p.Pin) sam.PORT.DIRCLR0.Set(1 << uint8(p))
sam.PORT.OUTSET0.Set(1 << p.Pin) sam.PORT.OUTSET0.Set(1 << uint8(p))
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN) p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
case GPIO_SERCOM: case PinSERCOM:
if p.Pin&1 > 0 { if uint8(p)&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_SERCOM << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_SERCOM << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN)
case GPIO_SERCOM_ALT: case PinSERCOMAlt:
if p.Pin&1 > 0 { if uint8(p)&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_SERCOM_ALT << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_SERCOM_ALT << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
case GPIO_COM: case PinCom:
if p.Pin&1 > 0 { if uint8(p)&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_COM << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_COM << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN)
case GPIO_ANALOG: case PinAnalog:
if p.Pin&1 > 0 { if uint8(p)&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_ANALOG << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_ANALOG << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
@ -114,9 +114,8 @@ func (p GPIO) Configure(config GPIOConfig) {
} }
// getPMux returns the value for the correct PMUX register for this pin. // getPMux returns the value for the correct PMUX register for this pin.
func getPMux(p uint8) uint8 { func (p Pin) getPMux() uint8 {
pin := p >> 1 switch p >> 1 {
switch pin {
case 0: case 0:
return sam.PORT.PMUX0_0.Get() return sam.PORT.PMUX0_0.Get()
case 1: case 1:
@ -155,9 +154,8 @@ func getPMux(p uint8) uint8 {
} }
// setPMux sets the value for the correct PMUX register for this pin. // setPMux sets the value for the correct PMUX register for this pin.
func setPMux(p uint8, val uint8) { func (p Pin) setPMux(val uint8) {
pin := p >> 1 switch p >> 1 {
switch pin {
case 0: case 0:
sam.PORT.PMUX0_0.Set(val) sam.PORT.PMUX0_0.Set(val)
case 1: case 1:
@ -194,7 +192,7 @@ func setPMux(p uint8, val uint8) {
} }
// getPinCfg returns the value for the correct PINCFG register for this pin. // getPinCfg returns the value for the correct PINCFG register for this pin.
func getPinCfg(p uint8) uint8 { func (p Pin) getPinCfg() uint8 {
switch p { switch p {
case 0: case 0:
return sam.PORT.PINCFG0_0.Get() return sam.PORT.PINCFG0_0.Get()
@ -266,7 +264,7 @@ func getPinCfg(p uint8) uint8 {
} }
// setPinCfg sets the value for the correct PINCFG register for this pin. // setPinCfg sets the value for the correct PINCFG register for this pin.
func setPinCfg(p uint8, val uint8) { func (p Pin) setPinCfg(val uint8) {
switch p { switch p {
case 0: case 0:
sam.PORT.PINCFG0_0.Set(val) sam.PORT.PINCFG0_0.Set(val)

Просмотреть файл

@ -13,143 +13,143 @@ import (
// Return the register and mask to enable a given GPIO pin. This can be used to // Return the register and mask to enable a given GPIO pin. This can be used to
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskSet() (*uint32, uint32) { func (p Pin) PortMaskSet() (*uint32, uint32) {
if p.Pin < 32 { if p < 32 {
return &sam.PORT.OUTSET0.Reg, 1 << p.Pin return &sam.PORT.OUTSET0.Reg, 1 << uint8(p)
} else { } else {
return &sam.PORT.OUTSET1.Reg, 1 << (p.Pin - 32) return &sam.PORT.OUTSET1.Reg, 1 << uint8(p-32)
} }
} }
// Return the register and mask to disable a given port. This can be used to // Return the register and mask to disable a given port. This can be used to
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskClear() (*uint32, uint32) { func (p Pin) PortMaskClear() (*uint32, uint32) {
if p.Pin < 32 { if p < 32 {
return &sam.PORT.OUTCLR0.Reg, 1 << p.Pin return &sam.PORT.OUTCLR0.Reg, 1 << uint8(p)
} else { } else {
return &sam.PORT.OUTCLR1.Reg, 1 << (p.Pin - 32) return &sam.PORT.OUTCLR1.Reg, 1 << uint8(p-32)
} }
} }
// Set the pin to high or low. // Set the pin to high or low.
// Warning: only use this on an output pin! // Warning: only use this on an output pin!
func (p GPIO) Set(high bool) { func (p Pin) Set(high bool) {
if p.Pin < 32 { if p < 32 {
if high { if high {
sam.PORT.OUTSET0.Set(1 << p.Pin) sam.PORT.OUTSET0.Set(1 << uint8(p))
} else { } else {
sam.PORT.OUTCLR0.Set(1 << p.Pin) sam.PORT.OUTCLR0.Set(1 << uint8(p))
} }
} else { } else {
if high { if high {
sam.PORT.OUTSET1.Set(1 << (p.Pin - 32)) sam.PORT.OUTSET1.Set(1 << uint8(p-32))
} else { } else {
sam.PORT.OUTCLR1.Set(1 << (p.Pin - 32)) sam.PORT.OUTCLR1.Set(1 << uint8(p-32))
} }
} }
} }
// Get returns the current value of a GPIO pin. // Get returns the current value of a GPIO pin.
func (p GPIO) Get() bool { func (p Pin) Get() bool {
if p.Pin < 32 { if p < 32 {
return (sam.PORT.IN0.Get()>>p.Pin)&1 > 0 return (sam.PORT.IN0.Get()>>uint8(p))&1 > 0
} else { } else {
return (sam.PORT.IN1.Get()>>(p.Pin-32))&1 > 0 return (sam.PORT.IN1.Get()>>(uint8(p)-32))&1 > 0
} }
} }
// Configure this pin with the given configuration. // Configure this pin with the given configuration.
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
switch config.Mode { switch config.Mode {
case GPIO_OUTPUT: case PinOutput:
if p.Pin < 32 { if p < 32 {
sam.PORT.DIRSET0.Set(1 << p.Pin) sam.PORT.DIRSET0.Set(1 << uint8(p))
// output is also set to input enable so pin can read back its own value // output is also set to input enable so pin can read back its own value
p.setPinCfg(sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_INEN)
} else { } else {
sam.PORT.DIRSET1.Set(1 << (p.Pin - 32)) sam.PORT.DIRSET1.Set(1 << uint8(p-32))
// output is also set to input enable so pin can read back its own value // output is also set to input enable so pin can read back its own value
p.setPinCfg(sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_INEN)
} }
case GPIO_INPUT: case PinInput:
if p.Pin < 32 { if p < 32 {
sam.PORT.DIRCLR0.Set(1 << p.Pin) sam.PORT.DIRCLR0.Set(1 << uint8(p))
p.setPinCfg(sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_INEN)
} else { } else {
sam.PORT.DIRCLR1.Set(1<<p.Pin - 32) sam.PORT.DIRCLR1.Set(1<<uint8(p) - 32)
p.setPinCfg(sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_INEN)
} }
case GPIO_INPUT_PULLDOWN: case PinInputPulldown:
if p.Pin < 32 { if p < 32 {
sam.PORT.DIRCLR0.Set(1 << p.Pin) sam.PORT.DIRCLR0.Set(1 << uint8(p))
sam.PORT.OUTCLR0.Set(1 << p.Pin) sam.PORT.OUTCLR0.Set(1 << uint8(p))
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN) p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
} else { } else {
sam.PORT.DIRCLR1.Set(1<<p.Pin - 32) sam.PORT.DIRCLR1.Set(1<<uint8(p) - 32)
sam.PORT.OUTCLR1.Set(1<<p.Pin - 32) sam.PORT.OUTCLR1.Set(1<<uint8(p) - 32)
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN) p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
} }
case GPIO_INPUT_PULLUP: case PinInputPullup:
if p.Pin < 32 { if p < 32 {
sam.PORT.DIRCLR0.Set(1 << p.Pin) sam.PORT.DIRCLR0.Set(1 << uint8(p))
sam.PORT.OUTSET0.Set(1 << p.Pin) sam.PORT.OUTSET0.Set(1 << uint8(p))
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN) p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
} else { } else {
sam.PORT.DIRCLR1.Set(1<<p.Pin - 32) sam.PORT.DIRCLR1.Set(1<<uint8(p) - 32)
sam.PORT.OUTSET1.Set(1<<p.Pin - 32) sam.PORT.OUTSET1.Set(1<<uint8(p) - 32)
p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN) p.setPinCfg(sam.PORT_PINCFG0_INEN | sam.PORT_PINCFG0_PULLEN)
} }
case GPIO_SERCOM: case PinSERCOM:
if p.Pin&1 > 0 { if p&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_SERCOM << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_SERCOM << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinSERCOM) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR | sam.PORT_PINCFG0_INEN)
case GPIO_SERCOM_ALT: case PinSERCOMAlt:
if p.Pin&1 > 0 { if p&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_SERCOM_ALT << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_SERCOM_ALT << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinSERCOMAlt) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
case GPIO_COM: case PinCom:
if p.Pin&1 > 0 { if p&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_COM << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_COM << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinCom) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN)
case GPIO_ANALOG: case PinAnalog:
if p.Pin&1 > 0 { if p&1 > 0 {
// odd pin, so save the even pins // odd pin, so save the even pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXE_Msk
p.setPMux(val | (GPIO_ANALOG << sam.PORT_PMUX0_PMUXO_Pos)) p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXO_Pos))
} else { } else {
// even pin, so save the odd pins // even pin, so save the odd pins
val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk val := p.getPMux() & sam.PORT_PMUX0_PMUXO_Msk
p.setPMux(val | (GPIO_ANALOG << sam.PORT_PMUX0_PMUXE_Pos)) p.setPMux(val | (uint8(PinAnalog) << sam.PORT_PMUX0_PMUXE_Pos))
} }
// enable port config // enable port config
p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR) p.setPinCfg(sam.PORT_PINCFG0_PMUXEN | sam.PORT_PINCFG0_DRVSTR)
@ -157,9 +157,8 @@ func (p GPIO) Configure(config GPIOConfig) {
} }
// getPMux returns the value for the correct PMUX register for this pin. // getPMux returns the value for the correct PMUX register for this pin.
func getPMux(p uint8) uint8 { func (p Pin) getPMux() uint8 {
pin := p >> 1 switch uint8(p) >> 1 {
switch pin {
case 0: case 0:
return sam.PORT.PMUX0_0.Get() return sam.PORT.PMUX0_0.Get()
case 1: case 1:
@ -230,9 +229,8 @@ func getPMux(p uint8) uint8 {
} }
// setPMux sets the value for the correct PMUX register for this pin. // setPMux sets the value for the correct PMUX register for this pin.
func setPMux(p uint8, val uint8) { func (p Pin) setPMux(val uint8) {
pin := p >> 1 switch uint8(p) >> 1 {
switch pin {
case 0: case 0:
sam.PORT.PMUX0_0.Set(val) sam.PORT.PMUX0_0.Set(val)
case 1: case 1:
@ -301,7 +299,7 @@ func setPMux(p uint8, val uint8) {
} }
// getPinCfg returns the value for the correct PINCFG register for this pin. // getPinCfg returns the value for the correct PINCFG register for this pin.
func getPinCfg(p uint8) uint8 { func (p Pin) getPinCfg() uint8 {
switch p { switch p {
case 0: case 0:
return sam.PORT.PINCFG0_0.Get() return sam.PORT.PINCFG0_0.Get()
@ -437,7 +435,7 @@ func getPinCfg(p uint8) uint8 {
} }
// setPinCfg sets the value for the correct PINCFG register for this pin. // setPinCfg sets the value for the correct PINCFG register for this pin.
func setPinCfg(p uint8, val uint8) { func (p Pin) setPinCfg(val uint8) {
switch p { switch p {
case 0: case 0:
sam.PORT.PINCFG0_0.Set(val) sam.PORT.PINCFG0_0.Set(val)

Просмотреть файл

@ -7,21 +7,21 @@ import (
) )
// Configure sets the pin to input or output. // Configure sets the pin to input or output.
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
if config.Mode == GPIO_OUTPUT { // set output bit if config.Mode == PinOutput { // set output bit
avr.DDRB.SetBits(1 << p.Pin) avr.DDRB.SetBits(1 << uint8(p))
} else { // configure input: clear output bit } else { // configure input: clear output bit
avr.DDRB.ClearBits(1 << p.Pin) avr.DDRB.ClearBits(1 << uint8(p))
} }
} }
func (p GPIO) getPortMask() (*avr.Register8, uint8) { func (p Pin) getPortMask() (*avr.Register8, uint8) {
return avr.PORTB, 1 << p.Pin return avr.PORTB, 1 << uint8(p)
} }
// Get returns the current value of a GPIO pin. // Get returns the current value of a GPIO pin.
func (p GPIO) Get() bool { func (p Pin) Get() bool {
val := avr.PINB.Get() & (1 << p.Pin) val := avr.PINB.Get() & (1 << uint8(p))
return (val > 0) return (val > 0)
} }

Просмотреть файл

@ -6,15 +6,15 @@ import (
"device/avr" "device/avr"
) )
type GPIOMode uint8 type PinMode uint8
const ( const (
GPIO_INPUT = iota PinInput PinMode = iota
GPIO_OUTPUT PinOutput
) )
// Set changes the value of the GPIO pin. The pin must be configured as output. // Set changes the value of the GPIO pin. The pin must be configured as output.
func (p GPIO) Set(value bool) { func (p Pin) Set(value bool) {
if value { // set bits if value { // set bits
port, mask := p.PortMaskSet() port, mask := p.PortMaskSet()
port.Set(mask) port.Set(mask)
@ -30,7 +30,7 @@ func (p GPIO) Set(value bool) {
// Warning: there are no separate pin set/clear registers on the AVR. The // Warning: there are no separate pin set/clear registers on the AVR. The
// returned mask is only valid as long as no other pin in the same port has been // returned mask is only valid as long as no other pin in the same port has been
// changed. // changed.
func (p GPIO) PortMaskSet() (*avr.Register8, uint8) { func (p Pin) PortMaskSet() (*avr.Register8, uint8) {
port, mask := p.getPortMask() port, mask := p.getPortMask()
return port, port.Get() | mask return port, port.Get() | mask
} }
@ -41,7 +41,7 @@ func (p GPIO) PortMaskSet() (*avr.Register8, uint8) {
// Warning: there are no separate pin set/clear registers on the AVR. The // Warning: there are no separate pin set/clear registers on the AVR. The
// returned mask is only valid as long as no other pin in the same port has been // returned mask is only valid as long as no other pin in the same port has been
// changed. // changed.
func (p GPIO) PortMaskClear() (*avr.Register8, uint8) { func (p Pin) PortMaskClear() (*avr.Register8, uint8) {
port, mask := p.getPortMask() port, mask := p.getPortMask()
return port, port.Get() &^ mask return port, port.Get() &^ mask
} }
@ -68,7 +68,7 @@ func (a ADC) Get() uint16 {
// set the ADLAR bit (left-adjusted result) to get a value scaled to 16 // set the ADLAR bit (left-adjusted result) to get a value scaled to 16
// bits. This has the same effect as shifting the return value left by 6 // bits. This has the same effect as shifting the return value left by 6
// bits. // bits.
avr.ADMUX.Set(avr.ADMUX_REFS0 | avr.ADMUX_ADLAR | (a.Pin & 0x07)) avr.ADMUX.Set(avr.ADMUX_REFS0 | avr.ADMUX_ADLAR | (uint8(a.Pin) & 0x07))
// start the conversion // start the conversion
avr.ADCSRA.SetBits(avr.ADCSRA_ADSC) avr.ADCSRA.SetBits(avr.ADCSRA_ADSC)

Просмотреть файл

@ -4,37 +4,37 @@ package machine
// Dummy machine package, filled with no-ops. // Dummy machine package, filled with no-ops.
type GPIOMode uint8 type PinMode uint8
const ( const (
GPIO_INPUT = iota PinInput PinMode = iota
GPIO_OUTPUT PinOutput
) )
// Fake LED numbers, for testing. // Fake LED numbers, for testing.
const ( const (
LED = LED1 LED Pin = LED1
LED1 = 0 LED1 Pin = 0
LED2 = 0 LED2 Pin = 0
LED3 = 0 LED3 Pin = 0
LED4 = 0 LED4 Pin = 0
) )
// Fake button numbers, for testing. // Fake button numbers, for testing.
const ( const (
BUTTON = BUTTON1 BUTTON Pin = BUTTON1
BUTTON1 = 0 BUTTON1 Pin = 0
BUTTON2 = 0 BUTTON2 Pin = 0
BUTTON3 = 0 BUTTON3 Pin = 0
BUTTON4 = 0 BUTTON4 Pin = 0
) )
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
} }
func (p GPIO) Set(value bool) { func (p Pin) Set(value bool) {
} }
func (p GPIO) Get() bool { func (p Pin) Get() bool {
return false return false
} }

Просмотреть файл

@ -7,17 +7,17 @@ import (
"device/nrf" "device/nrf"
) )
type GPIOMode uint8 type PinMode uint8
const ( const (
GPIO_INPUT = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) PinInput PinMode = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos)
GPIO_INPUT_PULLUP = GPIO_INPUT | (nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) PinInputPullup PinMode = PinInput | (nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos)
GPIO_INPUT_PULLDOWN = GPIO_INPUT | (nrf.GPIO_PIN_CNF_PULL_Pulldown << nrf.GPIO_PIN_CNF_PULL_Pos) PinInputPulldown PinMode = PinOutput | (nrf.GPIO_PIN_CNF_PULL_Pulldown << nrf.GPIO_PIN_CNF_PULL_Pos)
GPIO_OUTPUT = (nrf.GPIO_PIN_CNF_DIR_Output << nrf.GPIO_PIN_CNF_DIR_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Disconnect << nrf.GPIO_PIN_CNF_INPUT_Pos) PinOutput PinMode = (nrf.GPIO_PIN_CNF_DIR_Output << nrf.GPIO_PIN_CNF_DIR_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Disconnect << nrf.GPIO_PIN_CNF_INPUT_Pos)
) )
// Configure this pin with the given configuration. // Configure this pin with the given configuration.
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
cfg := config.Mode | nrf.GPIO_PIN_CNF_DRIVE_S0S1 | nrf.GPIO_PIN_CNF_SENSE_Disabled cfg := config.Mode | nrf.GPIO_PIN_CNF_DRIVE_S0S1 | nrf.GPIO_PIN_CNF_SENSE_Disabled
port, pin := p.getPortPin() port, pin := p.getPortPin()
port.PIN_CNF[pin].Set(uint32(cfg)) port.PIN_CNF[pin].Set(uint32(cfg))
@ -25,7 +25,7 @@ func (p GPIO) Configure(config GPIOConfig) {
// Set the pin to high or low. // Set the pin to high or low.
// Warning: only use this on an output pin! // Warning: only use this on an output pin!
func (p GPIO) Set(high bool) { func (p Pin) Set(high bool) {
port, pin := p.getPortPin() port, pin := p.getPortPin()
if high { if high {
port.OUTSET.Set(1 << pin) port.OUTSET.Set(1 << pin)
@ -36,20 +36,20 @@ func (p GPIO) Set(high bool) {
// Return the register and mask to enable a given GPIO pin. This can be used to // Return the register and mask to enable a given GPIO pin. This can be used to
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskSet() (*uint32, uint32) { func (p Pin) PortMaskSet() (*uint32, uint32) {
port, pin := p.getPortPin() port, pin := p.getPortPin()
return &port.OUTSET.Reg, 1 << pin return &port.OUTSET.Reg, 1 << pin
} }
// Return the register and mask to disable a given port. This can be used to // Return the register and mask to disable a given port. This can be used to
// implement bit-banged drivers. // implement bit-banged drivers.
func (p GPIO) PortMaskClear() (*uint32, uint32) { func (p Pin) PortMaskClear() (*uint32, uint32) {
port, pin := p.getPortPin() port, pin := p.getPortPin()
return &port.OUTCLR.Reg, 1 << pin return &port.OUTCLR.Reg, 1 << pin
} }
// Get returns the current value of a GPIO pin. // Get returns the current value of a GPIO pin.
func (p GPIO) Get() bool { func (p Pin) Get() bool {
port, pin := p.getPortPin() port, pin := p.getPortPin()
return (port.IN.Get()>>pin)&1 != 0 return (port.IN.Get()>>pin)&1 != 0
} }
@ -132,8 +132,8 @@ var (
// I2CConfig is used to store config info for I2C. // I2CConfig is used to store config info for I2C.
type I2CConfig struct { type I2CConfig struct {
Frequency uint32 Frequency uint32
SCL uint8 SCL Pin
SDA uint8 SDA Pin
} }
// Configure is intended to setup the I2C interface. // Configure is intended to setup the I2C interface.
@ -149,14 +149,14 @@ func (i2c I2C) Configure(config I2CConfig) {
} }
// do config // do config
sclPort, sclPin := GPIO{config.SCL}.getPortPin() sclPort, sclPin := config.SCL.getPortPin()
sclPort.PIN_CNF[sclPin].Set((nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) | sclPort.PIN_CNF[sclPin].Set((nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
(nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) |
(nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) | (nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) |
(nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) | (nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) |
(nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos)) (nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos))
sdaPort, sdaPin := GPIO{config.SDA}.getPortPin() sdaPort, sdaPin := config.SDA.getPortPin()
sdaPort.PIN_CNF[sdaPin].Set((nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) | sdaPort.PIN_CNF[sdaPin].Set((nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
(nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) | (nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) |
(nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) | (nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) |
@ -242,9 +242,9 @@ var (
// SPIConfig is used to store config info for SPI. // SPIConfig is used to store config info for SPI.
type SPIConfig struct { type SPIConfig struct {
Frequency uint32 Frequency uint32
SCK uint8 SCK Pin
MOSI uint8 MOSI Pin
MISO uint8 MISO Pin
LSBFirst bool LSBFirst bool
Mode uint8 Mode uint8
} }

Просмотреть файл

@ -9,13 +9,13 @@ import (
const CPU_FREQUENCY = 16000000 const CPU_FREQUENCY = 16000000
// Get peripheral and pin number for this GPIO pin. // Get peripheral and pin number for this GPIO pin.
func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) { func (p Pin) getPortPin() (*nrf.GPIO_Type, uint32) {
return nrf.GPIO, p.Pin return nrf.GPIO, uint32(p)
} }
func (uart UART) setPins(tx, rx uint32) { func (uart UART) setPins(tx, rx Pin) {
nrf.UART0.PSELTXD.Set(tx) nrf.UART0.PSELTXD.Set(uint32(tx))
nrf.UART0.PSELRXD.Set(rx) nrf.UART0.PSELRXD.Set(uint32(rx))
} }
//go:export UART0_IRQHandler //go:export UART0_IRQHandler
@ -23,13 +23,13 @@ func handleUART0() {
UART0.handleInterrupt() UART0.handleInterrupt()
} }
func (i2c I2C) setPins(scl, sda uint8) { func (i2c I2C) setPins(scl, sda Pin) {
i2c.Bus.PSELSCL.Set(uint32(scl)) i2c.Bus.PSELSCL.Set(uint32(scl))
i2c.Bus.PSELSDA.Set(uint32(sda)) i2c.Bus.PSELSDA.Set(uint32(sda))
} }
// SPI // SPI
func (spi SPI) setPins(sck, mosi, miso uint8) { func (spi SPI) setPins(sck, mosi, miso Pin) {
if sck == 0 { if sck == 0 {
sck = SPI0_SCK_PIN sck = SPI0_SCK_PIN
} }

Просмотреть файл

@ -10,13 +10,13 @@ import (
const CPU_FREQUENCY = 64000000 const CPU_FREQUENCY = 64000000
// Get peripheral and pin number for this GPIO pin. // Get peripheral and pin number for this GPIO pin.
func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) { func (p Pin) getPortPin() (*nrf.GPIO_Type, uint32) {
return nrf.P0, p.Pin return nrf.P0, uint32(p)
} }
func (uart UART) setPins(tx, rx uint32) { func (uart UART) setPins(tx, rx Pin) {
nrf.UART0.PSELTXD.Set(tx) nrf.UART0.PSELTXD.Set(uint32(tx))
nrf.UART0.PSELRXD.Set(rx) nrf.UART0.PSELRXD.Set(uint32(rx))
} }
//go:export UARTE0_UART0_IRQHandler //go:export UARTE0_UART0_IRQHandler
@ -24,13 +24,13 @@ func handleUART0() {
UART0.handleInterrupt() UART0.handleInterrupt()
} }
func (i2c I2C) setPins(scl, sda uint8) { func (i2c I2C) setPins(scl, sda Pin) {
i2c.Bus.PSELSCL.Set(uint32(scl)) i2c.Bus.PSELSCL.Set(uint32(scl))
i2c.Bus.PSELSDA.Set(uint32(sda)) i2c.Bus.PSELSDA.Set(uint32(sda))
} }
// SPI // SPI
func (spi SPI) setPins(sck, mosi, miso uint8) { func (spi SPI) setPins(sck, mosi, miso Pin) {
if sck == 0 { if sck == 0 {
sck = SPI0_SCK_PIN sck = SPI0_SCK_PIN
} }

Просмотреть файл

@ -10,17 +10,17 @@ import (
const CPU_FREQUENCY = 64000000 const CPU_FREQUENCY = 64000000
// Get peripheral and pin number for this GPIO pin. // Get peripheral and pin number for this GPIO pin.
func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) { func (p Pin) getPortPin() (*nrf.GPIO_Type, uint32) {
if p.Pin >= 32 { if p >= 32 {
return nrf.P1, p.Pin - 32 return nrf.P1, uint32(p - 32)
} else { } else {
return nrf.P0, p.Pin return nrf.P0, uint32(p)
} }
} }
func (uart UART) setPins(tx, rx uint32) { func (uart UART) setPins(tx, rx Pin) {
nrf.UART0.PSEL.TXD.Set(tx) nrf.UART0.PSEL.TXD.Set(uint32(tx))
nrf.UART0.PSEL.RXD.Set(rx) nrf.UART0.PSEL.RXD.Set(uint32(rx))
} }
//go:export UARTE0_UART0_IRQHandler //go:export UARTE0_UART0_IRQHandler
@ -28,13 +28,13 @@ func handleUART0() {
UART0.handleInterrupt() UART0.handleInterrupt()
} }
func (i2c I2C) setPins(scl, sda uint8) { func (i2c I2C) setPins(scl, sda Pin) {
i2c.Bus.PSEL.SCL.Set(uint32(scl)) i2c.Bus.PSEL.SCL.Set(uint32(scl))
i2c.Bus.PSEL.SDA.Set(uint32(sda)) i2c.Bus.PSEL.SDA.Set(uint32(sda))
} }
// SPI // SPI
func (spi SPI) setPins(sck, mosi, miso uint8) { func (spi SPI) setPins(sck, mosi, miso Pin) {
if sck == 0 { if sck == 0 {
sck = SPI0_SCK_PIN sck = SPI0_SCK_PIN
} }

Просмотреть файл

@ -4,10 +4,10 @@ package machine
// Peripheral abstraction layer for the stm32. // Peripheral abstraction layer for the stm32.
type GPIOMode uint8 type PinMode uint8
const ( const (
portA = iota * 16 portA Pin = iota * 16
portB portB
portC portC
portD portD

Просмотреть файл

@ -13,25 +13,25 @@ import (
const CPU_FREQUENCY = 72000000 const CPU_FREQUENCY = 72000000
const ( const (
GPIO_INPUT = 0 // Input mode PinInput PinMode = 0 // Input mode
GPIO_OUTPUT_10MHz = 1 // Output mode, max speed 10MHz PinOutput10MHz PinMode = 1 // Output mode, max speed 10MHz
GPIO_OUTPUT_2MHz = 2 // Output mode, max speed 2MHz PinOutput2MHz PinMode = 2 // Output mode, max speed 2MHz
GPIO_OUTPUT_50MHz = 3 // Output mode, max speed 50MHz PinOutput50MHz PinMode = 3 // Output mode, max speed 50MHz
GPIO_OUTPUT = GPIO_OUTPUT_2MHz PinOutput PinMode = PinOutput2MHz
GPIO_INPUT_MODE_ANALOG = 0 // Input analog mode PinInputModeAnalog PinMode = 0 // Input analog mode
GPIO_INPUT_MODE_FLOATING = 4 // Input floating mode PinInputModeFloating PinMode = 4 // Input floating mode
GPIO_INPUT_MODE_PULL_UP_DOWN = 8 // Input pull up/down mode PinInputModePullUpDown PinMode = 8 // Input pull up/down mode
GPIO_INPUT_MODE_RESERVED = 12 // Input mode (reserved) PinInputModeReserved PinMode = 12 // Input mode (reserved)
GPIO_OUTPUT_MODE_GP_PUSH_PULL = 0 // Output mode general purpose push/pull PinOutputModeGPPushPull PinMode = 0 // Output mode general purpose push/pull
GPIO_OUTPUT_MODE_GP_OPEN_DRAIN = 4 // Output mode general purpose open drain PinOutputModeGPOpenDrain PinMode = 4 // Output mode general purpose open drain
GPIO_OUTPUT_MODE_ALT_PUSH_PULL = 8 // Output mode alt. purpose push/pull PinOutputModeAltPushPull PinMode = 8 // Output mode alt. purpose push/pull
GPIO_OUTPUT_MODE_ALT_OPEN_DRAIN = 12 // Output mode alt. purpose open drain PinOutputModeAltOpenDrain PinMode = 12 // Output mode alt. purpose open drain
) )
func (p GPIO) getPort() *stm32.GPIO_Type { func (p Pin) getPort() *stm32.GPIO_Type {
switch p.Pin / 16 { switch p / 16 {
case 0: case 0:
return stm32.GPIOA return stm32.GPIOA
case 1: case 1:
@ -52,8 +52,8 @@ func (p GPIO) getPort() *stm32.GPIO_Type {
} }
// enableClock enables the clock for this desired GPIO port. // enableClock enables the clock for this desired GPIO port.
func (p GPIO) enableClock() { func (p Pin) enableClock() {
switch p.Pin / 16 { switch p / 16 {
case 0: case 0:
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_IOPAEN) stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_IOPAEN)
case 1: case 1:
@ -74,12 +74,12 @@ func (p GPIO) enableClock() {
} }
// Configure this pin with the given configuration. // Configure this pin with the given configuration.
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
// Configure the GPIO pin. // Configure the GPIO pin.
p.enableClock() p.enableClock()
port := p.getPort() port := p.getPort()
pin := p.Pin % 16 pin := uint8(p) % 16
pos := p.Pin % 8 * 4 pos := uint8(p) % 8 * 4
if pin < 8 { if pin < 8 {
port.CRL.Set((uint32(port.CRL.Get()) &^ (0xf << pos)) | (uint32(config.Mode) << pos)) port.CRL.Set((uint32(port.CRL.Get()) &^ (0xf << pos)) | (uint32(config.Mode) << pos))
} else { } else {
@ -89,9 +89,9 @@ func (p GPIO) Configure(config GPIOConfig) {
// Set the pin to high or low. // Set the pin to high or low.
// Warning: only use this on an output pin! // Warning: only use this on an output pin!
func (p GPIO) Set(high bool) { func (p Pin) Set(high bool) {
port := p.getPort() port := p.getPort()
pin := p.Pin % 16 pin := uint8(p) % 16
if high { if high {
port.BSRR.Set(1 << pin) port.BSRR.Set(1 << pin)
} else { } else {
@ -124,12 +124,12 @@ func (uart UART) Configure(config UARTConfig) {
// use alternate TX/RX pins PB6/PB7 via AFIO mapping // use alternate TX/RX pins PB6/PB7 via AFIO mapping
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_AFIOEN) stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_AFIOEN)
stm32.AFIO.MAPR.SetBits(stm32.AFIO_MAPR_USART1_REMAP) stm32.AFIO.MAPR.SetBits(stm32.AFIO_MAPR_USART1_REMAP)
GPIO{PB6}.Configure(GPIOConfig{Mode: GPIO_OUTPUT_50MHz + GPIO_OUTPUT_MODE_ALT_PUSH_PULL}) PB6.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltPushPull})
GPIO{PB7}.Configure(GPIOConfig{Mode: GPIO_INPUT_MODE_FLOATING}) PB7.Configure(PinConfig{Mode: PinInputModeFloating})
default: default:
// use standard TX/RX pins PA9 and PA10 // use standard TX/RX pins PA9 and PA10
GPIO{UART_TX_PIN}.Configure(GPIOConfig{Mode: GPIO_OUTPUT_50MHz + GPIO_OUTPUT_MODE_ALT_PUSH_PULL}) UART_TX_PIN.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltPushPull})
GPIO{UART_RX_PIN}.Configure(GPIOConfig{Mode: GPIO_INPUT_MODE_FLOATING}) UART_RX_PIN.Configure(PinConfig{Mode: PinInputModeFloating})
} }
// Enable USART1 clock // Enable USART1 clock
@ -183,9 +183,9 @@ var (
// SPIConfig is used to store config info for SPI. // SPIConfig is used to store config info for SPI.
type SPIConfig struct { type SPIConfig struct {
Frequency uint32 Frequency uint32
SCK uint8 SCK Pin
MOSI uint8 MOSI Pin
MISO uint8 MISO Pin
LSBFirst bool LSBFirst bool
Mode uint8 Mode uint8
} }
@ -280,7 +280,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
return byte(spi.Bus.DR.Get()), nil return byte(spi.Bus.DR.Get()), nil
} }
func (spi SPI) setPins(sck, mosi, miso uint8) { func (spi SPI) setPins(sck, mosi, miso Pin) {
if sck == 0 { if sck == 0 {
sck = SPI0_SCK_PIN sck = SPI0_SCK_PIN
} }
@ -291,9 +291,9 @@ func (spi SPI) setPins(sck, mosi, miso uint8) {
miso = SPI0_MISO_PIN miso = SPI0_MISO_PIN
} }
GPIO{sck}.Configure(GPIOConfig{Mode: GPIO_OUTPUT_50MHz + GPIO_OUTPUT_MODE_ALT_PUSH_PULL}) sck.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltPushPull})
GPIO{mosi}.Configure(GPIOConfig{Mode: GPIO_OUTPUT_50MHz + GPIO_OUTPUT_MODE_ALT_PUSH_PULL}) mosi.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltPushPull})
GPIO{miso}.Configure(GPIOConfig{Mode: GPIO_INPUT_MODE_FLOATING}) miso.Configure(PinConfig{Mode: PinInputModeFloating})
} }
// I2C on the STM32F103xx. // I2C on the STM32F103xx.
@ -312,8 +312,8 @@ var (
// I2CConfig is used to store config info for I2C. // I2CConfig is used to store config info for I2C.
type I2CConfig struct { type I2CConfig struct {
Frequency uint32 Frequency uint32
SCL uint8 SCL Pin
SDA uint8 SDA Pin
} }
// Configure is intended to setup the I2C interface. // Configure is intended to setup the I2C interface.
@ -339,8 +339,8 @@ func (i2c I2C) Configure(config I2CConfig) {
config.SCL = SCL_PIN config.SCL = SCL_PIN
} }
GPIO{config.SDA}.Configure(GPIOConfig{Mode: GPIO_OUTPUT_50MHz + GPIO_OUTPUT_MODE_ALT_OPEN_DRAIN}) config.SDA.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltOpenDrain})
GPIO{config.SCL}.Configure(GPIOConfig{Mode: GPIO_OUTPUT_50MHz + GPIO_OUTPUT_MODE_ALT_OPEN_DRAIN}) config.SCL.Configure(PinConfig{Mode: PinOutput50MHz + PinOutputModeAltOpenDrain})
// Disable the selected I2C peripheral to configure // Disable the selected I2C peripheral to configure
i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE) i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE)

Просмотреть файл

@ -13,15 +13,15 @@ const CPU_FREQUENCY = 168000000
const ( const (
// Mode Flag // Mode Flag
GPIO_OUTPUT = 0 PinOutput PinMode = 0
GPIO_INPUT = GPIO_INPUT_PULLDOWN PinInput PinMode = PinInputFloating
GPIO_INPUT_FLOATING = 1 PinInputFloating PinMode = 1
GPIO_INPUT_PULLDOWN = 2 PinInputPulldown PinMode = 2
GPIO_INPUT_PULLUP = 3 PinInputPullup PinMode = 3
// for UART // for UART
GPIO_UART_TX = 4 PinModeUartTX PinMode = 4
GPIO_UART_RX = 5 PinModeUartRX PinMode = 5
//GPIOx_MODER //GPIOx_MODER
GPIO_MODE_INPUT = 0 GPIO_MODE_INPUT = 0
@ -45,8 +45,8 @@ const (
GPIO_PULL_DOWN = 2 GPIO_PULL_DOWN = 2
) )
func (p GPIO) getPort() *stm32.GPIO_Type { func (p Pin) getPort() *stm32.GPIO_Type {
switch p.Pin / 16 { switch p / 16 {
case 0: case 0:
return stm32.GPIOA return stm32.GPIOA
case 1: case 1:
@ -71,8 +71,8 @@ func (p GPIO) getPort() *stm32.GPIO_Type {
} }
// enableClock enables the clock for this desired GPIO port. // enableClock enables the clock for this desired GPIO port.
func (p GPIO) enableClock() { func (p Pin) enableClock() {
switch p.Pin / 16 { switch p / 16 {
case 0: case 0:
stm32.RCC.AHB1ENR.SetBits(stm32.RCC_AHB1ENR_GPIOAEN) stm32.RCC.AHB1ENR.SetBits(stm32.RCC_AHB1ENR_GPIOAEN)
case 1: case 1:
@ -97,40 +97,40 @@ func (p GPIO) enableClock() {
} }
// Configure this pin with the given configuration. // Configure this pin with the given configuration.
func (p GPIO) Configure(config GPIOConfig) { func (p Pin) Configure(config PinConfig) {
// Configure the GPIO pin. // Configure the GPIO pin.
p.enableClock() p.enableClock()
port := p.getPort() port := p.getPort()
pin := p.Pin % 16 pin := uint8(p) % 16
pos := pin * 2 pos := pin * 2
if config.Mode == GPIO_INPUT_FLOATING { if config.Mode == PinInputFloating {
port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_INPUT) << pos))) port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_INPUT) << pos)))
port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_FLOATING) << pos))) port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_FLOATING) << pos)))
} else if config.Mode == GPIO_INPUT_PULLDOWN { } else if config.Mode == PinInputPulldown {
port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_INPUT) << pos))) port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_INPUT) << pos)))
port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_PULL_DOWN) << pos))) port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_PULL_DOWN) << pos)))
} else if config.Mode == GPIO_INPUT_PULLUP { } else if config.Mode == PinInputPullup {
port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_INPUT) << pos))) port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_INPUT) << pos)))
port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_PULL_UP) << pos))) port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_PULL_UP) << pos)))
} else if config.Mode == GPIO_OUTPUT { } else if config.Mode == PinOutput {
port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_GENERAL_OUTPUT) << pos))) port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_GENERAL_OUTPUT) << pos)))
port.OSPEEDR.Set((uint32(port.OSPEEDR.Get())&^(0x3<<pos) | (uint32(GPIO_SPEED_HI) << pos))) port.OSPEEDR.Set((uint32(port.OSPEEDR.Get())&^(0x3<<pos) | (uint32(GPIO_SPEED_HI) << pos)))
} else if config.Mode == GPIO_UART_TX { } else if config.Mode == PinModeUartTX {
port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_ALTERNABTIVE) << pos))) port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_ALTERNABTIVE) << pos)))
port.OSPEEDR.Set((uint32(port.OSPEEDR.Get())&^(0x3<<pos) | (uint32(GPIO_SPEED_HI) << pos))) port.OSPEEDR.Set((uint32(port.OSPEEDR.Get())&^(0x3<<pos) | (uint32(GPIO_SPEED_HI) << pos)))
port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_PULL_UP) << pos))) port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_PULL_UP) << pos)))
p.setAltFunc(0x7) p.setAltFunc(0x7)
} else if config.Mode == GPIO_UART_RX { } else if config.Mode == PinModeUartRX {
port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_ALTERNABTIVE) << pos))) port.MODER.Set((uint32(port.MODER.Get())&^(0x3<<pos) | (uint32(GPIO_MODE_ALTERNABTIVE) << pos)))
port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_FLOATING) << pos))) port.PUPDR.Set((uint32(port.PUPDR.Get())&^(0x3<<pos) | (uint32(GPIO_FLOATING) << pos)))
p.setAltFunc(0x7) p.setAltFunc(0x7)
} }
} }
func (p GPIO) setAltFunc(af uint32) { func (p Pin) setAltFunc(af uint32) {
port := p.getPort() port := p.getPort()
pin := p.Pin % 16 pin := uint8(p) % 16
pos := pin * 4 pos := pin * 4
if pin >= 8 { if pin >= 8 {
port.AFRH.Set(uint32(port.AFRH.Get())&^(0xF<<pos) | ((af & 0xF) << pos)) port.AFRH.Set(uint32(port.AFRH.Get())&^(0xF<<pos) | ((af & 0xF) << pos))
@ -141,13 +141,13 @@ func (p GPIO) setAltFunc(af uint32) {
// Set the pin to high or low. // Set the pin to high or low.
// Warning: only use this on an output pin! // Warning: only use this on an output pin!
func (p GPIO) Set(high bool) { func (p Pin) Set(high bool) {
port := p.getPort() port := p.getPort()
pin := p.Pin % 16 pin := p % 16
if high { if high {
port.BSRR.Set(1 << pin) port.BSRR.Set(1 << uint8(pin))
} else { } else {
port.BSRR.Set(1 << (pin + 16)) port.BSRR.Set(1 << uint8(pin+16))
} }
} }
@ -173,8 +173,8 @@ func (uart UART) Configure(config UARTConfig) {
switch config.TX { switch config.TX {
default: default:
// use standard TX/RX pins PA2 and PA3 // use standard TX/RX pins PA2 and PA3
GPIO{UART_TX_PIN}.Configure(GPIOConfig{Mode: GPIO_UART_TX}) UART_TX_PIN.Configure(PinConfig{Mode: PinModeUartTX})
GPIO{UART_RX_PIN}.Configure(GPIOConfig{Mode: GPIO_UART_RX}) UART_RX_PIN.Configure(PinConfig{Mode: PinModeUartRX})
} }
// Enable USART2 clock // Enable USART2 clock

Просмотреть файл

@ -6,8 +6,8 @@ import "errors"
type UARTConfig struct { type UARTConfig struct {
BaudRate uint32 BaudRate uint32
TX uint8 TX Pin
RX uint8 RX Pin
} }
// To implement the UART interface for a board, you must declare a concrete type as follows: // To implement the UART interface for a board, you must declare a concrete type as follows: