Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of gr-peach-opencv-project-sd-card by
SPIFBlockDevice.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2016 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_SPIF_BLOCK_DEVICE_H 00017 #define MBED_SPIF_BLOCK_DEVICE_H 00018 00019 /* If the target has no SPI support then SPIF is not supported */ 00020 #ifdef DEVICE_SPI 00021 00022 #include <mbed.h> 00023 #include "BlockDevice.h" 00024 00025 00026 /** BlockDevice for SPI based flash devices 00027 * such as the MX25R or SST26F016B 00028 * 00029 * @code 00030 * #include "mbed.h" 00031 * #include "SPIFBlockDevice.h" 00032 * 00033 * // Create mx25r on SPI bus with PTE5 as chip select 00034 * SPIFBlockDevice mx25r(PTE2, PTE4, PTE1, PTE5); 00035 * 00036 * int main() { 00037 * printf("mx25r test\n"); 00038 * mx52r.init(); 00039 * printf("mx25r size: %llu\n", mx25r.size()); 00040 * printf("mx25r read size: %llu\n", mx25r.get_read_size()); 00041 * printf("mx25r program size: %llu\n", mx25r.get_program_size()); 00042 * printf("mx25r erase size: %llu\n", mx25r.get_erase_size()); 00043 * 00044 * uint8_t *buffer = malloc(mx25r.get_erase_size()); 00045 * sprintf(buffer, "Hello World!\n"); 00046 * mx25r.erase(0, mx25r.get_erase_size()); 00047 * mx25r.program(buffer, 0, mx25r.get_erase_size()); 00048 * mx25r.read(buffer, 0, mx25r.get_erase_size()); 00049 * printf("%s", buffer); 00050 * 00051 * mx25r.deinit(); 00052 * } 00053 */ 00054 class SPIFBlockDevice : public BlockDevice 00055 { 00056 public: 00057 /** Creates a SPIFBlockDevice on a SPI bus specified by pins 00058 * 00059 * @param mosi SPI master out, slave in pin 00060 * @param miso SPI master in, slave out pin 00061 * @param sclk SPI clock pin 00062 * @param csel SPI chip select pin 00063 * @param freq Clock speed of the SPI bus (defaults to 40MHz) 00064 */ 00065 SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq=4000000); 00066 00067 /** Initialize a block device 00068 * 00069 * @return 0 on success or a negative error code on failure 00070 */ 00071 virtual int init(); 00072 00073 /** Deinitialize a block device 00074 * 00075 * @return 0 on success or a negative error code on failure 00076 */ 00077 virtual int deinit(); 00078 00079 /** Read blocks from a block device 00080 * 00081 * @param buffer Buffer to write blocks to 00082 * @param addr Address of block to begin reading from 00083 * @param size Size to read in bytes, must be a multiple of read block size 00084 * @return 0 on success, negative error code on failure 00085 */ 00086 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00087 00088 /** Program blocks to a block device 00089 * 00090 * The blocks must have been erased prior to being programmed 00091 * 00092 * @param buffer Buffer of data to write to blocks 00093 * @param addr Address of block to begin writing to 00094 * @param size Size to write in bytes, must be a multiple of program block size 00095 * @return 0 on success, negative error code on failure 00096 */ 00097 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00098 00099 /** Erase blocks on a block device 00100 * 00101 * The state of an erased block is undefined until it has been programmed 00102 * 00103 * @param addr Address of block to begin erasing 00104 * @param size Size to erase in bytes, must be a multiple of erase block size 00105 * @return 0 on success, negative error code on failure 00106 */ 00107 virtual int erase(bd_addr_t addr, bd_size_t size); 00108 00109 /** Get the size of a readable block 00110 * 00111 * @return Size of a readable block in bytes 00112 */ 00113 virtual bd_size_t get_read_size() const; 00114 00115 /** Get the size of a programable block 00116 * 00117 * @return Size of a programable block in bytes 00118 * @note Must be a multiple of the read size 00119 */ 00120 virtual bd_size_t get_program_size() const; 00121 00122 /** Get the size of a eraseable block 00123 * 00124 * @return Size of a eraseable block in bytes 00125 * @note Must be a multiple of the program size 00126 */ 00127 virtual bd_size_t get_erase_size() const; 00128 00129 /** Get the total size of the underlying device 00130 * 00131 * @return Size of the underlying device in bytes 00132 */ 00133 virtual bd_size_t size() const; 00134 00135 private: 00136 // Master side hardware 00137 SPI _spi; 00138 DigitalOut _cs; 00139 00140 // Device configuration discovered through sfdp 00141 bd_size_t _size; 00142 00143 // Internal functions 00144 int _wren(); 00145 int _sync(); 00146 void _cmdread(uint8_t op, uint32_t addrc, uint32_t retc, 00147 uint32_t addr, uint8_t *rets); 00148 void _cmdwrite(uint8_t op, uint32_t addrc, uint32_t argc, 00149 uint32_t addr, const uint8_t *args); 00150 }; 00151 00152 00153 #endif /* DEVICE_SPI */ 00154 00155 #endif /* MBED_SPIF_BLOCK_DEVICE_H */
Generated on Tue Jul 12 2022 14:47:37 by
1.7.2
