QSPI external flash block device for file system on external flash for DISCO_L475VG_IOT01
Dependencies: BSP_B-L475E-IOT01
QSPIExtFlashBlockDevice/QSPIExtFlashBlockDevice.h@0:0ca082ff5da6, 2018-04-26 (annotated)
- Committer:
- DoronRaifman
- Date:
- Thu Apr 26 09:25:47 2018 +0000
- Revision:
- 0:0ca082ff5da6
- Child:
- 1:2baccc030920
QSPI external flash block device for file system on external flash; for ST DISCO_L475VG_IOT01
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DoronRaifman | 0:0ca082ff5da6 | 1 | /* QSPIExtFlashBlockDevice.h |
DoronRaifman | 0:0ca082ff5da6 | 2 | * QSPI external flash block device to be used for file system on external flash |
DoronRaifman | 0:0ca082ff5da6 | 3 | * for DISCO_L475VG_IOT01 |
DoronRaifman | 0:0ca082ff5da6 | 4 | * |
DoronRaifman | 0:0ca082ff5da6 | 5 | * Doron Raifman, draifman@gmail.com |
DoronRaifman | 0:0ca082ff5da6 | 6 | */ |
DoronRaifman | 0:0ca082ff5da6 | 7 | #ifndef QSPIExtFlashBlockDevice_H |
DoronRaifman | 0:0ca082ff5da6 | 8 | #define QSPIExtFlashBlockDevice_H |
DoronRaifman | 0:0ca082ff5da6 | 9 | |
DoronRaifman | 0:0ca082ff5da6 | 10 | #include <mbed.h> |
DoronRaifman | 0:0ca082ff5da6 | 11 | #include <BlockDevice.h> |
DoronRaifman | 0:0ca082ff5da6 | 12 | |
DoronRaifman | 0:0ca082ff5da6 | 13 | #include "stm32l475e_iot01_qspi.h" |
DoronRaifman | 0:0ca082ff5da6 | 14 | |
DoronRaifman | 0:0ca082ff5da6 | 15 | |
DoronRaifman | 0:0ca082ff5da6 | 16 | #define QSPIExtFlashBlockDeviceTimeout 1000 // Timeout [mSec] |
DoronRaifman | 0:0ca082ff5da6 | 17 | #define QSPIExtFlashBlockDeviceEraseTimeout 10000 // Timeout [0.1 mSec] |
DoronRaifman | 0:0ca082ff5da6 | 18 | #define QSPIExtFlashBlockDevice_VERBOSE 0 // Verbose Debug prints |
DoronRaifman | 0:0ca082ff5da6 | 19 | #define QSPIExtFlashBlockDevice_DEBUG 0 // Debug prints |
DoronRaifman | 0:0ca082ff5da6 | 20 | #define QSPIExtFlashBlockDevice_ERRORS 1 // Errors prints |
DoronRaifman | 0:0ca082ff5da6 | 21 | |
DoronRaifman | 0:0ca082ff5da6 | 22 | // Error codes |
DoronRaifman | 0:0ca082ff5da6 | 23 | #define RC_QSPIExtFlash_OK 0 |
DoronRaifman | 0:0ca082ff5da6 | 24 | #define RC_QSPIExtFlash_ErrTimeout -1 |
DoronRaifman | 0:0ca082ff5da6 | 25 | #define RC_QSPIExtFlash_ErrBadDevice -2 |
DoronRaifman | 0:0ca082ff5da6 | 26 | #define RC_QSPIExtFlash_ErrMemAloc -3 |
DoronRaifman | 0:0ca082ff5da6 | 27 | #define RC_QSPIExtFlash_ErrIO -4 |
DoronRaifman | 0:0ca082ff5da6 | 28 | #define RC_QSPIExtFlash_ErrInit -5 |
DoronRaifman | 0:0ca082ff5da6 | 29 | |
DoronRaifman | 0:0ca082ff5da6 | 30 | |
DoronRaifman | 0:0ca082ff5da6 | 31 | /* |
DoronRaifman | 0:0ca082ff5da6 | 32 | * BlockDevice for QSPI external flash devices on ST DISCO_L476VG |
DoronRaifman | 0:0ca082ff5da6 | 33 | */ |
DoronRaifman | 0:0ca082ff5da6 | 34 | class QSPIExtFlashBlockDevice : public BlockDevice |
DoronRaifman | 0:0ca082ff5da6 | 35 | { |
DoronRaifman | 0:0ca082ff5da6 | 36 | public: |
DoronRaifman | 0:0ca082ff5da6 | 37 | /** Creates a BlockDevice on a QSPI bus |
DoronRaifman | 0:0ca082ff5da6 | 38 | */ |
DoronRaifman | 0:0ca082ff5da6 | 39 | QSPIExtFlashBlockDevice(); |
DoronRaifman | 0:0ca082ff5da6 | 40 | virtual ~QSPIExtFlashBlockDevice(); |
DoronRaifman | 0:0ca082ff5da6 | 41 | |
DoronRaifman | 0:0ca082ff5da6 | 42 | /** Initialize a block device |
DoronRaifman | 0:0ca082ff5da6 | 43 | * |
DoronRaifman | 0:0ca082ff5da6 | 44 | * @return 0 on success or a negative error code on failure |
DoronRaifman | 0:0ca082ff5da6 | 45 | */ |
DoronRaifman | 0:0ca082ff5da6 | 46 | virtual int init(); |
DoronRaifman | 0:0ca082ff5da6 | 47 | |
DoronRaifman | 0:0ca082ff5da6 | 48 | /** Deinitialize a block device |
DoronRaifman | 0:0ca082ff5da6 | 49 | * |
DoronRaifman | 0:0ca082ff5da6 | 50 | * @return 0 on success or a negative error code on failure |
DoronRaifman | 0:0ca082ff5da6 | 51 | */ |
DoronRaifman | 0:0ca082ff5da6 | 52 | virtual int deinit(); |
DoronRaifman | 0:0ca082ff5da6 | 53 | |
DoronRaifman | 0:0ca082ff5da6 | 54 | /** Read blocks from a block device |
DoronRaifman | 0:0ca082ff5da6 | 55 | * |
DoronRaifman | 0:0ca082ff5da6 | 56 | * @param buffer Buffer to write blocks to |
DoronRaifman | 0:0ca082ff5da6 | 57 | * @param addr Address of block to begin reading from |
DoronRaifman | 0:0ca082ff5da6 | 58 | * @param size Size to read in bytes, must be a multiple of read block size |
DoronRaifman | 0:0ca082ff5da6 | 59 | * @return 0 on success, negative error code on failure |
DoronRaifman | 0:0ca082ff5da6 | 60 | */ |
DoronRaifman | 0:0ca082ff5da6 | 61 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
DoronRaifman | 0:0ca082ff5da6 | 62 | |
DoronRaifman | 0:0ca082ff5da6 | 63 | /** Program blocks to a block device |
DoronRaifman | 0:0ca082ff5da6 | 64 | * |
DoronRaifman | 0:0ca082ff5da6 | 65 | * The blocks must have been erased prior to being programmed |
DoronRaifman | 0:0ca082ff5da6 | 66 | * |
DoronRaifman | 0:0ca082ff5da6 | 67 | * @param buffer Buffer of data to write to blocks |
DoronRaifman | 0:0ca082ff5da6 | 68 | * @param addr Address of block to begin writing to |
DoronRaifman | 0:0ca082ff5da6 | 69 | * @param size Size to write in bytes, must be a multiple of program block size |
DoronRaifman | 0:0ca082ff5da6 | 70 | * @return 0 on success, negative error code on failure |
DoronRaifman | 0:0ca082ff5da6 | 71 | */ |
DoronRaifman | 0:0ca082ff5da6 | 72 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
DoronRaifman | 0:0ca082ff5da6 | 73 | |
DoronRaifman | 0:0ca082ff5da6 | 74 | /** Erase blocks on a block device |
DoronRaifman | 0:0ca082ff5da6 | 75 | * |
DoronRaifman | 0:0ca082ff5da6 | 76 | * The state of an erased block is undefined until it has been programmed |
DoronRaifman | 0:0ca082ff5da6 | 77 | * |
DoronRaifman | 0:0ca082ff5da6 | 78 | * @param addr Address of block to begin erasing |
DoronRaifman | 0:0ca082ff5da6 | 79 | * @param size Size to erase in bytes, must be a multiple of erase block size |
DoronRaifman | 0:0ca082ff5da6 | 80 | * @return 0 on success, negative error code on failure |
DoronRaifman | 0:0ca082ff5da6 | 81 | */ |
DoronRaifman | 0:0ca082ff5da6 | 82 | virtual int erase(bd_addr_t addr, bd_size_t size); |
DoronRaifman | 0:0ca082ff5da6 | 83 | |
DoronRaifman | 0:0ca082ff5da6 | 84 | /** Erase all flash memory |
DoronRaifman | 0:0ca082ff5da6 | 85 | * |
DoronRaifman | 0:0ca082ff5da6 | 86 | * @return 0 on success, negative error code on failure |
DoronRaifman | 0:0ca082ff5da6 | 87 | */ |
DoronRaifman | 0:0ca082ff5da6 | 88 | int EraseAllFlashMemory(); |
DoronRaifman | 0:0ca082ff5da6 | 89 | |
DoronRaifman | 0:0ca082ff5da6 | 90 | /** Get the size of a readable block |
DoronRaifman | 0:0ca082ff5da6 | 91 | * |
DoronRaifman | 0:0ca082ff5da6 | 92 | * @return Size of a readable block in bytes |
DoronRaifman | 0:0ca082ff5da6 | 93 | */ |
DoronRaifman | 0:0ca082ff5da6 | 94 | virtual bd_size_t get_read_size() const; |
DoronRaifman | 0:0ca082ff5da6 | 95 | |
DoronRaifman | 0:0ca082ff5da6 | 96 | /** Get the size of a programable block |
DoronRaifman | 0:0ca082ff5da6 | 97 | * |
DoronRaifman | 0:0ca082ff5da6 | 98 | * @return Size of a programable block in bytes |
DoronRaifman | 0:0ca082ff5da6 | 99 | * @note Must be a multiple of the read size |
DoronRaifman | 0:0ca082ff5da6 | 100 | */ |
DoronRaifman | 0:0ca082ff5da6 | 101 | virtual bd_size_t get_program_size() const; |
DoronRaifman | 0:0ca082ff5da6 | 102 | |
DoronRaifman | 0:0ca082ff5da6 | 103 | /** Get the size of a eraseable block |
DoronRaifman | 0:0ca082ff5da6 | 104 | * |
DoronRaifman | 0:0ca082ff5da6 | 105 | * @return Size of a eraseable block in bytes |
DoronRaifman | 0:0ca082ff5da6 | 106 | * @note Must be a multiple of the program size |
DoronRaifman | 0:0ca082ff5da6 | 107 | */ |
DoronRaifman | 0:0ca082ff5da6 | 108 | virtual bd_size_t get_erase_size() const; |
DoronRaifman | 0:0ca082ff5da6 | 109 | |
DoronRaifman | 0:0ca082ff5da6 | 110 | /** Get the total size of the underlying device |
DoronRaifman | 0:0ca082ff5da6 | 111 | * |
DoronRaifman | 0:0ca082ff5da6 | 112 | * @return Size of the underlying device in bytes |
DoronRaifman | 0:0ca082ff5da6 | 113 | */ |
DoronRaifman | 0:0ca082ff5da6 | 114 | virtual bd_size_t size() const; |
DoronRaifman | 0:0ca082ff5da6 | 115 | private: |
DoronRaifman | 0:0ca082ff5da6 | 116 | int _SyncLock() const; |
DoronRaifman | 0:0ca082ff5da6 | 117 | int _SyncUnlock() const; |
DoronRaifman | 0:0ca082ff5da6 | 118 | int _WaitForWrite(const char *ptCaller); |
DoronRaifman | 0:0ca082ff5da6 | 119 | |
DoronRaifman | 0:0ca082ff5da6 | 120 | private: |
DoronRaifman | 0:0ca082ff5da6 | 121 | // Device configuration discovered through init |
DoronRaifman | 0:0ca082ff5da6 | 122 | QSPI_Info m_sQSPIExtFlash_Info; |
DoronRaifman | 0:0ca082ff5da6 | 123 | }; |
DoronRaifman | 0:0ca082ff5da6 | 124 | |
DoronRaifman | 0:0ca082ff5da6 | 125 | |
DoronRaifman | 0:0ca082ff5da6 | 126 | #endif /* QSPIExtFlashBlockDevice_H */ |