From 5eff0ab5d648408cf4df528d2be3d29b04034083 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sat, 28 Feb 2015 22:09:50 -0800 Subject: [PATCH] Factor out common GPIO code in STM32 platforms. --- src/platforms/f4discovery/platform.h | 28 ++----------- src/platforms/native/platform.h | 33 ++------------- src/platforms/stlink/platform.h | 33 ++------------- src/platforms/stm32/gpio.h | 61 ++++++++++++++++++++++++++++ src/platforms/swlink/platform.h | 35 ++-------------- 5 files changed, 74 insertions(+), 116 deletions(-) create mode 100644 src/platforms/stm32/gpio.h diff --git a/src/platforms/f4discovery/platform.h b/src/platforms/f4discovery/platform.h index 90b2a11..dcad8a0 100644 --- a/src/platforms/f4discovery/platform.h +++ b/src/platforms/f4discovery/platform.h @@ -34,9 +34,9 @@ #include #include "gdb_packet.h" +#include "gpio.h" #include "morse.h" -#define INLINE_GPIO #define CDCACM_PACKET_SIZE 64 #define PLATFORM_HAS_TRACESWO #define BOARD_IDENT "Black Magic Probe (F4Discovery), (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")" @@ -200,31 +200,9 @@ void uart_usb_buf_drain(uint8_t ep); #define sprintf siprintf #define vasprintf vasiprintf -#ifdef INLINE_GPIO -static inline void _gpio_set(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BSRR(gpioport) = gpios; - GPIO_BSRR(gpioport) = gpios; -} -#define gpio_set _gpio_set - -static inline void _gpio_clear(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BSRR(gpioport) = gpios<<16; - GPIO_BSRR(gpioport) = gpios<<16; -} -#define gpio_clear _gpio_clear - -static inline uint16_t _gpio_get(uint32_t gpioport, uint16_t gpios) -{ - return (uint16_t)GPIO_IDR(gpioport) & gpios; -} -#define gpio_get _gpio_get -#endif - -#endif - #define disconnect_usb() do {usbd_disconnect(usbdev,1); nvic_disable_irq(USB_IRQ);} while(0) void assert_boot_pin(void); #define setup_vbus_irq() +#endif + diff --git a/src/platforms/native/platform.h b/src/platforms/native/platform.h index 73b6c59..2fa1e84 100644 --- a/src/platforms/native/platform.h +++ b/src/platforms/native/platform.h @@ -35,9 +35,9 @@ #include #include "gdb_packet.h" +#include "gpio.h" #include "morse.h" -#define INLINE_GPIO #define CDCACM_PACKET_SIZE 64 #define PLATFORM_HAS_TRACESWO #define PLATFORM_HAS_POWER_SWITCH @@ -164,14 +164,6 @@ extern volatile uint32_t timeout_counter; extern jmp_buf fatal_error_jmpbuf; - -#define gpio_set_val(port, pin, val) do { \ - if(val) \ - gpio_set((port), (pin)); \ - else \ - gpio_clear((port), (pin)); \ -} while(0) - #define SET_RUN_STATE(state) {running_status = (state);} #define SET_IDLE_STATE(state) {gpio_set_val(LED_PORT, LED_IDLE_RUN, state);} #define SET_ERROR_STATE(state) {gpio_set_val(LED_PORT, LED_ERROR, state);} @@ -189,6 +181,8 @@ extern jmp_buf fatal_error_jmpbuf; int platform_init(void); const char *platform_target_voltage(void); int platform_hwversion(void); +void platform_set_timeout(uint32_t ms); +bool platform_timeout_expired(void); void platform_delay(uint32_t delay); /* */ @@ -205,27 +199,6 @@ void uart_usb_buf_drain(uint8_t ep); #define sprintf siprintf #define vasprintf vasiprintf -#ifdef INLINE_GPIO -static inline void _gpio_set(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BSRR(gpioport) = gpios; -} -#define gpio_set _gpio_set - -static inline void _gpio_clear(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BRR(gpioport) = gpios; -} -#define gpio_clear _gpio_clear - -static inline uint16_t _gpio_get(uint32_t gpioport, uint16_t gpios) -{ - return (uint16_t)GPIO_IDR(gpioport) & gpios; -} -#define gpio_get _gpio_get -#endif - - #define disconnect_usb() gpio_set_mode(USB_PU_PORT, GPIO_MODE_INPUT, 0, USB_PU_PIN); void assert_boot_pin(void); void setup_vbus_irq(void); diff --git a/src/platforms/stlink/platform.h b/src/platforms/stlink/platform.h index ee08875..a49e73a 100644 --- a/src/platforms/stlink/platform.h +++ b/src/platforms/stlink/platform.h @@ -35,8 +35,8 @@ #include #include "gdb_packet.h" +#include "gpio.h" -#define INLINE_GPIO #define CDCACM_PACKET_SIZE 64 #define BOARD_IDENT "Black Magic Probe (STLINK), (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")" #define BOARD_IDENT_DFU "Black Magic (Upgrade) for STLink/Discovery, (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")" @@ -143,13 +143,6 @@ extern volatile uint32_t timeout_counter; extern jmp_buf fatal_error_jmpbuf; -#define gpio_set_val(port, pin, val) do { \ - if(val) \ - gpio_set((port), (pin)); \ - else \ - gpio_clear((port), (pin)); \ -} while(0) - extern uint16_t led_idle_run; #define SET_RUN_STATE(state) {running_status = (state);} #define SET_IDLE_STATE(state) {gpio_set_val(LED_PORT, led_idle_run, state);} @@ -182,28 +175,8 @@ void uart_usb_buf_drain(uint8_t ep); #define sprintf siprintf #define vasprintf vasiprintf -#ifdef INLINE_GPIO -static inline void _gpio_set(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BSRR(gpioport) = gpios; -} -#define gpio_set _gpio_set - -static inline void _gpio_clear(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BRR(gpioport) = gpios; -} -#define gpio_clear _gpio_clear - -static inline uint16_t _gpio_get(uint32_t gpioport, uint16_t gpios) -{ - return (uint16_t)GPIO_IDR(gpioport) & gpios; -} -#define gpio_get _gpio_get -#endif - -#endif - void disconnect_usb(void); void assert_boot_pin(void); +#endif + diff --git a/src/platforms/stm32/gpio.h b/src/platforms/stm32/gpio.h new file mode 100644 index 0000000..035f3c9 --- /dev/null +++ b/src/platforms/stm32/gpio.h @@ -0,0 +1,61 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2015 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ +#ifndef __GPIO_H +#define __GPIO_H + +#define INLINE_GPIO + +#define gpio_set_val(port, pin, val) do { \ + if(val) \ + gpio_set((port), (pin)); \ + else \ + gpio_clear((port), (pin)); \ +} while(0) + +#ifdef INLINE_GPIO +static inline void _gpio_set(uint32_t gpioport, uint16_t gpios) +{ + GPIO_BSRR(gpioport) = gpios; +#ifdef F4DISCOVERY + GPIO_BSRR(gpioport) = gpios; +#endif +} +#define gpio_set _gpio_set + +static inline void _gpio_clear(uint32_t gpioport, uint16_t gpios) +{ +#ifndef F4DISCOVERY + GPIO_BRR(gpioport) = gpios; +#else + GPIO_BSRR(gpioport) = gpios<<16; + GPIO_BSRR(gpioport) = gpios<<16; +#endif +} +#define gpio_clear _gpio_clear + +static inline uint16_t _gpio_get(uint32_t gpioport, uint16_t gpios) +{ + return (uint16_t)GPIO_IDR(gpioport) & gpios; +} +#define gpio_get _gpio_get +#endif + +#endif + diff --git a/src/platforms/swlink/platform.h b/src/platforms/swlink/platform.h index 99c8fa3..4b26466 100644 --- a/src/platforms/swlink/platform.h +++ b/src/platforms/swlink/platform.h @@ -35,8 +35,8 @@ #include #include "gdb_packet.h" +#include "gpio.h" -#define INLINE_GPIO #define CDCACM_PACKET_SIZE 64 #define BOARD_IDENT "Black Magic Probe (SWLINK), (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")" #define BOARD_IDENT_DFU "Black Magic (Upgrade), STM8S Discovery, (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")" @@ -141,14 +141,6 @@ extern volatile uint32_t timeout_counter; extern jmp_buf fatal_error_jmpbuf; - -#define gpio_set_val(port, pin, val) do { \ - if(val) \ - gpio_set((port), (pin)); \ - else \ - gpio_clear((port), (pin)); \ -} while(0) - #define led_idle_run GPIO8 #define SET_RUN_STATE(state) {running_status = (state);} #define SET_IDLE_STATE(state) {gpio_set_val(LED_PORT, led_idle_run, state);} @@ -180,27 +172,8 @@ void uart_usb_buf_drain(uint8_t ep); #define sprintf siprintf #define vasprintf vasiprintf -#ifdef INLINE_GPIO -static inline void _gpio_set(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BSRR(gpioport) = gpios; -} -#define gpio_set _gpio_set - -static inline void _gpio_clear(uint32_t gpioport, uint16_t gpios) -{ - GPIO_BRR(gpioport) = gpios; -} -#define gpio_clear _gpio_clear - -static inline uint16_t _gpio_get(uint32_t gpioport, uint16_t gpios) -{ - return (uint16_t)GPIO_IDR(gpioport) & gpios; -} -#define gpio_get _gpio_get -#endif - -#endif - void disconnect_usb(void); void assert_boot_pin(void); + +#endif +