From f509a5896ad8d36eb308fead925884fbbe88087e Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 08:27:15 +0800 Subject: [PATCH 01/18] Add multi targets support: NRF51822/STM32F051/STM32F071/STM32F013RC/STM32F405 --- projects.yaml | 4 + records/board/mesh_multi_targets.yaml | 15 +++ source/board/mesh_multi_targets.c | 22 ++++ source/daplink/drag-n-drop/flash_decoder.c | 4 +- source/daplink/interface/target_flash.c | 53 ++++++-- source/daplink/validation.c | 9 +- source/hic_hal/flash_blob.h | 4 + source/hic_hal/target_config.h | 9 +- .../DBG_STM32F051/stm32f051_target_flash.c | 79 ++++++++++++ .../DBG_STM32F071/stm32f071_target_flash.c | 78 +++++++++++ .../DBG_STM32F103RC/stm32f103_target_flash.c | 74 +++++++++++ .../DBG_STM32F405/stm32f405_target_flash.c | 117 +++++++++++++++++ .../DBG_nRF51822/nrf51_target_flash.c | 73 +++++++++++ source/target/mesheven/target.c | 122 ++++++++++++++++++ source/target/mesheven/target_ids.h | 31 +++++ source/target/mesheven/target_reset.c | 91 +++++++++++++ 16 files changed, 769 insertions(+), 16 deletions(-) create mode 100644 records/board/mesh_multi_targets.yaml create mode 100644 source/board/mesh_multi_targets.c create mode 100644 source/target/mesheven/DBG_STM32F051/stm32f051_target_flash.c create mode 100644 source/target/mesheven/DBG_STM32F071/stm32f071_target_flash.c create mode 100644 source/target/mesheven/DBG_STM32F103RC/stm32f103_target_flash.c create mode 100644 source/target/mesheven/DBG_STM32F405/stm32f405_target_flash.c create mode 100644 source/target/mesheven/DBG_nRF51822/nrf51_target_flash.c create mode 100644 source/target/mesheven/target.c create mode 100644 source/target/mesheven/target_ids.h create mode 100644 source/target/mesheven/target_reset.c diff --git a/projects.yaml b/projects.yaml index 8c6dd8101..4fb759faa 100644 --- a/projects.yaml +++ b/projects.yaml @@ -181,3 +181,7 @@ projects: - *module_if - *module_hic_lpc11u35 - records/board/archlink.yaml + lpc11u35_multi_targets_if: + - *module_if + - *module_hic_lpc11u35 + - records/board/mesh_multi_targets.yaml diff --git a/records/board/mesh_multi_targets.yaml b/records/board/mesh_multi_targets.yaml new file mode 100644 index 000000000..3454a846b --- /dev/null +++ b/records/board/mesh_multi_targets.yaml @@ -0,0 +1,15 @@ +common: + includes: + - source/target/mesheven/ + sources: + board: + - source/board/mesh_multi_targets.c + target: + - source/target/mesheven/DBG_nRF51822/nrf51_target_flash.c + - source/target/mesheven/DBG_STM32F051/stm32f051_target_flash.c + - source/target/mesheven/DBG_STM32F071/stm32f071_target_flash.c + - source/target/mesheven/DBG_STM32F103RC/stm32f103_target_flash.c + - source/target/mesheven/DBG_STM32F405/stm32f405_target_flash.c + - source/target/mesheven/target.c + - source/target/mesheven/target_ids.h + - source/target/mesheven/target_reset.c diff --git a/source/board/mesh_multi_targets.c b/source/board/mesh_multi_targets.c new file mode 100644 index 000000000..f697c31ea --- /dev/null +++ b/source/board/mesh_multi_targets.c @@ -0,0 +1,22 @@ +/** + * @file mesh_multi_targets.c + * @brief board ID for the Mesheven multi-targets: nRF51/STM32F051/STM32F072/STM32F103/STM32F405 + * + * DAPLink Interface Firmware + * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + */ + +const char *board_id = "2000"; diff --git a/source/daplink/drag-n-drop/flash_decoder.c b/source/daplink/drag-n-drop/flash_decoder.c index 8277f656a..cf4922f65 100644 --- a/source/daplink/drag-n-drop/flash_decoder.c +++ b/source/daplink/drag-n-drop/flash_decoder.c @@ -137,7 +137,7 @@ error_t flash_decoder_get_flash(flash_decoder_type_t type, uint32_t addr, bool a flash_intf_local = flash_intf_iap_protected; } } else if (FLASH_DECODER_TYPE_TARGET == type) { - flash_start_local = target_device.flash_start; + flash_start_local = target_device[targetID].flash_start; flash_intf_local = flash_intf_target; } } else { @@ -338,7 +338,7 @@ static bool flash_decoder_is_at_end(uint32_t addr, const uint8_t *data, uint32_t break; case FLASH_DECODER_TYPE_TARGET: - end_addr = target_device.flash_end; + end_addr = target_device[targetID].flash_end; break; default: diff --git a/source/daplink/interface/target_flash.c b/source/daplink/interface/target_flash.c index f7c99feeb..718d520f3 100644 --- a/source/daplink/interface/target_flash.c +++ b/source/daplink/interface/target_flash.c @@ -32,6 +32,8 @@ #include "util.h" #include "settings.h" +#include "target_ids.h" + static error_t target_flash_init(void); static error_t target_flash_uninit(void); static error_t target_flash_program_page(uint32_t adr, const uint8_t *buf, uint32_t size); @@ -52,10 +54,17 @@ static const flash_intf_t flash_intf = { const flash_intf_t *const flash_intf_target = &flash_intf; +static uint32_t lastEraseSectorNumber = 0xFFFFFFFF; + static error_t target_flash_init() { - const program_target_t *const flash = target_device.flash_algo; + if (targetID == Target_UNKNOWN) + return ERROR_TARGET_UNKNOWN; + + const program_target_t *const flash = target_device[targetID].flash_algo; + lastEraseSectorNumber = 0xFFFFFFFF; + if (0 == target_set_state(RESET_PROGRAM)) { return ERROR_RESET; } @@ -65,7 +74,7 @@ static error_t target_flash_init() return ERROR_ALGO_DL; } - if (0 == swd_flash_syscall_exec(&flash->sys_call_s, flash->init, target_device.flash_start, 0, 0, 0)) { + if (0 == swd_flash_syscall_exec(&flash->sys_call_s, flash->init, target_device[targetID].flash_start, 0, 0, 0)) { return ERROR_INIT; } @@ -85,7 +94,10 @@ static error_t target_flash_uninit(void) static error_t target_flash_program_page(uint32_t addr, const uint8_t *buf, uint32_t size) { - const program_target_t *const flash = target_device.flash_algo; + if (targetID == Target_UNKNOWN) + return ERROR_TARGET_UNKNOWN; + + const program_target_t *const flash = target_device[targetID].flash_algo; // check if security bits were set if (1 == security_bits_set(addr, (uint8_t *)buf, size)) { @@ -94,7 +106,20 @@ static error_t target_flash_program_page(uint32_t addr, const uint8_t *buf, uint while (size > 0) { uint32_t write_size = MIN(size, flash->program_buffer_size); - + uint32_t nextSectorAddress = 0; + uint32_t currentSectorNumber = target_device[targetID].get_sector_number(addr); + if (currentSectorNumber != lastEraseSectorNumber) { + if(ERROR_SUCCESS != target_flash_erase_sector(currentSectorNumber)){ + return ERROR_ERASE_SECTOR; + } + lastEraseSectorNumber = currentSectorNumber; + } + //check is cross sectors + nextSectorAddress = target_device[targetID].get_sector_address(currentSectorNumber) + target_device[targetID].get_sector_length(currentSectorNumber); + if((addr + write_size) > nextSectorAddress){ + write_size = nextSectorAddress - addr; + } + // Write page to buffer if (!swd_write_memory(flash->program_buffer, (uint8_t *)buf, write_size)) { return ERROR_ALGO_DATA_SEQ; @@ -104,7 +129,7 @@ static error_t target_flash_program_page(uint32_t addr, const uint8_t *buf, uint if (!swd_flash_syscall_exec(&flash->sys_call_s, flash->program_page, addr, - flash->program_buffer_size, + write_size, flash->program_buffer, 0)) { return ERROR_WRITE; @@ -120,9 +145,15 @@ static error_t target_flash_program_page(uint32_t addr, const uint8_t *buf, uint static error_t target_flash_erase_sector(uint32_t sector) { - const program_target_t *const flash = target_device.flash_algo; + uint32_t address = 0; + + if (targetID == Target_UNKNOWN) + return ERROR_TARGET_UNKNOWN; + + const program_target_t *const flash = target_device[targetID].flash_algo; - if (0 == swd_flash_syscall_exec(&flash->sys_call_s, flash->erase_sector, sector * target_device.sector_size, 0, 0, 0)) { + address = target_device[targetID].get_sector_address(sector); + if (0 == swd_flash_syscall_exec(&flash->sys_call_s, flash->erase_sector, address, 0, 0, 0)) { return ERROR_ERASE_SECTOR; } @@ -132,14 +163,14 @@ static error_t target_flash_erase_sector(uint32_t sector) static error_t target_flash_erase_chip(void) { error_t status = ERROR_SUCCESS; - const program_target_t *const flash = target_device.flash_algo; + const program_target_t *const flash = target_device[targetID].flash_algo; if (0 == swd_flash_syscall_exec(&flash->sys_call_s, flash->erase_chip, 0, 0, 0, 0)) { return ERROR_ERASE_ALL; } // Reset and re-initialize the target after the erase if required - if (target_device.erase_reset) { + if (target_device[targetID].erase_reset) { status = target_flash_init(); } @@ -149,11 +180,11 @@ static error_t target_flash_erase_chip(void) static uint32_t target_flash_program_page_min_size(uint32_t addr) { uint32_t size = 256; - util_assert(target_device.sector_size >= size); + util_assert(target_device[targetID].sector_size >= size); return size; } static uint32_t target_flash_erase_sector_size(uint32_t addr) { - return target_device.sector_size; + return target_device[targetID].sector_size; } diff --git a/source/daplink/validation.c b/source/daplink/validation.c index 6ce97cd3e..9aff1ef36 100644 --- a/source/daplink/validation.c +++ b/source/daplink/validation.c @@ -23,6 +23,8 @@ #include "string.h" #include "target_config.h" +#include "target_ids.h" + static inline uint32_t test_range(const uint32_t test, const uint32_t min, const uint32_t max) { return ((test < min) || (test > max)) ? 0 : 1; @@ -38,8 +40,11 @@ uint8_t validate_bin_nvic(const uint8_t *buf) uint32_t i = 4, nvic_val = 0; // test the initial SP value memcpy(&nvic_val, buf + 0, sizeof(nvic_val)); + + if (targetID == Target_UNKNOWN) + return 0; - if (0 == test_range(nvic_val, target_device.ram_start, target_device.ram_end)) { + if (0 == test_range(nvic_val, target_device[targetID].ram_start, target_device[targetID].ram_end)) { return 0; } @@ -49,7 +54,7 @@ uint8_t validate_bin_nvic(const uint8_t *buf) for (; i <= 12; i += 4) { memcpy(&nvic_val, buf + i, sizeof(nvic_val)); - if (0 == test_range(nvic_val, target_device.flash_start, target_device.flash_end)) { + if (0 == test_range(nvic_val, target_device[targetID].flash_start, target_device[targetID].flash_end)) { return 0; } } diff --git a/source/hic_hal/flash_blob.h b/source/hic_hal/flash_blob.h index 33ba489fa..820c4eed4 100644 --- a/source/hic_hal/flash_blob.h +++ b/source/hic_hal/flash_blob.h @@ -46,6 +46,10 @@ typedef struct { const uint32_t algo_size; const uint32_t *algo_blob; const uint32_t program_buffer_size; + + uint32_t flash_sector_size; + uint32_t auto_increment_page_size; + uint32_t flash_base_addr; } program_target_t; #ifdef __cplusplus diff --git a/source/hic_hal/target_config.h b/source/hic_hal/target_config.h index a149a3969..14e3d12b7 100644 --- a/source/hic_hal/target_config.h +++ b/source/hic_hal/target_config.h @@ -53,9 +53,16 @@ typedef struct target_cfg { uint32_t ram_end; /*!< Highest contigous RAM address the application uses */ program_target_t *flash_algo; /*!< A pointer to the flash algorithm structure */ uint8_t erase_reset; /*!< Reset after performing an erase */ + + uint32_t (*get_sector_number)(uint32_t addr); // convert flash address to sector number + uint32_t (*get_sector_address)(uint32_t sector); //convert sector number to flash address + uint32_t (*get_sector_length)(uint32_t sector); //get sector size. (some device has difference sector size) + } target_cfg_t; -extern const target_cfg_t target_device; +extern const target_cfg_t target_device[]; +extern uint8_t targetID; +uint8_t swd_init_get_target(void); #ifdef __cplusplus } diff --git a/source/target/mesheven/DBG_STM32F051/stm32f051_target_flash.c b/source/target/mesheven/DBG_STM32F051/stm32f051_target_flash.c new file mode 100644 index 000000000..7a400f4c0 --- /dev/null +++ b/source/target/mesheven/DBG_STM32F051/stm32f051_target_flash.c @@ -0,0 +1,79 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "flash_blob.h" +#include "swd_host.h" +#include "target_reset.h" +#include +#include +#include + + +static const uint32_t STM32F051_FLM[] = { + 0xE00ABE00, 0x062D780D, 0x24084068, 0xD3000040, 0x1E644058, 0x1C49D1FA, 0x2A001E52, 0x4770D1F2, + + 0x49544853, 0x48546048, 0x20006048, 0xb5104770, 0x20344603, 0x60e04c4f, 0xbd102000, 0x20004601, + 0xb5004770, 0x23002200, 0x6902484a, 0x40102080, 0xd1012880, 0xffe4f7ff, 0x4846bf00, 0x07d868c3, + 0xd1fa0fc0, 0x69024843, 0x43022004, 0x61024841, 0x20406902, 0x483f4302, 0xbf006102, 0x68c3483d, + 0x0fc007d8, 0x483bd1fa, 0x21046902, 0x43884610, 0x48384602, 0x20006102, 0xb510bd00, 0x22004603, + 0x48342400, 0x20806902, 0x28804010, 0xf7ffd101, 0xbf00ffb7, 0x68c4482f, 0x0fc007e0, 0x482dd1fa, + 0x20026902, 0x482b4302, 0x61436102, 0x20406902, 0x48284302, 0xbf006102, 0x68c44826, 0x0fc007e0, + 0x4824d1fa, 0x21026902, 0x43884610, 0x48214602, 0x20006102, 0xb5f7bd10, 0x22004615, 0x27002600, + 0x462c9b00, 0x6902481b, 0x40102080, 0xd1012880, 0xff86f7ff, 0x4817bf00, 0x07f068c6, 0xd1fa0fc0, + 0x4814e01b, 0x20016902, 0x48124302, 0x88206102, 0xbf008018, 0x68c6480f, 0x0fc007f0, 0x8820d1fa, + 0x42888819, 0x480bd006, 0x08526902, 0x61020052, 0xbdfe2001, 0x1ca41c9b, 0x98011c7f, 0x42b80840, + 0x4804d8df, 0x08526902, 0x61020052, 0xe7f02000, 0x45670123, 0x40022000, 0xcdef89ab, 0x00000000, +}; + +const program_target_t stm32f051_flash = { + 0x2000002F, // Init + 0x2000003D, // UnInit + 0x20000043, // EraseChip + 0x2000009B, // EraseSector + 0x200000F7, // ProgramPage + + // RSB : base adreess is address of Execution Region PrgData in map file + // to access global/static data + // RSP : Initial stack pointer + { + 0x20000001, // breakpoint location + 0x20000020+0x00000180, // static_base + 0x20000C00 // stack_pointer + }, + 0x20000400, // program_buffer + 0x20000000, // algo_start + 0x00000180, // algo_size + STM32F051_FLM, // image + 1024, // ram_to_flash_bytes_to_be_written + 1024, // flash sector size: 1KB + 1024, // auto increment page size + 0x08000000 // flash base address +}; + +uint32_t stm32f051_GetSecNum (uint32_t addr){ + uint32_t rc = ( (addr - 0x08000000) >> 10); + return rc; +} + +uint32_t stm32f051_GetSecAddress(uint32_t sector){ + uint32_t rc = 0x08000000 + (sector << 10); + return rc; +} + +uint32_t stm32f051_GetSecLength(uint32_t sector){ + return 0x400; //1024 +} + diff --git a/source/target/mesheven/DBG_STM32F071/stm32f071_target_flash.c b/source/target/mesheven/DBG_STM32F071/stm32f071_target_flash.c new file mode 100644 index 000000000..48cb985fb --- /dev/null +++ b/source/target/mesheven/DBG_STM32F071/stm32f071_target_flash.c @@ -0,0 +1,78 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "flash_blob.h" +#include "swd_host.h" +#include "target_reset.h" +#include +#include +#include + + +static const uint32_t STM32F071_FLM[] = { + 0xE00ABE00, 0x062D780D, 0x24084068, 0xD3000040, 0x1E644058, 0x1C49D1FA, 0x2A001E52, 0x4770D1F2, + + 0x49544853, 0x48546048, 0x20006048, 0xb5104770, 0x20344603, 0x60e04c4f, 0xbd102000, 0x20004601, + 0xb5004770, 0x23002200, 0x6902484a, 0x40102080, 0xd1012880, 0xffe4f7ff, 0x4846bf00, 0x07d868c3, + 0xd1fa0fc0, 0x69024843, 0x43022004, 0x61024841, 0x20406902, 0x483f4302, 0xbf006102, 0x68c3483d, + 0x0fc007d8, 0x483bd1fa, 0x21046902, 0x43884610, 0x48384602, 0x20006102, 0xb510bd00, 0x22004603, + 0x48342400, 0x20806902, 0x28804010, 0xf7ffd101, 0xbf00ffb7, 0x68c4482f, 0x0fc007e0, 0x482dd1fa, + 0x20026902, 0x482b4302, 0x61436102, 0x20406902, 0x48284302, 0xbf006102, 0x68c44826, 0x0fc007e0, + 0x4824d1fa, 0x21026902, 0x43884610, 0x48214602, 0x20006102, 0xb5f7bd10, 0x22004615, 0x27002600, + 0x462c9b00, 0x6902481b, 0x40102080, 0xd1012880, 0xff86f7ff, 0x4817bf00, 0x07f068c6, 0xd1fa0fc0, + 0x4814e01b, 0x20016902, 0x48124302, 0x88206102, 0xbf008018, 0x68c6480f, 0x0fc007f0, 0x8820d1fa, + 0x42888819, 0x480bd006, 0x08526902, 0x61020052, 0xbdfe2001, 0x1ca41c9b, 0x98011c7f, 0x42b80840, + 0x4804d8df, 0x08526902, 0x61020052, 0xe7f02000, 0x45670123, 0x40022000, 0xcdef89ab, 0x00000000, +}; + +const program_target_t stm32f071_flash = { + 0x2000002F, // Init + 0x2000003D, // UnInit + 0x20000043, // EraseChip + 0x2000009B, // EraseSector + 0x200000F7, // ProgramPage + + // RSB : base adreess is address of Execution Region PrgData in map file + // to access global/static data + // RSP : Initial stack pointer + { + 0x20000001, // breakpoint location + 0x20000020+0x00000180, // static_base + 0x20001000 // stack_pointer + }, + 0x20000400, // program_buffer + 0x20000000, // algo_start + 0x00000180, // algo_size + STM32F071_FLM, // image + 2048, // ram_to_flash_bytes_to_be_written + 2048, // flash sector size: 2KB + 2048, // auto increment page size + 0x08000000 // flash base address +}; + +uint32_t stm32f071_GetSecNum (uint32_t addr){ + uint32_t rc = ( (addr - 0x08000000) >> 11); + return rc; +} + +uint32_t stm32f071_GetSecAddress(uint32_t sector){ + uint32_t rc = 0x08000000 + (sector << 11); + return rc; +} + +uint32_t stm32f071_GetSecLength(uint32_t sector){ + return 0x800; //2048 +} diff --git a/source/target/mesheven/DBG_STM32F103RC/stm32f103_target_flash.c b/source/target/mesheven/DBG_STM32F103RC/stm32f103_target_flash.c new file mode 100644 index 000000000..fcad6f2c3 --- /dev/null +++ b/source/target/mesheven/DBG_STM32F103RC/stm32f103_target_flash.c @@ -0,0 +1,74 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "flash_blob.h" +#include "swd_host.h" +#include "target_reset.h" +#include +#include +#include + + +static const uint32_t STM32F103RC_FLM[] = { + 0xE00ABE00, 0x062D780D, 0x24084068, 0xD3000040, 0x1E644058, 0x1C49D1FA, 0x2A001E52, 0x4770D1F2, + 0x49384839, 0x49396041, 0x20006041, 0x49364770, 0x60c82034, 0x47702000, 0x47702000, 0xb5004a32, + 0x06006910, 0xf7ffd501, 0x68d0ffeb, 0xd1fc07c0, 0xf0406910, 0x61100004, 0xf0406910, 0x61100040, + 0x07c068d0, 0x6910d1fc, 0x0004f020, 0x20006110, 0x4a25bd00, 0x4603b500, 0x06006910, 0xf7ffd501, + 0x68d1ffcf, 0xd1fc07c9, 0xf0406910, 0x61100002, 0x69106153, 0x0040f040, 0x68d06110, 0xd1fc07c0, + 0xf0206910, 0x61100002, 0xbd002000, 0x4d16b570, 0x460e4603, 0x24006928, 0xd5010600, 0xffb0f7ff, + 0x07c068e8, 0xe014d1fc, 0x0001f040, 0x88106128, 0x68e88018, 0xd1fc07c0, 0x88198810, 0xd0054288, + 0xf0206928, 0x61280001, 0xbd702001, 0x1c9b1c92, 0x69281c64, 0x0f56ebb4, 0xf020d3e6, 0x61280001, + 0xbd702000, 0x45670123, 0x40022000, 0xcdef89ab, 0x00000000, +}; + +const program_target_t stm32f103_flash = { + 0x2000002F, // Init + 0x20000039, // UnInit + 0x2000003D, // EraseChip + 0x20000073, // EraseSector + 0x200000AD, // ProgramPage + + // RSB : base adreess is address of Execution Region PrgData in map file + // to access global/static data + // RSP : Initial stack pointer + { + 0x20000001, // breakpoint location + 0x20000020+0x00000114, // static_base + 0x20002000 // stack_pointer + }, + 0x20001000, // program_buffer + 0x20000000, // algo_start + 0x00000114, // algo_size + STM32F103RC_FLM, // image + 2048, // ram_to_flash_bytes_to_be_written + 2048, // flash sector size: 2KB + 2048, // auto increment page size + 0x08000000 // flash base address +}; + +uint32_t stm32f103_GetSecNum (uint32_t addr){ + uint32_t rc = ( (addr - 0x08000000) >> 11); + return rc; +} + +uint32_t stm32f103_GetSecAddress(uint32_t sector){ + uint32_t rc = 0x08000000 + (sector << 11); + return rc; +} + +uint32_t stm32f103_GetSecLength(uint32_t sector){ + return 0x800; //2048 +} diff --git a/source/target/mesheven/DBG_STM32F405/stm32f405_target_flash.c b/source/target/mesheven/DBG_STM32F405/stm32f405_target_flash.c new file mode 100644 index 000000000..fdc6dece1 --- /dev/null +++ b/source/target/mesheven/DBG_STM32F405/stm32f405_target_flash.c @@ -0,0 +1,117 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "flash_blob.h" +#include "swd_host.h" +#include "target_reset.h" +#include +#include +#include + + +static const uint32_t STM32F405_FLM[] = { + 0xE00ABE00, 0x062D780D, 0x24084068, 0xD3000040, 0x1E644058, 0x1C49D1FA, 0x2A001E52, 0x4770D1F2, /*0x0020*/ + + 0xf1a14601, 0xf44f6200, 0xf04f3380, 0x429a30ff, 0x0b90d201, 0x4a584770, 0x429a440a, 0x2004d201, /*0x0040*/ + 0x4a564770, 0xf5b14411, 0xd2f92f60, 0xeb002005, 0x47704051, 0x49524853, 0x49536001, 0x20006001, /*0x0060*/ + 0x49504770, 0x310820f1, 0x20006008, 0x20004770, 0x4a4c4770, 0x320cb500, 0x28006810, 0xf7ffda01, /*0x0080*/ + 0x4948ffe9, 0x68083108, 0xd4fc03c0, 0xf4206810, 0xf0407040, 0x60100004, 0xf4406810, 0x60103080, /*0x00A0*/ + 0x03c06808, 0x6810d4fc, 0x0004f020, 0x20006010, 0xb530bd00, 0x46024d3b, 0x6828350c, 0xda012800, /*0x00C0*/ + 0xffc8f7ff, 0x34084c37, 0x03c86821, 0x4610d4fc, 0xffa6f7ff, 0x22026829, 0x715ef421, 0x00c0eb02, /*0x00E0*/ + 0x0001ea40, 0x7000f440, 0x68286028, 0x3080f440, 0x68206028, 0xd4fc03c0, 0xf0206828, 0x60280002, /*0x0100*/ + 0xbd302000, 0x4e27b5f0, 0x360c2400, 0x68304603, 0x2800460d, 0xf7ffda01, 0x4922ff9d, 0x68083108, /*0x0120*/ + 0xd4fc03c0, 0xf4206830, 0x60307040, 0x3701f240, 0x2c01f240, 0x6830e010, 0x000cea40, 0x68106030, /*0x0140*/ + 0x68086018, 0xd4fc03c0, 0xf8d36810, 0x4570e000, 0x1d1bd117, 0x1c641d12, 0x0f95ebb4, 0x07a8d3eb, /*0x0160*/ + 0x6830d014, 0x1401f240, 0x7040f420, 0x60304320, 0x80188810, 0x03c06808, 0x8810d4fc, 0x42888819, /*0x0180*/ + 0x6830d004, 0x603043b8, 0xbdf02001, 0x43b86830, 0x20006030, 0x0000bdf0, 0xf7ff0000, 0xf7fe0000, /*0x01A0*/ + 0x45670123, 0x40023c04, 0xcdef89ab, 0x00000000, +}; + +const program_target_t stm32f405_flash = { + 0x20000063, // Init + 0x2000006F, // UnInit + 0x20000073, // EraseChip + 0x200000B3, // EraseSector + 0x20000105, // ProgramPage + + // RSB : base adreess is address of Execution Region PrgData in map file + // to access global/static data + // RSP : Initial stack pointer + { + 0x20000001, // breakpoint location + 0x20000020+0x000001B0, // static_base + 0x20002000 // stack_pointer + }, + 0x20001000, // program_buffer + 0x20000000, // algo_start + 0x000001B0, // algo_size + STM32F405_FLM, // image + 512, // ram_to_flash_bytes_to_be_written + 16384, // flash sector size : 16KB/64KB/128KB + 512, // auto increment page size + 0x08000000 // flash base address +}; + +uint32_t stm32f405_GetSecNum (uint32_t addr) { + uint32_t rc = 0xFFFFFFFF; + if (addr >= 0x08000000 && addr < 0x08010000) + { + rc = (addr - 0x08000000)/0x4000; + } + else if (addr >= 0x08010000 && addr < 0x08020000) + { + rc = 4; + } + else if (addr >= 0x08020000 && addr < 0x08100000) + { + rc = 5 + (addr - 0x08020000)/0x20000; + } + return rc; +} + +uint32_t stm32f405_GetSecAddress (uint32_t sector) { + uint32_t rc = 0x08000000; + if (sector < 4) + { + rc += 0x4000 * sector; + } + else if (sector == 4) + { + rc += 0x10000; + } + else + { + rc += (sector-4)*0x20000; + } + return rc; +} + +uint32_t stm32f405_GetSecLength (uint32_t sector) { + uint32_t rc = 0; + if(sector < 4) + { + rc = 0x4000; //16KB + } + else if(sector == 4) + { + rc = 0x10000; //64KB + } + else + { + rc = 0x20000; //128KB + } + return rc; +} diff --git a/source/target/mesheven/DBG_nRF51822/nrf51_target_flash.c b/source/target/mesheven/DBG_nRF51822/nrf51_target_flash.c new file mode 100644 index 000000000..2a69bd185 --- /dev/null +++ b/source/target/mesheven/DBG_nRF51822/nrf51_target_flash.c @@ -0,0 +1,73 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "flash_blob.h" +#include "swd_host.h" +#include "target_reset.h" +#include +#include +#include + + +static const uint32_t nRF51822AA_FLM[] = { + 0xE00ABE00, 0x062D780D, 0x24084068, 0xD3000040, 0x1E644058, 0x1C49D1FA, 0x2A001E52, 0x4770D1F2, + + /*0x020*/ 0x47702000, 0x47702000, 0x4c26b570, 0x60602002, 0x60e02001, 0x68284d24, 0xd00207c0L, 0x60602000, + /*0x040*/ 0xf000bd70L, 0xe7f6f82cL, 0x4c1eb570, 0x60612102, 0x4288491e, 0x2001d302, 0xe0006160L, 0x4d1a60a0, + /*0x060*/ 0xf81df000L, 0x7c06828, 0x2000d0fa, 0xbd706060L, 0x4605b5f8, 0x4813088e, 0x46142101, 0x4f126041, + /*0x080*/ 0xc501cc01L, 0x7c06838, 0x1e76d006, 0x480dd1f8, 0x60412100, 0xbdf84608L, 0xf801f000L, 0x480ce7f2, + /*0x0A0*/ 0x6006840, 0xd00b0e00L, 0x6849490a, 0xd0072900L, 0x4a0a4909, 0xd00007c3L, 0x1d09600a, 0xd1f90840L, + /*0x0C0*/ 0x4770, 0x4001e500, 0x4001e400, 0x10001000, 0x40010400, 0x40010500, 0x40010600, 0x6e524635, + /*0x0E0*/ 0x0, +}; + +const program_target_t NRF51_flash = { + 0x20000021, // Init + 0x20000025, // UnInit + 0x20000029, // EraseChip + 0x20000049, // EraseSector + 0x20000071, // ProgramPage + + // RSB : base adreess is address of Execution Region PrgData in map file + // to access global/static data + // RSP : Initial stack pointer + { + 0x20000001, // breakpoint location + 0x20000020+0x00000150, // static_base + 0x20001000 // stack_pointer + }, + 0x20000200, // program_buffer + 0x20000000, // algo_start + 0x00000150, // algo_size + nRF51822AA_FLM, // image + 512, // ram_to_flash_bytes_to_be_written + 1024, // flash sector size + 1024, // auto increment page size + 0x00000000 // flash base address +}; + +uint32_t nrf51_GetSecNum (uint32_t addr){ + return (addr >> 10); +} + +uint32_t nrf51_GetSecAddress(uint32_t sector){ + return (sector << 10); +} + +uint32_t nrf51_GetSecLength(uint32_t sector){ + return 0x400; //1024 +} + diff --git a/source/target/mesheven/target.c b/source/target/mesheven/target.c new file mode 100644 index 000000000..bb0679fc8 --- /dev/null +++ b/source/target/mesheven/target.c @@ -0,0 +1,122 @@ +/** + * @file target.c + * @brief Target information for the multi-targets + * + * DAPLink Interface Firmware + * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "target_config.h" +#include "target_ids.h" + +// The file flash_blob.c must only be included in target.c +//#include "flash_blob.c" +extern const program_target_t NRF51_flash; +extern const program_target_t stm32f051_flash; +extern const program_target_t stm32f071_flash; +extern const program_target_t stm32f103_flash; +extern const program_target_t stm32f405_flash; + +uint32_t nrf51_GetSecNum (uint32_t addr); +uint32_t nrf51_GetSecAddress(uint32_t sector); +uint32_t nrf51_GetSecLength(uint32_t sector); + +uint32_t stm32f051_GetSecNum (uint32_t addr); +uint32_t stm32f051_GetSecAddress(uint32_t sector); +uint32_t stm32f051_GetSecLength(uint32_t sector); + +uint32_t stm32f071_GetSecNum (uint32_t addr); +uint32_t stm32f071_GetSecAddress(uint32_t sector); +uint32_t stm32f071_GetSecLength(uint32_t sector); + +uint32_t stm32f103_GetSecNum (uint32_t addr); +uint32_t stm32f103_GetSecAddress(uint32_t sector); +uint32_t stm32f103_GetSecLength(uint32_t sector); + +uint32_t stm32f405_GetSecNum (uint32_t addr); +uint32_t stm32f405_GetSecAddress (uint32_t sector); +uint32_t stm32f405_GetSecLength (uint32_t sector); + +// target information +const target_cfg_t target_device[] = +{ + //nrf51822 + { + .sector_size = 1024, + .sector_cnt = 256, + .flash_start = 0x00000000, + .flash_end = 0x10001100, + .ram_start = 0x20000000, + .ram_end = 0x20008000, + .flash_algo = (program_target_t *) &NRF51_flash, + .get_sector_number = nrf51_GetSecNum, + .get_sector_address = nrf51_GetSecAddress, + .get_sector_length = nrf51_GetSecLength, + }, + //stm32f051kX + { + .sector_size = 1024, + .sector_cnt = 64, + .flash_start = 0x08000000, + .flash_end = 0x08010000, + .ram_start = 0x20000000, + .ram_end = 0x20002000, + .flash_algo = (program_target_t *) &stm32f051_flash, + .get_sector_number = stm32f051_GetSecNum, + .get_sector_address = stm32f051_GetSecAddress, + .get_sector_length = stm32f051_GetSecLength, + }, + //stm32f103rc + { + .sector_size = 2048, + .sector_cnt = 128, + .flash_start = 0x08000000, + .flash_end = 0x08040000, + .ram_start = 0x20000000, + .ram_end = 0x2000C000, + .flash_algo = (program_target_t *) &stm32f103_flash, + .get_sector_number = stm32f103_GetSecNum, + .get_sector_address = stm32f103_GetSecAddress, + .get_sector_length = stm32f103_GetSecLength, + }, + //stm32f405 + { + .sector_size = 16384, + .sector_cnt = 64, + .flash_start = 0x08000000, + .flash_end = 0x08100000, + .ram_start = 0x20000000, + .ram_end = 0x20020000, + .flash_algo = (program_target_t *) &stm32f405_flash, + .get_sector_number = stm32f405_GetSecNum, + .get_sector_address = stm32f405_GetSecAddress, + .get_sector_length = stm32f405_GetSecLength, + }, + //stm32f071 + { + .sector_size = 2048, + .sector_cnt = 64, + .flash_start = 0x08000000, + .flash_end = 0x08020000, + .ram_start = 0x20000000, + .ram_end = 0x20004000, + .flash_algo = (program_target_t *) &stm32f071_flash, + .get_sector_number = stm32f071_GetSecNum, + .get_sector_address = stm32f071_GetSecAddress, + .get_sector_length = stm32f071_GetSecLength, + } + +}; diff --git a/source/target/mesheven/target_ids.h b/source/target/mesheven/target_ids.h new file mode 100644 index 000000000..6f0f23705 --- /dev/null +++ b/source/target/mesheven/target_ids.h @@ -0,0 +1,31 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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. + */ +#ifndef TARGET_IDS_H +#define TARGET_IDS_H + +enum Target_IDs +{ + Target_NRF51822 = 0, + Target_STM32F051 = 1, + Target_STM32F103 = 2, + Target_STM32F405 = 3, + Target_STM32F071 = 4, + + Target_UNKNOWN = 0xFF +}; + + +#endif //TARGET_IDS_H diff --git a/source/target/mesheven/target_reset.c b/source/target/mesheven/target_reset.c new file mode 100644 index 000000000..a3ee9c4b1 --- /dev/null +++ b/source/target/mesheven/target_reset.c @@ -0,0 +1,91 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "target_reset.h" +#include "swd_host.h" +#include "target_ids.h" + +uint8_t targetID = Target_UNKNOWN; + +typedef struct{ + void (*init)(void); + uint8_t (*unlock)(void); + uint8_t (*securitybitset)(uint32_t addr, uint8_t *data, uint32_t size); + uint8_t (*setstate)(TARGET_RESET_STATE); +}Target_Reset; + + +void common_target_before_init_debug(void) +{ + return; +} + +uint8_t common_target_unlock_sequence(void) +{ + return 1; +} + +uint8_t common_security_bits_set(uint32_t addr, uint8_t *data, uint32_t size) +{ + return 0; +} + +uint8_t common_target_set_state(TARGET_RESET_STATE state) +{ + return swd_set_target_state_hw(state); +} + + +static const Target_Reset targets[] = { + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, +}; + + +void target_before_init_debug(void) { + if (targetID != Target_UNKNOWN) { + targets[targetID].init(); + } else { + common_target_before_init_debug(); + } +} + +uint8_t target_unlock_sequence(void) { + if (targetID != Target_UNKNOWN) { + return targets[targetID].unlock(); + } else { + return common_target_unlock_sequence(); + } +} + +uint8_t security_bits_set(uint32_t addr, uint8_t *data, uint32_t size) +{ + if (targetID != Target_UNKNOWN) { + return targets[targetID].securitybitset(addr, data, size); + } else { + return common_security_bits_set(addr, data, size); + } +} + +uint8_t target_set_state(TARGET_RESET_STATE state) { + if (targetID != Target_UNKNOWN) { + return targets[targetID].setstate(state); + } else { + return common_target_set_state(state); + } +} From 8245416d23a9a4ac658798a8e7ff24d53ee649f7 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 08:27:54 +0800 Subject: [PATCH 02/18] add ERROR_TARGET_UNKNOWN error. --- source/daplink/error.c | 3 ++- source/daplink/error.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/daplink/error.c b/source/daplink/error.c index db800374e..b9e66a58a 100644 --- a/source/daplink/error.c +++ b/source/daplink/error.c @@ -115,7 +115,8 @@ static const char *const error_message[] = { "", // ERROR_BL_UPDT_BAD_CRC "The bootloader CRC did not pass.", - + // ERROR_TARGET_UNKNOWN + "unsupported target device." }; COMPILER_ASSERT(ERROR_COUNT == ELEMENTS_IN_ARRAY(error_message)); diff --git a/source/daplink/error.h b/source/daplink/error.h index 7e5b7f6a5..6a587980d 100644 --- a/source/daplink/error.h +++ b/source/daplink/error.h @@ -77,7 +77,7 @@ typedef enum { ERROR_BL_UPDT_BAD_CRC, // Add new values here - + ERROR_TARGET_UNKNOWN, ERROR_COUNT } error_t; From 3822d090b22be13afeb4e0c93b0e3c581d5c1cfb Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 08:39:24 +0800 Subject: [PATCH 03/18] change IO pins configuration for MB2100. --- source/hic_hal/nxp/lpc11u35/DAP_config.h | 9 ++-- source/hic_hal/nxp/lpc11u35/IO_Config.h | 61 ++++++++++++++++-------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/source/hic_hal/nxp/lpc11u35/DAP_config.h b/source/hic_hal/nxp/lpc11u35/DAP_config.h index 33ee962a1..f44585eb3 100644 --- a/source/hic_hal/nxp/lpc11u35/DAP_config.h +++ b/source/hic_hal/nxp/lpc11u35/DAP_config.h @@ -166,7 +166,7 @@ Configures the DAP Hardware I/O pins for Serial Wire Debug (SWD) mode: */ static __inline void PORT_SWD_SETUP(void) { - LPC_GPIO->SET[PIN_SWCLK_PORT] = PIN_SWCLK; + LPC_GPIO->CLR[PIN_SWCLK_PORT] = PIN_SWCLK; LPC_GPIO->SET[PIN_SWDIO_PORT] = PIN_SWDIO; // open drain logic LPC_GPIO->DIR[PIN_nRESET_PORT] &= ~PIN_nRESET; @@ -181,9 +181,12 @@ Disables the DAP Hardware I/O pins which configures: */ static __inline void PORT_OFF(void) { + LPC_GPIO->CLR[PIN_SWCLK_PORT] = PIN_SWCLK; + LPC_GPIO->SET[PIN_SWDIO_PORT] = PIN_SWDIO; LPC_GPIO->DIR[PIN_nRESET_PORT] &= ~PIN_nRESET; - LPC_GPIO->DIR[PIN_SWCLK_PORT] &= ~PIN_SWCLK; - LPC_GPIO->DIR[PIN_SWDIO_PORT] &= ~PIN_SWDIO; + LPC_GPIO->CLR[PIN_nRESET_PORT] = PIN_nRESET; + LPC_GPIO->DIR[PIN_SWCLK_PORT] |= PIN_SWCLK; + LPC_GPIO->DIR[PIN_SWDIO_PORT] |= PIN_SWDIO; } diff --git a/source/hic_hal/nxp/lpc11u35/IO_Config.h b/source/hic_hal/nxp/lpc11u35/IO_Config.h index 3acc7d6c6..4a2d142d7 100644 --- a/source/hic_hal/nxp/lpc11u35/IO_Config.h +++ b/source/hic_hal/nxp/lpc11u35/IO_Config.h @@ -45,25 +45,18 @@ COMPILER_ASSERT(DAPLINK_HIC_ID == DAPLINK_HIC_ID_LPC11U35); #define PULL_UP_ENABLED (2 << 3) #define OPENDRAIN (1 << 10) -// DAP LED PIO0_21 +// DAP LED PIO0_13 #define PIN_DAP_LED_PORT 0 -#define PIN_DAP_LED_BIT 21 +#define PIN_DAP_LED_BIT 13 #define PIN_DAP_LED (1 << PIN_DAP_LED_BIT) -#define PIN_DAP_LED_IOCON LPC_IOCON->PIO0_21 +#define PIN_DAP_LED_IOCON LPC_IOCON->TDO_PIO0_13 #define PIN_DAP_LED_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) -// MSD LED PIO0_20 -#define PIN_MSD_LED_PORT 0 -#define PIN_MSD_LED_BIT 20 -#define PIN_MSD_LED (1 << PIN_MSD_LED_BIT) -#define PIN_MSD_LED_IOCON LPC_IOCON->PIO0_20 -#define PIN_MSD_LED_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) - -// CDC LED PIO0_11 +// CDC LED PIO0_12 #define PIN_CDC_LED_PORT 0 -#define PIN_CDC_LED_BIT 11 +#define PIN_CDC_LED_BIT 12 #define PIN_CDC_LED (1 << PIN_CDC_LED_BIT) -#define PIN_CDC_LED_IOCON LPC_IOCON->TDI_PIO0_11 +#define PIN_CDC_LED_IOCON LPC_IOCON->TMS_PIO0_12 #define PIN_CDC_LED_IOCON_INIT (FUNC_1 | PULL_UP_ENABLED) // Non-Forwarded Reset in PIN PIO0_1 @@ -80,19 +73,19 @@ COMPILER_ASSERT(DAPLINK_HIC_ID == DAPLINK_HIC_ID_LPC11U35); #define PIN_RESET_IN_FWRD_IOCON LPC_IOCON->PIO1_19 #define PIN_RESET_IN_FWRD_IOCON_INIT (FUNC_0 | OPENDRAIN | PULL_UP_ENABLED) -// nRESET OUT Pin PIO0_2 +// nRESET OUT Pin PIO0_23 #define PIN_nRESET_PORT 0 -#define PIN_nRESET_BIT 2 +#define PIN_nRESET_BIT 23 #define PIN_nRESET (1 << PIN_nRESET_BIT) -#define PIN_nRESET_IOCON LPC_IOCON->PIO0_2 +#define PIN_nRESET_IOCON LPC_IOCON->PIO0_23 #define PIN_nRESET_IOCON_INIT (FUNC_0 | OPENDRAIN | PULL_UP_ENABLED) -// SWCLK/TCK Pin PIO0_7 +// SWCLK/TCK Pin PIO0_9 #define PIN_SWCLK_PORT 0 -#define PIN_SWCLK_BIT 7 +#define PIN_SWCLK_BIT 9 #define PIN_SWCLK (1 << PIN_SWCLK_BIT) -#define PIN_SWCLK_TCK_IOCON LPC_IOCON->PIO0_7 -#define PIN_SWCLK_TCK_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) +#define PIN_SWCLK_TCK_IOCON LPC_IOCON->PIO0_9 +#define PIN_SWCLK_TCK_IOCON_INIT (FUNC_0 | PULL_DOWN_ENABLED) // SWDIO/TMS In/Out Pin PIO0_8 #define PIN_SWDIO_PORT 0 @@ -115,4 +108,32 @@ COMPILER_ASSERT(DAPLINK_HIC_ID == DAPLINK_HIC_ID_LPC11U35); #define PIN_TDO_IOCON LPC_IOCON->PIO0_9 #define PIN_TDO_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) +// CFG0 pin PIO0_20 +#define PIN_CFG0_PORT 0 +#define PIN_CFG0_BIT 20 +#define PIN_CFG0 (1 << PIN_CFG0_BIT) +#define PIN_CFG0_IOCON LPC_IOCON->PIO0_20 +#define PIN_CFG0_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) + +// CFG1 pin PIO0_16 +#define PIN_CFG1_PORT 0 +#define PIN_CFG1_BIT 16 +#define PIN_CFG1 (1 << PIN_CFG1_BIT) +#define PIN_CFG1_IOCON LPC_IOCON->PIO0_16 +#define PIN_CFG1_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) + +// CFG2 pin PIO0_11 +#define PIN_CFG2_PORT 0 +#define PIN_CFG2_BIT 11 +#define PIN_CFG2 (1 << PIN_CFG2_BIT) +#define PIN_CFG2_IOCON LPC_IOCON->TDI_PIO0_11 +#define PIN_CFG2_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) + +// CFG3 pin PIO0_14 +#define PIN_CFG3_PORT 0 +#define PIN_CFG3_BIT 14 +#define PIN_CFG3 (1 << PIN_CFG3_BIT) +#define PIN_CFG3_IOCON LPC_IOCON->TRST_PIO0_14 +#define PIN_CFG3_IOCON_INIT (FUNC_0 | PULL_UP_ENABLED) + #endif From 8fd4bc1a3a23631443b07a0ed26e93dc61637cc4 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 08:41:25 +0800 Subject: [PATCH 04/18] change MBED to MESH-DBG --- source/daplink/drag-n-drop/virtual_fs.c | 2 +- source/daplink/interface/daplink.c | 2 +- source/hic_hal/nxp/lpc11u35/usb_config.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/daplink/drag-n-drop/virtual_fs.c b/source/daplink/drag-n-drop/virtual_fs.c index 78719db52..5c9204ba6 100644 --- a/source/daplink/drag-n-drop/virtual_fs.c +++ b/source/daplink/drag-n-drop/virtual_fs.c @@ -141,7 +141,7 @@ static const mbr_t mbr_tmpl = { /*uint8_t */.boot_record_signature = 0x29, // signature is present /*uint32_t*/.volume_id = 0x27021974, // serial number // needs to match the root dir label - /*char[11]*/.volume_label = {'D', 'A', 'P', 'L', 'I', 'N', 'K', '-', 'D', 'N', 'D'}, + /*char[11]*/.volume_label = {'M', 'E', 'S', 'H', '-', 'D', 'B', 'G', ' ', ' ', ' '}, // unused by msft - just a label (FAT, FAT12, FAT16) /*char[8] */.file_system_type = {'F', 'A', 'T', '1', '2', ' ', ' ', ' '}, diff --git a/source/daplink/interface/daplink.c b/source/daplink/interface/daplink.c index 24e9c2074..e4bfddaff 100644 --- a/source/daplink/interface/daplink.c +++ b/source/daplink/interface/daplink.c @@ -32,7 +32,7 @@ const vfs_filename_t daplink_mode_file_name = "START_BLACT"; __attribute__((weak)) const vfs_filename_t daplink_url_name = "MBED HTM"; __attribute__((weak)) -const vfs_filename_t daplink_drive_name = "DAPLINK "; +const vfs_filename_t daplink_drive_name = "MESH-DBG "; __attribute__((weak)) const char *const daplink_target_url = "https://mbed.org/device/?code=@U?version=@V?target_id=@T"; diff --git a/source/hic_hal/nxp/lpc11u35/usb_config.c b/source/hic_hal/nxp/lpc11u35/usb_config.c index ae5cd632e..e4aca3f25 100644 --- a/source/hic_hal/nxp/lpc11u35/usb_config.c +++ b/source/hic_hal/nxp/lpc11u35/usb_config.c @@ -192,7 +192,7 @@ #define USBD_MSC_STRDESC L"USB_MSC" // Make sure changes to USBD_MSC_INQUIRY_DATA are coordinated with mbed-ls // since this is used to detect DAPLink drives -#define USBD_MSC_INQUIRY_DATA "MBED " \ +#define USBD_MSC_INQUIRY_DATA "MESH-DBG" \ "VFS " \ "0.1" From 5b435ee61b51ed4f9321e60cd9757aede57f28aa Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 08:56:11 +0800 Subject: [PATCH 05/18] add 4 config PINs. remove MSD led pin. remove MSD led function. drag&drop using DAP led. add disable CDC led by config. --- source/daplink/drag-n-drop/vfs_manager.c | 10 ++-- source/daplink/interface/main.c | 37 +++--------- source/daplink/interface/main.h | 1 - source/daplink/usb2uart/usbd_user_cdc_acm.c | 28 ++------- source/hic_hal/gpio.h | 17 +++++- source/hic_hal/nxp/lpc11u35/gpio.c | 65 ++++++++++++++++++--- 6 files changed, 91 insertions(+), 67 deletions(-) diff --git a/source/daplink/drag-n-drop/vfs_manager.c b/source/daplink/drag-n-drop/vfs_manager.c index 8d385fa28..04d44e943 100644 --- a/source/daplink/drag-n-drop/vfs_manager.c +++ b/source/daplink/drag-n-drop/vfs_manager.c @@ -39,6 +39,8 @@ #include "file_stream.h" #include "error.h" +#include "target_config.h" + // Set to 1 to enable debugging #define DEBUG_VFS_MANAGER 0 @@ -307,8 +309,6 @@ void usbd_msc_read_sect(uint32_t sector, uint8_t *buf, uint32_t num_of_sectors) return; } - // indicate msc activity - main_blink_msc_led(MAIN_LED_OFF); vfs_read(sector, buf, num_of_sectors); } @@ -329,8 +329,8 @@ void usbd_msc_write_sect(uint32_t sector, uint8_t *buf, uint32_t num_of_sectors) return; } - // indicate msc activity - main_blink_msc_led(MAIN_LED_OFF); + // indicate DAP activity + main_blink_hid_led(MAIN_LED_OFF); vfs_write(sector, buf, num_of_sectors); file_data_handler(sector, buf, num_of_sectors); } @@ -421,7 +421,7 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_ // this is the key for starting a file write - we dont care what file types are sent // just look for something unique (NVIC table, hex, srec, etc) until root dir is updated - if (!file_transfer_state.stream_started) { + if (!file_transfer_state.stream_started) { // look for file types we can program stream = stream_start_identify((uint8_t *)buf, VFS_SECTOR_SIZE * num_of_sectors); diff --git a/source/daplink/interface/main.c b/source/daplink/interface/main.c index af173cfd5..1397a4d49 100644 --- a/source/daplink/interface/main.c +++ b/source/daplink/interface/main.c @@ -69,10 +69,8 @@ OS_TID serial_task_id; // USB busy LED state; when TRUE the LED will flash once using 30mS clock tick static uint8_t hid_led_usb_activity = 0; static uint8_t cdc_led_usb_activity = 0; -static uint8_t msc_led_usb_activity = 0; static main_led_state_t hid_led_state = MAIN_LED_FLASH; static main_led_state_t cdc_led_state = MAIN_LED_FLASH; -static main_led_state_t msc_led_state = MAIN_LED_FLASH; // Global state of usb main_usb_connect_t usb_state; @@ -128,21 +126,21 @@ void main_blink_hid_led(main_led_state_t permanent) } // Flash CDC LED using 30mS tick +extern uint8_t stopCDCLed; void main_blink_cdc_led(main_led_state_t permanent) { + if(stopCDCLed == 1) + { + cdc_led_usb_activity = 0; + cdc_led_state = MAIN_LED_OFF; + return; + } + cdc_led_usb_activity = 1; cdc_led_state = (permanent) ? MAIN_LED_FLASH_PERMANENT : MAIN_LED_FLASH; return; } -// Flash MSC LED using 30mS tick -void main_blink_msc_led(main_led_state_t permanent) -{ - msc_led_usb_activity = 1; - msc_led_state = (permanent) ? MAIN_LED_FLASH_PERMANENT : MAIN_LED_FLASH; - return; -} - // Power down the interface void main_powerdown_event(void) { @@ -251,7 +249,6 @@ __task void main_task(void) // LED gpio_led_state_t hid_led_value = GPIO_LED_ON; gpio_led_state_t cdc_led_value = GPIO_LED_ON; - gpio_led_state_t msc_led_value = GPIO_LED_ON; // USB uint32_t usb_state_count = USB_BUSY_TIME; // thread running after usb connected started @@ -269,7 +266,6 @@ __task void main_task(void) // Turn off LED gpio_set_hid_led(GPIO_LED_ON); gpio_set_cdc_led(GPIO_LED_ON); - gpio_set_msc_led(GPIO_LED_ON); // Initialize the DAP DAP_Setup(); // do some init with the target before USB and files are configured @@ -312,7 +308,6 @@ __task void main_task(void) // Turn off LED gpio_set_hid_led(GPIO_LED_OFF); gpio_set_cdc_led(GPIO_LED_OFF); - gpio_set_msc_led(GPIO_LED_OFF); // TODO: put the interface chip in sleep mode while (1); @@ -408,22 +403,6 @@ __task void main_task(void) gpio_set_hid_led(hid_led_value); } - if (msc_led_usb_activity && ((msc_led_state == MAIN_LED_FLASH) || (msc_led_state == MAIN_LED_FLASH_PERMANENT))) { - // Flash MSD LED ONCE - if (msc_led_value) { - msc_led_value = GPIO_LED_OFF; - } else { - msc_led_value = GPIO_LED_ON; // Turn on - - if (msc_led_state == MAIN_LED_FLASH) { - msc_led_usb_activity = 0; - } - } - - // Update hardware - gpio_set_msc_led(msc_led_value); - } - if (cdc_led_usb_activity && ((cdc_led_state == MAIN_LED_FLASH) || (cdc_led_state == MAIN_LED_FLASH_PERMANENT))) { // Flash CDC LED ONCE if (cdc_led_value) { diff --git a/source/daplink/interface/main.h b/source/daplink/interface/main.h index 36e7e545d..d9fa43d04 100644 --- a/source/daplink/interface/main.h +++ b/source/daplink/interface/main.h @@ -59,7 +59,6 @@ void main_msc_disconnect_event(void); void main_msc_delay_disconnect_event(void); void main_force_msc_disconnect_event(void); void main_blink_hid_led(main_led_state_t permanent); -void main_blink_msc_led(main_led_state_t permanent); void main_blink_cdc_led(main_led_state_t permanent); #ifdef __cplusplus diff --git a/source/daplink/usb2uart/usbd_user_cdc_acm.c b/source/daplink/usb2uart/usbd_user_cdc_acm.c index 04683c222..1336e1758 100644 --- a/source/daplink/usb2uart/usbd_user_cdc_acm.c +++ b/source/daplink/usb2uart/usbd_user_cdc_acm.c @@ -108,28 +108,6 @@ int32_t USBD_CDC_ACM_PortGetLineCoding(CDC_LINE_CODING *line_coding) return (0); } -static U32 start_break_time = 0; -int32_t USBD_CDC_ACM_SendBreak(uint16_t dur) -{ - uint32_t end_break_time; - - // reset and send the unique id over CDC - if (dur != 0) { - start_break_time = os_time_get(); - target_set_state(RESET_HOLD); - } else { - end_break_time = os_time_get(); - - // long reset -> send uID over serial (300 -> break > 3s) - if ((end_break_time - start_break_time) >= (300)) { - main_reset_target(1); - } else { - main_reset_target(0); - } - } - - return (1); -} /** @brief Virtual COM Port set control line state * @@ -141,7 +119,13 @@ int32_t USBD_CDC_ACM_SendBreak(uint16_t dur) * @return 0 Function failed. * @return 1 Function succeeded. */ +uint8_t stopCDCLed = 0; int32_t USBD_CDC_ACM_PortSetControlLineState(uint16_t ctrl_bmp) { + if (ctrl_bmp & 0x01) { + stopCDCLed = 0; + } else { + stopCDCLed = 1; + } return (1); } diff --git a/source/hic_hal/gpio.h b/source/hic_hal/gpio.h index 9fd9001d4..c86994a22 100644 --- a/source/hic_hal/gpio.h +++ b/source/hic_hal/gpio.h @@ -27,6 +27,20 @@ #ifdef __cplusplus extern "C" { #endif + +enum +{ + PIN_CONFIG_DT01 = 0, + PIN_CONFIG_1 = 1, + PIN_CONFIG_2 = 2, + PIN_CONFIG_3 = 3, +}; + +enum +{ + PIN_LOW = 0, + PIN_HIGH = 1, +}; // wiring on PCB is unknown so implementations may vary typedef enum led_state { @@ -37,9 +51,10 @@ typedef enum led_state { void gpio_init(void); void gpio_set_hid_led(gpio_led_state_t state); void gpio_set_cdc_led(gpio_led_state_t state); -void gpio_set_msc_led(gpio_led_state_t state); uint8_t gpio_get_sw_reset(void); +uint8_t gpio_get_config(uint8_t cfgid); + #ifdef __cplusplus } #endif diff --git a/source/hic_hal/nxp/lpc11u35/gpio.c b/source/hic_hal/nxp/lpc11u35/gpio.c index c8b811c88..ae5323ee0 100644 --- a/source/hic_hal/nxp/lpc11u35/gpio.c +++ b/source/hic_hal/nxp/lpc11u35/gpio.c @@ -81,15 +81,26 @@ void gpio_init(void) { // enable clock for GPIO port 0 LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 6); + + //config the CFG 4 pins as input + PIN_CFG0_IOCON = PIN_CFG0_IOCON_INIT; + LPC_GPIO->CLR[PIN_CFG0_PORT] = PIN_CFG0; + LPC_GPIO->DIR[PIN_CFG0_PORT] &= ~PIN_CFG0; + PIN_CFG1_IOCON = PIN_CFG1_IOCON_INIT; + LPC_GPIO->CLR[PIN_CFG1_PORT] = PIN_CFG1; + LPC_GPIO->DIR[PIN_CFG1_PORT] &= ~PIN_CFG1; + PIN_CFG2_IOCON = PIN_CFG2_IOCON_INIT; + LPC_GPIO->CLR[PIN_CFG2_PORT] = PIN_CFG2; + LPC_GPIO->DIR[PIN_CFG2_PORT] &= ~PIN_CFG2; + PIN_CFG3_IOCON = PIN_CFG3_IOCON_INIT; + LPC_GPIO->CLR[PIN_CFG3_PORT] = PIN_CFG3; + LPC_GPIO->DIR[PIN_CFG3_PORT] &= ~PIN_CFG3; + // configure GPIO-LED as output // DAP led (green) PIN_DAP_LED_IOCON = PIN_DAP_LED_IOCON_INIT; LPC_GPIO->SET[PIN_DAP_LED_PORT] = PIN_DAP_LED; LPC_GPIO->DIR[PIN_DAP_LED_PORT] |= PIN_DAP_LED; - // MSD led (red) - PIN_MSD_LED_IOCON = PIN_MSD_LED_IOCON_INIT; - LPC_GPIO->SET[PIN_MSD_LED_PORT] = PIN_MSD_LED; - LPC_GPIO->DIR[PIN_MSD_LED_PORT] |= PIN_MSD_LED; // Serial LED (blue) PIN_CDC_LED_IOCON = PIN_CDC_LED_IOCON_INIT; LPC_GPIO->SET[PIN_CDC_LED_PORT] = PIN_CDC_LED; @@ -141,13 +152,49 @@ void gpio_set_cdc_led(gpio_led_state_t state) } } -void gpio_set_msc_led(gpio_led_state_t state) +static uint32_t gpio_get_config0(void) { - if (state) { - LPC_GPIO->SET[PIN_MSD_LED_PORT] = PIN_MSD_LED; - } else { - LPC_GPIO->CLR[PIN_MSD_LED_PORT] = PIN_MSD_LED; + return ((LPC_GPIO->PIN[PIN_CFG0_PORT] & PIN_CFG0) != 0x00)? PIN_HIGH:PIN_LOW; +} + +static uint32_t gpio_get_config1(void) +{ + return ((LPC_GPIO->PIN[PIN_CFG1_PORT] & PIN_CFG1) != 0x00)? PIN_HIGH:PIN_LOW; +} + +static uint32_t gpio_get_config2(void) +{ + return ((LPC_GPIO->PIN[PIN_CFG2_PORT] & PIN_CFG2) != 0x00)? PIN_HIGH:PIN_LOW; +} + +static uint32_t gpio_get_config3(void) +{ + return ((LPC_GPIO->PIN[PIN_CFG3_PORT] & PIN_CFG3) != 0x00)? PIN_HIGH:PIN_LOW; +} + +uint8_t gpio_get_config(uint8_t cfgid) +{ + uint8_t rc = 0; + switch (cfgid) + { + case PIN_CONFIG_DT01: + rc = gpio_get_config0(); + break; + + case PIN_CONFIG_1: + rc = gpio_get_config1(); + break; + + case PIN_CONFIG_2: + rc = gpio_get_config2(); + break; + + case PIN_CONFIG_3: + rc = gpio_get_config3(); + break; } + + return rc; } uint8_t gpio_get_sw_reset(void) From 8e2795d7b00e59f0d1c6822b54993a12190b5529 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 08:58:34 +0800 Subject: [PATCH 06/18] merge UART setting from CMSIS-DAP project setting. use RTS/CTS when configured. --- source/hic_hal/nxp/lpc11u35/uart.c | 85 ++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/source/hic_hal/nxp/lpc11u35/uart.c b/source/hic_hal/nxp/lpc11u35/uart.c index dd786dec0..e87b9dcd1 100644 --- a/source/hic_hal/nxp/lpc11u35/uart.c +++ b/source/hic_hal/nxp/lpc11u35/uart.c @@ -22,6 +22,7 @@ #include "LPC11Uxx.h" #include "uart.h" #include "util.h" +#include "gpio.h" static uint32_t baudrate; static uint32_t dll; @@ -36,8 +37,8 @@ static struct { uint8_t data[BUFFER_SIZE]; volatile uint16_t idx_in; volatile uint16_t idx_out; - volatile int16_t cnt_in; - volatile int16_t cnt_out; + volatile int32_t cnt_in; + volatile int32_t cnt_out; } write_buffer, read_buffer; int32_t uart_initialize(void) @@ -52,14 +53,25 @@ int32_t uart_initialize(void) // alternate function USART and PullNone LPC_IOCON->PIO0_18 |= 0x01; LPC_IOCON->PIO0_19 |= 0x01; + // alternate function USART RTS/CTS and PullUp + if(gpio_get_config(PIN_CONFIG_DT01) == PIN_HIGH) + { + LPC_IOCON->PIO0_7 = 0x11; // CTS + LPC_IOCON->PIO0_17 = 0x11; // RTS + } // enable FIFOs (trigger level 1) and clear them LPC_USART->FCR = 0x87; // Transmit Enable LPC_USART->TER = 0x80; + // Set RTS/CTS + if(gpio_get_config(PIN_CONFIG_DT01) == PIN_HIGH) + { + LPC_USART->MCR = (1 << 7) | (1 << 6); // Enable RTS and CTS flow control + } // reset uart uart_reset(); - // enable rx and tx interrupt - LPC_USART->IER |= (1 << 0) | (1 << 1); + // enable rx and rx error interrupt + LPC_USART->IER = (1 << 0) | (1 << 2); NVIC_EnableIRQ(UART_IRQn); return 1; } @@ -80,8 +92,9 @@ int32_t uart_reset(void) int32_t i; // disable interrupt NVIC_DisableIRQ(UART_IRQn); - // Reset FIFOs - LPC_USART->FCR = 0x06; + // enable FIFOs (trigger level 1) and clear them + LPC_USART->FCR = 0x87; + baudrate = 0; dll = 0; tx_in_progress = 0; @@ -118,6 +131,12 @@ int32_t uart_set_configuration(UART_Configuration *config) NVIC_DisableIRQ(UART_IRQn); // reset uart uart_reset(); + //clear RTS + if(gpio_get_config(PIN_CONFIG_DT01) == PIN_HIGH) + { + LPC_USART->MCR = (1 << 1); + } + baudrate = config->Baudrate; // Compute baud rate dividers mv = 15; @@ -169,21 +188,15 @@ int32_t uart_set_configuration(UART_Configuration *config) break; } - if (config->FlowControl == UART_FLOW_CONTROL_RTS_CTS) { - LPC_IOCON->PIO0_17 |= 0x01; // RTS - LPC_IOCON->PIO0_7 |= 0x01; // CTS - // enable auto RTS and CTS - LPC_USART->MCR = (1 << 6) | (1 << 7); - } else { - LPC_IOCON->PIO0_17 &= ~0x01; // RTS - LPC_IOCON->PIO0_7 &= ~0x01; // CTS - // disable auto RTS and CTS - LPC_USART->MCR = (0 << 6) | (0 << 7); - } LPC_USART->LCR = (data_bits << 0) | (stop_bits << 2) | (parity << 3); + // Set RTS/CTS + if(gpio_get_config(PIN_CONFIG_DT01) == PIN_HIGH) + { + LPC_USART->MCR = (1 << 7) | (1 << 6); // Enable RTS and CTS flow control + } // Enable UART interrupt NVIC_EnableIRQ(UART_IRQn); return 1; @@ -283,7 +296,7 @@ int32_t uart_write_free(void) int32_t uart_write_data(uint8_t *data, uint16_t size) { uint32_t cnt; - int16_t len_in_buf; + int32_t len_in_buf; if (size == 0) { return 0; @@ -333,6 +346,9 @@ int32_t uart_read_data(uint8_t *data, uint16_t size) } } + //enable RX interrupt + LPC_USART->IER |= (1 << 0); + return cnt; } @@ -340,7 +356,7 @@ int32_t uart_read_data(uint8_t *data, uint16_t size) void UART_IRQHandler(void) { uint32_t iir; - int16_t len_in_buf; + int32_t len_in_buf; // read interrupt status iir = LPC_USART->IIR; @@ -360,20 +376,31 @@ void UART_IRQHandler(void) LPC_USART->IER &= ~(1 << 1); } + //handle RLS interrupt + if((iir & 0x0E) == 0x06){ //RLS + //clear the FIFO. + while (LPC_USART->LSR & 0x01) { + LPC_USART->RBR; + } + LPC_USART->LSR; + return; + } + // handle received character if (((iir & 0x0E) == 0x04) || // Rx interrupt (RDA) - ((iir & 0x0E) == 0x0C)) { // Rx interrupt (CTI) + ((iir & 0x0E) == 0x0C)) { // Rx interrupt (CTI) while (LPC_USART->LSR & 0x01) { len_in_buf = read_buffer.cnt_in - read_buffer.cnt_out; - read_buffer.data[read_buffer.idx_in++] = LPC_USART->RBR; - read_buffer.idx_in &= (BUFFER_SIZE - 1); - read_buffer.cnt_in++; - - // if buffer full: write by dropping oldest characters - if (len_in_buf == BUFFER_SIZE) { - read_buffer.idx_out++; - read_buffer.idx_out &= (BUFFER_SIZE - 1); - read_buffer.cnt_out++; + if (len_in_buf == BUFFER_SIZE) { + //buffer full. keep data in FIFO, assert RTS=HIGH. + //disable the RX interrupt + LPC_USART->IER &= ~(0x01 << 0); + break; + } + else{ + read_buffer.data[read_buffer.idx_in++] = LPC_USART->RBR; + read_buffer.idx_in &= (BUFFER_SIZE - 1); + read_buffer.cnt_in++; } } } From 8aa5bb57347d4d4c31d8e3b1a3b4f7ab6a3493e8 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 09:01:14 +0800 Subject: [PATCH 07/18] add target detect function --- source/daplink/drag-n-drop/file_stream.c | 5 ++ source/daplink/interface/swd_host.c | 107 ++++++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/source/daplink/drag-n-drop/file_stream.c b/source/daplink/drag-n-drop/file_stream.c index 03eb7e459..27f9ac8af 100644 --- a/source/daplink/drag-n-drop/file_stream.c +++ b/source/daplink/drag-n-drop/file_stream.c @@ -31,6 +31,8 @@ #include "RTL.h" #include "compiler.h" +#include "target_config.h" + typedef enum { STREAM_STATE_CLOSED, STREAM_STATE_OPEN, @@ -141,6 +143,9 @@ error_t stream_open(stream_type_t stream_type) return ERROR_INTERNAL; } + //get target ID + targetID = swd_init_get_target(); + stream_thread_set(); // Initialize all variables memset(&shared_state, 0, sizeof(shared_state)); diff --git a/source/daplink/interface/swd_host.c b/source/daplink/interface/swd_host.c index 9b742ea46..b706d15f7 100644 --- a/source/daplink/interface/swd_host.c +++ b/source/daplink/interface/swd_host.c @@ -26,6 +26,7 @@ #include "debug_cm.h" #include "DAP_config.h" #include "DAP.h" +#include "target_ids.h" // Default NVIC and Core debug base addresses // TODO: Read these addresses from ROM. @@ -584,9 +585,9 @@ static uint8_t swd_write_debug_state(DEBUG_STATE *state) return 0; } - if (!swd_write_memory(target_device.flash_algo->algo_start, - (uint8_t *)target_device.flash_algo->algo_blob, - target_device.flash_algo->algo_size)) { + if (!swd_write_memory(target_device[targetID].flash_algo->algo_start, + (uint8_t *)target_device[targetID].flash_algo->algo_blob, + target_device[targetID].flash_algo->algo_size)) { return 0; } @@ -830,6 +831,106 @@ uint8_t swd_init_debug(void) return 1; } +static uint8_t get_target_id(uint32_t coreid) +{ + uint32_t tmp = 0; + uint8_t rc = Target_UNKNOWN; + //check IDCODE: + if(coreid == 0x2BA01477){ + //cortex-M4 + rc = Target_STM32F405; + }else if(coreid == 0x1BA01477){ + //cortex-M3 + rc = Target_STM32F103; + }else if(coreid == 0x0BB11477){ + //cortex-M0 + //check 0x40015800 address: + if(!swd_read_word(0x40015800, &tmp)){ + rc = Target_UNKNOWN; + }else{ + tmp &= 0x00000FFF; //device ID has 12 bits + if(tmp == 0x00000440){ + rc = Target_STM32F051; + }else if(tmp == 0x00000448){ + rc = Target_STM32F071; + }else if(tmp == 0x00000000){ + rc = Target_NRF51822; + }else{ + rc = Target_UNKNOWN; + } + } + } + + return rc; +} + +uint8_t swd_init_get_target(void) +{ + volatile uint32_t i = 0 ; + uint32_t tmp = 0; + uint32_t tmpid = 0; + uint32_t val; + + swd_init(); + + //add Reset Pin + swd_set_target_reset(1); + os_dly_wait(1); + swd_set_target_reset(0); + //need wait 500us + for( i = 0; i < 1200; i++) {} + + //init SWD sequence and get IDcode + if (!swd_reset()) { + return Target_UNKNOWN; + } + if (!swd_switch(0xE79E)) { + return Target_UNKNOWN; + } + if (!swd_reset()) { + return Target_UNKNOWN; + } + if (!swd_read_idcode(&tmpid)) { + return Target_UNKNOWN; + } + + if (!swd_write_dp(DP_ABORT, STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR)) { + return Target_UNKNOWN; + } + + // Ensure CTRL/STAT register selected in DPBANKSEL + if (!swd_write_dp(DP_SELECT, 0)) { + return Target_UNKNOWN; + } + + // Power up + if (!swd_write_dp(DP_CTRL_STAT, CSYSPWRUPREQ | CDBGPWRUPREQ)) { + return Target_UNKNOWN; + } + + do { + if (!swd_read_dp(DP_CTRL_STAT, &tmp)) { + return Target_UNKNOWN; + } + } while ((tmp & (CDBGPWRUPACK | CSYSPWRUPACK)) != (CDBGPWRUPACK | CSYSPWRUPACK)); + + + //halt CPU + // Enable halt on reset + if (!swd_write_word(DBG_HCSR, DBGKEY | C_DEBUGEN | C_HALT)) { + return Target_UNKNOWN; + } + //wait to halt + do { + if (!swd_read_word(DBG_HCSR, &val)) { + return Target_UNKNOWN; + } + } while((val & S_HALT) == 0); + + // core ID -> target ID + return get_target_id(tmpid); +} + __attribute__((weak)) void swd_set_target_reset(uint8_t asserted) { (asserted) ? PIN_nRESET_OUT(0) : PIN_nRESET_OUT(1); From bb7ef7ce448ea6a46323c3c8008334206a44b764 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 09:03:48 +0800 Subject: [PATCH 08/18] remove erase flash chip in drag&drop --- source/daplink/drag-n-drop/flash_manager.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/source/daplink/drag-n-drop/flash_manager.c b/source/daplink/drag-n-drop/flash_manager.c index 74f71cd3c..5949e29c3 100644 --- a/source/daplink/drag-n-drop/flash_manager.c +++ b/source/daplink/drag-n-drop/flash_manager.c @@ -95,15 +95,6 @@ error_t flash_manager_init(const flash_intf_t *flash_intf) return status; } - // Erase flash and unint if there are errors - status = intf->erase_chip(); - flash_manager_printf(" intf->erase_chip ret=%i\r\n", status); - - if (ERROR_SUCCESS != status) { - intf->uninit(); - return status; - } - state = STATE_OPEN; return status; } From 7188ef1fcc4c7fdceeaab7936bb573e30305587f Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Tue, 5 Jul 2016 09:04:59 +0800 Subject: [PATCH 09/18] should not twice memset string_board_id. use string_version instead. --- source/daplink/info.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/daplink/info.c b/source/daplink/info.c index 9b987cdc0..e238c1476 100644 --- a/source/daplink/info.c +++ b/source/daplink/info.c @@ -98,7 +98,7 @@ const char *info_get_unique_id_string_descriptor(void) static void setup_basics() { uint8_t i = 0, idx = 0; - memset(string_board_id, 0, sizeof(string_board_id)); + memset(string_version, 0, sizeof(string_version)); memset(string_host_id, 0, sizeof(string_host_id)); memset(string_target_id, 0, sizeof(string_target_id)); memset(string_hic_id, 0, sizeof(string_hic_id)); From 2a1b665139710181a5270fa4c65dd776aef5b10f Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Wed, 6 Jul 2016 14:32:31 +0800 Subject: [PATCH 10/18] nrf51822 use swd_set_target_state_sw(). --- records/board/mesh_multi_targets.yaml | 1 + .../DBG_nRF51822/nrf51_target_reset.c | 24 +++++++++++++++++++ source/target/mesheven/target_reset.c | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 source/target/mesheven/DBG_nRF51822/nrf51_target_reset.c diff --git a/records/board/mesh_multi_targets.yaml b/records/board/mesh_multi_targets.yaml index 3454a846b..c98850067 100644 --- a/records/board/mesh_multi_targets.yaml +++ b/records/board/mesh_multi_targets.yaml @@ -6,6 +6,7 @@ common: - source/board/mesh_multi_targets.c target: - source/target/mesheven/DBG_nRF51822/nrf51_target_flash.c + - source/target/mesheven/DBG_nRF51822/nrf51_target_reset.c - source/target/mesheven/DBG_STM32F051/stm32f051_target_flash.c - source/target/mesheven/DBG_STM32F071/stm32f071_target_flash.c - source/target/mesheven/DBG_STM32F103RC/stm32f103_target_flash.c diff --git a/source/target/mesheven/DBG_nRF51822/nrf51_target_reset.c b/source/target/mesheven/DBG_nRF51822/nrf51_target_reset.c new file mode 100644 index 000000000..fe2bd2a6c --- /dev/null +++ b/source/target/mesheven/DBG_nRF51822/nrf51_target_reset.c @@ -0,0 +1,24 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "target_reset.h" +#include "swd_host.h" + +uint8_t nrf51_target_set_state(TARGET_RESET_STATE state) +{ + return swd_set_target_state_sw(state); +} + diff --git a/source/target/mesheven/target_reset.c b/source/target/mesheven/target_reset.c index a3ee9c4b1..bc7949b69 100644 --- a/source/target/mesheven/target_reset.c +++ b/source/target/mesheven/target_reset.c @@ -26,6 +26,7 @@ typedef struct{ uint8_t (*setstate)(TARGET_RESET_STATE); }Target_Reset; +uint8_t nrf51_target_set_state(TARGET_RESET_STATE state); void common_target_before_init_debug(void) { @@ -49,7 +50,7 @@ uint8_t common_target_set_state(TARGET_RESET_STATE state) static const Target_Reset targets[] = { - {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, nrf51_target_set_state }, {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, From 0ef545a747f5edc6ea7b4551a9908749e86a4cd8 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Wed, 6 Jul 2016 14:33:43 +0800 Subject: [PATCH 11/18] complete DAP pins initialize. --- source/daplink/interface/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/daplink/interface/main.c b/source/daplink/interface/main.c index 1397a4d49..c03c3e775 100644 --- a/source/daplink/interface/main.c +++ b/source/daplink/interface/main.c @@ -242,6 +242,7 @@ __task void serial_process() extern __task void hid_process(void); __attribute__((weak)) void prerun_target_config(void) {} + __task void main_task(void) { // State processing @@ -267,7 +268,7 @@ __task void main_task(void) gpio_set_hid_led(GPIO_LED_ON); gpio_set_cdc_led(GPIO_LED_ON); // Initialize the DAP - DAP_Setup(); + swd_init(); // do some init with the target before USB and files are configured prerun_target_config(); // Update versions and IDs From 708f0d08aa28c4ccffd9a1ccbb3188e70700c047 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Wed, 6 Jul 2016 14:35:31 +0800 Subject: [PATCH 12/18] add return true when the vfs_write() process the buf datas. --- source/daplink/drag-n-drop/virtual_fs.c | 5 ++++- source/daplink/drag-n-drop/virtual_fs.h | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/daplink/drag-n-drop/virtual_fs.c b/source/daplink/drag-n-drop/virtual_fs.c index 5c9204ba6..b8da515ca 100644 --- a/source/daplink/drag-n-drop/virtual_fs.c +++ b/source/daplink/drag-n-drop/virtual_fs.c @@ -461,8 +461,9 @@ void vfs_read(uint32_t requested_sector, uint8_t *buf, uint32_t num_sectors) } } -void vfs_write(uint32_t requested_sector, const uint8_t *buf, uint32_t num_sectors) +bool vfs_write(uint32_t requested_sector, const uint8_t *buf, uint32_t num_sectors) { + bool rc = false; uint8_t i = 0; uint32_t current_sector; current_sector = 0; @@ -484,6 +485,7 @@ void vfs_write(uint32_t requested_sector, const uint8_t *buf, uint32_t num_secto // Update requested sector requested_sector += sectors_to_read; num_sectors -= sectors_to_read; + rc = true; } // If there is no more data to be read then break @@ -494,6 +496,7 @@ void vfs_write(uint32_t requested_sector, const uint8_t *buf, uint32_t num_secto // Move to the next virtual media entry current_sector += vm_sectors; } + return rc; } static uint32_t read_zero(uint32_t sector_offset, uint8_t *data, uint32_t num_sectors) diff --git a/source/daplink/drag-n-drop/virtual_fs.h b/source/daplink/drag-n-drop/virtual_fs.h index c95fbb285..7c305e63c 100644 --- a/source/daplink/drag-n-drop/virtual_fs.h +++ b/source/daplink/drag-n-drop/virtual_fs.h @@ -23,6 +23,7 @@ #define VIRTUAL_FS_H #include "stdint.h" +#include "stdbool.h" #ifdef __cplusplus extern "C" { @@ -99,7 +100,7 @@ void vfs_set_file_change_callback(vfs_file_change_cb_t cb); void vfs_read(uint32_t sector, uint8_t *buf, uint32_t num_of_sectors); // Write one or more sectors to the virtual filesystem -void vfs_write(uint32_t sector, const uint8_t *buf, uint32_t num_of_sectors); +bool vfs_write(uint32_t sector, const uint8_t *buf, uint32_t num_of_sectors); #ifdef __cplusplus } From 2646ab727281e519d00e42fc28918b8a7beb11db Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Wed, 6 Jul 2016 14:40:54 +0800 Subject: [PATCH 13/18] detect the data stream only it's belong the Bin/Hex file. add detect bin file end use file size information. add run target after flashed. --- source/daplink/drag-n-drop/vfs_manager.c | 37 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/source/daplink/drag-n-drop/vfs_manager.c b/source/daplink/drag-n-drop/vfs_manager.c index 04d44e943..6b5f5f92f 100644 --- a/source/daplink/drag-n-drop/vfs_manager.c +++ b/source/daplink/drag-n-drop/vfs_manager.c @@ -39,6 +39,7 @@ #include "file_stream.h" #include "error.h" +#include "target_ids.h" #include "target_config.h" // Set to 1 to enable debugging @@ -131,6 +132,9 @@ static uint32_t time_usb_idle; static OS_MUT sync_mutex; static OS_TID sync_thread = 0; +// the drag&drop file is Bin/Hex format +static bool fileIsBinOrHex = false; + // Synchronization functions static void sync_init(void); static void sync_assert_usb_thread(void); @@ -331,7 +335,8 @@ void usbd_msc_write_sect(uint32_t sector, uint8_t *buf, uint32_t num_of_sectors) // indicate DAP activity main_blink_hid_led(MAIN_LED_OFF); - vfs_write(sector, buf, num_of_sectors); + if (vfs_write(sector, buf, num_of_sectors) == true) + return; file_data_handler(sector, buf, num_of_sectors); } @@ -363,6 +368,9 @@ static bool changing_state() static void build_filesystem() { + //init + fileIsBinOrHex = false; + // Update anything that could have changed file system state file_transfer_state = default_transfer_state; vfs_user_build_filesystem(); @@ -400,6 +408,8 @@ static void file_change_handler(const vfs_filename_t filename, vfs_file_change_t // the same extension to keep track of transfer info in some cases. if (!(VFS_FILE_ATTR_HIDDEN & vfs_file_get_attr(new_file_data))) { stream = stream_type_from_name(filename); + if (STREAM_TYPE_NONE != stream) + fileIsBinOrHex = true; uint32_t size = vfs_file_get_size(new_file_data); vfs_sector_t sector = vfs_file_get_start_sector(new_file_data); transfer_update_file_info(file, sector, size, stream); @@ -421,7 +431,9 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_ // this is the key for starting a file write - we dont care what file types are sent // just look for something unique (NVIC table, hex, srec, etc) until root dir is updated - if (!file_transfer_state.stream_started) { + if (!file_transfer_state.stream_started && fileIsBinOrHex == true) { + if ( targetID == Target_UNKNOWN ) + targetID = swd_init_get_target(); // look for file types we can program stream = stream_start_identify((uint8_t *)buf, VFS_SECTOR_SIZE * num_of_sectors); @@ -659,6 +671,7 @@ static void transfer_stream_data(uint32_t sector, const uint8_t *data, uint32_t util_assert(file_transfer_state.stream_open); status = stream_write((uint8_t *)data, size); vfs_mngr_printf(" stream_write ret=%i\r\n", status); + file_transfer_state.size_processed += size; if (ERROR_SUCCESS_DONE == status) { // Override status so ERROR_SUCCESS_DONE @@ -669,13 +682,23 @@ static void transfer_stream_data(uint32_t sector, const uint8_t *data, uint32_t file_transfer_state.stream_finished = true; file_transfer_state.stream_optional_finish = true; } else if (ERROR_SUCCESS_DONE_OR_CONTINUE == status) { - status = ERROR_SUCCESS; - file_transfer_state.stream_optional_finish = true; + if (file_transfer_state.size_processed < file_transfer_state.file_size) + { + status = ERROR_SUCCESS; + file_transfer_state.stream_optional_finish = true; + } + else + { + status = stream_close(); + vfs_mngr_printf(" stream_close ret=%i\r\n", status); + file_transfer_state.stream_open = false; + file_transfer_state.stream_finished = true; + file_transfer_state.stream_optional_finish = true; + } } else { file_transfer_state.stream_optional_finish = false; } - file_transfer_state.size_processed += size; transfer_update_state(status); } @@ -778,6 +801,10 @@ static void transfer_update_state(error_t status) // Set the fail reason fail_reason = local_status; vfs_mngr_printf(" Transfer finished, status: %i=%s\r\n", fail_reason, error_get_string(fail_reason)); + + target_set_state(RESET_RUN); + targetID = Target_UNKNOWN; + } // If this state change is not from aborting a transfer From 85b26980d156f6a13f73cf446679c53ab1388b68 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Thu, 7 Jul 2016 10:04:07 +0800 Subject: [PATCH 14/18] change STM32F051/STM32F071 use owner target_set_state(). because the modules using STM32F051/STM32F071 did not Reset Pin. --- records/board/mesh_multi_targets.yaml | 2 ++ .../DBG_STM32F051/stm32f051_target_reset.c | 24 +++++++++++++++++++ .../DBG_STM32F071/stm32f071_target_reset.c | 24 +++++++++++++++++++ source/target/mesheven/target_reset.c | 20 +++++++++++----- 4 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 source/target/mesheven/DBG_STM32F051/stm32f051_target_reset.c create mode 100644 source/target/mesheven/DBG_STM32F071/stm32f071_target_reset.c diff --git a/records/board/mesh_multi_targets.yaml b/records/board/mesh_multi_targets.yaml index c98850067..8e1edbdd4 100644 --- a/records/board/mesh_multi_targets.yaml +++ b/records/board/mesh_multi_targets.yaml @@ -8,7 +8,9 @@ common: - source/target/mesheven/DBG_nRF51822/nrf51_target_flash.c - source/target/mesheven/DBG_nRF51822/nrf51_target_reset.c - source/target/mesheven/DBG_STM32F051/stm32f051_target_flash.c + - source/target/mesheven/DBG_STM32F051/stm32f051_target_reset.c - source/target/mesheven/DBG_STM32F071/stm32f071_target_flash.c + - source/target/mesheven/DBG_STM32F071/stm32f071_target_reset.c - source/target/mesheven/DBG_STM32F103RC/stm32f103_target_flash.c - source/target/mesheven/DBG_STM32F405/stm32f405_target_flash.c - source/target/mesheven/target.c diff --git a/source/target/mesheven/DBG_STM32F051/stm32f051_target_reset.c b/source/target/mesheven/DBG_STM32F051/stm32f051_target_reset.c new file mode 100644 index 000000000..e5f653b7c --- /dev/null +++ b/source/target/mesheven/DBG_STM32F051/stm32f051_target_reset.c @@ -0,0 +1,24 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "target_reset.h" +#include "swd_host.h" + +uint8_t stm32f051_target_set_state(TARGET_RESET_STATE state) +{ + return swd_set_target_state_sw(state); +} + diff --git a/source/target/mesheven/DBG_STM32F071/stm32f071_target_reset.c b/source/target/mesheven/DBG_STM32F071/stm32f071_target_reset.c new file mode 100644 index 000000000..574245610 --- /dev/null +++ b/source/target/mesheven/DBG_STM32F071/stm32f071_target_reset.c @@ -0,0 +1,24 @@ +/* CMSIS-DAP Interface Firmware + * Copyright (c) 2009-2013 ARM Limited + * + * 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 "target_reset.h" +#include "swd_host.h" + +uint8_t stm32f071_target_set_state(TARGET_RESET_STATE state) +{ + return swd_set_target_state_sw(state); +} + diff --git a/source/target/mesheven/target_reset.c b/source/target/mesheven/target_reset.c index bc7949b69..df566da83 100644 --- a/source/target/mesheven/target_reset.c +++ b/source/target/mesheven/target_reset.c @@ -16,6 +16,7 @@ #include "target_reset.h" #include "swd_host.h" #include "target_ids.h" +#include "gpio.h" uint8_t targetID = Target_UNKNOWN; @@ -27,6 +28,9 @@ typedef struct{ }Target_Reset; uint8_t nrf51_target_set_state(TARGET_RESET_STATE state); +uint8_t stm32f051_target_set_state(TARGET_RESET_STATE state); +uint8_t stm32f071_target_set_state(TARGET_RESET_STATE state); + void common_target_before_init_debug(void) { @@ -45,16 +49,20 @@ uint8_t common_security_bits_set(uint32_t addr, uint8_t *data, uint32_t size) uint8_t common_target_set_state(TARGET_RESET_STATE state) { - return swd_set_target_state_hw(state); + if (gpio_get_config(PIN_CONFIG_DT01) == PIN_HIGH) { + return swd_set_target_state_hw(state); + } else { + return swd_set_target_state_sw(state); + } } static const Target_Reset targets[] = { - {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, nrf51_target_set_state }, - {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, - {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, - {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, - {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, nrf51_target_set_state }, //nRF51 + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, stm32f051_target_set_state }, //STM32F051 + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, //STM32F103 + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, common_target_set_state }, //STM32F405 + {common_target_before_init_debug , common_target_unlock_sequence , common_security_bits_set, stm32f071_target_set_state }, //STM32F071 }; From 1fdab7082e68e520800fbe130ce20021bbf150dd Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Thu, 7 Jul 2016 10:06:35 +0800 Subject: [PATCH 15/18] fixed blocked in swd_init_get_target() when target is STM32F405. fixed use Software reset when No hardware Reset Pin in RESET_RUN. --- source/daplink/interface/swd_host.c | 36 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/source/daplink/interface/swd_host.c b/source/daplink/interface/swd_host.c index b706d15f7..1436fb066 100644 --- a/source/daplink/interface/swd_host.c +++ b/source/daplink/interface/swd_host.c @@ -869,7 +869,6 @@ uint8_t swd_init_get_target(void) volatile uint32_t i = 0 ; uint32_t tmp = 0; uint32_t tmpid = 0; - uint32_t val; swd_init(); @@ -914,19 +913,6 @@ uint8_t swd_init_get_target(void) } } while ((tmp & (CDBGPWRUPACK | CSYSPWRUPACK)) != (CDBGPWRUPACK | CSYSPWRUPACK)); - - //halt CPU - // Enable halt on reset - if (!swd_write_word(DBG_HCSR, DBGKEY | C_DEBUGEN | C_HALT)) { - return Target_UNKNOWN; - } - //wait to halt - do { - if (!swd_read_word(DBG_HCSR, &val)) { - return Target_UNKNOWN; - } - } while((val & S_HALT) == 0); - // core ID -> target ID return get_target_id(tmpid); } @@ -1044,11 +1030,23 @@ uint8_t swd_set_target_state_sw(TARGET_RESET_STATE state) break; case RESET_RUN: - swd_set_target_reset(1); - os_dly_wait(2); - swd_set_target_reset(0); - os_dly_wait(2); - swd_off(); + if (!swd_init_debug()) { + return 0; + } + //enable debug + if (!swd_write_word(DBG_HCSR, DBGKEY | C_DEBUGEN)) { + return 0; + } + //disable halt reset + if (!swd_write_word(DBG_EMCR, 0)) { + return 0; + } + + //SysReset + if (!swd_write_word(NVIC_AIRCR, VECTKEY | SOFT_RESET)) { + return 0; + } + break; case RESET_PROGRAM: From abd4cbf31083e4d5f353817e717714ae3fca13d0 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Thu, 7 Jul 2016 10:53:08 +0800 Subject: [PATCH 16/18] fixed data lost when some UART tool set same configuration during Rx/Tx. --- source/daplink/usb2uart/serial.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/source/daplink/usb2uart/serial.c b/source/daplink/usb2uart/serial.c index f8df7842b..1d2c13765 100644 --- a/source/daplink/usb2uart/serial.c +++ b/source/daplink/usb2uart/serial.c @@ -46,9 +46,34 @@ int32_t serial_reset(void) int32_t serial_set_configuration(UART_Configuration *config) { - uart_config = *config; - os_mbx_send(&serial_mailbox, (void *)SERIAL_SET_CONFIGURATION, 0); - return 1; + int32_t needupdate = 0; + //check config + if(uart_config.Baudrate != config->Baudrate){ + uart_config.Baudrate = config->Baudrate; + needupdate = 1; + } + if(uart_config.DataBits != config->DataBits){ + uart_config.DataBits = config->DataBits; + needupdate = 1; + } + if(uart_config.Parity != config->Parity){ + uart_config.Parity = config->Parity; + needupdate = 1; + } + if(uart_config.StopBits != config->StopBits){ + uart_config.StopBits = config->StopBits; + needupdate = 1; + } + + if(uart_config.FlowControl != config->FlowControl){ + uart_config.FlowControl = config->FlowControl; + needupdate = 1; + } + + if(needupdate == 1){ + os_mbx_send(&serial_mailbox, (void*)SERIAL_SET_CONFIGURATION, 0); + } + return 1; } int32_t serial_get_configuration(UART_Configuration *config) From f24eba4298bc146cd5b06f138df8950d5b88aa71 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Thu, 7 Jul 2016 11:16:57 +0800 Subject: [PATCH 17/18] fixed after uart_reset() the UART IRQ be enabled. --- source/hic_hal/nxp/lpc11u35/uart.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/hic_hal/nxp/lpc11u35/uart.c b/source/hic_hal/nxp/lpc11u35/uart.c index e87b9dcd1..402b3f20f 100644 --- a/source/hic_hal/nxp/lpc11u35/uart.c +++ b/source/hic_hal/nxp/lpc11u35/uart.c @@ -78,11 +78,12 @@ int32_t uart_initialize(void) int32_t uart_uninitialize(void) { - // disable interrupt + // clear interrupt enable bits LPC_USART->IER &= ~(0x7); - NVIC_DisableIRQ(UART_IRQn); // reset uart uart_reset(); + // disable UART + NVIC_DisableIRQ(UART_IRQn); return 1; } @@ -127,10 +128,10 @@ int32_t uart_set_configuration(UART_Configuration *config) uint8_t DivAddVal = 0; uint8_t MulVal = 1; uint8_t mv, data_bits = 8, parity, stop_bits = 0; - // disable interrupt - NVIC_DisableIRQ(UART_IRQn); // reset uart uart_reset(); + // disable interrupt + NVIC_DisableIRQ(UART_IRQn); //clear RTS if(gpio_get_config(PIN_CONFIG_DT01) == PIN_HIGH) { From 9813ba25535b311e130c362ef958a38d8aff17d8 Mon Sep 17 00:00:00 2001 From: wenjin zhang Date: Thu, 7 Jul 2016 16:57:03 +0800 Subject: [PATCH 18/18] fixed missing delete code when move call swd_init_get_target() in file_data_handler(). --- source/daplink/drag-n-drop/file_stream.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/source/daplink/drag-n-drop/file_stream.c b/source/daplink/drag-n-drop/file_stream.c index 27f9ac8af..adbcf353f 100644 --- a/source/daplink/drag-n-drop/file_stream.c +++ b/source/daplink/drag-n-drop/file_stream.c @@ -31,7 +31,6 @@ #include "RTL.h" #include "compiler.h" -#include "target_config.h" typedef enum { STREAM_STATE_CLOSED, @@ -142,9 +141,6 @@ error_t stream_open(stream_type_t stream_type) util_assert(0); return ERROR_INTERNAL; } - - //get target ID - targetID = swd_init_get_target(); stream_thread_set(); // Initialize all variables