Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FlashPrg.c Source File

FlashPrg.c

00001 /**
00002  * @file    FlashPrg.c
00003  * @brief
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "FlashOS.h"        // FlashOS Structures
00023 #include "fsl_flash.h"
00024 #include "string.h"
00025 #include "cortex_m.h"
00026 
00027 flash_config_t g_flash; //!< Storage for flash driver.
00028 
00029 uint32_t Init(uint32_t adr, uint32_t clk, uint32_t fnc)
00030 {
00031     cortex_int_state_t state = cortex_int_get_and_disable();
00032 #if defined (WDOG)
00033     /* Write 0xC520 to the unlock register */
00034     WDOG->UNLOCK = 0xC520;
00035     /* Followed by 0xD928 to complete the unlock */
00036     WDOG->UNLOCK = 0xD928;
00037     /* Clear the WDOGEN bit to disable the watchdog */
00038     WDOG->STCTRLH &= ~WDOG_STCTRLH_WDOGEN_MASK;
00039 #else
00040     SIM->COPC = 0x00u;
00041 #endif
00042     cortex_int_restore(state);
00043 
00044     return (FLASH_Init(&g_flash) != kStatus_Success);
00045 }
00046 
00047 
00048 /*
00049  *  De-Initialize Flash Programming Functions
00050  *    Parameter:      fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
00051  *    Return Value:   0 - OK,  1 - Failed
00052  */
00053 
00054 uint32_t UnInit(uint32_t fnc)
00055 {
00056     return (0);
00057 }
00058 
00059 
00060 /*  Blank Check Block in Flash Memory
00061  *    Parameter:      adr:  Block Start Address
00062  *                    sz:   Block Size (in bytes)
00063  *                    pat:  Block Pattern
00064  *    Return Value:   0 - OK,  1 - Failed
00065  */
00066 
00067 // int BlankCheck (unsigned long adr, unsigned long sz, unsigned char pat)
00068 // {
00069 //     return (flash_verify_erase(&g_flash, adr, sz, kFlashMargin_Normal) != kStatus_Success);
00070 // }
00071 //
00072 // /*
00073 //  *  Verify Flash Contents
00074 //  *    Parameter:      adr:  Start Address
00075 //  *                    sz:   Size (in bytes)
00076 //  *                    buf:  Data
00077 //  *    Return Value:   (adr+sz) - OK, Failed Address
00078 //  */
00079 // unsigned long Verify (unsigned long adr, unsigned long sz, unsigned char *buf)
00080 // {
00081 //     uint32_t failedAddress;
00082 //     status_t status = flash_verify_program(&g_flash, adr, sz,
00083 //                               (const uint8_t *)buf, kFlashMargin_Normal,
00084 //                               &failedAddress, NULL);
00085 //
00086 //     if (status == kStatus_Success)
00087 //     {
00088 //         // Finished without Errors
00089 //         return (adr+sz);
00090 //     }
00091 //     else
00092 //     {
00093 //         return failedAddress;
00094 //     }
00095 // }
00096 
00097 /*
00098  *  Erase complete Flash Memory
00099  *    Return Value:   0 - OK,  1 - Failed
00100  */
00101 uint32_t EraseChip(void)
00102 {
00103     cortex_int_state_t state = cortex_int_get_and_disable();
00104     int status = FLASH_EraseAll(&g_flash, kFLASH_apiEraseKey );
00105     if (status == kStatus_Success)
00106     {
00107         status = FLASH_VerifyEraseAll(&g_flash, kFLASH_marginValueNormal );
00108     }
00109     cortex_int_restore(state);
00110     return status;
00111 }
00112 
00113 /*
00114  *  Erase Sector in Flash Memory
00115  *    Parameter:      adr:  Sector Address
00116  *    Return Value:   0 - OK,  1 - Failed
00117  */
00118 uint32_t EraseSector(uint32_t adr)
00119 {
00120     cortex_int_state_t state = cortex_int_get_and_disable();
00121     int status = FLASH_Erase(&g_flash, adr, g_flash.PFlashSectorSize , kFLASH_apiEraseKey );
00122     if (status == kStatus_Success)
00123     {
00124         status = FLASH_VerifyErase(&g_flash, adr, g_flash.PFlashSectorSize , kFLASH_marginValueNormal );
00125     }
00126     cortex_int_restore(state);
00127     return status;
00128 }
00129 
00130 /*
00131  *  Program Page in Flash Memory
00132  *    Parameter:      adr:  Page Start Address
00133  *                    sz:   Page Size
00134  *                    buf:  Page Data
00135  *    Return Value:   0 - OK,  1 - Failed
00136  */
00137 uint32_t ProgramPage(uint32_t adr, uint32_t sz, uint32_t *buf)
00138 {
00139     cortex_int_state_t state = cortex_int_get_and_disable();
00140     int status = FLASH_Program(&g_flash, adr, buf, sz);
00141     if (status == kStatus_Success)
00142     {
00143         // Must use kFlashMargin_User, or kFlashMargin_Factory for verify program
00144         status = FLASH_VerifyProgram(&g_flash, adr, sz,
00145                               buf, kFLASH_marginValueUser ,
00146                               NULL, NULL);
00147     }
00148     cortex_int_restore(state);
00149     return status;
00150 }
00151