helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Apr 20 11:31:35 2018 +0100
Revision:
186:9c2029bfadbe
Parent:
185:08ed48f1de7f
Update to latest version of mbed lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 185:08ed48f1de7f 1 /* mbed Microcontroller Library
AnnaBridge 185:08ed48f1de7f 2 * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved
AnnaBridge 185:08ed48f1de7f 3 *
AnnaBridge 185:08ed48f1de7f 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 185:08ed48f1de7f 5 * you may not use this file except in compliance with the License.
AnnaBridge 185:08ed48f1de7f 6 * You may obtain a copy of the License at
AnnaBridge 185:08ed48f1de7f 7 *
AnnaBridge 185:08ed48f1de7f 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 185:08ed48f1de7f 9 *
AnnaBridge 185:08ed48f1de7f 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 185:08ed48f1de7f 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 185:08ed48f1de7f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 185:08ed48f1de7f 13 * See the License for the specific language governing permissions and
AnnaBridge 185:08ed48f1de7f 14 * limitations under the License.
AnnaBridge 185:08ed48f1de7f 15 */
AnnaBridge 185:08ed48f1de7f 16
AnnaBridge 185:08ed48f1de7f 17 #include "flash_api.h"
AnnaBridge 185:08ed48f1de7f 18 #include "mbed_critical.h"
AnnaBridge 185:08ed48f1de7f 19 #include "tmpm46b_fc.h"
AnnaBridge 185:08ed48f1de7f 20
AnnaBridge 185:08ed48f1de7f 21 #define PROGRAM_WIRTE_MAX 16U /* Page program could be written 16 bytes/4 words once */
AnnaBridge 185:08ed48f1de7f 22 #define SECTOR_SIZE 0x8000 /* (512 * 8) sectors */
AnnaBridge 185:08ed48f1de7f 23
AnnaBridge 185:08ed48f1de7f 24 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 25 #define FLASH_API_ROM ((uint32_t *)__section_begin("FLASH_CODE_ROM"))
AnnaBridge 185:08ed48f1de7f 26 #define SIZE_FLASH_API ((uint32_t)__section_size("FLASH_CODE_ROM"))
AnnaBridge 185:08ed48f1de7f 27 #define FLASH_API_RAM ((uint32_t *)__section_begin("FLASH_CODE_RAM"))
AnnaBridge 185:08ed48f1de7f 28 #pragma section = "FLASH_CODE_RAM"
AnnaBridge 185:08ed48f1de7f 29 #pragma section = "FLASH_CODE_ROM"
AnnaBridge 185:08ed48f1de7f 30 #endif
AnnaBridge 185:08ed48f1de7f 31
AnnaBridge 185:08ed48f1de7f 32 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 33 static void Copy_Routine(uint32_t * dest, uint32_t * source, uint32_t size)
AnnaBridge 185:08ed48f1de7f 34 {
AnnaBridge 185:08ed48f1de7f 35 uint32_t *dest_addr, *source_addr, tmpsize;
AnnaBridge 185:08ed48f1de7f 36 uint32_t i, tmps, tmpd, mask;
AnnaBridge 185:08ed48f1de7f 37
AnnaBridge 185:08ed48f1de7f 38 dest_addr = dest;
AnnaBridge 185:08ed48f1de7f 39 source_addr = source;
AnnaBridge 185:08ed48f1de7f 40
AnnaBridge 185:08ed48f1de7f 41 tmpsize = size >> 2U;
AnnaBridge 185:08ed48f1de7f 42 for (i = 0U; i < tmpsize; i++) { /* 32bits copy */
AnnaBridge 185:08ed48f1de7f 43 *dest_addr = *source_addr;
AnnaBridge 185:08ed48f1de7f 44 dest_addr++;
AnnaBridge 185:08ed48f1de7f 45 source_addr++;
AnnaBridge 185:08ed48f1de7f 46 }
AnnaBridge 185:08ed48f1de7f 47 if (size & 0x00000003U) { /* if the last data size is not 0(maybe 1,2 or 3), copy the last data */
AnnaBridge 185:08ed48f1de7f 48 mask = 0xFFFFFF00U;
AnnaBridge 185:08ed48f1de7f 49 i = size & 0x00000003U;
AnnaBridge 185:08ed48f1de7f 50 tmps = *source_addr;
AnnaBridge 185:08ed48f1de7f 51 tmpd = *dest_addr;
AnnaBridge 185:08ed48f1de7f 52 while (i - 1U) {
AnnaBridge 185:08ed48f1de7f 53 mask = mask << 8U;
AnnaBridge 185:08ed48f1de7f 54 i--;
AnnaBridge 185:08ed48f1de7f 55 }
AnnaBridge 185:08ed48f1de7f 56 tmps = tmps & (~mask);
AnnaBridge 185:08ed48f1de7f 57 tmpd = tmpd & (mask);
AnnaBridge 185:08ed48f1de7f 58 *dest_addr = tmps + tmpd; /* 32bits copy, but only change the bytes need to be changed */
AnnaBridge 185:08ed48f1de7f 59 } else {
AnnaBridge 185:08ed48f1de7f 60 /* Do nothing */
AnnaBridge 185:08ed48f1de7f 61 }
AnnaBridge 185:08ed48f1de7f 62 }
AnnaBridge 185:08ed48f1de7f 63 #endif
AnnaBridge 185:08ed48f1de7f 64
AnnaBridge 185:08ed48f1de7f 65 int32_t flash_init(flash_t *obj)
AnnaBridge 185:08ed48f1de7f 66 {
AnnaBridge 185:08ed48f1de7f 67 TSB_FC->WCLKCR = 0x00000004U;
AnnaBridge 185:08ed48f1de7f 68 TSB_FC->PROGCR = 0x00000001U;
AnnaBridge 185:08ed48f1de7f 69 TSB_FC->ERASECR = 0x00000007U;
AnnaBridge 185:08ed48f1de7f 70 TSB_FC->AREASEL = 0x00000000U;
AnnaBridge 185:08ed48f1de7f 71 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 72 Copy_Routine(FLASH_API_RAM, FLASH_API_ROM, SIZE_FLASH_API);
AnnaBridge 185:08ed48f1de7f 73 #endif
AnnaBridge 185:08ed48f1de7f 74 return 0;
AnnaBridge 185:08ed48f1de7f 75 }
AnnaBridge 185:08ed48f1de7f 76
AnnaBridge 185:08ed48f1de7f 77 int32_t flash_free(flash_t *obj)
AnnaBridge 185:08ed48f1de7f 78 {
AnnaBridge 185:08ed48f1de7f 79 return 0;
AnnaBridge 185:08ed48f1de7f 80 }
AnnaBridge 185:08ed48f1de7f 81
AnnaBridge 185:08ed48f1de7f 82 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 83 #pragma location = "FLASH_ROM"
AnnaBridge 185:08ed48f1de7f 84 #endif
AnnaBridge 185:08ed48f1de7f 85 int32_t flash_erase_sector(flash_t *obj, uint32_t address)
AnnaBridge 185:08ed48f1de7f 86 {
AnnaBridge 185:08ed48f1de7f 87 int status = FAIL;
AnnaBridge 185:08ed48f1de7f 88 /* We need to prevent flash accesses during erase operation */
AnnaBridge 185:08ed48f1de7f 89 core_util_critical_section_enter();
AnnaBridge 185:08ed48f1de7f 90
AnnaBridge 185:08ed48f1de7f 91 if (FC_SUCCESS == FC_EraseBlock(address)) {
AnnaBridge 185:08ed48f1de7f 92 status = SUCCESS;
AnnaBridge 185:08ed48f1de7f 93 } else {
AnnaBridge 185:08ed48f1de7f 94 /* Do nothing */
AnnaBridge 185:08ed48f1de7f 95 }
AnnaBridge 185:08ed48f1de7f 96 core_util_critical_section_exit();
AnnaBridge 185:08ed48f1de7f 97 return status;
AnnaBridge 185:08ed48f1de7f 98 }
AnnaBridge 185:08ed48f1de7f 99
AnnaBridge 185:08ed48f1de7f 100 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 101 #pragma location = "FLASH_ROM"
AnnaBridge 185:08ed48f1de7f 102 #endif
AnnaBridge 185:08ed48f1de7f 103 int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
AnnaBridge 185:08ed48f1de7f 104 {
AnnaBridge 185:08ed48f1de7f 105 int status = SUCCESS;
AnnaBridge 185:08ed48f1de7f 106
AnnaBridge 185:08ed48f1de7f 107 /* We need to prevent flash accesses during program operation */
AnnaBridge 185:08ed48f1de7f 108 core_util_critical_section_enter();
AnnaBridge 185:08ed48f1de7f 109 while (size > PROGRAM_WIRTE_MAX) {
AnnaBridge 185:08ed48f1de7f 110 if (FC_SUCCESS == FC_WritePage((uint32_t)address, (uint32_t *)data)) { /* write one page every time */
AnnaBridge 185:08ed48f1de7f 111 /* Do nothing */
AnnaBridge 185:08ed48f1de7f 112 } else {
AnnaBridge 185:08ed48f1de7f 113 status = FAIL;
AnnaBridge 185:08ed48f1de7f 114 break;
AnnaBridge 185:08ed48f1de7f 115 }
AnnaBridge 185:08ed48f1de7f 116 size = size - PROGRAM_WIRTE_MAX;
AnnaBridge 185:08ed48f1de7f 117 address = address + PROGRAM_WIRTE_MAX;
AnnaBridge 185:08ed48f1de7f 118 data = data + PROGRAM_WIRTE_MAX;
AnnaBridge 185:08ed48f1de7f 119 }
AnnaBridge 185:08ed48f1de7f 120 if (FC_SUCCESS == FC_WritePage((uint32_t)address, (uint32_t *)data)) { /* write the last data, no more than one page */
AnnaBridge 185:08ed48f1de7f 121 /* Do nothing */
AnnaBridge 185:08ed48f1de7f 122 } else {
AnnaBridge 185:08ed48f1de7f 123 status = FAIL;
AnnaBridge 185:08ed48f1de7f 124 }
AnnaBridge 185:08ed48f1de7f 125
AnnaBridge 185:08ed48f1de7f 126 core_util_critical_section_exit();
AnnaBridge 185:08ed48f1de7f 127 return status;
AnnaBridge 185:08ed48f1de7f 128 }
AnnaBridge 185:08ed48f1de7f 129
AnnaBridge 185:08ed48f1de7f 130 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 131 #pragma location = "FLASH_ROM"
AnnaBridge 185:08ed48f1de7f 132 #endif
AnnaBridge 185:08ed48f1de7f 133 uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
AnnaBridge 185:08ed48f1de7f 134 {
AnnaBridge 185:08ed48f1de7f 135 if (address >= FLASH_CHIP_SIZE)
AnnaBridge 185:08ed48f1de7f 136 return MBED_FLASH_INVALID_SIZE;
AnnaBridge 185:08ed48f1de7f 137
AnnaBridge 185:08ed48f1de7f 138 return SECTOR_SIZE;
AnnaBridge 185:08ed48f1de7f 139 }
AnnaBridge 185:08ed48f1de7f 140
AnnaBridge 185:08ed48f1de7f 141 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 142 #pragma location = "FLASH_ROM"
AnnaBridge 185:08ed48f1de7f 143 #endif
AnnaBridge 185:08ed48f1de7f 144 uint32_t flash_get_page_size(const flash_t *obj)
AnnaBridge 185:08ed48f1de7f 145 {
AnnaBridge 185:08ed48f1de7f 146 return FLASH_PAGE_SIZE;
AnnaBridge 185:08ed48f1de7f 147 }
AnnaBridge 185:08ed48f1de7f 148
AnnaBridge 185:08ed48f1de7f 149 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 150 #pragma location = "FLASH_ROM"
AnnaBridge 185:08ed48f1de7f 151 #endif
AnnaBridge 185:08ed48f1de7f 152 uint32_t flash_get_start_address(const flash_t *obj)
AnnaBridge 185:08ed48f1de7f 153 {
AnnaBridge 185:08ed48f1de7f 154 return FLASH_START_ADDR;
AnnaBridge 185:08ed48f1de7f 155 }
AnnaBridge 185:08ed48f1de7f 156
AnnaBridge 185:08ed48f1de7f 157 #if defined ( __ICCARM__ ) /* IAR Compiler */
AnnaBridge 185:08ed48f1de7f 158 #pragma location = "FLASH_ROM"
AnnaBridge 185:08ed48f1de7f 159 #endif
AnnaBridge 185:08ed48f1de7f 160 uint32_t flash_get_size(const flash_t *obj)
AnnaBridge 185:08ed48f1de7f 161 {
AnnaBridge 185:08ed48f1de7f 162 return FLASH_CHIP_SIZE;
AnnaBridge 185:08ed48f1de7f 163 }