little llumpu / USBMSD_AT45

Dependents:   USBMSD_AT45_HelloWorld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMSD_AT45.h Source File

USBMSD_AT45.h

00001 /* Copyright (c) <2012> <llumpu>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 /* 
00020  * Inspired by Steen Joergensen (stjo2809) and Chris Styles AT45 libraries
00021  */
00022 
00023 #include "mbed.h"
00024 #include "USBMSD.h"
00025 
00026 #ifndef USBMSD_AT45_H
00027 #define USBMSD_AT45_H
00028 
00029 
00030 /** USBMSD device (USB flashdisk) with Atmel AT45DBxx serial flash
00031  *
00032  * Example:
00033  * @code
00034  * #include "mbed.h"
00035  * #include "USBMSD_AT45.h"
00036  *
00037  * USBMSD_AT45 Flash(p5, p6, p7, p8, 512);  // Mosi, Miso, Sclk, CS, Size of block being transported over USB
00038  *                                 // each time. Can be 512, 1024, 2048, 4096. Best is to select same
00039  *                                 // size as AT45DBxx SRAM buffer size. If page size of flash is not
00040  *                                 // binary 2^N (256, 512, 1024 bytes) but is 264, 528 or 1056 bytes
00041  *                                 // before each write we read block being written to SRAM then rewrite
00042  *                                 // part of them with data from host and write whole SRAM buffer back
00043  *                                 // to flash. This avoids to data being rewritten in other blocks
00044  *                                 // we actually do not write to.
00045  * int main() {
00046  *
00047  *  while(1) {
00048  *
00049  *  // Do something else here
00050  *
00051  *   }
00052  *  }
00053  * @endcode
00054  */
00055 
00056 class USBMSD_AT45 : public USBMSD
00057 {
00058 public:
00059 
00060     /**
00061     * Constructor
00062     *
00063     * Create an instance of the USBMSD_AT45 connected to specfied SPI pins, with the specified CHIP SELECT pin
00064     *
00065     * @param mosi The SPI mosi Pin (p5 or p11)
00066     * @param miso The SPI miso Pin (p6 or p12)
00067     * @param sclk The SPI sclk Pin (p7 or p13)
00068     * @param ncs The SPI chip select Pin (any digital out pin)
00069     * @param transport_block_size The size of block being transported over USB
00070     */
00071 
00072     USBMSD_AT45(PinName mosi, PinName miso, PinName sclk, PinName ncs, int transport_block_size);
00073 
00074     /** disk_read()
00075     *
00076     * Reads block of data from storage chip. Size of block is set in constructor
00077     *
00078     * @param data Pointer to buffer where data will be read to
00079     * @param block Number of requested block
00080     */
00081 
00082     virtual int disk_read(char * data, int block);
00083 
00084     /** disk_write()
00085     *
00086     * Writes block of data to storage chip. Size of block is set in constructor
00087     *
00088     * @param data Pointer to buffer from which contains data to be written to storage chip
00089     * @param block Number of block to be written to
00090     */
00091 
00092     virtual int disk_write(const char * data, int block);
00093 
00094     /** disk_size()
00095     *
00096     * @param returns Size of storage chip in bytes
00097     */
00098 
00099     virtual int disk_size();
00100 
00101     /** disk_sectors()
00102     *
00103     * @param returns Count of sectors of storage chip
00104     */
00105 
00106     virtual int disk_sectors();
00107 
00108     /** disk_status()
00109     *
00110     * @param returns Status of storage chip - 0x00 ready, 0x01 not initialized (disk_initialize is then called)
00111     */
00112 
00113     virtual int disk_status();
00114 
00115     /** disk_initialize()
00116     *
00117     * This method is called when disk_status returns 0x01 (not initialized)
00118     */
00119 
00120     virtual int disk_initialize();
00121 
00122 
00123 protected:
00124 
00125 // SPI bus and Chip select
00126 
00127     SPI _spi; // SPI bus
00128     DigitalOut _ncs; // Chip Select pin
00129 
00130 // Protected variables storing flash chip parameters
00131 
00132     int _flash_size; // Total size of storage chip in bytes
00133     int _flash_buffer_size; // !!! Used size of memory page (and SRAM buffer) in bytes
00134 
00135     int _page_size; // !!! Real size of memory page (and SRAM buffer) in bytes
00136     int _transport_block_size; // Size of block transported over USB in bytes
00137     int _init_status; // Status of storage chip - 0x00 ready, 0x01 not initialized
00138 
00139 // Helper routines
00140 
00141     void _initialize(); // Determine parameters of connected storage chip
00142     void _busy (void);  // Check if storage chip is not busy
00143 
00144 // Transferring SRAM buffers to/from FLASH
00145 
00146     void _flashwrite (int buffer, int paddr);
00147     void _flashread (int buffer, int paddr);
00148 
00149 // Calculate page/subpage addresses
00150 
00151     int _getpaddr (int address);
00152 
00153 // Send 3 byte address
00154 
00155     void _sendaddr (int address);
00156 
00157 };
00158 #endif