[DemoCode下载] USB 鼠標IAP 更新代碼

[复制链接]
2247|2
 楼主| a_ziliu 发表于 2015-10-27 12:04 | 显示全部楼层 |阅读模式

以下功能
1support iap.ncu123 64k flash 分成232k,iap rom32k, ap rom32k

2support hid transfer,keyboard, mouse. APROM LDROM rom都使用同一個pid,vid.

3windows ap實現更新aprom的功能,讀取版號,及ap rom/iap rom 跳躍功能。



因為2project建立環境,請執行build_command.bat

APROM:\SampleCode\StdDriver\USBD_HID_Mouse_KB_TRANSFER_AP V1.0\KEIL
LDROM:\SampleCode\StdDriver\USBD_HID_Mouse_KB_TRANSFER_IAP V1.0\KEIL
PC AP:\SampleCode\StdDriver\WindowsTool\HIDTransferTest

Checksum source code: \SampleCode\StdDriver\checksum

NUC123SeriesBSP_CMSIS_V3.00.001_mouse_dev - 20150211.zip (486.58 KB, 下载次数: 35)



玛尼玛尼哄 发表于 2015-10-27 13:14 | 显示全部楼层
support iap.以ncu123 64k flash 分成2個32k,iap rom前32k, ap rom後32k
玛尼玛尼哄 发表于 2015-10-27 13:14 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * @brief
  4. *           Demonstrate how to implement a USB mouse device.
  5. *           It use PC0 ~ PC5 to control mouse direction and mouse key.
  6. *           It also supports USB suspend and remote wakeup.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC123.h"
  12. #include "hid_mouse.h"
  13. #include "common.h"
  14. #define CLK_PLLCON_60MHz_XTAL     0xC026 // 60MHz
  15. #define CLK_PLLCON_72MHz_XTAL     0xC02E // 72MHz
  16. #define CLK_PLLCON_144MHz_XTAL    0x402E // 144MHz

  17. /*--------------------------------------------------------------------------*/
  18. void SYS_Init(void)
  19. {

  20.     /* Enable XT1_OUT (PF0) and XT1_IN (PF1) */
  21.     SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;


  22.     /*---------------------------------------------------------------------------------------------------------*/
  23.     /* Init System Clock                                                                                       */
  24.     /*---------------------------------------------------------------------------------------------------------*/

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

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

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

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

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

  35.     /* Set Flash Access Delay */
  36.     FMC->FATCON |= FMC_FATCON_FOMSEL1_Msk | FMC_FATCON_FOMSEL0_Msk;

  37.     /* Set core clock */
  38.     CLK_SetCoreClock(72000000);

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

  42.     /* Select module clock source */
  43.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  44.     CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV_USB(3));


  45.     /*---------------------------------------------------------------------------------------------------------*/
  46.     /* Init I/O Multi-function                                                                                 */
  47.     /*---------------------------------------------------------------------------------------------------------*/

  48.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  49.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
  50.     /* Set PC.13 as CLKO function pin */
  51.     //SYS->GPC_MFP = SYS_GPC_MFP_PC13_CLKO;
  52.     //SYS->ALT_MFP = SYS_ALT_MFP_PC13_CLKO;

  53.     /* Enable CLKO (PC.13) for monitor HCLK. CLKO = HCLK/8 Hz*/
  54.     //CLK_EnableCKO(CLK_CLKSEL2_FRQDIV_S_HCLK, 2, NULL);
  55. }


  56. void UART0_Init(void)
  57. {
  58.     /*---------------------------------------------------------------------------------------------------------*/
  59.     /* Init UART                                                                                               */
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Reset IP */
  62.     SYS->IPRSTC2 |=  SYS_IPRSTC2_UART0_RST_Msk;
  63.     SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART0_RST_Msk;

  64.     /* Configure UART0 and set UART0 Baudrate */
  65.     UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
  66.     UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  67. }

  68. void GPIO_Init(void)
  69. {
  70.     /* Enable PC0~5 interrupt for wakeup */
  71.     PC->ISRC |= 0x3f;
  72.     PC->IEN |= 0x3f | (0x3f << 16);
  73.     PC->DBEN |= 0x3f;      // Enable key debounce
  74.     GPIO->DBNCECON = 0x16; // Debounce time is about 6ms
  75.     NVIC_EnableIRQ(GPCDF_IRQn);
  76. }


  77. void GPCDF_IRQHandler(void)
  78. {
  79.     PC->ISRC = 0x3f;
  80.     //PB4 ^= 1;

  81. }



  82. unsigned int  Check_Sum_read_flash(uint32_t u32StartAddr, uint32_t u32EndAddr)
  83. {
  84.     uint32_t         u32Addr;
  85.     uint32_t         u32data=0,temp;
  86.    
  87.     for (u32Addr = u32StartAddr; u32Addr < u32EndAddr; u32Addr += 4)
  88.     {     
  89.                         //temp=Read_Flash(u32Addr);
  90.                         temp=FMC_Read(u32Addr);
  91.                   u32data += (temp&0xff);               
  92.                         u32data += (temp>>8)&0xff;               
  93.                         u32data += (temp>>16)&0xff;               
  94.                         u32data += (temp>>24)&0xff;               
  95.     }
  96.     return u32data;
  97. }

  98. #define IAP_COMMAND_RAM        (0x20004000-4)

  99. #define IAP_SIG 0X5AA55771       
  100. volatile unsigned int CHECKSUM, AP_FLASH_CHECKSUM;
  101. typedef void (FUNC_PTR)(void);

  102.        
  103.        


  104. static int  load_image_to_flash(uint32_t image_base, uint32_t image_limit)
  105. {
  106.     uint32_t  u32ImageSize;

  107.     u32ImageSize = image_limit - image_base;
  108.     if (u32ImageSize == 0) {
  109.         //printf("  ERROR: Loader Image is 0 bytes!\n");
  110.         return -1;
  111.     }

  112.     if (u32ImageSize > APROM_SIZE) {
  113.         //printf("  ERROR: Loader Image is larger than %d KBytes!\n", APROM_SIZE/1024);
  114.         return -1;
  115.     }
  116.     //printf("OK.\n");
  117.     return 0;
  118. }
  119. extern uint32_t loaderImage1Base, loaderImage1Limit;
  120. uint32_t jump_flag;
  121. /*---------------------------------------------------------------------------------------------------------*/
  122. /*  Main Function                                                                                          */
  123. /*---------------------------------------------------------------------------------------------------------*/
  124. int32_t main(void)
  125. {
  126.           uint32_t *IAP_COMMAND;
  127.         uint32_t i;
  128.           FUNC_PTR    *ResetFunc;
  129.           IAP_COMMAND = (uint32_t *)IAP_COMMAND_RAM; //for ram command
  130.                
  131.           //for backrom file
  132.           load_image_to_flash((uint32_t)&loaderImage1Base, (uint32_t)&loaderImage1Limit);
  133.        
  134.     /* Unlock protected registers */
  135.     SYS_UnlockReg();

  136.     SYS_Init();

  137.     UART0_Init();

  138.     GPIO_Init();

  139.           /*enable FMC ISP function */
  140.           //for flash operation
  141.                 FMC_Open();
  142.          //FMC_SetVectorPageAddr(0x0);
  143.           FMC_EnableAPUpdate();       


  144.          if(*IAP_COMMAND==IAP_SIG)
  145.           goto JMP_IAP;               
  146.        
  147.                
  148.                 //read flash backup checksum
  149.     CHECKSUM=FMC_Read(CHECKSUM_ADDRESS);
  150.        
  151.     //CHECK SUM                                
  152.     AP_FLASH_CHECKSUM=Check_Sum_read_flash(APROM_ADDRESS,APROM_ADDRESS_END);

  153.           if(CHECKSUM == AP_FLASH_CHECKSUM){
  154.                   goto JMP_APROM;               
  155.           }
  156.        
  157.            //if checksum is ok, jump to IAP
  158. JMP_IAP:
  159.                 USBD_Open(&gsInfo, HID_ClassRequest, NULL); //OPEN USB FUNCTION.

  160.     /* Endpoint configuration */
  161.     HID_Init();
  162.     USBD_Start();
  163.     NVIC_EnableIRQ(USBD_IRQn);
  164.     USBD_SET_PAYLOAD_LEN(EP4,64);
  165.     USBD_SET_PAYLOAD_LEN(EP5,64);
  166.     //PB4 = 0; // LED to show system is on line                               
  167.                
  168.                 //DO IAP FLOW
  169.                 jump_flag=0;

  170.     while(1)
  171.     {
  172.                         if(jump_flag==1)
  173.                         {
  174.                                 for(i=0;i<100;i++)                               
  175.                                 CLK_SysTickDelay(5000);
  176.                                 break;
  177.                         }
  178.      //HID_UpdateMouseData();
  179.                  //HID_UpdateKbData();
  180.     }
  181.                
  182. JMP_APROM:
  183.                 //FMC_DisableAPUpdate();
  184.           //FMC_Close();
  185.                 //BranchTo(APROM_ADDRESS);
  186.                 *IAP_COMMAND=0;
  187.                 /* Disable all interrupts before change VECMAP */
  188.     NVIC->ICER[0] = 0xFFFFFFFF;  
  189.                 /* Set vector table of startup AP address */
  190.                  FMC_SetVectorPageAddr(0x8000);       
  191.                
  192.                 /* Reset All IP before boot to new AP */
  193.     SYS->IPRSTC2 = 0xFFFFFFFF;
  194.     SYS->IPRSTC2 = 0;
  195.    
  196.     /* Obtain Reset Handler address of new boot. */  
  197.                 ResetFunc = (FUNC_PTR *)M32(4);  

  198.     /* Set Main Stack Pointer register of new boot */
  199.     __set_MSP(M32(0));
  200.    
  201.     /* Call reset handler of new boot */
  202.     ResetFunc();      
  203.                
  204.     while(1);               
  205. }

  206. typedef __packed struct
  207. {
  208.     uint32_t u8Cmd;
  209.     uint32_t address;      
  210.     uint32_t data;
  211.           uint32_t pid;
  212. } CMD_T;

  213. CMD_T rCmd, tCmd;

  214. void EP5_Handler(void)  /* Interrupt out  handler */
  215. {
  216.           uint8_t *ptr4;       
  217.           uint8_t *ptr5;       
  218.           uint32_t erase_addrss;
  219.           /* Interrupt In */       
  220.           ptr4 = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP4));
  221.     /* Interrupt OUT */       
  222.     ptr5 = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP5));
  223.     //USBD_MemCopy(&g_u8PageBuff[0], ptr5, EP5_MAX_PKT_SIZE);
  224.        
  225.           //do iap command in here
  226.          /*
  227.          for(t=0;t<64;t++)
  228.           {
  229.             printf("0x%x\n\r",g_u8PageBuff[t]);
  230.            g_u8PageBuff[t]=g_u8PageBuff[t]+sss;
  231.         }       
  232.         sss=sss+1;
  233.         */
  234.         USBD_MemCopy((uint8_t *)&rCmd, ptr5, 64);
  235.         //check verison
  236.         if(rCmd.u8Cmd==ISP_COMMAND_VERSION)
  237.         {
  238.                 tCmd.u8Cmd=ISP_COMMAND_VERSION;
  239.                 tCmd.data=ISP_VERION;
  240.                
  241.   }
  242.        
  243.                 //erase
  244.         if(rCmd.u8Cmd==ISP_COMMAND_ERASE_APROM)
  245.         {
  246.                 tCmd.u8Cmd=ISP_COMMAND_ERASE_APROM;
  247.                 for(erase_addrss=APROM_ADDRESS;erase_addrss<APROM_ADDRESS+APROM_SIZE;erase_addrss=erase_addrss+512)
  248.                 {
  249.                 if(FMC_Erase(erase_addrss)!=0)
  250.                 {                               
  251.                 tCmd.data=0;//false               
  252.                 goto finish;
  253.           }               
  254.                 }
  255.                 tCmd.data=1;//true
  256.   }
  257.        
  258.         //program
  259.         if(rCmd.u8Cmd==ISP_COMMAND_PROGRAM_APROM)
  260.         {
  261.                 tCmd.u8Cmd=ISP_COMMAND_PROGRAM_APROM;
  262.                 FMC_Write(rCmd.address,rCmd.data);
  263.                 if(FMC_Read(rCmd.address)!=rCmd.data)
  264.                 tCmd.data=0;//false               
  265.                 else               
  266.                 tCmd.data=1;//pass
  267.   }
  268.        
  269.         if(rCmd.u8Cmd==ISP_COMMAND_JUMP_APROM)
  270.         {
  271.                 tCmd.u8Cmd=ISP_COMMAND_JUMP_APROM;
  272.                 tCmd.data=1;
  273.                 jump_flag=1;
  274.   }
  275.        
  276. finish:       
  277.         tCmd.pid=rCmd.pid;
  278.        
  279. //end iap
  280.           USBD_MemCopy(ptr4, (uint8_t *)&tCmd, EP4_MAX_PKT_SIZE);
  281.     USBD_SET_PAYLOAD_LEN(EP4, EP4_MAX_PKT_SIZE);
  282.           
  283.           USBD_SET_PAYLOAD_LEN(EP5, EP5_MAX_PKT_SIZE);
  284.          // g_u8EP5Ready = 1;       
  285. }


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


您需要登录后才可以回帖 登录 | 注册

本版积分规则

100

主题

310

帖子

6

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