From 4ab5c94b90a3812de6d3b65c5cf16c8dcdbd34a6 Mon Sep 17 00:00:00 2001 From: Ken Sarkies Date: Sat, 3 Nov 2012 15:56:35 +1030 Subject: Proposed deduplication of peripherals, GPIO example See rambling post on dev list for justification --- lib/stm32/f1/gpio.c | 138 ++-------------------------------------------------- 1 file changed, 5 insertions(+), 133 deletions(-) (limited to 'lib/stm32/f1/gpio.c') diff --git a/lib/stm32/f1/gpio.c b/lib/stm32/f1/gpio.c index 0602012..f0b7f70 100644 --- a/lib/stm32/f1/gpio.c +++ b/lib/stm32/f1/gpio.c @@ -1,4 +1,4 @@ -/** @defgroup STM32F1xx_gpio_file GPIO +/** @defgroup gpio_file GPIO @ingroup STM32F1xx @@ -11,9 +11,6 @@ @date 18 August 2012 -This library supports the General Purpose I/O System in the STM32F1xx series -of ARM Cortex Microcontrollers by ST Microelectronics. - Each I/O port has 16 individually configurable bits. Many I/O pins share GPIO functionality with a number of alternate functions and must be configured to the alternate function mode if these are to be accessed. A feature is available to @@ -55,7 +52,7 @@ Example 1: Digital input on port C12 @endcode LGPL License Terms @ref lgpl_license - */ +*/ /* * This file is part of the libopencm3 project. * @@ -75,25 +72,10 @@ LGPL License Terms @ref lgpl_license * along with this library. If not, see . */ -/* - * Basic GPIO handling API. - * - * Examples: - * gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, - * GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); - * gpio_set(GPIOB, GPIO4); - * gpio_clear(GPIOG, GPIO2 | GPIO9); - * gpio_get(GPIOC, GPIO1); - * gpio_toggle(GPIOA, GPIO7 | GPIO8); - * reg16 = gpio_port_read(GPIOD); - * gpio_port_write(GPIOF, 0xc8fe); - * - * TODO: - * - GPIO remapping support - */ -/**@{*/ +#include +#include -#include +/**@{*/ /*-----------------------------------------------------------------------------*/ /** @brief Set GPIO Pin Mode @@ -145,116 +127,6 @@ void gpio_set_mode(u32 gpioport, u8 mode, u8 cnf, u16 gpios) GPIO_CRH(gpioport) = crh; } -/*-----------------------------------------------------------------------------*/ -/** @brief Set a Group of Pins Atomic - -Set one or more pins of the given GPIO port to 1 in an atomic operation. - -@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id -@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id - If multiple pins are to be changed, use logical OR '|' to separate them. -*/ -void gpio_set(u32 gpioport, u16 gpios) -{ - GPIO_BSRR(gpioport) = gpios; -} - -/*-----------------------------------------------------------------------------*/ -/** @brief Clear a Group of Pins Atomic - -Clear one or more pins of the given GPIO port to 0 in an atomic operation. - -@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id -@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id - If multiple pins are to be changed, use logical OR '|' to separate them. -*/ -void gpio_clear(u32 gpioport, u16 gpios) -{ - GPIO_BRR(gpioport) = gpios; -} - -/*-----------------------------------------------------------------------------*/ -/** @brief Read a Group of Pins. - -@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id -@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id - If multiple pins are to be read, use logical OR '|' to separate them. -@return Unsigned int16 value of the pin values. The bit position of the pin value - returned corresponds to the pin number. -*/ -u16 gpio_get(u32 gpioport, u16 gpios) -{ - return gpio_port_read(gpioport) & gpios; -} - -/*-----------------------------------------------------------------------------*/ -/** @brief Toggle a Group of Pins - -Toggle one or more pins of the given GPIO port. This is not an atomic operation. - -@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id -@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id - If multiple pins are to be changed, use logical OR '|' to separate them. -*/ -void gpio_toggle(u32 gpioport, u16 gpios) -{ - GPIO_ODR(gpioport) ^= gpios; -} - -/*-----------------------------------------------------------------------------*/ -/** @brief Read from a Port - -Read the current value of the given GPIO port. Only the lower 16 bits contain -valid pin data. - -@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id -@return Unsigned int16. The value held in the specified GPIO port. -*/ -u16 gpio_port_read(u32 gpioport) -{ - return (u16)GPIO_IDR(gpioport); -} - -/*-----------------------------------------------------------------------------*/ -/** @brief Write to a Port - -Write a value to the given GPIO port. - -@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id -@param[in] data Unsigned int16. The value to be written to the GPIO port. -*/ -void gpio_port_write(u32 gpioport, u16 data) -{ - GPIO_ODR(gpioport) = data; -} - -/*-----------------------------------------------------------------------------*/ -/** @brief Lock the Configuration of a Group of Pins - -The configuration of one or more pins of the given GPIO port is locked. There is -no mechanism to unlock these via software. Unlocking occurs at the next reset. - -@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id -@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id - If multiple pins are to be locked, use logical OR '|' to separate them. -*/ -void gpio_port_config_lock(u32 gpioport, u16 gpios) -{ - u32 reg32; - - /* Special "Lock Key Writing Sequence", see datasheet. */ - GPIO_LCKR(gpioport) = GPIO_LCKK | gpios; /* Set LCKK. */ - GPIO_LCKR(gpioport) = ~GPIO_LCKK & gpios; /* Clear LCKK. */ - GPIO_LCKR(gpioport) = GPIO_LCKK | gpios; /* Set LCKK. */ - reg32 = GPIO_LCKR(gpioport); /* Read LCKK. */ - reg32 = GPIO_LCKR(gpioport); /* Read LCKK again. */ - - /* Tell the compiler the variable is actually used. It will get optimized out anyways. */ - reg32 = reg32; - - /* If (reg32 & GPIO_LCKK) is true, the lock is now active. */ -} - /*-----------------------------------------------------------------------------*/ /** @brief Map the EVENTOUT signal -- cgit v1.2.3