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