Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
19620: dist/tools/openocd: fix parsing of flash bank base r=aabadie a=maribu

### Contribution description

Since [80fc9fabc66a0bc767467fa14c703e5a9f340cd3] the format of the `flash list` command changed to a more human readable multi-line variant. Technically, the change is white-space only. Still, the current approach of parsing them with awk, sed and cut doesn't like the new multi-line format. The parsing is now delegated into a python script that is compatible across OpenOCD versions.

[80fc9fabc66a0bc767467fa14c703e5a9f340cd3]: openocd-org/openocd@80fc9fa


19636: sys: model ecc, evtimer, pipe and shell_lock in kconfig r=aabadie a=aabadie



19639: tests/net/gnrc_mac_timeout: add automated test r=aabadie a=aabadie



Co-authored-by: Marian Buschsieweke <[email protected]>
Co-authored-by: Alexandre Abadie <[email protected]>
  • Loading branch information
3 people authored May 23, 2023
4 parents 4c27aff + 47ec58d + 0ef40af + da67b11 commit 712c9be
Show file tree
Hide file tree
Showing 27 changed files with 268 additions and 152 deletions.
69 changes: 6 additions & 63 deletions dist/tools/openocd/openocd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,42 +195,7 @@ _is_binfile() {
[[ -z "${firmware_type}" ]] && _has_bin_extension "${firmware}"; }
}

# Split bank info on different lines without the '{}'
_split_banks() {
# Input:
# ...
# {name nrf51 base 0 size 0 bus_width 1 chip_width 1} {name nrf51 base 268439552 size 0 bus_width 1 chip_width 1}
# ...
# or for newer openocd versions (v0.12.0 or higher)
# ...
# {name nrf51.flash driver nrf51 base 0 size 0 bus_width 1 chip_width 1 target nrf51.cpu} {name nrf51.uicr ...}
# ...
#
# Output:
# ...
# name nrf51 base 0 size 0 bus_width 1 chip_width 1
# name nrf51 base 268439552 size 0 bus_width 1 chip_width 1
# ...
# or for newer openocd versions (v0.12.0 or higher)
# ...
# name nrf51.flash driver nrf51 base 0 size 0 bus_width 1 chip_width 1 target nrf51.cpu
# name nrf51.uicr driver nrf51 base 268439552 size 0 bus_width 1 chip_width 1 target nrf51.cpu
# ...
#
# The following command needs specific osx handling (non gnu):
# * Same commands for a pattern should be on different lines
# * Cannot use '\n' in the replacement string
local sed_escaped_newline=\\$'\n'

sed -n '
/^{.*}$/ {
s/\} /\}'"${sed_escaped_newline}"'/g
s/[{}]//g
p
}'
}

_flash_list_raw() {
_flash_list() {
# Openocd output for 'flash list' is either
# ....
# {name nrf51 base 0 size 0 bus_width 1 chip_width 1} {name nrf51 base 268439552 size 0 bus_width 1 chip_width 1}
Expand Down Expand Up @@ -272,32 +237,10 @@ _flash_list_raw() {
-c 'shutdown'" 2>&1
}

# Outputs bank info on different lines without the '{}'
_flash_list() {
# ....
# name nrf51 base 0 size 0 bus_width 1 chip_width 1
# name nrf51 base 268439552 size 0 bus_width 1 chip_width 1
# ....
# or for newer openocd versions (v0.12.0 or higher)
# ....
# name nrf51.flash driver nrf51 base 0 size 0 bus_width 1 chip_width 1 target nrf51.cpu
# name nrf51.uicr driver nrf51 base 268439552 size 0 bus_width 1 chip_width 1 target nrf51.cpu
# ....
_flash_list_raw | _split_banks
}

# Print flash address for 'bank_num' num defaults to 1
# _flash_address [bank_num:1]
# Print flash address for 'bank_num' num defaults to 0
# _flash_address [bank_num:0]
_flash_address() {
# extract the line from '_flash_list' output for bank with number 'bank_num'
bank=$(_flash_list | awk "NR==${1:-1}")
# determine the column of base address, a line can have following formats
# name nrf51 base 268439552 size 0 bus_width 1 chip_width 1
# or for newer openocd versions (v0.12.0 or higher)
# name nrf51.flash driver nrf51 base 0 size 0 bus_width 1 chip_width 1 target nrf51.cpu
base_addr_idx=$(echo ${bank} | awk '{ for (i=1; i <= NF; i++) if ($i == "base") print i + 1 }')
# extract the base address in hexadecimal format
printf 0x"%08x" $(echo ${bank} | cut -d " " -f${base_addr_idx})
_flash_list | "${RIOTTOOLS}/openocd/openocd_flashinfo.py" --idx "${1:-0}"
}

do_flashr() {
Expand All @@ -317,7 +260,7 @@ do_flashr() {
# This allows flashing normal binary files without env configuration
if _is_binfile "${IMAGE_FILE}" "${IMAGE_TYPE}"; then
# hardwritten to use the first bank
FLASH_ADDR=$(_flash_address 1)
FLASH_ADDR=$(_flash_address 0)
echo "Binfile detected, adding ROM base address: ${FLASH_ADDR}"
IMAGE_TYPE=bin
IMAGE_OFFSET=$(printf "0x%08x\n" "$((${IMAGE_OFFSET} + ${FLASH_ADDR}))")
Expand Down Expand Up @@ -366,7 +309,7 @@ do_flash() {
# This allows flashing normal binary files without env configuration
if _is_binfile "${IMAGE_FILE}" "${IMAGE_TYPE}"; then
# hardwritten to use the first bank
FLASH_ADDR=$(_flash_address 1)
FLASH_ADDR=$(_flash_address 0)
echo "Binfile detected, adding ROM base address: ${FLASH_ADDR}"
IMAGE_TYPE=bin
IMAGE_OFFSET=$(printf "0x%08x\n" "$((${IMAGE_OFFSET} + ${FLASH_ADDR}))")
Expand Down
69 changes: 69 additions & 0 deletions dist/tools/openocd/openocd_flashinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
"""
Utility to parse the output of OpenOCD's "flash list" command
"""
import argparse
import sys

NUMERIC_FIELDS = {"base", "size", "bus_width", "chip_width"}


def parse_flash_info(lines):
"""
Read output of OpenOCD's "flash list" command given in lines into a list
of dictionaries
:param lines: Output of "flash list" lines
:return: [{"name": "nrf52.flash", "base": 0, ...},
{"name": "nrf52.uicr", ...}, ...]
"""
tokens = []
for line in lines:
for word in line.split():
if word.startswith('{') and len(word) > 1:
tokens += ["{", word[1:]]
elif word.endswith('}') and len(word) > 1:
tokens += [word[:-1], "}"]
else:
tokens.append(word)

idx = 0
result = []
while idx < len(tokens):
entry = {}
while idx < len(tokens) and tokens[idx] != "{":
idx += 1
idx += 1
while idx < len(tokens) and tokens[idx] != "}":
if idx + 1 >= len(tokens) or tokens[idx + 1] == "}":
break
key = tokens[idx]
value = tokens[idx + 1]
if key in NUMERIC_FIELDS:
value = int(value, 0)
entry[key] = value
idx += 2
if idx < len(tokens) and tokens[idx] == "}":
result.append(entry)

return result


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Parse OpenOCD's \"flash list\" output")
parser.add_argument("--field", default="base", type=str,
help="Field to extract (default \"base\")")
parser.add_argument("--idx", default=0, type=int,
help="Index of the bank to extract info from " +
"(default 0)")
args = parser.parse_args()
info = parse_flash_info(sys.stdin)
if args.idx < 0 or args.idx >= len(info):
sys.exit("flash bank index out of range")
value = info[args.idx][args.field]
if args.field in NUMERIC_FIELDS:
print(f"0x{value:08x}")
else:
print(value)
1 change: 0 additions & 1 deletion makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ PSEUDOMODULES += event_%
PSEUDOMODULES += event_timeout
PSEUDOMODULES += event_timeout_ztimer
PSEUDOMODULES += evtimer_mbox
PSEUDOMODULES += evtimer_on_ztimer
PSEUDOMODULES += fatfs_vfs_format
PSEUDOMODULES += fmt_%
PSEUDOMODULES += gcoap_forward_proxy
Expand Down
4 changes: 4 additions & 0 deletions sys/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ choice LOG
endchoice

rsource "coding/Kconfig"
rsource "ecc/Kconfig"
rsource "evtimer/Kconfig"
rsource "log_color/Kconfig"
rsource "log_printfnoformat/Kconfig"
rsource "luid/Kconfig"
Expand All @@ -85,6 +87,7 @@ rsource "net/Kconfig"
rsource "od/Kconfig"
rsource "oneway-malloc/Kconfig"
rsource "phydat/Kconfig"
rsource "pipe/Kconfig"
rsource "pm_layered/Kconfig"
rsource "posix/Kconfig"
rsource "preprocessor/Kconfig"
Expand All @@ -100,6 +103,7 @@ rsource "sema_inv/Kconfig"
rsource "senml/Kconfig"
rsource "seq/Kconfig"
rsource "shell/Kconfig"
rsource "shell_lock/Kconfig"
rsource "ssp/Kconfig"
rsource "test_utils/Kconfig"
rsource "timex/Kconfig"
Expand Down
9 changes: 1 addition & 8 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -998,14 +998,7 @@ ifneq (,$(filter ztimer64%,$(USEMODULE)))
endif

ifneq (,$(filter evtimer,$(USEMODULE)))
ifneq (,$(filter evtimer_on_ztimer,$(USEMODULE)))
USEMODULE += ztimer_msec
else
USEMODULE += xtimer
ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
USEMODULE += evtimer_on_ztimer
endif
endif
USEMODULE += ztimer_msec
endif

# handle xtimer's deps. Needs to be done *after* ztimer
Expand Down
35 changes: 35 additions & 0 deletions sys/ecc/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2023 Inria
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#

menuconfig MODULE_ECC
bool "Error Correction Code (ECC) algorithms"
depends on TEST_KCONFIG
help
Provides Golay1412, Hamming256 and Repetition algorithms.

if MODULE_ECC

menu "ECC algorithms"

config MODULE_ECC_GOLAY1412
bool "Golay1412 Error Correction Code (ECC) algorithm"
help
Provides Golay1412 ECC algorithm.

config MODULE_ECC_HAMMING256
bool "Hamming256 Error Correction Code (ECC) algorithm"
help
Provides Hamming256 ECC algorithm.

config MODULE_ECC_REPETITION
bool "Repetition Error Correction Code (ECC) algorithm"
help
Provides Repetition ECC algorithm.

endmenu # ECC algorithms

endif
19 changes: 19 additions & 0 deletions sys/evtimer/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2023 Inria
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#

config MODULE_EVTIMER
bool "Event timer module"
depends on TEST_KCONFIG
select MODULE_ZTIMER
select MODULE_ZTIMER_MSEC

config MODULE_EVTIMER_MBOX
bool "Use message box"
select MODULE_CORE_MBOX
select MODULE_EVTIMER
help
Use message box to implement event timer.
32 changes: 0 additions & 32 deletions sys/evtimer/evtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,9 @@ static void _del_event_from_list(evtimer_t *evtimer, evtimer_event_t *event)
static void _set_timer(evtimer_t *evtimer)
{
evtimer_event_t *next_event = evtimer->events;

#if IS_USED(MODULE_EVTIMER_ON_ZTIMER)
evtimer->base = ztimer_set(ZTIMER_MSEC, &evtimer->timer, next_event->offset);
DEBUG("evtimer: now=%" PRIu32 " ms setting ztimer to %" PRIu32 " ms\n",
evtimer->base, next_event->offset);
#else
uint64_t offset_us = (uint64_t)next_event->offset * US_PER_MS;

DEBUG("evtimer: now=%" PRIu32 " us setting xtimer to %" PRIu32 ":%" PRIu32 " us\n",
xtimer_now_usec(), (uint32_t)(offset_us >> 32), (uint32_t)(offset_us));

xtimer_set64(&evtimer->timer, offset_us);
#endif
}

static void _update_timer(evtimer_t *evtimer)
Expand All @@ -114,15 +104,10 @@ static void _update_timer(evtimer_t *evtimer)
_set_timer(evtimer);
}
else {
#if IS_USED(MODULE_EVTIMER_ON_ZTIMER)
ztimer_remove(ZTIMER_MSEC, &evtimer->timer);
#else
xtimer_remove(&evtimer->timer);
#endif
}
}

#if IS_USED(MODULE_EVTIMER_ON_ZTIMER)
static void _update_head_offset(evtimer_t *evtimer)
{
if (evtimer->events) {
Expand All @@ -137,23 +122,6 @@ static void _update_head_offset(evtimer_t *evtimer)
evtimer->base = now;
}
}
#else /* IS_USED(MODULE_EVTIMER_ON_ZTIMER) */
static uint32_t _get_offset(xtimer_t *timer)
{
uint64_t left = xtimer_left_usec(timer);
/* add half of 125 so integer division rounds to nearest */
return div_u64_by_125((left >> 3) + 62);
}

static void _update_head_offset(evtimer_t *evtimer)
{
if (evtimer->events) {
evtimer_event_t *event = evtimer->events;
event->offset = _get_offset(&evtimer->timer);
DEBUG("evtimer: _update_head_offset(): new head offset %" PRIu32 "\n", event->offset);
}
}
#endif /* !IS_USED(MODULE_EVTIMER_ON_ZTIMER) */

void evtimer_add(evtimer_t *evtimer, evtimer_event_t *event)
{
Expand Down
Loading

0 comments on commit 712c9be

Please sign in to comment.