]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libboard_samv7-ek/include/s25fl1_qspi.h
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained_IAR_Keil / libboard_samv7-ek / include / s25fl1_qspi.h
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2010, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 /**\r
31  * \file\r
32  *\r
33  * Interface for the S25fl1 SPI driver.\r
34  *\r
35  */\r
36 \r
37 #ifndef S25FL1_SPI_H\r
38 #define S25FL1_SPI_H\r
39 \r
40 /*----------------------------------------------------------------------------\r
41  *        Headers\r
42  *----------------------------------------------------------------------------*/\r
43 #include <board.h>\r
44 \r
45 /*----------------------------------------------------------------------------\r
46  *        Macros\r
47  *----------------------------------------------------------------------------*/\r
48 \r
49 #define S25FL1_Size(pS25fl1)            ((pS25fl1)->pDesc->size)\r
50 #define S25FL1_PageSize(pS25fl1)        ((pS25fl1)->pDesc->pageSize)\r
51 #define S25FL1_BlockSize(pS25fl1)       ((pS25fl1)->pDesc->blockSize)\r
52 #define S25FL1_Name(pS25fl1)            ((pS25fl1)->pDesc->name)\r
53 #define S25FL1_ManId(pS25fl1)           (((pS25fl1)->pDesc->jedecId) & 0xFF)\r
54 #define S25FL1_PageNumber(pS25fl1)      (S25FL1_Size(pS25FL1) / S25FL1_PageSize(pS25fl1))\r
55 #define S25FL1_BlockNumber(pS25fl1)     (S25FL1_Size(pS25fl1) / S25FL1_BlockSize(pS25fl1))\r
56 #define S25FL1_PagePerBlock(pS25fl1)    (S25FL1_BlockSize(pS25fl1) / S25FL1_PageSize(pS25fl1))\r
57 #define S25FL1_BlockEraseCmd(pS25fl1)   ((pS25fl1)->pDesc->blockEraseCmd)\r
58 \r
59 /*----------------------------------------------------------------------------\r
60  *        Local definitions\r
61  *----------------------------------------------------------------------------*/\r
62 \r
63 /** Device is protected, operation cannot be carried out. */\r
64 #define S25FL1_ERROR_PROTECTED        1\r
65 /** Device is busy executing a command. */\r
66 #define S25FL1_ERROR_BUSY             2\r
67 /** There was a problem while trying to program page data. */\r
68 #define S25FL1_ERROR_PROGRAM          3\r
69 /** There was an SPI communication error. */\r
70 #define S25FL1_ERROR_SPI              4\r
71 \r
72 /** Device ready/busy status bit. */\r
73 #define S25FL1_STATUS_RDYBSY          (1 << 0)\r
74 /** Device is ready. */\r
75 #define S25FL1_STATUS_RDYBSY_READY    (0 << 0)\r
76 /** Device is busy with internal operations. */\r
77 #define S25FL1_STATUS_RDYBSY_BUSY     (1 << 0)\r
78 /** Write enable latch status bit. */\r
79 #define S25FL1_STATUS_WEL             (1 << 1)\r
80 /** Device is not write enabled. */\r
81 #define S25FL1_STATUS_WEL_DISABLED    (0 << 1)\r
82 /** Device is write enabled. */\r
83 #define S25FL1_STATUS_WEL_ENABLED     (1 << 1)\r
84 /** Software protection status bitfield. */\r
85 #define S25FL1_STATUS_SWP             (3 << 2)\r
86 /** All sectors are software protected. */\r
87 #define S25FL1_STATUS_SWP_PROTALL     (3 << 2)\r
88 /** Some sectors are software protected. */\r
89 #define S25FL1_STATUS_SWP_PROTSOME    (1 << 2)\r
90 /** No sector is software protected. */\r
91 #define S25FL1_STATUS_SWP_PROTNONE    (0 << 2)\r
92 /** Write protect pin status bit. */\r
93 #define S25FL1_STATUS_WPP             (1 << 4)\r
94 /** Write protect signal is not asserted. */\r
95 #define S25FL1_STATUS_WPP_NOTASSERTED (0 << 4)\r
96 /** Write protect signal is asserted. */\r
97 #define S25FL1_STATUS_WPP_ASSERTED    (1 << 4)\r
98 /** Erase/program error bit. */\r
99 #define S25FL1_STATUS_EPE             (1 << 5)\r
100 /** Erase or program operation was successful. */\r
101 #define S25FL1_STATUS_EPE_SUCCESS     (0 << 5)\r
102 /** Erase or program error detected. */\r
103 #define S25FL1_STATUS_EPE_ERROR       (1 << 5)\r
104 /** Sector protection registers locked bit. */\r
105 #define S25FL1_STATUS_SPRL            (1 << 7)\r
106 /** Sector protection registers are unlocked. */\r
107 #define S25FL1_STATUS_SPRL_UNLOCKED   (0 << 7)\r
108 /** Sector protection registers are locked. */\r
109 #define S25FL1_STATUS_SPRL_LOCKED     (1 << 7)\r
110 \r
111 /** Read array command code. */\r
112 #define S25FL1_READ_ARRAY             0x0B\r
113 /** Read array (low frequency) command code. */\r
114 #define S25FL1_READ_ARRAY_LF          0x03\r
115 /** Block erase command code (4K block). */\r
116 #define S25FL1_BLOCK_ERASE_4K         0x20\r
117 /** Block erase command code (32K block). */\r
118 #define S25FL1_BLOCK_ERASE_32K        0x52\r
119 /** Block erase command code (64K block). */\r
120 #define S25FL1_BLOCK_ERASE_64K        0xD8\r
121 /** Chip erase command code 1. */\r
122 #define S25FL1_CHIP_ERASE_1           0x60\r
123 /** Chip erase command code 2. */\r
124 #define S25FL1_CHIP_ERASE_2           0xC7\r
125 /** Byte/page program command code. */\r
126 #define S25FL1_BYTE_PAGE_PROGRAM      0x02\r
127 /** Sequential program mode command code 1. */\r
128 #define S25FL1_SEQUENTIAL_PROGRAM_1   0xAD\r
129 /** Sequential program mode command code 2. */\r
130 #define S25FL1_SEQUENTIAL_PROGRAM_2   0xAF\r
131 /** Write enable command code. */\r
132 #define S25FL1_WRITE_ENABLE           0x06\r
133 /** Write disable command code. */\r
134 #define S25FL1_WRITE_DISABLE          0x04\r
135 /** Protect sector command code. */\r
136 #define S25FL1_PROTECT_SECTOR         0x36\r
137 /** Unprotect sector command code. */\r
138 #define S25FL1_UNPROTECT_SECTOR       0x39\r
139 /** Read sector protection registers command code. */\r
140 #define S25FL1_READ_SECTOR_PROT       0x3C\r
141 /** Read status register command code. */\r
142 #define S25FL1_READ_STATUS            0x05\r
143 /** Write status register command code. */\r
144 #define S25FL1_WRITE_STATUS           0x01\r
145 /** Read manufacturer and device ID command code. */\r
146 #define S25FL1_READ_JEDEC_ID          0x9F\r
147 /** Deep power-down command code. */\r
148 #define S25FL1_DEEP_PDOWN             0xB9\r
149 /** Resume from deep power-down command code. */\r
150 #define S25FL1_RES_DEEP_PDOWN         0xAB\r
151 \r
152 /* Enter 4-BYTE ADDRESS mode  */\r
153 #define S25FL1_ENTER_4ADDR_MODE       0xB7\r
154 /* Exit 4-BYTE ADDRESS mode  */\r
155 #define S25FL1_EXIT_4ADDR_MODE        0xE9\r
156 \r
157 /** SPI Flash Manufacturer JEDEC ID */\r
158 #define ATMEL_SPI_FLASH             0x1F\r
159 #define ST_SPI_FLASH                0x20\r
160 #define WINBOND_SPI_FLASH           0xEF\r
161 #define MACRONIX_SPI_FLASH          0xC2\r
162 #define SST_SPI_FLASH               0xBF\r
163 \r
164 /*----------------------------------------------------------------------------\r
165  *        Types\r
166  *----------------------------------------------------------------------------*/\r
167 \r
168 /** Describes a serial firmware flash device parameters. */\r
169 typedef struct _S25fl1Desc {\r
170 \r
171     /** Device string name. */\r
172     const char *name;\r
173     /** JEDEC ID of device. */\r
174     uint32_t jedecId;\r
175     /** Size of device in bytes. */\r
176     uint32_t size;\r
177     /** Size of one page in bytes. */\r
178     uint32_t pageSize;\r
179     /** Block erase size in bytes. */\r
180     uint32_t blockSize;\r
181     /** Block erase command. */\r
182     uint32_t blockEraseCmd;\r
183 \r
184 } S25fl1Desc;\r
185 \r
186 /**\r
187  * Serial flash driver structure. Holds the current state of the driver,\r
188  * including the current command and the descriptor for the underlying device.\r
189  */\r
190 typedef struct _S25fl1 {\r
191 \r
192     /** Pointer to the underlying QSPI driver. */\r
193     Qspid *pQspid;\r
194     /** Current command sent to the QSPI driver. */\r
195     QspidCmd command;\r
196     /** Pointer to a descriptor for the serial firmware flash device. */\r
197     const S25fl1Desc *pDesc;\r
198     /** Qspi Command buffer. */\r
199     qspiFrame CmdBuffer;\r
200     /** Polling mode */\r
201     uint32_t pollingMode;\r
202     /** Support for 4 byte address mode */\r
203     uint32_t fourbytemode;\r
204 } S25fl1;\r
205 \r
206 /*----------------------------------------------------------------------------\r
207  *        Exported functions\r
208  *----------------------------------------------------------------------------*/\r
209 \r
210 extern void S25fl1_Configure(S25fl1 *pS25fl1,\r
211                            Qspid *pQspid,\r
212                            uint8_t cs,\r
213                            uint8_t polling);\r
214 \r
215 extern uint8_t S25fl1_SendCommand(\r
216     S25fl1 *pS25fl1,\r
217     uint8_t cmd,\r
218     uint8_t cmdSize,\r
219     uint8_t *pData,\r
220     uint32_t dataSize,\r
221     uint32_t address,\r
222     QspidCallback callback,\r
223     void *pArgument);\r
224 \r
225 extern uint8_t S25fl1_IsBusy(S25fl1 *pS25fl1);\r
226 \r
227 extern const S25fl1Desc * S25fl1_FindDevice(\r
228     S25fl1 *pS25fl1,\r
229     uint32_t jedecId);\r
230 \r
231 #endif /*#ifndef S25FL1_SPI_H */\r
232 \r