Merge pull request #135 from gsmcmullin/timeout_overhaul

Timeout overhaul, improve reset spin loop on Cortex-A
This commit is contained in:
Gareth McMullin 2016-06-30 18:23:02 +12:00 committed by GitHub
commit 393a8fddef
28 changed files with 141 additions and 64 deletions

View File

@ -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");

View File

@ -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");

View File

@ -44,6 +44,7 @@ static bool cmd_swdp_scan(void);
static bool cmd_targets(target *t);
static bool cmd_morse(void);
static bool cmd_connect_srst(target *t, int argc, const char **argv);
static bool cmd_hard_srst(void);
#ifdef PLATFORM_HAS_POWER_SWITCH
static bool cmd_target_power(target *t, int argc, const char **argv);
#endif
@ -62,6 +63,7 @@ const struct command_s cmd_list[] = {
{"targets", (cmd_handler)cmd_targets, "Display list of available targets" },
{"morse", (cmd_handler)cmd_morse, "Display morse error message" },
{"connect_srst", (cmd_handler)cmd_connect_srst, "Configure connect under SRST: (enable|disable)" },
{"hard_srst", (cmd_handler)cmd_hard_srst, "Force a pulse on the hard SRST line - disconnects target" },
#ifdef PLATFORM_HAS_POWER_SWITCH
{"tpwr", (cmd_handler)cmd_target_power, "Supplies power to the target: (enable|disable)"},
#endif
@ -262,6 +264,14 @@ static bool cmd_connect_srst(target *t, int argc, const char **argv)
return true;
}
static bool cmd_hard_srst(void)
{
target_list_free();
platform_srst_set_val(true);
platform_srst_set_val(false);
return true;
}
#ifdef PLATFORM_HAS_POWER_SWITCH
static bool cmd_target_power(target *t, int argc, const char **argv)
{

View File

@ -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;
@ -334,6 +334,8 @@ void cortexa_detach(target *t)
/* Restore any clobbered registers */
cortexa_regs_write_internal(t);
/* Invalidate cache */
apb_write(t, DBGITR, MCR | ICIALLU);
uint32_t dbgdscr = apb_read(t, DBGDSCR);
/* Disable halting debug mode */
@ -441,12 +443,16 @@ static void cortexa_reset(target *t)
platform_srst_set_val(false);
/* Spin until Xilinx reconnects us */
platform_timeout timeout;
platform_timeout_set(&timeout, 1000);
volatile struct exception e;
do {
TRY_CATCH (e, EXCEPTION_ALL) {
apb_read(t, DBGDIDR);
}
} while (e.type == EXCEPTION_ERROR);
} while (!platform_timeout_is_expired(&timeout) && e.type == EXCEPTION_ERROR);
if (e.type == EXCEPTION_ERROR)
raise_exception(e.type, e.msg);
platform_delay(100);

View File

@ -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;

View File

@ -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);

View File

@ -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 <gareth@blacksphere.co.nz>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View File

@ -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 <gareth@blacksphere.co.nz>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __TIMING__H
#define __TIMING_H
struct platform_timeout {
uint32_t time;
};
uint32_t platform_time_ms(void);
#endif

View File

@ -20,6 +20,7 @@ SRC += cdcacm.c \
usbuart.c \
serialno.c \
timing.c \
timing_stm32.c \
all: blackmagic.bin

View File

@ -26,6 +26,7 @@
#include "gpio.h"
#include "timing.h"
#include "timing_stm32.h"
#include "version.h"
#include <setjmp.h>

View File

@ -20,6 +20,7 @@ SRC += cdcacm.c \
usbuart.c \
serialno.c \
timing.c \
timing_stm32.c \
all: blackmagic.bin blackmagic.hex blackmagic.dfu

View File

@ -27,6 +27,7 @@
#include "gpio.h"
#include "timing.h"
#include "timing_stm32.h"
#include "version.h"
#include <setjmp.h>

View File

@ -15,6 +15,7 @@ VPATH += platforms/tm4c
SRC += cdcacm.c \
usbuart.c \
timing.c \
traceswo.o
all: blackmagic.bin

View File

@ -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)

View File

@ -20,6 +20,7 @@
#include <libopencm3/lm4f/gpio.h>
#include <libopencm3/usb/usbd.h>
#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

View File

@ -1,2 +1,4 @@
CFLAGS += -DLIBFTDI
LDFLAGS += -lftdi -lusb
SRC += timing.c \

View File

@ -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;
}

View File

@ -23,6 +23,8 @@
#include <ftdi.h>
#include "timing.h"
#ifndef WIN32
# include <alloca.h>
#else

View File

@ -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

View File

@ -26,6 +26,7 @@
#include "gpio.h"
#include "timing.h"
#include "timing_stm32.h"
#define PLATFORM_HAS_TRACESWO
#define PLATFORM_HAS_POWER_SWITCH

View File

@ -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

View File

@ -26,6 +26,7 @@
#include "gpio.h"
#include "timing.h"
#include "timing_stm32.h"
#include "version.h"
#include <libopencm3/cm3/common.h>

View File

@ -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();

View File

@ -23,8 +23,7 @@
#include <libopencm3/cm3/scb.h>
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;
}

View File

@ -16,8 +16,8 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TIMING_H
#define __TIMING_H
#ifndef __TIMING_STM32_H
#define __TIMING_STM32_H
extern uint8_t running_status;

View File

@ -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

View File

@ -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 ")"

View File

@ -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();