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 FlashPrg.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) 2009-2016, 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 "FlashOS.h" // FlashOS Structures
Pawel Zarembski 0:01f31e923fe2 23 #include "fsl_flash.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "string.h"
Pawel Zarembski 0:01f31e923fe2 25 #include "cortex_m.h"
Pawel Zarembski 0:01f31e923fe2 26
Pawel Zarembski 0:01f31e923fe2 27 flash_config_t g_flash; //!< Storage for flash driver.
Pawel Zarembski 0:01f31e923fe2 28
Pawel Zarembski 0:01f31e923fe2 29 uint32_t Init(uint32_t adr, uint32_t clk, uint32_t fnc)
Pawel Zarembski 0:01f31e923fe2 30 {
Pawel Zarembski 0:01f31e923fe2 31 cortex_int_state_t state = cortex_int_get_and_disable();
Pawel Zarembski 0:01f31e923fe2 32 #if defined (WDOG)
Pawel Zarembski 0:01f31e923fe2 33 /* Write 0xC520 to the unlock register */
Pawel Zarembski 0:01f31e923fe2 34 WDOG->UNLOCK = 0xC520;
Pawel Zarembski 0:01f31e923fe2 35 /* Followed by 0xD928 to complete the unlock */
Pawel Zarembski 0:01f31e923fe2 36 WDOG->UNLOCK = 0xD928;
Pawel Zarembski 0:01f31e923fe2 37 /* Clear the WDOGEN bit to disable the watchdog */
Pawel Zarembski 0:01f31e923fe2 38 WDOG->STCTRLH &= ~WDOG_STCTRLH_WDOGEN_MASK;
Pawel Zarembski 0:01f31e923fe2 39 #else
Pawel Zarembski 0:01f31e923fe2 40 SIM->COPC = 0x00u;
Pawel Zarembski 0:01f31e923fe2 41 #endif
Pawel Zarembski 0:01f31e923fe2 42 cortex_int_restore(state);
Pawel Zarembski 0:01f31e923fe2 43
Pawel Zarembski 0:01f31e923fe2 44 return (FLASH_Init(&g_flash) != kStatus_Success);
Pawel Zarembski 0:01f31e923fe2 45 }
Pawel Zarembski 0:01f31e923fe2 46
Pawel Zarembski 0:01f31e923fe2 47
Pawel Zarembski 0:01f31e923fe2 48 /*
Pawel Zarembski 0:01f31e923fe2 49 * De-Initialize Flash Programming Functions
Pawel Zarembski 0:01f31e923fe2 50 * Parameter: fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify)
Pawel Zarembski 0:01f31e923fe2 51 * Return Value: 0 - OK, 1 - Failed
Pawel Zarembski 0:01f31e923fe2 52 */
Pawel Zarembski 0:01f31e923fe2 53
Pawel Zarembski 0:01f31e923fe2 54 uint32_t UnInit(uint32_t fnc)
Pawel Zarembski 0:01f31e923fe2 55 {
Pawel Zarembski 0:01f31e923fe2 56 return (0);
Pawel Zarembski 0:01f31e923fe2 57 }
Pawel Zarembski 0:01f31e923fe2 58
Pawel Zarembski 0:01f31e923fe2 59
Pawel Zarembski 0:01f31e923fe2 60 /* Blank Check Block in Flash Memory
Pawel Zarembski 0:01f31e923fe2 61 * Parameter: adr: Block Start Address
Pawel Zarembski 0:01f31e923fe2 62 * sz: Block Size (in bytes)
Pawel Zarembski 0:01f31e923fe2 63 * pat: Block Pattern
Pawel Zarembski 0:01f31e923fe2 64 * Return Value: 0 - OK, 1 - Failed
Pawel Zarembski 0:01f31e923fe2 65 */
Pawel Zarembski 0:01f31e923fe2 66
Pawel Zarembski 0:01f31e923fe2 67 // int BlankCheck (unsigned long adr, unsigned long sz, unsigned char pat)
Pawel Zarembski 0:01f31e923fe2 68 // {
Pawel Zarembski 0:01f31e923fe2 69 // return (flash_verify_erase(&g_flash, adr, sz, kFlashMargin_Normal) != kStatus_Success);
Pawel Zarembski 0:01f31e923fe2 70 // }
Pawel Zarembski 0:01f31e923fe2 71 //
Pawel Zarembski 0:01f31e923fe2 72 // /*
Pawel Zarembski 0:01f31e923fe2 73 // * Verify Flash Contents
Pawel Zarembski 0:01f31e923fe2 74 // * Parameter: adr: Start Address
Pawel Zarembski 0:01f31e923fe2 75 // * sz: Size (in bytes)
Pawel Zarembski 0:01f31e923fe2 76 // * buf: Data
Pawel Zarembski 0:01f31e923fe2 77 // * Return Value: (adr+sz) - OK, Failed Address
Pawel Zarembski 0:01f31e923fe2 78 // */
Pawel Zarembski 0:01f31e923fe2 79 // unsigned long Verify (unsigned long adr, unsigned long sz, unsigned char *buf)
Pawel Zarembski 0:01f31e923fe2 80 // {
Pawel Zarembski 0:01f31e923fe2 81 // uint32_t failedAddress;
Pawel Zarembski 0:01f31e923fe2 82 // status_t status = flash_verify_program(&g_flash, adr, sz,
Pawel Zarembski 0:01f31e923fe2 83 // (const uint8_t *)buf, kFlashMargin_Normal,
Pawel Zarembski 0:01f31e923fe2 84 // &failedAddress, NULL);
Pawel Zarembski 0:01f31e923fe2 85 //
Pawel Zarembski 0:01f31e923fe2 86 // if (status == kStatus_Success)
Pawel Zarembski 0:01f31e923fe2 87 // {
Pawel Zarembski 0:01f31e923fe2 88 // // Finished without Errors
Pawel Zarembski 0:01f31e923fe2 89 // return (adr+sz);
Pawel Zarembski 0:01f31e923fe2 90 // }
Pawel Zarembski 0:01f31e923fe2 91 // else
Pawel Zarembski 0:01f31e923fe2 92 // {
Pawel Zarembski 0:01f31e923fe2 93 // return failedAddress;
Pawel Zarembski 0:01f31e923fe2 94 // }
Pawel Zarembski 0:01f31e923fe2 95 // }
Pawel Zarembski 0:01f31e923fe2 96
Pawel Zarembski 0:01f31e923fe2 97 /*
Pawel Zarembski 0:01f31e923fe2 98 * Erase complete Flash Memory
Pawel Zarembski 0:01f31e923fe2 99 * Return Value: 0 - OK, 1 - Failed
Pawel Zarembski 0:01f31e923fe2 100 */
Pawel Zarembski 0:01f31e923fe2 101 uint32_t EraseChip(void)
Pawel Zarembski 0:01f31e923fe2 102 {
Pawel Zarembski 0:01f31e923fe2 103 cortex_int_state_t state = cortex_int_get_and_disable();
Pawel Zarembski 0:01f31e923fe2 104 int status = FLASH_EraseAll(&g_flash, kFLASH_apiEraseKey);
Pawel Zarembski 0:01f31e923fe2 105 if (status == kStatus_Success)
Pawel Zarembski 0:01f31e923fe2 106 {
Pawel Zarembski 0:01f31e923fe2 107 status = FLASH_VerifyEraseAll(&g_flash, kFLASH_marginValueNormal);
Pawel Zarembski 0:01f31e923fe2 108 }
Pawel Zarembski 0:01f31e923fe2 109 cortex_int_restore(state);
Pawel Zarembski 0:01f31e923fe2 110 return status;
Pawel Zarembski 0:01f31e923fe2 111 }
Pawel Zarembski 0:01f31e923fe2 112
Pawel Zarembski 0:01f31e923fe2 113 /*
Pawel Zarembski 0:01f31e923fe2 114 * Erase Sector in Flash Memory
Pawel Zarembski 0:01f31e923fe2 115 * Parameter: adr: Sector Address
Pawel Zarembski 0:01f31e923fe2 116 * Return Value: 0 - OK, 1 - Failed
Pawel Zarembski 0:01f31e923fe2 117 */
Pawel Zarembski 0:01f31e923fe2 118 uint32_t EraseSector(uint32_t adr)
Pawel Zarembski 0:01f31e923fe2 119 {
Pawel Zarembski 0:01f31e923fe2 120 cortex_int_state_t state = cortex_int_get_and_disable();
Pawel Zarembski 0:01f31e923fe2 121 int status = FLASH_Erase(&g_flash, adr, g_flash.PFlashSectorSize, kFLASH_apiEraseKey);
Pawel Zarembski 0:01f31e923fe2 122 if (status == kStatus_Success)
Pawel Zarembski 0:01f31e923fe2 123 {
Pawel Zarembski 0:01f31e923fe2 124 status = FLASH_VerifyErase(&g_flash, adr, g_flash.PFlashSectorSize, kFLASH_marginValueNormal);
Pawel Zarembski 0:01f31e923fe2 125 }
Pawel Zarembski 0:01f31e923fe2 126 cortex_int_restore(state);
Pawel Zarembski 0:01f31e923fe2 127 return status;
Pawel Zarembski 0:01f31e923fe2 128 }
Pawel Zarembski 0:01f31e923fe2 129
Pawel Zarembski 0:01f31e923fe2 130 /*
Pawel Zarembski 0:01f31e923fe2 131 * Program Page in Flash Memory
Pawel Zarembski 0:01f31e923fe2 132 * Parameter: adr: Page Start Address
Pawel Zarembski 0:01f31e923fe2 133 * sz: Page Size
Pawel Zarembski 0:01f31e923fe2 134 * buf: Page Data
Pawel Zarembski 0:01f31e923fe2 135 * Return Value: 0 - OK, 1 - Failed
Pawel Zarembski 0:01f31e923fe2 136 */
Pawel Zarembski 0:01f31e923fe2 137 uint32_t ProgramPage(uint32_t adr, uint32_t sz, uint32_t *buf)
Pawel Zarembski 0:01f31e923fe2 138 {
Pawel Zarembski 0:01f31e923fe2 139 cortex_int_state_t state = cortex_int_get_and_disable();
Pawel Zarembski 0:01f31e923fe2 140 int status = FLASH_Program(&g_flash, adr, buf, sz);
Pawel Zarembski 0:01f31e923fe2 141 if (status == kStatus_Success)
Pawel Zarembski 0:01f31e923fe2 142 {
Pawel Zarembski 0:01f31e923fe2 143 // Must use kFlashMargin_User, or kFlashMargin_Factory for verify program
Pawel Zarembski 0:01f31e923fe2 144 status = FLASH_VerifyProgram(&g_flash, adr, sz,
Pawel Zarembski 0:01f31e923fe2 145 buf, kFLASH_marginValueUser,
Pawel Zarembski 0:01f31e923fe2 146 NULL, NULL);
Pawel Zarembski 0:01f31e923fe2 147 }
Pawel Zarembski 0:01f31e923fe2 148 cortex_int_restore(state);
Pawel Zarembski 0:01f31e923fe2 149 return status;
Pawel Zarembski 0:01f31e923fe2 150 }
Pawel Zarembski 0:01f31e923fe2 151