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