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
60 /**************************************************************************/
62 @brief Describes the storage capacity of the SPI flash, including the
63 size of the minimum HW write (page) and erase (sector) units.
65 /**************************************************************************/
68 uint32_t pageSize
; // Page size to write data (in bytes)
69 uint32_t pageCount
; // Number of pages
70 uint32_t sectorSize
; // Sector size to erase data (in bytes)
71 uint32_t sectorCount
; // Number of sectors
75 /**************************************************************************/
77 @brief Tries to initialise the flash device, and setups any HW
78 required by the SPI flash
80 /**************************************************************************/
81 void spiflashInit (void);
83 /**************************************************************************/
85 @brief Gets an instance of spiflashSizeInfo_t that describes the
86 storage limits of the SPI flash like page size (minimum write
87 size) and sector size (minimum erase size).
89 /**************************************************************************/
90 spiflashSizeInfo_t
spiflashGetSizeInfo(void);
92 /**************************************************************************/
94 @brief Gets the 8-bit manufacturer ID and device ID for the flash
97 Pointer to the uint8_t that will store the manufacturer ID
99 Pointer to the uint8_t that will store the device ID
101 /**************************************************************************/
102 void spiflashGetManufacturerInfo (uint8_t *manufID
, uint8_t *deviceID
);
104 /**************************************************************************/
106 @brief Sets the write flag on the SPI flash, and if required puts the
107 WP pin in an appropriate state
110 True (1) to enable writing, false (0) to disable it
112 /**************************************************************************/
113 void spiflashWriteEnable (bool enable
);
115 /**************************************************************************/
117 @brief Reads the specified number of bytes from the supplied address.
119 This function will read one or more bytes starting at the supplied
120 address. Please note that bufferLength is zero-based, meaning you
121 should supply '0' to read a single byte, '3' to read 4 bytes of data,
125 The 24-bit address where the read will start.
127 Pointer to the buffer that will store the read results
129 Length of the buffer (zero-based).
135 spiflashError_e error;
136 error = spiflashReadBuffer (0, buffer, 64);
139 // Check what went wrong
142 case SPIFLASH_ERROR_ADDROUTOFRANGE:
143 // Specified starting address is out of range
145 case SPIFLASH_ERROR_TIMEOUT_READY:
146 // Timed out waiting for flash to return ready state
148 case SPIFLASH_ERROR_ADDROVERFLOW:
149 // Ran over the upper address during read
155 /**************************************************************************/
156 spiflashError_e
spiflashReadBuffer (uint32_t address
, uint8_t *buffer
, uint32_t len
);
158 /**************************************************************************/
160 @brief Writes a single byte to the SPI flash
163 The 24-bit address where the read will start.
165 Pointer to the buffer that will store the read results
167 Length of the buffer (zero-based).
169 /**************************************************************************/
170 spiflashError_e
spiflashWriteBuffer (uint32_t address
, uint8_t *buffer
, uint32_t len
);
172 /**************************************************************************/
174 @brief Erases the contents of a single sector
176 @param[in] sectorNumber
177 The sector number to erase (zero-based).
182 spiflashError_e error;
183 error = spiflashEraseSector(0);
186 // Check what went wrong
189 case SPIFLASH_ERROR_ADDROUTOFRANGE:
190 // Specified starting address is out of range
192 case SPIFLASH_ERROR_PROTECTIONERR:
193 // Couldn't set the write enable bit
195 case SPIFLASH_ERROR_TIMEOUT_READY:
196 // Timed out waiting for flash to return ready state
202 /**************************************************************************/
203 spiflashError_e
spiflashEraseSector (uint32_t sectorNumber
);
205 /**************************************************************************/
207 @brief Erases the entire flash chip
212 spiflashError_e error;
213 error = spiflashEraseChip();
216 // Check what went wrong
219 case SPIFLASH_ERROR_PROTECTIONERR:
220 // Couldn't set the write enable bit
222 case SPIFLASH_ERROR_TIMEOUT_READY:
223 // Timed out waiting for flash to return ready state
229 /**************************************************************************/
230 spiflashError_e
spiflashEraseChip (void);