Interface to the Atmel AT45 series Dataflash with Serial Interface (SPI)
AT45.h@0:2e9d45485414, 2012-01-10 (annotated)
- Committer:
- stjo2809
- Date:
- Tue Jan 10 09:20:38 2012 +0000
- Revision:
- 0:2e9d45485414
First Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stjo2809 | 0:2e9d45485414 | 1 | /* mbed AT45 Library, for driving the Atmel AT45 series Dataflash with Serial Interface (SPI) |
stjo2809 | 0:2e9d45485414 | 2 | * Copyright (c) 2012, Created by Steen Joergensen (stjo2809) inspired by Chris Styles AT45 library |
stjo2809 | 0:2e9d45485414 | 3 | * |
stjo2809 | 0:2e9d45485414 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
stjo2809 | 0:2e9d45485414 | 5 | * of this software and associated documentation files (the "Software"), to deal |
stjo2809 | 0:2e9d45485414 | 6 | * in the Software without restriction, including without limitation the rights |
stjo2809 | 0:2e9d45485414 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
stjo2809 | 0:2e9d45485414 | 8 | * copies of the Software, and to permit persons to whom the Software is |
stjo2809 | 0:2e9d45485414 | 9 | * furnished to do so, subject to the following conditions: |
stjo2809 | 0:2e9d45485414 | 10 | * |
stjo2809 | 0:2e9d45485414 | 11 | * The above copyright notice and this permission notice shall be included in |
stjo2809 | 0:2e9d45485414 | 12 | * all copies or substantial portions of the Software. |
stjo2809 | 0:2e9d45485414 | 13 | * |
stjo2809 | 0:2e9d45485414 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
stjo2809 | 0:2e9d45485414 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
stjo2809 | 0:2e9d45485414 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
stjo2809 | 0:2e9d45485414 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
stjo2809 | 0:2e9d45485414 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
stjo2809 | 0:2e9d45485414 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
stjo2809 | 0:2e9d45485414 | 20 | * THE SOFTWARE. |
stjo2809 | 0:2e9d45485414 | 21 | * |
stjo2809 | 0:2e9d45485414 | 22 | * |
stjo2809 | 0:2e9d45485414 | 23 | * This driver supports 021,041,081,161,321,641 variants of the AT45DBxxx family |
stjo2809 | 0:2e9d45485414 | 24 | * |
stjo2809 | 0:2e9d45485414 | 25 | * || Code || Density || Page size || Pages || Package || |
stjo2809 | 0:2e9d45485414 | 26 | * || 021 || 2 || 256 || 1024 || 8 SOIC || |
stjo2809 | 0:2e9d45485414 | 27 | * || 041 || 4 || 256 || 2048 || 8 SOIC || |
stjo2809 | 0:2e9d45485414 | 28 | * || 081 || 8 || 256 || 4096 || 8 SOIC || |
stjo2809 | 0:2e9d45485414 | 29 | * || 161 || 16 || 512 || 4096 || 8 SOIC || |
stjo2809 | 0:2e9d45485414 | 30 | * || 321 || 32 || 512 || 8192 || 8 SOIC || |
stjo2809 | 0:2e9d45485414 | 31 | * || 641 || 64 || 1024 || 8192 || 28 TSOP || |
stjo2809 | 0:2e9d45485414 | 32 | */ |
stjo2809 | 0:2e9d45485414 | 33 | |
stjo2809 | 0:2e9d45485414 | 34 | #include "mbed.h" |
stjo2809 | 0:2e9d45485414 | 35 | |
stjo2809 | 0:2e9d45485414 | 36 | #ifndef AT45_H |
stjo2809 | 0:2e9d45485414 | 37 | #define AT45_H |
stjo2809 | 0:2e9d45485414 | 38 | |
stjo2809 | 0:2e9d45485414 | 39 | //============================================================================= |
stjo2809 | 0:2e9d45485414 | 40 | // Functions Declaration |
stjo2809 | 0:2e9d45485414 | 41 | //============================================================================= |
stjo2809 | 0:2e9d45485414 | 42 | |
stjo2809 | 0:2e9d45485414 | 43 | /** Interface to the Atmel AT45 series Dataflash with Serial Interface (SPI) |
stjo2809 | 0:2e9d45485414 | 44 | * |
stjo2809 | 0:2e9d45485414 | 45 | * Using the driver: |
stjo2809 | 0:2e9d45485414 | 46 | * - remenber to setup SPI in main routine. |
stjo2809 | 0:2e9d45485414 | 47 | * - remenber to setup Chipselect in main routine. |
stjo2809 | 0:2e9d45485414 | 48 | * - remenber to use FAT functions set pagesize to binary. |
stjo2809 | 0:2e9d45485414 | 49 | * |
stjo2809 | 0:2e9d45485414 | 50 | * Limitations of using this driver: |
stjo2809 | 0:2e9d45485414 | 51 | * - can't use lockdown functions. |
stjo2809 | 0:2e9d45485414 | 52 | * - can't use protections functions. |
stjo2809 | 0:2e9d45485414 | 53 | * - can't use security functions. |
stjo2809 | 0:2e9d45485414 | 54 | * |
stjo2809 | 0:2e9d45485414 | 55 | */ |
stjo2809 | 0:2e9d45485414 | 56 | class AT45 { |
stjo2809 | 0:2e9d45485414 | 57 | public: |
stjo2809 | 0:2e9d45485414 | 58 | /** Create an instance of the AT45 connected to specfied SPI pins, with the specified address. |
stjo2809 | 0:2e9d45485414 | 59 | * |
stjo2809 | 0:2e9d45485414 | 60 | * @param spi The mbed SPI instance (make in main routine) |
stjo2809 | 0:2e9d45485414 | 61 | * @param nCs The SPI chip select pin. |
stjo2809 | 0:2e9d45485414 | 62 | */ |
stjo2809 | 0:2e9d45485414 | 63 | AT45(SPI& spi, PinName ncs); |
stjo2809 | 0:2e9d45485414 | 64 | |
stjo2809 | 0:2e9d45485414 | 65 | /** Read a byte. |
stjo2809 | 0:2e9d45485414 | 66 | * |
stjo2809 | 0:2e9d45485414 | 67 | * @param address The address of the byte to read. |
stjo2809 | 0:2e9d45485414 | 68 | * @return The data in the byte. |
stjo2809 | 0:2e9d45485414 | 69 | */ |
stjo2809 | 0:2e9d45485414 | 70 | char read_byte(int address); |
stjo2809 | 0:2e9d45485414 | 71 | |
stjo2809 | 0:2e9d45485414 | 72 | /** Read a page. |
stjo2809 | 0:2e9d45485414 | 73 | * |
stjo2809 | 0:2e9d45485414 | 74 | * @param data The data is pointer to a userdefined array that the page is read into. |
stjo2809 | 0:2e9d45485414 | 75 | * @param page The page number of the page to read (0 to device page size). |
stjo2809 | 0:2e9d45485414 | 76 | * @return Returns "0" or "-1" for error. |
stjo2809 | 0:2e9d45485414 | 77 | */ |
stjo2809 | 0:2e9d45485414 | 78 | int read_page(char* data, int page); |
stjo2809 | 0:2e9d45485414 | 79 | |
stjo2809 | 0:2e9d45485414 | 80 | /** Read a block (from 1 dimension array). |
stjo2809 | 0:2e9d45485414 | 81 | * |
stjo2809 | 0:2e9d45485414 | 82 | * @param data The data is pointer to a userdefined array that holds 4096 bytes of the data that is read into. |
stjo2809 | 0:2e9d45485414 | 83 | * @param block The block number of the block to read (0 to device block size). |
stjo2809 | 0:2e9d45485414 | 84 | * @return Returns "0" or "-1" for error. |
stjo2809 | 0:2e9d45485414 | 85 | */ |
stjo2809 | 0:2e9d45485414 | 86 | int read_block(char *data, int block); |
stjo2809 | 0:2e9d45485414 | 87 | |
stjo2809 | 0:2e9d45485414 | 88 | /** Read a block (from 2 dimension array). |
stjo2809 | 0:2e9d45485414 | 89 | * |
stjo2809 | 0:2e9d45485414 | 90 | * Then using 2 dimension array, the array shall have a starting point like "read_block(data[0],0)" |
stjo2809 | 0:2e9d45485414 | 91 | * @param data The data is pointer to a userdefined array that holds 8 x 512 bytes of the data that is read into. |
stjo2809 | 0:2e9d45485414 | 92 | * @param block The block number of the block to read (0 to device block size). |
stjo2809 | 0:2e9d45485414 | 93 | * @return Returns "0" or "-1" for error. |
stjo2809 | 0:2e9d45485414 | 94 | */ |
stjo2809 | 0:2e9d45485414 | 95 | int read_block(char *data[], int block); |
stjo2809 | 0:2e9d45485414 | 96 | |
stjo2809 | 0:2e9d45485414 | 97 | /** Write a byte. |
stjo2809 | 0:2e9d45485414 | 98 | * |
stjo2809 | 0:2e9d45485414 | 99 | * @param address The address to where the data is storage in the flash. |
stjo2809 | 0:2e9d45485414 | 100 | * @param data The data to write into the flash. |
stjo2809 | 0:2e9d45485414 | 101 | */ |
stjo2809 | 0:2e9d45485414 | 102 | void write_byte(int address, char data); |
stjo2809 | 0:2e9d45485414 | 103 | |
stjo2809 | 0:2e9d45485414 | 104 | /** Write a page. |
stjo2809 | 0:2e9d45485414 | 105 | * |
stjo2809 | 0:2e9d45485414 | 106 | * @param data The data is pointer to a userdefined array that holds the data to write into. |
stjo2809 | 0:2e9d45485414 | 107 | * @param page The page number of the page to write into (0 to device page size). |
stjo2809 | 0:2e9d45485414 | 108 | * @return Returns "0" or "-1" for error. |
stjo2809 | 0:2e9d45485414 | 109 | */ |
stjo2809 | 0:2e9d45485414 | 110 | int write_page(char* data, int page); |
stjo2809 | 0:2e9d45485414 | 111 | |
stjo2809 | 0:2e9d45485414 | 112 | /** Write a block (from 1 dimension array). |
stjo2809 | 0:2e9d45485414 | 113 | * |
stjo2809 | 0:2e9d45485414 | 114 | * @param data The data is pointer to a userdefined array that holds 4096 bytes of the data to write into. |
stjo2809 | 0:2e9d45485414 | 115 | * @param block The block number of the block to write into (0 to device block size). |
stjo2809 | 0:2e9d45485414 | 116 | * @return Returns "0" or "-1" for error. |
stjo2809 | 0:2e9d45485414 | 117 | */ |
stjo2809 | 0:2e9d45485414 | 118 | int write_block(char *data, int block); |
stjo2809 | 0:2e9d45485414 | 119 | |
stjo2809 | 0:2e9d45485414 | 120 | /** Write a block (from 2 dimension array). |
stjo2809 | 0:2e9d45485414 | 121 | * |
stjo2809 | 0:2e9d45485414 | 122 | * Then using 2 dimension array, the array shall have a starting point like "write_block(data[0],0)" |
stjo2809 | 0:2e9d45485414 | 123 | * @param data The data is pointer to a userdefined array that holds 8 x 512 bytes of the data to write into (remenber it has to be a 2 dimension array). |
stjo2809 | 0:2e9d45485414 | 124 | * @param block The block number of the block to write into (0 to device block size). |
stjo2809 | 0:2e9d45485414 | 125 | * @return Returns "0" or "-1" for error. |
stjo2809 | 0:2e9d45485414 | 126 | */ |
stjo2809 | 0:2e9d45485414 | 127 | int write_block(char *data[], int block); |
stjo2809 | 0:2e9d45485414 | 128 | |
stjo2809 | 0:2e9d45485414 | 129 | /** FAT Read (512 bits). |
stjo2809 | 0:2e9d45485414 | 130 | * |
stjo2809 | 0:2e9d45485414 | 131 | * Remenber to set page size to binary. |
stjo2809 | 0:2e9d45485414 | 132 | * @param data The data is pointer to a userdefined array that the page is read into. |
stjo2809 | 0:2e9d45485414 | 133 | * @param page The page number of the page to read (0 to device page size). |
stjo2809 | 0:2e9d45485414 | 134 | * @return Returns "0". |
stjo2809 | 0:2e9d45485414 | 135 | */ |
stjo2809 | 0:2e9d45485414 | 136 | int FAT_read(char* data, int page); |
stjo2809 | 0:2e9d45485414 | 137 | |
stjo2809 | 0:2e9d45485414 | 138 | /** FAT Write (512bits). |
stjo2809 | 0:2e9d45485414 | 139 | * |
stjo2809 | 0:2e9d45485414 | 140 | * Remenber to set page size to binary. |
stjo2809 | 0:2e9d45485414 | 141 | * @param data The data is pointer to a userdefined array that holds the data to write into. |
stjo2809 | 0:2e9d45485414 | 142 | * @param page The page number of the page to write into (0 to device page size). |
stjo2809 | 0:2e9d45485414 | 143 | * @return Returns "0" or "-1" for error. |
stjo2809 | 0:2e9d45485414 | 144 | */ |
stjo2809 | 0:2e9d45485414 | 145 | int FAT_write(char* data, int page); |
stjo2809 | 0:2e9d45485414 | 146 | |
stjo2809 | 0:2e9d45485414 | 147 | /** Function to erase the entire chip. |
stjo2809 | 0:2e9d45485414 | 148 | * |
stjo2809 | 0:2e9d45485414 | 149 | * Issue: |
stjo2809 | 0:2e9d45485414 | 150 | * In a certain percentage of units, the chip erase feature may not function correctly and may adversely affect device operation. |
stjo2809 | 0:2e9d45485414 | 151 | * Therefore, it is recommended that the chip erase commands (opcodes C7H, 94H, 80H, and 9AH) not be used. |
stjo2809 | 0:2e9d45485414 | 152 | * Workaround: |
stjo2809 | 0:2e9d45485414 | 153 | * Use block erase (opcode 50H) as an alternative. The block erase function is not affected by the chip erase issue. |
stjo2809 | 0:2e9d45485414 | 154 | * Resolution: |
stjo2809 | 0:2e9d45485414 | 155 | * The chip erase feature may be fixed with a new revision of the device. Please contact Atmel for the estimated availability of |
stjo2809 | 0:2e9d45485414 | 156 | * devices with the fix. |
stjo2809 | 0:2e9d45485414 | 157 | */ |
stjo2809 | 0:2e9d45485414 | 158 | void chip_erase(void); |
stjo2809 | 0:2e9d45485414 | 159 | |
stjo2809 | 0:2e9d45485414 | 160 | /** Function to erase the selected block. |
stjo2809 | 0:2e9d45485414 | 161 | * |
stjo2809 | 0:2e9d45485414 | 162 | * @param block The selected block to erase. |
stjo2809 | 0:2e9d45485414 | 163 | */ |
stjo2809 | 0:2e9d45485414 | 164 | void block_erase(int block); |
stjo2809 | 0:2e9d45485414 | 165 | |
stjo2809 | 0:2e9d45485414 | 166 | /** Function to erase the selected block. |
stjo2809 | 0:2e9d45485414 | 167 | * |
stjo2809 | 0:2e9d45485414 | 168 | * @param page The number of the page to erase. |
stjo2809 | 0:2e9d45485414 | 169 | */ |
stjo2809 | 0:2e9d45485414 | 170 | void page_erase(int page); |
stjo2809 | 0:2e9d45485414 | 171 | |
stjo2809 | 0:2e9d45485414 | 172 | /** Device size in mbits. |
stjo2809 | 0:2e9d45485414 | 173 | * |
stjo2809 | 0:2e9d45485414 | 174 | * @return device size. |
stjo2809 | 0:2e9d45485414 | 175 | */ |
stjo2809 | 0:2e9d45485414 | 176 | int device_size(void); |
stjo2809 | 0:2e9d45485414 | 177 | |
stjo2809 | 0:2e9d45485414 | 178 | /** Pages in flash. |
stjo2809 | 0:2e9d45485414 | 179 | * |
stjo2809 | 0:2e9d45485414 | 180 | * @return Numbers af pages. |
stjo2809 | 0:2e9d45485414 | 181 | */ |
stjo2809 | 0:2e9d45485414 | 182 | int pages(void); |
stjo2809 | 0:2e9d45485414 | 183 | |
stjo2809 | 0:2e9d45485414 | 184 | /** Page size. |
stjo2809 | 0:2e9d45485414 | 185 | * |
stjo2809 | 0:2e9d45485414 | 186 | * for 2-8 Mbits 256 or 264 |
stjo2809 | 0:2e9d45485414 | 187 | * for 16-32 Mbits 512 or 528 |
stjo2809 | 0:2e9d45485414 | 188 | * for 64 Mbits 1024 or 1056 |
stjo2809 | 0:2e9d45485414 | 189 | * |
stjo2809 | 0:2e9d45485414 | 190 | * @return Page size. |
stjo2809 | 0:2e9d45485414 | 191 | */ |
stjo2809 | 0:2e9d45485414 | 192 | int pagesize(void); |
stjo2809 | 0:2e9d45485414 | 193 | |
stjo2809 | 0:2e9d45485414 | 194 | /** Function to set the page size to binary. |
stjo2809 | 0:2e9d45485414 | 195 | * |
stjo2809 | 0:2e9d45485414 | 196 | * Remenber is a one-time programmable configuration. |
stjo2809 | 0:2e9d45485414 | 197 | * Remenber to total power down after the functions has been run. |
stjo2809 | 0:2e9d45485414 | 198 | */ |
stjo2809 | 0:2e9d45485414 | 199 | void set_pageszie_to_binary(void); |
stjo2809 | 0:2e9d45485414 | 200 | |
stjo2809 | 0:2e9d45485414 | 201 | /** blocks in flash. |
stjo2809 | 0:2e9d45485414 | 202 | * |
stjo2809 | 0:2e9d45485414 | 203 | * @return Numbers af blocks. |
stjo2809 | 0:2e9d45485414 | 204 | */ |
stjo2809 | 0:2e9d45485414 | 205 | int blocks(void); |
stjo2809 | 0:2e9d45485414 | 206 | |
stjo2809 | 0:2e9d45485414 | 207 | /** ID of the device. |
stjo2809 | 0:2e9d45485414 | 208 | * |
stjo2809 | 0:2e9d45485414 | 209 | * @return Manufacturer, Family and Density code. |
stjo2809 | 0:2e9d45485414 | 210 | */ |
stjo2809 | 0:2e9d45485414 | 211 | int id(void); |
stjo2809 | 0:2e9d45485414 | 212 | |
stjo2809 | 0:2e9d45485414 | 213 | /** Status register. |
stjo2809 | 0:2e9d45485414 | 214 | * |
stjo2809 | 0:2e9d45485414 | 215 | * @return The status register. |
stjo2809 | 0:2e9d45485414 | 216 | */ |
stjo2809 | 0:2e9d45485414 | 217 | int status(void); // Status register |
stjo2809 | 0:2e9d45485414 | 218 | |
stjo2809 | 0:2e9d45485414 | 219 | /** busy ?. |
stjo2809 | 0:2e9d45485414 | 220 | * |
stjo2809 | 0:2e9d45485414 | 221 | * Function will want to the device is not busy. |
stjo2809 | 0:2e9d45485414 | 222 | */ |
stjo2809 | 0:2e9d45485414 | 223 | void busy(void); // Wait until Flash is not busy |
stjo2809 | 0:2e9d45485414 | 224 | |
stjo2809 | 0:2e9d45485414 | 225 | /** Deep Power Down. |
stjo2809 | 0:2e9d45485414 | 226 | * |
stjo2809 | 0:2e9d45485414 | 227 | * Remenber that you have to want 35uS after the wake up to use the device. |
stjo2809 | 0:2e9d45485414 | 228 | * @param True = Activate and False = Wake Up. |
stjo2809 | 0:2e9d45485414 | 229 | */ |
stjo2809 | 0:2e9d45485414 | 230 | void deep_power_down(bool onoff); |
stjo2809 | 0:2e9d45485414 | 231 | |
stjo2809 | 0:2e9d45485414 | 232 | /** Is the device deep power down. |
stjo2809 | 0:2e9d45485414 | 233 | * |
stjo2809 | 0:2e9d45485414 | 234 | * @return True = Activate and False = Awake. |
stjo2809 | 0:2e9d45485414 | 235 | */ |
stjo2809 | 0:2e9d45485414 | 236 | bool is_it_awake(void); |
stjo2809 | 0:2e9d45485414 | 237 | |
stjo2809 | 0:2e9d45485414 | 238 | private: |
stjo2809 | 0:2e9d45485414 | 239 | |
stjo2809 | 0:2e9d45485414 | 240 | SPI _spi; |
stjo2809 | 0:2e9d45485414 | 241 | DigitalOut _ncs; |
stjo2809 | 0:2e9d45485414 | 242 | |
stjo2809 | 0:2e9d45485414 | 243 | int _pages; // Integer number of pages |
stjo2809 | 0:2e9d45485414 | 244 | int _pagesize; // page size, in bytes |
stjo2809 | 0:2e9d45485414 | 245 | int _devicesize; // device size in bytes |
stjo2809 | 0:2e9d45485414 | 246 | int _blocks; // Number of blocks |
stjo2809 | 0:2e9d45485414 | 247 | bool _deep_down; // True = the device is deep down |
stjo2809 | 0:2e9d45485414 | 248 | bool _deep_down_onoff; // variable for deep power down function (On/Off) |
stjo2809 | 0:2e9d45485414 | 249 | |
stjo2809 | 0:2e9d45485414 | 250 | // Helper routunes |
stjo2809 | 0:2e9d45485414 | 251 | void _initialize(); |
stjo2809 | 0:2e9d45485414 | 252 | void _select(); |
stjo2809 | 0:2e9d45485414 | 253 | void _deselect(); |
stjo2809 | 0:2e9d45485414 | 254 | void _busy (void); |
stjo2809 | 0:2e9d45485414 | 255 | |
stjo2809 | 0:2e9d45485414 | 256 | // accessing SRAM buffers |
stjo2809 | 0:2e9d45485414 | 257 | void _sramwrite (int buffer, int address, int data); |
stjo2809 | 0:2e9d45485414 | 258 | int _sramread (int buffer, int address); |
stjo2809 | 0:2e9d45485414 | 259 | |
stjo2809 | 0:2e9d45485414 | 260 | // Transferring SRAM buffers to/from FLASH |
stjo2809 | 0:2e9d45485414 | 261 | void _flashwrite (int buffer, int paddr); |
stjo2809 | 0:2e9d45485414 | 262 | void _flashread (int buffer, int paddr); |
stjo2809 | 0:2e9d45485414 | 263 | |
stjo2809 | 0:2e9d45485414 | 264 | // Reading FLASH directly |
stjo2809 | 0:2e9d45485414 | 265 | int _memread (int address); |
stjo2809 | 0:2e9d45485414 | 266 | |
stjo2809 | 0:2e9d45485414 | 267 | // Calculate page/subpage addresses |
stjo2809 | 0:2e9d45485414 | 268 | int _getpaddr (int); |
stjo2809 | 0:2e9d45485414 | 269 | int _getbaddr (int); |
stjo2809 | 0:2e9d45485414 | 270 | |
stjo2809 | 0:2e9d45485414 | 271 | // Send 3 byte address |
stjo2809 | 0:2e9d45485414 | 272 | void _sendaddr (int address); |
stjo2809 | 0:2e9d45485414 | 273 | |
stjo2809 | 0:2e9d45485414 | 274 | }; |
stjo2809 | 0:2e9d45485414 | 275 | #endif |
stjo2809 | 0:2e9d45485414 | 276 | |
stjo2809 | 0:2e9d45485414 | 277 | |
stjo2809 | 0:2e9d45485414 | 278 |