1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
\r
2 * File Name : spi_flash.c
\r
3 * Author : MCD Application Team
\r
4 * Date First Issued : 02/05/2007
\r
5 * Description : This file provides a set of functions needed to manage the
\r
6 * communication between SPI peripheral and SPI M25P64 FLASH.
\r
7 ********************************************************************************
\r
11 ********************************************************************************
\r
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
\r
14 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
\r
15 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
\r
16 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
\r
17 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 *******************************************************************************/
\r
20 /* Includes ------------------------------------------------------------------*/
\r
21 #include "spi_flash.h"
\r
23 /* Private typedef -----------------------------------------------------------*/
\r
24 #define SPI_FLASH_PageSize 256
\r
26 #define WRITE 0x02 /* Write to Memory instruction */
\r
27 #define WRSR 0x01 /* Write Status Register instruction */
\r
28 #define WREN 0x06 /* Write enable instruction */
\r
30 #define READ 0x03 /* Read from Memory instruction */
\r
31 #define RDSR 0x05 /* Read Status Register instruction */
\r
32 #define RDID 0x9F /* Read identification */
\r
33 #define SE 0xD8 /* Sector Erase instruction */
\r
34 #define BE 0xC7 /* Bulk Erase instruction */
\r
36 #define WIP_Flag 0x01 /* Write In Progress (WIP) flag */
\r
38 #define Dummy_Byte 0xA5
\r
40 /* Private define ------------------------------------------------------------*/
\r
41 /* Private macro -------------------------------------------------------------*/
\r
42 /* Private variables ---------------------------------------------------------*/
\r
43 /* Private function prototypes -----------------------------------------------*/
\r
44 /* Private functions ---------------------------------------------------------*/
\r
46 /*******************************************************************************
\r
47 * Function Name : SPI_FLASH_Init
\r
48 * Description : Initializes the peripherals used by the SPI FLASH driver.
\r
52 *******************************************************************************/
\r
53 void SPI_FLASH_Init(void)
\r
55 SPI_InitTypeDef SPI_InitStructure;
\r
56 GPIO_InitTypeDef GPIO_InitStructure;
\r
58 /* Enable SPI1 and GPIOA clocks */
\r
59 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA, ENABLE);
\r
61 /* Configure SPI1 pins: NSS, SCK, MISO and MOSI */
\r
62 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
\r
63 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
64 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
\r
65 GPIO_Init(GPIOA, &GPIO_InitStructure);
\r
67 /* Configure PA.4 as Output push-pull, used as Flash Chip select */
\r
68 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
\r
69 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
70 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
71 GPIO_Init(GPIOA, &GPIO_InitStructure);
\r
73 /* Deselect the FLASH: Chip Select high */
\r
74 SPI_FLASH_ChipSelect(High);
\r
76 /* SPI1 configuration */
\r
77 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
\r
78 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
\r
79 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
\r
80 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
\r
81 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
\r
82 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
\r
83 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
\r
84 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
\r
85 SPI_InitStructure.SPI_CRCPolynomial = 7;
\r
86 SPI_Init(SPI1, &SPI_InitStructure);
\r
89 SPI_Cmd(SPI1, ENABLE);
\r
92 /*******************************************************************************
\r
93 * Function Name : SPI_FLASH_SectorErase
\r
94 * Description : Erases the specified FLASH sector.
\r
95 * Input : SectorAddr: address of the sector to erase.
\r
98 *******************************************************************************/
\r
99 void SPI_FLASH_SectorErase(u32 SectorAddr)
\r
101 /* Send write enable instruction */
\r
102 SPI_FLASH_WriteEnable();
\r
104 /* Sector Erase */
\r
105 /* Select the FLASH: Chip Select low */
\r
106 SPI_FLASH_ChipSelect(Low);
\r
107 /* Send Sector Erase instruction */
\r
108 SPI_FLASH_SendByte(SE);
\r
109 /* Send SectorAddr high nibble address byte */
\r
110 SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
\r
111 /* Send SectorAddr medium nibble address byte */
\r
112 SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
\r
113 /* Send SectorAddr low nibble address byte */
\r
114 SPI_FLASH_SendByte(SectorAddr & 0xFF);
\r
115 /* Deselect the FLASH: Chip Select high */
\r
116 SPI_FLASH_ChipSelect(High);
\r
118 /* Wait the end of Flash writing */
\r
119 SPI_FLASH_WaitForWriteEnd();
\r
122 /*******************************************************************************
\r
123 * Function Name : SPI_FLASH_BulkErase
\r
124 * Description : Erases the entire FLASH.
\r
128 *******************************************************************************/
\r
129 void SPI_FLASH_BulkErase(void)
\r
131 /* Send write enable instruction */
\r
132 SPI_FLASH_WriteEnable();
\r
135 /* Select the FLASH: Chip Select low */
\r
136 SPI_FLASH_ChipSelect(Low);
\r
137 /* Send Bulk Erase instruction */
\r
138 SPI_FLASH_SendByte(BE);
\r
139 /* Deselect the FLASH: Chip Select high */
\r
140 SPI_FLASH_ChipSelect(High);
\r
142 /* Wait the end of Flash writing */
\r
143 SPI_FLASH_WaitForWriteEnd();
\r
146 /*******************************************************************************
\r
147 * Function Name : SPI_FLASH_PageWrite
\r
148 * Description : Writes more than one byte to the FLASH with a single WRITE
\r
149 * cycle(Page WRITE sequence). The number of byte can't exceed
\r
150 * the FLASH page size.
\r
151 * Input : - pBuffer : pointer to the buffer containing the data to be
\r
152 * written to the FLASH.
\r
153 * - WriteAddr : FLASH's internal address to write to.
\r
154 * - NumByteToWrite : number of bytes to write to the FLASH,
\r
155 * must be equal or less than "SPI_FLASH_PageSize" value.
\r
158 *******************************************************************************/
\r
159 void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
\r
161 /* Enable the write access to the FLASH */
\r
162 SPI_FLASH_WriteEnable();
\r
164 /* Select the FLASH: Chip Select low */
\r
165 SPI_FLASH_ChipSelect(Low);
\r
166 /* Send "Write to Memory " instruction */
\r
167 SPI_FLASH_SendByte(WRITE);
\r
168 /* Send WriteAddr high nibble address byte to write to */
\r
169 SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
\r
170 /* Send WriteAddr medium nibble address byte to write to */
\r
171 SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
\r
172 /* Send WriteAddr low nibble address byte to write to */
\r
173 SPI_FLASH_SendByte(WriteAddr & 0xFF);
\r
175 /* while there is data to be written on the FLASH */
\r
176 while(NumByteToWrite--)
\r
178 /* Send the current byte */
\r
179 SPI_FLASH_SendByte(*pBuffer);
\r
180 /* Point on the next byte to be written */
\r
184 /* Deselect the FLASH: Chip Select high */
\r
185 SPI_FLASH_ChipSelect(High);
\r
187 /* Wait the end of Flash writing */
\r
188 SPI_FLASH_WaitForWriteEnd();
\r
191 /*******************************************************************************
\r
192 * Function Name : SPI_FLASH_BufferWrite
\r
193 * Description : Writes block of data to the FLASH. In this function, the
\r
194 * number of WRITE cycles are reduced, using Page WRITE sequence.
\r
195 * Input : - pBuffer : pointer to the buffer containing the data to be
\r
196 * written to the FLASH.
\r
197 * - WriteAddr : FLASH's internal address to write to.
\r
198 * - NumByteToWrite : number of bytes to write to the FLASH.
\r
201 *******************************************************************************/
\r
202 void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
\r
204 u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
\r
206 Addr = WriteAddr % SPI_FLASH_PageSize;
\r
207 count = SPI_FLASH_PageSize - Addr;
\r
208 NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
\r
209 NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
\r
211 if(Addr == 0) /* WriteAddr is SPI_FLASH_PageSize aligned */
\r
213 if(NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
\r
215 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
\r
217 else /* NumByteToWrite > SPI_FLASH_PageSize */
\r
221 SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
\r
222 WriteAddr += SPI_FLASH_PageSize;
\r
223 pBuffer += SPI_FLASH_PageSize;
\r
226 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
\r
229 else /* WriteAddr is not SPI_FLASH_PageSize aligned */
\r
231 if(NumOfPage== 0) /* NumByteToWrite < SPI_FLASH_PageSize */
\r
233 if(NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */
\r
235 temp = NumOfSingle - count;
\r
237 SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
\r
238 WriteAddr += count;
\r
241 SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
\r
245 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
\r
248 else /* NumByteToWrite > SPI_FLASH_PageSize */
\r
250 NumByteToWrite -= count;
\r
251 NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
\r
252 NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
\r
254 SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
\r
255 WriteAddr += count;
\r
260 SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
\r
261 WriteAddr += SPI_FLASH_PageSize;
\r
262 pBuffer += SPI_FLASH_PageSize;
\r
265 if(NumOfSingle != 0)
\r
267 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
\r
273 /*******************************************************************************
\r
274 * Function Name : SPI_FLASH_BufferRead
\r
275 * Description : Reads a block of data from the FLASH.
\r
276 * Input : - pBuffer : pointer to the buffer that receives the data read
\r
278 * - ReadAddr : FLASH's internal address to read from.
\r
279 * - NumByteToRead : number of bytes to read from the FLASH.
\r
282 *******************************************************************************/
\r
283 void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
\r
285 /* Select the FLASH: Chip Select low */
\r
286 SPI_FLASH_ChipSelect(Low);
\r
288 /* Send "Read from Memory " instruction */
\r
289 SPI_FLASH_SendByte(READ);
\r
291 /* Send ReadAddr high nibble address byte to read from */
\r
292 SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
\r
293 /* Send ReadAddr medium nibble address byte to read from */
\r
294 SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
\r
295 /* Send ReadAddr low nibble address byte to read from */
\r
296 SPI_FLASH_SendByte(ReadAddr & 0xFF);
\r
298 while(NumByteToRead--) /* while there is data to be read */
\r
300 /* Read a byte from the FLASH */
\r
301 *pBuffer = SPI_FLASH_SendByte(Dummy_Byte);
\r
302 /* Point to the next location where the byte read will be saved */
\r
306 /* Deselect the FLASH: Chip Select high */
\r
307 SPI_FLASH_ChipSelect(High);
\r
310 /*******************************************************************************
\r
311 * Function Name : SPI_FLASH_ReadID
\r
312 * Description : Reads FLASH identification.
\r
315 * Return : FLASH identification
\r
316 *******************************************************************************/
\r
317 u32 SPI_FLASH_ReadID(void)
\r
319 u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
\r
321 /* Select the FLASH: Chip Select low */
\r
322 SPI_FLASH_ChipSelect(Low);
\r
324 /* Send "RDID " instruction */
\r
325 SPI_FLASH_SendByte(0x9F);
\r
327 /* Read a byte from the FLASH */
\r
328 Temp0 = SPI_FLASH_SendByte(Dummy_Byte);
\r
330 /* Read a byte from the FLASH */
\r
331 Temp1 = SPI_FLASH_SendByte(Dummy_Byte);
\r
333 /* Read a byte from the FLASH */
\r
334 Temp2 = SPI_FLASH_SendByte(Dummy_Byte);
\r
336 /* Deselect the FLASH: Chip Select high */
\r
337 SPI_FLASH_ChipSelect(High);
\r
339 Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
\r
344 /*******************************************************************************
\r
345 * Function Name : SPI_FLASH_StartReadSequence
\r
346 * Description : Initiates a read data byte (READ) sequence from the Flash.
\r
347 * This is done by driving the /CS line low to select the device,
\r
348 * then the READ instruction is transmitted followed by 3 bytes
\r
349 * address. This function exit and keep the /CS line low, so the
\r
350 * Flash still being selected. With this technique the whole
\r
351 * content of the Flash is read with a single READ instruction.
\r
352 * Input : - ReadAddr : FLASH's internal address to read from.
\r
355 *******************************************************************************/
\r
356 void SPI_FLASH_StartReadSequence(u32 ReadAddr)
\r
358 /* Select the FLASH: Chip Select low */
\r
359 SPI_FLASH_ChipSelect(Low);
\r
361 /* Send "Read from Memory " instruction */
\r
362 SPI_FLASH_SendByte(READ);
\r
364 /* Send the 24-bit address of the address to read from -----------------------*/
\r
365 /* Send ReadAddr high nibble address byte */
\r
366 SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
\r
367 /* Send ReadAddr medium nibble address byte */
\r
368 SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
\r
369 /* Send ReadAddr low nibble address byte */
\r
370 SPI_FLASH_SendByte(ReadAddr & 0xFF);
\r
373 /*******************************************************************************
\r
374 * Function Name : SPI_FLASH_ReadByte
\r
375 * Description : Reads a byte from the SPI Flash.
\r
376 * This function must be used only if the Start_Read_Sequence
\r
377 * function has been previously called.
\r
380 * Return : Byte Read from the SPI Flash.
\r
381 *******************************************************************************/
\r
382 u8 SPI_FLASH_ReadByte(void)
\r
384 return (SPI_FLASH_SendByte(Dummy_Byte));
\r
387 /*******************************************************************************
\r
388 * Function Name : SPI_FLASH_ChipSelect
\r
389 * Description : Selects or deselects the FLASH.
\r
390 * Input : State : level to be applied on the FLASH's ChipSelect pin.
\r
393 *******************************************************************************/
\r
394 void SPI_FLASH_ChipSelect(u8 State)
\r
396 /* Set High or low the chip select line on PA.4 pin */
\r
397 GPIO_WriteBit(GPIOA, GPIO_Pin_4, (BitAction)State);
\r
400 /*******************************************************************************
\r
401 * Function Name : SPI_FLASH_SendByte
\r
402 * Description : Sends a byte through the SPI interface and return the byte
\r
403 * received from the SPI bus.
\r
404 * Input : byte : byte to send.
\r
406 * Return : The value of the received byte.
\r
407 *******************************************************************************/
\r
408 u8 SPI_FLASH_SendByte(u8 byte)
\r
410 /* Loop while DR register in not emplty */
\r
411 while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
\r
413 /* Send byte through the SPI1 peripheral */
\r
414 SPI_SendData(SPI1, byte);
\r
416 /* Wait to receive a byte */
\r
417 while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
\r
419 /* Return the byte read from the SPI bus */
\r
420 return SPI_ReceiveData(SPI1);
\r
423 /*******************************************************************************
\r
424 * Function Name : SPI_FLASH_SendHalfWord
\r
425 * Description : Sends a Half Word through the SPI interface and return the
\r
426 * Half Word received from the SPI bus.
\r
427 * Input : Half Word : Half Word to send.
\r
429 * Return : The value of the received Half Word.
\r
430 *******************************************************************************/
\r
431 u16 SPI_FLASH_SendHalfWord(u16 HalfWord)
\r
433 /* Loop while DR register in not emplty */
\r
434 while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
\r
436 /* Send Half Word through the SPI1 peripheral */
\r
437 SPI_SendData(SPI1, HalfWord);
\r
439 /* Wait to receive a Half Word */
\r
440 while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
\r
442 /* Return the Half Word read from the SPI bus */
\r
443 return SPI_ReceiveData(SPI1);
\r
446 /*******************************************************************************
\r
447 * Function Name : SPI_FLASH_WriteEnable
\r
448 * Description : Enables the write access to the FLASH.
\r
452 *******************************************************************************/
\r
453 void SPI_FLASH_WriteEnable(void)
\r
455 /* Select the FLASH: Chip Select low */
\r
456 SPI_FLASH_ChipSelect(Low);
\r
458 /* Send "Write Enable" instruction */
\r
459 SPI_FLASH_SendByte(WREN);
\r
461 /* Deselect the FLASH: Chip Select high */
\r
462 SPI_FLASH_ChipSelect(High);
\r
465 /*******************************************************************************
\r
466 * Function Name : SPI_FLASH_WaitForWriteEnd
\r
467 * Description : Polls the status of the Write In Progress (WIP) flag in the
\r
468 * FLASH's status register and loop until write opertaion
\r
473 *******************************************************************************/
\r
474 void SPI_FLASH_WaitForWriteEnd(void)
\r
476 u8 FLASH_Status = 0;
\r
478 /* Select the FLASH: Chip Select low */
\r
479 SPI_FLASH_ChipSelect(Low);
\r
481 /* Send "Read Status Register" instruction */
\r
482 SPI_FLASH_SendByte(RDSR);
\r
484 /* Loop as long as the memory is busy with a write cycle */
\r
488 /* Send a dummy byte to generate the clock needed by the FLASH
\r
489 and put the value of the status register in FLASH_Status variable */
\r
490 FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);
\r
492 } while((FLASH_Status & WIP_Flag) == SET); /* Write in progress */
\r
494 /* Deselect the FLASH: Chip Select high */
\r
495 SPI_FLASH_ChipSelect(High);
\r
498 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
\r