找回密码
 注册

QQ登录

只需一步,快速开始

搜索

stm32f407硬件spi读写sst25vf闪存flash代码

[复制链接]
coolice 发表于 2021-8-13 23:49:24 | 显示全部楼层 |阅读模式
  1. #ifndef __SPI_FLASH_H
  2. #define __SPI_FLASH_H

  3. /* Includes ------------------------------------------------------------------*/
  4. #include "stm324xg_eval.h"


  5. /* Private typedef -----------------------------------------------------------*/
  6. #define SPI_FLASH_PageSize    0x100

  7. /* Private define ------------------------------------------------------------*/
  8. #define WRITE      0x02  /* Write to Memory instruction */
  9. #define WRSR       0x01  /* Write Status Register instruction */
  10. #define WREN       0x06  /* Write enable instruction */
  11. #define WRDI       0x04  /* Write disable instruction */

  12. #define READ       0x03  /* Read from Memory instruction */
  13. #define RDSR       0x05  /* Read Status Register instruction  */
  14. #define RDID       0x9F  /* Read identification */
  15. #define SE         0xD8  /* Sector Erase instruction */
  16. #define BE         0xC7  /* Bulk Erase instruction */
  17. #define CE         0x60  /* Chip Erase instruction */
  18. #define EWSR       0x50  /* Enable Write Status Register instruction */

  19. #define WIP_Flag   0x01  /* Write In Progress (WIP) flag */

  20. #define Dummy_Byte 0xA5

  21. #define countof(a) (sizeof(a) / sizeof(*(a)))
  22. /*for M25P64 SERIAL FLASH---------------------------------------------------*/
  23. #define  FLASH_WriteAddress     0x700000
  24. #define  FLASH_ReadAddress      FLASH_WriteAddress
  25. #define  FLASH_SectorToErase    FLASH_WriteAddress
  26. #define  M25P64_FLASH_ID        0x202017
  27. #define  BufferSize (countof(Tx_Buffer)-1)

  28. #define FLASH_SPI                       SPI1
  29. #define FLASH_SPI_CLK                   RCC_APB2Periph_SPI1

  30. #define FLASH_SPI_SCK_PIN               GPIO_Pin_5                  /* PA.05 */
  31. #define FLASH_SPI_SCK_GPIO_PORT         GPIOA                       /* GPIOA */
  32. #define FLASH_SPI_SCK_GPIO_CLK          RCC_AHB1Periph_GPIOA
  33. #define FLASH_SPI_SCK_SOURCE            GPIO_PinSource5
  34. #define FLASH_SPI_SCK_AF                GPIO_AF_SPI1

  35. #define FLASH_SPI_MISO_PIN              GPIO_Pin_6                  
  36. #define FLASH_SPI_MISO_GPIO_PORT        GPIOA                     
  37. #define FLASH_SPI_MISO_GPIO_CLK         RCC_AHB1Periph_GPIOA
  38. #define FLASH_SPI_MISO_SOURCE           GPIO_PinSource6
  39. #define FLASH_SPI_MISO_AF               GPIO_AF_SPI1

  40. #define FLASH_SPI_MOSI_PIN              GPIO_Pin_5                  /* PB.5 */
  41. #define FLASH_SPI_MOSI_GPIO_PORT        GPIOB                       /* GPIOB */
  42. #define FLASH_SPI_MOSI_GPIO_CLK         RCC_AHB1Periph_GPIOB
  43. #define FLASH_SPI_MOSI_SOURCE           GPIO_PinSource5
  44. #define FLASH_SPI_MOSI_AF               GPIO_AF_SPI1

  45. #define FLASH_SPI_CS_PIN                GPIO_Pin_7/* PB.00 */
  46. #define FLASH_SPI_CS_GPIO_PORT          GPIOE                       /* GPIOB */
  47. #define FLASH_SPI_CS_GPIO_CLK           RCC_AHB1Periph_GPIOE

  48. #define FLASH_SPI_157_PIN                GPIO_Pin_1                  /* PB.01 */
  49. #define FLASH_SPI_157_GPIO_PORT          GPIOB                       /* GPIOB */
  50. #define FLASH_SPI_157_GPIO_CLK           RCC_AHB1Periph_GPIOB

  51. /* Exported macro ------------------------------------------------------------*/
  52. /* Select SPI FLASH: Chip Select pin low  */
  53. #define SPI_FLASH_CS_LOW()       GPIO_ResetBits(FLASH_SPI_CS_GPIO_PORT, FLASH_SPI_CS_PIN)
  54. /* Deselect SPI FLASH: Chip Select pin high */
  55. #define SPI_FLASH_CS_HIGH()      GPIO_SetBits(FLASH_SPI_CS_GPIO_PORT, FLASH_SPI_CS_PIN)

  56. #define SPI_FLASH_157_EN()    GPIO_ResetBits(FLASH_SPI_157_GPIO_PORT, FLASH_SPI_157_PIN) //高选通
  57. #define SPI_FLASH_157_DIS()     GPIO_SetBits(FLASH_SPI_157_GPIO_PORT, FLASH_SPI_157_PIN)

  58. /* Exported functions ------------------------------------------------------- */
  59. /*----- High layer function -----*/

  60. #define TIME_PAGE_PROGRAME                        3//5MS
  61. #define TIME_BULK_ERASE                                200//100000//160S
  62. extern uint64_t        flash_wait;

  63. void SPI_FLASH_Init(void);
  64. //void SPI_FLASH_SectorErase(u32 SectorAddr);
  65. //void SPI_FLASH_BulkErase(void);
  66. //void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
  67. //void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
  68. //void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);
  69. //u32 SPI_FLASH_ReadID(void);
  70. //void SPI_FLASH_StartReadSequence(u32 ReadAddr);

  71. /*----- Low layer function -----*/
  72. //u8 SPI_FLASH_ReadByte(void);
  73. u8 SPI_FLASH_SendByte(u8 byte);
  74. //u16 SPI_FLASH_SendHalfWord(u16 HalfWord);
  75. void SPI_FLASH_WriteEnable();
  76. void SPI_FLASH_WriteDisable();
  77. void SPI_FLASH_WaitForWriteEnd();
  78. void SPI_FLASH_Wait(uint64_t time);

  79. //void SPI_FLASH_ByteWrite(u32 WriteAddr);
  80. //u8 SPI_FLASH_ByteRead(u32 ReadAddr);
  81. u32 SPI_FLASH_Read_ID();
  82. u32 SPI_FLASH_ReadID();
  83. void SPI_FLASH_ChipErase();
  84. //void SPI_FLASH_4KByteErase(u32 SectorAddr);
  85. void SPI_FLASH_PageWrite(unsigned int Addr, unsigned char *buf, unsigned int num);


  86. void SPI_FLASH_PageWriteEnd(unsigned int Addr, unsigned char *buf, unsigned int num);
  87. void SPI_FLASH_PageRead(unsigned int Addr,unsigned char *buf);
  88. void SPI_FLASH_Writebyte(unsigned int Addr, unsigned char *buf);
  89. void SPI_FLASH_Readnbyte(unsigned int Addr,unsigned char *buf,u16 num);
  90. void SPI_FLASH_Writenbyte(unsigned int Addr, unsigned char *buf,u16 num);
  91. #endif /* __SPI_FLASH_H */
复制代码
  1. /************************************************************
  2.   Description:    FPGA和FLI32626的Flash操作      
  3.   Version:        Flash使用ssT25VF032B
  4.   Function List:  适用于stm32f4  自带硬件spi
  5.     1. -------
  6.   History:         
  7.       <author>          <time>     <version >   <desc>
  8.       lide           2012.5.22      1.0      创建模块  
  9. ***********************************************************/

  10. /* Includes ------------------------------------------------------------------*/
  11. #include "spi_flash.h"
  12. #include "stm32_delay.h"

  13. //#define SPI1DMA_RX 0
  14. uint64_t        flash_wait = 0;
  15. extern unsigned char time_is_up;
  16. /*******************************************************************************
  17. * Function Name  : SPI_FLASH_Init
  18. * Description    : Initializes the peripherals used by the SPI FLASH driver.
  19. * Input          : None
  20. * Output         : None
  21. * Return         : None
  22. *******************************************************************************/
  23. void delay_nus(unsigned short n)
  24. {
  25.     unsigned char i;
  26.    
  27.     while(n--)
  28.     {
  29.         for(i=0;i<26;i++);              //i=26 刚好1us
  30.     }
  31. }

  32. void SPI_FLASH_Init(void)
  33. {
  34.     GPIO_InitTypeDef GPIO_InitStructure;
  35.     SPI_InitTypeDef  SPI_InitStructure;
  36.     NVIC_InitTypeDef   NVIC_InitStructure;

  37.     /* Enable the SPI periph */
  38.     RCC_APB2PeriphClockCmd(FLASH_SPI_CLK, ENABLE);

  39.     /* Enable SCK, MOSI and MISO GPIO clocks */
  40.     RCC_AHB1PeriphClockCmd(FLASH_SPI_SCK_GPIO_CLK | FLASH_SPI_MOSI_GPIO_CLK | FLASH_SPI_MISO_GPIO_CLK, ENABLE);//FLASH_SPI_MISO_GPIO_CLK

  41.     /* Enable CS  GPIO clock */
  42.     RCC_AHB1PeriphClockCmd(FLASH_SPI_CS_GPIO_CLK, ENABLE);
  43.    
  44.     /* Enable 157  GPIO clock */
  45.     RCC_AHB1PeriphClockCmd(FLASH_SPI_157_GPIO_CLK, ENABLE);
  46.    

  47.     GPIO_PinAFConfig(FLASH_SPI_SCK_GPIO_PORT, FLASH_SPI_SCK_SOURCE, FLASH_SPI_SCK_AF);
  48.     GPIO_PinAFConfig(FLASH_SPI_MISO_GPIO_PORT, FLASH_SPI_MISO_SOURCE, FLASH_SPI_MISO_AF);
  49.     GPIO_PinAFConfig(FLASH_SPI_MOSI_GPIO_PORT, FLASH_SPI_MOSI_SOURCE, FLASH_SPI_MOSI_AF);

  50.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  51.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  52.     GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
  53.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  54.     /* SPI SCK pin configuration */
  55.     GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
  56.     GPIO_Init(FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);

  57.     /* SPI  MOSI pin configuration */
  58.     GPIO_InitStructure.GPIO_Pin =  FLASH_SPI_MOSI_PIN;
  59.     GPIO_Init(FLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);

  60.     /* SPI MISO pin configuration */
  61.     GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
  62.     GPIO_Init(FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);


  63.     /* Configure GPIO PIN for Lis Chip select */
  64.     GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
  65.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  66.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  67.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  68.     GPIO_Init(FLASH_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
  69.    
  70.     SPI_FLASH_CS_HIGH();

  71.   /* Configure GPIO PIN for 157 */
  72.     GPIO_InitStructure.GPIO_Pin = FLASH_SPI_157_PIN;
  73.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  74.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  75.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  76.     GPIO_Init(FLASH_SPI_157_GPIO_PORT, &GPIO_InitStructure);

  77.     /* SPI1 configuration */
  78.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  79.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  80.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  81.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  82.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  83.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  84.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  85.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  86.     SPI_InitStructure.SPI_CRCPolynomial = 7;
  87.     SPI_Init(FLASH_SPI, &SPI_InitStructure);

  88.     /* Enable SPI1  */
  89.     SPI_Cmd(FLASH_SPI, ENABLE);
  90.     SPI_FLASH_157_DIS();
  91.    
  92.     //SPI_DMAInit();
  93.     /* I2S DMA IRQ Channel configuration */
  94.    /* NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_RX_IRQn;
  95.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  96.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  97.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  98.     NVIC_Init(&NVIC_InitStructure);
  99.    
  100.     NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_TX_IRQn;
  101.     NVIC_Init(&NVIC_InitStructure);
  102.     Delay_Init(168);*/

  103. }

  104. /*******************************************************************************
  105. * Function Name  : SPI_FLASH_SectorErase
  106. * Description    : Erases the specified FLASH sector.
  107. * Input          : SectorAddr: address of the sector to erase.
  108. * Output         : None
  109. * Return         : None
  110. *******************************************************************************/
  111. //void SPI_FLASH_SectorErase(u32 SectorAddr)
  112. //{
  113. //  /* Send write enable instruction */
  114. //  SPI_FLASH_WriteEnable();
  115. //
  116. //  /* Sector Erase */
  117. //  /* Select the FLASH: Chip Select low */
  118. //  SPI_FLASH_CS_LOW();
  119. //  /* Send Sector Erase instruction */
  120. //  SPI_FLASH_SendByte(SE);
  121. //  /* Send SectorAddr high nibble address byte */
  122. //  SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
  123. //  /* Send SectorAddr medium nibble address byte */
  124. //  SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
  125. //  /* Send SectorAddr low nibble address byte */
  126. //  SPI_FLASH_SendByte(SectorAddr & 0xFF);
  127. //  /* Deselect the FLASH: Chip Select high */
  128. //  SPI_FLASH_CS_HIGH();
  129. //
  130. //  /* Wait the end of Flash writing */
  131. //  SPI_FLASH_WaitForWriteEnd();
  132. //}

  133. /*******************************************************************************
  134. * Function Name  : SPI_FLASH_BulkErase
  135. * Description    : Erases the entire FLASH.
  136. * Input          : None
  137. * Output         : None
  138. * Return         : None
  139. *******************************************************************************/
  140. //void SPI_FLASH_BulkErase(void)
  141. //{
  142. //  /* Send write enable instruction */
  143. //  SPI_FLASH_WriteEnable();
  144. //
  145. //  /* Bulk Erase */
  146. //  /* Select the FLASH: Chip Select low */
  147. //  SPI_FLASH_CS_LOW();
  148. //  /* Send Bulk Erase instruction  */
  149. //  SPI_FLASH_SendByte(0xc7);
  150. //  /* Deselect the FLASH: Chip Select high */
  151. //  SPI_FLASH_CS_HIGH();
  152. //
  153. //  /* Wait the end of Flash writing */
  154. //  //SPI_FLASH_WaitForWriteEnd();
  155. //  SPI_FLASH_Wait(TIME_BULK_ERASE);
  156. //}

  157. //void SPI_FLASH_4KByteErase(u32 SectorAddr)
  158. //{
  159. //  /* Send write enable instruction */
  160. //  SPI_FLASH_WriteEnable();
  161. //
  162. //  /* Bulk Erase */
  163. //  /* Select the FLASH: Chip Select low */
  164. //  SPI_FLASH_CS_LOW();
  165. //  /* Send Bulk Erase instruction  */
  166. //  SPI_FLASH_SendByte(0x20);
  167. //  /* Send SectorAddr high nibble address byte */
  168. //  SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
  169. //  /* Send SectorAddr medium nibble address byte */
  170. //  SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
  171. //  /* Send SectorAddr low nibble address byte */
  172. //  SPI_FLASH_SendByte(SectorAddr & 0xFF);
  173. //  /* Deselect the FLASH: Chip Select high */
  174. //
  175. //  SPI_FLASH_CS_HIGH();
  176. //
  177. //  /* Wait the end of Flash writing */
  178. //  //SPI_FLASH_WaitForWriteEnd();
  179. //  SPI_FLASH_Wait(30);
  180. //}

  181. void SPI_FLASH_WriteSR(unsigned char data)
  182. {
  183.     SPI_FLASH_CS_LOW();
  184.     SPI_FLASH_SendByte(EWSR);
  185.     SPI_FLASH_CS_HIGH();
  186.    
  187.     SPI_FLASH_CS_LOW();
  188.     SPI_FLASH_SendByte(WRSR);
  189.     SPI_FLASH_SendByte(data);
  190.     SPI_FLASH_CS_HIGH();
  191. }

  192. void SPI_FLASH_ChipErase()
  193. {
  194.     SPI_FLASH_WriteSR(0x00);  //写状态寄存器为整片都非保护模式
  195.         SPI_FLASH_WriteEnable();

  196.         SPI_FLASH_CS_LOW();
  197.     SPI_FLASH_SendByte(CE);
  198.     SPI_FLASH_CS_HIGH();

  199.         SPI_FLASH_WriteDisable();
  200.         //Delay_ms(40);   
  201.         TIM_Delay_10ms(40);
  202.         while(time_is_up == 0);
  203.         time_is_up = 0;

  204.         //SPI_FLASH_Wait(50);/* 延时至少35ms */
  205. }


  206. //void SPI_FLASH_ByteWrite(u32 WriteAddr)
  207. //{
  208. //  /* Enable the write access to the FLASH */
  209. //  SPI_FLASH_WriteEnable();
  210. //
  211. //  /* Select the FLASH: Chip Select low */
  212. //  SPI_FLASH_CS_LOW();
  213. //  /* Send "Write to Memory " instruction */
  214. //  SPI_FLASH_SendByte(WRITE);
  215. //  /* Send WriteAddr high nibble address byte to write to */
  216. //  SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
  217. //  /* Send WriteAddr medium nibble address byte to write to */
  218. //  SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
  219. //  /* Send WriteAddr low nibble address byte to write to */
  220. //  SPI_FLASH_SendByte(WriteAddr & 0xFF);
  221. //
  222. //    /* Send the current byte */
  223. //    SPI_FLASH_SendByte(0x22);
  224. //
  225. //  /* Deselect the FLASH: Chip Select high */
  226. //  SPI_FLASH_CS_HIGH();
  227. //
  228. //  /* Wait the end of Flash writing */
  229. ////  SPI_FLASH_WaitForWriteEnd();
  230. ////SPI_FLASH_Wait(TIME_PAGE_PROGRAME);
  231. //  SPI_FLASH_Wait(100);
  232. //}
  233. extern DMA_InitTypeDef    SPIDMA_InitStructure;
  234. void SPI_DMAInit1(unsigned char *buf, unsigned short size, unsigned char type)
  235. {
  236.   unsigned char temp[256]={2,2,2,2,2,2,2,2,2,2,2,2};
  237.       /* Enable the DMA clock */
  238.   RCC_AHB1PeriphClockCmd(SPI1_DMA_CLK, ENABLE);
  239.   
  240.   /* Clear any pending flag on Rx Stream  */
  241.   DMA_ClearFlag(SPI1_DMA_STREAM_TX, SPI1_TX_DMA_FLAG_FEIF | SPI1_TX_DMA_FLAG_DMEIF | SPI1_TX_DMA_FLAG_TEIF | \
  242.                                        SPI1_TX_DMA_FLAG_HTIF | SPI1_TX_DMA_FLAG_TCIF);
  243.   /* Disable the EE I2C Tx DMA stream */
  244.   DMA_Cmd(SPI1_DMA_STREAM_TX, DISABLE);
  245.   /* Configure the DMA stream for the EE I2C peripheral TX direction */
  246.   DMA_DeInit(SPI1_DMA_STREAM_TX);
  247.   SPIDMA_InitStructure.DMA_Channel = SPI1_DMA_CHANNEL;
  248.   SPIDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI1_DR_Address;
  249.   if(type == 1) //RX
  250.       SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)temp;    /* This parameter will be configured durig communication */
  251.   else if(type == 2) //TX
  252.       SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buf;    /* This parameter will be configured durig communication */
  253.   SPIDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */
  254.   SPIDMA_InitStructure.DMA_BufferSize = size;              /* This parameter will be configured durig communication */
  255.   SPIDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  256.   SPIDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  257.   SPIDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  258.   SPIDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  259.   SPIDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  260.   SPIDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  261.   SPIDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  262.   SPIDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
  263.   SPIDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  264.   SPIDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  265.   DMA_Init(SPI1_DMA_STREAM_TX, &SPIDMA_InitStructure);
  266.   
  267.   /* Clear any pending flag on Rx Stream */
  268.   DMA_ClearFlag(SPI1_DMA_STREAM_RX, SPI1_RX_DMA_FLAG_FEIF | SPI1_RX_DMA_FLAG_DMEIF | SPI1_RX_DMA_FLAG_TEIF | \
  269.                                        SPI1_RX_DMA_FLAG_HTIF | SPI1_RX_DMA_FLAG_TCIF);
  270.   /* Disable the EE I2C DMA Rx stream */
  271.   DMA_Cmd(SPI1_DMA_STREAM_RX, DISABLE);
  272.   /* Configure the DMA stream for the EE I2C peripheral RX direction */
  273.   DMA_DeInit(SPI1_DMA_STREAM_RX);
  274.   if(type == 1) //RX
  275.       SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buf;    /* This parameter will be configured durig communication */
  276.   else if(type == 2) //TX
  277.       SPIDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)temp;    /* This parameter will be configured durig communication */
  278.   SPIDMA_InitStructure.DMA_BufferSize = size;
  279.   SPIDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  280.   DMA_Init(SPI1_DMA_STREAM_RX, &SPIDMA_InitStructure);

  281.   /* Enable the DMA Channels Interrupts */
  282.   DMA_ITConfig(SPI1_DMA_STREAM_RX, DMA_IT_TC, ENABLE);  
  283.   DMA_ITConfig(SPI1_DMA_STREAM_TX, DMA_IT_TC, ENABLE);  
  284. }
  285. /*------------------------------------------------------------------------------
  286. 函数名称 : SPI_FLASH_WaitForWriteEnd
  287. 函数功能 : 等待FLASH操作完成
  288. 入口参数 :  无
  289. 出口参数 :  无
  290. ------------------------------------------------------------------------------*/

  291. void SPI_FLASH_PageWrite(unsigned int Addr, unsigned char *buf, unsigned int num)
  292. {
  293.     unsigned short i = 0, j = 0;
  294.     SPI_FLASH_WriteEnable();

  295.         /* 传输前两个字节数据 */
  296.     SPI_FLASH_CS_LOW();

  297.     SPI_FLASH_SendByte(0xAD);
  298.     SPI_FLASH_SendByte(Addr>>16);
  299.     SPI_FLASH_SendByte(Addr>>8);
  300.     SPI_FLASH_SendByte(Addr);
  301.     SPI_FLASH_SendByte(*buf++);
  302.     SPI_FLASH_SendByte(*buf++);

  303.     SPI_FLASH_CS_HIGH();
  304.    
  305.     delay_nus(20);                          /* 延时 */
  306.         
  307.    for(i=0; i<((num-2)/2); i++) //总共大小为256个字
  308.     {
  309.         SPI_FLASH_CS_LOW();

  310.         SPI_FLASH_SendByte(0xAD);
  311.         
  312. #ifdef SPI1DMA_TX         
  313.         SPI_DMAInit1(buf, 2, 2);
  314.         
  315.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
  316.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
  317.         DMA_Cmd(SPI1_DMA_STREAM_TX, ENABLE);
  318.         DMA_Cmd(SPI1_DMA_STREAM_RX, ENABLE);
  319.        // for(j=0; j<0xfff8; j++);

  320.         while (!DMA_GetFlagStatus(SPI1_DMA_STREAM_TX, SPI1_RX_DMA_FLAG_TCIF))
  321.         {
  322.           if((j++) > 0xfff0)    break;
  323.         }

  324.         DMA_Cmd(SPI1_DMA_STREAM_TX, DISABLE);
  325.         DMA_Cmd(SPI1_DMA_STREAM_RX, DISABLE);
  326.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, DISABLE);
  327.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, DISABLE);

  328.         
  329.         buf+=2;
  330. #else
  331.         SPI_FLASH_SendByte(*buf++);
  332.    SPI_FLASH_SendByte(*buf++);
  333. #endif
  334.         SPI_FLASH_CS_HIGH();

  335.         delay_nus(15);/* 延时 */
  336.     }
  337.     SPI_FLASH_WriteDisable();
  338.     delay_nus(20);
  339. }

  340. /*------------------------------------------------------------------------------
  341. 函数名称 : SPI_FLASH_WaitForWriteEnd
  342. 函数功能 : 等待FLASH操作完成
  343. 入口参数 :  无
  344. 出口参数 :  无
  345. ------------------------------------------------------------------------------*/
  346. void SPI_FLASH_WaitForWriteEnd2(void)
  347. {
  348.   u8 FLASH_Status = 0;

  349.   /* Select the FLASH: Chip Select low */

  350.    SPI_FLASH_CS_LOW();

  351.   /* Send "Read Status Register" instruction */
  352.   SPI_FLASH_SendByte(RDSR);

  353.   /* Loop as long as the memory is busy with a write cycle */
  354.   do
  355.   {
  356.     /* Send a dummy byte to generate the clock needed by the FLASH
  357.     and put the value of the status register in FLASH_Status variable */
  358.     FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);

  359.   }
  360.   while ((FLASH_Status & WIP_Flag) == RESET); /* Write in progress */

  361.   /* Deselect the FLASH: Chip Select high */
  362.   SPI_FLASH_CS_HIGH();
  363. }

  364. /*------------------------------------------------------------------------------
  365. 函数名称 : SPI_FLASH_Writebyte
  366. 函数功能 : 写一个字节数据
  367. 入口参数 :  address:地址  buffer:数据
  368. 出口参数 :  无
  369. ------------------------------------------------------------------------------*/

  370. void SPI_FLASH_Writebyte(unsigned int Addr, unsigned char *buf)
  371. {
  372.     unsigned short i = 0, j = 0;

  373.     SPI_FLASH_WriteEnable();

  374.     SPI_FLASH_CS_LOW();
  375.         SPI_FLASH_SendByte(0x02);
  376.         SPI_FLASH_SendByte((Addr)>>16);
  377.     SPI_FLASH_SendByte((Addr)>>8);
  378.         SPI_FLASH_SendByte((Addr));
  379.         SPI_FLASH_SendByte(*buf);
  380.     SPI_FLASH_CS_HIGH();
  381.            SPI_FLASH_WaitForWriteEnd();
  382.     SPI_FLASH_WriteDisable();
  383.     delay_nus(20);
  384. }
  385. /*------------------------------------------------------------------------------
  386. 函数名称 : SPI_FLASH_Writenbyte
  387. 函数功能 : 写一个字节数据
  388. 入口参数 :  address:地址  buffer:数据 num :数量
  389. 出口参数 :  无

  390. ------------------------------------------------------------------------------*/


  391. void SPI_FLASH_Writenbyte(unsigned int Addr, unsigned char *buf,u16 num)
  392. {
  393. u8 i;
  394. for(i=0;i<num;i++)
  395. {
  396.    SPI_FLASH_Writebyte(Addr++,&buf[i]);
  397. }

  398. }
  399. /*------------------------------------------------------------------------------
  400. 函数名称 : SPI_FLASH_Readnbyte
  401. 函数功能 : 写一个字节数据
  402. 入口参数 :  address:地址  buffer:数据 num :数量
  403. 出口参数 :  无

  404. ------------------------------------------------------------------------------*/

  405. void SPI_FLASH_Readnbyte(unsigned int Addr,unsigned char *buf,u16 num)
  406. {
  407.     unsigned short i = 0;

  408.     SPI_FLASH_CS_LOW();

  409.     SPI_FLASH_SendByte(0x0b);
  410.         SPI_FLASH_SendByte(Addr>>16);
  411.         SPI_FLASH_SendByte(Addr>>8);
  412.         SPI_FLASH_SendByte(Addr);
  413.         //dummy
  414.         SPI_FLASH_SendByte(Dummy_Byte);
  415.     for(i=0;i<num;i++)
  416.     {
  417.                 buf[i]= SPI_FLASH_SendByte(Dummy_Byte);

  418.         }

  419.        
  420.     SPI_FLASH_CS_HIGH();
  421. }

  422. void SPI_FLASH_PageWriteEnd(unsigned int Addr, unsigned char *buf, unsigned int num)
  423. {
  424.     unsigned short i = 0;
  425.     SPI_FLASH_WriteEnable();

  426.         /* 传输前两个字节数据 */
  427.     SPI_FLASH_CS_LOW();

  428.     SPI_FLASH_SendByte(0xAD);
  429.         SPI_FLASH_SendByte(Addr>>16);
  430.         SPI_FLASH_SendByte(Addr>>8);
  431.         SPI_FLASH_SendByte(Addr);
  432.         SPI_FLASH_SendByte(*buf++);
  433.         SPI_FLASH_SendByte(*buf++);

  434.     SPI_FLASH_CS_HIGH();

  435.     delay_nus(20);                          /* 延时 */

  436.         for(i=0; i<((num-2)/2); i++) //总共大小为256个字节
  437.     {
  438.         SPI_FLASH_CS_LOW();

  439.                 SPI_FLASH_SendByte(0xAD);
  440.                 SPI_FLASH_SendByte(*buf++);
  441.             SPI_FLASH_SendByte(*buf++);

  442.         SPI_FLASH_CS_HIGH();

  443.                 delay_nus(15);/* 延时 */
  444.         }

  445.     if(num%2)
  446.     {
  447.         SPI_FLASH_CS_LOW();

  448.                 SPI_FLASH_SendByte(0xAD);
  449.                 SPI_FLASH_SendByte(*buf++);
  450.             SPI_FLASH_SendByte(0xFF);

  451.         SPI_FLASH_CS_HIGH();

  452.                 delay_nus(15);
  453.     }

  454.         SPI_FLASH_WriteDisable();
  455.         delay_nus(20);
  456. }


  457. void SPI_FLASH_PageRead(unsigned int Addr,unsigned char *buf)
  458. {
  459.     unsigned short i = 0;

  460.     SPI_FLASH_CS_LOW();

  461.     SPI_FLASH_SendByte(0x0B);
  462.         SPI_FLASH_SendByte(Addr>>16);
  463.         SPI_FLASH_SendByte(Addr>>8);
  464.         SPI_FLASH_SendByte(Addr);
  465.         SPI_FLASH_SendByte(0xFF);  //dummy
  466. #ifdef SPI1DMA_RX     
  467.         SPI_DMAInit1(buf, 256, 1);
  468.         
  469.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
  470.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
  471.         DMA_Cmd(SPI1_DMA_STREAM_RX, ENABLE);
  472.         DMA_Cmd(SPI1_DMA_STREAM_TX, ENABLE);
  473.         //for(i=0; i<0xfff8; i++);
  474.         while (!DMA_GetFlagStatus(SPI1_DMA_STREAM_RX, SPI1_RX_DMA_FLAG_TCIF))
  475.         {
  476.           if((i++) > 0xfff0)    break;
  477.         }  
  478.         

  479.         DMA_Cmd(SPI1_DMA_STREAM_RX, DISABLE);
  480.         DMA_Cmd(SPI1_DMA_STREAM_TX, DISABLE);
  481.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, DISABLE);
  482.         SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, DISABLE);
  483.         
  484. #else
  485.         for(i=0; i<256; i++) //总共大小为256个字节
  486.         {
  487.                 *buf++ = SPI_FLASH_SendByte(Dummy_Byte);
  488.         }
  489. #endif

  490.     SPI_FLASH_CS_HIGH();
  491. }

  492. /*******************************************************************************
  493. * Function Name  : SPI_FLASH_BufferWrite
  494. * Description    : Writes block of data to the FLASH. In this function, the
  495. *                  number of WRITE cycles are reduced, using Page WRITE sequence.
  496. * Input          : - pBuffer : pointer to the buffer  containing the data to be
  497. *                    written to the FLASH.
  498. *                  - WriteAddr : FLASH's internal address to write to.
  499. *                  - NumByteToWrite : number of bytes to write to the FLASH.
  500. * Output         : None
  501. * Return         : None
  502. *******************************************************************************/
  503. //void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
  504. //{
  505. //  u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
  506. //
  507. //  Addr = WriteAddr % SPI_FLASH_PageSize;
  508. //  count = SPI_FLASH_PageSize - Addr;
  509. //  NumOfPage =  NumByteToWrite / SPI_FLASH_PageSize;
  510. //  NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
  511. //
  512. //  if (Addr == 0) /* WriteAddr is SPI_FLASH_PageSize aligned  */
  513. //  {
  514. //    if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
  515. //    {
  516. //      SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
  517. //    }
  518. //    else /* NumByteToWrite > SPI_FLASH_PageSize */
  519. //    {
  520. //      while (NumOfPage--)
  521. //      {
  522. //        SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
  523. //        WriteAddr +=  SPI_FLASH_PageSize;
  524. //        pBuffer += SPI_FLASH_PageSize;
  525. //      }
  526. //
  527. //      SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  528. //    }
  529. //  }
  530. //  else /* WriteAddr is not SPI_FLASH_PageSize aligned  */
  531. //  {
  532. //    if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
  533. //    {
  534. //      if (NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */
  535. //      {
  536. //        temp = NumOfSingle - count;
  537. //
  538. //        SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
  539. //        WriteAddr +=  count;
  540. //        pBuffer += count;
  541. //
  542. //        SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
  543. //      }
  544. //      else
  545. //      {
  546. //        SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
  547. //      }
  548. //    }
  549. //    else /* NumByteToWrite > SPI_FLASH_PageSize */
  550. //    {
  551. //      NumByteToWrite -= count;
  552. //      NumOfPage =  NumByteToWrite / SPI_FLASH_PageSize;
  553. //      NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
  554. //
  555. //      SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
  556. //      WriteAddr +=  count;
  557. //      pBuffer += count;
  558. //
  559. //      while (NumOfPage--)
  560. //      {
  561. //        SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
  562. //        WriteAddr +=  SPI_FLASH_PageSize;
  563. //        pBuffer += SPI_FLASH_PageSize;
  564. //      }
  565. //
  566. //      if (NumOfSingle != 0)
  567. //      {
  568. //        SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  569. //      }
  570. //    }
  571. //  }
  572. //}
  573. //
  574. /*******************************************************************************
  575. * Function Name  : SPI_FLASH_BufferRead
  576. * Description    : Reads a block of data from the FLASH.
  577. * Input          : - pBuffer : pointer to the buffer that receives the data read
  578. *                    from the FLASH.
  579. *                  - ReadAddr : FLASH's internal address to read from.
  580. *                  - NumByteToRead : number of bytes to read from the FLASH.
  581. * Output         : None
  582. * Return         : None
  583. *******************************************************************************/
  584. //void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
  585. //{
  586. //  /* Select the FLASH: Chip Select low */
  587. //  SPI_FLASH_CS_LOW();
  588. //
  589. //  /* Send "Read from Memory " instruction */
  590. //  SPI_FLASH_SendByte(READ);
  591. //
  592. //  /* Send ReadAddr high nibble address byte to read from */
  593. //  SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  594. //  /* Send ReadAddr medium nibble address byte to read from */
  595. //  SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  596. //  /* Send ReadAddr low nibble address byte to read from */
  597. //  SPI_FLASH_SendByte(ReadAddr & 0xFF);
  598. //
  599. //  while (NumByteToRead--) /* while there is data to be read */
  600. //  {
  601. //    /* Read a byte from the FLASH */
  602. //    *pBuffer = SPI_FLASH_SendByte(Dummy_Byte);
  603. //    /* Point to the next location where the byte read will be saved */
  604. //    pBuffer++;
  605. //  }
  606. //
  607. //  /* Deselect the FLASH: Chip Select high */
  608. //  SPI_FLASH_CS_HIGH();
  609. //}

  610. //u8 SPI_FLASH_ByteRead(u32 ReadAddr)
  611. //{
  612. //    u8 temp = 0;
  613. //
  614. //  /* Select the FLASH: Chip Select low */
  615. //  SPI_FLASH_CS_LOW();
  616. //
  617. //  /* Send "Read from Memory " instruction */
  618. //  SPI_FLASH_SendByte(0x03);
  619. //
  620. //  /* Send ReadAddr high nibble address byte to read from */
  621. //  SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  622. //  /* Send ReadAddr medium nibble address byte to read from */
  623. //  SPI_FLASH_SendByte((ReadAddr & 0xFF00) >> 8);
  624. //  /* Send ReadAddr low nibble address byte to read from */
  625. //  SPI_FLASH_SendByte(ReadAddr & 0xFF);
  626. //
  627. //    /* Read a byte from the FLASH */
  628. //    temp = SPI_FLASH_SendByte(Dummy_Byte);
  629. //
  630. //  /* Deselect the FLASH: Chip Select high */
  631. //  SPI_FLASH_CS_HIGH();
  632. //
  633. //    return temp;
  634. //}



  635. /*******************************************************************************
  636. * Function Name  : SPI_FLASH_ReadID
  637. * Description    : Reads FLASH identification.
  638. * Input          : None
  639. * Output         : None
  640. * Return         : FLASH identification
  641. *******************************************************************************/
  642. u32 SPI_FLASH_ReadID()
  643. {
  644.   u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;

  645.   /* Select the FLASH: Chip Select low */
  646.   SPI_FLASH_CS_LOW();

  647.   /* Send "RDID " instruction */
  648.   SPI_FLASH_SendByte(0x9F);

  649.   /* Read a byte from the FLASH */
  650.   Temp0 = SPI_FLASH_SendByte(Dummy_Byte);

  651.   /* Read a byte from the FLASH */
  652.   Temp1 = SPI_FLASH_SendByte(Dummy_Byte);

  653.   /* Read a byte from the FLASH */
  654.   Temp2 = SPI_FLASH_SendByte(Dummy_Byte);

  655.   /* Deselect the FLASH: Chip Select high */
  656.   SPI_FLASH_CS_HIGH();

  657.   Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;

  658.   return Temp;
  659. }


  660. /*******************************************************************************
  661. * Function Name  : SPI_FLASH_StartReadSequence
  662. * Description    : Initiates a read data byte (READ) sequence from the Flash.
  663. *                  This is done by driving the /CS line low to select the device,
  664. *                  then the READ instruction is transmitted followed by 3 bytes
  665. *                  address. This function exit and keep the /CS line low, so the
  666. *                  Flash still being selected. With this technique the whole
  667. *                  content of the Flash is read with a single READ instruction.
  668. * Input          : - ReadAddr : FLASH's internal address to read from.
  669. * Output         : None
  670. * Return         : None
  671. *******************************************************************************/
  672. //void SPI_FLASH_StartReadSequence(u32 ReadAddr)
  673. //{
  674. //  /* Select the FLASH: Chip Select low */
  675. //  SPI_FLASH_CS_LOW();
  676. //
  677. //  /* Send "Read from Memory " instruction */
  678. //  SPI_FLASH_SendByte(READ);
  679. //
  680. //  /* Send the 24-bit address of the address to read from -----------------------*/
  681. //  /* Send ReadAddr high nibble address byte */
  682. //  SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  683. //  /* Send ReadAddr medium nibble address byte */
  684. //  SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  685. //  /* Send ReadAddr low nibble address byte */
  686. //  SPI_FLASH_SendByte(ReadAddr & 0xFF);
  687. //}

  688. /*******************************************************************************
  689. * Function Name  : SPI_FLASH_ReadByte
  690. * Description    : Reads a byte from the SPI Flash.
  691. *                  This function must be used only if the Start_Read_Sequence
  692. *                  function has been previously called.
  693. * Input          : None
  694. * Output         : None
  695. * Return         : Byte Read from the SPI Flash.
  696. *******************************************************************************/
  697. u8 SPI_FLASH_ReadByte(void)
  698. {
  699.   return (SPI_FLASH_SendByte(Dummy_Byte));
  700. }

  701. /*******************************************************************************
  702. * Function Name  : SPI_FLASH_SendByte
  703. * Description    : Sends a byte through the SPI interface and return the byte
  704. *                  received from the SPI bus.
  705. * Input          : byte : byte to send.
  706. * Output         : None
  707. * Return         : The value of the received byte.
  708. *******************************************************************************/
  709. u8 SPI_FLASH_SendByte(u8 byte)
  710. {
  711.   /* Loop while DR register in not emplty */
  712. //          while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

  713.   /* Send byte through the SPI1 peripheral */
  714.         SPI_I2S_SendData(SPI1, byte);
  715.         //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  716.   /* Wait to receive a byte */
  717.         while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

  718.   /* Return the byte read from the SPI bus */
  719.           return SPI_I2S_ReceiveData(SPI1);
  720. //          return 0;
  721. }

  722. /*******************************************************************************
  723. * Function Name  : SPI_FLASH_SendHalfWord
  724. * Description    : Sends a Half Word through the SPI interface and return the
  725. *                  Half Word received from the SPI bus.
  726. * Input          : Half Word : Half Word to send.
  727. * Output         : None
  728. * Return         : The value of the received Half Word.
  729. *******************************************************************************/
  730. u16 SPI_FLASH_SendHalfWord(u16 HalfWord)
  731. {
  732.   /* Loop while DR register in not emplty */
  733.   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

  734.   /* Send Half Word through the SPI1 peripheral */
  735.   SPI_I2S_SendData(SPI1, HalfWord);

  736.   /* Wait to receive a Half Word */
  737.   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

  738.   /* Return the Half Word read from the SPI bus */
  739.   return SPI_I2S_ReceiveData(SPI1);
  740. }

  741. /*******************************************************************************
  742. * Function Name  : SPI_FLASH_WriteEnable
  743. * Description    : Enables the write access to the FLASH.
  744. * Input          : None
  745. * Output         : None
  746. * Return         : None
  747. *******************************************************************************/
  748. void SPI_FLASH_WriteEnable()
  749. {
  750.   /* Select the FLASH: Chip Select low */
  751.     SPI_FLASH_CS_LOW();

  752.   /* Send "Write Enable" instruction */
  753.   SPI_FLASH_SendByte(WREN);

  754.   /* Deselect the FLASH: Chip Select high */
  755.     SPI_FLASH_CS_HIGH();
  756. }

  757. void SPI_FLASH_WriteDisable()
  758. {
  759.   /* Select the FLASH: Chip Select low */
  760.     SPI_FLASH_CS_LOW();

  761.   /* Send "Write Enable" instruction */
  762.   SPI_FLASH_SendByte(WRDI);

  763.   /* Deselect the FLASH: Chip Select high */
  764.     SPI_FLASH_CS_HIGH();
  765. }

  766. /*******************************************************************************
  767. * Function Name  : SPI_FLASH_WaitForWriteEnd
  768. * Description    : Polls the status of the Write In Progress (WIP) flag in the
  769. *                  FLASH's status  register  and  loop  until write  opertaion
  770. *                  has completed.
  771. * Input          : None
  772. * Output         : None
  773. * Return         : None
  774. *******************************************************************************/
  775. void SPI_FLASH_WaitForWriteEnd()
  776. {
  777.   u8 FLASH_Status = 0;

  778.   /* Select the FLASH: Chip Select low */
  779.     SPI_FLASH_CS_LOW();

  780.   /* Send "Read Status Register" instruction */
  781.   SPI_FLASH_SendByte(RDSR);

  782.   /* Loop as long as the memory is busy with a write cycle */
  783.   do
  784.   {
  785.     /* Send a dummy byte to generate the clock needed by the FLASH
  786.     and put the value of the status register in FLASH_Status variable */
  787.     FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);

  788.   }
  789.   while ((FLASH_Status & WIP_Flag) == SET); /* Write in progress */

  790.   /* Deselect the FLASH: Chip Select high */
  791.     SPI_FLASH_CS_HIGH();
  792. }

  793. void SPI_FLASH_Wait(uint64_t time)
  794. {
  795.         flash_wait = time;
  796.         while(flash_wait);
  797. }


  798. //void FlashRWTest(void)
  799. //{
  800. //                /* Get SPI Flash ID */
  801. //        FLASH_ID = SPI_FLASH_ReadID();
  802. //        /* Check the SPI Flash ID */
  803. //        if (FLASH_ID == M25P64_FLASH_ID)
  804. //        {
  805. //                /* OK: Set GPIO_LED */
  806. //                GPIOB->BSRR = 0x1000E000;
  807. //        }
  808. //        else
  809. //        {
  810. //                /* Error: Set GPIO_LED*/
  811. //                GPIOB->BSRR = 0x2000D000;
  812. //        }
  813. //
  814. //        SPI_FLASH_SectorErase(FLASH_SectorToErase);
  815. //
  816. //        /* Write Tx_Buffer data to SPI FLASH memory */
  817. //        //SPI_FLASH_BufferWrite(TxBuffer, FLASH_WriteAddress, BufferSize);
  818. //
  819. //        /* Read data from SPI FLASH memory */
  820. //        //SPI_FLASH_BufferRead(RxBuffer, FLASH_ReadAddress, BufferSize);
  821. //}

  822. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-12-22 10:24

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表