|
| 1 | +/** |
| 2 | + * @file flash.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 "flash_hal.h" |
| 23 | +#include "iap.h" |
| 24 | +#include "macro.h" |
| 25 | +#include "LPC11Uxx.h" |
| 26 | +#include "string.h" |
| 27 | + |
| 28 | +// page buffer must be 4 byte algined and must reside in ram bank 1 |
| 29 | +static __attribute__((section("RAM1"))) uint32_t page_buf[64]; |
| 30 | + |
| 31 | +static uint32_t get_sector_num(uint32_t addr); |
| 32 | + |
| 33 | +uint32_t Init(uint32_t adr, uint32_t clk, uint32_t fnc) |
| 34 | +{ |
| 35 | + return 0; // No init needed |
| 36 | +} |
| 37 | + |
| 38 | +uint32_t UnInit(uint32_t fnc) |
| 39 | +{ |
| 40 | + return 0; // No init needed |
| 41 | +} |
| 42 | + |
| 43 | +uint32_t EraseChip(void) |
| 44 | +{ |
| 45 | + return (1); // IAP not supported |
| 46 | +} |
| 47 | + |
| 48 | +uint32_t EraseSector(uint32_t adr) |
| 49 | +{ |
| 50 | + uint32_t num = get_sector_num(adr); |
| 51 | + |
| 52 | + iap_lock(); |
| 53 | + |
| 54 | + iap_op.cmd = 50; // Prepare Sector for Erase |
| 55 | + iap_op.par[0] = num; // Start Sector |
| 56 | + iap_op.par[1] = num; // End Sector |
| 57 | + iap_call(&iap_op); |
| 58 | + if (iap_op.stat != CMD_SUCCESS) { |
| 59 | + iap_unlock(); |
| 60 | + return (1); |
| 61 | + } |
| 62 | + |
| 63 | + iap_op.cmd = 52; // Erase Sector |
| 64 | + iap_op.par[0] = num; // Start Sector |
| 65 | + iap_op.par[1] = num; // End Sector |
| 66 | + iap_op.par[2] = SystemCoreClock / 1000; // Core Clock in kHz |
| 67 | + iap_call(&iap_op); |
| 68 | + if (iap_op.stat != CMD_SUCCESS) { |
| 69 | + iap_unlock(); |
| 70 | + return (1); |
| 71 | + } |
| 72 | + |
| 73 | + iap_unlock(); |
| 74 | + |
| 75 | + return 0; // Success |
| 76 | +} |
| 77 | + |
| 78 | +uint32_t ProgramPage(uint32_t adr, uint32_t sz, uint32_t *buf) |
| 79 | +{ |
| 80 | + uint32_t num = get_sector_num(adr); |
| 81 | + |
| 82 | + iap_lock(); |
| 83 | + |
| 84 | + iap_op.cmd = 50; // Prepare Sector for Program |
| 85 | + iap_op.par[0] = num; // Start Sector |
| 86 | + iap_op.par[1] = num; // End Sector |
| 87 | + iap_call(&iap_op); |
| 88 | + if (iap_op.stat != CMD_SUCCESS) { |
| 89 | + iap_unlock(); |
| 90 | + return (1); |
| 91 | + } |
| 92 | + |
| 93 | + while (sz > 0) { |
| 94 | + uint32_t copy_size = MIN(sz, sizeof(page_buf)); |
| 95 | + memset(page_buf, 0xFF, sizeof(page_buf)); |
| 96 | + memcpy(page_buf, buf, copy_size); |
| 97 | + iap_op.cmd = 51; // Erase Sector |
| 98 | + iap_op.par[0] = adr; // Destination |
| 99 | + iap_op.par[1] = (uint32_t)page_buf; // Source |
| 100 | + iap_op.par[2] = 256; // Write size |
| 101 | + iap_op.par[3] = SystemCoreClock / 1000; // Core Clock in kHz |
| 102 | + iap_call(&iap_op); |
| 103 | + if (iap_op.stat != CMD_SUCCESS) { |
| 104 | + iap_unlock(); |
| 105 | + return (1); |
| 106 | + } |
| 107 | + sz -= copy_size; |
| 108 | + adr += copy_size; |
| 109 | + buf += copy_size / 4; |
| 110 | + } |
| 111 | + |
| 112 | + iap_unlock(); |
| 113 | + |
| 114 | + return 0; // Success |
| 115 | +} |
| 116 | + |
| 117 | +static uint32_t get_sector_num(uint32_t addr) |
| 118 | +{ |
| 119 | + return addr / 0x1000; |
| 120 | +} |
0 commit comments