Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

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?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file iap.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2017-2017, 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
Pawel Zarembski 0:01f31e923fe2 22 #include "iap.h"
Pawel Zarembski 0:01f31e923fe2 23 #include "cortex_m.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "LPC11Uxx.h"
Pawel Zarembski 0:01f31e923fe2 25
Pawel Zarembski 0:01f31e923fe2 26 iap_operation_t iap_op;
Pawel Zarembski 0:01f31e923fe2 27 static cortex_int_state_t state;
Pawel Zarembski 0:01f31e923fe2 28 static uint32_t lock_count;
Pawel Zarembski 0:01f31e923fe2 29
Pawel Zarembski 0:01f31e923fe2 30 // taken code from the Nxp App Note AN11305
Pawel Zarembski 0:01f31e923fe2 31 /* This data must be global so it is not read from the stack */
Pawel Zarembski 0:01f31e923fe2 32 typedef void (*IAP)(uint32_t [], uint32_t []);
Pawel Zarembski 0:01f31e923fe2 33 static const IAP iap_entry = (IAP)0x1fff1ff1;
Pawel Zarembski 0:01f31e923fe2 34 #define init_msdstate() *((uint32_t *)(0x10000054)) = 0x0
Pawel Zarembski 0:01f31e923fe2 35
Pawel Zarembski 0:01f31e923fe2 36 /* This function resets some microcontroller peripherals to reset
Pawel Zarembski 0:01f31e923fe2 37 * hardware configuration to ensure that the USB In-System Programming module
Pawel Zarembski 0:01f31e923fe2 38 * will work properly. It is normally called from reset and assumes some reset
Pawel Zarembski 0:01f31e923fe2 39 * configuration settings for the MCU.
Pawel Zarembski 0:01f31e923fe2 40 * Some of the peripheral configurations may be redundant in your specific
Pawel Zarembski 0:01f31e923fe2 41 * project.
Pawel Zarembski 0:01f31e923fe2 42 */
Pawel Zarembski 0:01f31e923fe2 43 void iap_reinvoke(void)
Pawel Zarembski 0:01f31e923fe2 44 {
Pawel Zarembski 0:01f31e923fe2 45 /* make sure USB clock is turned on before calling ISP */
Pawel Zarembski 0:01f31e923fe2 46 LPC_SYSCON->SYSAHBCLKCTRL |= 0x04000;
Pawel Zarembski 0:01f31e923fe2 47 /* make sure 32-bit Timer 1 is turned on before calling ISP */
Pawel Zarembski 0:01f31e923fe2 48 LPC_SYSCON->SYSAHBCLKCTRL |= 0x00400;
Pawel Zarembski 0:01f31e923fe2 49 /* make sure GPIO clock is turned on before calling ISP */
Pawel Zarembski 0:01f31e923fe2 50 LPC_SYSCON->SYSAHBCLKCTRL |= 0x00040;
Pawel Zarembski 0:01f31e923fe2 51 /* make sure IO configuration clock is turned on before calling ISP */
Pawel Zarembski 0:01f31e923fe2 52 LPC_SYSCON->SYSAHBCLKCTRL |= 0x10000;
Pawel Zarembski 0:01f31e923fe2 53 /* make sure AHB clock divider is 1:1 */
Pawel Zarembski 0:01f31e923fe2 54 LPC_SYSCON->SYSAHBCLKDIV = 1;
Pawel Zarembski 0:01f31e923fe2 55 /* Send Reinvoke ISP command to ISP entry point*/
Pawel Zarembski 0:01f31e923fe2 56 iap_op.cmd = 57;
Pawel Zarembski 0:01f31e923fe2 57 init_msdstate(); /* Initialize Storage state machine */
Pawel Zarembski 0:01f31e923fe2 58 /* Set stack pointer to ROM value (reset default) This must be the last
Pawel Zarembski 0:01f31e923fe2 59 * piece of code executed before calling ISP, because most C expressions
Pawel Zarembski 0:01f31e923fe2 60 * and function returns will fail after the stack pointer is changed.
Pawel Zarembski 0:01f31e923fe2 61 * In addition ensure the CONTROL register is set to 0 so the MSP is
Pawel Zarembski 0:01f31e923fe2 62 * used rather than the PSP.
Pawel Zarembski 0:01f31e923fe2 63 */
Pawel Zarembski 0:01f31e923fe2 64 __set_MSP(*((volatile uint32_t *)0x00000000));
Pawel Zarembski 0:01f31e923fe2 65 __set_CONTROL(0);
Pawel Zarembski 0:01f31e923fe2 66 /* Enter ISP. We call "iap_entry" to enter ISP because the ISP entry is done
Pawel Zarembski 0:01f31e923fe2 67 * through the same command interface as IAP.
Pawel Zarembski 0:01f31e923fe2 68 */
Pawel Zarembski 0:01f31e923fe2 69 iap_entry(&iap_op.cmd, &iap_op.stat);
Pawel Zarembski 0:01f31e923fe2 70 // Not supposed to come back!
Pawel Zarembski 0:01f31e923fe2 71 }
Pawel Zarembski 0:01f31e923fe2 72
Pawel Zarembski 0:01f31e923fe2 73 void iap_call(iap_operation_t *operation)
Pawel Zarembski 0:01f31e923fe2 74 {
Pawel Zarembski 0:01f31e923fe2 75 iap_entry(&operation->cmd, &operation->stat);
Pawel Zarembski 0:01f31e923fe2 76 }
Pawel Zarembski 0:01f31e923fe2 77
Pawel Zarembski 0:01f31e923fe2 78 void iap_lock()
Pawel Zarembski 0:01f31e923fe2 79 {
Pawel Zarembski 0:01f31e923fe2 80 cortex_int_state_t local_state;
Pawel Zarembski 0:01f31e923fe2 81 local_state = cortex_int_get_and_disable();
Pawel Zarembski 0:01f31e923fe2 82 if (lock_count == 0) {
Pawel Zarembski 0:01f31e923fe2 83 state = local_state;
Pawel Zarembski 0:01f31e923fe2 84 }
Pawel Zarembski 0:01f31e923fe2 85 lock_count++;
Pawel Zarembski 0:01f31e923fe2 86 }
Pawel Zarembski 0:01f31e923fe2 87
Pawel Zarembski 0:01f31e923fe2 88 void iap_unlock()
Pawel Zarembski 0:01f31e923fe2 89 {
Pawel Zarembski 0:01f31e923fe2 90 lock_count--;
Pawel Zarembski 0:01f31e923fe2 91 if (lock_count == 0) {
Pawel Zarembski 0:01f31e923fe2 92 cortex_int_restore(state);
Pawel Zarembski 0:01f31e923fe2 93 }
Pawel Zarembski 0:01f31e923fe2 94
Pawel Zarembski 0:01f31e923fe2 95 }