1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/cpu/stm32f0/periph/gpio.c

215 lines
5.2 KiB
C
Raw Normal View History

2014-04-17 19:41:48 +02:00
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup cpu_stm32f0
* @{
*
* @file
* @brief Low-level GPIO driver implementation
*
* @author Hauke Petersen <mail@haukepetersen.de>
2014-04-17 19:41:48 +02:00
*
* @}
*/
#include "cpu.h"
#include "sched.h"
#include "thread.h"
2014-04-17 19:41:48 +02:00
#include "periph/gpio.h"
#include "periph_conf.h"
/**
* @brief The STM32F0 family has 16 external interrupt lines
*/
#define EXTI_NUMOF (16U)
/**
* @brief Allocate memory for one callback and argument per EXTI channel
*/
static gpio_isr_ctx_t isr_ctx[EXTI_NUMOF];
/**
* @brief Extract the port base address from the given pin identifier
*/
static inline GPIO_TypeDef *_port(gpio_t pin)
{
return (GPIO_TypeDef *)(pin & ~(0x0f));
}
/**
* @brief Extract the port number form the given identifier
*
* The port number is extracted by looking at bits 10, 11, 12, 13 of the base
* register addresses.
*/
static inline int _port_num(gpio_t pin)
{
return ((pin >> 10) & 0x0f);
}
/**
* @brief Extract the pin number from the last 4 bit of the pin identifier
*/
static inline int _pin_num(gpio_t pin)
{
return (pin & 0x0f);
}
2014-04-17 19:41:48 +02:00
2016-02-20 16:46:35 +01:00
int gpio_init(gpio_t pin, gpio_mode_t mode)
{
GPIO_TypeDef *port = _port(pin);
int pin_num = _pin_num(pin);
/* enable clock */
RCC->AHBENR |= (RCC_AHBENR_GPIOAEN << _port_num(pin));
2016-02-20 16:46:35 +01:00
/* set mode */
port->MODER &= ~(0x3 << (2 * pin_num));
port->MODER |= ((mode & 0x3) << (2 * pin_num));
/* set pull resistor configuration */
port->PUPDR &= ~(0x3 << (2 * pin_num));
port->PUPDR |= (((mode >> 2) & 0x3) << (2 * pin_num));
/* set output mode */
port->OTYPER &= ~(1 << pin_num);
port->OTYPER |= (((mode >> 4) & 0x1) << pin_num);
/* finally set pin speed to maximum and reset output */
port->OSPEEDR |= (3 << (2 * pin_num));
port->BRR = (1 << pin_num);
return 0;
2014-04-17 19:41:48 +02:00
}
2016-02-20 16:46:35 +01:00
int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
gpio_cb_t cb, void *arg)
2014-04-17 19:41:48 +02:00
{
int pin_num = _pin_num(pin);
int port_num = _port_num(pin);
/* set callback */
isr_ctx[pin_num].cb = cb;
isr_ctx[pin_num].arg = arg;
2014-04-17 19:41:48 +02:00
/* enable clock of the SYSCFG module for EXTI configuration */
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGCOMPEN;
/* initialize pin as input */
2016-02-20 16:46:35 +01:00
gpio_init(pin, mode);
2014-04-17 19:41:48 +02:00
/* enable global pin interrupt */
if (pin_num < 2) {
NVIC_EnableIRQ(EXTI0_1_IRQn);
}
else if (pin_num < 4) {
NVIC_EnableIRQ(EXTI2_3_IRQn);
}
else {
NVIC_EnableIRQ(EXTI4_15_IRQn);
2014-04-17 19:41:48 +02:00
}
/* configure the active flank */
EXTI->RTSR &= ~(1 << pin_num);
EXTI->RTSR |= ((flank & 0x1) << pin_num);
EXTI->FTSR &= ~(1 << pin_num);
EXTI->FTSR |= ((flank >> 1) << pin_num);
/* enable specific pin as exti sources */
SYSCFG->EXTICR[pin_num >> 2] &= ~(0xf << ((pin_num & 0x03) * 4));
SYSCFG->EXTICR[pin_num >> 2] |= (port_num << ((pin_num & 0x03) * 4));
2014-04-17 19:41:48 +02:00
/* clear any pending requests */
EXTI->PR = (1 << pin);
2014-04-17 19:41:48 +02:00
/* unmask the pins interrupt channel */
EXTI->IMR |= (1 << pin);
return 0;
}
void gpio_init_af(gpio_t pin, gpio_af_t af)
{
GPIO_TypeDef *port = _port(pin);
uint32_t pin_num = _pin_num(pin);
/* set pin to AF mode */
port->MODER &= ~(3 << (2 * pin_num));
port->MODER |= (2 << (2 * pin_num));
/* set selected function */
port->AFR[(pin_num > 7) ? 1 : 0] &= ~(0xf << ((pin_num & 0x07) * 4));
port->AFR[(pin_num > 7) ? 1 : 0] |= (af << ((pin_num & 0x07) * 4));
}
2016-02-13 13:05:21 +01:00
void gpio_init_analog(gpio_t pin)
{
/* enable clock, needed as this function can be used without calling
* gpio_init first */
RCC->AHBENR |= (RCC_AHBENR_GPIOAEN << _port_num(pin));
/* set to analog mode */
_port(pin)->MODER |= (0x3 << (2 * _pin_num(pin)));
}
void gpio_irq_enable(gpio_t pin)
{
EXTI->IMR |= (1 << _pin_num(pin));
}
void gpio_irq_disable(gpio_t pin)
2014-04-17 19:41:48 +02:00
{
EXTI->IMR &= ~(1 << _pin_num(pin));
}
2014-04-17 19:41:48 +02:00
int gpio_read(gpio_t pin)
{
if (_port(pin)->MODER & (0x3 << (_pin_num(pin) * 2))) {
return _port(pin)->ODR & (1 << _pin_num(pin));
}
else {
return _port(pin)->IDR & (1 << _pin_num(pin));
2014-04-17 19:41:48 +02:00
}
}
void gpio_set(gpio_t pin)
2014-04-17 19:41:48 +02:00
{
_port(pin)->BSRR = (1 << _pin_num(pin));
2014-04-17 19:41:48 +02:00
}
void gpio_clear(gpio_t pin)
2014-04-17 19:41:48 +02:00
{
_port(pin)->BRR = (1 << _pin_num(pin));
2014-04-17 19:41:48 +02:00
}
void gpio_toggle(gpio_t pin)
2014-04-17 19:41:48 +02:00
{
if (gpio_read(pin)) {
_port(pin)->BRR = (1 << _pin_num(pin));
2014-04-17 19:41:48 +02:00
} else {
_port(pin)->BSRR = (1 << _pin_num(pin));
2014-04-17 19:41:48 +02:00
}
}
void gpio_write(gpio_t pin, int value)
2014-04-17 19:41:48 +02:00
{
if (value) {
_port(pin)->BSRR = (1 << _pin_num(pin));
2014-04-17 19:41:48 +02:00
} else {
_port(pin)->BRR = (1 << _pin_num(pin));
2014-04-17 19:41:48 +02:00
}
}
void isr_exti(void)
2014-04-17 19:41:48 +02:00
{
/* only generate interrupts against lines which have their IMR set */
uint32_t pending_isr = (EXTI->PR & EXTI->IMR);
for (size_t i = 0; i < EXTI_NUMOF; i++) {
if (pending_isr & (1 << i)) {
EXTI->PR = (1 << i); /* clear by writing a 1 */
isr_ctx[i].cb(isr_ctx[i].arg);
}
}
if (sched_context_switch_request) {
thread_yield();
2014-04-17 19:41:48 +02:00
}
}