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

« Back to documentation index

Show/hide line numbers flash.c Source File

flash.c

00001 /**
00002  * @file    flash_hal_stm32f103xb.c
00003  * @brief
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2019, 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 "flash_hal.h"        // FlashOS Structures
00023 #include "target_config.h"    // target_device
00024 #include "stm32f1xx.h"
00025 #include "util.h"
00026 #include "string.h"
00027 #include "target_board.h"
00028 
00029 /*********************************************************************
00030 *
00031 *       Static code
00032 *
00033 **********************************************************************
00034 */
00035 
00036 /*********************************************************************
00037 *
00038 *       Public code
00039 *
00040 **********************************************************************
00041 */
00042 uint32_t Init(uint32_t adr, uint32_t clk, uint32_t fnc)
00043 {
00044     //
00045     // No special init required
00046     //
00047     return (0);
00048 }
00049 
00050 uint32_t UnInit(uint32_t fnc)
00051 {
00052     //
00053     // No special uninit required
00054     //
00055     return (0);
00056 }
00057 
00058 uint32_t EraseChip(void)
00059 {
00060     FLASH_EraseInitTypeDef erase_init;
00061     uint32_t error;
00062     uint32_t ret = 0;  // O.K.
00063     if (g_board_info.target_cfg) {
00064         HAL_FLASH_Unlock();
00065         //bootloader, interface flashing only concerns 1 flash region
00066         util_assert((g_board_info.target_cfg->flash_regions[0].end - g_board_info.target_cfg->flash_regions[0].start) %
00067                     FLASH_PAGE_SIZE == 0);
00068         memset(&erase_init, 0, sizeof(erase_init));
00069         erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
00070         erase_init.PageAddress = g_board_info.target_cfg->flash_regions[0].start;
00071         erase_init.NbPages = (g_board_info.target_cfg->flash_regions[0].end - g_board_info.target_cfg->flash_regions[0].start) % FLASH_PAGE_SIZE;
00072         if (HAL_FLASHEx_Erase(&erase_init, &error) != HAL_OK) {
00073             ret = 1;
00074         }
00075         
00076         HAL_FLASH_Lock();
00077     }else{
00078         ret = 1;
00079     }
00080     return ret;
00081 }
00082 
00083 uint32_t EraseSector(uint32_t adr)
00084 {
00085     FLASH_EraseInitTypeDef erase_init;
00086     uint32_t error;
00087     uint32_t ret = 0;  // O.K.
00088 
00089     HAL_FLASH_Unlock();
00090     
00091     memset(&erase_init, 0, sizeof(erase_init));
00092     erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
00093     erase_init.PageAddress = adr;
00094     erase_init.NbPages = 1;
00095     if (HAL_FLASHEx_Erase(&erase_init, &error) != HAL_OK) {
00096         ret = 1;
00097     }
00098 
00099     HAL_FLASH_Lock();
00100     return ret;
00101 }
00102 
00103 uint32_t ProgramPage(uint32_t adr, uint32_t sz, uint32_t *buf)
00104 {
00105     uint32_t i;
00106     uint32_t ret = 0;  // O.K.
00107 
00108     HAL_FLASH_Unlock();
00109 
00110     util_assert(sz % 4 == 0);
00111     for (i = 0; i < sz / 4; i++) {
00112         if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, adr + i * 4, buf[i]) != HAL_OK) {
00113             ret = 1;
00114             break;
00115         }
00116     }
00117 
00118     HAL_FLASH_Lock();
00119     return ret;
00120 }