[DemoCode下载] M051-FMC_IAP例程

[复制链接]
2055|3
 楼主| 玛尼玛尼哄 发表于 2016-5-7 15:59 | 显示全部楼层 |阅读模式
iap, fm, se, TE, AD
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     APROM_main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 7 $
  5. * $Date: 14/01/28 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    FMC APROM IAP sample for M051 series MCU
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"

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

  15. typedef void (FUNC_PTR)(void);

  16. extern uint32_t  loaderImage1Base, loaderImage1Limit;


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

  22.     /* Enable External XTAL (4~24 MHz) */
  23.     CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk;

  24.     CLK->PLLCON = PLLCON_SETTING;

  25.     /* Waiting for clock ready */
  26.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk);

  27.     /* Switch HCLK clock source to PLL */
  28.     CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_PLL;

  29.     /* Enable IP clock */
  30.     CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk;

  31.     /* Select IP clock source */
  32.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_HXT;

  33.     /* Update System Core Clock */
  34.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  35.     //SystemCoreClockUpdate();
  36.     PllClock        = PLL_CLOCK;            // PLL
  37.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  38.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init I/O Multi-function                                                                                 */
  41.     /*---------------------------------------------------------------------------------------------------------*/
  42.     /* Set P3 multi-function pins for UART0 RXD and TXD  */
  43.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  44. }



  45. void UART_Init()
  46. {
  47.     /*---------------------------------------------------------------------------------------------------------*/
  48.     /* Init UART                                                                                               */
  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     UART_Open(UART0, 115200);
  51. }


  52. static int SetIAPBoot(void)
  53. {
  54.     uint32_t  au32Config[2];
  55.     uint32_t u32CBS;

  56.     /* Read current boot mode */
  57.     u32CBS = (FMC->ISPSTA & FMC_ISPSTA_CBS_Msk) >> FMC_ISPSTA_CBS_Pos;
  58.     if(u32CBS & 1)
  59.     {
  60.         /* Modify User Configuration when it is not in IAP mode */

  61.         FMC_ReadConfig(au32Config, 2);
  62.         if(au32Config[0] & 0x40)
  63.         {
  64.             FMC_EnableConfigUpdate();
  65.             au32Config[0] &= ~0x40;
  66.             FMC_Erase(FMC_CONFIG_BASE);
  67.             FMC_WriteConfig(au32Config, 2);

  68.             // Perform chip reset to make new User Config take effect
  69.             SYS_ResetChip();
  70.         }
  71.     }
  72.     return 0;
  73. }

  74. static int  LoadImage(uint32_t u32ImageBase, uint32_t u32ImageLimit, uint32_t u32FlashAddr, uint32_t u32MaxSize)
  75. {
  76.     uint32_t   i, j, u32Data, u32ImageSize, *pu32Loader;

  77.     u32ImageSize = u32MaxSize;

  78.     printf("Program image to flash address 0x%x...", u32FlashAddr);
  79.     pu32Loader = (uint32_t *)u32ImageBase;
  80.     for(i = 0; i < u32ImageSize; i += FMC_FLASH_PAGE_SIZE)
  81.     {
  82.         FMC_Erase(u32FlashAddr + i);
  83.         for(j = 0; j < FMC_FLASH_PAGE_SIZE; j += 4)
  84.         {
  85.             FMC_Write(u32FlashAddr + i + j, pu32Loader[(i + j) / 4]);
  86.         }
  87.     }
  88.     printf("OK.\n");

  89.     printf("Verify ...");

  90.     /* Verify loader */
  91.     for(i = 0; i < u32ImageSize; i += FMC_FLASH_PAGE_SIZE)
  92.     {
  93.         for(j = 0; j < FMC_FLASH_PAGE_SIZE; j += 4)
  94.         {
  95.             u32Data = FMC_Read(u32FlashAddr + i + j);

  96.             if(u32Data != pu32Loader[(i + j) / 4])
  97.             {
  98.                 printf("data mismatch on 0x%x, [0x%x], [0x%x]\n", u32FlashAddr + i + j, u32Data, pu32Loader[(i + j) / 4]);
  99.                 return -1;
  100.             }

  101.             if(i + j >= u32ImageSize)
  102.                 break;
  103.         }
  104.     }
  105.     printf("OK.\n");
  106.     return 0;
  107. }


  108. int main()
  109. {
  110.     uint8_t     u8Item;
  111.     uint32_t    u32Data;
  112.     char *acBootMode[] = {"LDROM+IAP", "LDROM", "APROM+IAP", "APROM"};
  113.     uint32_t u32CBS;

  114.     /* Unlock protected registers */
  115.     SYS_UnlockReg();

  116.     /* Init system clock and multi-function I/O */
  117.     SYS_Init();

  118.     /* Init UART */
  119.     UART_Init();

  120.     printf("\n\n");
  121.     printf("+----------------------------------------+\n");
  122.     printf("|      M051 FMC IAP Sample Code          |\n");
  123.     printf("|           [APROM code]                 |\n");
  124.     printf("+----------------------------------------+\n");


  125.     /* Enable FMC ISP function */
  126.     FMC_Open();

  127.     if(SetIAPBoot() < 0)
  128.     {
  129.         printf("Failed to set IAP boot mode!\n");
  130.         goto lexit;
  131.     }

  132.     /* Get boot mode */
  133.     printf("  Boot Mode ............................. ");
  134.     u32CBS = (FMC->ISPSTA & FMC_ISPSTA_CBS_Msk) >> FMC_ISPSTA_CBS_Pos;
  135.     printf("[%s]\n", acBootMode[u32CBS]);

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

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

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

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

  145.     do
  146.     {
  147.         printf("\n\n\n");
  148.         printf("+----------------------------------------+\n");
  149.         printf("|               Select                   |\n");
  150.         printf("+----------------------------------------+\n");
  151.         printf("| [0] Load IAP code to LDROM             |\n");
  152.         printf("| [1] Run IAP program (in LDROM)         |\n");
  153.         printf("+----------------------------------------+\n");
  154.         printf("Please select...");
  155.         u8Item = getchar();
  156.         printf("%c\n", u8Item);

  157.         switch(u8Item)
  158.         {
  159.         case '0':
  160.             FMC_EnableLDUpdate();
  161.             if(LoadImage((uint32_t)&loaderImage1Base, (uint32_t)&loaderImage1Limit,
  162.                          FMC_LDROM_BASE, FMC_LDROM_SIZE) != 0)
  163.             {
  164.                 printf("Load image to LDROM failed!\n");
  165.                 goto lexit;
  166.             }
  167.             FMC_DisableLDUpdate();
  168.             break;

  169.         case '1':
  170.             printf("\n\nChange VECMAP and branch to LDROM...\n");
  171.             UART_WAIT_TX_EMPTY(UART0); /* To make sure all message has been print out */

  172.             /* Mask all interrupt before changing VECMAP to avoid wrong interrupt handler fetched */
  173.             __set_PRIMASK(1);

  174.             /* Set VECMAP to LDROM for booting from LDROM */
  175.             FMC_SetVectorPageAddr(FMC_LDROM_BASE);

  176.             /* Software reset to boot to LDROM */
  177.             NVIC_SystemReset();

  178.             break;

  179.         default :
  180.             break;
  181.         }
  182.     }
  183.     while(1);


  184. lexit:

  185.     /* Disable FMC ISP function */
  186.     FMC_Close();

  187.     /* Lock protected registers */
  188.     SYS_LockReg();

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

  190.     while(1);
  191. }

  192. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| 玛尼玛尼哄 发表于 2016-5-7 16:04 | 显示全部楼层
QQ截图20160507160325.png
看这里就用了个goto ,这 说明一个程序里来个这,还是非常完美的
 楼主| 玛尼玛尼哄 发表于 2016-5-7 16:07 | 显示全部楼层
QQ截图20160507160609.png
而这里使用了一个 do while(1)
我们知道do while 是至少执行一次的意思,而 do while(0)也因此被经常使用。
neeringstu 发表于 2016-5-7 22:31 | 显示全部楼层
玛尼玛尼哄 发表于 2016-5-7 16:04
看这里就用了个goto ,这 说明一个程序里来个这,还是非常完美的

新唐的程序有没有库函数版本啊,感觉对于新手库函数容易些
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

3261

帖子

2

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