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