You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.0 KiB
119 lines
3.0 KiB
16 years ago
|
/*
|
||
14 years ago
|
* This file is part of the libopencm3 project.
|
||
16 years ago
|
*
|
||
|
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
|
||
|
*
|
||
16 years ago
|
* This program is free software: you can redistribute it and/or modify
|
||
16 years ago
|
* it under the terms of the GNU General Public License as published by
|
||
16 years ago
|
* the Free Software Foundation, either version 3 of the License, or
|
||
16 years ago
|
* (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
|
||
16 years ago
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
16 years ago
|
*/
|
||
|
|
||
16 years ago
|
/*
|
||
|
* 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);
|
||
16 years ago
|
* gpio_get(GPIOC, GPIO1);
|
||
14 years ago
|
* gpio_toggle(GPIOA, GPIO7 | GPIO8);
|
||
16 years ago
|
* reg16 = gpio_port_read(GPIOD);
|
||
|
* gpio_port_write(GPIOF, 0xc8fe);
|
||
|
*
|
||
|
* TODO:
|
||
|
* - GPIO remapping support
|
||
|
*/
|
||
|
|
||
13 years ago
|
#include <libopencm3/stm32/f1/gpio.h>
|
||
16 years ago
|
|
||
16 years ago
|
void gpio_set_mode(u32 gpioport, u8 mode, u8 cnf, u16 gpios)
|
||
|
{
|
||
15 years ago
|
u16 i, offset = 0;
|
||
|
u32 crl = 0, crh = 0, tmp32 = 0;
|
||
16 years ago
|
|
||
15 years ago
|
/*
|
||
|
* We want to set the config only for the pins mentioned in gpios,
|
||
|
* but keeping the others, so read out the actual config first.
|
||
|
*/
|
||
|
crl = GPIO_CRL(gpioport);
|
||
|
crh = GPIO_CRH(gpioport);
|
||
|
|
||
|
/* Iterate over all bits, use i as the bitnumber. */
|
||
16 years ago
|
for (i = 0; i < 16; i++) {
|
||
15 years ago
|
/* Only set the config if the bit is set in gpios. */
|
||
|
if (!((1 << i) & gpios))
|
||
16 years ago
|
continue;
|
||
15 years ago
|
|
||
|
/* Calculate bit offset. */
|
||
16 years ago
|
offset = (i < 8) ? (i * 4) : ((i - 8) * 4);
|
||
15 years ago
|
|
||
|
/* Use tmp32 to either modify crl or crh. */
|
||
|
tmp32 = (i < 8) ? crl : crh;
|
||
|
|
||
|
/* Modify bits are needed. */
|
||
|
tmp32 &= ~(0b1111 << offset); /* Clear the bits first. */
|
||
|
tmp32 |= (mode << offset) | (cnf << (offset + 2));
|
||
|
|
||
|
/* Write tmp32 into crl or crh, leave the other unchanged. */
|
||
|
crl = (i < 8) ? tmp32 : crl;
|
||
|
crh = (i >= 8) ? tmp32 : crh;
|
||
16 years ago
|
}
|
||
|
|
||
|
GPIO_CRL(gpioport) = crl;
|
||
|
GPIO_CRH(gpioport) = crh;
|
||
|
}
|
||
|
|
||
|
void gpio_set(u32 gpioport, u16 gpios)
|
||
|
{
|
||
|
GPIO_BSRR(gpioport) = gpios;
|
||
|
}
|
||
|
|
||
|
void gpio_clear(u32 gpioport, u16 gpios)
|
||
|
{
|
||
|
GPIO_BRR(gpioport) = gpios;
|
||
|
}
|
||
|
|
||
16 years ago
|
u16 gpio_get(u32 gpioport, u16 gpios)
|
||
|
{
|
||
|
return gpio_port_read(gpioport) & gpios;
|
||
|
}
|
||
|
|
||
14 years ago
|
void gpio_toggle(u32 gpioport, u16 gpios)
|
||
16 years ago
|
{
|
||
13 years ago
|
GPIO_ODR(gpioport) ^= gpios;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
u16 gpio_port_read(u32 gpioport)
|
||
16 years ago
|
{
|
||
16 years ago
|
return (u16)GPIO_IDR(gpioport);
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
void gpio_port_write(u32 gpioport, u16 data)
|
||
16 years ago
|
{
|
||
16 years ago
|
GPIO_ODR(gpioport) = data;
|
||
16 years ago
|
}
|
||
14 years ago
|
|
||
|
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. */
|
||
|
|
||
|
/* If (reg32 & GPIO_LCKK) is true, the lock is now active. */
|
||
|
}
|