From 4ab131d10a34bb1bde69ae4e1a31da0a3e237849 Mon Sep 17 00:00:00 2001 From: Augusto Zanellato Date: Fri, 18 Aug 2023 22:13:06 +0200 Subject: [PATCH 1/6] Implement wipe fds command used to factory reset --- firmware/application/src/app_cmd.c | 14 +++++ firmware/application/src/data_cmd.h | 1 + firmware/application/src/utils/fds_util.c | 69 ++++++++++++++++++----- firmware/application/src/utils/fds_util.h | 2 + software/script/chameleon_cli_main.py | 1 + software/script/chameleon_cli_unit.py | 26 +++++++++ software/script/chameleon_cmd.py | 8 +++ 7 files changed, 108 insertions(+), 13 deletions(-) diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index 99c7d01..8dba70e 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -357,6 +357,19 @@ data_frame_tx_t* cmd_processor_get_slot_info(uint16_t cmd, uint16_t status, uint return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 16, slot_info); } +data_frame_tx_t* cmd_processor_wipe_fds(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { + bool success = fds_wipe(); + if (!success) { + return data_frame_make(cmd, STATUS_FLASH_WRITE_FAIL, 0, NULL); + } + while (NRF_LOG_PROCESS()); + ret_code_t ret = sd_nvic_SystemReset(); + APP_ERROR_CHECK(ret); + while (1) { + __NOP(); + } +} + data_frame_tx_t* cmd_processor_set_em410x_emu_id(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { if (length == LF_EM410X_TAG_ID_SIZE) { tag_data_buffer_t* buffer = get_buffer_by_tag_type(TAG_TYPE_EM410X); @@ -607,6 +620,7 @@ static cmd_data_map_t m_data_cmd_map[] = { { DATA_CMD_SLOT_DATA_CONFIG_SAVE, NULL, cmd_processor_slot_data_config_save, NULL }, { DATA_CMD_GET_ACTIVE_SLOT, NULL, cmd_processor_get_activated_slot, NULL }, { DATA_CMD_GET_SLOT_INFO, NULL, cmd_processor_get_slot_info, NULL }, + { DATA_CMD_WIPE_FDS, NULL, cmd_processor_wipe_fds, NULL }, { DATA_CMD_SET_EM410X_EMU_ID, NULL, cmd_processor_set_em410x_emu_id, NULL }, diff --git a/firmware/application/src/data_cmd.h b/firmware/application/src/data_cmd.h index 32803cf..2050fa8 100644 --- a/firmware/application/src/data_cmd.h +++ b/firmware/application/src/data_cmd.h @@ -27,6 +27,7 @@ #define DATA_CMD_GET_GIT_VERSION (1017) #define DATA_CMD_GET_ACTIVE_SLOT (1018) #define DATA_CMD_GET_SLOT_INFO (1019) +#define DATA_CMD_WIPE_FDS (1020) // // ****************************************************************** diff --git a/firmware/application/src/utils/fds_util.c b/firmware/application/src/utils/fds_util.c index 43850b8..7006854 100644 --- a/firmware/application/src/utils/fds_util.c +++ b/firmware/application/src/utils/fds_util.c @@ -10,10 +10,11 @@ NRF_LOG_MODULE_REGISTER(); // current write record info static struct { - uint16_t id; // file id - uint16_t key; // file key - bool success; // task is success - bool waiting; // task waiting done. + uint32_t record_id; // record id, used for sync delete + uint16_t id; // file id + uint16_t key; // file key + bool success; // task is success + bool waiting; // task waiting done. } fds_operation_info; @@ -120,12 +121,7 @@ bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void* } else if (err_code == FDS_ERR_NO_SPACE_IN_FLASH) { // 确保还有空间可以操作,否则需要GC // 当前报错是属于空间不足的报错,可能我们需要进行GC NRF_LOG_INFO("FDS no space, gc auto start."); - err_code = fds_gc(); // 发起GC操作 - APP_ERROR_CHECK(err_code); // 检查GC是否正常执行 - // 等待GC完成 - while(!fds_operation_info.success) { - __NOP(); - }; + fds_gc_sync(); // gc完成后,可以重新进行相应的操作了 NRF_LOG_INFO("FDS auto gc success, write record continue."); @@ -162,6 +158,7 @@ int fds_delete_sync(uint16_t id, uint16_t key) { ret_code_t err_code; while(fds_find_record(id, key, &record_desc)) { fds_operation_info.success = false; + fds_record_id_from_desc(&record_desc, &fds_operation_info.record_id); err_code = fds_record_delete(&record_desc); APP_ERROR_CHECK(err_code); delete_count++; @@ -199,9 +196,13 @@ static void fds_evt_handler(fds_evt_t const * p_evt) { } break; case FDS_EVT_DEL_RECORD: { if (p_evt->result == NRF_SUCCESS) { - NRF_LOG_INFO("Record remove: FileID: 0x%04x, RecordKey: 0x%04x", p_evt->del.file_id, p_evt->del.record_key); - if (p_evt->write.file_id == fds_operation_info.id && p_evt->write.record_key == fds_operation_info.key) { - // 上面的逻辑已经确保是我们当前删除记录的任务完成了! + NRF_LOG_INFO( + "Record remove: FileID: 0x%04x, RecordKey: 0x%04x, RecordID: %08x", + p_evt->del.file_id, p_evt->del.record_key, p_evt->del.record_id + ); + if (p_evt->del.record_id == fds_operation_info.record_id) { + // Only check record id because fileID and recordKey aren't available + // if deleting via fds_record_iterate. record id is guaranteed to be unique. fds_operation_info.success = true; } } else { @@ -234,3 +235,45 @@ void fds_util_init() { err_code = fds_init(); APP_ERROR_CHECK(err_code); } + +void fds_gc_sync(void) { + fds_operation_info.success = false; + ret_code_t err_code = fds_gc(); + APP_ERROR_CHECK(err_code); + while(!fds_operation_info.success) { + __NOP(); + }; +} + +static bool fds_next_record_delete_sync() { + fds_find_token_t tok = {0}; + fds_record_desc_t desc = {0}; + if (fds_record_iterate(&desc, &tok) != NRF_SUCCESS) { + NRF_LOG_INFO("No more records to delete"); + return false; + } + + fds_record_id_from_desc(&desc, &fds_operation_info.record_id); + NRF_LOG_INFO("Deleting record with id=%08x", fds_operation_info.record_id); + + fds_operation_info.success = false; + ret_code_t rc = fds_record_delete(&desc); + if (rc != NRF_SUCCESS) { + NRF_LOG_WARNING("Record id=%08x deletion failed with rc=%d!", fds_operation_info.record_id, rc); + return false; + } + + while(!fds_operation_info.success) { + __NOP(); + } + + NRF_LOG_INFO("Record id=%08x deleted successfully", fds_operation_info.record_id); + return true; +} + +bool fds_wipe(void) { + NRF_LOG_INFO("Full fds wipe requested"); + while (fds_next_record_delete_sync()) {} + fds_gc_sync(); + return true; +} diff --git a/firmware/application/src/utils/fds_util.h b/firmware/application/src/utils/fds_util.h index 706ca42..df1b832 100644 --- a/firmware/application/src/utils/fds_util.h +++ b/firmware/application/src/utils/fds_util.h @@ -9,5 +9,7 @@ bool fds_write_sync(uint16_t id, uint16_t key, uint16_t data_length_words, void* int fds_delete_sync(uint16_t id, uint16_t key); bool fds_is_exists(uint16_t id, uint16_t key); void fds_util_init(void); +void fds_gc_sync(void); +bool fds_wipe(void); #endif diff --git a/software/script/chameleon_cli_main.py b/software/script/chameleon_cli_main.py index 5241e90..5519eb8 100755 --- a/software/script/chameleon_cli_main.py +++ b/software/script/chameleon_cli_main.py @@ -99,6 +99,7 @@ class ChameleonCLI: 'reset': new_uint(chameleon_cli_unit.HWSettingsReset, "Reset settings to default values"), 'help': "Chameleon settings management" }, + 'factory_reset': new_uint(chameleon_cli_unit.HWFactoryReset, "Wipe all data and return to factory settings"), 'help': "hardware controller", }, 'hf': { diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 15f056d..7bd0fa7 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -1066,3 +1066,29 @@ class HWSettingsReset(DeviceRequiredUnit): print(" - Reset success @.@~") else: print(" - Reset failed") + +class HWFactoryReset(DeviceRequiredUnit): + def args_parser(self) -> ArgumentParserNoExit: + parser = ArgumentParserNoExit() + parser.description = "Permanently wipes Chameleon to factory settings. " \ + "This will delete all your slot data and custom settings. " \ + "There's no going back." + parser.add_argument( + "--i-know-what-im-doing", + default=False, + action="store_true", + help="Just to be sure :)" + ) + return parser + def on_exec(self, args: argparse.Namespace): + if not args.i_know_what_im_doing: + print("This time your data's safe. Read the command documentation next time.") + return + try: + resp = self.cmd_positive.factory_reset() + if resp.status != chameleon_status.Device.STATUS_DEVICE_SUCCESS: + print(" - Reset failed!") + return + except KeyError: + print(" - A Serial Error above is normal, please ignore it") + print(" - Reset successful! Please reconnect.") diff --git a/software/script/chameleon_cmd.py b/software/script/chameleon_cmd.py index 391f605..284be24 100644 --- a/software/script/chameleon_cmd.py +++ b/software/script/chameleon_cmd.py @@ -30,6 +30,8 @@ DATA_CMD_GET_GIT_VERSION = 1017 DATA_CMD_GET_ACTIVE_SLOT = 1018 DATA_CMD_GET_SLOT_INFO = 1019 +DATA_CMD_WIPE_FDS = 1020 + DATA_CMD_SCAN_14A_TAG = 2000 DATA_CMD_MF1_SUPPORT_DETECT = 2001 DATA_CMD_MF1_NT_LEVEL_DETECT = 2002 @@ -495,6 +497,12 @@ class BaseChameleonCMD: Store settings to flash memory """ return self.device.send_cmd_sync(DATA_CMD_SAVE_SETTINGS, 0x00) + + def factory_reset(self): + """ + Reset to factory settings + """ + return self.device.send_cmd_sync(DATA_CMD_WIPE_FDS, 0x00) class NegativeResponseError(Exception): From bbf5f3027bc8e11558fefdfa487c29f602003dc2 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 18 Aug 2023 21:56:35 +0200 Subject: [PATCH 2/6] NRF_LOG: add option to activate UART backend on SWO pin, see How_to_use_Firmware.md --- How_to_use_Firmware.md | 11 +++++++++++ firmware/Makefile.defs | 3 +++ firmware/application/Makefile | 17 +++++++++++++++++ firmware/application/src/sdk_config.h | 7 ++++--- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/How_to_use_Firmware.md b/How_to_use_Firmware.md index 14dd727..7775ec7 100644 --- a/How_to_use_Firmware.md +++ b/How_to_use_Firmware.md @@ -309,3 +309,14 @@ in a second terminal: ``` JLinkRTTClient ``` + +## Using SWO pin as UART to monitor NRF_LOG + +One can set `NRF_LOG_UART_ON_SWO_ENABLED := 1` in `Makefile.defs` to activate this functionality. +When activated, NRF_LOG will be available if one connects a UART bridge to the SWO pin which will work as a UART TX pin. +UART works at 115200 bauds. E.g. one can use a FTDI dongle and `screen /dev/ttyUSB0 115200`. +Contrary to RTT that needs to be activated by a JTAG probe, UART logs are immediately available. + +Limitations: +* SWO pin is shared with... SWO so when e.g. reflashing the device, garbage may appear on the monitoring terminal. +* SWO pin is also shared with the blue channel of the RGB slot LEDs, so faint blue may appear briefly when logs are sent and LED might not work properly when supposed to be blue. diff --git a/firmware/Makefile.defs b/firmware/Makefile.defs index 7023b1d..6bc5f44 100644 --- a/firmware/Makefile.defs +++ b/firmware/Makefile.defs @@ -27,3 +27,6 @@ CURRENT_DEVICE_TYPE ?= ${CHAMELEON_ULTRA} # Versioning information GIT_VERSION := "$(shell git describe --abbrev=7 --dirty --always --tags)" + +# Enable NRF_LOG on SWO pin as UART TX +NRF_LOG_UART_ON_SWO_ENABLED := 0 diff --git a/firmware/application/Makefile b/firmware/application/Makefile index 05a1d4a..0857b49 100644 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -288,6 +288,23 @@ else $(error Chameleon : No device type define.) endif +ifeq (${NRF_LOG_UART_ON_SWO_ENABLED}, 1) +SRC_FILES += \ + $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_uart.c \ + $(SDK_ROOT)/integration/nrfx/legacy/nrf_drv_uart.c \ + $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_uarte.c + + CFLAGS += -DNRFX_UARTE_ENABLED=1 -DNRFX_UARTE0_ENABLED=1 + CFLAGS += -DNRF_LOG_BACKEND_UART_ENABLED=1 +#define NRF_GPIO_PIN_MAP(port, pin) (((port) << 5) | ((pin) & 0x1F)) +#define NRF_LOG_BACKEND_UART_TX_PIN NRF_GPIO_PIN_MAP(1, 0) + CFLAGS += -DNRF_LOG_BACKEND_UART_TX_PIN=32 +# 30801920 = 115200 baud + CFLAGS += -DNRF_LOG_BACKEND_UART_BAUDRATE=30801920 + CFLAGS += -DNRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE=64 + +$(info Chameleon : enable NRF_LOG on UART via SWO pin.) +endif # Optimization flags OPT = -O3 -g3 diff --git a/firmware/application/src/sdk_config.h b/firmware/application/src/sdk_config.h index 1e990e4..aa7f8eb 100644 --- a/firmware/application/src/sdk_config.h +++ b/firmware/application/src/sdk_config.h @@ -5868,9 +5868,10 @@ // UART_ENABLED - nrf_drv_uart - UART/UARTE peripheral driver - legacy layer //========================================================== -#ifndef UART_ENABLED -#define UART_ENABLED 0 -#endif +//Don't define it at all else it conflicts with NRFX +//#ifndef UART_ENABLED +//#define UART_ENABLED 0 +//#endif // UART_DEFAULT_CONFIG_HWFC - Hardware Flow Control // <0=> Disabled From 7b76aefd5ee488400bf34f30e845792f2d9ec9c3 Mon Sep 17 00:00:00 2001 From: Augusto Zanellato Date: Fri, 18 Aug 2023 22:35:39 +0200 Subject: [PATCH 3/6] ACK command and use delayed reset --- firmware/application/Makefile | 1 + firmware/application/src/app_cmd.c | 13 +++------- .../application/src/utils/delayed_reset.c | 26 +++++++++++++++++++ .../application/src/utils/delayed_reset.h | 3 +++ software/script/chameleon_cli_unit.py | 12 ++++----- 5 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 firmware/application/src/utils/delayed_reset.c create mode 100644 firmware/application/src/utils/delayed_reset.h diff --git a/firmware/application/Makefile b/firmware/application/Makefile index 05a1d4a..cbabcbc 100644 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -30,6 +30,7 @@ SRC_FILES += \ $(PROJ_DIR)/rfid/nfctag/hf/nfc_ntag.c \ $(PROJ_DIR)/rfid/nfctag/lf/lf_tag_em.c \ $(PROJ_DIR)/utils/dataframe.c \ + $(PROJ_DIR)/utils/delayed_reset.c \ $(PROJ_DIR)/utils/fds_util.c \ $(PROJ_DIR)/utils/syssleep.c \ $(PROJ_DIR)/utils/timeslot.c \ diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index 8dba70e..d9df5f7 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -13,6 +13,7 @@ #include "tag_persistence.h" #include "nrf_pwr_mgmt.h" #include "settings.h" +#include "delayed_reset.h" #define NRF_LOG_MODULE_NAME app_cmd @@ -359,15 +360,9 @@ data_frame_tx_t* cmd_processor_get_slot_info(uint16_t cmd, uint16_t status, uint data_frame_tx_t* cmd_processor_wipe_fds(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { bool success = fds_wipe(); - if (!success) { - return data_frame_make(cmd, STATUS_FLASH_WRITE_FAIL, 0, NULL); - } - while (NRF_LOG_PROCESS()); - ret_code_t ret = sd_nvic_SystemReset(); - APP_ERROR_CHECK(ret); - while (1) { - __NOP(); - } + status = success ? STATUS_DEVICE_SUCCESS : STATUS_FLASH_WRITE_FAIL; + delayed_reset(50); + return data_frame_make(cmd, status, 0, NULL); } data_frame_tx_t* cmd_processor_set_em410x_emu_id(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { diff --git a/firmware/application/src/utils/delayed_reset.c b/firmware/application/src/utils/delayed_reset.c new file mode 100644 index 0000000..1dc4c89 --- /dev/null +++ b/firmware/application/src/utils/delayed_reset.c @@ -0,0 +1,26 @@ +#include "app_timer.h" +#include "delayed_reset.h" + +#include "nrf_log.h" +#include "nrf_log_ctrl.h" +#include "nrf_log_default_backends.h" + +APP_TIMER_DEF(m_reset_timer); + +static void delayed_reset_event_handler(void* ctx) { + while (NRF_LOG_PROCESS()); + ret_code_t ret = sd_nvic_SystemReset(); + APP_ERROR_CHECK(ret); + while (1) { + __NOP(); + } +} + +void delayed_reset(uint32_t delay) { + NRF_LOG_INFO("Resetting in %d ms...", delay); + ret_code_t ret; + ret = app_timer_create(&m_reset_timer, APP_TIMER_MODE_SINGLE_SHOT, delayed_reset_event_handler); + APP_ERROR_CHECK(ret); + ret = app_timer_start(m_reset_timer, APP_TIMER_TICKS(delay), NULL); + APP_ERROR_CHECK(ret); +} diff --git a/firmware/application/src/utils/delayed_reset.h b/firmware/application/src/utils/delayed_reset.h new file mode 100644 index 0000000..6be37ad --- /dev/null +++ b/firmware/application/src/utils/delayed_reset.h @@ -0,0 +1,3 @@ +#pragma once + +void delayed_reset(uint32_t delay); \ No newline at end of file diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 7bd0fa7..d4cc599 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -1084,11 +1084,9 @@ class HWFactoryReset(DeviceRequiredUnit): if not args.i_know_what_im_doing: print("This time your data's safe. Read the command documentation next time.") return - try: - resp = self.cmd_positive.factory_reset() - if resp.status != chameleon_status.Device.STATUS_DEVICE_SUCCESS: - print(" - Reset failed!") - return - except KeyError: - print(" - A Serial Error above is normal, please ignore it") + resp = self.cmd_positive.factory_reset() + if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: print(" - Reset successful! Please reconnect.") + print(" - A Serial Error below is normal, please ignore it") + else: + print(" - Reset failed!") From 8e093d00b678c60f125b07af439eb74fde6460f0 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 18 Aug 2023 21:49:56 +0200 Subject: [PATCH 4/6] Enable watchdog --- firmware/application/Makefile | 2 ++ firmware/application/src/app_main.c | 5 ++++- firmware/application/src/bsp/bsp_wdt.c | 30 ++++++++++++++++++++++++++ firmware/application/src/bsp/bsp_wdt.h | 15 +++++++++++++ firmware/application/src/sdk_config.h | 13 +++++------ 5 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 firmware/application/src/bsp/bsp_wdt.c create mode 100644 firmware/application/src/bsp/bsp_wdt.h diff --git a/firmware/application/Makefile b/firmware/application/Makefile index 0857b49..4208e15 100644 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -17,6 +17,7 @@ SRC_FILES += \ $(PROJ_DIR)/rgb_marquee.c \ $(PROJ_DIR)/bsp/bsp_delay.c \ $(PROJ_DIR)/bsp/bsp_time.c \ + $(PROJ_DIR)/bsp/bsp_wdt.c \ $(PROJ_DIR)/rfid/crc_utils.c \ $(PROJ_DIR)/rfid/hex_utils.c \ $(PROJ_DIR)/rfid/mf1_crapto1.c \ @@ -93,6 +94,7 @@ SRC_FILES += \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_spi.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_rng.c \ $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_ppi.c \ + $(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_wdt.c \ $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT.c \ $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_Syscalls_GCC.c \ $(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_printf.c \ diff --git a/firmware/application/src/app_main.c b/firmware/application/src/app_main.c index 1e92c1b..192a156 100644 --- a/firmware/application/src/app_main.c +++ b/firmware/application/src/app_main.c @@ -27,6 +27,7 @@ NRF_LOG_MODULE_REGISTER(); #include "ble_main.h" #include "bsp_delay.h" #include "bsp_time.h" +#include "bsp_wdt.h" #include "dataframe.h" #include "fds_util.h" #include "hex_utils.h" @@ -38,7 +39,6 @@ NRF_LOG_MODULE_REGISTER(); #include "settings.h" - // Defining soft timers APP_TIMER_DEF(m_button_check_timer); // Timer for button debounce static bool m_is_read_btn_press = false; @@ -545,6 +545,7 @@ int main(void) { // usbd event listener APP_ERROR_CHECK(app_usbd_power_events_enable()); + bsp_wdt_init(); // Enter main loop. NRF_LOG_INFO("Chameleon working"); while (1) { @@ -558,6 +559,8 @@ int main(void) { while (NRF_LOG_PROCESS()); // USB event process while (app_usbd_event_queue_process()); + // WDT refresh + bsp_wdt_feed(); // No task to process, system sleep enter. // If system idle sometime, we can enter deep sleep state. // Some task process done, we can enter cpu sleep state. diff --git a/firmware/application/src/bsp/bsp_wdt.c b/firmware/application/src/bsp/bsp_wdt.c new file mode 100644 index 0000000..5405bf2 --- /dev/null +++ b/firmware/application/src/bsp/bsp_wdt.c @@ -0,0 +1,30 @@ +#include "nrf_drv_wdt.h" +#include "hw_connect.h" +#include "nrf_gpio.h" + +static nrf_drv_wdt_channel_id m_channel_id; + +static void wdt_event_handler(void) +{ + //NOTE: The max amount of time we can spend in WDT interrupt is two cycles of 32768[Hz] clock - after that, reset occurs + uint32_t* p_led_array = hw_get_led_array(); + for (uint8_t i = 0; i < RGB_LIST_NUM; i++) { + nrf_gpio_pin_clear(p_led_array[i]); + } +} + +void bsp_wdt_init(void) { + ret_code_t err_code; +// err_code = nrf_drv_clock_init(); // already done by usb_cdc_init() -> app_usbd_init() +// APP_ERROR_CHECK(err_code); + nrf_drv_wdt_config_t config = NRF_DRV_WDT_DEAFULT_CONFIG; // typo is in the SDK... + err_code = nrf_drv_wdt_init(&config, wdt_event_handler); + APP_ERROR_CHECK(err_code); + err_code = nrf_drv_wdt_channel_alloc(&m_channel_id); + APP_ERROR_CHECK(err_code); + nrf_drv_wdt_enable(); +} + +void bsp_wdt_feed(void) { + nrf_drv_wdt_channel_feed(m_channel_id); +} diff --git a/firmware/application/src/bsp/bsp_wdt.h b/firmware/application/src/bsp/bsp_wdt.h new file mode 100644 index 0000000..0b19764 --- /dev/null +++ b/firmware/application/src/bsp/bsp_wdt.h @@ -0,0 +1,15 @@ +#ifndef __BSP_WDT_H__ +#define __BSP_WDT_H__ + +#ifdef __cplusplus + extern "C" { +#endif + +void bsp_wdt_init(void); +void bsp_wdt_feed(void); + +#ifdef __cplusplus +} +#endif + +#endif // __BSP_WDT_H__ diff --git a/firmware/application/src/sdk_config.h b/firmware/application/src/sdk_config.h index aa7f8eb..c65803f 100644 --- a/firmware/application/src/sdk_config.h +++ b/firmware/application/src/sdk_config.h @@ -4630,7 +4630,7 @@ // NRFX_WDT_ENABLED - nrfx_wdt - WDT peripheral driver //========================================================== #ifndef NRFX_WDT_ENABLED -#define NRFX_WDT_ENABLED 0 +#define NRFX_WDT_ENABLED 1 #endif // NRFX_WDT_CONFIG_BEHAVIOUR - WDT behavior in CPU SLEEP or HALT mode @@ -4647,7 +4647,7 @@ #ifndef NRFX_WDT_CONFIG_RELOAD_VALUE -#define NRFX_WDT_CONFIG_RELOAD_VALUE 2000 +#define NRFX_WDT_CONFIG_RELOAD_VALUE 5000 #endif // NRFX_WDT_CONFIG_NO_IRQ - Remove WDT IRQ handling from WDT driver @@ -4677,7 +4677,7 @@ // NRFX_WDT_CONFIG_LOG_ENABLED - Enables logging in the module. //========================================================== #ifndef NRFX_WDT_CONFIG_LOG_ENABLED -#define NRFX_WDT_CONFIG_LOG_ENABLED 0 +#define NRFX_WDT_CONFIG_LOG_ENABLED 1 #endif // NRFX_WDT_CONFIG_LOG_LEVEL - Default Severity level @@ -6019,9 +6019,10 @@ // WDT_ENABLED - nrf_drv_wdt - WDT peripheral driver - legacy layer //========================================================== -#ifndef WDT_ENABLED -#define WDT_ENABLED 0 -#endif +//Don't define it at all else it conflicts with NRFX +//#ifndef WDT_ENABLED +//#define WDT_ENABLED 0 +//#endif // WDT_CONFIG_BEHAVIOUR - WDT behavior in CPU SLEEP or HALT mode // <1=> Run in SLEEP, Pause in HALT From 3bfc52432ac995cb26ccfefee9d9d18b13b69dab Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 19 Aug 2023 00:13:43 +0200 Subject: [PATCH 5/6] change slot nicknames encoding: gbk -> utf8 --- firmware/application/src/app_cmd.c | 2 -- software/script/chameleon_cli_unit.py | 7 ++++--- software/script/chameleon_cmd.py | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index 99c7d01..dbe49d3 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -473,7 +473,6 @@ data_frame_tx_t* cmd_processor_set_mf1_anti_collision_res(uint16_t cmd, uint16_t } data_frame_tx_t* cmd_processor_set_slot_tag_nick_name(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { - // one chinese have 2byte(gbk). if (length > 34 || length < 3) { status = STATUS_PAR_ERR; } else { @@ -498,7 +497,6 @@ data_frame_tx_t* cmd_processor_set_slot_tag_nick_name(uint16_t cmd, uint16_t sta } data_frame_tx_t* cmd_processor_get_slot_tag_nick_name(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { - // one chinese have 2byte(gbk). if (length != 2) { status = STATUS_PAR_ERR; } else { diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 15f056d..a6964aa 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -940,9 +940,10 @@ class HWSlotNickSet(SlotIndexRequireUint, SenseTypeRequireUint): slot_num = args.slot sense_type = args.sense_type name: str = args.name - if len(name.encode(encoding="gbk")) > 32: + uname = name.encode(encoding="utf8") + if len(uname) > 32: raise ValueError("Your tag nick name too long.") - self.cmd_positive.set_slot_tag_nick_name(slot_num, sense_type, name) + self.cmd_positive.set_slot_tag_nick_name(slot_num, sense_type, uname) print(f' - Set tag nick name for slot {slot_num} success.') @@ -958,7 +959,7 @@ class HWSlotNickGet(SlotIndexRequireUint, SenseTypeRequireUint): slot_num = args.slot sense_type = args.sense_type res = self.cmd_positive.get_slot_tag_nick_name(slot_num, sense_type) - print(f' - Get tag nick name for slot {slot_num}: {res.data.decode(encoding="gbk")}') + print(f' - Get tag nick name for slot {slot_num}: {res.data.decode(encoding="utf8")}') class HWSlotUpdate(DeviceRequiredUnit): diff --git a/software/script/chameleon_cmd.py b/software/script/chameleon_cmd.py index 391f605..88ab7de 100644 --- a/software/script/chameleon_cmd.py +++ b/software/script/chameleon_cmd.py @@ -433,7 +433,7 @@ class BaseChameleonCMD: data.extend(uid) return self.device.send_cmd_sync(DATA_CMD_SET_MF1_ANTI_COLLISION_RES, 0X00, data) - def set_slot_tag_nick_name(self, slot: int, sense_type: int, name: str): + def set_slot_tag_nick_name(self, slot: int, sense_type: int, name: bytes): """ 设置MF1的模拟卡的防冲撞资源信息 :param slot: 卡槽号码 @@ -443,7 +443,7 @@ class BaseChameleonCMD: """ data = bytearray() data.extend([slot, sense_type]) - data.extend(name.encode(encoding="gbk")) + data.extend(name) return self.device.send_cmd_sync(DATA_CMD_SET_SLOT_TAG_NICK, 0x00, data) def get_slot_tag_nick_name(self, slot: int, sense_type: int): @@ -620,7 +620,7 @@ class PositiveChameleonCMD(BaseChameleonCMD): self.check_status(ret.status, chameleon_status.Device.STATUS_DEVICE_SUCCESS) return ret - def set_slot_tag_nick_name(self, slot: int, sense_type: int, name: str): + def set_slot_tag_nick_name(self, slot: int, sense_type: int, name: bytes): ret = super(PositiveChameleonCMD, self).set_slot_tag_nick_name(slot, sense_type, name) self.check_status(ret.status, chameleon_status.Device.STATUS_DEVICE_SUCCESS) return ret From 28572958a7946a7ad737a3b85b916f53bf434dda Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 19 Aug 2023 00:20:17 +0200 Subject: [PATCH 6/6] thinner logo to fit 80col terms --- software/script/chameleon_cli_main.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/software/script/chameleon_cli_main.py b/software/script/chameleon_cli_main.py index 5241e90..7bc9e85 100755 --- a/software/script/chameleon_cli_main.py +++ b/software/script/chameleon_cli_main.py @@ -27,13 +27,12 @@ LITE = r""" # create by http://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=Chameleon%20Ultra BANNER = f""" - ██████╗██╗ ██╗ █████╗ ███╗ ███╗███████╗██╗ ███████╗ ██████╗ ███╗ ██╗ -██╔════╝██║ ██║██╔══██╗████╗ ████║██╔════╝██║ ██╔════╝██╔═══██╗████╗ ██║ -██║ ███████║███████║██╔████╔██║█████╗ ██║ █████╗ ██║ ██║██╔██╗ ██║ -██║ ██╔══██║██╔══██║██║╚██╔╝██║██╔══╝ ██║ ██╔══╝ ██║ ██║██║╚██╗██║ -╚██████╗██║ ██║██║ ██║██║ ╚═╝ ██║███████╗███████╗███████╗╚██████╔╝██║ ╚████║ - ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝ ╚═════╝ ╚═╝ ╚═══╝ - + ██████╗██╗ ██╗ █████╗ ██╗ ██╗███████╗██╗ ███████╗ █████╗ ██╗ ██╗ +██╔════╝██║ ██║██╔══██╗███╗ ███║██╔════╝██║ ██╔════╝██╔══██╗███╗ ██║ +██║ ███████║███████║████████║█████╗ ██║ █████╗ ██║ ██║████╗██║ +██║ ██╔══██║██╔══██║██╔██╔██║██╔══╝ ██║ ██╔══╝ ██║ ██║██╔████║ +╚██████╗██║ ██║██║ ██║██║╚═╝██║███████╗███████╗███████╗╚█████╔╝██║╚███║ + ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝ ╚════╝ ╚═╝ ╚══╝ """