fota lib for mdot

Dependents:   UQ_LoraWAN

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SpiFlash25.h Source File

SpiFlash25.h

00001 /* Library for SPI flash 25* devices.
00002  * Copyright (c) 2014 Multi-Tech Systems
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 THE
00020  * SOFTWARE.
00021  */
00022 
00023 #ifndef SPIFLASH25_H
00024 #define SPIFLASH25_H
00025 
00026 #include "mbed.h"
00027 
00028 class SpiFlash25 {
00029     public:
00030         SpiFlash25(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName W = NC, PinName HOLD = NC, int page_size = 256, int mem_size = 2097152);
00031 
00032         /* Set the page size (default 256) */
00033         void set_page_size(int size);
00034 
00035         /* Set up the internal SPI object */
00036         void format(int bits, int mode);
00037         void frequency(int hz);
00038 
00039         /* Reads and writes can be across page boundaries */
00040         bool read(int addr, int len, char* data);
00041         bool write(int addr, int len, const char* data);
00042 
00043         /* Read ID and status registers */
00044         char* read_id();
00045         void write_status(char data);
00046         char read_status();
00047 
00048         /* Erase methods */
00049         void clear_sector(int addr);
00050         void clear_mem();
00051 
00052         void deep_power_down();
00053         void wakeup();
00054 
00055     private:
00056         enum {
00057             WRITE_ENABLE                = 0x06,
00058             WRITE_DISABLE               = 0x04,
00059             READ_IDENTIFICATION         = 0x9F,
00060             READ_STATUS                 = 0x05,
00061             WRITE_STATUS                = 0x01,
00062             READ_DATA                   = 0x03,
00063             READ_DATA_FAST              = 0x0B,
00064             PAGE_PROGRAM                = 0x02,
00065             SECTOR_ERASE                = 0xD8,
00066             BULK_ERASE                  = 0xC7,
00067             DEEP_POWER_DOWN             = 0xB9,
00068             DEEP_POWER_DOWN_RELEASE     = 0xAB,
00069         };
00070 
00071         enum {
00072             STATUS_SRWD                 = 0x80,     // 0b 1000 0000
00073             STATUS_BP2                  = 0x10,     // 0b 0001 0000
00074             STATUS_BP1                  = 0x08,     // 0b 0000 1000
00075             STATUS_BP0                  = 0x04,     // 0b 0000 0100
00076             STATUS_WEL                  = 0x02,     // 0b 0000 0010
00077             STATUS_WIP                  = 0x01,     // 0b 0000 0001
00078         };
00079 
00080         enum {
00081             ID_MANUFACTURER             = 0,
00082             ID_MEM_TYPE                 = 1,
00083             ID_MEM_SIZE                 = 2,
00084         };
00085 
00086         bool write_page(int addr, int len, const char* data);
00087         void enable_write();
00088         void wait_for_write();
00089 
00090         static inline char high_byte(int addr) {
00091             return ((addr & 0xff0000) >> 16);
00092         }
00093         static inline char mid_byte(int addr) {
00094             return ((addr & 0xff00) >> 8);
00095         }
00096         static inline char low_byte(int addr) {
00097             return (addr & 0xff);
00098         }
00099 
00100         SPI _spi;
00101         DigitalOut _cs;
00102         DigitalOut* _w;
00103         DigitalOut* _hold;
00104         int _mem_size;
00105         int _page_size;
00106         char _id[3];
00107 };
00108 #endif