132 lines
2.9 KiB
C
Raw Normal View History

2018-03-01 13:03:40 -05:00
/**
* @file gpio.h
* Abstracts gpio access, makes things easier
2018-03-25 21:16:33 -04:00
*
* Copyright (C) 2018 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-03-01 13:03:40 -05:00
*/
2018-01-04 11:47:43 -05:00
#ifndef GPIO_H_
#define GPIO_H_
#include <stm32l476xx.h>
2018-03-01 13:03:40 -05:00
/**
* Helps simplify gpio calls.
* @param p port, e.g. GPIOA
* @param b pin, e.g. 4
*/
2018-01-04 11:47:43 -05:00
#define GPIO_PORT(p, b) GPIO##p, b
2018-03-01 13:03:40 -05:00
/**
* Defines possible modes for a gpio pin
*/
2018-01-04 11:47:43 -05:00
enum GPIO_MODE
{
2018-03-01 13:03:40 -05:00
INPUT = 0, /**< digital input */
OUTPUT, /**< digital output */
ALTERNATE, /**< alternate function */
ANALOG /**< analog function */
2018-01-04 11:47:43 -05:00
};
2018-03-01 13:03:40 -05:00
/**
* Defines whether to use push-pull or open drain.
*/
2018-01-04 11:47:43 -05:00
enum GPIO_TYPE
{
2018-03-01 13:03:40 -05:00
PUSHPULL = 0, /**< push-pull */
OPENDRAIN /**< open drain */
2018-01-04 11:47:43 -05:00
};
2018-03-01 13:03:40 -05:00
/**
* Defines the pin's speed
*/
2018-01-04 11:47:43 -05:00
enum GPIO_SPEED
{
2018-03-01 13:03:40 -05:00
LOW = 0, /**< low */
MEDIUM, /**< medium */
HIGH, /**< high */
VERYHIGH /**< very high/maximum */
2018-01-04 11:47:43 -05:00
};
2018-03-01 13:03:40 -05:00
/**
* Defines if a pullup or pulldown should be used.
*/
2018-01-04 11:47:43 -05:00
enum GPIO_PUPD
{
2018-03-01 13:03:40 -05:00
NOPUPD, /**< no pullup/pulldown */
PULLUP, /**< use pullup */
PULLDOWN /**< use pulldown */
2018-01-04 11:47:43 -05:00
};
2018-03-01 13:03:40 -05:00
/**
* Initializes the gpio.
*/
2018-01-04 11:47:43 -05:00
void gpio_init(void);
2018-03-01 13:03:40 -05:00
/**
* Enables or disables pullup/pulldown for the given pin.
* @param port the port, e.g. GPIOA
* @param pin the pin
* @param pupd pullup/pulldown enable
* @see GPIO_PUPD
*/
void gpio_pupd(GPIO_TypeDef *port, uint32_t pin, uint32_t pupd);
2018-03-01 13:03:40 -05:00
/**
* Sets whether to use push-pull or open drain for the given pin.
* @param port the port
* @param pin the pin
* @param type what to use
* @see GPIO_TYPE
*/
void gpio_type(GPIO_TypeDef *port, uint32_t pin, uint32_t type);
2018-03-01 13:03:40 -05:00
/**
* Sets the pin's speed.
* @param port the port
* @param pin the pin
* @param speed the speed to use
* @see GPIO_SPEED
*/
void gpio_speed(GPIO_TypeDef *port, uint32_t pin, uint32_t speed);
2018-03-01 13:03:40 -05:00
/**
* Sets the pin's i/o mode.
* @param port the port
* @param pin the pin
* @param mode the mode to use
* @see GPIO_MODE
*/
void gpio_mode(GPIO_TypeDef *port, uint32_t pin, uint32_t mode);
2018-03-01 13:03:40 -05:00
/**
* Sets the state of a digital output pin.
* @param port the port
* @param pin the pin
* @param val non-zero for high, zero for low
*/
void gpio_dout(GPIO_TypeDef *port, uint32_t pin, uint32_t val);
2018-03-01 13:03:40 -05:00
/**
* Reads a digital input pin.
* @param port the port
* @param pin the pin
* @return non-zero for high, zero for low
*/
uint32_t gpio_din(GPIO_TypeDef *port, uint32_t pin);
2018-01-04 11:47:43 -05:00
#endif // GPIO_H_