200 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			200 lines
		
	
	
		
			4.5 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_clock.h"
							 | 
						||
| 
								 | 
							
								#include "dbg_io.h"
							 | 
						||
| 
								 | 
							
								#include "strformat.h"
							 | 
						||
| 
								 | 
							
								#include "ahb.h"
							 | 
						||
| 
								 | 
							
								#include "uart.h"
							 | 
						||
| 
								 | 
							
								#include "iot_uart.h"
							 | 
						||
| 
								 | 
							
								#include "mtd.h"
							 | 
						||
| 
								 | 
							
								#include "iot_mtd.h"
							 | 
						||
| 
								 | 
							
								#include "iot_led.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								extern int platform_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void iot_task_1(void *arg)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    iot_printf("task 1 entry....\r\n");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    int ret = -1;
							 | 
						||
| 
								 | 
							
								    uint32_t pos = 0;
							 | 
						||
| 
								 | 
							
								    uint32_t rbuf[0x100] = {0};
							 | 
						||
| 
								 | 
							
								    int wsize = 0x0400;
							 | 
						||
| 
								 | 
							
								    int rsize = 0x100;
							 | 
						||
| 
								 | 
							
								    int unit = 0x100;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    uint32_t *wbuf = os_mem_malloc(IOT_DRIVER_MID, wsize);
							 | 
						||
| 
								 | 
							
								    int i = 0, j=0;
							 | 
						||
| 
								 | 
							
								    int fd = dev_open(PART_NUM_FW1, 0);
							 | 
						||
| 
								 | 
							
								    if (fd<0) {
							 | 
						||
| 
								 | 
							
								        iot_printf("open file failed.\n");
							 | 
						||
| 
								 | 
							
								        return ;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /* test get info */
							 | 
						||
| 
								 | 
							
								    mtd_device_get_info(rbuf, DEV_TYPE_FLASH);
							 | 
						||
| 
								 | 
							
								    iot_printf("\r\n ID : %02x\r\n", (uint32_t )rbuf[0]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /* test erase */
							 | 
						||
| 
								 | 
							
								    uint32_t mode = 0x00U;
							 | 
						||
| 
								 | 
							
								    mode = DEV_ERASE_TYPE_FULL_PART;
							 | 
						||
| 
								 | 
							
								    ret = dev_erase(fd, 0x0, mode);
							 | 
						||
| 
								 | 
							
								    if (ret < 0) {
							 | 
						||
| 
								 | 
							
								        iot_printf("\r\nerase failed.\r\n");
							 | 
						||
| 
								 | 
							
								        return ;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /* test write: size 0x50000 */
							 | 
						||
| 
								 | 
							
								    uint32_t tmp = 0;
							 | 
						||
| 
								 | 
							
								    for (i=0;i<0x010;i++) {
							 | 
						||
| 
								 | 
							
								        for (j=0;j<unit; j++) {
							 | 
						||
| 
								 | 
							
								            wbuf[j] = tmp + j;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        tmp = wbuf[j-1]+1;
							 | 
						||
| 
								 | 
							
								        ret = dev_write(fd, wbuf, wsize);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    if (ret < 0) {
							 | 
						||
| 
								 | 
							
								        pos = dev_seek(fd, 0, DEV_SEEK_CUR);
							 | 
						||
| 
								 | 
							
								        iot_printf("\r\nwrite failed.%08x\r\n", pos);
							 | 
						||
| 
								 | 
							
								        return ;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    os_mem_free(wbuf);
							 | 
						||
| 
								 | 
							
								       ret = dev_read(fd, rbuf, rsize);
							 | 
						||
| 
								 | 
							
								    pos = dev_seek(fd, 0, DEV_SEEK_SET);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    for(;;) {
							 | 
						||
| 
								 | 
							
								        if (pos >= 0x010000 ) {
							 | 
						||
| 
								 | 
							
								            iot_printf("read at the end, back to head.\r\n");
							 | 
						||
| 
								 | 
							
								            pos = dev_seek(fd, 0, DEV_SEEK_SET);
							 | 
						||
| 
								 | 
							
								            //break;
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            pos = dev_seek(fd, 0, DEV_SEEK_CUR);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        iot_printf("\r\nread data ++ start pos is %02x\r\n", pos);
							 | 
						||
| 
								 | 
							
								        ret = dev_read(fd, rbuf, rsize);
							 | 
						||
| 
								 | 
							
								        if (ret<0) {
							 | 
						||
| 
								 | 
							
								            iot_printf("read error\r\n");
							 | 
						||
| 
								 | 
							
								            dev_close(fd);
							 | 
						||
| 
								 | 
							
								            return ;
							 | 
						||
| 
								 | 
							
								            /* move to DEV_SEEK_SET */
							 | 
						||
| 
								 | 
							
								            dev_seek(fd, 0, DEV_SEEK_SET);
							 | 
						||
| 
								 | 
							
								            continue;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        for(i=0;i<rsize/sizeof(uint32_t);i++) {
							 | 
						||
| 
								 | 
							
								            iot_printf("%08x ", rbuf[i]);
							 | 
						||
| 
								 | 
							
								            if (i%4 == 3){
							 | 
						||
| 
								 | 
							
								                 iot_printf("\r\n");
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        pos = dev_seek(fd, 0, DEV_SEEK_CUR);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        iot_printf("read data ++ end pos is %02x\r\n", pos);
							 | 
						||
| 
								 | 
							
								        os_delay(300);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    dev_close(fd);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								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");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    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();
							 | 
						||
| 
								 | 
							
								    dbg_uart_stage1_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    iot_led_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return 0;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int32_t iot_module_init(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    //platform intialization;
							 | 
						||
| 
								 | 
							
								    iot_platform_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // emc enable
							 | 
						||
| 
								 | 
							
								    //ahb_emc_enable();
							 | 
						||
| 
								 | 
							
								    // reset emc cache
							 | 
						||
| 
								 | 
							
								    //ahb_emc_reset();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // 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;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int main(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								   //module init;
							 | 
						||
| 
								 | 
							
								   iot_module_init();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   //module start;
							 | 
						||
| 
								 | 
							
								   iot_module_start();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   return 0;
							 | 
						||
| 
								 | 
							
								}
							 |