![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
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) 2016 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 | |
kadonotakashi | 0:8fdf9a60065b | 17 | #ifndef MBED_DATAFLASH_BLOCK_DEVICE_H |
kadonotakashi | 0:8fdf9a60065b | 18 | #define MBED_DATAFLASH_BLOCK_DEVICE_H |
kadonotakashi | 0:8fdf9a60065b | 19 | |
kadonotakashi | 0:8fdf9a60065b | 20 | #include <mbed.h> |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "BlockDevice.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | |
kadonotakashi | 0:8fdf9a60065b | 23 | |
kadonotakashi | 0:8fdf9a60065b | 24 | /** BlockDevice for DataFlash flash devices |
kadonotakashi | 0:8fdf9a60065b | 25 | * |
kadonotakashi | 0:8fdf9a60065b | 26 | * @code |
kadonotakashi | 0:8fdf9a60065b | 27 | * // Here's an example using the AT45DB on the K64F |
kadonotakashi | 0:8fdf9a60065b | 28 | * #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 29 | * #include "DataFlashBlockDevice.h" |
kadonotakashi | 0:8fdf9a60065b | 30 | * |
kadonotakashi | 0:8fdf9a60065b | 31 | * // Create DataFlash on SPI bus with PTE5 as chip select |
kadonotakashi | 0:8fdf9a60065b | 32 | * DataFlashBlockDevice dataflash(PTE2, PTE4, PTE1, PTE5); |
kadonotakashi | 0:8fdf9a60065b | 33 | * |
kadonotakashi | 0:8fdf9a60065b | 34 | * // Create DataFlash on SPI bus with PTE6 as write-protect |
kadonotakashi | 0:8fdf9a60065b | 35 | * DataFlashBlockDevice dataflash2(PTE2, PTE4, PTE1, PTE5, PTE6); |
kadonotakashi | 0:8fdf9a60065b | 36 | * |
kadonotakashi | 0:8fdf9a60065b | 37 | * int main() { |
kadonotakashi | 0:8fdf9a60065b | 38 | * printf("dataflash test\n"); |
kadonotakashi | 0:8fdf9a60065b | 39 | * |
kadonotakashi | 0:8fdf9a60065b | 40 | * // Initialize the SPI flash device and print the memory layout |
kadonotakashi | 0:8fdf9a60065b | 41 | * dataflash.init(); |
kadonotakashi | 0:8fdf9a60065b | 42 | * printf("dataflash size: %llu\n", dataflash.size()); |
kadonotakashi | 0:8fdf9a60065b | 43 | * printf("dataflash read size: %llu\n", dataflash.get_read_size()); |
kadonotakashi | 0:8fdf9a60065b | 44 | * printf("dataflash program size: %llu\n", dataflash.get_program_size()); |
kadonotakashi | 0:8fdf9a60065b | 45 | * printf("dataflash erase size: %llu\n", dataflash.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 46 | * |
kadonotakashi | 0:8fdf9a60065b | 47 | * // Write "Hello World!" to the first block |
kadonotakashi | 0:8fdf9a60065b | 48 | * char *buffer = (char*)malloc(dataflash.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 49 | * sprintf(buffer, "Hello World!\n"); |
kadonotakashi | 0:8fdf9a60065b | 50 | * dataflash.erase(0, dataflash.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 51 | * dataflash.program(buffer, 0, dataflash.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 52 | * |
kadonotakashi | 0:8fdf9a60065b | 53 | * // Read back what was stored |
kadonotakashi | 0:8fdf9a60065b | 54 | * dataflash.read(buffer, 0, dataflash.get_erase_size()); |
kadonotakashi | 0:8fdf9a60065b | 55 | * printf("%s", buffer); |
kadonotakashi | 0:8fdf9a60065b | 56 | * |
kadonotakashi | 0:8fdf9a60065b | 57 | * // Deinitialize the device |
kadonotakashi | 0:8fdf9a60065b | 58 | * dataflash.deinit(); |
kadonotakashi | 0:8fdf9a60065b | 59 | * } |
kadonotakashi | 0:8fdf9a60065b | 60 | * @endcode |
kadonotakashi | 0:8fdf9a60065b | 61 | */ |
kadonotakashi | 0:8fdf9a60065b | 62 | class DataFlashBlockDevice : public BlockDevice { |
kadonotakashi | 0:8fdf9a60065b | 63 | public: |
kadonotakashi | 0:8fdf9a60065b | 64 | /** Creates a DataFlashBlockDevice on a SPI bus specified by pins |
kadonotakashi | 0:8fdf9a60065b | 65 | * |
kadonotakashi | 0:8fdf9a60065b | 66 | * @param mosi SPI master out, slave in pin |
kadonotakashi | 0:8fdf9a60065b | 67 | * @param miso SPI master in, slave out pin |
kadonotakashi | 0:8fdf9a60065b | 68 | * @param sclk SPI clock pin |
kadonotakashi | 0:8fdf9a60065b | 69 | * @param csel SPI chip select pin |
kadonotakashi | 0:8fdf9a60065b | 70 | * @param nowp GPIO not-write-protect |
kadonotakashi | 0:8fdf9a60065b | 71 | * @param freq Clock speed of the SPI bus (defaults to 40MHz) |
kadonotakashi | 0:8fdf9a60065b | 72 | */ |
kadonotakashi | 0:8fdf9a60065b | 73 | DataFlashBlockDevice(PinName mosi, |
kadonotakashi | 0:8fdf9a60065b | 74 | PinName miso, |
kadonotakashi | 0:8fdf9a60065b | 75 | PinName sclk, |
kadonotakashi | 0:8fdf9a60065b | 76 | PinName csel, |
kadonotakashi | 0:8fdf9a60065b | 77 | int freq = MBED_CONF_DATAFLASH_SPI_FREQ, |
kadonotakashi | 0:8fdf9a60065b | 78 | PinName nowp = NC); |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | /** Initialize a block device |
kadonotakashi | 0:8fdf9a60065b | 81 | * |
kadonotakashi | 0:8fdf9a60065b | 82 | * @return 0 on success or a negative error code on failure |
kadonotakashi | 0:8fdf9a60065b | 83 | */ |
kadonotakashi | 0:8fdf9a60065b | 84 | virtual int init(); |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | /** Deinitialize a block device |
kadonotakashi | 0:8fdf9a60065b | 87 | * |
kadonotakashi | 0:8fdf9a60065b | 88 | * @return 0 on success or a negative error code on failure |
kadonotakashi | 0:8fdf9a60065b | 89 | */ |
kadonotakashi | 0:8fdf9a60065b | 90 | virtual int deinit(); |
kadonotakashi | 0:8fdf9a60065b | 91 | |
kadonotakashi | 0:8fdf9a60065b | 92 | /** Read blocks from a block device |
kadonotakashi | 0:8fdf9a60065b | 93 | * |
kadonotakashi | 0:8fdf9a60065b | 94 | * @param buffer Buffer to write blocks to |
kadonotakashi | 0:8fdf9a60065b | 95 | * @param addr Address of block to begin reading from |
kadonotakashi | 0:8fdf9a60065b | 96 | * @param size Size to read in bytes, must be a multiple of read block size |
kadonotakashi | 0:8fdf9a60065b | 97 | * @return 0 on success, negative error code on failure |
kadonotakashi | 0:8fdf9a60065b | 98 | */ |
kadonotakashi | 0:8fdf9a60065b | 99 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 100 | |
kadonotakashi | 0:8fdf9a60065b | 101 | /** Program blocks to a block device |
kadonotakashi | 0:8fdf9a60065b | 102 | * |
kadonotakashi | 0:8fdf9a60065b | 103 | * The blocks must have been erased prior to being programmed |
kadonotakashi | 0:8fdf9a60065b | 104 | * |
kadonotakashi | 0:8fdf9a60065b | 105 | * @param buffer Buffer of data to write to blocks |
kadonotakashi | 0:8fdf9a60065b | 106 | * @param addr Address of block to begin writing to |
kadonotakashi | 0:8fdf9a60065b | 107 | * @param size Size to write in bytes, must be a multiple of program block size |
kadonotakashi | 0:8fdf9a60065b | 108 | * @return 0 on success, negative error code on failure |
kadonotakashi | 0:8fdf9a60065b | 109 | */ |
kadonotakashi | 0:8fdf9a60065b | 110 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 111 | |
kadonotakashi | 0:8fdf9a60065b | 112 | /** Erase blocks on a block device |
kadonotakashi | 0:8fdf9a60065b | 113 | * |
kadonotakashi | 0:8fdf9a60065b | 114 | * The state of an erased block is undefined until it has been programmed |
kadonotakashi | 0:8fdf9a60065b | 115 | * |
kadonotakashi | 0:8fdf9a60065b | 116 | * @param addr Address of block to begin erasing |
kadonotakashi | 0:8fdf9a60065b | 117 | * @param size Size to erase in bytes, must be a multiple of erase block size |
kadonotakashi | 0:8fdf9a60065b | 118 | * @return 0 on success, negative error code on failure |
kadonotakashi | 0:8fdf9a60065b | 119 | */ |
kadonotakashi | 0:8fdf9a60065b | 120 | virtual int erase(bd_addr_t addr, bd_size_t size); |
kadonotakashi | 0:8fdf9a60065b | 121 | |
kadonotakashi | 0:8fdf9a60065b | 122 | /** Get the size of a readable block |
kadonotakashi | 0:8fdf9a60065b | 123 | * |
kadonotakashi | 0:8fdf9a60065b | 124 | * @return Size of a readable block in bytes |
kadonotakashi | 0:8fdf9a60065b | 125 | */ |
kadonotakashi | 0:8fdf9a60065b | 126 | virtual bd_size_t get_read_size() const; |
kadonotakashi | 0:8fdf9a60065b | 127 | |
kadonotakashi | 0:8fdf9a60065b | 128 | /** Get the size of a programable block |
kadonotakashi | 0:8fdf9a60065b | 129 | * |
kadonotakashi | 0:8fdf9a60065b | 130 | * @return Size of a programable block in bytes |
kadonotakashi | 0:8fdf9a60065b | 131 | * @note Must be a multiple of the read size |
kadonotakashi | 0:8fdf9a60065b | 132 | */ |
kadonotakashi | 0:8fdf9a60065b | 133 | virtual bd_size_t get_program_size() const; |
kadonotakashi | 0:8fdf9a60065b | 134 | |
kadonotakashi | 0:8fdf9a60065b | 135 | /** Get the size of a eraseable block |
kadonotakashi | 0:8fdf9a60065b | 136 | * |
kadonotakashi | 0:8fdf9a60065b | 137 | * @return Size of a eraseable block in bytes |
kadonotakashi | 0:8fdf9a60065b | 138 | * @note Must be a multiple of the program size |
kadonotakashi | 0:8fdf9a60065b | 139 | */ |
kadonotakashi | 0:8fdf9a60065b | 140 | virtual bd_size_t get_erase_size() const; |
kadonotakashi | 0:8fdf9a60065b | 141 | |
kadonotakashi | 0:8fdf9a60065b | 142 | /** Get the size of an erasable block given address |
kadonotakashi | 0:8fdf9a60065b | 143 | * |
kadonotakashi | 0:8fdf9a60065b | 144 | * @param addr Address within the erasable block |
kadonotakashi | 0:8fdf9a60065b | 145 | * @return Size of an erasable block in bytes |
kadonotakashi | 0:8fdf9a60065b | 146 | * @note Must be a multiple of the program size |
kadonotakashi | 0:8fdf9a60065b | 147 | */ |
kadonotakashi | 0:8fdf9a60065b | 148 | virtual bd_size_t get_erase_size(bd_addr_t addr) const; |
kadonotakashi | 0:8fdf9a60065b | 149 | |
kadonotakashi | 0:8fdf9a60065b | 150 | /** Get the total size of the underlying device |
kadonotakashi | 0:8fdf9a60065b | 151 | * |
kadonotakashi | 0:8fdf9a60065b | 152 | * @return Size of the underlying device in bytes |
kadonotakashi | 0:8fdf9a60065b | 153 | */ |
kadonotakashi | 0:8fdf9a60065b | 154 | virtual bd_size_t size() const; |
kadonotakashi | 0:8fdf9a60065b | 155 | |
kadonotakashi | 0:8fdf9a60065b | 156 | private: |
kadonotakashi | 0:8fdf9a60065b | 157 | // Master side hardware |
kadonotakashi | 0:8fdf9a60065b | 158 | SPI _spi; |
kadonotakashi | 0:8fdf9a60065b | 159 | DigitalOut _cs; |
kadonotakashi | 0:8fdf9a60065b | 160 | DigitalOut _nwp; |
kadonotakashi | 0:8fdf9a60065b | 161 | |
kadonotakashi | 0:8fdf9a60065b | 162 | // Device configuration |
kadonotakashi | 0:8fdf9a60065b | 163 | uint32_t _device_size; |
kadonotakashi | 0:8fdf9a60065b | 164 | uint16_t _page_size; |
kadonotakashi | 0:8fdf9a60065b | 165 | uint16_t _block_size; |
kadonotakashi | 0:8fdf9a60065b | 166 | bool _is_initialized; |
kadonotakashi | 0:8fdf9a60065b | 167 | uint32_t _init_ref_count; |
kadonotakashi | 0:8fdf9a60065b | 168 | |
kadonotakashi | 0:8fdf9a60065b | 169 | // Internal functions |
kadonotakashi | 0:8fdf9a60065b | 170 | uint16_t _get_register(uint8_t opcode); |
kadonotakashi | 0:8fdf9a60065b | 171 | void _write_command(uint32_t command, const uint8_t *buffer, uint32_t size); |
kadonotakashi | 0:8fdf9a60065b | 172 | void _write_enable(bool enable); |
kadonotakashi | 0:8fdf9a60065b | 173 | int _sync(void); |
kadonotakashi | 0:8fdf9a60065b | 174 | int _write_page(const uint8_t *buffer, uint32_t addr, uint32_t offset, uint32_t size); |
kadonotakashi | 0:8fdf9a60065b | 175 | uint32_t _translate_address(bd_addr_t addr); |
kadonotakashi | 0:8fdf9a60065b | 176 | }; |
kadonotakashi | 0:8fdf9a60065b | 177 | |
kadonotakashi | 0:8fdf9a60065b | 178 | |
kadonotakashi | 0:8fdf9a60065b | 179 | #endif /* MBED_DATAFLASH_BLOCK_DEVICE_H */ |