From d417666095b6743dc56ff15a5276eaed7bfa8d6d Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Mon, 29 Oct 2012 22:53:59 +0000 Subject: Eliminate redundant gpio code from f2/f4/l1 Implemented as per exti2 --- lib/stm32/f2/Makefile | 2 +- lib/stm32/f2/gpio.c | 142 ----------------------------------------------- lib/stm32/f4/Makefile | 2 +- lib/stm32/f4/gpio.c | 142 ----------------------------------------------- lib/stm32/gpio2.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/stm32/l1/Makefile | 2 +- lib/stm32/l1/gpio.c | 146 ------------------------------------------------ 7 files changed, 153 insertions(+), 433 deletions(-) delete mode 100644 lib/stm32/f2/gpio.c delete mode 100644 lib/stm32/f4/gpio.c create mode 100644 lib/stm32/gpio2.c delete mode 100644 lib/stm32/l1/gpio.c (limited to 'lib') diff --git a/lib/stm32/f2/Makefile b/lib/stm32/f2/Makefile index e3d73fe..b64a033 100644 --- a/lib/stm32/f2/Makefile +++ b/lib/stm32/f2/Makefile @@ -28,7 +28,7 @@ CFLAGS = -Os -g -Wall -Wextra -I../../../include -fno-common \ -ffunction-sections -fdata-sections -MD -DSTM32F2 # ARFLAGS = rcsv ARFLAGS = rcs -OBJS = rcc.o gpio.o usart.o spi.o flash.o \ +OBJS = rcc.o gpio2.o usart.o spi.o flash.o \ i2c.o exti2.o timer.o VPATH += ../../usb:../:../../cm3 diff --git a/lib/stm32/f2/gpio.c b/lib/stm32/f2/gpio.c deleted file mode 100644 index c577c3a..0000000 --- a/lib/stm32/f2/gpio.c +++ /dev/null @@ -1,142 +0,0 @@ -/* - * This file is part of the libopencm3 project. - * - * Copyright (C) 2011 Fergus Noble - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library. If not, see . - */ - -#include - -void gpio_mode_setup(u32 gpioport, u8 mode, u8 pull_up_down, u16 gpios) -{ - u16 i; - u32 moder, pupd; - - /* - * We want to set the config only for the pins mentioned in gpios, - * but keeping the others, so read out the actual config first. - */ - moder = GPIO_MODER(gpioport); - pupd = GPIO_PUPDR(gpioport); - - for (i = 0; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - - moder &= ~GPIO_MODE_MASK(i); - moder |= GPIO_MODE(i, mode); - pupd &= ~GPIO_PUPD_MASK(i); - pupd |= GPIO_PUPD(i, pull_up_down); - } - - /* Set mode and pull up/down control registers. */ - GPIO_MODER(gpioport) = moder; - GPIO_PUPDR(gpioport) = pupd; -} - -void gpio_set_output_options(u32 gpioport, u8 otype, u8 speed, u16 gpios) -{ - u16 i; - u32 ospeedr; - - if (otype == 0x1) - GPIO_OTYPER(gpioport) |= gpios; - else - GPIO_OTYPER(gpioport) &= ~gpios; - - ospeedr = GPIO_OSPEEDR(gpioport); - - for (i = 0; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - ospeedr &= ~GPIO_OSPEED_MASK(i); - ospeedr |= GPIO_OSPEED(i, speed); - } - - GPIO_OSPEEDR(gpioport) = ospeedr; -} - -void gpio_set_af(u32 gpioport, u8 alt_func_num, u16 gpios) -{ - u16 i; - u32 afrl, afrh; - - afrl = GPIO_AFRL(gpioport); - afrh = GPIO_AFRH(gpioport); - - for (i = 0; i < 8; i++) { - if (!((1 << i) & gpios)) - continue; - afrl &= ~GPIO_AFR_MASK(i); - afrl |= GPIO_AFR(i, alt_func_num); - } - - for (i = 8; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - afrh &= ~GPIO_AFR_MASK(i - 8); - afrh |= GPIO_AFR(i - 8, alt_func_num); - } - - GPIO_AFRL(gpioport) = afrl; - GPIO_AFRH(gpioport) = afrh; -} - -void gpio_set(u32 gpioport, u16 gpios) -{ - GPIO_BSRR(gpioport) = gpios; -} - -void gpio_clear(u32 gpioport, u16 gpios) -{ - GPIO_BSRR(gpioport) = gpios << 16; -} - -u16 gpio_get(u32 gpioport, u16 gpios) -{ - return gpio_port_read(gpioport) & gpios; -} - -void gpio_toggle(u32 gpioport, u16 gpios) -{ - GPIO_ODR(gpioport) ^= gpios; -} - -u16 gpio_port_read(u32 gpioport) -{ - return (u16)GPIO_IDR(gpioport); -} - -void gpio_port_write(u32 gpioport, u16 data) -{ - GPIO_ODR(gpioport) = data; -} - -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. */ -} diff --git a/lib/stm32/f4/Makefile b/lib/stm32/f4/Makefile index 1e3192b..9e62fea 100644 --- a/lib/stm32/f4/Makefile +++ b/lib/stm32/f4/Makefile @@ -29,7 +29,7 @@ CFLAGS = -Os -g -Wall -Wextra -I../../../include -fno-common \ -ffunction-sections -fdata-sections -MD -DSTM32F4 # ARFLAGS = rcsv ARFLAGS = rcs -OBJS = rcc.o gpio.o usart.o spi.o flash.o \ +OBJS = rcc.o gpio2.o usart.o spi.o flash.o \ i2c.o exti2.o pwr.o timer.o \ usb.o usb_standard.o usb_control.o usb_fx07_common.o usb_f107.o \ usb_f207.o adc.o diff --git a/lib/stm32/f4/gpio.c b/lib/stm32/f4/gpio.c deleted file mode 100644 index aa2fda4..0000000 --- a/lib/stm32/f4/gpio.c +++ /dev/null @@ -1,142 +0,0 @@ -/* - * This file is part of the libopencm3 project. - * - * Copyright (C) 2011 Fergus Noble - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library. If not, see . - */ - -#include - -void gpio_mode_setup(u32 gpioport, u8 mode, u8 pull_up_down, u16 gpios) -{ - u16 i; - u32 moder, pupd; - - /* - * We want to set the config only for the pins mentioned in gpios, - * but keeping the others, so read out the actual config first. - */ - moder = GPIO_MODER(gpioport); - pupd = GPIO_PUPDR(gpioport); - - for (i = 0; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - - moder &= ~GPIO_MODE_MASK(i); - moder |= GPIO_MODE(i, mode); - pupd &= ~GPIO_PUPD_MASK(i); - pupd |= GPIO_PUPD(i, pull_up_down); - } - - /* Set mode and pull up/down control registers. */ - GPIO_MODER(gpioport) = moder; - GPIO_PUPDR(gpioport) = pupd; -} - -void gpio_set_output_options(u32 gpioport, u8 otype, u8 speed, u16 gpios) -{ - u16 i; - u32 ospeedr; - - if (otype == 0x1) - GPIO_OTYPER(gpioport) |= gpios; - else - GPIO_OTYPER(gpioport) &= ~gpios; - - ospeedr = GPIO_OSPEEDR(gpioport); - - for (i = 0; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - ospeedr &= ~GPIO_OSPEED_MASK(i); - ospeedr |= GPIO_OSPEED(i, speed); - } - - GPIO_OSPEEDR(gpioport) = ospeedr; -} - -void gpio_set_af(u32 gpioport, u8 alt_func_num, u16 gpios) -{ - u16 i; - u32 afrl, afrh; - - afrl = GPIO_AFRL(gpioport); - afrh = GPIO_AFRH(gpioport); - - for (i = 0; i < 8; i++) { - if (!((1 << i) & gpios)) - continue; - afrl &= ~GPIO_AFR_MASK(i); - afrl |= GPIO_AFR(i, alt_func_num); - } - - for (i = 8; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - afrh &= ~GPIO_AFR_MASK(i - 8); - afrh |= GPIO_AFR(i - 8, alt_func_num); - } - - GPIO_AFRL(gpioport) = afrl; - GPIO_AFRH(gpioport) = afrh; -} - -void gpio_set(u32 gpioport, u16 gpios) -{ - GPIO_BSRR(gpioport) = gpios; -} - -void gpio_clear(u32 gpioport, u16 gpios) -{ - GPIO_BSRR(gpioport) = gpios << 16; -} - -u16 gpio_get(u32 gpioport, u16 gpios) -{ - return gpio_port_read(gpioport) & gpios; -} - -void gpio_toggle(u32 gpioport, u16 gpios) -{ - GPIO_ODR(gpioport) ^= gpios; -} - -u16 gpio_port_read(u32 gpioport) -{ - return (u16)GPIO_IDR(gpioport); -} - -void gpio_port_write(u32 gpioport, u16 data) -{ - GPIO_ODR(gpioport) = data; -} - -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. */ -} diff --git a/lib/stm32/gpio2.c b/lib/stm32/gpio2.c new file mode 100644 index 0000000..c185e98 --- /dev/null +++ b/lib/stm32/gpio2.c @@ -0,0 +1,150 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Fergus Noble + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#if defined(STM32F2) +#include +#elif defined(STM32F4) +#include +#elif defined(STM32L1) +#include +#else +#error "invalid/unknown stm32 family for this code" +#endif + +void gpio_mode_setup(u32 gpioport, u8 mode, u8 pull_up_down, u16 gpios) +{ + u16 i; + u32 moder, pupd; + + /* + * We want to set the config only for the pins mentioned in gpios, + * but keeping the others, so read out the actual config first. + */ + moder = GPIO_MODER(gpioport); + pupd = GPIO_PUPDR(gpioport); + + for (i = 0; i < 16; i++) { + if (!((1 << i) & gpios)) + continue; + + moder &= ~GPIO_MODE_MASK(i); + moder |= GPIO_MODE(i, mode); + pupd &= ~GPIO_PUPD_MASK(i); + pupd |= GPIO_PUPD(i, pull_up_down); + } + + /* Set mode and pull up/down control registers. */ + GPIO_MODER(gpioport) = moder; + GPIO_PUPDR(gpioport) = pupd; +} + +void gpio_set_output_options(u32 gpioport, u8 otype, u8 speed, u16 gpios) +{ + u16 i; + u32 ospeedr; + + if (otype == 0x1) + GPIO_OTYPER(gpioport) |= gpios; + else + GPIO_OTYPER(gpioport) &= ~gpios; + + ospeedr = GPIO_OSPEEDR(gpioport); + + for (i = 0; i < 16; i++) { + if (!((1 << i) & gpios)) + continue; + ospeedr &= ~GPIO_OSPEED_MASK(i); + ospeedr |= GPIO_OSPEED(i, speed); + } + + GPIO_OSPEEDR(gpioport) = ospeedr; +} + +void gpio_set_af(u32 gpioport, u8 alt_func_num, u16 gpios) +{ + u16 i; + u32 afrl, afrh; + + afrl = GPIO_AFRL(gpioport); + afrh = GPIO_AFRH(gpioport); + + for (i = 0; i < 8; i++) { + if (!((1 << i) & gpios)) + continue; + afrl &= ~GPIO_AFR_MASK(i); + afrl |= GPIO_AFR(i, alt_func_num); + } + + for (i = 8; i < 16; i++) { + if (!((1 << i) & gpios)) + continue; + afrh &= ~GPIO_AFR_MASK(i - 8); + afrh |= GPIO_AFR(i - 8, alt_func_num); + } + + GPIO_AFRL(gpioport) = afrl; + GPIO_AFRH(gpioport) = afrh; +} + +void gpio_set(u32 gpioport, u16 gpios) +{ + GPIO_BSRR(gpioport) = gpios; +} + +void gpio_clear(u32 gpioport, u16 gpios) +{ + GPIO_BSRR(gpioport) = gpios << 16; +} + +u16 gpio_get(u32 gpioport, u16 gpios) +{ + return gpio_port_read(gpioport) & gpios; +} + +void gpio_toggle(u32 gpioport, u16 gpios) +{ + GPIO_ODR(gpioport) ^= gpios; +} + +u16 gpio_port_read(u32 gpioport) +{ + return (u16)GPIO_IDR(gpioport); +} + +void gpio_port_write(u32 gpioport, u16 data) +{ + GPIO_ODR(gpioport) = data; +} + +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. */ +} diff --git a/lib/stm32/l1/Makefile b/lib/stm32/l1/Makefile index 71a6505..9e07b2d 100644 --- a/lib/stm32/l1/Makefile +++ b/lib/stm32/l1/Makefile @@ -28,7 +28,7 @@ CFLAGS = -Os -g -Wall -Wextra -I../../../include -fno-common \ -ffunction-sections -fdata-sections -MD -DSTM32L1 # ARFLAGS = rcsv ARFLAGS = rcs -OBJS = rcc.o gpio.o desig.o crc.o usart.o exti2.o +OBJS = rcc.o gpio2.o desig.o crc.o usart.o exti2.o VPATH += ../../usb:../:../../cm3 diff --git a/lib/stm32/l1/gpio.c b/lib/stm32/l1/gpio.c deleted file mode 100644 index 2314bd4..0000000 --- a/lib/stm32/l1/gpio.c +++ /dev/null @@ -1,146 +0,0 @@ -/* - * This file is part of the libopencm3 project. - * - * Copyright (C) 2011 Fergus Noble - * Copyright (C) 2012 Karl Palsson - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library. If not, see . - * - * This is virtually a carbon copy of the F4 code... - * TODO: make this code shared by f2, f4, l1 - */ - -#include - -void gpio_mode_setup(u32 gpioport, u8 mode, u8 pull_up_down, u16 gpios) -{ - u16 i; - u32 moder, pupd; - - /* - * We want to set the config only for the pins mentioned in gpios, - * but keeping the others, so read out the actual config first. - */ - moder = GPIO_MODER(gpioport); - pupd = GPIO_PUPDR(gpioport); - - for (i = 0; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - - moder &= ~GPIO_MODE_MASK(i); - moder |= GPIO_MODE(i, mode); - pupd &= ~GPIO_PUPD_MASK(i); - pupd |= GPIO_PUPD(i, pull_up_down); - } - - /* Set mode and pull up/down control registers. */ - GPIO_MODER(gpioport) = moder; - GPIO_PUPDR(gpioport) = pupd; -} - -void gpio_set_output_options(u32 gpioport, u8 otype, u8 speed, u16 gpios) -{ - u16 i; - u32 ospeedr; - - if (otype == GPIO_OTYPE_OD) - GPIO_OTYPER(gpioport) |= gpios; - else - GPIO_OTYPER(gpioport) &= ~gpios; - - ospeedr = GPIO_OSPEEDR(gpioport); - - for (i = 0; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - ospeedr &= ~GPIO_OSPEED_MASK(i); - ospeedr |= GPIO_OSPEED(i, speed); - } - - GPIO_OSPEEDR(gpioport) = ospeedr; -} - -void gpio_set_af(u32 gpioport, u8 alt_func_num, u16 gpios) -{ - u16 i; - u32 afrl, afrh; - - afrl = GPIO_AFRL(gpioport); - afrh = GPIO_AFRH(gpioport); - - for (i = 0; i < 8; i++) { - if (!((1 << i) & gpios)) - continue; - afrl &= ~GPIO_AFR_MASK(i); - afrl |= GPIO_AFR(i, alt_func_num); - } - - for (i = 8; i < 16; i++) { - if (!((1 << i) & gpios)) - continue; - afrl &= ~GPIO_AFR_MASK(i - 8); - afrh |= GPIO_AFR(i - 8, alt_func_num); - } - - GPIO_AFRL(gpioport) = afrl; - GPIO_AFRH(gpioport) = afrh; -} - -void gpio_set(u32 gpioport, u16 gpios) -{ - GPIO_BSRR(gpioport) = gpios; -} - -void gpio_clear(u32 gpioport, u16 gpios) -{ - GPIO_BSRR(gpioport) = gpios << 16; -} - -u16 gpio_get(u32 gpioport, u16 gpios) -{ - return gpio_port_read(gpioport) & gpios; -} - -void gpio_toggle(u32 gpioport, u16 gpios) -{ - GPIO_ODR(gpioport) ^= gpios; -} - -u16 gpio_port_read(u32 gpioport) -{ - return (u16)GPIO_IDR(gpioport); -} - -void gpio_port_write(u32 gpioport, u16 data) -{ - GPIO_ODR(gpioport) = data; -} - -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. */ -} -- cgit v1.2.3