#include "elec_det.h"
#include "commend.h"
#include "mystring.h"
#include "mystdlib.h"
#include "base/delay.h"
#include "hardware/jw3425iic.h"
#include "hardware/power.h"
#include "interface/BaseChecker.h"
#include "base/define.h"
#include "base/utility.h"
#include "interface/EWChecker.h"

// 电子模块相关调试命令



#define UPDATA_CRC_ALL ((uint32_t *)(MC_CODE_ADDR+16*1024))[0]
#define UPDATA_CRC_APP ((uint32_t *)(MC_CODE_ADDR+16*1024))[1]
#define UPDATA_FILE_NAME ((const char *)(MC_CODE_ADDR+16*1024+8))



// 运行检测方案
static int cmd_check(list_def *argv)
{
  array_def *data=arr_creat();
  cmd_print("start check.");
  array_def *r=elec_check_with_scheme(data);
  arr_delete(data);
  cmd_print("check end,ret=%s.",str_temp(arr_string(r)));
  return 0;
}
commend_export(check,cmd_check,"start check")



// 通过iic擦除芯片
static int cmd_iic_erase(list_def *argv)
{
  if(list_length(argv)<3){
    cmd_print("param num too less.");
    return -1;
  }
  int mtp_addr=str_atoi(list_get_str(argv,1));
  int erase_mode=str_atoi(list_get_str(argv,2));
  int ret=0;
  checker_runcfg.params[0]=(uint16_t)mtp_addr;
  checker_runcfg.params[1]=(uint16_t)erase_mode;
  checker_runcfg.param_count=2;
  MC_EraseMTP();
  ret=checker_runcfg.excue_rtv;
  cmd_print("cmd end,ret=%d.",ret);
  return 0;
}
commend_export(iic_erase,cmd_iic_erase,"erase mtp via iic,param:mtp_addr,erase_mode")



// 通过iic升级芯片
static int cmd_iic_updata(list_def *argv)
{
  if(list_length(argv)<3){
    cmd_print("param num too less.");
    return -1;
  }
  int mtp_addr=str_atoi(list_get_str(argv,1));
  int data_length=str_atoi(list_get_str(argv,2));
  int ret=0;
  uint32_t crc32=0;
  checker_runcfg.params[0]=(uint16_t)mtp_addr;
  checker_runcfg.params[1]=(uint16_t)data_length;
  checker_runcfg.param_count=2;
  //crc32=Crc32Calu((uint32_t*)MC_CODE_ADDR,16*1024);
  crc32=UPDATA_CRC_ALL;
  cmd_print("rom name:%s.",UPDATA_FILE_NAME);
  boardinfo_un.boardinfo.mc_ICodeCrc32=crc32;
  cmd_print("crc32=0x%04x.",crc32);
  
  ret=PowerCalibration_set(85,55);
  if(ret==0){
    MC_IICUpdateMTP();
    ret=checker_runcfg.excue_rtv;
  }else{
    cmd_print("power set failed.");
  }
  PowerCalibration_set(POWER_DEF_V,45);
  POWER_OFF;
  
  cmd_print("cmd end,ret=%d.",ret);
  return 0;
}
commend_export(iic_updata,cmd_iic_updata,"updata mtp via iic,param:mtp_addr,data_length")



// 通过iic读取mtp
static int cmd_iic_read(list_def *argv)
{
  if(list_length(argv)<3){
    cmd_print("param num too less.");
    return -1;
  }
  int mtp_addr=str_atoi(list_get_str(argv,1));
  int data_length=str_atoi(list_get_str(argv,2));
  int ret=0;
  checker_runcfg.params[0]=(uint16_t)mtp_addr;
  checker_runcfg.params[1]=(uint16_t)data_length;
  checker_runcfg.param_count=2;
  MC_IICReadMTP();
  ret=checker_runcfg.excue_rtv;
  cmd_print("cmd end,ret=%d.",ret);
  return 0;
}
commend_export(iic_read,cmd_iic_read,"read mtp via iic,param:mtp_addr,data_length")




// 设置总线电压
static int cmd_set_bus_power(list_def *argv)
{
  if(list_length(argv)<3){
    cmd_print("param num too less.");
    return -1;
  }
  int bus_hv=str_atoi(list_get_str(argv,1));
  int bus_mv=str_atoi(list_get_str(argv,2));
  int ret=0;
  if(bus_hv>0){
    ret=PowerCalibration_set(bus_hv,bus_mv);
  }
  else{
    ret=PowerCalibration_set(POWER_DEF_V,45);
    POWER_OFF;
  }
  cmd_print("cmd end,ret=%d.",ret);
  return 0;
}
commend_export(bus_power,cmd_set_bus_power,"set bus power,param:bus_high_v,bus_mid_v")







// 切换总线极性
static int cmd_turn_polarity(list_def *argv)
{
  if(list_length(argv)<3){
    cmd_print("param num too less.");
    return -1;
  }
  int ret=0;
  int us=str_atoi(list_get_str(argv,1));
  int times=str_atoi(list_get_str(argv,2));
  for(int i=0;i<times;i++)
  {
    EW_DIFF_MOD_L;
    delay_us(us);
    EW_DIFF_MOD_H;
    delay_us(us);
  }
  cmd_print("cmd end,ret=%d.",ret);
  return 0;
}
commend_export(bus_polarity,cmd_turn_polarity,"set bus polarity,param:delay_us,times")











