⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfg_utility.c

📁 基于ecos的redboot
💻 C
📖 第 1 页 / 共 3 页
字号:
		slave_addr = SLAVE_ADDR_3;
		cfg_data_sz = sizeof(struct COMMON_CFG_CONTENT);
		break;
	case KI_DEV_ID:
		slave_addr = SLAVE_ADDR_1;
		cfg_data_sz = sizeof(struct KI_EEPROM_CONTENT);
		break;
    default:
        return ERROR;
	}
	
    //Calculate checksum
	r = 55665;
    chksum_addr = (PAGESZ * 4)+9;
    for(i = 0; i < chksum_addr; i++)
	{
		checksum += calculate_chksum(databuf[i]);
	}
	
    i += 4;
	for(; i < cfg_data_sz; i++)
	{
		checksum += calculate_chksum(databuf[i]);
	}

    data->chksum[0] = checksum;
	data->chksum[1] = checksum >> 8;
	data->chksum[2] = checksum >> 16;
	data->chksum[3] = checksum >> 24;
	 
	for(i = 0; i < cfg_data_sz; i += 16)
	{
        if(i2c_page_write(slave_addr, i, &databuf[i], 16))
        {
            printf("Encountered problem in writing EPROM\n");
            return ERROR;
        }
	}
    return OK;
}

/******************************************************************************
Function	:	calculate_chksum() caluculates the checksum 
Arguments	:   value - data whose checksum has to be calculated
Return		:   returns the checksum
******************************************************************************/
unsigned char calculate_chksum(unsigned char value)
{
	unsigned char cipher = (value ^ (r >> 8));
	r = (cipher + r) * c1 + c2;
	return cipher;	
}

/******************************************************************************
Function	: from_hex() converts a string to integer of 4 bytes
Arguments	: buf- Ptr to the str
			  size-string length
Return		: the 4 byte integer
******************************************************************************/
unsigned long from_hex(unsigned char *buf, int size)
{
    unsigned char h[256];
    unsigned long result;
    unsigned int c;

	for (c = 0; c < 256; c++)
		h[c] = 0;
	for (c = '0'; c <= '9'; c++)
		h[c] = c - '0';
	for (c = 'a'; c <= 'f'; c++)
		h[c] = c - 'a' + 10;
	for (c = 'A'; c <= 'F'; c++)
		h[c] = c - 'A' + 10;
	
	result = 0;
    while (size > 0) {
        result = (result * 16) + h[*buf++];
        size--;
    }
    return result;
}


/******************************************************************************
Function	: cmd_cfg_rd() will be invoked when the user chooses to read the 
			  cfg data from the BootMonitor prompt.This will check if the data 
			  is valid and displays the data if valid.
Arguments	: not used. 
Return		: none
*******************************************************************************/			  
void cmd_cfg_rd(int argc, char * argv[])
{
	char cfg_data[MAX_CFG_DATA_SZ];
	unsigned char datr, slave_addr;
	int id;
    struct option_info opts[1];
	bool id_set = false;
    
    init_opts(&opts[0], 'n', true, OPTION_ARG_TYPE_NUM,(void **)&id,
                            (bool *)&id_set, "Device ID");    

    if(!scan_opts(argc, argv, 2, opts, 1, (void **)0, OPTION_ARG_TYPE_NUM, "Device ID"))
    {
        cfg_usage("Invalid arguments");
        return;
    }
	if(!id_set)
	{
		printf("Device ID required\n");
		return;
	}

    switch(id)
    {
    case PL_DEV_ID:
    case BW_DEV_ID:
    case BD_DEV_ID:
        slave_addr = SLAVE_ADDR_2;
		break;
    case MI_DEV_ID:
        slave_addr = SLAVE_ADDR_4;
		break;
	case BI_DEV_ID:
        slave_addr = SLAVE_ADDR_3;
		break;
	case KI_DEV_ID:
        slave_addr = SLAVE_ADDR_1;
		break;
	default:
		printf("Invalid Device ID\n");
		return;
    }

    if(i2c_read(slave_addr, 0x0, &datr))
    {
        printf("Encountered problem in reading EPROM\n");
        return;
    }
	if(datr == '0')
	{
		read_config_data(cfg_data, id);
		display_config_data(cfg_data,id);
	}
	else
	{
		printf("EEPROM is either corrupted or not programmed!\n");	
	}
}

/******************************************************************************
Function	: cmd_cfg_wr() will be invoked when the user chooses to write/edit 
			  cfg data from the BootMonitor prompt. This will check if the Data
			  is valid.if not valid restores the default values. if valid 
			  prompts the user to edit the values. if user enters incorrect 
			  values, the operation is aborted.
Arguments	: not used. 
Return		: none
*******************************************************************************/	
void cmd_cfg_wr(int arc, char *argv[])
{
	char cfg_data[MAX_CFG_DATA_SZ];
	unsigned char datr, slave_addr;
    int id;
    struct option_info opts[1];
	bool id_set = false;
    
    init_opts(&opts[0], 'n', true, OPTION_ARG_TYPE_NUM,(void **)&id,
                            (bool *)&id_set, "cfg write device ID");    

    if(!scan_opts(argc, argv, 2, opts, 1, (void **)0, OPTION_ARG_TYPE_NUM, "Device ID"))
    {
        cfg_usage("Invalid arguments");
        return;
    }
	if(!id_set)
	{
		printf("Device ID required\n");
		return;
	}

	memset(cfg_data, 0, MAX_CFG_DATA_SZ);
    switch(id)
    {
    case PL_DEV_ID:
    case BW_DEV_ID:
    case BD_DEV_ID:
        slave_addr = SLAVE_ADDR_2;
		break;
    case MI_DEV_ID:
        slave_addr = SLAVE_ADDR_4;
		break;
	case BI_DEV_ID:
        slave_addr = SLAVE_ADDR_3;
		break;
	case KI_DEV_ID:
        slave_addr = SLAVE_ADDR_1;
		break;
	default:
		printf("Invalid Device ID\n");
		return;
    }

    if(i2c_read(slave_addr, 0x0, &datr))
    {
        printf("Encountered problem in reading EPROM\n");
        return;
    }

	if(datr != '0')
	{
		if(!get_data(cfg_data, DEFAULT, id))
        {
			printf("Aborting operation...\n");
			return;
		}
		if(!write_config_data(cfg_data, DEFAULT,id))
            return;
        read_config_data(cfg_data,id);
        display_config_data(cfg_data,id);
	}
	else
	{
		if(!get_data(cfg_data,USER,id))
		{
            printf("Aborting operation...\n");
			return;
		}
        if(!write_config_data(cfg_data, USER,id))
            return;
        read_config_data(cfg_data,id);
        display_config_data(cfg_data,id);
	}
}

/******************************************************************************
Function	: get_test_status() will be update the test status field and date 
			  field of the cfg data.
Arguments	: ts - Ptr to store test status data 
			  curr_ts - current test status 
			  user    - 1 take input from user 
					    0  default value
Return		: OK if the entered test sttaus is valid, else ERROR
*******************************************************************************/	
int get_test_status(unsigned char *curr_ts, int user, int id)
{
	char ts[2];

	*curr_ts = NOT_TESTED;
	if(user)
	{
		if(id == KI_DEV_ID)
		{
			printf("Enter Test Status(1 - SKU1_PASS, 2 - SKU1_FAIL, 3 - SKU2_PASS, 4 - SKU2_FAIL): ");

			gets((char *)ts, 2, 0);
			if((*ts < '1') ||(*ts > '4'))
			{
				printf("Invalid Test Status Code!!!\n");
				return ERROR;
			}
		}
		else
		{
			printf("Enter Test Status(1 - PASS, 2 - FAIL): ");

			gets((char *)ts, 2, 0);
			if((*ts < '1') ||(*ts > '2'))
			{
				printf("Invalid Test Status Code!!!\n");
				return ERROR;
			}
		}
		*curr_ts = *ts;
	}

	return OK;
}

/******************************************************************************
Function	:get_date() prompts the user to enter the date field. 
Arguments	: date-ptr to store the date field
			  curr_date - current date field data
			  user    - 1 take input from user 
					    0  default value
Return		:none
******************************************************************************/					    
void get_date(unsigned char *curr_date,int user)
{
	char date[CFG_DATE_LEN + 1];

	if(user)
	{
		memset(date, '\0', CFG_DATE_LEN + 1);
		memcpy(date, curr_date, CFG_DATE_LEN);
		printf("Test Date is: %s\n", date);

		printf("Enter new Test Date: ");
		gets(date, (CFG_DATE_LEN + 1),0);
		if(*date == '\0')
			return;

		memcpy((char *)curr_date, (const char*)date, CFG_DATE_LEN);
	}
}


/******************************************************************************
Function	: cmd_cfg_ts() will be invoked when the user chooses to update 
			  test sttaus and date fields of the config data after running 
			  diagnotics
Arguments	: not used. 
Return		: none
*******************************************************************************/	
void cmd_cfg_ts(int argc, char*argv[])
{
    char curr_data[MAX_CFG_DATA_SZ];
	int id, cfg_data_sz;
    struct option_info opts[1];
	bool id_set = false;
    unsigned char slave_addr;
    struct COMMON_CFG_CONTENT *cfg_data = (struct COMMON_CFG_CONTENT *)curr_data;
    
    init_opts(&opts[0], 'n', true, OPTION_ARG_TYPE_NUM,(void **)&id,
                            (bool *)&id_set, "cfg read device ID");    

    if(!scan_opts(argc, argv, 2, opts, 1, (void **)0, OPTION_ARG_TYPE_NUM, "Device ID"))
    {
        cfg_usage("invalid arguments");
        return;
    }
	if(!id_set)
	{
		printf("Device ID required\n");
		return;
	}

    switch(id)
	{
    case PL_DEV_ID:
		slave_addr = SLAVE_ADDR_2;
		cfg_data_sz = sizeof(struct PL_EEPROM_CONTENT);
		break;
    case BW_DEV_ID:
    case BD_DEV_ID:
        slave_addr = SLAVE_ADDR_2;
		cfg_data_sz = sizeof(struct COMMON_CFG_CONTENT);
		break;
	case MI_DEV_ID:
		slave_addr = SLAVE_ADDR_4;
		cfg_data_sz = sizeof(struct MI_EEPROM_CONTENT);
		break;
	case BI_DEV_ID:
		slave_addr = SLAVE_ADDR_3;
		cfg_data_sz = sizeof(struct COMMON_CFG_CONTENT); 
		break;
	case KI_DEV_ID:
		slave_addr = SLAVE_ADDR_1;
		cfg_data_sz = sizeof(struct KI_EEPROM_CONTENT);
		break;
    default:
        printf("Invalid Device ID\n");
        return;
	}
	
    if(i2c_seq_read(slave_addr,0x0, curr_data, cfg_data_sz))
    {
        printf("Encountered problem in reading EPROM\n");
        return;
    }
	get_test_status(&(cfg_data->test_status), USER, id);
	get_date(cfg_data->date, USER);
	if(!write_config_data(curr_data, USER, id))
        return;
	display_config_data(curr_data,id);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -