diff --git a/src/adiv5_jtagdp.c b/src/adiv5_jtagdp.c index b9e805a..c8b5847 100644 --- a/src/adiv5_jtagdp.c +++ b/src/adiv5_jtagdp.c @@ -82,16 +82,17 @@ static uint32_t adiv5_jtagdp_low_access(ADIv5_DP_t *dp, uint8_t RnW, addr &= 0xff; uint64_t request, response; uint8_t ack; + platform_timeout timeout; request = ((uint64_t)value << 3) | ((addr >> 1) & 0x06) | (RnW?1:0); jtag_dev_write_ir(dp->dev, APnDP ? IR_APACC : IR_DPACC); - platform_timeout_set(2000); + platform_timeout_set(&timeout, 2000); do { jtag_dev_shift_dr(dp->dev, (uint8_t*)&response, (uint8_t*)&request, 35); ack = response & 0x07; - } while(!platform_timeout_is_expired() && (ack == JTAGDP_ACK_WAIT)); + } while(!platform_timeout_is_expired(&timeout) && (ack == JTAGDP_ACK_WAIT)); if (ack == JTAGDP_ACK_WAIT) raise_exception(EXCEPTION_TIMEOUT, "JTAG-DP ACK timeout"); diff --git a/src/adiv5_swdp.c b/src/adiv5_swdp.c index 5edf49e..57d0e67 100644 --- a/src/adiv5_swdp.c +++ b/src/adiv5_swdp.c @@ -120,6 +120,7 @@ static uint32_t adiv5_swdp_low_access(ADIv5_DP_t *dp, uint8_t RnW, uint8_t request = 0x81; uint32_t response = 0; uint8_t ack; + platform_timeout timeout; if(APnDP && dp->fault) return 0; @@ -131,11 +132,11 @@ static uint32_t adiv5_swdp_low_access(ADIv5_DP_t *dp, uint8_t RnW, if((addr == 4) || (addr == 8)) request ^= 0x20; - platform_timeout_set(2000); + platform_timeout_set(&timeout, 2000); do { swdptap_seq_out(request, 8); ack = swdptap_seq_in(3); - } while (!platform_timeout_is_expired() && ack == SWDP_ACK_WAIT); + } while (!platform_timeout_is_expired(&timeout) && ack == SWDP_ACK_WAIT); if (ack == SWDP_ACK_WAIT) raise_exception(EXCEPTION_TIMEOUT, "SWDP ACK timeout"); diff --git a/src/cortexa.c b/src/cortexa.c index 51b546f..4e47b77 100644 --- a/src/cortexa.c +++ b/src/cortexa.c @@ -307,7 +307,7 @@ bool cortexa_attach(target *t) target_halt_request(t); tries = 10; while(!platform_srst_get_val() && !target_halt_wait(t) && --tries) - platform_delay(2); + platform_delay(200); if(!tries) return false; diff --git a/src/cortexm.c b/src/cortexm.c index 91da4f5..be371d1 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -307,7 +307,7 @@ bool cortexm_attach(target *t) target_halt_request(t); tries = 10; while(!platform_srst_get_val() && !target_halt_wait(t) && --tries) - platform_delay(2); + platform_delay(200); if(!tries) return false; diff --git a/src/include/platform_support.h b/src/include/platform_support.h index 3db7ad3..f595ad1 100644 --- a/src/include/platform_support.h +++ b/src/include/platform_support.h @@ -30,11 +30,13 @@ void platform_init(int argc, char **argv); void platform_init(void); #endif +typedef struct platform_timeout platform_timeout; +void platform_timeout_set(platform_timeout *t, uint32_t ms); +bool platform_timeout_is_expired(platform_timeout *t); +void platform_delay(uint32_t ms); + const char *platform_target_voltage(void); int platform_hwversion(void); -void platform_timeout_set(uint32_t ms); -bool platform_timeout_is_expired(void); -void platform_delay(uint32_t delay); void platform_srst_set_val(bool assert); bool platform_srst_get_val(void); bool platform_target_get_power(void); diff --git a/src/platforms/common/timing.c b/src/platforms/common/timing.c new file mode 100644 index 0000000..9087ba7 --- /dev/null +++ b/src/platforms/common/timing.c @@ -0,0 +1,31 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2016 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 . + */ +#include "general.h" + +void platform_timeout_set(platform_timeout *t, uint32_t ms) +{ + t->time = platform_time_ms() + ms; +} + +bool platform_timeout_is_expired(platform_timeout *t) +{ + return platform_time_ms() > t->time; +} + diff --git a/src/platforms/common/timing.h b/src/platforms/common/timing.h new file mode 100644 index 0000000..39b58e5 --- /dev/null +++ b/src/platforms/common/timing.h @@ -0,0 +1,30 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2016 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 __TIMING__H +#define __TIMING_H + +struct platform_timeout { + uint32_t time; +}; + +uint32_t platform_time_ms(void); + +#endif + diff --git a/src/platforms/f4discovery/Makefile.inc b/src/platforms/f4discovery/Makefile.inc index e57bb0e..a1ec121 100644 --- a/src/platforms/f4discovery/Makefile.inc +++ b/src/platforms/f4discovery/Makefile.inc @@ -20,6 +20,7 @@ SRC += cdcacm.c \ usbuart.c \ serialno.c \ timing.c \ + timing_stm32.c \ all: blackmagic.bin diff --git a/src/platforms/f4discovery/platform.h b/src/platforms/f4discovery/platform.h index 5f5b4da..d1260da 100644 --- a/src/platforms/f4discovery/platform.h +++ b/src/platforms/f4discovery/platform.h @@ -26,6 +26,7 @@ #include "gpio.h" #include "timing.h" +#include "timing_stm32.h" #include "version.h" #include diff --git a/src/platforms/hydrabus/Makefile.inc b/src/platforms/hydrabus/Makefile.inc index e7c6907..36973e0 100644 --- a/src/platforms/hydrabus/Makefile.inc +++ b/src/platforms/hydrabus/Makefile.inc @@ -20,6 +20,7 @@ SRC += cdcacm.c \ usbuart.c \ serialno.c \ timing.c \ + timing_stm32.c \ all: blackmagic.bin blackmagic.hex blackmagic.dfu diff --git a/src/platforms/hydrabus/platform.h b/src/platforms/hydrabus/platform.h index eb7a298..b340f97 100644 --- a/src/platforms/hydrabus/platform.h +++ b/src/platforms/hydrabus/platform.h @@ -27,6 +27,7 @@ #include "gpio.h" #include "timing.h" +#include "timing_stm32.h" #include "version.h" #include diff --git a/src/platforms/launchpad-icdi/Makefile.inc b/src/platforms/launchpad-icdi/Makefile.inc index e46b727..ac71797 100644 --- a/src/platforms/launchpad-icdi/Makefile.inc +++ b/src/platforms/launchpad-icdi/Makefile.inc @@ -15,6 +15,7 @@ VPATH += platforms/tm4c SRC += cdcacm.c \ usbuart.c \ + timing.c \ traceswo.o all: blackmagic.bin diff --git a/src/platforms/launchpad-icdi/platform.c b/src/platforms/launchpad-icdi/platform.c index 24c4aa0..5ecfb54 100644 --- a/src/platforms/launchpad-icdi/platform.c +++ b/src/platforms/launchpad-icdi/platform.c @@ -33,14 +33,19 @@ extern void trace_tick(void); +volatile platform_timeout * volatile head_timeout; uint8_t running_status; -volatile uint32_t timeout_counter; +static volatile uint32_t time_ms; void sys_tick_handler(void) { - if(timeout_counter) - timeout_counter--; trace_tick(); + time_ms += 10; +} + +uint32_t platform_time_ms(void) +{ + return time_ms; } void @@ -101,20 +106,11 @@ bool platform_srst_get_val(void) return gpio_get(SRST_PORT, SRST_PIN) == 0; } -void platform_timeout_set(uint32_t ms) +void platform_delay(uint32_t ms) { - timeout_counter = ms / 10; -} - -bool platform_timeout_is_expired(void) -{ - return timeout_counter == 0; -} - -void platform_delay(uint32_t delay) -{ - platform_timeout_set(delay); - while (platform_timeout_is_expired()); + platform_timeout timeout; + platform_timeout_set(&timeout, ms); + while (!platform_timeout_is_expired(&timeout)); } const char *platform_target_voltage(void) diff --git a/src/platforms/launchpad-icdi/platform.h b/src/platforms/launchpad-icdi/platform.h index fe86e03..a710c52 100644 --- a/src/platforms/launchpad-icdi/platform.h +++ b/src/platforms/launchpad-icdi/platform.h @@ -20,6 +20,7 @@ #include #include +#include "timing.h" #include "version.h" #define BOARD_IDENT "Black Magic Probe (Launchpad ICDI), (Firmware " FIRMWARE_VERSION ")" @@ -28,7 +29,6 @@ #define DFU_IFACE_STRING "lolwut" extern uint8_t running_status; -extern volatile uint32_t timeout_counter; #define TMS_PORT GPIOA_BASE #define TMS_PIN GPIO3 diff --git a/src/platforms/libftdi/Makefile.inc b/src/platforms/libftdi/Makefile.inc index fa6292b..5b7b17f 100644 --- a/src/platforms/libftdi/Makefile.inc +++ b/src/platforms/libftdi/Makefile.inc @@ -1,2 +1,4 @@ CFLAGS += -DLIBFTDI LDFLAGS += -lftdi -lusb + +SRC += timing.c \ diff --git a/src/platforms/libftdi/platform.c b/src/platforms/libftdi/platform.c index 00a97ee..7686e13 100644 --- a/src/platforms/libftdi/platform.c +++ b/src/platforms/libftdi/platform.c @@ -279,26 +279,15 @@ const char *platform_target_voltage(void) return "not supported"; } -void platform_delay(uint32_t delay) +void platform_delay(uint32_t ms) { - usleep(delay * 100000); + usleep(ms * 1000); } -static uint32_t timeout_time; -static uint32_t time_ms(void) +uint32_t platform_time_ms(void) { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec * 1000) + (tv.tv_usec / 1000); } -void platform_timeout_set(uint32_t ms) -{ - timeout_time = time_ms() + ms; -} - -bool platform_timeout_is_expired(void) -{ - return time_ms() > timeout_time; -} - diff --git a/src/platforms/libftdi/platform.h b/src/platforms/libftdi/platform.h index 56064fc..84fa7f3 100644 --- a/src/platforms/libftdi/platform.h +++ b/src/platforms/libftdi/platform.h @@ -23,6 +23,8 @@ #include +#include "timing.h" + #ifndef WIN32 # include #else diff --git a/src/platforms/native/Makefile.inc b/src/platforms/native/Makefile.inc index 8365555..ba08e71 100644 --- a/src/platforms/native/Makefile.inc +++ b/src/platforms/native/Makefile.inc @@ -19,6 +19,7 @@ SRC += cdcacm.c \ usbuart.c \ serialno.c \ timing.c \ + timing_stm32.c \ all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex diff --git a/src/platforms/native/platform.h b/src/platforms/native/platform.h index 5b19bdc..282b767 100644 --- a/src/platforms/native/platform.h +++ b/src/platforms/native/platform.h @@ -26,6 +26,7 @@ #include "gpio.h" #include "timing.h" +#include "timing_stm32.h" #define PLATFORM_HAS_TRACESWO #define PLATFORM_HAS_POWER_SWITCH diff --git a/src/platforms/stlink/Makefile.inc b/src/platforms/stlink/Makefile.inc index fdee700..d7edcf4 100644 --- a/src/platforms/stlink/Makefile.inc +++ b/src/platforms/stlink/Makefile.inc @@ -19,6 +19,7 @@ SRC += cdcacm.c \ usbuart.c \ serialno.c \ timing.c \ + timing_stm32.c \ all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex dfu_upgrade.bin dfu_upgrade.hex diff --git a/src/platforms/stlink/platform.h b/src/platforms/stlink/platform.h index 06afe1d..e10aa86 100644 --- a/src/platforms/stlink/platform.h +++ b/src/platforms/stlink/platform.h @@ -26,6 +26,7 @@ #include "gpio.h" #include "timing.h" +#include "timing_stm32.h" #include "version.h" #include diff --git a/src/platforms/stm32/gdb_if.c b/src/platforms/stm32/gdb_if.c index 52922e7..6cc1f9c 100644 --- a/src/platforms/stm32/gdb_if.c +++ b/src/platforms/stm32/gdb_if.c @@ -113,7 +113,8 @@ unsigned char gdb_if_getchar(void) unsigned char gdb_if_getchar_to(int timeout) { - platform_timeout_set(timeout); + platform_timeout t; + platform_timeout_set(&t, timeout); if (!(out_ptr < count_out)) do { /* Detach if port closed */ @@ -121,7 +122,7 @@ unsigned char gdb_if_getchar_to(int timeout) return 0x04; gdb_if_update_buf(); - } while (!platform_timeout_is_expired() && !(out_ptr < count_out)); + } while (!platform_timeout_is_expired(&t) && !(out_ptr < count_out)); if(out_ptr < count_out) return gdb_if_getchar(); diff --git a/src/platforms/stm32/timing.c b/src/platforms/stm32/timing_stm32.c similarity index 80% rename from src/platforms/stm32/timing.c rename to src/platforms/stm32/timing_stm32.c index 725674d..234f039 100644 --- a/src/platforms/stm32/timing.c +++ b/src/platforms/stm32/timing_stm32.c @@ -23,8 +23,7 @@ #include uint8_t running_status; - -static volatile uint32_t timeout_counter; +static volatile uint32_t time_ms; void platform_timing_init(void) { @@ -37,20 +36,11 @@ void platform_timing_init(void) systick_counter_enable(); } -void platform_timeout_set(uint32_t ms) +void platform_delay(uint32_t ms) { - timeout_counter = ms / 100; -} - -bool platform_timeout_is_expired(void) -{ - return timeout_counter == 0; -} - -void platform_delay(uint32_t delay) -{ - platform_timeout_set(delay); - while (!platform_timeout_is_expired()); + platform_timeout timeout; + platform_timeout_set(&timeout, ms); + while (!platform_timeout_is_expired(&timeout)); } void sys_tick_handler(void) @@ -58,9 +48,13 @@ void sys_tick_handler(void) if(running_status) gpio_toggle(LED_PORT, LED_IDLE_RUN); - if(timeout_counter) - timeout_counter--; + time_ms += 100; SET_ERROR_STATE(morse_update()); } +uint32_t platform_time_ms(void) +{ + return time_ms; +} + diff --git a/src/platforms/stm32/timing.h b/src/platforms/stm32/timing_stm32.h similarity index 94% rename from src/platforms/stm32/timing.h rename to src/platforms/stm32/timing_stm32.h index 0178ff5..7690901 100644 --- a/src/platforms/stm32/timing.h +++ b/src/platforms/stm32/timing_stm32.h @@ -16,8 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#ifndef __TIMING_H -#define __TIMING_H +#ifndef __TIMING_STM32_H +#define __TIMING_STM32_H extern uint8_t running_status; diff --git a/src/platforms/swlink/Makefile.inc b/src/platforms/swlink/Makefile.inc index 62f90bb..1a979c5 100644 --- a/src/platforms/swlink/Makefile.inc +++ b/src/platforms/swlink/Makefile.inc @@ -19,6 +19,7 @@ SRC += cdcacm.c \ usbuart.c \ serialno.c \ timing.c \ + timing_stm32.c \ all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex diff --git a/src/platforms/swlink/platform.h b/src/platforms/swlink/platform.h index def38e8..74bb787 100644 --- a/src/platforms/swlink/platform.h +++ b/src/platforms/swlink/platform.h @@ -26,6 +26,7 @@ #include "gpio.h" #include "timing.h" +#include "timing_stm32.h" #include "version.h" #define BOARD_IDENT "Black Magic Probe (SWLINK), (Firmware " FIRMWARE_VERSION ")" diff --git a/src/platforms/tm4c/gdb_if.c b/src/platforms/tm4c/gdb_if.c index 7119638..4238a02 100644 --- a/src/platforms/tm4c/gdb_if.c +++ b/src/platforms/tm4c/gdb_if.c @@ -84,7 +84,8 @@ unsigned char gdb_if_getchar(void) unsigned char gdb_if_getchar_to(int timeout) { - timeout_counter = timeout/100; + platform_timeout t; + platform_timeout_set(&t, timeout); if(head_out == tail_out) do { /* Detach if port closed */ @@ -92,7 +93,7 @@ unsigned char gdb_if_getchar_to(int timeout) return 0x04; while(cdcacm_get_config() != 1); - } while(timeout_counter && head_out == tail_out); + } while(!platform_timeout_is_expired(&t) && head_out == tail_out); if(head_out != tail_out) return gdb_if_getchar();