[技术问答] 有人帮忙看看新唐NUC100是如何写Flash的吗

[复制链接]
2025|4
 楼主| syfw 发表于 2017-1-11 19:49 | 显示全部楼层 |阅读模式
各位大神,有写Flash的DEMO程序麻烦发给我一个,一直都是写入失败
gejigeji521 发表于 2017-1-11 22:05 | 显示全部楼层
官方的这个例程是吗、
NUC100_120BSPv3.00.002\SampleCode\StdDriver\FMC_IAP
gejigeji521 发表于 2017-1-11 22:06 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     FMC_IAP.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
  4. * $Revision: 1 $
  5. * $Date: 15/02/17 3:54p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M058S Series IAP function sample code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "NUC100Series.h"

  14. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  15. #define PLL_CLOCK           50000000

  16. #define KEY_ADDR    0x20000FFC      /* The location of signature */
  17. #define SIGNATURE   0x21557899      /* The signature word is used by AP code to check if simple LD is finished */

  18. extern uint32_t loaderImageBase;
  19. extern uint32_t loaderImageLimit;

  20. uint32_t g_u32ImageSize;

  21. uint32_t *g_au32funcTable = (uint32_t *)0x100e00; /* The location of function table */

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

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

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

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

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

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

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

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

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

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

  46.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  47.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
  48. }


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


  56. void FMC_LDROM_Test(void)
  57. {
  58.     int32_t  i32Err;
  59.     uint32_t u32Data, i, *pu32Loader;

  60.     /* Enable LDROM Update */
  61.     FMC_EnableLDUpdate();

  62.     printf("  Erase LD ROM ............................... ");

  63.     /* Page Erase LDROM */
  64.     for(i = 0; i < FMC_LDROM_SIZE; i += FMC_FLASH_PAGE_SIZE)
  65.         FMC_Erase(FMC_LDROM_BASE + i);

  66.     /* Erase Verify */
  67.     i32Err = 0;

  68.     for(i = FMC_LDROM_BASE; i < (FMC_LDROM_BASE + FMC_LDROM_SIZE); i += 4)
  69.     {
  70.         u32Data = FMC_Read(i);

  71.         if(u32Data != 0xFFFFFFFF)
  72.         {
  73.             i32Err = 1;
  74.         }
  75.     }

  76.     if(i32Err)
  77.         printf("[FAIL]\n");
  78.     else
  79.         printf("[OK]\n");

  80.     printf("  Program Simple LD Code ..................... ");
  81.     pu32Loader = (uint32_t *)&loaderImageBase;
  82.     g_u32ImageSize = (uint32_t)&loaderImageLimit - (uint32_t)&loaderImageBase;
  83.     for(i = 0; i < g_u32ImageSize; i += 4)
  84.     {
  85.         /* Erase page when necessary */
  86.         if((i & (FMC_FLASH_PAGE_SIZE - 1)) == 0)
  87.         {
  88.             FMC_Erase(FMC_LDROM_BASE + i);
  89.         }

  90.         FMC_Write(FMC_LDROM_BASE + i, pu32Loader[i / 4]);
  91.     }

  92.     /* Verify loader */
  93.     i32Err = 0;
  94.     for(i = 0; i < g_u32ImageSize; i += 4)
  95.     {
  96.         u32Data = FMC_Read(FMC_LDROM_BASE + i);
  97.         if(u32Data != pu32Loader[i / 4])
  98.             i32Err = 1;
  99.     }

  100.     if(i32Err)
  101.     {
  102.         printf("[FAIL]\n");
  103.     }
  104.     else
  105.     {
  106.         printf("[OK]\n");
  107.     }
  108. }

  109. /*---------------------------------------------------------------------------------------------------------*/
  110. /*  Main Function                                                                                          */
  111. /*---------------------------------------------------------------------------------------------------------*/
  112. int32_t main(void)
  113. {
  114.     uint32_t u32Data;
  115.     uint32_t u32Cfg;
  116.     int32_t (*func)(int32_t n);
  117.     int32_t i;

  118.     /*
  119.         This sample code is used to demo how IAP works.
  120.         In other words, it shows how to call functions of LDROM code in APROM.

  121.         The execution flow of this code is:
  122.         1. Make sure FMC_LD.bin is existed. User can select "FMC_IAP_LD" target to build it.
  123.         2. The code will ask user to update LDROM code. User may press 'y' to update LDROM with "FMC_IAP_LD.bin".
  124.         3. The code will call the functions in LDROM by function table located at 0x100e00.
  125.     */

  126.     /* Unlock protected registers for ISP function */
  127.     SYS_UnlockReg();

  128.     /* Init System, IP clock and multi-function I/O */
  129.     SYS_Init();

  130.     /* Init UART0 for printf */
  131.     UART0_Init();

  132.     printf("+---------------------------------------------------+\n");
  133.     printf("|   NUC100 Series FMC IAP Sample Code for APROM     |\n");
  134.     printf("+---------------------------------------------------+\n");

  135.     printf("\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n\n", SystemCoreClock);


  136.     /* Enable ISP function */
  137.     FMC_Open();

  138.     /* Check IAP mode */
  139.     u32Cfg = FMC_Read(FMC_CONFIG_BASE);
  140.     if((u32Cfg & 0xc0) != 0x80)
  141.     {
  142.         printf("Do you want to set to new IAP mode (APROM boot + LDROM) y/n?\n");
  143.         if(getchar() == 'y')
  144.         {
  145.             FMC_EnableConfigUpdate(); /* Enable user configuration update */

  146.             /* Set CBS to b'10 */
  147.             u32Cfg &= ~0xc0ul;
  148.             u32Cfg |= 0x80;
  149.             u32Data = FMC_Read(FMC_CONFIG_BASE + 0x4); /* Backup the data of config1 */
  150.             FMC_Erase(FMC_CONFIG_BASE);
  151.             FMC_Write(FMC_CONFIG_BASE, u32Cfg);
  152.             FMC_Write(FMC_CONFIG_BASE + 0x4, u32Data);

  153.             printf("Press any key to reset system to enable new IAP mode ...\n");
  154.             getchar();
  155.             SYS_ResetChip(); /* Reset MCU */
  156.             while(1);
  157.         }
  158.         else
  159.         {
  160.             goto lexit;
  161.         }
  162.     }

  163.     printf("Do you want to write LDROM code to 0x100000\n");

  164.     if(getchar() == 'y')
  165.     {
  166.         /* Check LD image size */
  167.         g_u32ImageSize = (uint32_t)&loaderImageLimit - (uint32_t)&loaderImageBase;

  168.         if(g_u32ImageSize == 0)
  169.         {
  170.             printf("  ERROR: Loader Image is 0 bytes!\n");
  171.             goto lexit;
  172.         }

  173.         if(g_u32ImageSize > FMC_LDROM_SIZE)
  174.         {
  175.             printf("  ERROR: Loader Image is larger than 4KBytes!\n");
  176.             goto lexit;
  177.         }

  178.         /* Erase LDROM, program LD sample code to LDROM, and verify LD sample code */
  179.         FMC_LDROM_Test();
  180.     }

  181.     for(i = 0; i < 4; i++)
  182.     {
  183.         /* Call the function of LDROM */
  184.         func = (int32_t (*)(int32_t))g_au32funcTable[i];
  185.         if(func(i + 1) == i + 1)
  186.         {
  187.             printf("Call LDROM function %d ok!\n", i);
  188.         }
  189.         else
  190.         {
  191.             printf("Call LDROM function %d fail.\n", i);
  192.         }
  193.     }

  194. lexit:

  195.     /* Disable ISP function */
  196.     FMC_Close();

  197.     /* Lock protected registers */
  198.     SYS_LockReg();

  199.     printf("\nDone\n");
  200.     while(SYS->PDID) __WFI();
  201. }





 楼主| syfw 发表于 2017-1-12 10:12 | 显示全部楼层

请问写入Flash或者读Flash是否需要关闭所有中断啊?
gejigeji521 发表于 2017-1-26 20:13 | 显示全部楼层
不用管中断的事情。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

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