[platform][stm32] add generic gpio driver

-can be used for simple pin muxing setup as well
This commit is contained in:
Travis Geiselbrecht
2012-07-21 13:10:46 -07:00
parent e0b986f29a
commit 3c4a11795e
7 changed files with 134 additions and 5 deletions

View File

@@ -347,7 +347,7 @@ typedef enum
void GPIO_DeInit(GPIO_TypeDef* GPIOx);
void GPIO_AFIODeInit(void);
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
void GPIO_Init(GPIO_TypeDef* GPIOx, const GPIO_InitTypeDef* GPIO_InitStruct);
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);

View File

@@ -169,7 +169,7 @@ void GPIO_AFIODeInit(void)
* contains the configuration information for the specified GPIO peripheral.
* @retval None
*/
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
void GPIO_Init(GPIO_TypeDef* GPIOx, const GPIO_InitTypeDef* GPIO_InitStruct)
{
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;

104
platform/stm32f1xx/gpio.c Normal file
View File

@@ -0,0 +1,104 @@
/*
* Copyright (c) 2012 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <debug.h>
#include <assert.h>
#include <dev/gpio.h>
#include <platform/stm32.h>
#include <platform/gpio.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_rcc.h>
static GPIO_TypeDef *port_to_pointer(unsigned int port)
{
switch (port) {
default:
case GPIO_PORT_A: return GPIOA;
case GPIO_PORT_B: return GPIOB;
case GPIO_PORT_C: return GPIOC;
case GPIO_PORT_D: return GPIOD;
case GPIO_PORT_E: return GPIOE;
case GPIO_PORT_F: return GPIOF;
case GPIO_PORT_G: return GPIOG;
}
}
static void enable_port(unsigned int port)
{
DEBUG_ASSERT(port <= GPIO_PORT_G);
/* happens to be the RCC ids are sequential bits, so we can start from A and shift */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA << port, ENABLE);
}
void stm32_gpio_early_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
}
int gpio_config(unsigned nr, unsigned flags)
{
uint port = GPIO_PORT(nr);
uint pin = GPIO_PIN(nr);
enable_port(port);
GPIO_InitTypeDef init;
init.GPIO_Speed = GPIO_Speed_50MHz;
init.GPIO_Pin = (1 << pin);
if (flags & GPIO_STM32_AF) {
if (flags & GPIO_STM32_OD)
init.GPIO_Mode = GPIO_Mode_Out_OD;
else
init.GPIO_Mode = GPIO_Mode_AF_PP;
} else if (flags & GPIO_OUTPUT) {
if (flags & GPIO_STM32_OD)
init.GPIO_Mode = GPIO_Mode_Out_OD;
else
init.GPIO_Mode = GPIO_Mode_Out_PP;
} else { // GPIO_INPUT
if (flags & GPIO_PULLUP) {
init.GPIO_Mode = GPIO_Mode_IPU;
} else if (flags & GPIO_PULLDOWN) {
init.GPIO_Mode = GPIO_Mode_IPD;
} else {
init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
}
}
GPIO_Init(port_to_pointer(port), &init);
return 0;
}
void gpio_set(unsigned nr, unsigned on)
{
GPIO_WriteBit(port_to_pointer(GPIO_PORT(nr)), 1 << GPIO_PIN(nr), on);
}
int gpio_get(unsigned nr)
{
return GPIO_ReadInputDataBit(port_to_pointer(GPIO_PORT(nr)), 1 << GPIO_PIN(nr));
}

View File

@@ -0,0 +1,25 @@
#ifndef __PLATFORM_STM32_GPIO_H
#define __PLATFORM_STM32_GPIO_H
/* helper defines for STM32 platforms */
/* flag to gpio_configure */
#define GPIO_STM32_AF (0x1 << 16)
#define GPIO_STM32_OD (0x2 << 16)
/* gpio port/pin is packed into a single unsigned int in 16x:8port:8pin format */
#define GPIO(port, pin) ((unsigned int)(((port) << 8) | (pin)))
#define GPIO_PORT(gpio) (((gpio) >> 8) & 0xff)
#define GPIO_PIN(gpio) ((gpio) & 0xff)
#define GPIO_PORT_A 0
#define GPIO_PORT_B 1
#define GPIO_PORT_C 2
#define GPIO_PORT_D 3
#define GPIO_PORT_E 4
#define GPIO_PORT_F 5
#define GPIO_PORT_G 6
#endif

View File

@@ -27,9 +27,7 @@ void stm32_debug_early_init(void);
void stm32_debug_init(void);
void stm32_timer_early_init(void);
void stm32_timer_init(void);
// XXX refactor this into a proper usart driver
void stm32_debug_rx_irq(void);
void stm32_gpio_early_init(void);
#endif

View File

@@ -33,6 +33,7 @@ void platform_early_init(void)
SystemInit();
stm32_timer_early_init();
stm32_gpio_early_init();
}
void platform_init(void)

View File

@@ -46,6 +46,7 @@ OBJS += \
$(LOCAL_DIR)/uart.o \
$(LOCAL_DIR)/timer.o \
$(LOCAL_DIR)/vectab.o \
$(LOCAL_DIR)/gpio.o \
# $(LOCAL_DIR)/debug.o \
$(LOCAL_DIR)/interrupts.o \