[DemoCode下载] M451的SPI Flash操作

[复制链接]
488|1
 楼主| xuanhuanzi 发表于 2019-4-30 21:22 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 4 $
  5. * $Date: 15/09/02 10:03a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate how to set GPIO pin mode and use pin data input/output control.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include "stdio.h"
  12. #include "M451Series.h"
  13. #include "NuEdu-Basic01.h"

  14. #define PLL_CLOCK       72000000
  15. #define TEST_NUMBER                 10 /* page numbers */
  16. unsigned char   SrcArray[256];
  17. unsigned char DestArray[256];

  18. void SYS_Init(void)
  19. {
  20.     /*---------------------------------------------------------------------------------------------------------*/
  21.     /* Init System Clock                                                                                       */
  22.     /*---------------------------------------------------------------------------------------------------------*/

  23.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  24.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  25.     /* Wait for HIRC clock ready */
  26.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  27.     /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
  28.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  29.     /* Enable HXT clock (external XTAL 12MHz) */
  30.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  31.     /* Wait for HXT clock ready */
  32.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  33.     /* Set core clock as PLL_CLOCK from PLL */
  34.     CLK_SetCoreClock(PLL_CLOCK);

  35.     /* Enable UART module clock */
  36.     CLK_EnableModuleClock(UART0_MODULE);

  37.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  38.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init I/O Multi-function                                                                                 */
  41.     /*---------------------------------------------------------------------------------------------------------*/

  42.     /* Set PD multi-function pins for UART0 RXD(PD.6) and TXD(PD.1) */
  43.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD6MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  44.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD6MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  45. }

  46. void UART0_Init()
  47. {
  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Init UART                                                                                               */
  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Reset UART module */
  52.     SYS_ResetModule(UART0_RST);

  53.     /* Configure UART0 and set UART0 baud rate */
  54.     UART_Open(UART0, 115200);
  55. }

  56. /*---------------------------------------------------------------------------------------------------------*/
  57. /*  Main Function                                                                                          */
  58. /*---------------------------------------------------------------------------------------------------------*/
  59. int32_t main(void)
  60. {
  61.     unsigned int u32ByteCount;
  62.     unsigned int u32PageNumber;
  63.     unsigned int u32ProgramFlashAddress = 0;
  64.     unsigned int u32VerifyFlashAddress = 0;
  65.     unsigned int MidDid;

  66.     /* Unlock protected registers */
  67.     SYS_UnlockReg();

  68.     /* Init System, peripheral clock and multi-function I/O */
  69.     SYS_Init();

  70.     /* Lock protected registers */
  71.     SYS_LockReg();

  72.     /* Init UART0 for printf */
  73.     UART0_Init();

  74.     printf("Hello World.\n");
  75.     printf("PLL Clock = %d Hz\n", CLK_GetPLLClockFreq());
  76.     printf("Core Clock = %d Hz\n\n", CLK_GetHCLKFreq());
  77.     printf("+-------------------------------------------------------+\n");
  78.     printf("|       M451 Series SPI_Flash Sample Code         |\n");
  79.     printf("+-------------------------------------------------------+\n");

  80.     /* Open 7-Seg */
  81.     Open_Seven_Segment();

  82.     /* Open SPI for Serial Flash */
  83.     Open_SPI_Flash();

  84.     /* Read MID & DID */
  85.     MidDid = SpiFlash_ReadMidDid();
  86.     printf("\nMID and DID = %x", MidDid);

  87.     /* Erase SPI Flash */
  88.     SpiFlash_ChipErase();
  89.     printf("\nFlash Erasing... ");

  90.     /* Wait ready */
  91.     SpiFlash_WaitReady();
  92.     printf("Done!");

  93.     /* Fill the Source Data and clear Destination Data Buffer */
  94.     for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++)
  95.     {
  96.         SrcArray[u32ByteCount] = u32ByteCount;
  97.         DestArray[u32ByteCount] = 0;
  98.     }

  99.     u32ProgramFlashAddress = 0;
  100.     u32VerifyFlashAddress = 0;
  101.     for(u32PageNumber = 0; u32PageNumber < TEST_NUMBER; u32PageNumber++)
  102.     {
  103.         printf("\n\nTest Page Number = %d", u32PageNumber);
  104.         Show_Seven_Segment(u32PageNumber, 1);
  105.         CLK_SysTickDelay(200000);

  106.         /*=== Program SPI Flash ===*/
  107.         printf("\n Flash Programming... ");

  108.         /* Page Program */
  109.         SpiFlash_PageProgram(SrcArray, u32ProgramFlashAddress, 256);
  110.         SpiFlash_WaitReady();
  111.         u32ProgramFlashAddress += 0x100;
  112.         printf("Done!");

  113.         /*=== Read Back and Compare Data ===*/
  114.         printf("\n Flash Verifying... ");

  115.         /* Page Read */
  116.         SpiFlash_ReadData(DestArray, u32VerifyFlashAddress, 256);
  117.         u32VerifyFlashAddress += 0x100;

  118.         for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++)
  119.         {
  120.             if(DestArray[u32ByteCount] != u32ByteCount)
  121.             {
  122.                 /* Error */
  123.                 printf("SPI Flash R/W Fail!");
  124.                 while(1);
  125.             }
  126.         }

  127.         /* Clear Destination Data Buffer */
  128.         for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++)
  129.             DestArray[u32ByteCount] = 0;

  130.         printf("Done!");
  131.     }

  132.     printf("\n\nSPI Flash Test Ok!");
  133.     printf("\n\n");

  134.     while(1);

  135. }


 楼主| xuanhuanzi 发表于 2019-4-30 21:23 | 显示全部楼层
外挂SPI接口的闪存芯片,很好用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部