mirror of https://github.com/tinygo-org/tinygo.git
gwtnz
5 years ago
committed by
GitHub
6 changed files with 306 additions and 145 deletions
@ -0,0 +1,45 @@ |
|||
// Hand created file. DO NOT DELETE.
|
|||
// STM32FXXX (except stm32f1xx) bitfield definitions that are not
|
|||
// auto-generated by gen-device-svd.go
|
|||
|
|||
// These apply to the stm32 families that use the MODER, OTYPE amd PUPDR
|
|||
// registers for managing GPIO functionality.
|
|||
|
|||
// Add in other families that use the same settings, e.g. stm32f0xx, etc
|
|||
|
|||
// +build stm32,!stm32f103xx
|
|||
|
|||
package stm32 |
|||
|
|||
// AltFunc represents the alternate function peripherals that can be mapped to
|
|||
// the GPIO ports. Since these differ by what is supported on the stm32 family
|
|||
// they are defined in the more specific files
|
|||
type AltFunc uint8 |
|||
|
|||
// Family-wide common post-reset AltFunc. This represents
|
|||
// normal GPIO operation of the pins
|
|||
const AF0_SYSTEM AltFunc = 0 |
|||
|
|||
const ( |
|||
// Register values for the chip
|
|||
// GPIOx_MODER
|
|||
GPIOModeInput = 0 |
|||
GPIOModeOutputGeneral = 1 |
|||
GPIOModeOutputAltFunc = 2 |
|||
GPIOModeAnalog = 3 |
|||
|
|||
// GPIOx_OTYPER
|
|||
GPIOOutputTypePushPull = 0 |
|||
GPIOOutputTypeOpenDrain = 1 |
|||
|
|||
// GPIOx_OSPEEDR
|
|||
GPIOSpeedLow = 0 |
|||
GPIOSpeedMid = 1 |
|||
GPIOSpeedHigh = 2 // Note: this is also low speed on stm32f0, see RM0091
|
|||
GPIOSpeedVeryHigh = 3 |
|||
|
|||
// GPIOx_PUPDR
|
|||
GPIOPUPDRFloating = 0 |
|||
GPIOPUPDRPullUp = 1 |
|||
GPIOPUPDRPullDown = 2 |
|||
) |
@ -0,0 +1,26 @@ |
|||
// These are the supported alternate function numberings on the stm32f407
|
|||
// +build stm32,stm32f407
|
|||
|
|||
// Alternate function settings on the stm32f4xx series
|
|||
|
|||
package stm32 |
|||
|
|||
const ( |
|||
// Alternative peripheral pin functions
|
|||
// AF0_SYSTEM is defined im the common bitfields package
|
|||
AF1_TIM1_2 AltFunc = 1 |
|||
AF2_TIM3_4_5 = 2 |
|||
AF3_TIM8_9_10_11 = 3 |
|||
AF4_I2C1_2_3 = 4 |
|||
AF5_SPI1_SPI2 = 5 |
|||
AF6_SPI3 = 6 |
|||
AF7_USART1_2_3 = 7 |
|||
AF8_USART4_5_6 = 8 |
|||
AF9_CAN1_CAN2_TIM12_13_14 = 9 |
|||
AF10_OTG_FS_OTG_HS = 10 |
|||
AF11_ETH = 11 |
|||
AF12_FSMC_SDIO_OTG_HS_1 = 12 |
|||
AF13_DCMI = 13 |
|||
AF14 = 14 |
|||
AF15_EVENTOUT = 15 |
|||
) |
@ -0,0 +1,128 @@ |
|||
// +build stm32,!stm32f103xx
|
|||
|
|||
package machine |
|||
|
|||
import ( |
|||
"device/stm32" |
|||
) |
|||
|
|||
// GPIO for the stm32 families except the stm32f1xx which uses a simpler but
|
|||
// less flexible mechanism. Extend the +build directive above to exclude other
|
|||
// models in the stm32f1xx series as necessary
|
|||
|
|||
const ( |
|||
// Mode Flag
|
|||
PinOutput PinMode = 0 |
|||
PinInput PinMode = PinInputFloating |
|||
PinInputFloating PinMode = 1 |
|||
PinInputPulldown PinMode = 2 |
|||
PinInputPullup PinMode = 3 |
|||
|
|||
// for UART
|
|||
PinModeUARTTX PinMode = 4 |
|||
PinModeUARTRX PinMode = 5 |
|||
|
|||
// for I2C
|
|||
PinModeI2CSCL PinMode = 6 |
|||
PinModeI2CSDA PinMode = 7 |
|||
|
|||
// for SPI
|
|||
PinModeSPICLK PinMode = 8 |
|||
PinModeSPIMOSI PinMode = 9 |
|||
PinModeSPIMISO PinMode = 10 |
|||
|
|||
// for analog/ADC
|
|||
PinInputAnalog PinMode = 11 |
|||
|
|||
// for PWM
|
|||
// TBD
|
|||
) |
|||
|
|||
// Configure this pin with the given configuration
|
|||
func (p Pin) Configure(config PinConfig) { |
|||
// Use the default system alternate function; this
|
|||
// will only be used if you try to call this with
|
|||
// one of the peripheral modes instead of vanilla GPIO.
|
|||
p.ConfigureAltFunc(config, stm32.AF0_SYSTEM) |
|||
} |
|||
|
|||
// Configure this pin with the given configuration including alternate
|
|||
// function mapping if necessary.
|
|||
func (p Pin) ConfigureAltFunc(config PinConfig, altFunc stm32.AltFunc) { |
|||
// Configure the GPIO pin.
|
|||
p.enableClock() |
|||
port := p.getPort() |
|||
pin := uint8(p) % 16 |
|||
pos := pin * 2 |
|||
|
|||
switch config.Mode { |
|||
|
|||
// GPIO
|
|||
case PinInputFloating: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeInput, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRFloating, 0x3, pos) |
|||
case PinInputPulldown: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeInput, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRPullDown, 0x3, pos) |
|||
case PinInputPullup: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeInput, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRPullUp, 0x3, pos) |
|||
case PinOutput: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputGeneral, 0x3, pos) |
|||
port.OSPEEDR.ReplaceBits(stm32.GPIOSpeedHigh, 0x3, pos) |
|||
|
|||
// UART
|
|||
case PinModeUARTTX: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputAltFunc, 0x3, pos) |
|||
port.OSPEEDR.ReplaceBits(stm32.GPIOSpeedHigh, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRPullUp, 0x3, pos) |
|||
p.SetAltFunc(altFunc) |
|||
case PinModeUARTRX: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputAltFunc, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRFloating, 0x3, pos) |
|||
p.SetAltFunc(altFunc) |
|||
|
|||
// I2C)
|
|||
case PinModeI2CSCL: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputAltFunc, 0x3, pos) |
|||
port.OTYPER.ReplaceBits(stm32.GPIOOutputTypeOpenDrain, 0x1, pos) |
|||
port.OSPEEDR.ReplaceBits(stm32.GPIOSpeedLow, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRFloating, 0x3, pos) |
|||
p.SetAltFunc(altFunc) |
|||
case PinModeI2CSDA: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputAltFunc, 0x3, pos) |
|||
port.OTYPER.ReplaceBits(stm32.GPIOOutputTypeOpenDrain, 0x1, pos) |
|||
port.OSPEEDR.ReplaceBits(stm32.GPIOSpeedLow, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRFloating, 0x3, pos) |
|||
p.SetAltFunc(altFunc) |
|||
|
|||
// SPI
|
|||
case PinModeSPICLK: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputAltFunc, 0x3, pos) |
|||
port.OSPEEDR.ReplaceBits(stm32.GPIOSpeedLow, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRFloating, 0x3, pos) |
|||
p.SetAltFunc(altFunc) |
|||
case PinModeSPIMOSI: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputAltFunc, 0x3, pos) |
|||
port.OSPEEDR.ReplaceBits(stm32.GPIOSpeedLow, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRFloating, 0x3, pos) |
|||
p.SetAltFunc(altFunc) |
|||
case PinModeSPIMISO: |
|||
port.MODER.ReplaceBits(stm32.GPIOModeOutputAltFunc, 0x3, pos) |
|||
port.OSPEEDR.ReplaceBits(stm32.GPIOSpeedLow, 0x3, pos) |
|||
port.PUPDR.ReplaceBits(stm32.GPIOPUPDRFloating, 0x3, pos) |
|||
p.SetAltFunc(altFunc) |
|||
} |
|||
} |
|||
|
|||
// SetAltFunc maps the given alternative function to the I/O pin
|
|||
func (p Pin) SetAltFunc(af stm32.AltFunc) { |
|||
port := p.getPort() |
|||
pin := uint8(p) % 16 |
|||
pos := (pin % 8) * 4 |
|||
if pin < 8 { |
|||
port.AFRL.ReplaceBits(uint32(af), 0xf, pos) |
|||
} else { |
|||
port.AFRH.ReplaceBits(uint32(af), 0xf, pos) |
|||
} |
|||
} |
Loading…
Reference in new issue