TEST
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
Drivers/SPIBlockDevice/SPIFBlockDevice.h@1:f60eafbf009a, 2019-04-10 (annotated)
- Committer:
- gmehmet
- Date:
- Wed Apr 10 14:56:25 2019 +0300
- Revision:
- 1:f60eafbf009a
upload from local
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gmehmet | 1:f60eafbf009a | 1 | /* mbed Microcontroller Library |
gmehmet | 1:f60eafbf009a | 2 | * Copyright (c) 2016 ARM Limited |
gmehmet | 1:f60eafbf009a | 3 | * |
gmehmet | 1:f60eafbf009a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
gmehmet | 1:f60eafbf009a | 5 | * you may not use this file except in compliance with the License. |
gmehmet | 1:f60eafbf009a | 6 | * You may obtain a copy of the License at |
gmehmet | 1:f60eafbf009a | 7 | * |
gmehmet | 1:f60eafbf009a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
gmehmet | 1:f60eafbf009a | 9 | * |
gmehmet | 1:f60eafbf009a | 10 | * Unless required by applicable law or agreed to in writing, software |
gmehmet | 1:f60eafbf009a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
gmehmet | 1:f60eafbf009a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
gmehmet | 1:f60eafbf009a | 13 | * See the License for the specific language governing permissions and |
gmehmet | 1:f60eafbf009a | 14 | * limitations under the License. |
gmehmet | 1:f60eafbf009a | 15 | */ |
gmehmet | 1:f60eafbf009a | 16 | #ifndef MBED_SPIF_BLOCK_DEVICE_H |
gmehmet | 1:f60eafbf009a | 17 | #define MBED_SPIF_BLOCK_DEVICE_H |
gmehmet | 1:f60eafbf009a | 18 | |
gmehmet | 1:f60eafbf009a | 19 | #include <mbed.h> |
gmehmet | 1:f60eafbf009a | 20 | #include "BlockDevice.h" |
gmehmet | 1:f60eafbf009a | 21 | |
gmehmet | 1:f60eafbf009a | 22 | |
gmehmet | 1:f60eafbf009a | 23 | /** BlockDevice for SPI based flash devices |
gmehmet | 1:f60eafbf009a | 24 | * such as the MX25R or SST26F016B |
gmehmet | 1:f60eafbf009a | 25 | * |
gmehmet | 1:f60eafbf009a | 26 | * @code |
gmehmet | 1:f60eafbf009a | 27 | * // Here's an example using the MX25R SPI flash device on the K82F |
gmehmet | 1:f60eafbf009a | 28 | * #include "mbed.h" |
gmehmet | 1:f60eafbf009a | 29 | * #include "SPIFBlockDevice.h" |
gmehmet | 1:f60eafbf009a | 30 | * |
gmehmet | 1:f60eafbf009a | 31 | * // Create flash device on SPI bus with PTE5 as chip select |
gmehmet | 1:f60eafbf009a | 32 | * SPIFBlockDevice spif(PTE2, PTE4, PTE1, PTE5); |
gmehmet | 1:f60eafbf009a | 33 | * |
gmehmet | 1:f60eafbf009a | 34 | * int main() { |
gmehmet | 1:f60eafbf009a | 35 | * printf("spif test\n"); |
gmehmet | 1:f60eafbf009a | 36 | * |
gmehmet | 1:f60eafbf009a | 37 | * // Initialize the SPI flash device and print the memory layout |
gmehmet | 1:f60eafbf009a | 38 | * spif.init(); |
gmehmet | 1:f60eafbf009a | 39 | * printf("spif size: %llu\n", spif.size()); |
gmehmet | 1:f60eafbf009a | 40 | * printf("spif read size: %llu\n", spif.get_read_size()); |
gmehmet | 1:f60eafbf009a | 41 | * printf("spif program size: %llu\n", spif.get_program_size()); |
gmehmet | 1:f60eafbf009a | 42 | * printf("spif erase size: %llu\n", spif.get_erase_size()); |
gmehmet | 1:f60eafbf009a | 43 | * |
gmehmet | 1:f60eafbf009a | 44 | * // Write "Hello World!" to the first block |
gmehmet | 1:f60eafbf009a | 45 | * char *buffer = (char*)malloc(spif.get_erase_size()); |
gmehmet | 1:f60eafbf009a | 46 | * sprintf(buffer, "Hello World!\n"); |
gmehmet | 1:f60eafbf009a | 47 | * spif.erase(0, spif.get_erase_size()); |
gmehmet | 1:f60eafbf009a | 48 | * spif.program(buffer, 0, spif.get_erase_size()); |
gmehmet | 1:f60eafbf009a | 49 | * |
gmehmet | 1:f60eafbf009a | 50 | * // Read back what was stored |
gmehmet | 1:f60eafbf009a | 51 | * spif.read(buffer, 0, spif.get_erase_size()); |
gmehmet | 1:f60eafbf009a | 52 | * printf("%s", buffer); |
gmehmet | 1:f60eafbf009a | 53 | * |
gmehmet | 1:f60eafbf009a | 54 | * // Deinitialize the device |
gmehmet | 1:f60eafbf009a | 55 | * spif.deinit(); |
gmehmet | 1:f60eafbf009a | 56 | * } |
gmehmet | 1:f60eafbf009a | 57 | * @endcode |
gmehmet | 1:f60eafbf009a | 58 | */ |
gmehmet | 1:f60eafbf009a | 59 | class SPIFBlockDevice : public BlockDevice { |
gmehmet | 1:f60eafbf009a | 60 | public: |
gmehmet | 1:f60eafbf009a | 61 | /** Creates a SPIFBlockDevice on a SPI bus specified by pins |
gmehmet | 1:f60eafbf009a | 62 | * |
gmehmet | 1:f60eafbf009a | 63 | * @param mosi SPI master out, slave in pin |
gmehmet | 1:f60eafbf009a | 64 | * @param miso SPI master in, slave out pin |
gmehmet | 1:f60eafbf009a | 65 | * @param sclk SPI clock pin |
gmehmet | 1:f60eafbf009a | 66 | * @param csel SPI chip select pin |
gmehmet | 1:f60eafbf009a | 67 | * @param freq Clock speed of the SPI bus (defaults to 40MHz) |
gmehmet | 1:f60eafbf009a | 68 | */ |
gmehmet | 1:f60eafbf009a | 69 | SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq=40000000); |
gmehmet | 1:f60eafbf009a | 70 | |
gmehmet | 1:f60eafbf009a | 71 | /** Initialize a block device |
gmehmet | 1:f60eafbf009a | 72 | * |
gmehmet | 1:f60eafbf009a | 73 | * @return 0 on success or a negative error code on failure |
gmehmet | 1:f60eafbf009a | 74 | */ |
gmehmet | 1:f60eafbf009a | 75 | virtual int init(); |
gmehmet | 1:f60eafbf009a | 76 | |
gmehmet | 1:f60eafbf009a | 77 | /** Deinitialize a block device |
gmehmet | 1:f60eafbf009a | 78 | * |
gmehmet | 1:f60eafbf009a | 79 | * @return 0 on success or a negative error code on failure |
gmehmet | 1:f60eafbf009a | 80 | */ |
gmehmet | 1:f60eafbf009a | 81 | virtual int deinit(); |
gmehmet | 1:f60eafbf009a | 82 | |
gmehmet | 1:f60eafbf009a | 83 | /** Read blocks from a block device |
gmehmet | 1:f60eafbf009a | 84 | * |
gmehmet | 1:f60eafbf009a | 85 | * @param buffer Buffer to write blocks to |
gmehmet | 1:f60eafbf009a | 86 | * @param addr Address of block to begin reading from |
gmehmet | 1:f60eafbf009a | 87 | * @param size Size to read in bytes, must be a multiple of read block size |
gmehmet | 1:f60eafbf009a | 88 | * @return 0 on success, negative error code on failure |
gmehmet | 1:f60eafbf009a | 89 | */ |
gmehmet | 1:f60eafbf009a | 90 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
gmehmet | 1:f60eafbf009a | 91 | |
gmehmet | 1:f60eafbf009a | 92 | /** Program blocks to a block device |
gmehmet | 1:f60eafbf009a | 93 | * |
gmehmet | 1:f60eafbf009a | 94 | * The blocks must have been erased prior to being programmed |
gmehmet | 1:f60eafbf009a | 95 | * |
gmehmet | 1:f60eafbf009a | 96 | * @param buffer Buffer of data to write to blocks |
gmehmet | 1:f60eafbf009a | 97 | * @param addr Address of block to begin writing to |
gmehmet | 1:f60eafbf009a | 98 | * @param size Size to write in bytes, must be a multiple of program block size |
gmehmet | 1:f60eafbf009a | 99 | * @return 0 on success, negative error code on failure |
gmehmet | 1:f60eafbf009a | 100 | */ |
gmehmet | 1:f60eafbf009a | 101 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
gmehmet | 1:f60eafbf009a | 102 | |
gmehmet | 1:f60eafbf009a | 103 | /** Erase blocks on a block device |
gmehmet | 1:f60eafbf009a | 104 | * |
gmehmet | 1:f60eafbf009a | 105 | * The state of an erased block is undefined until it has been programmed |
gmehmet | 1:f60eafbf009a | 106 | * |
gmehmet | 1:f60eafbf009a | 107 | * @param addr Address of block to begin erasing |
gmehmet | 1:f60eafbf009a | 108 | * @param size Size to erase in bytes, must be a multiple of erase block size |
gmehmet | 1:f60eafbf009a | 109 | * @return 0 on success, negative error code on failure |
gmehmet | 1:f60eafbf009a | 110 | */ |
gmehmet | 1:f60eafbf009a | 111 | virtual int erase(bd_addr_t addr, bd_size_t size); |
gmehmet | 1:f60eafbf009a | 112 | |
gmehmet | 1:f60eafbf009a | 113 | /** Get the size of a readable block |
gmehmet | 1:f60eafbf009a | 114 | * |
gmehmet | 1:f60eafbf009a | 115 | * @return Size of a readable block in bytes |
gmehmet | 1:f60eafbf009a | 116 | */ |
gmehmet | 1:f60eafbf009a | 117 | virtual bd_size_t get_read_size() const; |
gmehmet | 1:f60eafbf009a | 118 | |
gmehmet | 1:f60eafbf009a | 119 | /** Get the size of a programable block |
gmehmet | 1:f60eafbf009a | 120 | * |
gmehmet | 1:f60eafbf009a | 121 | * @return Size of a programable block in bytes |
gmehmet | 1:f60eafbf009a | 122 | * @note Must be a multiple of the read size |
gmehmet | 1:f60eafbf009a | 123 | */ |
gmehmet | 1:f60eafbf009a | 124 | virtual bd_size_t get_program_size() const; |
gmehmet | 1:f60eafbf009a | 125 | |
gmehmet | 1:f60eafbf009a | 126 | /** Get the size of a eraseable block |
gmehmet | 1:f60eafbf009a | 127 | * |
gmehmet | 1:f60eafbf009a | 128 | * @return Size of a eraseable block in bytes |
gmehmet | 1:f60eafbf009a | 129 | * @note Must be a multiple of the program size |
gmehmet | 1:f60eafbf009a | 130 | */ |
gmehmet | 1:f60eafbf009a | 131 | virtual bd_size_t get_erase_size() const; |
gmehmet | 1:f60eafbf009a | 132 | |
gmehmet | 1:f60eafbf009a | 133 | /** Get the total size of the underlying device |
gmehmet | 1:f60eafbf009a | 134 | * |
gmehmet | 1:f60eafbf009a | 135 | * @return Size of the underlying device in bytes |
gmehmet | 1:f60eafbf009a | 136 | */ |
gmehmet | 1:f60eafbf009a | 137 | virtual bd_size_t size() const; |
gmehmet | 1:f60eafbf009a | 138 | |
gmehmet | 1:f60eafbf009a | 139 | private: |
gmehmet | 1:f60eafbf009a | 140 | // Master side hardware |
gmehmet | 1:f60eafbf009a | 141 | SPI _spi; |
gmehmet | 1:f60eafbf009a | 142 | DigitalOut _cs; |
gmehmet | 1:f60eafbf009a | 143 | |
gmehmet | 1:f60eafbf009a | 144 | // Device configuration discovered through sfdp |
gmehmet | 1:f60eafbf009a | 145 | bd_size_t _size; |
gmehmet | 1:f60eafbf009a | 146 | |
gmehmet | 1:f60eafbf009a | 147 | // Internal functions |
gmehmet | 1:f60eafbf009a | 148 | int _wren(); |
gmehmet | 1:f60eafbf009a | 149 | int _sync(); |
gmehmet | 1:f60eafbf009a | 150 | void _cmdread(uint8_t op, uint32_t addrc, uint32_t retc, |
gmehmet | 1:f60eafbf009a | 151 | uint32_t addr, uint8_t *rets); |
gmehmet | 1:f60eafbf009a | 152 | void _cmdwrite(uint8_t op, uint32_t addrc, uint32_t argc, |
gmehmet | 1:f60eafbf009a | 153 | uint32_t addr, const uint8_t *args); |
gmehmet | 1:f60eafbf009a | 154 | }; |
gmehmet | 1:f60eafbf009a | 155 | |
gmehmet | 1:f60eafbf009a | 156 | |
gmehmet | 1:f60eafbf009a | 157 | #endif /* MBED_SPIF_BLOCK_DEVICE_H */ |