Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DataFlashBlockDevice.h Source File

DataFlashBlockDevice.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 
00017 #ifndef MBED_DATAFLASH_BLOCK_DEVICE_H
00018 #define MBED_DATAFLASH_BLOCK_DEVICE_H
00019 
00020 #include "platform/PlatformMutex.h"
00021 #include "PinNames.h"
00022 #include "features/storage/blockdevice/BlockDevice.h"
00023 #include "drivers/SPI.h"
00024 #include "drivers/DigitalOut.h"
00025 
00026 
00027 /** BlockDevice for DataFlash flash devices
00028  *
00029  *  @code
00030  *  // Here's an example using the AT45DB on the K64F
00031  *  #include "mbed.h"
00032  *  #include "DataFlashBlockDevice.h"
00033  *
00034  *  // Create DataFlash on SPI bus with PTE5 as chip select
00035  *  DataFlashBlockDevice dataflash(PTE2, PTE4, PTE1, PTE5);
00036  *
00037  *  // Create DataFlash on SPI bus with PTE6 as write-protect
00038  *  DataFlashBlockDevice dataflash2(PTE2, PTE4, PTE1, PTE5, PTE6);
00039  *
00040  *  int main() {
00041  *      printf("dataflash test\n");
00042  *
00043  *      // Initialize the SPI flash device and print the memory layout
00044  *      dataflash.init();
00045  *      printf("dataflash size: %llu\n", dataflash.size());
00046  *      printf("dataflash read size: %llu\n", dataflash.get_read_size());
00047  *      printf("dataflash program size: %llu\n", dataflash.get_program_size());
00048  *      printf("dataflash erase size: %llu\n", dataflash.get_erase_size());
00049  *
00050  *      // Write "Hello World!" to the first block
00051  *      char *buffer = (char*)malloc(dataflash.get_erase_size());
00052  *      sprintf(buffer, "Hello World!\n");
00053  *      dataflash.erase(0, dataflash.get_erase_size());
00054  *      dataflash.program(buffer, 0, dataflash.get_erase_size());
00055  *
00056  *      // Read back what was stored
00057  *      dataflash.read(buffer, 0, dataflash.get_erase_size());
00058  *      printf("%s", buffer);
00059  *
00060  *      // Deinitialize the device
00061  *      dataflash.deinit();
00062  *  }
00063  *  @endcode
00064  */
00065 class DataFlashBlockDevice : public mbed::BlockDevice {
00066 public:
00067     /** Creates a DataFlashBlockDevice on a SPI bus specified by pins
00068      *
00069      *  @param mosi     SPI master out, slave in pin
00070      *  @param miso     SPI master in, slave out pin
00071      *  @param sclk     SPI clock pin
00072      *  @param csel     SPI chip select pin
00073      *  @param nowp     GPIO not-write-protect
00074      *  @param freq     Clock speed of the SPI bus (defaults to 40MHz)
00075      */
00076     DataFlashBlockDevice(PinName mosi,
00077                          PinName miso,
00078                          PinName sclk,
00079                          PinName csel,
00080                          int freq = MBED_CONF_DATAFLASH_SPI_FREQ,
00081                          PinName nowp = NC);
00082 
00083     /** Initialize a block device
00084      *
00085      *  @return         0 on success or a negative error code on failure
00086      */
00087     virtual int init();
00088 
00089     /** Deinitialize a block device
00090      *
00091      *  @return         0 on success or a negative error code on failure
00092      */
00093     virtual int deinit();
00094 
00095     /** Read blocks from a block device
00096      *
00097      *  @param buffer   Buffer to write blocks to
00098      *  @param addr     Address of block to begin reading from
00099      *  @param size     Size to read in bytes, must be a multiple of read block size
00100      *  @return         0 on success, negative error code on failure
00101      */
00102     virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
00103 
00104     /** Program blocks to a block device
00105      *
00106      *  The blocks must have been erased prior to being programmed
00107      *
00108      *  @param buffer   Buffer of data to write to blocks
00109      *  @param addr     Address of block to begin writing to
00110      *  @param size     Size to write in bytes, must be a multiple of program block size
00111      *  @return         0 on success, negative error code on failure
00112      */
00113     virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
00114 
00115     /** Erase blocks on a block device
00116      *
00117      *  The state of an erased block is undefined until it has been programmed
00118      *
00119      *  @param addr     Address of block to begin erasing
00120      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00121      *  @return         0 on success, negative error code on failure
00122      */
00123     virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size);
00124 
00125     /** Get the size of a readable block
00126      *
00127      *  @return         Size of a readable block in bytes
00128      */
00129     virtual mbed::bd_size_t get_read_size() const;
00130 
00131     /** Get the size of a programable block
00132      *
00133      *  @return         Size of a programable block in bytes
00134      *  @note Must be a multiple of the read size
00135      */
00136     virtual mbed::bd_size_t get_program_size() const;
00137 
00138     /** Get the size of a eraseable block
00139      *
00140      *  @return         Size of a eraseable block in bytes
00141      *  @note Must be a multiple of the program size
00142      */
00143     virtual mbed::bd_size_t get_erase_size() const;
00144 
00145     /** Get the size of an erasable block given address
00146      *
00147      *  @param addr     Address within the erasable block
00148      *  @return         Size of an erasable block in bytes
00149      *  @note Must be a multiple of the program size
00150      */
00151     virtual mbed::bd_size_t get_erase_size(mbed::bd_addr_t addr) const;
00152 
00153     /** Get the total size of the underlying device
00154      *
00155      *  @return         Size of the underlying device in bytes
00156      */
00157     virtual mbed::bd_size_t size() const;
00158 
00159     /** Get the BlockDevice class type.
00160      *
00161      *  @return         A string represent the BlockDevice class type.
00162      */
00163     virtual const char *get_type() const;
00164 
00165 private:
00166     // Master side hardware
00167     mbed::SPI _spi;
00168     mbed::DigitalOut _cs;
00169     mbed::DigitalOut _nwp;
00170 
00171     // Device configuration
00172     uint32_t _device_size;
00173     uint16_t _page_size;
00174     uint16_t _block_size;
00175     bool _is_initialized;
00176     uint32_t _init_ref_count;
00177 
00178     // Internal functions
00179     uint16_t _get_register(uint8_t opcode);
00180     void _write_command(uint32_t command, const uint8_t *buffer, uint32_t size);
00181     void _write_enable(bool enable);
00182     int _sync(void);
00183     int _write_page(const uint8_t *buffer, uint32_t addr, uint32_t offset, uint32_t size);
00184     uint32_t _translate_address(bd_addr_t addr);
00185 
00186     // Mutex for thread safety
00187     mutable PlatformMutex _mutex;
00188 };
00189 
00190 
00191 #endif  /* MBED_DATAFLASH_BLOCK_DEVICE_H */