takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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