// +build stm32,stm32f407 package machine // Peripheral abstraction layer for the stm32. import ( "device/arm" "device/stm32" ) const CPU_FREQUENCY = 168000000 const ( // Mode Flag GPIO_OUTPUT = 0 GPIO_INPUT = GPIO_INPUT_PULLDOWN GPIO_INPUT_FLOATING = 1 GPIO_INPUT_PULLDOWN = 2 GPIO_INPUT_PULLUP = 3 // for UART GPIO_UART_TX = 4 GPIO_UART_RX = 5 //GPIOx_MODER GPIO_MODE_INPUT = 0 GPIO_MODE_GENERAL_OUTPUT = 1 GPIO_MODE_ALTERNABTIVE = 2 GPIO_MODE_ANALOG = 3 //GPIOx_OTYPER GPIO_OUTPUT_MODE_PUSH_PULL = 0 GPIO_OUTPUT_MODE_OPEN_DRAIN = 1 // GPIOx_OSPEEDR GPIO_SPEED_LOW = 0 GPIO_SPEED_MID = 1 GPIO_SPEED_HI = 2 GPIO_SPEED_VERY_HI = 3 // GPIOx_PUPDR GPIO_FLOATING = 0 GPIO_PULL_UP = 1 GPIO_PULL_DOWN = 2 ) func (p GPIO) getPort() *stm32.GPIO_Type { switch p.Pin / 16 { case 0: return stm32.GPIOA case 1: return stm32.GPIOB case 2: return stm32.GPIOC case 3: return stm32.GPIOD case 4: return stm32.GPIOE case 5: return stm32.GPIOF case 6: return stm32.GPIOG case 7: return stm32.GPIOH case 8: return stm32.GPIOI default: panic("machine: unknown port") } } // enableClock enables the clock for this desired GPIO port. func (p GPIO) enableClock() { switch p.Pin / 16 { case 0: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOAEN case 1: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOBEN case 2: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOCEN case 3: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIODEN case 4: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOEEN case 5: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOFEN case 6: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOGEN case 7: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOHEN case 8: stm32.RCC.AHB1ENR |= stm32.RCC_AHB1ENR_GPIOIEN default: panic("machine: unknown port") } } // Configure this pin with the given configuration. func (p GPIO) Configure(config GPIOConfig) { // Configure the GPIO pin. p.enableClock() port := p.getPort() pin := p.Pin % 16 pos := pin * 2 if config.Mode == GPIO_INPUT_FLOATING { port.MODER = stm32.RegValue((uint32(port.MODER)&^(0x3<= 8 { port.AFRH = stm32.RegValue(uint32(port.AFRH)&^(0xF<