893 lines
21 KiB
C
Executable File
893 lines
21 KiB
C
Executable File
/**************************************************************************
|
|
*
|
|
* Copyright (c) 2015-2020 by WuYuan Technology, Inc.
|
|
*
|
|
* This software is copyrighted by and is the property of SiFar
|
|
* Technology, Inc.. All rights are reserved by SiFar Technology, Inc..
|
|
* This software may only be used in accordance with the corresponding
|
|
* license agreement. Any unauthorized use, duplication, distribution,
|
|
* or disclosure of this software is expressly forbidden.
|
|
*
|
|
* This Copyright notice MUST not be removed or modified without prior
|
|
* written consent of SiFar Technology, Inc..
|
|
*
|
|
* WuYuan Technology, Inc. reserves the right to modify this software without notice.
|
|
*
|
|
* Author: ljy
|
|
* Ver: 1.0.0 2022.06.15
|
|
* Description: creat
|
|
**************************************************************************/
|
|
|
|
#ifdef __KERNEL__
|
|
#include <linux/delay.h>
|
|
#include <linux/module.h>
|
|
//#include <mach/rcw_macro.h>
|
|
//#include "kwrap/type.h"
|
|
//#include "kwrap/semaphore.h"
|
|
//#include "kwrap/flag.h"
|
|
#include <sf_mcu.h>
|
|
#else
|
|
//#include "kwrap/type.h"
|
|
//#include "kwrap/semaphore.h"
|
|
//#include "kwrap/flag.h"
|
|
#include <sf_mcu.h>
|
|
#endif
|
|
|
|
#define __MODULE__ sf_i2c_mcu
|
|
#define __DBGLVL__ 2 // 0=FATAL, 1=ERR, 2=WRN, 3=UNIT, 4=FUNC, 5=IND, 6=MSG, 7=VALUE, 8=USER
|
|
#define __DBGFLT__ "*" // *=All, [mark]=CustomClass
|
|
#include "kwrap/debug.h"
|
|
//#include <kwrap/debug.h>
|
|
#include <stdio.h>
|
|
#include <kwrap/util.h>
|
|
#include "comm/hwclock.h"
|
|
#include "comm/timer.h"
|
|
#include <time.h>
|
|
#include <rtos_na51089/gpio.h>
|
|
#include "UIInfo.h"
|
|
#include "GxUSB.h"
|
|
#include "kwrap/task.h"
|
|
#include <uart.h>
|
|
#include "NvtUser/NvtUser.h"
|
|
#include "AppControl/AppControl.h"
|
|
|
|
#include <sf_commu_mcu.h>
|
|
|
|
#define SERIAL_DEVICE_PATH "/dev/ttyS2"
|
|
|
|
#ifdef __cplusplus
|
|
#if __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
#endif
|
|
|
|
unsigned char mcu_upgrade_buf[32];
|
|
unsigned char mcu_upgrade_buf_len;
|
|
|
|
static const unsigned short Crc16Table[256] =
|
|
{
|
|
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
|
|
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
|
|
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
|
|
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
|
|
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
|
|
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
|
|
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
|
|
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
|
|
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
|
|
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
|
|
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
|
|
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
|
|
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
|
|
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
|
|
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
|
|
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
|
|
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
|
|
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
|
|
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
|
|
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
|
|
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
|
|
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
|
|
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
|
|
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
|
|
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
|
|
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
|
|
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
|
|
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
|
|
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
|
|
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
|
|
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
|
|
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
|
|
};
|
|
|
|
/*************************************************
|
|
Function: sf_crc16_check
|
|
Description: crc16 check
|
|
Input: data, data len
|
|
Output: N/A
|
|
Return: crc16 value
|
|
Others: N/A
|
|
*************************************************/
|
|
|
|
static unsigned short sf_crc16_check(unsigned char * dataIn, int length)
|
|
{
|
|
unsigned short result = 0;
|
|
unsigned short tableNo = 0;
|
|
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
tableNo = ((result & 0xff) ^ (dataIn[i] &0xff));
|
|
result = ((result >> 8) & 0xff) ^Crc16Table[tableNo];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
static unsigned char sndBuf[BUF_REG_SIZE] =
|
|
{
|
|
0
|
|
};
|
|
|
|
SMAPBUF_t smap_buf =
|
|
{
|
|
.index = 0,
|
|
};
|
|
|
|
int SMAP_waitAck = 0;
|
|
|
|
unsigned char mcu_buf[BUF_REG_SIZE] = {0};
|
|
unsigned int mcu_buf_len = 0;
|
|
unsigned char cmpRegBuf[BUF_REG_SIZE] = {0};
|
|
unsigned char cmpValBuf[BUF_REG_SIZE] = {0};
|
|
|
|
static unsigned char sf_commu_mcu_exit = 1;
|
|
static VK_TASK_HANDLE sfCommuMcuThread = 0;
|
|
THREAD_RETTYPE sf_commu_mcu_task(void *arg);
|
|
|
|
int gsfd = -1;
|
|
|
|
void mcubuf_reset(void)
|
|
{
|
|
mcu_buf_len = 0;
|
|
memset(mcu_buf, 0x00, sizeof(mcu_buf));
|
|
}
|
|
|
|
int sf_commu_write(unsigned char * buf, int len)
|
|
{
|
|
int ret = FAIL;
|
|
ret = uart3_write(buf, len);
|
|
|
|
// vos_flag_set(FLG_ID_UART3, UART3_FLAG_DATAVALID);
|
|
if(ret <= 0)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
return FAIL;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
int sf_commu_read(unsigned char * buf, int len)
|
|
{
|
|
INT32 ret = 0;
|
|
//int j = 0;
|
|
/*
|
|
UART_DATA_PARA DataPara;
|
|
DataPara.Action = UART_ACT_GET;
|
|
DataPara.BufferInfo.uiSize = len;
|
|
DataPara.BufferInfo.pBuffer = buf;
|
|
ret = uart3_operateData(&DataPara);
|
|
*/
|
|
//uart3_waitData(UART_ACT_GET);
|
|
//printf("%s:%d s\r\n", __FUNCTION__, __LINE__);
|
|
|
|
ret = (INT32)uart3_read(buf, len);
|
|
|
|
if(ret < 0)
|
|
{
|
|
printf("%s:%d error ret:%lu\r\n", __FUNCTION__, __LINE__,ret);
|
|
return 0;
|
|
}
|
|
return ret;
|
|
|
|
}
|
|
|
|
int sf_set_mcu_reg(unsigned char reg, unsigned char val)
|
|
{
|
|
unsigned short index = 0;
|
|
unsigned short crc = 0;
|
|
unsigned short crc_start = 0;
|
|
unsigned short crc_end = 0;
|
|
int ret = FAIL;
|
|
memset(sndBuf, 0x00, sizeof(sndBuf));
|
|
// uint8_t i = 0;
|
|
sndBuf[index] = 0x55;
|
|
index++;
|
|
sndBuf[index] = 0xAA;
|
|
index++;
|
|
crc_start = index;
|
|
sndBuf[index] = 0x03;
|
|
index++;
|
|
sndBuf[index] = reg;
|
|
index++;
|
|
sndBuf[index] = val;
|
|
index++;
|
|
crc_end = index;
|
|
crc = sf_crc16_check(&sndBuf[crc_start], crc_end - crc_start);
|
|
sndBuf[index] = (crc & 0xff00) >> 8;
|
|
index++;
|
|
sndBuf[index] = crc & 0x00ff;
|
|
index++;
|
|
sndBuf[index] = 0xFF;
|
|
index++;
|
|
sndBuf[index] = 0xEE;
|
|
ret = sf_commu_write(sndBuf, index + 1);
|
|
if(ret == FAIL)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
}
|
|
return 0x03;
|
|
}
|
|
|
|
int sf_get_mcu_reg(unsigned char reg)
|
|
{
|
|
unsigned short index = 0;
|
|
unsigned short crc = 0;
|
|
unsigned short crc_start = 0;
|
|
unsigned short crc_end = 0;
|
|
int ret = FAIL;
|
|
//uint8_t i = 0;
|
|
memset(sndBuf, 0x00, sizeof(sndBuf));
|
|
sndBuf[index] = 0x55;
|
|
index++;
|
|
sndBuf[index] = 0xAA;
|
|
index++;
|
|
crc_start = index;
|
|
sndBuf[index] = 0x01;
|
|
index++;
|
|
sndBuf[index] = reg;
|
|
index++;
|
|
crc_end = index;
|
|
crc = sf_crc16_check(&sndBuf[crc_start], crc_end - crc_start);
|
|
sndBuf[index] = (crc & 0xff00) >> 8;
|
|
index++;
|
|
sndBuf[index] = crc & 0x00ff;
|
|
index++;
|
|
sndBuf[index] = 0xFF;
|
|
index++;
|
|
sndBuf[index] = 0xEE;
|
|
|
|
ret = sf_commu_write(sndBuf, index + 1);
|
|
if(ret == FAIL)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
}
|
|
|
|
return 0x01;
|
|
}
|
|
|
|
int sf_set_mcu_reg_many(unsigned char reg[], unsigned char val[], unsigned int num)
|
|
{
|
|
unsigned short index = 0;
|
|
unsigned short crc = 0;
|
|
unsigned short crc_start = 0;
|
|
unsigned short crc_end = 0;
|
|
unsigned int i = 0;
|
|
int ret = FAIL;
|
|
memset(sndBuf, 0x00, sizeof(sndBuf));
|
|
// uint8_t i = 0;
|
|
sndBuf[index] = 0x55;
|
|
index++;
|
|
sndBuf[index] = 0xAA;
|
|
index++;
|
|
crc_start = index;
|
|
sndBuf[index] = 0x04;
|
|
index++;
|
|
sndBuf[index] = num;
|
|
|
|
for(i = 0; i < num; i++)
|
|
{
|
|
index++;
|
|
sndBuf[index] = reg[i];
|
|
index++;
|
|
sndBuf[index] = val[i];
|
|
|
|
}
|
|
index++;
|
|
crc_end = index;
|
|
crc = sf_crc16_check(&sndBuf[crc_start], crc_end - crc_start);
|
|
sndBuf[index] = (crc & 0xff00) >> 8;
|
|
index++;
|
|
sndBuf[index] = crc & 0x00ff;
|
|
index++;
|
|
sndBuf[index] = 0xFF;
|
|
index++;
|
|
sndBuf[index] = 0xEE;
|
|
ret = sf_commu_write(sndBuf, index + 1);
|
|
if(ret == FAIL)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
}
|
|
return 0x03;
|
|
}
|
|
|
|
int sf_get_mcu_reg_many(unsigned char reg[], unsigned int num)
|
|
{
|
|
unsigned short index = 0;
|
|
unsigned short crc = 0;
|
|
unsigned short crc_start = 0;
|
|
unsigned short crc_end = 0;
|
|
unsigned int i = 0;
|
|
int ret = FAIL;
|
|
//uint8_t i = 0;
|
|
memset(sndBuf, 0x00, sizeof(sndBuf));
|
|
sndBuf[index] = 0x55;
|
|
index++;
|
|
sndBuf[index] = 0xAA;
|
|
index++;
|
|
crc_start = index;
|
|
sndBuf[index] = 0x02;
|
|
index++;
|
|
sndBuf[index] = num;
|
|
|
|
|
|
for(i = 0; i < num; i++)
|
|
{
|
|
index++;
|
|
sndBuf[index] = reg[i];
|
|
}
|
|
|
|
index++;
|
|
crc_end = index;
|
|
crc = sf_crc16_check(&sndBuf[crc_start], crc_end - crc_start);
|
|
sndBuf[index] = (crc & 0xff00) >> 8;
|
|
index++;
|
|
sndBuf[index] = crc & 0x00ff;
|
|
index++;
|
|
sndBuf[index] = 0xFF;
|
|
index++;
|
|
sndBuf[index] = 0xEE;
|
|
|
|
ret = sf_commu_write(sndBuf, index + 1);
|
|
if(ret == FAIL)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
}
|
|
|
|
return 0x01;
|
|
}
|
|
|
|
unsigned int sf_set_mcu_reg_ack_depack(unsigned int * address, unsigned int * value)
|
|
{
|
|
unsigned short crc1 = (((unsigned short) smap_buf.buf[smap_buf.index - 4]) << 8) | smap_buf.buf[smap_buf.index - 3];
|
|
unsigned short crc2 = 0;
|
|
|
|
crc2 = sf_crc16_check(&smap_buf.buf[2], smap_buf.index - 6);
|
|
|
|
if (crc2 != crc1)
|
|
{
|
|
printf("crc error crc1 %d, crc2 %d, function %s\r\n", crc1, crc2, __FUNCTION__);
|
|
return - 1;
|
|
}
|
|
|
|
*address = smap_buf.buf[3];
|
|
*value = smap_buf.buf[4];
|
|
|
|
if (smap_buf.buf[2] == 0xFF) /* error command 0xff */
|
|
{
|
|
printf("%s:%d command error\r\n", __FUNCTION__, __LINE__);
|
|
return - 1;
|
|
}
|
|
|
|
return (unsigned int)smap_buf.buf[2];
|
|
}
|
|
|
|
unsigned int sf_get_mcu_reg_ack_depack(unsigned int * address, unsigned int * value)
|
|
{
|
|
unsigned short crc1 = (((unsigned short) smap_buf.buf[smap_buf.index - 4] << 8) & 0xff00) | smap_buf.buf[smap_buf.index - 3];
|
|
unsigned short crc2 = 0;
|
|
|
|
crc2 = sf_crc16_check(&smap_buf.buf[2], smap_buf.index - 6);
|
|
|
|
if (crc2 != crc1)
|
|
{
|
|
printf("crc error crc1 %d, crc2 %d, function %s, file %s, line %d\r\n", crc1, crc2, __FUNCTION__,
|
|
__FILE__, __LINE__);
|
|
return - 1;
|
|
}
|
|
|
|
*address = smap_buf.buf[3];
|
|
*value = smap_buf.buf[4];
|
|
|
|
if (smap_buf.buf[2] == 0xFF) /* error command 0xff */
|
|
{
|
|
printf("%s:%d command error\r\n", __FUNCTION__, __LINE__);
|
|
return - 1;
|
|
}
|
|
|
|
return (unsigned int)smap_buf.buf[2];
|
|
}
|
|
|
|
|
|
|
|
unsigned int sf_get_mcu_reg_ack_depack_many(unsigned char reg[], unsigned char val[], unsigned int * num)
|
|
{
|
|
unsigned short crc1 = (((unsigned short) smap_buf.buf[smap_buf.index - 4] << 8) & 0xff00) | smap_buf.buf[smap_buf.index - 3];
|
|
unsigned short crc2 = 0;
|
|
unsigned int i = 0;
|
|
unsigned int j = 4;
|
|
|
|
crc2 = sf_crc16_check(&smap_buf.buf[2], smap_buf.index - 6);
|
|
|
|
if (crc2 != crc1)
|
|
{
|
|
printf("crc error crc1 %d, crc2 %d, function %s, file %s, line %d\r\n", crc1, crc2, __FUNCTION__,
|
|
__FILE__, __LINE__);
|
|
return - 1;
|
|
}
|
|
*num = smap_buf.buf[3];
|
|
|
|
for(i = 0; i < *num; i++)
|
|
{
|
|
reg[i]= smap_buf.buf[j];
|
|
j++;
|
|
val[i]= smap_buf.buf[j];
|
|
j++;
|
|
}
|
|
|
|
if (smap_buf.buf[2] == 0xFF) /* error command 0xff */
|
|
{
|
|
printf("%s:%d command error\r\n", __FUNCTION__, __LINE__);
|
|
return - 1;
|
|
}
|
|
|
|
return (unsigned int)smap_buf.buf[2];
|
|
}
|
|
|
|
int sf_commu_wait(unsigned char cmd)
|
|
{
|
|
volatile char trytimes = 0;
|
|
while ((smap_buf.buf[2] != cmd || SMAP_waitAck == 0) && trytimes <= 100)
|
|
{
|
|
trytimes++;
|
|
vos_util_delay_us(10000);
|
|
}
|
|
if (trytimes > 100)
|
|
{
|
|
sf_commu_reset();
|
|
return - 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void sf_commu_reset(void)
|
|
{
|
|
smap_buf.index = 0;
|
|
SMAP_waitAck = 0;
|
|
memset(smap_buf.buf, 0x00, sizeof(smap_buf.buf));
|
|
memset(cmpRegBuf, 0x00, sizeof(cmpRegBuf));
|
|
memset(cmpValBuf, 0x00, sizeof(cmpValBuf));
|
|
}
|
|
|
|
int sf_commu_set_mcu(unsigned char reg, unsigned char val)
|
|
{
|
|
int ret = -1;
|
|
unsigned int address = 0;
|
|
unsigned int value = 0;
|
|
//printf("W_addr[%02d,0x%02x]\n", reg,val);
|
|
sf_commu_reset();
|
|
sf_set_mcu_reg(reg, val);
|
|
ret = sf_commu_wait(0x03);
|
|
if(ret < 0)
|
|
{
|
|
printf("%s:%d time out\r\n", __FUNCTION__, __LINE__);
|
|
sf_commu_reset();
|
|
return - 1;
|
|
}
|
|
ret = sf_set_mcu_reg_ack_depack(&address, &value);
|
|
|
|
if (ret < 0 || address != reg || value != val)
|
|
{
|
|
printf("%s:%d error address %d, value %d\r\n", __FUNCTION__, __LINE__, address, value);
|
|
ret = -1;
|
|
}
|
|
else
|
|
{
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int sf_commu_get_mcu(unsigned char reg)
|
|
{
|
|
int ret = 0;
|
|
unsigned int address = 0;
|
|
unsigned int value = 0;
|
|
|
|
sf_commu_reset();
|
|
sf_get_mcu_reg(reg);
|
|
ret = sf_commu_wait(0x01);
|
|
if(ret < 0)
|
|
{
|
|
printf("%s:%d time out\r\n", __FUNCTION__, __LINE__);
|
|
sf_commu_reset();
|
|
return - 1;
|
|
}
|
|
|
|
ret = sf_get_mcu_reg_ack_depack(&address, &value);
|
|
if (ret < 0 || address != reg)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
ret = -1;
|
|
}
|
|
else
|
|
{
|
|
ret = (int)value;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
int sf_commu_set_mcu_many(unsigned char reg[], unsigned char val[], unsigned int *num)
|
|
{
|
|
int ret = -1;
|
|
//unsigned int address = 0;
|
|
//unsigned int value = 0;
|
|
unsigned int i = 0;
|
|
//printf("W_addr[%02d,0x%02x]\n", reg,val);
|
|
sf_commu_reset();
|
|
sf_set_mcu_reg_many(reg, val, *num);
|
|
ret = sf_commu_wait(0x04);
|
|
if(ret < 0)
|
|
{
|
|
printf("%s:%d time out\r\n", __FUNCTION__, __LINE__);
|
|
sf_commu_reset();
|
|
return - 1;
|
|
}
|
|
|
|
ret = sf_get_mcu_reg_ack_depack_many(cmpRegBuf, cmpValBuf, num);
|
|
if (ret < 0)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
ret = -1;
|
|
}
|
|
else
|
|
{
|
|
for(i = 0; i < *num; i++)
|
|
{
|
|
if((reg[i] != cmpRegBuf[i]) || (val[i] != cmpValBuf[i]))
|
|
{
|
|
printf("%s:%d error address %d, value %d get address %d, value %d\r\n", __FUNCTION__, __LINE__, reg[i], val[i], cmpRegBuf[i], cmpValBuf[i]);
|
|
ret = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int sf_commu_get_mcu_many(unsigned char reg[], unsigned char val[], unsigned int *num)
|
|
{
|
|
int ret = 0;
|
|
unsigned int i = 0;
|
|
|
|
sf_commu_reset();
|
|
sf_get_mcu_reg_many(reg,*num);
|
|
ret = sf_commu_wait(0x02);
|
|
if(ret < 0)
|
|
{
|
|
printf("%s:%d time out\r\n", __FUNCTION__, __LINE__);
|
|
sf_commu_reset();
|
|
return - 1;
|
|
}
|
|
|
|
ret = sf_get_mcu_reg_ack_depack_many(cmpRegBuf, val, num);
|
|
if (ret < 0)
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
ret = -1;
|
|
}
|
|
else
|
|
{
|
|
for(i = 0; i < *num; i++)
|
|
{
|
|
if(reg[i] != cmpRegBuf[i])
|
|
{
|
|
printf("%s:%d error\r\n", __FUNCTION__, __LINE__);
|
|
ret = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int sf_getRegFromMcu_depack(unsigned char * dataToParse, unsigned int datalen, unsigned char * regAddr, unsigned char * regVal)
|
|
{
|
|
unsigned short crc1 = 0;
|
|
unsigned short crc2 = 0;
|
|
unsigned char crc_start = 0;
|
|
unsigned char crc_end = 0;
|
|
|
|
crc_start = 2;
|
|
crc_end = datalen - 4 - 1;
|
|
crc1 = (((unsigned short) dataToParse[datalen - 1 - 3] << 8) & 0xff00) | dataToParse[datalen - 1 - 2];
|
|
crc2 = sf_crc16_check(&dataToParse[crc_start], crc_end - crc_start + 1);
|
|
|
|
if (crc1 != crc2)
|
|
{
|
|
printf("0x05 crc error\r\n");
|
|
return - 1;
|
|
}
|
|
else
|
|
{
|
|
*regVal = dataToParse[4];
|
|
*regAddr = dataToParse[3];
|
|
return dataToParse[2];
|
|
}
|
|
}
|
|
|
|
int sf_getRegFromMcu_ack(unsigned char regAddr, unsigned char regVal)
|
|
{
|
|
unsigned short index = 0;
|
|
unsigned short crc = 0;
|
|
unsigned short crc_start = 0;
|
|
unsigned short crc_end = 0;
|
|
memset(sndBuf, 0x00, sizeof(sndBuf));
|
|
|
|
sndBuf[index] = 0x55;
|
|
index++;
|
|
sndBuf[index] = 0xAA;
|
|
index++;
|
|
crc_start = index;
|
|
sndBuf[index] = 0x05;
|
|
index++;
|
|
sndBuf[index] = regAddr;
|
|
index++;
|
|
sndBuf[index] = regVal;
|
|
index++;
|
|
crc_end = index;
|
|
crc = sf_crc16_check(&sndBuf[crc_start], crc_end - crc_start);
|
|
sndBuf[index] = (crc & 0xff00) >> 8;
|
|
index++;
|
|
sndBuf[index] = crc & 0x00ff;
|
|
index++;
|
|
sndBuf[index] = 0xFF;
|
|
index++;
|
|
sndBuf[index] = 0xEE;
|
|
|
|
sf_commu_write(sndBuf, index + 1);
|
|
|
|
return 0x05;
|
|
}
|
|
|
|
unsigned char sf_commu_parse_mcu_data(unsigned char * src, unsigned int len)
|
|
{
|
|
unsigned char * copyBuf = src;
|
|
unsigned int copyLen = len;
|
|
int ret = -1;
|
|
unsigned char reg[3] = {0};
|
|
unsigned char val[3] = {0};
|
|
|
|
switch (copyBuf[2])
|
|
{
|
|
case 0x05:
|
|
ret = sf_getRegFromMcu_depack(copyBuf, copyLen, ®[0], &val[0]);
|
|
if (ret > 0)
|
|
{
|
|
printf("MCU send 0x05, reg_value[%d,%d]\n\n",reg[0], val[0]);
|
|
sf_getRegFromMcu_ack(reg[0], val[0]);
|
|
//SF_MESSAGE_BUF_S stMessageBuf = {0};
|
|
//stMessageBuf.cmdId = CMD_KEY;
|
|
//stMessageBuf.arg1 = SF_EVENT_KEY_SHORT_CLICK;
|
|
//stMessageBuf.arg2 = SF_KEY_PIN_DATAREADY;
|
|
//sf_com_message_send_to_app(&stMessageBuf);
|
|
ret = 0x05;
|
|
Ux_PostEvent(NVTEVT_SYSTEM_SHUTDOWN, 1, APP_POWER_OFF_APO);
|
|
}
|
|
else
|
|
{
|
|
printf("0x05 error\r\n");
|
|
}
|
|
break;
|
|
/*
|
|
case 0x06:
|
|
printf("SMAP_parse_data 0x06\r\n");
|
|
ret = SMAP_getMultiRegFromMcu_depack(copyBuf, copyLen, ®Cnt, reg, val);
|
|
if (ret > 0)
|
|
{
|
|
SMAP_getMultiRegFromMcu_ack(regCnt, reg, val);
|
|
ret = 0x06;
|
|
}
|
|
else
|
|
{
|
|
printf("0x06 error\r\n");
|
|
}
|
|
break;
|
|
*/
|
|
default:
|
|
//ret = 0x00;
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
THREAD_RETTYPE sf_commu_mcu_task(void *arg)
|
|
{
|
|
THREAD_ENTRY();
|
|
static unsigned char buffer[BUF_REG_SIZE] = {0};
|
|
int i = 0;
|
|
int readLen = 0;
|
|
int len = 0;
|
|
short start = -1;
|
|
short end = -1;
|
|
unsigned char temp_start1 = 0;
|
|
unsigned char temp_start2 = 0;
|
|
unsigned char temp_end1 = 0;
|
|
unsigned char temp_end2 = 0;
|
|
unsigned char copy_size = 0;
|
|
//sf_commu_mcu_interface_init(115200, 'N', 8, 1, 'N');
|
|
//sf_commu_mcu_open();
|
|
//FLGPTN FlgPtn;
|
|
|
|
mcubuf_reset();
|
|
CHKPNT;
|
|
while(sf_commu_mcu_exit == 0)
|
|
{
|
|
readLen = sf_commu_read(buffer + len, 1);
|
|
|
|
if(readLen > 0)
|
|
{
|
|
len += readLen;
|
|
//printf("%s:%d len = %d readLen = %d\n",__FUNCTION__,__LINE__, len, readLen);
|
|
}
|
|
|
|
while(i < len)
|
|
{
|
|
|
|
while(i < len && start < 0)
|
|
{
|
|
if(buffer[i] == 0x55)
|
|
{
|
|
temp_start1 = i;
|
|
}
|
|
if(buffer[i] == 0xAA)
|
|
{
|
|
temp_start2 = i;
|
|
}
|
|
if((temp_start2 - temp_start1) == 1)
|
|
{
|
|
start = temp_start1;
|
|
temp_start1 = 0;
|
|
temp_start2 = 0;
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
|
|
if(start >= 0)
|
|
{
|
|
while (i < len)
|
|
{
|
|
if(buffer[i] == 0xFF)
|
|
{
|
|
temp_end1 = i;
|
|
}
|
|
if(buffer[i] == 0xEE)
|
|
{
|
|
temp_end2 = i;
|
|
}
|
|
if((temp_end2 - temp_end1) == 1)
|
|
{
|
|
end = temp_end2;
|
|
temp_end1 = 0;
|
|
temp_end2 = 0;
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
if(start >= 0 && end > 0)
|
|
{
|
|
copy_size = end - start + 1;
|
|
if(buffer[start + 2] != 0xff)
|
|
{
|
|
if(buffer[start + 2] != 0x05 && buffer[start + 2] != 0x06 && buffer[start + 2] != 0xff)
|
|
{
|
|
if(buffer[start + 2] == 0xee || buffer[start + 2] == 0xef || buffer[start + 2] == 0xec)
|
|
{
|
|
memcpy(mcu_upgrade_buf, &buffer[start], copy_size);
|
|
mcu_upgrade_buf_len = copy_size;
|
|
}
|
|
else
|
|
{
|
|
memcpy(smap_buf.buf + smap_buf.index, &buffer[start], copy_size);
|
|
smap_buf.index += copy_size; //index从0开始
|
|
SMAP_waitAck = 1;
|
|
//printf("%s:%d SMAP_waitAck 1\n", __FUNCTION__, __LINE__);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
memcpy(mcu_buf, &buffer[start], copy_size);
|
|
mcu_buf_len = copy_size;
|
|
sf_commu_parse_mcu_data(mcu_buf, mcu_buf_len);
|
|
mcubuf_reset();
|
|
}
|
|
start = -1;
|
|
end = -1;
|
|
len = 0;
|
|
i = 0;
|
|
|
|
}
|
|
else
|
|
{
|
|
start = -1;
|
|
end = -1;
|
|
len = 0;
|
|
i = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
THREAD_RETURN(0);
|
|
}
|
|
|
|
|
|
|
|
int sf_commu_mcu_task_start(void)
|
|
{
|
|
printf("%s:%d s\r\n", __FUNCTION__, __LINE__);
|
|
if(sf_commu_mcu_exit == 1)
|
|
{
|
|
sf_commu_mcu_exit = 0;
|
|
int rt = 0;
|
|
static int InitFlag = 1;
|
|
//T_CFLG cflg ;
|
|
|
|
if(InitFlag)
|
|
{
|
|
//vos_flag_create(&FLG_ID_UART3, &cflg, "SF UART receive FLG");
|
|
//vos_flag_clr(FLG_ID_UART3, UART3_FLAG_ALL);
|
|
InitFlag = 0;
|
|
rt = sf_init_mcu();
|
|
printf("%s:%d %d\r\n", __FUNCTION__, __LINE__,rt);
|
|
|
|
}
|
|
sfCommuMcuThread = vos_task_create(sf_commu_mcu_task, NULL, "sf_commu_mcu_task", 10, 2048);
|
|
if(!sfCommuMcuThread)
|
|
{
|
|
DBG_ERR("[ERROR] sf_commu_mcu_task thread creat failed!\r\n");
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
vos_task_resume(sfCommuMcuThread);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
void sf_commu_mcu_task_stop(void)
|
|
{
|
|
sf_commu_mcu_exit = 1;
|
|
//vos_flag_destroy(FLG_ID_UART3);
|
|
vos_task_destroy(sfCommuMcuThread);
|
|
}
|
|
|
|
unsigned char sf_commu_mcu_task_running(void)
|
|
{
|
|
return sf_commu_mcu_exit;
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
#if __cplusplus
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
|