Mistake on this page?
Report an issue in GitHub or email us
DataFlashBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2016 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MBED_DATAFLASH_BLOCK_DEVICE_H
18 #define MBED_DATAFLASH_BLOCK_DEVICE_H
19 
20 #include "platform/PlatformMutex.h"
21 #include "PinNames.h"
22 #include "features/storage/blockdevice/BlockDevice.h"
23 #include "drivers/SPI.h"
24 #include "drivers/DigitalOut.h"
25 
26 
27 /** BlockDevice for DataFlash flash devices
28  *
29  * @code
30  * // Here's an example using the AT45DB on the K64F
31  * #include "mbed.h"
32  * #include "DataFlashBlockDevice.h"
33  *
34  * // Create DataFlash on SPI bus with PTE5 as chip select
35  * DataFlashBlockDevice dataflash(PTE2, PTE4, PTE1, PTE5);
36  *
37  * // Create DataFlash on SPI bus with PTE6 as write-protect
38  * DataFlashBlockDevice dataflash2(PTE2, PTE4, PTE1, PTE5, PTE6);
39  *
40  * int main() {
41  * printf("dataflash test\n");
42  *
43  * // Initialize the SPI flash device and print the memory layout
44  * dataflash.init();
45  * printf("dataflash size: %llu\n", dataflash.size());
46  * printf("dataflash read size: %llu\n", dataflash.get_read_size());
47  * printf("dataflash program size: %llu\n", dataflash.get_program_size());
48  * printf("dataflash erase size: %llu\n", dataflash.get_erase_size());
49  *
50  * // Write "Hello World!" to the first block
51  * char *buffer = (char*)malloc(dataflash.get_erase_size());
52  * sprintf(buffer, "Hello World!\n");
53  * dataflash.erase(0, dataflash.get_erase_size());
54  * dataflash.program(buffer, 0, dataflash.get_erase_size());
55  *
56  * // Read back what was stored
57  * dataflash.read(buffer, 0, dataflash.get_erase_size());
58  * printf("%s", buffer);
59  *
60  * // Deinitialize the device
61  * dataflash.deinit();
62  * }
63  * @endcode
64  */
66 public:
67  /** Creates a DataFlashBlockDevice on a SPI bus specified by pins
68  *
69  * @param mosi SPI master out, slave in pin
70  * @param miso SPI master in, slave out pin
71  * @param sclk SPI clock pin
72  * @param csel SPI chip select pin
73  * @param nowp GPIO not-write-protect
74  * @param freq Clock speed of the SPI bus (defaults to 40MHz)
75  */
76  DataFlashBlockDevice(PinName mosi,
77  PinName miso,
78  PinName sclk,
79  PinName csel,
80  int freq = MBED_CONF_DATAFLASH_SPI_FREQ,
81  PinName nowp = NC);
82 
83  /** Initialize a block device
84  *
85  * @return 0 on success or a negative error code on failure
86  */
87  virtual int init();
88 
89  /** Deinitialize a block device
90  *
91  * @return 0 on success or a negative error code on failure
92  */
93  virtual int deinit();
94 
95  /** Read blocks from a block device
96  *
97  * @param buffer Buffer to write blocks to
98  * @param addr Address of block to begin reading from
99  * @param size Size to read in bytes, must be a multiple of read block size
100  * @return 0 on success, negative error code on failure
101  */
102  virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
103 
104  /** Program blocks to a block device
105  *
106  * The blocks must have been erased prior to being programmed
107  *
108  * @param buffer Buffer of data to write to blocks
109  * @param addr Address of block to begin writing to
110  * @param size Size to write in bytes, must be a multiple of program block size
111  * @return 0 on success, negative error code on failure
112  */
113  virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
114 
115  /** Erase blocks on a block device
116  *
117  * The state of an erased block is undefined until it has been programmed
118  *
119  * @param addr Address of block to begin erasing
120  * @param size Size to erase in bytes, must be a multiple of erase block size
121  * @return 0 on success, negative error code on failure
122  */
123  virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size);
124 
125  /** Get the size of a readable block
126  *
127  * @return Size of a readable block in bytes
128  */
129  virtual mbed::bd_size_t get_read_size() const;
130 
131  /** Get the size of a programable block
132  *
133  * @return Size of a programable block in bytes
134  * @note Must be a multiple of the read size
135  */
136  virtual mbed::bd_size_t get_program_size() const;
137 
138  /** Get the size of a eraseable block
139  *
140  * @return Size of a eraseable block in bytes
141  * @note Must be a multiple of the program size
142  */
143  virtual mbed::bd_size_t get_erase_size() const;
144 
145  /** Get the size of an erasable block given address
146  *
147  * @param addr Address within the erasable block
148  * @return Size of an erasable block in bytes
149  * @note Must be a multiple of the program size
150  */
151  virtual mbed::bd_size_t get_erase_size(mbed::bd_addr_t addr) const;
152 
153  /** Get the total size of the underlying device
154  *
155  * @return Size of the underlying device in bytes
156  */
157  virtual mbed::bd_size_t size() const;
158 
159  /** Get the BlockDevice class type.
160  *
161  * @return A string represent the BlockDevice class type.
162  */
163  virtual const char *get_type() const;
164 
165 private:
166  // Master side hardware
167  mbed::SPI _spi;
168  mbed::DigitalOut _cs;
169  mbed::DigitalOut _nwp;
170 
171  // Device configuration
172  uint32_t _device_size;
173  uint16_t _page_size;
174  uint16_t _block_size;
175  bool _is_initialized;
176  uint32_t _init_ref_count;
177 
178  // Internal functions
179  uint16_t _get_register(uint8_t opcode);
180  void _write_command(uint32_t command, const uint8_t *buffer, uint32_t size);
181  void _write_enable(bool enable);
182  int _sync(void);
183  int _write_page(const uint8_t *buffer, uint32_t addr, uint32_t offset, uint32_t size);
184  uint32_t _translate_address(bd_addr_t addr);
185 
186  // Mutex for thread safety
187  mutable PlatformMutex _mutex;
188 };
189 
190 
191 #endif /* MBED_DATAFLASH_BLOCK_DEVICE_H */
virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Read blocks from a block device.
virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Program blocks to a block device.
virtual mbed::bd_size_t size() const
Get the total size of the underlying device.
virtual const char * get_type() const
Get the BlockDevice class type.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:47
virtual int init()
Initialize a block device.
virtual mbed::bd_size_t get_read_size() const
Get the size of a readable block.
virtual mbed::bd_size_t get_erase_size() const
Get the size of a eraseable block.
virtual int deinit()
Deinitialize a block device.
DataFlashBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq=MBED_CONF_DATAFLASH_SPI_FREQ, PinName nowp=NC)
Creates a DataFlashBlockDevice on a SPI bus specified by pins.
virtual mbed::bd_size_t get_program_size() const
Get the size of a programable block.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
BlockDevice for DataFlash flash devices.
A digital output, used for setting the state of a pin.
Definition: DigitalOut.h:49
A SPI Master, used for communicating with SPI slave devices.
Definition: SPI.h:98
virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size)
Erase blocks on a block device.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.