249 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			249 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | /* os shim includes */ | ||
|  | #include "os_types.h"
 | ||
|  | #include "os_task.h"
 | ||
|  | #include "os_lock.h"
 | ||
|  | #include "os_mem.h"
 | ||
|  | #include "os_utils.h"
 | ||
|  | 
 | ||
|  | /* common includes */ | ||
|  | #include "iot_io.h"
 | ||
|  | #include "iot_utils.h"
 | ||
|  | #include "iot_clock.h"
 | ||
|  | #include "iot_pkt.h"
 | ||
|  | #include "dbg_io.h"
 | ||
|  | #include "strformat.h"
 | ||
|  | #include "uart.h"
 | ||
|  | #include "iot_uart.h"
 | ||
|  | #include "mtd.h"
 | ||
|  | #include "iot_mtd.h"
 | ||
|  | #include "upgrade.h"
 | ||
|  | #if 0
 | ||
|  | extern int platform_init(); | ||
|  | 
 | ||
|  | static const iot_pkt_config_t test_pkt_config = | ||
|  | { | ||
|  |     { | ||
|  |         { | ||
|  |             256, | ||
|  |             50, | ||
|  |             PKT_OWNER_ALL, | ||
|  |         }, | ||
|  |         { | ||
|  |             600, | ||
|  |             50, | ||
|  |             PKT_OWNER_ALL, | ||
|  |             }, | ||
|  |         { | ||
|  |             1100, | ||
|  |             15, | ||
|  |             PKT_OWNER_ALL, | ||
|  |         }, | ||
|  |         { | ||
|  |             2200, | ||
|  |             6, | ||
|  |             PKT_OWNER_ALL, | ||
|  |         }, | ||
|  |         { | ||
|  |             0, | ||
|  |             0, | ||
|  |             PKT_OWNER_NONE, | ||
|  |         }, | ||
|  |         { | ||
|  |             0, | ||
|  |             0, | ||
|  |             PKT_OWNER_NONE, | ||
|  |         }, | ||
|  |         { | ||
|  |             0, | ||
|  |             0, | ||
|  |             PKT_OWNER_NONE, | ||
|  |         }, | ||
|  |         { | ||
|  |             0, | ||
|  |             0, | ||
|  |             PKT_OWNER_NONE, | ||
|  |         }, | ||
|  |     } | ||
|  | }; | ||
|  | 
 | ||
|  | void iot_task_1(void *arg) | ||
|  | { | ||
|  | 
 | ||
|  |     int i = 0; | ||
|  |     int total_size = 0x20; | ||
|  |     uint32_t ret = 0; | ||
|  |     // start to upgrade
 | ||
|  |     iot_start_src_t start_src = { | ||
|  |         .id = 0xc8, | ||
|  |         .block_size = 100, | ||
|  |         .fw_size = 0x60000, | ||
|  |         .crc = 0xabcdef5a | ||
|  |     }; | ||
|  |     iot_start_rst_t start_rst = {0}; | ||
|  | 
 | ||
|  |     iot_printf("task 1 entry....\r\n"); | ||
|  |     ret = iot_upgrade_start(&start_rst, &start_src); | ||
|  |     if (ret) { | ||
|  |         iot_printf("upgrade start error\n"); | ||
|  |     } | ||
|  |     // trans command
 | ||
|  |     while(1) { | ||
|  |         uint8_t *pkt_data; | ||
|  |         uint8_t data[100] = {0,1,2,3,4,5,6,7,8,9}; | ||
|  |         iot_trans_cmd_t trans = {0}; | ||
|  |         iot_printf("\r\niot_task_1\n"); | ||
|  |         iot_pkt_t *pkt = iot_pkt_alloc(100, IOT_DRIVER_MID); | ||
|  |         data[9] = i; | ||
|  |         pkt_data = iot_pkt_block_ptr(pkt, IOT_PKT_BLOCK_DATA); | ||
|  |         os_mem_cpy(pkt_data, &data, 100); | ||
|  |         iot_pkt_put(pkt, 100); | ||
|  | 
 | ||
|  |         trans.id = start_src.id; | ||
|  |         trans.block_size = start_src.block_size; | ||
|  |         trans.block_num = i; | ||
|  |         trans.data = pkt; | ||
|  |         while(1) { | ||
|  |             ret = iot_upgrade_trans(&trans); | ||
|  |             if (ret) { | ||
|  |                 iot_printf("retry to send %d.\n", trans.block_num); | ||
|  |                 os_delay(700); | ||
|  |             } else { | ||
|  |                 break; | ||
|  |             } | ||
|  |         } | ||
|  |         os_delay(500); | ||
|  |         i++; | ||
|  |         if (i>=total_size) { | ||
|  |             break; | ||
|  |         } | ||
|  |         //iot_pkt_free(pkt);
 | ||
|  |     } | ||
|  | 
 | ||
|  |     iot_printf("upgrade trans end.\n"); | ||
|  | 
 | ||
|  |     os_delay(1000); | ||
|  |     while(1) { | ||
|  |         ret = iot_upgrade_commit(start_src.id); | ||
|  |         if (!ret) { | ||
|  |             iot_printf("upgrade trans end.\n"); | ||
|  |             break; | ||
|  |         } | ||
|  |         os_delay(2000); | ||
|  |     } | ||
|  |     ret = iot_upgrade_start(&start_rst, &start_src); | ||
|  |     if (ret) { | ||
|  |         iot_printf("upgrade start error\n"); | ||
|  |     } | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | void iot_task_2(void *arg) | ||
|  | { | ||
|  |     while(1) { | ||
|  |         iot_printf("\r\niot_task_2\n"); | ||
|  |         idle_upgrade_handle_queue(); | ||
|  |         os_delay(700); | ||
|  |     } | ||
|  | } | ||
|  | int32_t iot_task_init() | ||
|  | { | ||
|  |     os_task_h handle; | ||
|  | 
 | ||
|  |     /* start plc lib task */ | ||
|  |     handle = os_create_task(iot_task_1, NULL, 6); | ||
|  | 
 | ||
|  |     //create the tasks;
 | ||
|  |     if(handle != NULL) { | ||
|  |         iot_printf("task 1 init successfully...\r\n"); | ||
|  |     } | ||
|  | 
 | ||
|  |     /* start plc lib task */ | ||
|  |     handle = os_create_task(iot_task_2, NULL, 6); | ||
|  | 
 | ||
|  |     //create the tasks;
 | ||
|  |     if(handle != NULL) { | ||
|  |         iot_printf("task 1 init successfully...\r\n"); | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | int32_t iot_task_start() | ||
|  | { | ||
|  |     //start the tasks;
 | ||
|  |     os_start_kernel(); | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static int32_t iot_platform_init() | ||
|  | { | ||
|  |     /*platform intialization*/ | ||
|  |     platform_init(); | ||
|  | 
 | ||
|  |     //resource initializations;
 | ||
|  |     system_clock_init(); | ||
|  |     system_uart_init(); | ||
|  |     dbg_uart_init(); | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | int32_t iot_module_init(void) | ||
|  | { | ||
|  |     //platform intialization;
 | ||
|  |     iot_platform_init(); | ||
|  | 
 | ||
|  |     // mtd initializations
 | ||
|  |     mtd_device_init(1); | ||
|  | 
 | ||
|  |     //create all the tasks;
 | ||
|  |     iot_task_init(); | ||
|  | 
 | ||
|  |     iot_printf("\r\nstarting...\r\n"); | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | int32_t iot_module_start(void) | ||
|  | { | ||
|  |     int32_t res = 0; | ||
|  | 
 | ||
|  |     res = iot_task_start(); | ||
|  | 
 | ||
|  |     return res; | ||
|  | } | ||
|  | 
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | int main(void) | ||
|  | { | ||
|  | #if 0
 | ||
|  |     //module init;
 | ||
|  |     iot_module_init(); | ||
|  |     iot_pkt_init(&test_pkt_config); | ||
|  |     iot_printf("\r\nstarting...\r\n"); | ||
|  | 
 | ||
|  |     //module start;
 | ||
|  |     iot_module_start(); | ||
|  | #endif
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 |