[PATCH]: a44a9a78fa flash/nor/nrf5: don't misuse uint32_t for refcount

2022-08-17 Thread gerrit
This is an automated email from Gerrit.

"Tomas Vanek " just uploaded a new patch set to Gerrit, which 
you can find at https://review.openocd.org/c/openocd/+/7131

-- gerrit

commit a44a9a78faaedc02b46537aec8e2b0c90854ea49
Author: Tomas Vanek 
Date:   Wed Aug 17 10:42:47 2022 +0200

flash/nor/nrf5: don't misuse uint32_t for refcount

Change-Id: I016fc9ae037fae75528850d35da059d1db5a4d45
Signed-off-by: Tomas Vanek 

diff --git a/src/flash/nor/nrf5.c b/src/flash/nor/nrf5.c
index 23b221cb33..9d2565b010 100644
--- a/src/flash/nor/nrf5.c
+++ b/src/flash/nor/nrf5.c
@@ -136,7 +136,7 @@ struct nrf5_device_spec {
 };
 
 struct nrf5_info {
-   uint32_t refcount;
+   unsigned int refcount;
 
struct nrf5_bank {
struct nrf5_info *chip;

-- 



[PATCH]: 5ec5907cf4 flash/nor/bluenrg-x: clarify target algo stack usage

2022-08-17 Thread gerrit
This is an automated email from Gerrit.

"Tomas Vanek " just uploaded a new patch set to Gerrit, which 
you can find at https://review.openocd.org/c/openocd/+/7132

-- gerrit

commit 5ec5907cf4f622482b01e9521f28236040344df3
Author: Tomas Vanek 
Date:   Wed Aug 17 12:24:41 2022 +0200

flash/nor/bluenrg-x: clarify target algo stack usage

While on it rename misleading write_algorithm_sp to write_algorithm_stack
and change messages referring 'stack pointer' instead of stack.

No functional change.

Change-Id: Ibb9897d3f01734812ed0f8bc8cd43b935a573f8a
Signed-off-by: Tomas Vanek 

diff --git a/src/flash/nor/bluenrg-x.c b/src/flash/nor/bluenrg-x.c
index d4fb5e972b..ec5cae76e7 100644
--- a/src/flash/nor/bluenrg-x.c
+++ b/src/flash/nor/bluenrg-x.c
@@ -236,7 +236,7 @@ static int bluenrgx_write(struct flash_bank *bank, const 
uint8_t *buffer,
struct target *target = bank->target;
uint32_t buffer_size = 16384 + 8;
struct working_area *write_algorithm;
-   struct working_area *write_algorithm_sp;
+   struct working_area *write_algorithm_stack;
struct working_area *source;
uint32_t address = bank->base + offset;
struct reg_param reg_params[5];
@@ -285,10 +285,10 @@ static int bluenrgx_write(struct flash_bank *bank, const 
uint8_t *buffer,
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}
 
-   /* Stack pointer area */
+   /* Stack area */
if (target_alloc_working_area(target, 128,
- &write_algorithm_sp) != ERROR_OK) {
-   LOG_DEBUG("no working area for write code stack pointer");
+ &write_algorithm_stack) != ERROR_OK) {
+   LOG_DEBUG("no working area for target algorithm stack");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}
 
@@ -300,8 +300,19 @@ static int bluenrgx_write(struct flash_bank *bank, const 
uint8_t *buffer,
init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
init_reg_param(®_params[4], "sp", 32, PARAM_OUT);
-   /* Put the parameter at the first available stack location */
-   init_mem_param(&mem_params[0], write_algorithm_sp->address + 80, 32, 
PARAM_OUT);
+   /* Put the 4th parameter at the location in the stack frame of target 
write() function.
+* See contrib/loaders/flash/bluenrg-x/bluenrg-x_write.lst
+* 34 ldr r6, [sp, #80]
+* ^^^ offset
+*/
+   init_mem_param(&mem_params[0], write_algorithm_stack->address + 80, 32, 
PARAM_OUT);
+   /* Stack for target write algorithm - target write() function has
+* __attribute__((naked)) so it does not setup the new stack frame.
+* Therefore the stack frame uses the area from SP upwards!
+* Interrupts are disabled and no subroutines are called from write()
+* so no need to allocate stack below SP.
+* TODO: remove __attribute__((naked)) and use similar parameter 
passing as stm32l4x */
+   buf_set_u32(reg_params[4].value, 0, 32, write_algorithm_stack->address);
 
/* FIFO start address (first two words used for write and read 
pointers) */
buf_set_u32(reg_params[0].value, 0, 32, source->address);
@@ -311,14 +322,12 @@ static int bluenrgx_write(struct flash_bank *bank, const 
uint8_t *buffer,
buf_set_u32(reg_params[2].value, 0, 32, address);
/* Number of bytes */
buf_set_u32(reg_params[3].value, 0, 32, count);
-   /* Stack pointer for program working area */
-   buf_set_u32(reg_params[4].value, 0, 32, write_algorithm_sp->address);
/* Flash register base address */
buf_set_u32(mem_params[0].value, 0, 32, 
bluenrgx_info->flash_ptr->flash_regs_base);
 
LOG_DEBUG("source->address = " TARGET_ADDR_FMT, source->address);
LOG_DEBUG("source->address+ source->size = " TARGET_ADDR_FMT, 
source->address+source->size);
-   LOG_DEBUG("write_algorithm_sp->address = " TARGET_ADDR_FMT, 
write_algorithm_sp->address);
+   LOG_DEBUG("write_algorithm_stack->address = " TARGET_ADDR_FMT, 
write_algorithm_stack->address);
LOG_DEBUG("address = %08" PRIx32, address);
LOG_DEBUG("count = %08" PRIx32, count);
 
@@ -357,7 +366,7 @@ static int bluenrgx_write(struct flash_bank *bank, const 
uint8_t *buffer,
}
target_free_working_area(target, source);
target_free_working_area(target, write_algorithm);
-   target_free_working_area(target, write_algorithm_sp);
+   target_free_working_area(target, write_algorithm_stack);
 
destroy_reg_param(®_params[0]);
destroy_reg_param(®_params[1]);

-- 



[PATCH]: 0211a0e720 rtos/riot: read in thread state names at runtime

2022-08-17 Thread gerrit
This is an automated email from Gerrit.

"Marian Buschsieweke " just uploaded a new patch 
set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/7133

-- gerrit

commit 0211a0e720204238298dda8792700f5ecccd5c94
Author: Marian Buschsieweke 
Date:   Wed Aug 17 13:31:50 2022 +0200

rtos/riot: read in thread state names at runtime

Starting with https://github.com/RIOT-OS/RIOT/pull/18460 RIOT provides
thread state names so that they can be read out at runtime. This makes
use of this feature to populate the thread state descriptions at
run-time. If a pre-#18460 RIOT is used, it falls back to the current
builtin thread state lookup table, so that OpenOCD can still provide
thread state descriptions for older RIOT based firmwares.

It seems that RIOT is a bit more volatile in this regard than the
typical bare metal RTOS. Hence, reading this at runtime provides some
robustness despite occasional changes.

Signed-off-by: Marian Buschsieweke 
Change-Id: I729fd9d387aea45ec182b4e905c6b28036b77d98

diff --git a/src/rtos/riot.c b/src/rtos/riot.c
index 6717085d59..d8aa5548f1 100644
--- a/src/rtos/riot.c
+++ b/src/rtos/riot.c
@@ -27,27 +27,28 @@ static int riot_get_thread_reg_list(struct rtos *rtos, 
int64_t thread_id,
 static int riot_get_symbol_list_to_lookup(struct symbol_table_elem 
*symbol_list[]);
 
 struct riot_thread_state {
-   int value;
const char *desc;
 };
 
-/* refer RIOT sched.h */
-static const struct riot_thread_state riot_thread_states[] = {
-   { 0, "Stopped" },
-   { 1, "Zombie" },
-   { 2, "Sleeping" },
-   { 3, "Blocked mutex" },
-   { 4, "Blocked receive" },
-   { 5, "Blocked send" },
-   { 6, "Blocked reply" },
-   { 7, "Blocked any flag" },
-   { 8, "Blocked all flags" },
-   { 9, "Blocked mbox" },
-   { 10, "Blocked condition" },
-   { 11, "Running" },
-   { 12, "Pending" },
+/* Older versions of RIOT do not provide thread states in ELF for OpenOCD. For 
them,
+ * this lookup table is used as fallback: */
+static const struct riot_thread_state riot_thread_states_legacy[] = {
+   { "Stopped" },
+   { "Zombie" },
+   { "Sleeping" },
+   { "Blocked mutex" },
+   { "Blocked receive" },
+   { "Blocked send" },
+   { "Blocked reply" },
+   { "Blocked any flag" },
+   { "Blocked all flags" },
+   { "Blocked mbox" },
+   { "Blocked condition" },
+   { "Running" },
+   { "Pending" },
 };
-#define RIOT_NUM_STATES ARRAY_SIZE(riot_thread_states)
+static const struct riot_thread_state *riot_thread_states = 
riot_thread_states_legacy;
+static size_t riot_thread_states_numof = ARRAY_SIZE(riot_thread_states_legacy);
 
 struct riot_params {
const char *target_name;
@@ -78,6 +79,8 @@ enum riot_symbol_values {
RIOT_ACTIVE_PID,
RIOT_MAX_THREADS,
RIOT_NAME_OFFSET,
+   RIOT_THREAD_STATE_NAMES,
+   RIOT_THREAD_STATE_NAMES_NUMOF,
 };
 
 struct riot_symbol {
@@ -92,6 +95,8 @@ static struct riot_symbol const riot_symbol_list[] = {
{"sched_active_pid", false},
{"max_threads", false},
{"_tcb_name_offset", true},
+   {"thread_state_names", true},
+   {"thread_state_names_numof", true},
{NULL, false}
 };
 
@@ -237,20 +242,13 @@ static int riot_update_threads(struct rtos *rtos)
goto error;
}
 
-   /* Search for state */
-   unsigned int k;
-   for (k = 0; k < RIOT_NUM_STATES; k++) {
-   if (riot_thread_states[k].value == status)
-   break;
-   }
-
/* Copy state string */
-   if (k >= RIOT_NUM_STATES) {
+   if (status < riot_thread_states_numof) {
rtos->thread_details[tasks_found].extra_info_str =
-   strdup("unknown state");
+   strdup(riot_thread_states[status].desc);
} else {
rtos->thread_details[tasks_found].extra_info_str =
-   strdup(riot_thread_states[k].desc);
+   strdup("unknown state");
}
 
if (!rtos->thread_details[tasks_found].extra_info_str) {
@@ -375,11 +373,110 @@ static int riot_get_symbol_list_to_lookup(struct 
symbol_table_elem *symbol_list[
return ERROR_OK;
 }
 
+static void free_thread_state_names(struct riot_thread_state *names, size_t 
numof)
+{
+   for (size_t i = 0; i < numof; i++) {
+   if (names[i].desc) {
+   free((void *)names[i].desc);
+   names[i].desc = NULL;
+   }
+   }
+
+   free(names);
+}
+
+static int update_thread_state_names(struct rtos *rtos)
+{
+   int retval;
+
+   if (rtos->symbols[RIOT_THREAD_STATE_NAMES_NUMOF].address == 0) {
+   /* older version of R

[PATCH]: d30abaf36c jtag: rewrite the state machine for power drop and srst deassert

2022-08-17 Thread gerrit
This is an automated email from Gerrit.

"Antonio Borneo " just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/7134

-- gerrit

commit d30abaf36c3b874485370865547f1f8dd33557bd
Author: Antonio Borneo 
Date:   Thu Aug 18 01:29:33 2022 +0200

jtag: rewrite the state machine for power drop and srst deassert

The current state machine looks broken.

Rewrite it in a trivial way and document it through a diagram.
While there, use boolean type in place of integers.

Change-Id: I25b11e52a69d397e832724e01d18beb9770cc4b7
Signed-off-by: Antonio Borneo 

diff --git a/src/jtag/core.c b/src/jtag/core.c
index 806ee89265..e41b60d60d 100644
--- a/src/jtag/core.c
+++ b/src/jtag/core.c
@@ -1707,7 +1707,7 @@ bool jtag_will_verify_capture_ir(void)
return jtag_verify_capture_ir;
 }
 
-int jtag_power_dropout(int *dropout)
+int jtag_power_dropout(bool *dropout)
 {
if (!is_adapter_initialized()) {
/* TODO: as the jtag interface is not valid all
@@ -1718,16 +1718,16 @@ int jtag_power_dropout(int *dropout)
if (adapter_driver->power_dropout)
return adapter_driver->power_dropout(dropout);
 
-   *dropout = 0; /* by default we can't detect power dropout */
+   *dropout = false; /* by default we can't detect power dropout */
return ERROR_OK;
 }
 
-int jtag_srst_asserted(int *srst_asserted)
+int jtag_srst_asserted(bool *srst_asserted)
 {
if (adapter_driver->srst_asserted)
return adapter_driver->srst_asserted(srst_asserted);
 
-   *srst_asserted = 0; /* by default we can't detect srst asserted */
+   *srst_asserted = false; /* by default we can't detect srst asserted */
return ERROR_OK;
 }
 
diff --git a/src/jtag/interface.h b/src/jtag/interface.h
index a8d9ee47e9..1a64e093ff 100644
--- a/src/jtag/interface.h
+++ b/src/jtag/interface.h
@@ -293,7 +293,7 @@ struct adapter_driver {
 *
 * @returns ERROR_OK on success, or an error code on failure.
 */
-   int (*power_dropout)(int *power_dropout);
+   int (*power_dropout)(bool *power_dropout);
 
/**
 * Read and clear the srst asserted detection flag.
@@ -307,7 +307,7 @@ struct adapter_driver {
 * been asserted.
 * @returns ERROR_OK on success, or an error code on failure.
 */
-   int (*srst_asserted)(int *srst_asserted);
+   int (*srst_asserted)(bool *srst_asserted);
 
/**
 * Configure trace parameters for the adapter
diff --git a/src/jtag/jtag.h b/src/jtag/jtag.h
index 4f94e99135..d5757d7a53 100644
--- a/src/jtag/jtag.h
+++ b/src/jtag/jtag.h
@@ -525,8 +525,8 @@ int jtag_get_flush_queue_count(void);
 void jtag_notify_event(enum jtag_event);
 
 /* can be implemented by hw + sw */
-int jtag_power_dropout(int *dropout);
-int jtag_srst_asserted(int *srst_asserted);
+int jtag_power_dropout(bool *dropout);
+int jtag_srst_asserted(bool *srst_asserted);
 
 /* JTAG support functions */
 
diff --git a/src/jtag/tcl.c b/src/jtag/tcl.c
index 37a0285ce1..d7721492b6 100644
--- a/src/jtag/tcl.c
+++ b/src/jtag/tcl.c
@@ -1239,7 +1239,7 @@ COMMAND_HANDLER(handle_wait_srst_deassert)
}
 
LOG_USER("Waiting for srst assert + deassert for at most %dms", 
timeout_ms);
-   int asserted_yet;
+   bool asserted_yet;
int64_t then = timeval_ms();
while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
if ((timeval_ms() - then) > timeout_ms) {
diff --git a/src/target/target.c b/src/target/target.c
index 10a25efde6..83769cd087 100644
--- a/src/target/target.c
+++ b/src/target/target.c
@@ -2908,63 +2908,118 @@ COMMAND_HANDLER(handle_targets_command)
return retval;
 }
 
-/* every 300ms we check for reset & powerdropout and issue a "reset halt" if 
so. */
+/*
+ * Periodic check for reset & power dropout.
+ *
+ * State diagram for power dropout detection.
+ * Similar diagram applies to srst detection.
+ *
+ *  (START)
+ * v  ---
+ * |   |||   |
+ * v   v|v   |
+ *---   no-drop |   ---   drop   |
+ *   |   |>-   |   |>
+ *   |   POWER   | |   POWER   |
+ *   |ON |  drop (run-dropout) |OFF| no-drop
+ *   |   |>--->|   |>
+ *---   ---  |
+ * ^ ^   |
+ * | |   |
+ * | |   v
+ * | yes (run-restore)   |   drop   ---
+ *   _/^\_<|   |
+ * _/ \_   |   POWER   |
+ */ \