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 <mbed.h>
21 #include "BlockDevice.h"
22 
23 
24 /** BlockDevice for DataFlash flash devices
25  *
26  * @code
27  * // Here's an example using the AT45DB on the K64F
28  * #include "mbed.h"
29  * #include "DataFlashBlockDevice.h"
30  *
31  * // Create DataFlash on SPI bus with PTE5 as chip select
32  * DataFlashBlockDevice dataflash(PTE2, PTE4, PTE1, PTE5);
33  *
34  * // Create DataFlash on SPI bus with PTE6 as write-protect
35  * DataFlashBlockDevice dataflash2(PTE2, PTE4, PTE1, PTE5, PTE6);
36  *
37  * int main() {
38  * printf("dataflash test\n");
39  *
40  * // Initialize the SPI flash device and print the memory layout
41  * dataflash.init();
42  * printf("dataflash size: %llu\n", dataflash.size());
43  * printf("dataflash read size: %llu\n", dataflash.get_read_size());
44  * printf("dataflash program size: %llu\n", dataflash.get_program_size());
45  * printf("dataflash erase size: %llu\n", dataflash.get_erase_size());
46  *
47  * // Write "Hello World!" to the first block
48  * char *buffer = (char*)malloc(dataflash.get_erase_size());
49  * sprintf(buffer, "Hello World!\n");
50  * dataflash.erase(0, dataflash.get_erase_size());
51  * dataflash.program(buffer, 0, dataflash.get_erase_size());
52  *
53  * // Read back what was stored
54  * dataflash.read(buffer, 0, dataflash.get_erase_size());
55  * printf("%s", buffer);
56  *
57  * // Deinitialize the device
58  * dataflash.deinit();
59  * }
60  * @endcode
61  */
63 public:
64  /** Creates a DataFlashBlockDevice on a SPI bus specified by pins
65  *
66  * @param mosi SPI master out, slave in pin
67  * @param miso SPI master in, slave out pin
68  * @param sclk SPI clock pin
69  * @param csel SPI chip select pin
70  * @param nowp GPIO not-write-protect
71  * @param freq Clock speed of the SPI bus (defaults to 40MHz)
72  */
73  DataFlashBlockDevice(PinName mosi,
74  PinName miso,
75  PinName sclk,
76  PinName csel,
77  int freq = MBED_CONF_DATAFLASH_SPI_FREQ,
78  PinName nowp = NC);
79 
80  /** Initialize a block device
81  *
82  * @return 0 on success or a negative error code on failure
83  */
84  virtual int init();
85 
86  /** Deinitialize a block device
87  *
88  * @return 0 on success or a negative error code on failure
89  */
90  virtual int deinit();
91 
92  /** Read blocks from a block device
93  *
94  * @param buffer Buffer to write blocks to
95  * @param addr Address of block to begin reading from
96  * @param size Size to read in bytes, must be a multiple of read block size
97  * @return 0 on success, negative error code on failure
98  */
99  virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
100 
101  /** Program blocks to a block device
102  *
103  * The blocks must have been erased prior to being programmed
104  *
105  * @param buffer Buffer of data to write to blocks
106  * @param addr Address of block to begin writing to
107  * @param size Size to write in bytes, must be a multiple of program block size
108  * @return 0 on success, negative error code on failure
109  */
110  virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
111 
112  /** Erase blocks on a block device
113  *
114  * The state of an erased block is undefined until it has been programmed
115  *
116  * @param addr Address of block to begin erasing
117  * @param size Size to erase in bytes, must be a multiple of erase block size
118  * @return 0 on success, negative error code on failure
119  */
120  virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size);
121 
122  /** Get the size of a readable block
123  *
124  * @return Size of a readable block in bytes
125  */
126  virtual mbed::bd_size_t get_read_size() const;
127 
128  /** Get the size of a programable block
129  *
130  * @return Size of a programable block in bytes
131  * @note Must be a multiple of the read size
132  */
133  virtual mbed::bd_size_t get_program_size() const;
134 
135  /** Get the size of a eraseable block
136  *
137  * @return Size of a eraseable block in bytes
138  * @note Must be a multiple of the program size
139  */
140  virtual mbed::bd_size_t get_erase_size() const;
141 
142  /** Get the size of an erasable block given address
143  *
144  * @param addr Address within the erasable block
145  * @return Size of an erasable block in bytes
146  * @note Must be a multiple of the program size
147  */
148  virtual mbed::bd_size_t get_erase_size(mbed::bd_addr_t addr) const;
149 
150  /** Get the total size of the underlying device
151  *
152  * @return Size of the underlying device in bytes
153  */
154  virtual mbed::bd_size_t size() const;
155 
156  /** Get the BlockDevice class type.
157  *
158  * @return A string represent the BlockDevice class type.
159  */
160  virtual const char *get_type() const;
161 
162 private:
163  // Master side hardware
164  mbed::SPI _spi;
165  mbed::DigitalOut _cs;
166  mbed::DigitalOut _nwp;
167 
168  // Device configuration
169  uint32_t _device_size;
170  uint16_t _page_size;
171  uint16_t _block_size;
172  bool _is_initialized;
173  uint32_t _init_ref_count;
174 
175  // Internal functions
176  uint16_t _get_register(uint8_t opcode);
177  void _write_command(uint32_t command, const uint8_t *buffer, uint32_t size);
178  void _write_enable(bool enable);
179  int _sync(void);
180  int _write_page(const uint8_t *buffer, uint32_t addr, uint32_t offset, uint32_t size);
181  uint32_t _translate_address(bd_addr_t addr);
182 
183  // Mutex for thread safety
184  mutable PlatformMutex _mutex;
185 };
186 
187 
188 #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.