![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.h@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2018 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | #ifndef MBED_SPIF_BLOCK_DEVICE_H |
kadonotakashi | 0:8fdf9a60065b | 17 | #define MBED_SPIF_BLOCK_DEVICE_H |
kadonotakashi | 0:8fdf9a60065b | 18 | |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "SPI.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "DigitalOut.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "BlockDevice.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | |
kadonotakashi | 0:8fdf9a60065b | 23 | /** Enum spif standard error codes |
kadonotakashi | 0:8fdf9a60065b | 24 | * |
kadonotakashi | 0:8fdf9a60065b | 25 | * @enum spif_bd_error |
kadonotakashi | 0:8fdf9a60065b | 26 | */ |
kadonotakashi | 0:8fdf9a60065b | 27 | enum spif_bd_error { |
kadonotakashi | 0:8fdf9a60065b | 28 | SPIF_BD_ERROR_OK = 0, /*!< no error */ |
kadonotakashi | 0:8fdf9a60065b | 29 | SPIF_BD_ERROR_DEVICE_ERROR = BD_ERROR_DEVICE_ERROR, /*!< device specific error -4001 */ |
kadonotakashi | 0:8fdf9a60065b | 30 | SPIF_BD_ERROR_PARSING_FAILED = -4002, /* SFDP Parsing failed */ |
kadonotakashi | 0:8fdf9a60065b | 31 | SPIF_BD_ERROR_READY_FAILED = -4003, /* Wait for Mem Ready failed */ |
kadonotakashi | 0:8fdf9a60065b | 32 | SPIF_BD_ERROR_WREN_FAILED = -4004, /* Write Enable Failed */ |
kadonotakashi | 0:8fdf9a60065b | 33 | SPIF_BD_ERROR_INVALID_ERASE_PARAMS = -4005, /* Erase command not on sector aligned addresses or exceeds device size */ |
kadonotakashi | 0:8fdf9a60065b | 34 | }; |
kadonotakashi | 0:8fdf9a60065b | 35 | |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | #define SPIF_MAX_REGIONS 10 |
kadonotakashi | 0:8fdf9a60065b | 38 | #define MAX_NUM_OF_ERASE_TYPES 4 |
kadonotakashi | 0:8fdf9a60065b | 39 | |
kadonotakashi | 0:8fdf9a60065b | 40 | /** BlockDevice for SFDP based flash devices over SPI bus |
kadonotakashi | 0:8fdf9a60065b | 41 | * |
kadonotakashi | 0:8fdf9a60065b | 42 | * @code |
kadonotakashi | 0:8fdf9a60065b | 43 | * // Here's an example using SPI flash device on K82F target |
kadonotakashi | 0:8fdf9a60065b | 44 | * #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 45 | * #include "SPIFBlockDevice.h" |
kadonotakashi | 0:8fdf9a60065b | 46 | * |
kadonotakashi | 0:8fdf9a60065b | 47 | * // Create flash device on SPI bus with PTE5 as chip select |
kadonotakashi | 0:8fdf9a60065b | 48 | * SPIFBlockDevice spif(PTE2, PTE4, PTE1, PTE5); |
kadonotakashi | 0:8fdf9a60065b | 49 | * |
kadonotakashi | 0:8fdf9a60065b | 50 | * int main() { |
kadonotakashi | 0:8fdf9a60065b | 51 | * printf("spif test\n"); |
kadonotakashi | 0:8fdf9a60065b | 52 | * |
kadonotakashi | 0:8fdf9a60065b | 53 | * // Initialize the SPI flash device and print the memory layout |
kadonotakashi | 0:8fdf9a60065b | 54 | * spif.init(); |
kadonotakashi | 0:8fdf9a60065b | 55 | * printf("spif size: %llu\n", spif.size()); |
kadonotakashi | 0:8fdf9a60065b | 56 | * printf("spif read size: %llu\n", spif.get_read_size()); |
kadonotakashi | 0:8fdf9a60065b | 57 | * printf("spif program size: %llu\n", spif.get_program_size()); |
kadonotakashi | 0:8fdf9a60065b | 58 | * printf("spif erase size: %llu\n", spif.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 59 | * |
kadonotakashi | 0:8fdf9a60065b | 60 | * // Write "Hello World!" to the first block |
kadonotakashi | 0:8fdf9a60065b | 61 | * char *buffer = (char*)malloc(spif.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 62 | * sprintf(buffer, "Hello World!\n"); |
kadonotakashi | 0:8fdf9a60065b | 63 | * spif.erase(0, spif.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 64 | * spif.program(buffer, 0, spif.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 65 | * |
kadonotakashi | 0:8fdf9a60065b | 66 | * // Read back what was stored |
kadonotakashi | 0:8fdf9a60065b | 67 | * spif.read(buffer, 0, spif.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 68 | * printf("%s", buffer); |
kadonotakashi | 0:8fdf9a60065b | 69 | * |
kadonotakashi | 0:8fdf9a60065b | 70 | * // Deinitialize the device |
kadonotakashi | 0:8fdf9a60065b | 71 | * spif.deinit(); |
kadonotakashi | 0:8fdf9a60065b | 72 | * } |
kadonotakashi | 0:8fdf9a60065b | 73 | * @endcode |
kadonotakashi | 0:8fdf9a60065b | 74 | */ |
kadonotakashi | 0:8fdf9a60065b | 75 | class SPIFBlockDevice : public BlockDevice { |
kadonotakashi | 0:8fdf9a60065b | 76 | public: |
kadonotakashi | 0:8fdf9a60065b | 77 | /** Creates a SPIFBlockDevice on a SPI bus specified by pins |
kadonotakashi | 0:8fdf9a60065b | 78 | * |
kadonotakashi | 0:8fdf9a60065b | 79 | * @param mosi SPI master out, slave in pin |
kadonotakashi | 0:8fdf9a60065b | 80 | * @param miso SPI master in, slave out pin |
kadonotakashi | 0:8fdf9a60065b | 81 | * @param sclk SPI clock pin |
kadonotakashi | 0:8fdf9a60065b | 82 | * @param csel SPI chip select pin |
kadonotakashi | 0:8fdf9a60065b | 83 | * @param freq Clock speed of the SPI bus (defaults to 40MHz) |
kadonotakashi | 0:8fdf9a60065b | 84 | */ |
kadonotakashi | 0:8fdf9a60065b | 85 | SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq = 40000000); |
kadonotakashi | 0:8fdf9a60065b | 86 | |
kadonotakashi | 0:8fdf9a60065b | 87 | /** Initialize a block device |
kadonotakashi | 0:8fdf9a60065b | 88 | * |
kadonotakashi | 0:8fdf9a60065b | 89 | * @return SPIF_BD_ERROR_OK(0) - success |
kadonotakashi | 0:8fdf9a60065b | 90 | * SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed |
kadonotakashi | 0:8fdf9a60065b | 91 | * SPIF_BD_ERROR_READY_FAILED - Waiting for Memory ready failed or timedout |
kadonotakashi | 0:8fdf9a60065b | 92 | * SPIF_BD_ERROR_PARSING_FAILED - unexpected format or values in one of the SFDP tables |
kadonotakashi | 0:8fdf9a60065b | 93 | */ |
kadonotakashi | 0:8fdf9a60065b | 94 | virtual int init(); |
kadonotakashi | 0:8fdf9a60065b | 95 | |
kadonotakashi | 0:8fdf9a60065b | 96 | /** Deinitialize a block device |
kadonotakashi | 0:8fdf9a60065b | 97 | * |
kadonotakashi | 0:8fdf9a60065b | 98 | * @return SPIF_BD_ERROR_OK(0) - success |
kadonotakashi | 0:8fdf9a60065b | 99 | */ |
kadonotakashi | 0:8fdf9a60065b | 100 | virtual int deinit(); |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | /** Desctruct SPIFBlockDevie |
kadonotakashi | 0:8fdf9a60065b | 103 | */ |
kadonotakashi | 0:8fdf9a60065b | 104 | ~SPIFBlockDevice() {deinit();} |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | /** Read blocks from a block device |
kadonotakashi | 0:8fdf9a60065b | 107 | * |
kadonotakashi | 0:8fdf9a60065b | 108 | * @param buffer Buffer to write blocks to |
kadonotakashi | 0:8fdf9a60065b | 109 | * @param addr Address of block to begin reading from |
kadonotakashi | 0:8fdf9a60065b | 110 | * @param size Size to read in bytes, must be a multiple of read block size |
kadonotakashi | 0:8fdf9a60065b | 111 | * @return SPIF_BD_ERROR_OK(0) - success |
kadonotakashi | 0:8fdf9a60065b | 112 | * SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed |
kadonotakashi | 0:8fdf9a60065b | 113 | */ |
kadonotakashi | 0:8fdf9a60065b | 114 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 115 | |
kadonotakashi | 0:8fdf9a60065b | 116 | /** Program blocks to a block device |
kadonotakashi | 0:8fdf9a60065b | 117 | * |
kadonotakashi | 0:8fdf9a60065b | 118 | * The blocks must have been erased prior to being programmed |
kadonotakashi | 0:8fdf9a60065b | 119 | * |
kadonotakashi | 0:8fdf9a60065b | 120 | * @param buffer Buffer of data to write to blocks |
kadonotakashi | 0:8fdf9a60065b | 121 | * @param addr Address of block to begin writing to |
kadonotakashi | 0:8fdf9a60065b | 122 | * @param size Size to write in bytes, must be a multiple of program block size |
kadonotakashi | 0:8fdf9a60065b | 123 | * @return SPIF_BD_ERROR_OK(0) - success |
kadonotakashi | 0:8fdf9a60065b | 124 | * SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed |
kadonotakashi | 0:8fdf9a60065b | 125 | * SPIF_BD_ERROR_READY_FAILED - Waiting for Memory ready failed or timed out |
kadonotakashi | 0:8fdf9a60065b | 126 | * SPIF_BD_ERROR_WREN_FAILED - Write Enable failed |
kadonotakashi | 0:8fdf9a60065b | 127 | */ |
kadonotakashi | 0:8fdf9a60065b | 128 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 129 | |
kadonotakashi | 0:8fdf9a60065b | 130 | /** Erase blocks on a block device |
kadonotakashi | 0:8fdf9a60065b | 131 | * |
kadonotakashi | 0:8fdf9a60065b | 132 | * The state of an erased block is undefined until it has been programmed |
kadonotakashi | 0:8fdf9a60065b | 133 | * |
kadonotakashi | 0:8fdf9a60065b | 134 | * @param addr Address of block to begin erasing |
kadonotakashi | 0:8fdf9a60065b | 135 | * @param size Size to erase in bytes, must be a multiple of erase block size |
kadonotakashi | 0:8fdf9a60065b | 136 | * @return SPIF_BD_ERROR_OK(0) - success |
kadonotakashi | 0:8fdf9a60065b | 137 | * SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed |
kadonotakashi | 0:8fdf9a60065b | 138 | * SPIF_BD_ERROR_READY_FAILED - Waiting for Memory ready failed or timed out |
kadonotakashi | 0:8fdf9a60065b | 139 | * SPIF_BD_ERROR_WREN_FAILED - Write Enable failed |
kadonotakashi | 0:8fdf9a60065b | 140 | * SPIF_BD_ERROR_INVALID_ERASE_PARAMS - Trying to erase unaligned address or size |
kadonotakashi | 0:8fdf9a60065b | 141 | */ |
kadonotakashi | 0:8fdf9a60065b | 142 | virtual int erase(bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 143 | |
kadonotakashi | 0:8fdf9a60065b | 144 | /** Get the size of a readable block |
kadonotakashi | 0:8fdf9a60065b | 145 | * |
kadonotakashi | 0:8fdf9a60065b | 146 | * @return Size of a readable block in bytes |
kadonotakashi | 0:8fdf9a60065b | 147 | */ |
kadonotakashi | 0:8fdf9a60065b | 148 | virtual bd_size_t get_read_size() const; |
kadonotakashi | 0:8fdf9a60065b | 149 | |
kadonotakashi | 0:8fdf9a60065b | 150 | /** Get the size of a programable block |
kadonotakashi | 0:8fdf9a60065b | 151 | * |
kadonotakashi | 0:8fdf9a60065b | 152 | * @return Size of a programable block in bytes |
kadonotakashi | 0:8fdf9a60065b | 153 | * @note Must be a multiple of the read size |
kadonotakashi | 0:8fdf9a60065b | 154 | */ |
kadonotakashi | 0:8fdf9a60065b | 155 | virtual bd_size_t get_program_size() const; |
kadonotakashi | 0:8fdf9a60065b | 156 | |
kadonotakashi | 0:8fdf9a60065b | 157 | /** Get the size of a eraseable block |
kadonotakashi | 0:8fdf9a60065b | 158 | * |
kadonotakashi | 0:8fdf9a60065b | 159 | * @return Size of a eraseable block in bytes |
kadonotakashi | 0:8fdf9a60065b | 160 | * @note Must be a multiple of the program size |
kadonotakashi | 0:8fdf9a60065b | 161 | */ |
kadonotakashi | 0:8fdf9a60065b | 162 | virtual bd_size_t get_erase_size() const; |
kadonotakashi | 0:8fdf9a60065b | 163 | |
kadonotakashi | 0:8fdf9a60065b | 164 | /** Get the size of minimal eraseable sector size of given address |
kadonotakashi | 0:8fdf9a60065b | 165 | * |
kadonotakashi | 0:8fdf9a60065b | 166 | * @param addr Any address within block queried for erase sector size (can be any address within flash size offset) |
kadonotakashi | 0:8fdf9a60065b | 167 | * @return Size of minimal erase sector size, in given address region, in bytes |
kadonotakashi | 0:8fdf9a60065b | 168 | * @note Must be a multiple of the program size |
kadonotakashi | 0:8fdf9a60065b | 169 | */ |
kadonotakashi | 0:8fdf9a60065b | 170 | virtual bd_size_t get_erase_size(bd_addr_t addr); |
kadonotakashi | 0:8fdf9a60065b | 171 | |
kadonotakashi | 0:8fdf9a60065b | 172 | /** Get the value of storage byte after it was erased |
kadonotakashi | 0:8fdf9a60065b | 173 | * |
kadonotakashi | 0:8fdf9a60065b | 174 | * If get_erase_value returns a non-negative byte value, the underlying |
kadonotakashi | 0:8fdf9a60065b | 175 | * storage is set to that value when erased, and storage containing |
kadonotakashi | 0:8fdf9a60065b | 176 | * that value can be programmed without another erase. |
kadonotakashi | 0:8fdf9a60065b | 177 | * |
kadonotakashi | 0:8fdf9a60065b | 178 | * @return The value of storage when erased, or -1 if you can't |
kadonotakashi | 0:8fdf9a60065b | 179 | * rely on the value of erased storage |
kadonotakashi | 0:8fdf9a60065b | 180 | */ |
kadonotakashi | 0:8fdf9a60065b | 181 | virtual int get_erase_value() const; |
kadonotakashi | 0:8fdf9a60065b | 182 | |
kadonotakashi | 0:8fdf9a60065b | 183 | /** Get the total size of the underlying device |
kadonotakashi | 0:8fdf9a60065b | 184 | * |
kadonotakashi | 0:8fdf9a60065b | 185 | * @return Size of the underlying device in bytes |
kadonotakashi | 0:8fdf9a60065b | 186 | */ |
kadonotakashi | 0:8fdf9a60065b | 187 | virtual bd_size_t size() const; |
kadonotakashi | 0:8fdf9a60065b | 188 | |
kadonotakashi | 0:8fdf9a60065b | 189 | private: |
kadonotakashi | 0:8fdf9a60065b | 190 | |
kadonotakashi | 0:8fdf9a60065b | 191 | // Internal functions |
kadonotakashi | 0:8fdf9a60065b | 192 | |
kadonotakashi | 0:8fdf9a60065b | 193 | /****************************************/ |
kadonotakashi | 0:8fdf9a60065b | 194 | /* SFDP Detection and Parsing Functions */ |
kadonotakashi | 0:8fdf9a60065b | 195 | /****************************************/ |
kadonotakashi | 0:8fdf9a60065b | 196 | // Parse SFDP Headers and retrieve Basic Param and Sector Map Tables (if exist) |
kadonotakashi | 0:8fdf9a60065b | 197 | int _sfdp_parse_sfdp_headers(uint32_t& basic_table_addr, size_t& basic_table_size, |
kadonotakashi | 0:8fdf9a60065b | 198 | uint32_t& sector_map_table_addr, size_t& sector_map_table_size); |
kadonotakashi | 0:8fdf9a60065b | 199 | |
kadonotakashi | 0:8fdf9a60065b | 200 | // Parse and Detect required Basic Parameters from Table |
kadonotakashi | 0:8fdf9a60065b | 201 | int _sfdp_parse_basic_param_table(uint32_t basic_table_addr, size_t basic_table_size); |
kadonotakashi | 0:8fdf9a60065b | 202 | |
kadonotakashi | 0:8fdf9a60065b | 203 | // Parse and read information required by Regions Secotr Map |
kadonotakashi | 0:8fdf9a60065b | 204 | int _sfdp_parse_sector_map_table(uint32_t sector_map_table_addr, size_t sector_map_table_size); |
kadonotakashi | 0:8fdf9a60065b | 205 | |
kadonotakashi | 0:8fdf9a60065b | 206 | // Detect fastest read Bus mode supported by device |
kadonotakashi | 0:8fdf9a60065b | 207 | int _sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_ptr, int basic_param_table_size, int& read_inst); |
kadonotakashi | 0:8fdf9a60065b | 208 | |
kadonotakashi | 0:8fdf9a60065b | 209 | // Set Page size for program |
kadonotakashi | 0:8fdf9a60065b | 210 | unsigned int _sfdp_detect_page_size(uint8_t *basic_param_table_ptr, int basic_param_table_size); |
kadonotakashi | 0:8fdf9a60065b | 211 | |
kadonotakashi | 0:8fdf9a60065b | 212 | // Detect all supported erase types |
kadonotakashi | 0:8fdf9a60065b | 213 | int _sfdp_detect_erase_types_inst_and_size(uint8_t *basic_param_table_ptr, int basic_param_table_size, |
kadonotakashi | 0:8fdf9a60065b | 214 | int& erase4k_inst, |
kadonotakashi | 0:8fdf9a60065b | 215 | int *erase_type_inst_arr, unsigned int *erase_type_size_arr); |
kadonotakashi | 0:8fdf9a60065b | 216 | |
kadonotakashi | 0:8fdf9a60065b | 217 | /***********************/ |
kadonotakashi | 0:8fdf9a60065b | 218 | /* Utilities Functions */ |
kadonotakashi | 0:8fdf9a60065b | 219 | /***********************/ |
kadonotakashi | 0:8fdf9a60065b | 220 | // Find the region to which the given offset belong to |
kadonotakashi | 0:8fdf9a60065b | 221 | int _utils_find_addr_region(bd_size_t offset); |
kadonotakashi | 0:8fdf9a60065b | 222 | |
kadonotakashi | 0:8fdf9a60065b | 223 | // Iterate on all supported Erase Types of the Region to which the offset belong to. |
kadonotakashi | 0:8fdf9a60065b | 224 | // Iterates from highest type to lowest |
kadonotakashi | 0:8fdf9a60065b | 225 | int _utils_iterate_next_largest_erase_type(uint8_t& bitfield, int size, int offset, int boundry); |
kadonotakashi | 0:8fdf9a60065b | 226 | |
kadonotakashi | 0:8fdf9a60065b | 227 | /********************************/ |
kadonotakashi | 0:8fdf9a60065b | 228 | /* Calls to SPI Driver APIs */ |
kadonotakashi | 0:8fdf9a60065b | 229 | /********************************/ |
kadonotakashi | 0:8fdf9a60065b | 230 | // Send Program => Write command to Driver |
kadonotakashi | 0:8fdf9a60065b | 231 | spif_bd_error _spi_send_program_command(int prog_inst, const void *buffer, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 232 | |
kadonotakashi | 0:8fdf9a60065b | 233 | // Send Read command to Driver |
kadonotakashi | 0:8fdf9a60065b | 234 | //spif_bd_error _spi_send_read_command(uint8_t read_inst, void *buffer, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 235 | spif_bd_error _spi_send_read_command(int read_inst, uint8_t *buffer, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 236 | |
kadonotakashi | 0:8fdf9a60065b | 237 | // Send Erase Instruction using command_transfer command to Driver |
kadonotakashi | 0:8fdf9a60065b | 238 | spif_bd_error _spi_send_erase_command(int erase_inst, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 239 | |
kadonotakashi | 0:8fdf9a60065b | 240 | // Send Generic command_transfer command to Driver |
kadonotakashi | 0:8fdf9a60065b | 241 | spif_bd_error _spi_send_general_command(int instruction, bd_addr_t addr, char *tx_buffer, |
kadonotakashi | 0:8fdf9a60065b | 242 | size_t tx_length, char *rx_buffer, size_t rx_length); |
kadonotakashi | 0:8fdf9a60065b | 243 | |
kadonotakashi | 0:8fdf9a60065b | 244 | // Send set_frequency command to Driver |
kadonotakashi | 0:8fdf9a60065b | 245 | spif_bd_error _spi_set_frequency(int freq); |
kadonotakashi | 0:8fdf9a60065b | 246 | /********************************/ |
kadonotakashi | 0:8fdf9a60065b | 247 | |
kadonotakashi | 0:8fdf9a60065b | 248 | // Soft Reset Flash Memory |
kadonotakashi | 0:8fdf9a60065b | 249 | int _reset_flash_mem(); |
kadonotakashi | 0:8fdf9a60065b | 250 | |
kadonotakashi | 0:8fdf9a60065b | 251 | // Configure Write Enable in Status Register |
kadonotakashi | 0:8fdf9a60065b | 252 | int _set_write_enable(); |
kadonotakashi | 0:8fdf9a60065b | 253 | |
kadonotakashi | 0:8fdf9a60065b | 254 | // Wait on status register until write not-in-progress |
kadonotakashi | 0:8fdf9a60065b | 255 | bool _is_mem_ready(); |
kadonotakashi | 0:8fdf9a60065b | 256 | |
kadonotakashi | 0:8fdf9a60065b | 257 | private: |
kadonotakashi | 0:8fdf9a60065b | 258 | // Master side hardware |
kadonotakashi | 0:8fdf9a60065b | 259 | mbed::SPI _spi; |
kadonotakashi | 0:8fdf9a60065b | 260 | // Enable CS control (low/high) for SPI driver operatios |
kadonotakashi | 0:8fdf9a60065b | 261 | mbed::DigitalOut _cs; |
kadonotakashi | 0:8fdf9a60065b | 262 | |
kadonotakashi | 0:8fdf9a60065b | 263 | // Mutex is used to protect Flash device for some SPI Driver commands that must be done sequentially with no other commands in between |
kadonotakashi | 0:8fdf9a60065b | 264 | // e.g. (1)Set Write Enable, (2)Program, (3)Wait Memory Ready |
kadonotakashi | 0:8fdf9a60065b | 265 | static SingletonPtr<PlatformMutex> _mutex; |
kadonotakashi | 0:8fdf9a60065b | 266 | |
kadonotakashi | 0:8fdf9a60065b | 267 | // Command Instructions |
kadonotakashi | 0:8fdf9a60065b | 268 | int _read_instruction; |
kadonotakashi | 0:8fdf9a60065b | 269 | int _prog_instruction; |
kadonotakashi | 0:8fdf9a60065b | 270 | int _erase_instruction; |
kadonotakashi | 0:8fdf9a60065b | 271 | int _erase4k_inst; // Legacy 4K erase instruction (default 0x20h) |
kadonotakashi | 0:8fdf9a60065b | 272 | |
kadonotakashi | 0:8fdf9a60065b | 273 | // Up To 4 Erase Types are supported by SFDP (each with its own command Instruction and Size) |
kadonotakashi | 0:8fdf9a60065b | 274 | int _erase_type_inst_arr[MAX_NUM_OF_ERASE_TYPES]; |
kadonotakashi | 0:8fdf9a60065b | 275 | unsigned int _erase_type_size_arr[MAX_NUM_OF_ERASE_TYPES]; |
kadonotakashi | 0:8fdf9a60065b | 276 | |
kadonotakashi | 0:8fdf9a60065b | 277 | // Sector Regions Map |
kadonotakashi | 0:8fdf9a60065b | 278 | int _regions_count; //number of regions |
kadonotakashi | 0:8fdf9a60065b | 279 | int _region_size_bytes[SPIF_MAX_REGIONS]; //regions size in bytes |
kadonotakashi | 0:8fdf9a60065b | 280 | bd_size_t _region_high_boundary[SPIF_MAX_REGIONS]; //region high address offset boundary |
kadonotakashi | 0:8fdf9a60065b | 281 | //Each Region can support a bit combination of any of the 4 Erase Types |
kadonotakashi | 0:8fdf9a60065b | 282 | uint8_t _region_erase_types_bitfield[SPIF_MAX_REGIONS]; |
kadonotakashi | 0:8fdf9a60065b | 283 | unsigned int _min_common_erase_size; // minimal common erase size for all regions (0 if none exists) |
kadonotakashi | 0:8fdf9a60065b | 284 | |
kadonotakashi | 0:8fdf9a60065b | 285 | unsigned int _page_size_bytes; // Page size - 256 Bytes default |
kadonotakashi | 0:8fdf9a60065b | 286 | bd_size_t _device_size_bytes; |
kadonotakashi | 0:8fdf9a60065b | 287 | |
kadonotakashi | 0:8fdf9a60065b | 288 | // Bus configuration |
kadonotakashi | 0:8fdf9a60065b | 289 | unsigned int _address_size; // number of bytes for address |
kadonotakashi | 0:8fdf9a60065b | 290 | unsigned int _read_dummy_and_mode_cycles; // Number of Dummy and Mode Bits required by Read Bus Mode |
kadonotakashi | 0:8fdf9a60065b | 291 | unsigned int _write_dummy_and_mode_cycles; // Number of Dummy and Mode Bits required by Write Bus Mode |
kadonotakashi | 0:8fdf9a60065b | 292 | unsigned int _dummy_and_mode_cycles; // Number of Dummy and Mode Bits required by Current Bus Mode |
kadonotakashi | 0:8fdf9a60065b | 293 | uint32_t _init_ref_count; |
kadonotakashi | 0:8fdf9a60065b | 294 | bool _is_initialized; |
kadonotakashi | 0:8fdf9a60065b | 295 | }; |
kadonotakashi | 0:8fdf9a60065b | 296 | |
kadonotakashi | 0:8fdf9a60065b | 297 | #endif /* MBED_SPIF_BLOCK_DEVICE_H */ |