[DemoCode下载] NUC505如何在指定位置塞入指定数值

[复制链接]
972|6
 楼主| zhuomuniao110 发表于 2017-3-20 00:09 | 显示全部楼层 |阅读模式
本代码展示如何在指定位置塞入指定数值,可以当作加密时的辨识码,或是版本号码的判断。
代码内预设的辨识码为 20040929 其所在位置为 0x130,版本号码为 abcd0001 其所在位置为
0x100。若用 IAR 开发环境需先 define IAR 使可开始执行。
必须注意此位置不可与中断向量位置重迭,需摆在其之后。各芯片型号如下:
1. NUC505 须放在 0xBC 之后
2. M451 series 须放在 0x13C 之后
3. NUC400 series 须放在 0x240 之后

 楼主| zhuomuniao110 发表于 2017-3-20 00:10 | 显示全部楼层
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.0
  4. * $Date: 16/09/02 10:04a $
  5. * @brief
  6. *       Display how to insert OTP signature and Version number into SPI Flash.
  7. *                      
  8. * @note
  9. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "NUC505Series.h"

  15. /* If you want to encrypt binary firmware, you can enable
  16.    the session code and assign signature and offset here */
  17. #define OTP_SIG1    (0x20040929)
  18. #define OTP_OFFSET  (0x130)
  19. #define Version    (0xabcd0001)
  20. #define V_OFFSET  (0x100)
  21. //#define IAR

  22. #ifdef IAR   
  23. /* IAR */
  24. static const uint32_t gu32OtpAddr[1] [url=home.php?mod=space&uid=72445]@[/url] OTP_OFFSET = {OTP_SIG1};
  25. static const uint32_t gu32VAddr[1] @ V_OFFSET = {Version};
  26. #else
  27. /* Keil */
  28. __attribute__((at(OTP_OFFSET))) static const unsigned int gu32OtpAddr[1] = {OTP_SIG1};  
  29. __attribute__((at(V_OFFSET))) static const unsigned int gu32VAddr[1] = {Version};
  30. #endif
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. /* Global variables                                                                                        */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. uint32_t g_au32BufS[1],g_au32BufV[1];;

  35. void SYS_Init(void)
  36. {
  37.     /*---------------------------------------------------------------------------------------------------------*/
  38.     /* Init System Clock                                                                                       */
  39.     /*---------------------------------------------------------------------------------------------------------*/

  40.     /* Enable  XTAL */
  41.     CLK->PWRCTL |= CLK_PWRCTL_HXTEN_Msk;

  42.     CLK_SetCoreClock(96000000);
  43.        
  44.     /* Set PCLK divider */
  45.     CLK_SetModuleClock(PCLK_MODULE, NULL, 1);

  46.     /* Update System Core Clock */
  47.     SystemCoreClockUpdate();

  48.     /* Enable IP clock */
  49.     CLK_EnableModuleClock(UART0_MODULE);

  50.     /* Select IP clock source */
  51.     CLK_SetModuleClock(UART0_MODULE, CLK_UART0_SRC_EXT, 0);

  52.     /*---------------------------------------------------------------------------------------------------------*/
  53.     /* Init I/O Multi-function                                                                                 */
  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Configure multi-function pins for UART0 RXD and TXD */
  56.     SYS->GPB_MFPL  = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB0MFP_Msk) ) | SYS_GPB_MFPL_PB0MFP_UART0_TXD;
  57.     SYS->GPB_MFPL  = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB1MFP_Msk) ) | SYS_GPB_MFPL_PB1MFP_UART0_RXD;

  58. }

  59. void UART0_Init()
  60. {
  61.     /*---------------------------------------------------------------------------------------------------------*/
  62.     /* Init UART                                                                                               */
  63.     /*---------------------------------------------------------------------------------------------------------*/
  64.     /* Reset UART module */
  65.     SYS_ResetModule(UART0_RST);

  66.     /* Configure UART0 and set UART0 baud rate */
  67.     UART_Open(UART0, 115200);
  68. }

  69. /*---------------------------------------------------------------------------------------------------------*/
  70. /* MAIN function                                                                                           */
  71. /*---------------------------------------------------------------------------------------------------------*/
  72. int32_t main(void)
  73. {
  74.        
  75.     SYS_Init();
  76.     UART0_Init();
  77.     printf("+-------------------------------------+ \n");
  78.     printf("| NUC505 Cipher Signature Sample Code | \n");
  79.     printf("+-------------------------------------+ \n");
  80.     /* Set OTP */
  81. #ifdef IAR   
  82.     if (0)
  83.      {
  84.        uint32_t s;
  85.        s = gu32OtpAddr[0]+1;
  86.      }
  87.     if (0)
  88.      {
  89.        uint32_t s;
  90.        s = gu32VAddr[0]+1;
  91.      }
  92. #endif
  93.                
  94.                 printf("The signature is 0x20040929\n");
  95.                 printf("The Version is 0xabcd0001\n");
  96.                  
  97.                 memset((char *)g_au32BufS, 0, 32);
  98.                 memset((char *)g_au32BufV, 0, 32);
  99.                  
  100.                 /* Load signature from SPI Flash 0x130 */
  101.                 memcpy(g_au32BufS,(void *)304 , sizeof(uint32_t)*1);
  102.                 /* Load Version from SPI Flash 0x100 */
  103.                 memcpy(g_au32BufV,(void *)256 , sizeof(uint32_t)*1);
  104.                
  105.                  if((g_au32BufS[0]==OTP_SIG1)&&(g_au32BufV[0]==Version)){
  106.                         printf("Verify the Data is correct\n");
  107.                         printf("Demo complete\n");
  108.                 }
  109.                 else{
  110.                         printf("Data validation error\n");
  111.                 }
  112.                 while(1);       
  113. }

  114. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/


 楼主| zhuomuniao110 发表于 2017-3-20 00:11 | 显示全部楼层
这个技术还是很有用的。
 楼主| zhuomuniao110 发表于 2017-3-20 00:11 | 显示全部楼层
EC_NUC505_Cipher_Signature_V1.0.zip (917.19 KB, 下载次数: 4)



643757107 发表于 2017-3-20 13:42 | 显示全部楼层
这些位置存储的数据可以是系统或者项目的版本号之类的信息。
wahahaheihei 发表于 2017-3-20 17:07 | 显示全部楼层
主要用于产品的信息版本号之类的,也可以使用Flash搞啊,不过这个更方便。
dongnanxibei 发表于 2017-3-21 11:30 | 显示全部楼层
这一招还可以用于系统加密。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

233

主题

3529

帖子

11

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