179 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			179 lines
		
	
	
		
			5.8 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.
 | 
						|
 | 
						|
****************************************************************************/
 | 
						|
#include "os_types.h"
 | 
						|
#include "os_mem.h"
 | 
						|
 | 
						|
#include "iot_io_api.h"
 | 
						|
#include "iot_cli.h"
 | 
						|
#include "cli_ic_tool_def.h"
 | 
						|
#include "cli_ic_tool.h"
 | 
						|
#include "iot_ftm_msg.h"
 | 
						|
#include "iot_ftm_internal.h"
 | 
						|
#include "iot_cli_msg_id_definition.h"
 | 
						|
#include "iot_config.h"
 | 
						|
 | 
						|
#if IOT_CLI_IC_TOOL_EN
 | 
						|
 | 
						|
#define CLI_DATA_OPERATION_MAX_SIZE 256
 | 
						|
 | 
						|
void cli_common_memory_read_data(iot_pkt_t *buffer, uint32_t length)
 | 
						|
{
 | 
						|
    cli_cmd_read_data_t *cmd = (cli_cmd_read_data_t *)iot_pkt_data(buffer);
 | 
						|
    (void)length;
 | 
						|
 | 
						|
    if (cmd->length > CLI_DATA_OPERATION_MAX_SIZE) {
 | 
						|
        cmd->length = CLI_DATA_OPERATION_MAX_SIZE;
 | 
						|
    }
 | 
						|
 | 
						|
    if (cmd->operation_type == CLI_DATA_OPERATION_TYPE_REG) {
 | 
						|
        cli_reg_info reg_info;
 | 
						|
        reg_info.addr = cmd->addr;
 | 
						|
        reg_info.operation_type = cmd->operation_type;
 | 
						|
 | 
						|
        if (cmd->addr & 0x3) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
        reg_info.value = *(volatile uint32_t *)cmd->addr;
 | 
						|
 | 
						|
        cli_send_module_msg(CLI_MODULEID_MANUFACTURE_OP, CLI_MSGID_DATA_INFO,
 | 
						|
            (uint8_t *)®_info, sizeof(cli_reg_info));
 | 
						|
    } else if (cmd->operation_type == CLI_DATA_OPERATION_TYPE_RAM) {
 | 
						|
        cli_data_info *data_info =
 | 
						|
            os_mem_malloc(IOT_CLI_MID, sizeof(cli_data_info) + cmd->length);
 | 
						|
 | 
						|
        if (data_info == NULL) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        data_info->addr = cmd->addr;
 | 
						|
        data_info->len = cmd->length;
 | 
						|
        data_info->operation_type = cmd->operation_type;
 | 
						|
        os_mem_cpy(&data_info->data, (void *)cmd->addr, cmd->length);
 | 
						|
 | 
						|
        cli_send_module_msg(CLI_MODULEID_MANUFACTURE_OP, CLI_MSGID_DATA_INFO,
 | 
						|
            (uint8_t *)data_info, sizeof(cli_reg_info) + cmd->length);
 | 
						|
        os_mem_free(data_info);
 | 
						|
    } else if (cmd->operation_type == CLI_DATA_OPERATION_TYPE_REG_GROUP) {
 | 
						|
        if (cmd->length == 0 || cmd->length % 4 != 0 || cmd->addr % 4 != 0) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        cli_data_info *data_info =
 | 
						|
            os_mem_malloc(IOT_CLI_MID, sizeof(cli_data_info) + cmd->length);
 | 
						|
 | 
						|
        if (data_info == NULL) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        data_info->addr = cmd->addr;
 | 
						|
        data_info->len = cmd->length;
 | 
						|
        data_info->operation_type = cmd->operation_type;
 | 
						|
 | 
						|
        for (uint16_t i = 0; i < data_info->len; i += 4) {
 | 
						|
            uint32_t v = *(volatile uint32_t *)(data_info->addr + i);
 | 
						|
            data_info->data[i + 0] = v & 0xFF;
 | 
						|
            data_info->data[i + 1] = (v >> 8) & 0xFF;
 | 
						|
            data_info->data[i + 2] = (v >> 16) & 0xFF;
 | 
						|
            data_info->data[i + 3] = (v >> 24) & 0xFF;
 | 
						|
        }
 | 
						|
 | 
						|
        os_mem_cpy(&data_info->data, (void *)cmd->addr, cmd->length);
 | 
						|
 | 
						|
        cli_send_module_msg(CLI_MODULEID_MANUFACTURE_OP, CLI_MSGID_DATA_INFO,
 | 
						|
            (uint8_t *)data_info, sizeof(cli_reg_info) + cmd->length);
 | 
						|
 | 
						|
        os_mem_free(data_info);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void cli_common_memory_write_data(iot_pkt_t *buffer, uint32_t bufferlen)
 | 
						|
{
 | 
						|
    cli_data_info *data_info = NULL;
 | 
						|
    cli_set_data_ack ack = {0};
 | 
						|
 | 
						|
    if ((!buffer) || (bufferlen < sizeof(cli_reg_info))) {
 | 
						|
        iot_printf("cli_common_memory_write_data buffer:0x%08x bufferlen:%d\n",
 | 
						|
        buffer,bufferlen);
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    data_info = (cli_data_info *)iot_pkt_data(buffer);
 | 
						|
    iot_printf("cli set data 0x%08x %x\n", data_info->addr,
 | 
						|
                   data_info->operation_type);
 | 
						|
 | 
						|
    if (data_info->operation_type == CLI_DATA_OPERATION_TYPE_RAM) {
 | 
						|
        ack.addr = data_info->addr;
 | 
						|
        ack.len = data_info->len;
 | 
						|
 | 
						|
        if (bufferlen < sizeof(cli_data_info)) {
 | 
						|
            ack.result = 0;
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        os_mem_cpy((void *)data_info->addr, data_info->data, data_info->len);
 | 
						|
        ack.result = 0;
 | 
						|
    } else if (data_info->operation_type == CLI_DATA_OPERATION_TYPE_REG) {
 | 
						|
        cli_reg_info *reg = (cli_reg_info *)data_info;
 | 
						|
        ack.addr = reg->addr;
 | 
						|
        ack.len = 4;
 | 
						|
 | 
						|
        if (reg->addr % 4 != 0) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        *(volatile uint32_t *)reg->addr = reg->value;
 | 
						|
        ack.result = 0;
 | 
						|
    } else if (data_info->operation_type == CLI_DATA_OPERATION_TYPE_REG_GROUP) {
 | 
						|
        ack.addr = data_info->addr;
 | 
						|
        ack.len = 4;
 | 
						|
 | 
						|
        if (data_info->addr % 4 != 0 || data_info->len == 0
 | 
						|
            || data_info->len % 4 != 0) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        for (uint16_t i = 0; i < data_info->len; i += 4) {
 | 
						|
            *(volatile uint32_t *)(data_info->addr + i) =
 | 
						|
                data_info->data[i + 3] << 24 | data_info->data[i + 2] << 16
 | 
						|
                | data_info->data[i + 1] << 8 | data_info->data[i];
 | 
						|
        }
 | 
						|
 | 
						|
        ack.result = 0;
 | 
						|
    }
 | 
						|
 | 
						|
    cli_send_module_msg(CLI_MODULEID_MANUFACTURE_OP, CLI_MSGID_DATA_ACK,
 | 
						|
        (uint8_t *)&ack, sizeof(ack));
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
void iot_ic_tool_op(int msgid, iot_pkt_t* buffer, uint32_t len)
 | 
						|
{
 | 
						|
    iot_printf("common msgid:%d len:%d\n", msgid, len);
 | 
						|
 | 
						|
    switch (msgid)
 | 
						|
    {
 | 
						|
        case CLI_MSGID_READ_DATA:
 | 
						|
            cli_common_memory_read_data(buffer, len);
 | 
						|
            break;
 | 
						|
        case CLI_MSGID_SET_DATA:
 | 
						|
            cli_common_memory_write_data(buffer, len);
 | 
						|
            break;
 | 
						|
        default:
 | 
						|
            break;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
#endif  /* IOT_CLI_IC_TOOL_EN */
 |