找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4580|回复: 2
打印 上一主题 下一主题
收起左侧

STM32驱动 M25P64 FLASH读写程序

[复制链接]
跳转到指定楼层
楼主
STM32单片机驱动M25P64 SPI_FLASH参考例程.

全部资料下载地址:
STM32 M25P64 FLASH读写程序.rar (14.41 KB, 下载次数: 57)

  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name          : main.c
  3. * Author             : MCD Application Team
  4. * Version            : V2.0.1
  5. * Date               : 06/13/2008
  6. * Description        : Main program body
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/

  15. /* Includes ------------------------------------------------------------------*/
  16. #include "platform_config.h"
  17. #include "spi_flash.h"

  18. /* Local includes ------------------------------------------------------------*/
  19. /* Private typedef -----------------------------------------------------------*/
  20. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

  21. /* Private define ------------------------------------------------------------*/
  22. #define  FLASH_WriteAddress     0x700000
  23. #define  FLASH_ReadAddress      FLASH_WriteAddress
  24. #define  FLASH_SectorToErase    FLASH_WriteAddress
  25. #define  M25P64_FLASH_ID        0x202017
  26. #define  BufferSize (countof(Tx_Buffer)-1)

  27. /* Private macro -------------------------------------------------------------*/
  28. #define countof(a) (sizeof(a) / sizeof(*(a)))

  29. /* Private variables ---------------------------------------------------------*/
  30. u8 Tx_Buffer[] = "STM32F10x SPI Firmware Library Example: communication with an M25P64 SPI FLASH";
  31. u8 Index, Rx_Buffer[BufferSize];
  32. volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = PASSED;
  33. vu32 FLASH_ID = 0;
  34. ErrorStatus HSEStartUpStatus;

  35. /* Private functions ---------------------------------------------------------*/
  36. TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);
  37. void RCC_Configuration(void);
  38. void GPIO_Configuration(void);
  39. void NVIC_Configuration(void);


  40. /*******************************************************************************
  41. * Function Name  : main
  42. * Description    : Main program
  43. * Input          : None
  44. * Output         : None
  45. * Return         : None
  46. *******************************************************************************/
  47. int main(void)
  48. {
  49. #ifdef DEBUG
  50.   debug();
  51. #endif

  52.   /* System clocks configuration */
  53.   RCC_Configuration();

  54.   /* NVIC configuration */
  55.   NVIC_Configuration();

  56.   /* GPIO configuration */
  57.   GPIO_Configuration();

  58.   /* Initialize the SPI FLASH driver */
  59.   SPI_FLASH_Init();

  60.   /* Get SPI Flash ID */
  61.   FLASH_ID = SPI_FLASH_ReadID();
  62.   /* Check the SPI Flash ID */
  63.   if (FLASH_ID == M25P64_FLASH_ID)
  64.   {
  65.     /* OK: Set GPIO_LED pin 6 */
  66.     GPIO_WriteBit(GPIO_LED, GPIO_Pin_6, Bit_SET);
  67.   }
  68.   else
  69.   {
  70.     /* Error: Set GPIO_LED pin 7 */
  71.     GPIO_WriteBit(GPIO_LED, GPIO_Pin_7, Bit_SET);
  72.   }

  73.   /* Perform a write in the Flash followed by a read of the written data */
  74.   /* Erase SPI FLASH Sector to write on */
  75.   SPI_FLASH_SectorErase(FLASH_SectorToErase);

  76.   /* Write Tx_Buffer data to SPI FLASH memory */
  77.   SPI_FLASH_BufferWrite(Tx_Buffer, FLASH_WriteAddress, BufferSize);

  78.   /* Read data from SPI FLASH memory */
  79.   SPI_FLASH_BufferRead(Rx_Buffer, FLASH_ReadAddress, BufferSize);

  80.   /* Check the corectness of written dada */
  81.   TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize);
  82.   /* TransferStatus1 = PASSED, if the transmitted and received data by SPI1
  83.      are the same */
  84.   /* TransferStatus1 = FAILED, if the transmitted and received data by SPI1
  85.      are different */

  86.   /* Perform an erase in the Flash followed by a read of the written data */
  87.   /* Erase SPI FLASH Sector to write on */
  88.   SPI_FLASH_SectorErase(FLASH_SectorToErase);

  89.   /* Read data from SPI FLASH memory */
  90.   SPI_FLASH_BufferRead(Rx_Buffer, FLASH_ReadAddress, BufferSize);

  91.   /* Check the corectness of erasing operation dada */
  92.   for (Index = 0; Index < BufferSize; Index++)
  93.   {
  94.     if (Rx_Buffer[Index] != 0xFF)
  95.     {
  96.       TransferStatus2 = FAILED;
  97.     }
  98.   }
  99.   /* TransferStatus2 = PASSED, if the specified sector part is erased */
  100.   /* TransferStatus2 = FAILED, if the specified sector part is not well erased  */

  101.   while (1)
  102.   {}
  103. }

  104. /*******************************************************************************
  105. * Function Name  : RCC_Configuration
  106. * Description    : Configures the different system clocks.
  107. * Input          : None
  108. * Output         : None
  109. * Return         : None
  110. *******************************************************************************/
  111. void RCC_Configuration(void)
  112. {
  113.   /* RCC system reset(for debug purpose) */
  114.   RCC_DeInit();

  115.   /* Enable HSE */
  116.   RCC_HSEConfig(RCC_HSE_ON);

  117.   /* Wait till HSE is ready */
  118.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  119.   if (HSEStartUpStatus == SUCCESS)
  120.   {
  121.     /* Enable Prefetch Buffer */
  122.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  123.     /* Flash 2 wait state */
  124.     FLASH_SetLatency(FLASH_Latency_2);

  125.     /* HCLK = SYSCLK */
  126.     RCC_HCLKConfig(RCC_SYSCLK_Div1);

  127.     /* PCLK2 = HCLK */
  128.     RCC_PCLK2Config(RCC_HCLK_Div1);

  129.     /* PCLK1 = HCLK/2 */
  130.     RCC_PCLK1Config(RCC_HCLK_Div2);

  131.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  132.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  133.     /* Enable PLL */
  134.     RCC_PLLCmd(ENABLE);

  135.     /* Wait till PLL is ready */
  136.     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  137.     {}

  138.     /* Select PLL as system clock source */
  139.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  140.     /* Wait till PLL is used as system clock source */
  141.     while (RCC_GetSYSCLKSource() != 0x08)
  142.     {}
  143.   }

  144.   /* Enable GPIO_LED clock */
  145.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);
  146. }

  147. /*******************************************************************************
  148. * Function Name  : GPIO_Configuration
  149. * Description    : Configures the different GPIO ports.
  150. * Input          : None
  151. * Output         : None
  152. * Return         : None
  153. *******************************************************************************/
  154. void GPIO_Configuration(void)
  155. {
  156.   GPIO_InitTypeDef GPIO_InitStructure;

  157.   /* Configure GPIO LED pin6 and pin7 as Output push-pull --------------------*/
  158.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  159.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  160.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  161.   GPIO_Init(GPIO_LED, &GPIO_InitStructure);
  162. }

  163. /*******************************************************************************
  164. * Function Name  : NVIC_Configuration
  165. * Description    : Configures Vector Table base location.
  166. * Input          : None
  167. * Output         : None
  168. * Return         : None
  169. *******************************************************************************/
  170. void NVIC_Configuration(void)
  171. {
  172. #ifdef  VECT_TAB_RAM
  173.   /* Set the Vector Table base location at 0x20000000 */
  174.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  175. #else  /* VECT_TAB_FLASH  */
  176.   /* Set the Vector Table base location at 0x08000000 */
  177.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  178. #endif
  179. }

  180. /*******************************************************************************
  181. * Function Name  : Buffercmp
  182. * Description    : Compares two buffers.
  183. * Input          : - pBuffer1, pBuffer2: buffers to be compared.
  184. *                : - BufferLength: buffer's length
  185. * Output         : None
  186. * Return         : PASSED: pBuffer1 identical to pBuffer2
  187. *                  FAILED: pBuffer1 differs from pBuffer2
  188. *******************************************************************************/
  189. TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength)
  190. {
  191.   while (BufferLength--)
  192.   {
  193.     if (*pBuffer1 != *pBuffer2)
  194.     {
  195.       return FAILED;
  196.     }

  197.     pBuffer1++;
  198.     pBuffer2++;
  199.   }

  200.   return PASSED;
  201. }

  202. #ifdef  DEBUG
  203. /*******************************************************************************
  204. * Function Name  : assert_failed
  205. * Description    : Reports the name of the source file and the source line number
  206. *                  where the assert_param error has occurred.
  207. * Input          : - file: pointer to the source file name
  208. *                  - line: assert_param error line source number
  209. * Output         : None
  210. * Return         : None
  211. *******************************************************************************/
  212. void assert_failed(u8* file, u32 line)
  213. {
  214.   /* User can add his own implementation to report the file name and line number,
  215.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  216.   /* Infinite loop */
  217.   while (1)
  218.   {}
  219. }
  220. #endif
  221. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:420863 发表于 2018-11-5 10:41 | 只看该作者
下载下来分析下
回复

使用道具 举报

板凳
ID:463940 发表于 2019-3-7 22:05 | 只看该作者
试试。。。。
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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