[DemoCode下载] M0518读写闪存操作方式

[复制链接]
2783|15
 楼主| 小明的同学 发表于 2018-2-25 13:56 | 显示全部楼层 |阅读模式
  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: 2 $
  5. * $Date: 14/12/25 10:24a $
  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 "M0518.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_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  47. }

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


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

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

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

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

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

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

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

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

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

  77.     return 0;
  78. }


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

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


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

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


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

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

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

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

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

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

  129.         FMC_Erase(u32Addr);

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


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

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

  145.     SYS_Init();
  146.     UART_Init();

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

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

  154.     //SYS_UnlockReg();

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

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

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

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

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

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

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

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

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

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

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

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

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

  216. lexit:

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

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

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

  222.     while(1);
  223. }

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


 楼主| 小明的同学 发表于 2018-2-25 14:12 | 显示全部楼层
用这个方法,可以利用上用不完的flash
huahuagg 发表于 2018-2-25 15:17 | 显示全部楼层
省了一个EEPROM芯片。
xuanhuanzi 发表于 2018-2-25 19:57 | 显示全部楼层
看着不简单啊
21mengnan 发表于 2018-2-25 22:50 | 显示全部楼层
地址标记,地址读写
xinxianshi 发表于 2018-2-26 13:33 | 显示全部楼层
FMC是什么
yiy 发表于 2018-2-26 19:18 | 显示全部楼层
读写闪存看来是需要解除保护的。
xixi2017 发表于 2018-2-26 20:24 | 显示全部楼层
出错的时候返回负一
dongnanxibei 发表于 2018-2-26 21:03 | 显示全部楼层
每个新唐的好像都是可以的
稳稳の幸福 发表于 2018-2-26 21:45 | 显示全部楼层
也可以做IAP应用。
734774645 发表于 2018-2-26 21:51 | 显示全部楼层
如果使能外部晶振后,外部没有晶振会有什么
734774645 发表于 2018-2-26 21:51 | 显示全部楼层
会停机吗
734774645 发表于 2018-2-27 08:30 | 显示全部楼层
用开发板试了一下,非常好用,一次成功。
yiyigirl2014 发表于 2018-2-27 13:20 | 显示全部楼层
比ST的库函数好懂。
643757107 发表于 2018-2-27 18:22 | 显示全部楼层
比操作外部EEPROM复杂点啊。
jiekou001 发表于 2018-2-27 19:08 | 显示全部楼层
默认的串口使用最简单了,闪存的读写操作要会指针。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1640

帖子

2

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