1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "menu.h"
#include "ymodem.h"
#include "mk_flash.h"
#include "mk_uart.h"
//#include "Usart.h"
//#include "SPIFlash.h"
#include "AppConfig.h"
#include "HIDO_TypeDef.h"
#include "mk_trace.h"
//#include "stm32f4xx_hal.h"
 
 
pFunction Jump_To_Application;
uint32_t JumpAddress;
 
uint32_t FlashProtection = 0;
uint8_t tab_1024[1024] =
  {
    0
  };
    
void Int2Str(uint8_t* str, int32_t intnum)
{
  uint32_t i, Div = 1000000000, j = 0, Status = 0;
 
  for (i = 0; i < 10; i++)
  {
    str[j++] = (intnum / Div) + 48;
 
    intnum = intnum % Div;
    Div /= 10;
    if ((str[j-1] == '0') & (Status == 0))
    {
      j = 0;
    }
    else
    {
      Status++;
    }
  }
}
uint32_t IAP_JumpTo(uint32_t ApplicationAddress)
{
    
//    if (((*(uint32_t*)ApplicationAddress) & 0x20000000 ) == 0x20000000)
//    { 
       memcpy((void *)SRAM_BASE, (void *)APP_CONFIG_APPLICATION_ADDRESS, APP_CONFIG_APPLICATION_SIZE);//sizeÌ«´óµ¼Ö¸²¸Çû·¨Ìø×ª
                __set_MSP(*(uint32_t *)SRAM_BASE);
                    typedef void (*p_entry_t)(void);
                ((p_entry_t) (*(uint32_t *)(SRAM_BASE + 4) + SRAM_BASE))();
//        __set_MSP(*(uint32_t*) ApplicationAddress);         //Set MSP MK8000ÐÞ¸Ä
//        
//        (*((void(*)(void))JumpAddress))();
        
//        return 1;
//    }    
 
    return 0;
}
 
uint8_t FileName[FILE_NAME_LENGTH];
    
/**
  * @brief  Download a file via serial port
  * @param  None
  * @retval None
  */
unsigned char SerialDownload(void)
{
  uint8_t Number[10] = {0};
  int32_t Size = 0;
    Send_Byte(0x33);
  Size = Ymodem_Receive(&tab_1024[0]);//¿ªÊ¼½ÓÊÕÎļþ,³É¹¦»á·µ»ØÎļþµÄ´óС
  if (Size > 0)  //·µ»Ø³É¹¦
  {
        SerialPutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
    SerialPutString(FileName);
    Int2Str(Number, Size);
    SerialPutString("\n\r Size: ");
    SerialPutString(Number);
    SerialPutString(" Bytes\r\n");
    SerialPutString("-------------------\n");
        return 0;
  }
  else if (Size == -1)
  {
    //SerialPutString("\n\n\rThe image size is higher than the allowed space memory!\n\r");
        return 1;
    }
  else if (Size == -2)
  {
    //SerialPutString("\n\n\rVerification failed!\n\r");
        return 2;
    }
  else if (Size == -3)
  {
    //SerialPutString("\r\n\nAborted by user.\n\r");
        return 3;
  }
  else
  {
   // SerialPutString("\n\rFailed to receive the file!\n\r");
        return 4;
    }
}
 
/**
  * @brief  Upload a file via serial port.
  * @param  None
  * @retval None
  */
 
uint8_t GetKey(void)
{
  uint8_t key = 0;
 
  /* Waiting for user input */
  while (1)
  {
    if (SerialKeyPressed((uint8_t*)&key)) break;
  }
  return key;
 
}
void SerialUpload(void)
{
  uint8_t status = 0 ; 
 
  SerialPutString("\n\n\rSelect Receive File\n\r");
 
  if (GetKey() == CRC16)
  {
    /* Transmit the flash image through ymodem protocol */
    status = Ymodem_Transmit((uint8_t*)APP_CONFIG_APPLICATION_ADDRESS, (const uint8_t*)"UploadedFlashImage.bin", APP_CONFIG_APPLICATION_SIZE);
 
    if (status != 0) 
    {
      SerialPutString("\n\rError Occurred while Transmitting File\n\r");
    }
    else
    {
      SerialPutString("\n\rFile uploaded successfully \n\r");
    }
  }
}
 
/**
  * @brief  Display the Main Menu on HyperTerminal
  * @param  None
  * @retval None
  */
 
void delay_ms(uint32_t nTimer)  
{  
    uint32_t i=1000*nTimer;  
    delay_us(i);  
void Main_Menu(void)
{
  uint8_t key = 0;
 
  
  while (1)
  {
 
    Serial0PutString("Çë´«ÊäÉý¼¶Îļþ\r\n\n");
      /* Download user application in the Flash */
            if(SerialDownload()==0)//Èç¹û´«ÊäÍê³ÉÌø»ØAPP
            {
                //±êÖ¾Çå³ý
        uint16_t tmp = 0xFFFF;
                flash_erase(FLASH_ID0, APP_CONFIG_IAPFLAG_SECTOR_ADDR, FLASH_SECTOR_SIZE);//²Á³ýAPPFLAG
                flash_write_nbytes(FLASH_ID0,APP_CONFIG_IAPFLAG_MAP,(uint8_t*)&tmp,2);
//            SPIFlash_Erase(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP,2, SPI_FLASH_SECTOR_ERASE_MODE_4K);  MK8000ÐÞ¸Ä
//            SPIFlash_Write(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP, (HIDO_UINT8*)&tmp, sizeof(tmp)); 
    
//              if (((*(__IO uint32_t*)APP_CONFIG_APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)ÏÈ×¢Ê͵ô¼´Ò»³É¹¦¾ÍÌø×ª
//                { 
                    /* Jump to user application */
                    Serial0PutString("Ìø×ªAPP\r\n");
                    memcpy((void *)SRAM_BASE, (void *)APP_CONFIG_APPLICATION_ADDRESS, APP_CONFIG_APPLICATION_SIZE);
                 // setup new msp
                    __set_MSP(*(uint32_t *)SRAM_BASE);
                    Serial0PutString("Ìø×ªAPPºó\r\n");
                    typedef void (*p_entry_t)(void);
                    ((p_entry_t) (*(uint32_t *)(SRAM_BASE + 4) + SRAM_BASE))();
                    
                //JumpAddress = *(__IO uint32_t*) (SRAM_BASE + 4);
                
//                    Jump_To_Application = (pFunction) JumpAddress;
//                    
//                    /* Initialize user application's Stack Pointer */
//                    __set_MSP(*(__IO uint32_t*) SRAM_BASE);
//                    
//                    /* Jump to application */
//                    Jump_To_Application();
                }else{        
//                    uint16_t tmp = 0xFFFF;
//                flash_erase(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, 2);//²Á³ýAPPFLAG    ´Ë´¦Ê§°Ü¾ÍÖØÆôÖØÐ½øÈëbootÀïÃæ´«ÊäÇÒ²»¸Ä±äÒªÉý¼¶µÄ±ê־λ
//                flash_write(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, (uint8_t*)&tmp, sizeof(tmp));
                    //SPIFlash_Erase(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP,2, SPI_FLASH_SECTOR_ERASE_MODE_4K);  ¸ÄдΪMK8000API
                    //SPIFlash_Write(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP, (HIDO_UINT8*)&tmp, sizeof(tmp)); 
                    delay_ms(10);
                                        LOG_INFO(TRACE_MODULE_APP,"Éý¼¶Ê§°ÜÖØÆô\r\n\n");
                                        NVIC_SystemReset();
                            //SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //Èí¸´Î»»Øµ½bootloader  MK8000Ð޸Ġ
                }
                
                return;
            }
//            else
//            {
//                NVIC_SystemReset();
//                return;
//            }
    
//}
}