1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Software License Agreement (BSD License)
10 Copyright (c) 2010, microBuilder SARL
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /**************************************************************************/
40 #include "projectconfig.h"
42 /**************************************************************************/
46 /**************************************************************************/
49 SPIFLASH_ERROR_OK
= 0, // Everything executed normally
50 SPIFLASH_ERROR_ADDROUTOFRANGE
= 1, // Address out of range
51 SPIFLASH_ERROR_TIMEOUT_READY
= 2, // Timeout waiting for ready status
52 SPIFLASH_ERROR_WRITEERR
= 3, // Write Error
53 SPIFLASH_ERROR_PROTECTIONERR
= 4, // Write Protection Error
54 SPIFLASH_ERROR_ADDROVERFLOW
= 5, // Address overflow during read/write
55 SPIFLASH_ERROR_UNEXPECTEDID
= 6, // The manufacturer and/or device ID are different than expected
56 SPIFLASH_ERROR_NOTSTARTOFPAGE
= 7, // The supplied address is not the start of a new page
57 SPIFLASH_ERROR_DATAEXCEEDSPAGESIZE
= 9, // When writing page data, you can't exceed page size
58 SPIFLASH_ERROR_PAGEWRITEOVERFLOW
= 10, // Page data will overflow beause (start address + len) > page size
63 /**************************************************************************/
65 @brief Describes the storage capacity of the SPI flash, including the
66 size of the minimum HW write (page) and erase (sector) units.
68 /**************************************************************************/
71 uint32_t pageSize
; // Page size to write data (in bytes)
72 uint32_t pageCount
; // Number of pages
73 uint32_t sectorSize
; // Sector size to erase data (in bytes)
74 uint32_t sectorCount
; // Number of sectors
78 /**************************************************************************/
80 @brief Tries to initialise the flash device, and sets up any HW
81 required by the SPI flash
83 /**************************************************************************/
84 void spiflashInit (void);
86 /**************************************************************************/
88 @brief Gets an instance of spiflashSizeInfo_t that describes the
89 storage limits of the SPI flash like page size (minimum write
90 size) and sector size (minimum erase size).
92 /**************************************************************************/
93 spiflashSizeInfo_t
spiflashGetSizeInfo(void);
95 /**************************************************************************/
97 @brief Gets the 8-bit manufacturer ID and device ID for the flash
100 Pointer to the uint8_t that will store the manufacturer ID
101 @param[out] *deviceID
102 Pointer to the uint8_t that will store the device ID
104 /**************************************************************************/
105 void spiflashGetManufacturerInfo (uint8_t *manufID
, uint8_t *deviceID
);
107 /**************************************************************************/
109 @brief Sets the write flag on the SPI flash, and if required puts the
110 WP pin in an appropriate state
113 True (1) to enable writing, false (0) to disable it
115 /**************************************************************************/
116 void spiflashWriteEnable (bool enable
);
118 /**************************************************************************/
120 @brief Reads the specified number of bytes from the supplied address.
122 This function will read one or more bytes starting at the supplied
123 address. Please note that bufferLength is zero-based, meaning you
124 should supply '0' to read a single byte, '3' to read 4 bytes of data,
128 The 24-bit address where the read will start.
130 Pointer to the buffer that will store the read results
132 Length of the buffer.
138 spiflashError_e error;
139 error = spiflashReadBuffer (0, buffer, 64);
142 // Check what went wrong
145 case SPIFLASH_ERROR_ADDROUTOFRANGE:
146 // Specified starting address is out of range
148 case SPIFLASH_ERROR_TIMEOUT_READY:
149 // Timed out waiting for flash to return ready state
151 case SPIFLASH_ERROR_ADDROVERFLOW:
152 // Ran over the upper address during read
158 /**************************************************************************/
159 spiflashError_e
spiflashReadBuffer (uint32_t address
, uint8_t *buffer
, uint32_t len
);
161 /**************************************************************************/
163 @brief Erases the contents of a single sector
165 @param[in] sectorNumber
166 The sector number to erase (zero-based).
171 spiflashError_e error;
172 error = spiflashEraseSector(0);
175 // Check what went wrong
178 case SPIFLASH_ERROR_ADDROUTOFRANGE:
179 // Specified starting address is out of range
181 case SPIFLASH_ERROR_PROTECTIONERR:
182 // Couldn't set the write enable bit
184 case SPIFLASH_ERROR_TIMEOUT_READY:
185 // Timed out waiting for flash to return ready state
191 /**************************************************************************/
192 spiflashError_e
spiflashEraseSector (uint32_t sectorNumber
);
194 /**************************************************************************/
196 @brief Erases the entire flash chip
201 spiflashError_e error;
202 error = spiflashEraseChip();
205 // Check what went wrong
208 case SPIFLASH_ERROR_PROTECTIONERR:
209 // Couldn't set the write enable bit
211 case SPIFLASH_ERROR_TIMEOUT_READY:
212 // Timed out waiting for flash to return ready state
218 /**************************************************************************/
219 spiflashError_e
spiflashEraseChip (void);
221 /**************************************************************************/
223 @brief Writes up to 256 bytes of data to the specified page.
225 @note Before writing data to a page, make sure that the 4K sector
226 containing the specific page has been erased, otherwise the
227 data will be meaningless.
230 The 24-bit address where the write will start.
232 Pointer to the buffer that will store the read results
234 Length of the buffer. Valid values are from 1 to 256,
235 within the limits of the starting address and page length.
240 spiflashError_e error;
252 error = spiflashWritePage (0, buffer, 8);
255 // Check what went wrong
258 case SPIFLASH_ERROR_ADDROUTOFRANGE:
259 // Specified starting address is out of range
261 case SPIFLASH_ERROR_DATAEXCEEDSPAGESIZE:
262 // Supplied data exceeds max page size
264 case SPIFLASH_ERROR_PAGEWRITEOVERFLOW:
265 // The data length plus the start address offset exceeeds page limits
267 case SPIFLASH_ERROR_TIMEOUT_READY:
268 // Timeout waiting for ready status (can be pre or post write)
270 case SPIFLASH_ERROR_PROTECTIONERR:
271 // Unable to set write latch
277 /**************************************************************************/
278 spiflashError_e
spiflashWritePage (uint32_t address
, uint8_t *buffer
, uint32_t len
);
280 /**************************************************************************/
282 @brief Writes a continuous stream of data that will automatically
283 cross page boundaries.
285 @note Before writing data, make sure that the appropriate sectors
286 have been erased, otherwise the data will be meaningless.
289 The 24-bit address where the write will start.
291 Pointer to the buffer that will store the read results
293 Length of the buffer, within the limits of the starting
294 address and size of the flash device.
299 spiflashError_e error;
311 error = spiflashWrite (0, buffer, 8);
314 // Check what went wrong
317 case SPIFLASH_ERROR_ADDROUTOFRANGE:
318 // Specified starting address is out of range
320 case SPIFLASH_ERROR_DATAEXCEEDSPAGESIZE:
321 // Supplied data exceeds max page size
323 case SPIFLASH_ERROR_PAGEWRITEOVERFLOW:
324 // The data length plus the start address offset exceeeds page limits
326 case SPIFLASH_ERROR_TIMEOUT_READY:
327 // Timeout waiting for ready status (can be pre or post write)
329 case SPIFLASH_ERROR_PROTECTIONERR:
330 // Unable to set write latch
336 /**************************************************************************/
337 spiflashError_e
spiflashWrite (uint32_t address
, uint8_t *buffer
, uint32_t len
);