|
| 1 | +/** |
| 2 | + * @file iap.c |
| 3 | + * @brief |
| 4 | + * |
| 5 | + * DAPLink Interface Firmware |
| 6 | + * Copyright (c) 2017-2017, ARM Limited, All Rights Reserved |
| 7 | + * SPDX-License-Identifier: Apache-2.0 |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | + * not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "iap.h" |
| 23 | +#include "cortex_m.h" |
| 24 | +#include "LPC11Uxx.h" |
| 25 | + |
| 26 | +iap_operation_t iap_op; |
| 27 | +static cortex_int_state_t state; |
| 28 | +static uint32_t lock_count; |
| 29 | + |
| 30 | +// taken code from the Nxp App Note AN11305 |
| 31 | +/* This data must be global so it is not read from the stack */ |
| 32 | +typedef void (*IAP)(uint32_t [], uint32_t []); |
| 33 | +static const IAP iap_entry = (IAP)0x1fff1ff1; |
| 34 | +#define init_msdstate() *((uint32_t *)(0x10000054)) = 0x0 |
| 35 | + |
| 36 | +/* This function resets some microcontroller peripherals to reset |
| 37 | + * hardware configuration to ensure that the USB In-System Programming module |
| 38 | + * will work properly. It is normally called from reset and assumes some reset |
| 39 | + * configuration settings for the MCU. |
| 40 | + * Some of the peripheral configurations may be redundant in your specific |
| 41 | + * project. |
| 42 | + */ |
| 43 | +void iap_reinvoke(void) |
| 44 | +{ |
| 45 | + /* make sure USB clock is turned on before calling ISP */ |
| 46 | + LPC_SYSCON->SYSAHBCLKCTRL |= 0x04000; |
| 47 | + /* make sure 32-bit Timer 1 is turned on before calling ISP */ |
| 48 | + LPC_SYSCON->SYSAHBCLKCTRL |= 0x00400; |
| 49 | + /* make sure GPIO clock is turned on before calling ISP */ |
| 50 | + LPC_SYSCON->SYSAHBCLKCTRL |= 0x00040; |
| 51 | + /* make sure IO configuration clock is turned on before calling ISP */ |
| 52 | + LPC_SYSCON->SYSAHBCLKCTRL |= 0x10000; |
| 53 | + /* make sure AHB clock divider is 1:1 */ |
| 54 | + LPC_SYSCON->SYSAHBCLKDIV = 1; |
| 55 | + /* Send Reinvoke ISP command to ISP entry point*/ |
| 56 | + iap_op.cmd = 57; |
| 57 | + init_msdstate(); /* Initialize Storage state machine */ |
| 58 | + /* Set stack pointer to ROM value (reset default) This must be the last |
| 59 | + * piece of code executed before calling ISP, because most C expressions |
| 60 | + * and function returns will fail after the stack pointer is changed. |
| 61 | + * In addition ensure the CONTROL register is set to 0 so the MSP is |
| 62 | + * used rather than the PSP. |
| 63 | + */ |
| 64 | + __set_MSP(*((volatile uint32_t *)0x00000000)); |
| 65 | + __set_CONTROL(0); |
| 66 | + /* Enter ISP. We call "iap_entry" to enter ISP because the ISP entry is done |
| 67 | + * through the same command interface as IAP. |
| 68 | + */ |
| 69 | + iap_entry(&iap_op.cmd, &iap_op.stat); |
| 70 | + // Not supposed to come back! |
| 71 | +} |
| 72 | + |
| 73 | +void iap_call(iap_operation_t *operation) |
| 74 | +{ |
| 75 | + iap_entry(&operation->cmd, &operation->stat); |
| 76 | +} |
| 77 | + |
| 78 | +void iap_lock() |
| 79 | +{ |
| 80 | + cortex_int_state_t local_state; |
| 81 | + local_state = cortex_int_get_and_disable(); |
| 82 | + if (lock_count == 0) { |
| 83 | + state = local_state; |
| 84 | + } |
| 85 | + lock_count++; |
| 86 | +} |
| 87 | + |
| 88 | +void iap_unlock() |
| 89 | +{ |
| 90 | + lock_count--; |
| 91 | + if (lock_count == 0) { |
| 92 | + cortex_int_restore(state); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments