[DemoCode下载] FMC读写,可模拟EEPROM存储数据

[复制链接]
986|2
 楼主| 21mengnan 发表于 2024-4-21 17:38 | 显示全部楼层 |阅读模式
  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: 7 $
  5. * $Date: 15/01/16 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to read/program embedded flash by ISP function.
  7. * @note
  8. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  9. *****************************************************************************/
  10. #include <stdio.h>
  11. #include "M071M.h"

  12. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  13. #define PLL_CLOCK           50000000


  14. #define APROM_TEST_BASE             0x3000
  15. #define APROM_TEST_END              0xF000
  16. #define DATA_FLASH_TEST_BASE        0xF000
  17. #define DATA_FLASH_TEST_END         0x11000
  18. #define TEST_PATTERN                0x5A5A5A5A

  19. #define CONFIG0_DFEN                0x01
  20. #define CONFIG0_DFVSEN              0x04

  21. void SYS_Init(void)
  22. {
  23.     /*---------------------------------------------------------------------------------------------------------*/
  24.     /* Init System Clock                                                                                       */
  25.     /*---------------------------------------------------------------------------------------------------------*/

  26.     /* Enable Internal RC 22.1184MHz clock */
  27.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  28.     /* Waiting for Internal RC clock ready */
  29.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  30.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  31.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  32.     /* Enable external XTAL 12MHz clock */
  33.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  34.     /* Waiting for external XTAL clock ready */
  35.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  36.     /* Set core clock as PLL_CLOCK from PLL */
  37.     CLK_SetCoreClock(PLL_CLOCK);

  38.     /* Enable UART module clock */
  39.     CLK_EnableModuleClock(UART0_MODULE);

  40.     /* Select UART module clock source */
  41.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));


  42.     /*---------------------------------------------------------------------------------------------------------*/
  43.     /* Init I/O Multi-function                                                                                 */
  44.     /*---------------------------------------------------------------------------------------------------------*/

  45.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  46.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  47.     SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
  48. }

  49. void UART_Init()
  50. {
  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Init UART                                                                                               */
  53.     /*---------------------------------------------------------------------------------------------------------*/
  54.     UART_Open(UART0, 115200);
  55. }


  56. static int  SetDataFlashBase(uint32_t u32DFBA)
  57. {
  58.     uint32_t au32Config[2];

  59.     /* Read current User Configuration */
  60.     FMC_ReadConfig(au32Config, 1);

  61.     /* Just return when Data Flash has been enabled */
  62.     if(!(au32Config[0] & 0x1))
  63.         return 0;

  64.     /* Enable User Configuration Update */
  65.     FMC_EnableConfigUpdate();

  66.     /* Erase User Configuration */
  67.     FMC_Erase(FMC_CONFIG_BASE);

  68.     /* Write User Configuration to Enable Data Flash */
  69.     /* Note: DFVSEN = 1, DATA Flash Size is 4K bytes
  70.              DFVSEN = 0, DATA Flash Size is based on CONFIG1 */
  71.     au32Config[0] &= ~(CONFIG0_DFEN | CONFIG0_DFVSEN);
  72.     au32Config[1] = u32DFBA;

  73.     if(FMC_WriteConfig(au32Config, 2))
  74.         return -1;

  75.     printf("\nSet Data Flash base as 0x%x.\n", FMC_ReadDataFlashBaseAddr());

  76.     /* Perform chip reset to make new User Config take effect */
  77.     SYS->IPRSTC1 |= SYS_IPRSTC1_CHIP_RST_Msk;

  78.     return 0;
  79. }


  80. int32_t FillDataPattern(uint32_t u32StartAddr, uint32_t u32EndAddr, uint32_t u32Pattern)
  81. {
  82.     uint32_t u32Addr;

  83.     for(u32Addr = u32StartAddr; u32Addr < u32EndAddr; u32Addr += 4)
  84.     {
  85.         FMC_Write(u32Addr, u32Pattern);
  86.     }
  87.     return 0;
  88. }


  89. int32_t  VerifyData(uint32_t u32StartAddr, uint32_t u32EndAddr, uint32_t u32Pattern)
  90. {
  91.     uint32_t    u32Addr;
  92.     uint32_t    u32Data;

  93.     for(u32Addr = u32StartAddr; u32Addr < u32EndAddr; u32Addr += 4)
  94.     {
  95.         u32Data = FMC_Read(u32Addr);
  96.         if(u32Data != u32Pattern)
  97.         {
  98.             printf("\nFMC_Read data verify failed at address 0x%x, read=0x%x, expect=0x%x\n", u32Addr, u32Data, u32Pattern);
  99.             return -1;
  100.         }
  101.     }
  102.     return 0;
  103. }


  104. int32_t  FlashTest(uint32_t u32StartAddr, uint32_t u32EndAddr, uint32_t u32Pattern)
  105. {
  106.     uint32_t    u32Addr;

  107.     for(u32Addr = u32StartAddr; u32Addr < u32EndAddr; u32Addr += FMC_FLASH_PAGE_SIZE)
  108.     {
  109.         printf("    Flash test address: 0x%x    \r", u32Addr);

  110.         // Erase page
  111.         FMC_Erase(u32Addr);

  112.         // Verify if page contents are all 0xFFFFFFFF
  113.         if(VerifyData(u32Addr, u32Addr + FMC_FLASH_PAGE_SIZE, 0xFFFFFFFF) < 0)
  114.         {
  115.             printf("\nPage 0x%x erase verify failed!\n", u32Addr);
  116.             return -1;
  117.         }

  118.         // Write test pattern to fill the whole page
  119.         if(FillDataPattern(u32Addr, u32Addr + FMC_FLASH_PAGE_SIZE, u32Pattern) < 0)
  120.         {
  121.             printf("Failed to write page 0x%x!\n", u32Addr);
  122.             return -1;
  123.         }

  124.         // Verify if page contents are all equal to test pattern
  125.         if(VerifyData(u32Addr, u32Addr + FMC_FLASH_PAGE_SIZE, u32Pattern) < 0)
  126.         {
  127.             printf("\nData verify failed!\n ");
  128.             return -1;
  129.         }

  130.         FMC_Erase(u32Addr);

  131.         // Verify if page contents are all 0xFFFFFFFF
  132.         if(VerifyData(u32Addr, u32Addr + FMC_FLASH_PAGE_SIZE, 0xFFFFFFFF) < 0)
  133.         {
  134.             printf("\nPage 0x%x erase verify failed!\n", u32Addr);
  135.             return -1;
  136.         }
  137.     }
  138.     printf("\r    Flash Test Passed.          \n");
  139.     return 0;
  140. }


  141. int main()
  142. {
  143.     uint32_t i, u32Data;

  144.     /* Unlock protected registers */
  145.     SYS_UnlockReg();

  146.     SYS_Init();
  147.     UART_Init();

  148.     /*
  149.         This sample code is used to show how to use StdDriver API to implement ISP functions.
  150.     */

  151.     printf("\n\n");
  152.     printf("+----------------------------------------+\n");
  153.     printf("|          M071M FMC Sample Code        |\n");
  154.     printf("+----------------------------------------+\n");

  155.     //SYS_UnlockReg();

  156.     /* Enable FMC ISP function */
  157.     FMC_Open();

  158.     if(SetDataFlashBase(DATA_FLASH_TEST_BASE) < 0)
  159.     {
  160.         printf("Failed to set Data Flash base address!\n");
  161.         goto lexit;
  162.     }

  163.     /* Read BS */
  164.     printf("  Boot Mode ............................. ");
  165.     if(FMC_GetBootSource() == 0)
  166.         printf("[APROM]\n");
  167.     else
  168.     {
  169.         printf("[LDROM]\n");
  170.         printf("  WARNING: The driver sample code must execute in AP mode!\n");
  171.         goto lexit;
  172.     }

  173.     u32Data = FMC_ReadCID();
  174.     printf("  Company ID ............................ [0x%08x]\n", u32Data);

  175.     u32Data = FMC_ReadDID();
  176.     printf("  Device ID ............................. [0x%08x]\n", u32Data);

  177.     u32Data = FMC_ReadPID();
  178.     printf("  Product ID ............................ [0x%08x]\n", u32Data);

  179.     for(i = 0; i < 3; i++)
  180.     {
  181.         u32Data = FMC_ReadUID(i);
  182.         printf("  Unique ID %d ........................... [0x%08x]\n", i, u32Data);
  183.     }

  184.     for(i = 0; i < 4; i++)
  185.     {
  186.         u32Data = FMC_ReadUCID(i);
  187.         printf("  Unique Customer ID %d .................. [0x%08x]\n", i, u32Data);
  188.     }

  189.     /* Read User Configuration */
  190.     printf("  User Config 0 ......................... [0x%08x]\n", FMC_Read(FMC_CONFIG_BASE));
  191.     printf("  User Config 1 ......................... [0x%08x]\n", FMC_Read(FMC_CONFIG_BASE + 4));

  192.     /* Read Data Flash base address */
  193.     u32Data = FMC_ReadDataFlashBaseAddr();
  194.     printf("  Data Flash Base Address ............... [0x%08x]\n", u32Data);

  195.     printf("\n\nLDROM test =>\n");
  196.     FMC_EnableLDUpdate();
  197.     if(FlashTest(FMC_LDROM_BASE, FMC_LDROM_BASE + FMC_LDROM_SIZE, TEST_PATTERN) < 0)
  198.     {
  199.         printf("\n\nLDROM test failed!\n");
  200.         goto lexit;
  201.     }
  202.     FMC_DisableLDUpdate();

  203.     printf("\n\nAPROM test =>\n");
  204.     FMC_EnableAPUpdate();
  205.     if(FlashTest(APROM_TEST_BASE, APROM_TEST_END, TEST_PATTERN) < 0)
  206.     {
  207.         printf("\n\nAPROM test failed!\n");
  208.         goto lexit;
  209.     }
  210.     FMC_DisableAPUpdate();

  211.     printf("\n\nData Flash test =>\n");
  212.     if(FlashTest(DATA_FLASH_TEST_BASE, DATA_FLASH_TEST_END, TEST_PATTERN) < 0)
  213.     {
  214.         printf("\n\nUHB test failed!\n");
  215.         goto lexit;
  216.     }

  217. lexit:

  218.     /* Disable FMC ISP function */
  219.     FMC_Close();

  220.     /* Lock protected registers */
  221.     SYS_LockReg();

  222.     printf("\nFMC Sample Code Completed.\n");

  223.     while(1);
  224. }

  225. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/


 楼主| 21mengnan 发表于 2024-4-21 17:44 | 显示全部楼层
学会这个外设使用,那可以节约存储芯片。
jiekou001 发表于 2024-4-21 19:07 | 显示全部楼层
BSP提供的库函数让复杂的操作变得简单了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

88

主题

1151

帖子

1

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