SPI Flash Library for AT45

Dependents:   AT45_spi_flash_example AT45_spi_flash_example PLC1608-V1 RL0201-V1

Fork of AT45 by Steen Jørgensen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AT45.h Source File

AT45.h

00001 /* mbed AT45 Library, for driving the Atmel AT45 series Dataflash with Serial Interface (SPI)
00002  * Copyright (c) 2012, Created by Steen Joergensen (stjo2809) inspired by Chris Styles AT45 library
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  *
00022  *
00023  * This driver supports 021,041,081,161,321,641 variants of the AT45DBxxx family
00024  *
00025  * || Code || Density  || Page size || Pages || Package ||
00026  * || 021  || 2        || 256       || 1024  || 8 SOIC  ||
00027  * || 041  || 4        || 256       || 2048  || 8 SOIC  ||
00028  * || 081  || 8        || 256       || 4096  || 8 SOIC  ||
00029  * || 161  || 16       || 512       || 4096  || 8 SOIC  ||
00030  * || 321  || 32       || 512       || 8192  || 8 SOIC  ||
00031  * || 641  || 64       || 1024      || 8192  || 28 TSOP ||
00032  */
00033 
00034 #include "mbed.h"
00035 
00036 #ifndef AT45_H
00037 #define AT45_H
00038 
00039 //=============================================================================
00040 // Functions Declaration
00041 //=============================================================================
00042 
00043 /** Interface to the Atmel AT45 series Dataflash with Serial Interface (SPI)
00044  *
00045  *  Using the driver:
00046  *   - remenber to setup SPI in main routine.
00047  *   - remenber to setup Chipselect in main routine.
00048  *   - remenber to use FAT functions set pagesize to binary.
00049  *
00050  *  Limitations of using this driver:
00051  *   - can't use lockdown functions.
00052  *   - can't use protections functions.
00053  *   - can't use security functions.
00054  *
00055  */
00056 class AT45 {
00057 public:        
00058        /** Create an instance of the AT45 connected to specfied SPI pins, with the specified address.
00059         *
00060         * @param spi The mbed SPI instance (make in main routine)
00061         * @param nCs The SPI chip select pin.
00062         */
00063        AT45(SPI *spi, PinName ncs);
00064        
00065        /** Read a byte.
00066         *
00067         * @param address The address of the byte to read.
00068         * @return The data in the byte.
00069         */
00070        char read_byte(int address);
00071 
00072        /** Read a page.
00073         *
00074         * @param data The data is pointer to a userdefined array that the page is read into.
00075         * @param page The page number of the page to read (0 to device page size).
00076         * @return Returns "0" or "-1" for error.
00077         */
00078        int read_page(char* data, int page);
00079        
00080        /** Read a block (from 1 dimension array).
00081         *
00082         * @param data The data is pointer to a userdefined array that holds 4096 bytes of the data that is read into.
00083         * @param block The block number of the block to read (0 to device block size).
00084         * @return Returns "0" or "-1" for error.
00085         */        
00086        int read_block(char *data, int block);
00087        
00088        /** Read a block (from 2 dimension array).
00089         *
00090         * Then using 2 dimension array, the array shall have a starting point like "read_block(data[0],0)"
00091         * @param data The data is pointer to a userdefined array that holds 8 x 512 bytes of the data that is read into.
00092         * @param block The block number of the block to read (0 to device block size).
00093         * @return Returns "0" or "-1" for error.
00094         */       
00095        int read_block(char *data[], int block);
00096 
00097        /** Write a byte.
00098         *
00099         * @param address The address to where the data is storage in the flash.
00100         * @param data The data to write into the flash.
00101         */       
00102        void write_byte(int address, char data);
00103        
00104        /** Write a page.
00105         *
00106         * @param data The data is pointer to a userdefined array that holds the data to write into.
00107         * @param page The page number of the page to write into (0 to device page size).
00108         * @return Returns "0" or "-1" for error.
00109         */
00110        int write_page(char* data, int page);
00111 
00112        /** Write a block (from 1 dimension array).
00113         *
00114         * @param data The data is pointer to a userdefined array that holds 4096 bytes of the data to write into.
00115         * @param block The block number of the block to write into (0 to device block size).
00116         * @return Returns "0" or "-1" for error.
00117         */       
00118        int write_block(char *data, int block);
00119         
00120        /** Write a block (from 2 dimension array).
00121         *
00122         * Then using 2 dimension array, the array shall have a starting point like "write_block(data[0],0)"
00123         * @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).
00124         * @param block The block number of the block to write into (0 to device block size).
00125         * @return Returns "0" or "-1" for error.
00126         */       
00127        int write_block(char *data[], int block);
00128 
00129        /** FAT Read (512 bits).
00130         *
00131         * Remenber to set page size to binary.
00132         * @param data The data is pointer to a userdefined array that the page is read into.
00133         * @param page The page number of the page to read (0 to device page size).
00134         * @return Returns "0".
00135         */       
00136        int FAT_read(char* data, int page);
00137        
00138        /** FAT Write (512bits).
00139         *
00140         * Remenber to set page size to binary.
00141         * @param data The data is pointer to a userdefined array that holds the data to write into.
00142         * @param page The page number of the page to write into (0 to device page size).
00143         * @return Returns "0" or "-1" for error.
00144         */       
00145        int FAT_write(char* data, int page); 
00146             
00147        /** Function to erase the entire chip.
00148         *
00149         *  Issue:
00150         *  In a certain percentage of units, the chip erase feature may not function correctly and may adversely affect device operation.
00151         *  Therefore, it is recommended that the chip erase commands (opcodes C7H, 94H, 80H, and 9AH) not be used.
00152         *  Workaround:
00153         *  Use block erase (opcode 50H) as an alternative. The block erase function is not affected by the chip erase issue.
00154         *  Resolution:
00155         *  The chip erase feature may be fixed with a new revision of the device. Please contact Atmel for the estimated availability of
00156         *  devices with the fix.
00157         */
00158        void chip_erase(void);
00159        
00160        /** Function to erase the selected block.
00161         *
00162         * @param block The selected block to erase.
00163         */
00164        void block_erase(int block);
00165        
00166        /** Function to erase the selected block.
00167         *
00168         * @param page The number of the page to erase.
00169         */
00170        void page_erase(int page);
00171        
00172        /** Device size in mbits.
00173         *
00174         * @return device size.
00175         */
00176        int device_size(void);
00177        
00178        /** Pages in flash.
00179         *
00180         * @return Numbers af pages.
00181         */
00182        int pages(void);
00183        
00184        /** Page size.
00185         * 
00186         * for 2-8 Mbits 256 or 264
00187         * for 16-32 Mbits 512 or 528
00188         * for 64 Mbits 1024 or 1056
00189         *
00190         * @return Page size.
00191         */
00192        int pagesize(void);
00193 
00194        /** Function to set the page size to binary.
00195         *
00196         *  Remenber is a one-time programmable configuration.
00197         *  Remenber to total power down after the functions has been run. 
00198         */       
00199        void set_pageszie_to_binary(void);
00200        
00201        /** blocks in flash.
00202         *
00203         * @return Numbers af blocks.
00204         */       
00205        int blocks(void);
00206        
00207        /** ID of the device.
00208         *
00209         * @return Manufacturer, Family and Density code.
00210         */          
00211        int id(void);
00212        
00213        /** Status register.
00214         *
00215         * @return The status register.
00216         */           
00217        int status(void);    // Status register
00218        
00219        /** busy ?.
00220         *
00221         * Function will want to the device is not busy.
00222         */           
00223        void busy(void); // Wait until Flash is not busy
00224        
00225        /** Deep Power Down.
00226         *
00227         * Remenber that you have to want 35uS after the wake up to use the device. 
00228         * @param True = Activate and False = Wake Up.
00229         */         
00230        void deep_power_down(bool onoff);
00231         
00232        /** Is the device deep power down.
00233         *
00234         * @return True = Activate and False = Awake.
00235         */         
00236        bool is_it_awake(void);
00237 
00238 private:    
00239    
00240         SPI *_spi;
00241         DigitalOut _ncs;    
00242 
00243         int _pages;            // Integer number of pages
00244         int _pagesize;         // page size, in bytes 
00245         int _devicesize;       // device size in bytes
00246         int _blocks;           // Number of blocks
00247         bool _deep_down;       // True = the device is deep down
00248         bool _deep_down_onoff; // variable for deep power down function (On/Off) 
00249  
00250         // Helper routunes
00251         void _initialize();          
00252         void _select();
00253         void _deselect();
00254         void _busy (void);               
00255     
00256         // accessing SRAM buffers
00257         void _sramwrite (int buffer, int address, int data);
00258         int _sramread (int buffer, int address);
00259     
00260         // Transferring SRAM buffers to/from FLASH
00261         void _flashwrite (int buffer, int paddr);
00262         void _flashread (int buffer, int paddr);
00263 
00264         // Reading FLASH directly
00265         int _memread (int address);
00266     
00267         // Calculate page/subpage addresses
00268         int _getpaddr (int);
00269         int _getbaddr (int);
00270     
00271         // Send 3 byte address
00272         void _sendaddr (int address);
00273         
00274 };
00275 #endif
00276 
00277 
00278