From 8a7a6ff64034a3a5fda792fabb47e0e4806d3b70 Mon Sep 17 00:00:00 2001 From: Baldanos Date: Tue, 21 Jan 2025 21:28:07 +0100 Subject: [PATCH] Add `poke` and `peek` debug commands Also move debug commands in dedicated `debug.c` file --- src/common/common.c | 178 +----------------------------- src/common/common.h | 2 - src/common/common.mk | 3 +- src/common/debug.c | 232 ++++++++++++++++++++++++++++++++++++++++ src/common/debug.h | 23 ++++ src/common/exec.c | 7 ++ src/hydrabus/commands.c | 13 +++ src/hydrabus/commands.h | 2 + 8 files changed, 280 insertions(+), 180 deletions(-) create mode 100644 src/common/debug.c create mode 100644 src/common/debug.h diff --git a/src/common/common.c b/src/common/common.c index c4804e8d..c2a1bca2 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -29,6 +29,7 @@ #include "bsp_gpio.h" #include "microsd.h" #include "hydrabus_sd.h" +#include "debug.h" #define HYDRAFW_VERSION "HydraFW (HydraBus) " HYDRAFW_GIT_TAG " " HYDRAFW_CHECKIN_DATE #define TEST_WA_SIZE THD_WORKING_AREA_SIZE(256) @@ -270,183 +271,6 @@ int cmd_show(t_hydra_console *con, t_tokenline_parsed *p) return TRUE; } -/* Just debug to check Timing and accuracy with output pin */ -int cmd_debug_timing(t_hydra_console *con, t_tokenline_parsed *p) -{ - uint8_t i; - - (void)p; - -#if 0 - register volatile uint16_t* gpio_set; - register volatile uint16_t* gpio_clr; - /* GPIO B3 */ - gpio_set = (uint16_t*)&(((GPIO_TypeDef *)(0x40000000ul + 0x00020000ul + 0x0400ul))->BSRR.H.set); - gpio_clr = (uint16_t*)&(((GPIO_TypeDef *)(0x40000000ul + 0x00020000ul + 0x0400ul))->BSRR.H.clear); - //#define gpio_val (uint16_t)(((ioportmask_t)(1 << (3)))) - register uint16_t gpio_val = (uint16_t)(((ioportmask_t)(1 << (3)))); - -#define TST_OFF (*gpio_clr=gpio_val) -#define TST_ON (*gpio_set=gpio_val) -#endif - volatile systime_t tick, ticks10MHz, ticks3_39MHz, tick1MHz, ticks_1us; - - bsp_gpio_init(BSP_GPIO_PORTB, 3, MODE_CONFIG_DEV_GPIO_OUT_PUSHPULL, MODE_CONFIG_DEV_GPIO_NOPULL); - - /* 168MHz Clk=5.84ns/cycle */ - ticks10MHz = 8; /* 10MHz= (100ns/2) / 5.84 */ - ticks3_39MHz = 25; /* 3.39MHz= (295ns/2) / 5.84 */ - tick1MHz = 85; /* 1MHz= (1000ns/2) / 5.84 */ - ticks_1us = 171; - cprintf(con, "50ns=%.2ld ticks\r\n", (uint32_t)ticks10MHz); - cprintf(con, "148ns=%.2ld ticks\r\n", (uint32_t)ticks3_39MHz); - cprintf(con, "500ns=%.2ld ticks\r\n", (uint32_t)tick1MHz); - cprintf(con, "Test dbg Out Freq Max 84Mhz(11.9ns),10MHz(100ns/2),3.39MHz(295ns/2),1MHz(1us/2)\r\nPress User Button to exit\r\n"); - chThdSleepMilliseconds(1); - - /* Lock Kernel for sniffer */ - chSysLock(); - - while (1) { - /* Exit if User Button is pressed */ - if (hydrabus_ubtn()) { - break; - } - - - TST_OFF; - - /* Fastest possible 0/1 */ - for(i=0; i<16; i++) { - TST_ON; - TST_OFF; - - TST_ON; - TST_OFF; - - TST_ON; - TST_OFF; - - TST_ON; - TST_OFF; - } - - /* Delay 1us */ - tick = ticks_1us; - wait_delay(tick); - - /* Freq 10Mhz */ - tick = ticks10MHz; - for(i=0; i<16; i++) { - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - } - - /* Delay 1us */ - tick = ticks_1us; - wait_delay(tick); - - /* Freq 3.39Mhz */ - tick = ticks3_39MHz; - for(i=0; i<16; i++) { - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - } - - /* Delay 1us */ - tick = ticks_1us; - wait_delay(tick); - - /* Freq 1Mhz */ - tick = tick1MHz; - for(i=0; i<16; i++) { - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - - TST_ON; - wait_delay(tick); - TST_OFF; - wait_delay(tick); - } - - } - - chSysUnlock(); - cprintf(con, "Test dbg Out Freq end\r\n"); - - /* Reconfigure GPIOB3 in Safe Mode / Input */ - bsp_gpio_init(BSP_GPIO_PORTB, 3, MODE_CONFIG_DEV_GPIO_IN, MODE_CONFIG_DEV_GPIO_NOPULL); - - return TRUE; -} - -/* Just debug rx speed ignore all data received until UBTN + a key is pressed */ -int cmd_debug_test_rx(t_hydra_console *con, t_tokenline_parsed *p) -{ - (void)p; - BaseSequentialStream* chp = con->bss; - uint8_t * inbuf = pool_alloc_bytes(0x1000); // 4096 bytes - - cprintf(con, "Test debug-rx started, stop it with UBTN + Key\r\n"); - while(1) { - chnRead(chp, inbuf, 0x0FFF); - - /* Exit if User Button is pressed */ - if (hydrabus_ubtn()) { - break; - } - //get_char(con); - } - pool_free(inbuf); - return TRUE; -} - uint8_t hexchartonibble(char hex) { if (hex >= '0' && hex <= '9') return hex - '0'; diff --git a/src/common/common.h b/src/common/common.h index b461e55b..225ecb24 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -117,8 +117,6 @@ int cmd_mode_exec(t_hydra_console *con, t_tokenline_parsed *p); typedef int (*cmdfunc)(t_hydra_console *con, t_tokenline_parsed *p); int mode_exit(t_hydra_console *con, t_tokenline_parsed *p); int cmd_show(t_hydra_console *con, t_tokenline_parsed *p); -int cmd_debug_timing(t_hydra_console *con, t_tokenline_parsed *p); -int cmd_debug_test_rx(t_hydra_console *con, t_tokenline_parsed *p); int cmd_adc(t_hydra_console *con, t_tokenline_parsed *p); int cmd_dac(t_hydra_console *con, t_tokenline_parsed *p); int cmd_pwm(t_hydra_console *con, t_tokenline_parsed *p); diff --git a/src/common/common.mk b/src/common/common.mk index 0f562378..118be663 100644 --- a/src/common/common.mk +++ b/src/common/common.mk @@ -5,7 +5,8 @@ COMMONSRC = common/common.c \ common/usb1cfg.c \ common/usb2cfg.c \ common/script.c \ - common/alloc.c + common/alloc.c \ + common/debug.c # Required include directories COMMONINC = ./common diff --git a/src/common/debug.c b/src/common/debug.c new file mode 100644 index 00000000..a424bae9 --- /dev/null +++ b/src/common/debug.c @@ -0,0 +1,232 @@ +/* + * HydraBus/HydraNFC + * + * Copyright (C) 2014-2025 Benjamin VERNOUX + * Copyright (C) 2025 Nicolas OBERLI + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common.h" +#include "hydrabus.h" +#include "hydrafw_version.hdr" +#include "bsp_gpio.h" + +#include + +/* Just debug to check Timing and accuracy with output pin */ +int cmd_debug_timing(t_hydra_console *con, t_tokenline_parsed *p) +{ + uint8_t i; + + (void)p; + +#if 0 + register volatile uint16_t* gpio_set; + register volatile uint16_t* gpio_clr; + /* GPIO B3 */ + gpio_set = (uint16_t*)&(((GPIO_TypeDef *)(0x40000000ul + 0x00020000ul + 0x0400ul))->BSRR.H.set); + gpio_clr = (uint16_t*)&(((GPIO_TypeDef *)(0x40000000ul + 0x00020000ul + 0x0400ul))->BSRR.H.clear); + //#define gpio_val (uint16_t)(((ioportmask_t)(1 << (3)))) + register uint16_t gpio_val = (uint16_t)(((ioportmask_t)(1 << (3)))); + +#define TST_OFF (*gpio_clr=gpio_val) +#define TST_ON (*gpio_set=gpio_val) +#endif + volatile systime_t tick, ticks10MHz, ticks3_39MHz, tick1MHz, ticks_1us; + + bsp_gpio_init(BSP_GPIO_PORTB, 3, MODE_CONFIG_DEV_GPIO_OUT_PUSHPULL, MODE_CONFIG_DEV_GPIO_NOPULL); + + /* 168MHz Clk=5.84ns/cycle */ + ticks10MHz = 8; /* 10MHz= (100ns/2) / 5.84 */ + ticks3_39MHz = 25; /* 3.39MHz= (295ns/2) / 5.84 */ + tick1MHz = 85; /* 1MHz= (1000ns/2) / 5.84 */ + ticks_1us = 171; + cprintf(con, "50ns=%.2ld ticks\r\n", (uint32_t)ticks10MHz); + cprintf(con, "148ns=%.2ld ticks\r\n", (uint32_t)ticks3_39MHz); + cprintf(con, "500ns=%.2ld ticks\r\n", (uint32_t)tick1MHz); + cprintf(con, "Test dbg Out Freq Max 84Mhz(11.9ns),10MHz(100ns/2),3.39MHz(295ns/2),1MHz(1us/2)\r\nPress User Button to exit\r\n"); + chThdSleepMilliseconds(1); + + /* Lock Kernel for sniffer */ + chSysLock(); + + while (1) { + /* Exit if User Button is pressed */ + if (hydrabus_ubtn()) { + break; + } + + + TST_OFF; + + /* Fastest possible 0/1 */ + for(i=0; i<16; i++) { + TST_ON; + TST_OFF; + + TST_ON; + TST_OFF; + + TST_ON; + TST_OFF; + + TST_ON; + TST_OFF; + } + + /* Delay 1us */ + tick = ticks_1us; + wait_delay(tick); + + /* Freq 10Mhz */ + tick = ticks10MHz; + for(i=0; i<16; i++) { + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + } + + /* Delay 1us */ + tick = ticks_1us; + wait_delay(tick); + + /* Freq 3.39Mhz */ + tick = ticks3_39MHz; + for(i=0; i<16; i++) { + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + } + + /* Delay 1us */ + tick = ticks_1us; + wait_delay(tick); + + /* Freq 1Mhz */ + tick = tick1MHz; + for(i=0; i<16; i++) { + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + + TST_ON; + wait_delay(tick); + TST_OFF; + wait_delay(tick); + } + + } + + chSysUnlock(); + cprintf(con, "Test dbg Out Freq end\r\n"); + + /* Reconfigure GPIOB3 in Safe Mode / Input */ + bsp_gpio_init(BSP_GPIO_PORTB, 3, MODE_CONFIG_DEV_GPIO_IN, MODE_CONFIG_DEV_GPIO_NOPULL); + + return TRUE; +} + +/* Just debug rx speed ignore all data received until UBTN + a key is pressed */ +int cmd_debug_test_rx(t_hydra_console *con, t_tokenline_parsed *p) +{ + (void)p; + BaseSequentialStream* chp = con->bss; + uint8_t * inbuf = pool_alloc_bytes(0x1000); // 4096 bytes + + cprintf(con, "Test debug-rx started, stop it with UBTN + Key\r\n"); + while(1) { + chnRead(chp, inbuf, 0x0FFF); + + /* Exit if User Button is pressed */ + if (hydrabus_ubtn()) { + break; + } + //get_char(con); + } + pool_free(inbuf); + return TRUE; +} + +int cmd_debug_peek(t_hydra_console *con, t_tokenline_parsed *p, int token_pos) +{ + uint32_t arg_uint, result=0xdeadb33f; + token_pos += 2; + + memcpy(&arg_uint, p->buf + p->tokens[token_pos], sizeof(int)); + result = *((uint32_t *)arg_uint); + cprintf(con, "@ 0x%08x: 0x%08x\r\n", arg_uint, result); + + return token_pos; +} + +int cmd_debug_poke(t_hydra_console *con, t_tokenline_parsed *p, int token_pos) +{ + uint32_t address, value; + + token_pos += 2; + memcpy(&address, p->buf + p->tokens[token_pos], sizeof(int)); + token_pos += 2; + memcpy(&value, p->buf + p->tokens[token_pos], sizeof(int)); + + *(uint32_t *)address = value; + + cprintf(con, "@ 0x%08x=> 0x%08x\r\n", address, value); + + return token_pos; + return token_pos; + +} diff --git a/src/common/debug.h b/src/common/debug.h new file mode 100644 index 00000000..c954a1bb --- /dev/null +++ b/src/common/debug.h @@ -0,0 +1,23 @@ +/* + * HydraBus/HydraNFC + * + * Copyright (C) 2014-2025 Benjamin VERNOUX + * Copyright (C) 2025 Nicolas OBERLI + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +int cmd_debug_timing(t_hydra_console *con, t_tokenline_parsed *p); +int cmd_debug_test_rx(t_hydra_console *con, t_tokenline_parsed *p); +int cmd_debug_peek(t_hydra_console *con, t_tokenline_parsed *p, int token_pos); +int cmd_debug_poke(t_hydra_console *con, t_tokenline_parsed *p, int token_pos); diff --git a/src/common/exec.c b/src/common/exec.c index 8487b510..ea92a89b 100644 --- a/src/common/exec.c +++ b/src/common/exec.c @@ -28,6 +28,7 @@ #include "hydrabus_sd.h" #include "common.h" +#include "debug.h" uint32_t debug_flags = 0; @@ -122,6 +123,12 @@ static int cmd_debug(t_hydra_console *con, t_tokenline_parsed *p) case T_OFF: action = p->tokens[t]; break; + case T_PEEK: + t = cmd_debug_peek(con, p, t); + break; + case T_POKE: + t = cmd_debug_poke(con, p, t); + break; } } if (tmp_debug && !action) { diff --git a/src/hydrabus/commands.c b/src/hydrabus/commands.c index 8651fff1..d2b4f323 100644 --- a/src/hydrabus/commands.c +++ b/src/hydrabus/commands.c @@ -164,6 +164,8 @@ const t_token_dict tl_dict[] = { { T_DELAY, "delay" }, { T_CLOCK_STRETCH, "clock-stretch" }, { T_TIMEOUT, "timeout" }, + { T_PEEK, "peek" }, + { T_POKE, "poke" }, /* Developer warning add new command(s) here */ /* BP-compatible commands */ @@ -2037,6 +2039,17 @@ t_token tokens_show[] = { }; t_token tokens_debug[] = { + { + T_PEEK, + .arg_type = T_ARG_UINT, + .help = "Peek a value from memory" + }, + { + T_POKE, + .flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT, + .arg_type = T_ARG_UINT, + .help = "Poke a value to memory" + }, { T_TOKENLINE, .help = "Tokenline dump for every command" diff --git a/src/hydrabus/commands.h b/src/hydrabus/commands.h index 68844585..dd8e989e 100644 --- a/src/hydrabus/commands.h +++ b/src/hydrabus/commands.h @@ -156,6 +156,8 @@ enum { T_DELAY, T_CLOCK_STRETCH, T_TIMEOUT, + T_PEEK, + T_POKE, /* Developer warning add new command(s) here */ /* BP-compatible commands */