1 |
9 |
nussgipfel |
/***********************************************************
|
2 |
|
|
* Gecko3 SoC HW/SW Development Board
|
3 |
|
|
* ___ ___ _ _
|
4 |
|
|
* ( _`\ ( __)( ) ( )
|
5 |
|
|
* | (_) )| ( | |_| | Berne University of Applied Sciences
|
6 |
|
|
* | _ <'| _) | _ | School of Engineering and
|
7 |
|
|
* | (_) )| | | | | | Information Technology
|
8 |
|
|
* (____/'(_) (_) (_)
|
9 |
|
|
*
|
10 |
|
|
*
|
11 |
|
|
* This program is free software: you can redistribute it and/or modify
|
12 |
|
|
* it under the terms of the GNU General Public License as published by
|
13 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
14 |
|
|
* (at your option) any later version.
|
15 |
|
|
*
|
16 |
|
|
* This program is distributed in the hope that it will be useful,
|
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
|
|
* GNU General Public License for more details.
|
20 |
|
|
* You should have received a copy of the GNU General Public License
|
21 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22 |
|
|
*/
|
23 |
|
|
|
24 |
|
|
/************************************************************/
|
25 |
|
|
/** \file spi_flash.h
|
26 |
|
|
*************************************************************
|
27 |
|
|
* \author Christoph Zimmermann
|
28 |
|
|
* \date Date of creation: 17.09.2007
|
29 |
|
|
* \brief Headerfile for the spi-flash Library
|
30 |
|
|
*
|
31 |
|
|
* \details Library to access the SPI Flash devices from ST
|
32 |
|
|
* Microelectronics (now Numonyx) or Spansion.
|
33 |
|
|
* Supported densities:
|
34 |
|
|
* 8, 16, 32 Mbit
|
35 |
|
|
*
|
36 |
|
|
* \note to use this SPI/SPI Flash library you have to define the following
|
37 |
|
|
* keywords in your pinmapping header file. For GECKO3COM
|
38 |
|
|
* this is the "gecko3com_regs.h" file. \n
|
39 |
|
|
* \li SPI_PORT, SPI signals are connected to this port
|
40 |
|
|
* \li SPI_OE, SPI port direction register
|
41 |
|
|
* \li bmSPI_CLK, bitmask for SPI serial clock pin
|
42 |
|
|
* \li bmSPI_MOSI, bitmask for SPI MOSI pin, Master Out, Slave In
|
43 |
|
|
* \li bmSPI_MISO, bitmask for SPI MISO pin, Master In, Slave Out
|
44 |
|
|
* \li bitSPI_CLK, bitadress of the SPI CLK pin
|
45 |
|
|
* \li bitSPI_MOSI, bitadress of the SPI MOSI pin
|
46 |
|
|
* \li bitSPI_MISO, bitadress of the SPI MISO pin
|
47 |
|
|
* \li SPI_CS_PORT, SPI chip select signals are connected to this port
|
48 |
|
|
* \li bmSPI_CS_FLASH, bitmask to enable the SPI Flash
|
49 |
|
|
* \li bmSPI_CS_MASK, bit mask to select the SPI chip select pins
|
50 |
|
|
*
|
51 |
|
|
* \date 17.09.2007 first version
|
52 |
|
|
*
|
53 |
|
|
*/
|
54 |
|
|
|
55 |
|
|
#ifndef SPI_FLASH /* prevent circular inclusions */
|
56 |
|
|
#define SPI_FLASH /* by using protection macros */
|
57 |
|
|
|
58 |
|
|
#include <stdint.h>
|
59 |
|
|
//#include "spi.h"
|
60 |
|
|
|
61 |
|
|
/**************************** Type Definitions *******************************/
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* \brief This typedef contains configuration information for the device.
|
65 |
|
|
*/
|
66 |
|
|
typedef struct
|
67 |
|
|
{
|
68 |
|
|
uint32_t maxAdress; /**< highest available adress */
|
69 |
|
|
uint32_t capacity; /**< memory capacity in bytes */
|
70 |
|
|
uint16_t pages; /**< number of available pages */
|
71 |
|
|
uint8_t sectors; /**< number of available sectors */
|
72 |
|
|
uint8_t isBusy; /**< acivity indicator */
|
73 |
|
|
} SPI_flash;
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
#define FLASH_WRITE_BUFFER_SIZE 0x100 /**< Write Buffer = 256 bytes */
|
77 |
|
|
|
78 |
13 |
nussgipfel |
#define SPIFLASH_SECTOR_LENGTH 0x10000 /**< Length of one sector in the flash, needed for erasing */
|
79 |
|
|
#define SECTORS_PER_SLOT 16 /**< Number of sectors used for each configuration file slot */
|
80 |
|
|
|
81 |
9 |
nussgipfel |
#define MANUFACTURER_STM 0x20 /**< id of ST microelectronics */
|
82 |
|
|
#define MEMTYPE_STM 0x20 /**< Memory type of M25PXX */
|
83 |
|
|
#define MEMCAPACITY_8MBIT_STM 0x14 /**< 8 MBit memory capacity */
|
84 |
|
|
#define MEMCAPACITY_16MBIT_STM 0x15 /**< 16 MBit memory capacity */
|
85 |
|
|
#define MEMCAPACITY_32MBIT_STM 0x16 /**< 32 MBit memory capacity */
|
86 |
|
|
|
87 |
|
|
#define MANUFACTURER_SPA 0x01 /**< id of SPANSION */
|
88 |
|
|
#define MEMTYPE_SPA 0x02 /**< Memory type of S25FLXX */
|
89 |
|
|
#define MEMCAPACITY_8MBIT_SPA 0x13 /**< 8 MBit memory capacity */
|
90 |
|
|
#define MEMCAPACITY_16MBIT_SPA 0x14 /**< 16 MBit memory capacity */
|
91 |
|
|
#define MEMCAPACITY_32MBIT_SPA 0x15 /**< 32 MBit memory capacity */
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
#define MAXADRESS_8MBIT 0x0FFFFF
|
95 |
|
|
#define FLASH_SIZE_8MBIT (0x0100000) /**< Total 8 Mbit device size in Bytes */
|
96 |
|
|
#define FLASH_PAGE_COUNT_8MBIT (0x01000) /**< Total 8 Mbit device size in Pages */
|
97 |
|
|
#define FLASH_SECTOR_COUNT_8MBIT (0x10) /**< Total 8 Mbit device size in Sectors */
|
98 |
|
|
|
99 |
|
|
#define MAXADRESS_16MBIT 0x1FFFFF
|
100 |
|
|
#define FLASH_SIZE_16MBIT (0x0200000) /**< Total 16 Mbit device size in Bytes */
|
101 |
|
|
#define FLASH_PAGE_COUNT_16MBIT (0x02000) /**< Total 16 Mbit device size in Pages */
|
102 |
|
|
#define FLASH_SECTOR_COUNT_16MBIT (0x20) /**< Total 16 Mbit device size in Sectors */
|
103 |
|
|
|
104 |
|
|
#define MAXADRESS_32MBIT 0x3FFFFF
|
105 |
|
|
#define FLASH_SIZE_32MBIT (0x0400000) /**< Total 32 Mbit device size in Bytes */
|
106 |
|
|
#define FLASH_PAGE_COUNT_32MBIT (0x04000) /**< Total 32 Mbit device size in Pages */
|
107 |
|
|
#define FLASH_SECTOR_COUNT_32MBIT (0x40) /**< Total 32 Mbit device size in Sectors */
|
108 |
|
|
|
109 |
|
|
/* Return Codes */
|
110 |
|
|
#define GOOD 0 /**< anything ok */
|
111 |
|
|
#define BAD 1 /**< error */
|
112 |
|
|
#define NOINSTANCE 99 /**< no device found */
|
113 |
|
|
#define UNSUPPORTED_TYPE 5 /**< a device was found but it is not supported */
|
114 |
|
|
#define OVERFLOW 10 /**< an overflow occoured */
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
/* flash memory opcodes */
|
118 |
|
|
#define WREN 0x06 /**< Flash Opcode: Write Enable */
|
119 |
|
|
#define WRDI 0x04 /**< Flash Opcode: Write Disable */
|
120 |
|
|
#define RDID 0x9F /**< Flash Opcode: Read Identification */
|
121 |
|
|
#define RDSR 0x05 /**< Flash Opcode: Read Status Register */
|
122 |
|
|
#define WRSR 0x01 /**< Flash Opcode: Write Status Register */
|
123 |
|
|
#define READ 0x03 /**< Flash Opcode: Read Data bytes */
|
124 |
|
|
#define FAST_READ 0x0B /**< Flash Opcode: Read Data bytes at higher speed */
|
125 |
|
|
#define PP 0x02 /**< Flash Opcode: Page Programm */
|
126 |
|
|
#define SE 0xD8 /**< Flash Opcode: Sector Erase */
|
127 |
|
|
#define BE 0xC7 /**< Flash Opcode: Bulk Erase */
|
128 |
|
|
#define DP 0xB9 /**< Flash Opcode: Deep Power-down */
|
129 |
|
|
#define RES 0xAB /**< Flash Opcode: Release from Deep Power-down */
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
/***************** Macros (Inline Functions) Definitions *********************/
|
133 |
|
|
|
134 |
|
|
/** \brief check if it is a sane pointer
|
135 |
|
|
* \param[in] ptr pointer to a SPI_flash struct
|
136 |
|
|
*/
|
137 |
|
|
#define ptrCheck(ptr) \
|
138 |
|
|
{ \
|
139 |
|
|
if (ptr == 0) \
|
140 |
|
|
{ \
|
141 |
|
|
return NOINSTANCE; \
|
142 |
|
|
} \
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
/** \brief check if a overflow would occour with the given parameters
|
147 |
|
|
*
|
148 |
|
|
* \param[in] flashPtr pointer to a initialized SPI_flash struct
|
149 |
|
|
* \param[in] adr adress somewhere in the flash
|
150 |
|
|
* \param[in] byteCount number of bytes to process
|
151 |
|
|
* \exception breaks the operations and returns the OVERFLOW error code
|
152 |
|
|
*/
|
153 |
|
|
#define adrCheck(flashPtr, adr, byteCount) \
|
154 |
|
|
{ \
|
155 |
|
|
if (adr + byteCount >= flashPtr->maxAdress) \
|
156 |
|
|
{ \
|
157 |
|
|
return OVERFLOW; \
|
158 |
|
|
} \
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
/** returns the start adress of the sector that belongs to this adress
|
164 |
|
|
* \param[in] adr adress somewhere in the flash
|
165 |
|
|
*/
|
166 |
|
|
#define sectorStart(adr) ((adr) & 0xFFFF0000)
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
/** returns the end adress of the sector that belongs to this adress
|
170 |
|
|
* \param[in] adr adress somewhere in the flash
|
171 |
|
|
*/
|
172 |
|
|
#define sectorEnd(adr) ((adr) | 0x0000FFFF)
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
/** returns the start adress of the page that belongs to this adress
|
176 |
|
|
* \param[in] adr adress somewhere in the flash
|
177 |
|
|
*/
|
178 |
|
|
#define pageStart(adr) ((adr) & 0xFFFFFF00)
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
/** returns the end adress of the page that belongs to this adress
|
182 |
|
|
* \param[in] adr adress somewhere in the flash
|
183 |
|
|
*/
|
184 |
|
|
#define pageEnd(adr) ((adr) | 0x000000FF)
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
extern SPI_flash xdata flash_dr;
|
188 |
|
|
|
189 |
|
|
/************************** Function Prototypes ******************************/
|
190 |
|
|
|
191 |
|
|
/** \brief Checks if the SPI flash is busy
|
192 |
|
|
*
|
193 |
|
|
* \param[in] flashPtr pointer to an SPI_flash struct
|
194 |
|
|
* \return returns non-zero if SPI flash is busy, else 0
|
195 |
|
|
*/
|
196 |
|
|
int8_t spiflash_is_busy(xdata SPI_flash *flashPtr);
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
/** \brief Initalizes the values in the SPI_flash struct after reading
|
200 |
|
|
* the device ID
|
201 |
|
|
*
|
202 |
|
|
* \param[in] flashPtr pointer to an uninitialized SPI_flash struct
|
203 |
|
|
* \return returns GOOD (0) or an Error Code (non-zero)
|
204 |
|
|
*/
|
205 |
|
|
int8_t init_spiflash(xdata SPI_flash *flashPtr);
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
/** \brief Reads data from the SPI flash
|
209 |
|
|
*
|
210 |
|
|
* \param[in] xdata SPI_flash flashPtr pointer to an SPI_flash struct
|
211 |
|
|
* \param[in] xdata uint32_t *adress pointer to the flash start adress to read
|
212 |
|
|
* from
|
213 |
|
|
* \param[out] xdata uint8_t *buffer, pointer to a buffer to write the data into
|
214 |
|
|
* \param[in] length of the data to read
|
215 |
|
|
* \return returns non-zero if successful, else 0
|
216 |
|
|
*/
|
217 |
|
|
int8_t spiflash_read(xdata SPI_flash *flashPtr, xdata uint32_t *adress, xdata uint8_t *buffer, const idata uint16_t length);
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
/** \brief deletes the whole SPI flash
|
221 |
|
|
*
|
222 |
|
|
* \param[in] *flashPtr pointer to an SPI_flash struct
|
223 |
|
|
* \return returns non-zero if successful, else 0
|
224 |
|
|
*/
|
225 |
|
|
int8_t spiflash_erase_bulk(xdata SPI_flash *flashPtr);
|
226 |
|
|
|
227 |
|
|
|
228 |
|
|
/** \brief deletes one sector (64 kbyte) of the SPI flash
|
229 |
|
|
*
|
230 |
|
|
* \param[in] *flashPtr flashPtr pointer to an SPI_flash struct
|
231 |
|
|
* \param[in] *adress pointer to the flash adress in the sector to be erased
|
232 |
|
|
* \return returns non-zero if successful, else 0
|
233 |
|
|
*/
|
234 |
|
|
int8_t spiflash_erase(xdata SPI_flash *flashPtr, xdata uint32_t *adress);
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
/** \brief Writes data to the SPI flash
|
238 |
|
|
*
|
239 |
|
|
* This write function handles if you write data over the page adress
|
240 |
|
|
* boundary. It splits it into seperate page program commands.
|
241 |
|
|
* \note Don't forget to erase a sector before you try to write to it!
|
242 |
|
|
*
|
243 |
|
|
* \param[in] *flashPtr pointer to an SPI_flash struct
|
244 |
|
|
* \param[in] *adress pointer to the flash start adress to
|
245 |
|
|
* write to
|
246 |
|
|
* \param[out] *buffer pointer to a buffer to write the data into
|
247 |
|
|
* \param[in] length of the data to read
|
248 |
|
|
* \return returns non-zero if successful, else 0
|
249 |
|
|
*/
|
250 |
|
|
int8_t spiflash_write(xdata SPI_flash *flashPtr, xdata uint32_t *adress, xdata uint8_t *buffer, uint16_t length);
|
251 |
|
|
|
252 |
|
|
#ifdef __cplusplus
|
253 |
|
|
}
|
254 |
|
|
#endif
|
255 |
|
|
|
256 |
|
|
#endif
|