chen
2025-02-28 10a92afeae49a4ff85143bd17161c08256f30550
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (c) 2019-2023 Mauna Kea Semiconductor Holdings.
 * All rights reserved.
 *
 */
 
#include "mk_flash.h"
#include "mk_trace.h"
#include "mk_calib.h"
#include "mk_wdt.h"
#include "mk_clock.h"
#include "libc_rom.h"
#include "board.h"
#include "AppConfig.h"
#include "menu.h"
#include "aes.h"
static const uint32_t fw_addr = 0x04004000;
static const uint32_t fw_bytes = 14712;
 
extern const VECTOR_TABLE_Type __VECTOR_TABLE[48];
#define TEST_UART_POLL_MODE 0
#define TEST_UART_INTERUPT_MODE 1
#define TEST_UART_DMA_MODE 2
#define TEST_UART_MODE TEST_UART_DMA_MODE
 
volatile unsigned long time32_incr;
volatile unsigned long time32_reset = 0;
static unsigned char buff[100];
uint8_t key[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
 
extern int32_t Receive_Byte (uint8_t *c, uint32_t timeout);
void usartdata_process( uint8_t data)
{
    static uint8_t state=0;
    switch(state)
    {
        case 0:
            if(data==0x55)
            {
                state = 1;
            }
            break;
        case 1:
            if(data==0xAA)
            {
                state = 2;
            }else{
                state = 0;
            }
            break;
            case 2:
            if(data==0x03)
            {
                state = 3;
            }else{
                state = 0;
            }
            break;
            case 3:
            if(data==0x06)
            {
                state = 4;
            }else{
                state = 0;
            }
            break;
            case 4:
            if(data==0x02)
            {
                state = 5;
            }else{
                state = 0;
            }
            break;
            case 5:
            if(data==0x64)
            {
                state = 6;
            }else{
                state = 0;
            }
            break;
            case 6:
            if(data==0x01)
            {
                state = 7;
            }else{
                state = 0;
            }
            break;
            case 7:
            if(data==0x01)
            {
                state = 8;
            }else{
                state = 0;
            }
            break;
            case 8:
            if(data==0x8E)
            {
                state = 9;
            }else{
                state = 0;
            }
            break;
            case 9:
            if(data==0xFF)
            {
                uint16_t tmp = 0xAAAA,result;               
//                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)); 
                state = 0;
                
            }
            break;
    }
 
}
unsigned short int updata_byte = 0;
uint8_t trx_buf[10] = {0};
static void app_wdt_callback(void *dev, uint32_t status)
{
    ASSERT(status, "WDT TIMEOUT£¬³ÌÐò¸´Î»");
        //LOG_INFO(TRACE_MODULE_APP, "³ÌÐò¿¨ËÀ£¬¿´ÃŹ·¸´Î»");
}
 struct WDT_CFG_T app_wdt_cfg = {
        .timeout = 32768 * 30,
        .rst_en = true,
        .int_en = true,
        .callback = app_wdt_callback,
    };
int main(void)
{        uint8_t c;
        
      //sys_tick_callback_set(systic_time_callback);
        board_clock_run();
    board_pins_config();
        //board_debug_console_open(TRACE_PORT_UART0);
        // Chip calibration
    calib_chip();
        // Disable watchdog timer
    wdt_close(WDT_ID0);
        wdt_open(WDT_ID0,&app_wdt_cfg);//30s¼ì²âι¹·
        //LOG_INFO(TRACE_MODULE_APP, "½øÈëBootloader´®¿ÚÉý¼¶\r\n");
        gpio_open();
    board_configure();
    
     struct UART_CFG_T test_uart_cfg =
    {
        .parity = UART_PARITY_NONE,
        .stop = UART_STOP_BITS_1,
        .data = UART_DATA_BITS_8,
        .flow = UART_FLOW_CONTROL_NONE,
        .rx_level = UART_RXFIFO_CHAR_1,
        .tx_level = UART_TXFIFO_EMPTY,
        .baud = BAUD_115200,
#if (TEST_UART_MODE == TEST_UART_POLL_MODE)
        .dma_en = false,
        .int_rx = false,
        .int_tx = false,
#elif (TEST_UART_MODE == TEST_UART_INTERUPT_MODE)
        .dma_en = false,
        .int_rx = true,
        .int_tx = true,
#elif (TEST_UART_MODE == TEST_UART_DMA_MODE)
        .dma_en = true,
        .int_rx = false,
        .int_tx = false,
#endif
        };
         flash_open(FLASH_ID0, NULL);
         uart_open(UART_ID1, &test_uart_cfg);
            //uart_open(UART_ID0, &test_uart_cfg);
         //uart_receive(UART_ID1, trx_buf, 1, uart_receive_callback);
         SerialPutString("MK8000-BootLoader-V1.1Æô¶¯\r\n");
         //Serial0PutString("MK8000-BootLoader-V1.0Æô¶¯\r\n");
        while(1)
        {
            wdt_ping(WDT_ID0);//ι¹·
            uint16_t tmp = 0;
            tmp=0xAAAA;//²âÊÔÓÃ
            //flash_erase(FLASH_ID0, APP_CONFIG_IAPFLAG_SECTOR_ADDR, FLASH_SECTOR_SIZE);
            //flash_write_nbytes(FLASH_ID0,APP_CONFIG_IAPFLAG_MAP,(uint8_t*)&tmp,2);//²âÊÔÓÃ
//    SPIFlash_Read(SPI_FLASH_ID_0, (HIDO_UINT8*)&tmp, APP_CONFIG_IAPFLAG_MAP, sizeof(tmp)); MK8000ÐÞ¸Ä
            
            flash_read(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, (uint8_t*)&tmp, 2);
        while (flash_check_busy(FLASH_ID0))
        {
        }
    updata_byte = tmp;//¶Áȡָ¶¨µØÖ·µÄÖµ
    if(updata_byte == 0xAAAA)//Åжϴ˵ØÖ·µÄÖµÊÇ·ñΪ0xAAAA,Èç¹ûÊÇÔòÉý¼¶ 
    {
        SerialPutString("´®¿ÚÉý¼¶Æô¶¯\r\n");
            
        Main_Menu();
    }
    else if(updata_byte == 0xBBBB)//Åжϴ˵ØÖ·µÄÖµÊÇ·ñΪ0xBBBB,Èç¹ûÊÇÔòÍøÂçÉý¼¶
    {       
        unsigned int i = 0;
        unsigned int spiflash_addr = APP_CONFIG_APP_DOWN_LOAD_SPIFLASH_ADDRESS;
        unsigned int flashdestination = APP_CONFIG_APPLICATION_ADDRESS;
        uint32_t JumpAddress;
        pFunction Jump_To_Application;
        
        SerialPutString("ÍøÂçÉý¼¶Æô¶¯Æô¶¯\r\n");
//        Flash_Init();
//        Flash_Erase(APP_CONFIG_APPLICATION_ADDRESS, APP_CONFIG_APPLICATION_SIZE);//MK8000ÐÞ¸Ä
                flash_erase(FLASH_ID0, APP_CONFIG_APPLICATION_ADDRESS, APP_CONFIG_APPLICATION_SIZE);//²Á³ýAPP
        aesDecInit();
         /* 200KB */
        while(spiflash_addr < (APP_CONFIG_APP_DOWN_LOAD_SPIFLASH_ADDRESS + APP_CONFIG_APP_DOWN_LOAD_SPIFLASH_SIZE))
        {
            /* ¶Áȡһ°üÊý¾Ý */
            //SPIFlash_Read(SPI_FLASH_ID_0, buff, spiflash_addr, sizeof(buff));MK8000ÐÞ¸Ä
                        flash_read(FLASH_ID0, spiflash_addr, buff, sizeof(buff));
                        while (flash_check_busy(FLASH_ID0))
                        {
                        }
            spiflash_addr += sizeof(buff);
            
            for(i = 0; i < sizeof(buff); i += 16)
            {
                aesDecrypt(&buff[i],key);
            }
            
                        flash_write(FLASH_ID0, flashdestination, buff, sizeof(buff));
                        while (flash_check_busy(FLASH_ID0))
                        {
                        }
                        flashdestination += sizeof(buff);
//            Flash_Write(flashdestination, buff, sizeof(buff));
//            flashdestination += sizeof(buff); //MK8000ÐÞ¸Ä
        }
        
        /* Çå±êÖ¾ */
        tmp = 0xFFFF;
                flash_erase(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, 2);//²Á³ýAPPFLAG
                flash_write(FLASH_ID0, APP_CONFIG_IAPFLAG_MAP, (uint8_t*)&tmp, sizeof(tmp));
                delay_ms(500);
                NVIC_SystemReset();
//        SPIFlash_Erase(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP,2, SPI_FLASH_SECTOR_ERASE_MODE_4K);  
//        SPIFlash_Write(SPI_FLASH_ID_0, APP_CONFIG_IAPFLAG_MAP, (HIDO_UINT8*)&tmp, sizeof(tmp)); //MK8000ÐÞ¸Ä
    
        //HAL_Delay(500);
        //SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; MK8000 ÐÞ¸Ä
                
                 }
    else if(updata_byte == 0xCCCC)//OTA
    {
        SerialPutString("OTAÉý¼¶Æô¶¯\r\n");
        IAP_JumpTo(SRAM_BASE);
    }
    else  //²»ÊÇÔòÌø×ªµ½APP,×¢ÒâF0ºÍÆäËûϵÁеÄÌø×ªÓÐЩ²»Ò»Ñù,Ìø×ª²¿·Ö¿É²Î¿¼Ô­×Ó¸ç´úÂë
    {    
        //Delay_ms(1000);
        /* Test if user code is programmed starting from address "APP_CONFIG_APPLICATION_ADDRESS" */
         //Serial0PutString("Ìø×ªAPPhehe\r\n");
                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))();
        //IAP_JumpTo(SRAM_BASE);
    }
  /* USER CODE BEGIN 3 */
 
  }
  /* USER CODE END 3 */
 
    ////////////////////////////////////////////////////////////////////
    // do something as needed such as comms with pc                   //
    ////////////////////////////////////////////////////////////////////
    
    // copy application to it's link address
//    {
//        flash_open(FLASH_ID0, NULL);
//        
//        memcpy((void *)SRAM_BASE, (void *)fw_addr, fw_bytes);
//        
//        flash_close(FLASH_ID0);
//    }
//    
////    ////////////////////////////////////////////////////////////////////
////    // do something like restoring the status of registers if needed  //
////    ////////////////////////////////////////////////////////////////////
//    
//    __set_MSP(*(uint32_t *)SRAM_BASE);
//    
//    typedef void (*p_entry_t)(void);
//    ((p_entry_t) (*(uint32_t *)(SRAM_BASE + 4) + SRAM_BASE))();
}