Mistake on this page?
Report an issue in GitHub or email us
SPIFReducedBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 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 #ifndef MBED_RSPIF_BLOCK_DEVICE_H
17 #define MBED_RSPIF_BLOCK_DEVICE_H
18 
19 #include "SPI.h"
20 #include "DigitalOut.h"
21 #include "BlockDevice.h"
22 
23 /** Reduced BlockDevice for SPI based flash devices
24  * *Should only be used by Boot Loader*
25  *
26  * @code
27  * // Here's an example using the SPI flash device on K82F
28  * #include "mbed.h"
29  * #include "SPIFReducedBlockDevice.h"
30  *
31  * // Create flash device on SPI bus with PTE5 as chip select
32  * SPIFReducedBlockDevice rspif(PTE2, PTE4, PTE1, PTE5);
33  *
34  * int main() {
35  * printf("reduced spif test\n");
36  *
37  * // Initialize the Reduced SPI flash device and print the memory layout
38  * rspif.init();
39  * printf("rspif size: %llu\n", rspif.size());
40  * printf("rspif read size: %llu\n", rspif.get_read_size());
41  * printf("rspif program size: %llu\n", rspif.get_program_size());
42  * printf("rspif erase size: %llu\n", rspif.get_erase_size());
43  *
44  * // Write "Hello World!" to the first block
45  * char *buffer = (char*)malloc(rspif.get_erase_size());
46  * sprintf(buffer, "Hello World!\n");
47  * rspif.erase(0, rspif.get_erase_size());
48  * rspif.program(buffer, 0, rspif.get_erase_size());
49  *
50  * // Read back what was stored
51  * rspif.read(buffer, 0, rspif.get_erase_size());
52  * printf("%s", buffer);
53  *
54  * // Deinitialize the device
55  * rspif.deinit();
56  * }
57  * @endcode
58  */
60 public:
61  /** Creates a SPIFReducedBlockDevice on a SPI bus specified by pins
62  *
63  * @param mosi SPI master out, slave in pin
64  * @param miso SPI master in, slave out pin
65  * @param sclk SPI clock pin
66  * @param csel SPI chip select pin
67  * @param freq Clock speed of the SPI bus (defaults to 40MHz)
68  */
69  SPIFReducedBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq = 40000000);
70 
71  /** Initialize a block device
72  *
73  * @return 0 on success or a negative error code on failure
74  */
75  virtual int init();
76 
77  /** Deinitialize a block device
78  *
79  * @return 0 on success or a negative error code on failure
80  */
81  virtual int deinit();
82 
83  /** Read blocks from a block device
84  *
85  * @param buffer Buffer to write blocks to
86  * @param addr Address of block to begin reading from
87  * @param size Size to read in bytes, must be a multiple of read block size
88  * @return 0 on success, negative error code on failure
89  */
90  virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
91 
92  /** Program blocks to a block device
93  *
94  * The blocks must have been erased prior to being programmed
95  *
96  * @param buffer Buffer of data to write to blocks
97  * @param addr Address of block to begin writing to
98  * @param size Size to write in bytes, must be a multiple of program block size
99  * @return 0 on success, negative error code on failure
100  */
101  virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
102 
103  /** Erase blocks on a block device
104  *
105  * The state of an erased block is undefined until it has been programmed
106  *
107  * @param addr Address of block to begin erasing
108  * @param size Size to erase in bytes, must be a multiple of erase block size
109  * @return 0 on success, negative error code on failure
110  */
111  virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size);
112 
113  /** Get the size of a readable block
114  *
115  * @return Size of a readable block in bytes
116  */
117  virtual mbed::bd_size_t get_read_size() const;
118 
119  /** Get the size of a programable block
120  *
121  * @return Size of a programable block in bytes
122  * @note Must be a multiple of the read size
123  */
124  virtual mbed::bd_size_t get_program_size() const;
125 
126  /** Get the size of a eraseable block
127  *
128  * @return Size of a eraseable block in bytes
129  * @note Must be a multiple of the program size
130  */
131  virtual mbed::bd_size_t get_erase_size() const;
132 
133  /** Get the size of a eraseable block
134  *
135  * @param addr Address of block to query erase size
136  * @return Size of a eraseable block in bytes
137  * @note Must be a multiple of the program size
138  */
139  virtual mbed::bd_size_t get_erase_size(mbed::bd_addr_t addr) const;
140 
141  /** Get the value of storage byte after it was erased
142  *
143  * If get_erase_value returns a non-negative byte value, the underlying
144  * storage is set to that value when erased, and storage containing
145  * that value can be programmed without another erase.
146  *
147  * @return The value of storage when erased, or -1 if you can't
148  * rely on the value of erased storage
149  */
150  virtual int get_erase_value() const;
151 
152  /** Get the total size of the underlying device
153  *
154  * @return Size of the underlying device in bytes
155  */
156  virtual mbed::bd_size_t size() const;
157 
158  /** Get the BlockDevice class type.
159  *
160  * @return A string represent the BlockDevice class type.
161  */
162  virtual const char *get_type() const;
163 
164 private:
165  // Master side hardware
166  mbed::SPI _spi;
167  mbed::DigitalOut _cs;
168 
169  // Device configuration discovered through sfdp
170  mbed::bd_size_t _size;
171 
172  // Internal functions
173  int _wren();
174  int _sync();
175  void _cmdread(uint8_t op, uint32_t addrc, uint32_t retc,
176  uint32_t addr, uint8_t *rets);
177  void _cmdwrite(uint8_t op, uint32_t addrc, uint32_t argc,
178  uint32_t addr, const uint8_t *args);
179 };
180 #endif
virtual int deinit()
Deinitialize a block device.
virtual mbed::bd_size_t size() const
Get the total size of the underlying device.
virtual int init()
Initialize a block device.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:47
virtual mbed::bd_size_t get_erase_size() const
Get the size of a eraseable block.
SPIFReducedBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq=40000000)
Creates a SPIFReducedBlockDevice on a SPI bus specified by pins.
virtual mbed::bd_size_t get_program_size() const
Get the size of a programable block.
virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Read blocks from a block device.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual int get_erase_value() const
Get the value of storage byte after it was erased.
virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size)
Erase blocks on a block device.
Reduced BlockDevice for SPI based flash devices Should only be used by Boot Loader ...
virtual mbed::bd_size_t get_read_size() const
Get the size of a readable block.
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 program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Program blocks to 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.