Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/family/nxp/lpc55S6X/target_reset.c@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file target_reset.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief Target reset for the lpc812 |
Pawel Zarembski |
0:01f31e923fe2 | 4 | * |
Pawel Zarembski |
0:01f31e923fe2 | 5 | * DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 6 | * Copyright (c) 2009-2019, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 7 | * SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 8 | * |
Pawel Zarembski |
0:01f31e923fe2 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 10 | * not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 11 | * You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 12 | * |
Pawel Zarembski |
0:01f31e923fe2 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 14 | * |
Pawel Zarembski |
0:01f31e923fe2 | 15 | * Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 18 | * See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 19 | * limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 20 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 21 | #include "target_family.h" |
Pawel Zarembski |
0:01f31e923fe2 | 22 | #include "swd_host.h" |
Pawel Zarembski |
0:01f31e923fe2 | 23 | #include "cmsis_os2.h" |
Pawel Zarembski |
0:01f31e923fe2 | 24 | |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #define DBG_Addr (0xE000EDF0) |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #define DWT_COMP0 (0xE0001020) |
Pawel Zarembski |
0:01f31e923fe2 | 27 | #define DWT_FUNCTION0 (0xE0001028) |
Pawel Zarembski |
0:01f31e923fe2 | 28 | #define DWT_FUNCTION_MATCH (0x4 << 0) |
Pawel Zarembski |
0:01f31e923fe2 | 29 | #define DWT_FUNCTION_ACTION (0x1 << 4) |
Pawel Zarembski |
0:01f31e923fe2 | 30 | #define DWT_FUNCTION_DATAVSIZE (0x2 << 10) |
Pawel Zarembski |
0:01f31e923fe2 | 31 | |
Pawel Zarembski |
0:01f31e923fe2 | 32 | /* Debug mailbox AP registers */ |
Pawel Zarembski |
0:01f31e923fe2 | 33 | #define DEBUGMB_CSW 0x02000000 |
Pawel Zarembski |
0:01f31e923fe2 | 34 | #define DEBUGMB_REQ 0x02000004 |
Pawel Zarembski |
0:01f31e923fe2 | 35 | #define DEBUGMB_RET 0x02000008 |
Pawel Zarembski |
0:01f31e923fe2 | 36 | #define DEBUGMB_ID 0x020000FC |
Pawel Zarembski |
0:01f31e923fe2 | 37 | |
Pawel Zarembski |
0:01f31e923fe2 | 38 | static uint8_t lpc55s6x_target_set_state(target_state_t state) |
Pawel Zarembski |
0:01f31e923fe2 | 39 | { |
Pawel Zarembski |
0:01f31e923fe2 | 40 | uint32_t val; |
Pawel Zarembski |
0:01f31e923fe2 | 41 | int8_t ap_retries = 2; |
Pawel Zarembski |
0:01f31e923fe2 | 42 | |
Pawel Zarembski |
0:01f31e923fe2 | 43 | if (state == RESET_PROGRAM) { |
Pawel Zarembski |
0:01f31e923fe2 | 44 | if (!swd_init_debug()) { |
Pawel Zarembski |
0:01f31e923fe2 | 45 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 46 | } |
Pawel Zarembski |
0:01f31e923fe2 | 47 | |
Pawel Zarembski |
0:01f31e923fe2 | 48 | // Enable debug |
Pawel Zarembski |
0:01f31e923fe2 | 49 | while(swd_write_word(DBG_HCSR, DBGKEY | C_DEBUGEN) == 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 50 | if( --ap_retries <=0 ) |
Pawel Zarembski |
0:01f31e923fe2 | 51 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 52 | // Target is in invalid state? |
Pawel Zarembski |
0:01f31e923fe2 | 53 | swd_set_target_reset(1); |
Pawel Zarembski |
0:01f31e923fe2 | 54 | osDelay(2); |
Pawel Zarembski |
0:01f31e923fe2 | 55 | swd_set_target_reset(0); |
Pawel Zarembski |
0:01f31e923fe2 | 56 | osDelay(2); |
Pawel Zarembski |
0:01f31e923fe2 | 57 | } |
Pawel Zarembski |
0:01f31e923fe2 | 58 | |
Pawel Zarembski |
0:01f31e923fe2 | 59 | // Set Trace Enable bit |
Pawel Zarembski |
0:01f31e923fe2 | 60 | if (!swd_read_word(DBG_EMCR, &val)) { |
Pawel Zarembski |
0:01f31e923fe2 | 61 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 62 | } |
Pawel Zarembski |
0:01f31e923fe2 | 63 | |
Pawel Zarembski |
0:01f31e923fe2 | 64 | if (!swd_write_word(DBG_EMCR, val | TRCENA)) { |
Pawel Zarembski |
0:01f31e923fe2 | 65 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 66 | } |
Pawel Zarembski |
0:01f31e923fe2 | 67 | |
Pawel Zarembski |
0:01f31e923fe2 | 68 | // Clear the comparator function register |
Pawel Zarembski |
0:01f31e923fe2 | 69 | if (!swd_write_word(DWT_FUNCTION0, 0x0)) { |
Pawel Zarembski |
0:01f31e923fe2 | 70 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 71 | } |
Pawel Zarembski |
0:01f31e923fe2 | 72 | |
Pawel Zarembski |
0:01f31e923fe2 | 73 | // Set the address |
Pawel Zarembski |
0:01f31e923fe2 | 74 | if (!swd_write_word(DWT_COMP0, 0x50000040)) { |
Pawel Zarembski |
0:01f31e923fe2 | 75 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 76 | } |
Pawel Zarembski |
0:01f31e923fe2 | 77 | |
Pawel Zarembski |
0:01f31e923fe2 | 78 | // Update the comparator function register |
Pawel Zarembski |
0:01f31e923fe2 | 79 | if (!swd_write_word(DWT_FUNCTION0, (DWT_FUNCTION_MATCH | DWT_FUNCTION_ACTION | DWT_FUNCTION_DATAVSIZE))) { |
Pawel Zarembski |
0:01f31e923fe2 | 80 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 81 | } |
Pawel Zarembski |
0:01f31e923fe2 | 82 | |
Pawel Zarembski |
0:01f31e923fe2 | 83 | // Reset using the debug mailbox |
Pawel Zarembski |
0:01f31e923fe2 | 84 | if (!swd_write_ap(DEBUGMB_CSW, 0x20)) { |
Pawel Zarembski |
0:01f31e923fe2 | 85 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 86 | } |
Pawel Zarembski |
0:01f31e923fe2 | 87 | |
Pawel Zarembski |
0:01f31e923fe2 | 88 | osDelay(5); |
Pawel Zarembski |
0:01f31e923fe2 | 89 | |
Pawel Zarembski |
0:01f31e923fe2 | 90 | do { |
Pawel Zarembski |
0:01f31e923fe2 | 91 | if (!swd_read_word(DBG_HCSR, &val)) { |
Pawel Zarembski |
0:01f31e923fe2 | 92 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 93 | } |
Pawel Zarembski |
0:01f31e923fe2 | 94 | } while ((val & S_HALT) == 0); |
Pawel Zarembski |
0:01f31e923fe2 | 95 | |
Pawel Zarembski |
0:01f31e923fe2 | 96 | // Disable halt on reset |
Pawel Zarembski |
0:01f31e923fe2 | 97 | if (!swd_write_word(DBG_EMCR, 0)) { |
Pawel Zarembski |
0:01f31e923fe2 | 98 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 99 | } |
Pawel Zarembski |
0:01f31e923fe2 | 100 | |
Pawel Zarembski |
0:01f31e923fe2 | 101 | return 1; |
Pawel Zarembski |
0:01f31e923fe2 | 102 | |
Pawel Zarembski |
0:01f31e923fe2 | 103 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 104 | return swd_set_target_state_sw(state); |
Pawel Zarembski |
0:01f31e923fe2 | 105 | } |
Pawel Zarembski |
0:01f31e923fe2 | 106 | } |
Pawel Zarembski |
0:01f31e923fe2 | 107 | |
Pawel Zarembski |
0:01f31e923fe2 | 108 | const target_family_descriptor_t g_target_family_lpc55S6X = { |
Pawel Zarembski |
0:01f31e923fe2 | 109 | .family_id = VENDOR_TO_FAMILY(kNXP_VendorID, 0), //ID not maching the predefined family ids |
Pawel Zarembski |
0:01f31e923fe2 | 110 | .target_set_state = lpc55s6x_target_set_state, |
Pawel Zarembski |
0:01f31e923fe2 | 111 | }; |
Pawel Zarembski |
0:01f31e923fe2 | 112 | |
Pawel Zarembski |
0:01f31e923fe2 | 113 | const target_family_descriptor_t *g_target_family = &g_target_family_lpc55S6X; |