TEST_CODE_ApplyTo2V1_API

Dependencies:   SDFileSystem max32630fthr USBDevice

Committer:
china_sn0w
Date:
Tue Jul 28 01:40:05 2020 +0000
Revision:
4:217334c3a5b2
Parent:
3:35b05d91568d
A

Who changed what in which revision?

UserRevisionLine numberNew contents of line
china_sn0w 1:7530b7eb757a 1 #include "mbed.h"
china_sn0w 1:7530b7eb757a 2 #include "cmsis_os.h"
china_sn0w 1:7530b7eb757a 3 #include "max32630fthr.h"
china_sn0w 1:7530b7eb757a 4 #include "USBSerial.h"
china_sn0w 1:7530b7eb757a 5 #include "CmdHandler.h"
china_sn0w 1:7530b7eb757a 6
china_sn0w 1:7530b7eb757a 7 #include "DUT_RegConfig.h"
china_sn0w 1:7530b7eb757a 8 //#include "Config.h"
china_sn0w 1:7530b7eb757a 9 #include "ServoRun.h"
china_sn0w 1:7530b7eb757a 10
china_sn0w 4:217334c3a5b2 11 #include "Van_API.h"
china_sn0w 4:217334c3a5b2 12
china_sn0w 1:7530b7eb757a 13 GLOBAL_CMD_VAR g_cmd;
china_sn0w 1:7530b7eb757a 14
china_sn0w 1:7530b7eb757a 15 //extern CONFIG g_config;
china_sn0w 1:7530b7eb757a 16 extern DUTREG dut_reg[DUT_REG_NUM];
china_sn0w 1:7530b7eb757a 17 extern uint8_t histogram[10][1024];
china_sn0w 1:7530b7eb757a 18 extern uint8_t histogram_mode;
china_sn0w 1:7530b7eb757a 19 extern uint8_t histogram_tdc;
china_sn0w 1:7530b7eb757a 20 extern uint8_t Firmware[8192];
china_sn0w 1:7530b7eb757a 21 extern uint8_t dcr_matrix[17][9*2];
china_sn0w 1:7530b7eb757a 22
china_sn0w 1:7530b7eb757a 23 uint8_t _uart_send_pbuff[CMD_BUF_LEN] = {0x00};
china_sn0w 1:7530b7eb757a 24
china_sn0w 1:7530b7eb757a 25 uint8_t UserTxBufferFS[2048];
china_sn0w 1:7530b7eb757a 26
china_sn0w 1:7530b7eb757a 27 uint8_t sys_cfg_save = 0;
china_sn0w 1:7530b7eb757a 28
china_sn0w 1:7530b7eb757a 29 //extern Queue<uint8_t, 2048> cmd_queue;
china_sn0w 1:7530b7eb757a 30 extern USBSerial microUSB;
china_sn0w 1:7530b7eb757a 31 extern DigitalOut rLED;
china_sn0w 1:7530b7eb757a 32 extern DigitalOut xSHUT;
china_sn0w 1:7530b7eb757a 33 extern InterruptIn chip_int;
china_sn0w 4:217334c3a5b2 34 extern DigitalOut TRIM_EN;
china_sn0w 1:7530b7eb757a 35
china_sn0w 1:7530b7eb757a 36 extern uint16_t histogram_pos_num;
china_sn0w 1:7530b7eb757a 37 extern uint16_t histogram_per_pos;
china_sn0w 1:7530b7eb757a 38
china_sn0w 3:35b05d91568d 39 extern uint16_t range_step_num;
china_sn0w 3:35b05d91568d 40 extern uint16_t range_per_step;
china_sn0w 3:35b05d91568d 41
china_sn0w 4:217334c3a5b2 42 extern uint8_t work_mode;
china_sn0w 4:217334c3a5b2 43
china_sn0w 1:7530b7eb757a 44 void CmdHandleTask(void)
china_sn0w 1:7530b7eb757a 45 {
china_sn0w 1:7530b7eb757a 46 uint8_t ret;
china_sn0w 1:7530b7eb757a 47 uint16_t i=0;
china_sn0w 1:7530b7eb757a 48 uint8_t uart_val = 0;
china_sn0w 1:7530b7eb757a 49 uint16_t _pbuff_len = 0;
china_sn0w 1:7530b7eb757a 50
china_sn0w 1:7530b7eb757a 51 while(1) {
china_sn0w 1:7530b7eb757a 52 ret = microUSB.buf.dequeue(&uart_val);
china_sn0w 1:7530b7eb757a 53 if(ret == 1) {
china_sn0w 1:7530b7eb757a 54 if(g_cmd.valid_flag == 0) { //None Header
china_sn0w 1:7530b7eb757a 55 if(uart_val == 0x5A) { //Already find header
china_sn0w 1:7530b7eb757a 56 g_cmd.valid_flag = 1;
china_sn0w 1:7530b7eb757a 57 i=0;
china_sn0w 1:7530b7eb757a 58 g_cmd.buf[i++] = uart_val;
china_sn0w 1:7530b7eb757a 59 } else {
china_sn0w 1:7530b7eb757a 60 i=0;
china_sn0w 1:7530b7eb757a 61 }
china_sn0w 1:7530b7eb757a 62 } else if(g_cmd.valid_flag == 1) { //已经找到了帧头
china_sn0w 1:7530b7eb757a 63 if(i < CMD_BUF_LEN) {
china_sn0w 1:7530b7eb757a 64 g_cmd.buf[i++] = uart_val;
china_sn0w 1:7530b7eb757a 65 if(i>=4) { //根据协议长度区判断接收的协议帧大小
china_sn0w 1:7530b7eb757a 66 _pbuff_len = ((g_cmd.buf[3]&0x00ff)<<8) + g_cmd.buf[2];
china_sn0w 1:7530b7eb757a 67 if(i == (_pbuff_len+5)) { //一包结束
china_sn0w 1:7530b7eb757a 68 g_cmd.package_len = i;
china_sn0w 1:7530b7eb757a 69 DealUart1OnePackage(g_cmd.buf, g_cmd.package_len);
china_sn0w 1:7530b7eb757a 70 i = 0;
china_sn0w 1:7530b7eb757a 71 g_cmd.valid_flag = 0;
china_sn0w 1:7530b7eb757a 72 continue;
china_sn0w 1:7530b7eb757a 73 } else if((_pbuff_len+5)>=CMD_BUF_LEN) {
china_sn0w 1:7530b7eb757a 74 i = 0;
china_sn0w 1:7530b7eb757a 75 g_cmd.valid_flag = 0;
china_sn0w 1:7530b7eb757a 76 continue;
china_sn0w 1:7530b7eb757a 77 }
china_sn0w 1:7530b7eb757a 78 }
china_sn0w 1:7530b7eb757a 79 } else {
china_sn0w 1:7530b7eb757a 80 i = 0;
china_sn0w 1:7530b7eb757a 81 g_cmd.valid_flag = 0;;
china_sn0w 1:7530b7eb757a 82 }
china_sn0w 1:7530b7eb757a 83 }
china_sn0w 1:7530b7eb757a 84 } else {
china_sn0w 1:7530b7eb757a 85 i = 0;
china_sn0w 1:7530b7eb757a 86 g_cmd.valid_flag = 0;
china_sn0w 1:7530b7eb757a 87 wait_ms(50);
china_sn0w 1:7530b7eb757a 88 //osDelay(50);
china_sn0w 1:7530b7eb757a 89 }
china_sn0w 1:7530b7eb757a 90 }
china_sn0w 1:7530b7eb757a 91
china_sn0w 1:7530b7eb757a 92 }
china_sn0w 1:7530b7eb757a 93
china_sn0w 1:7530b7eb757a 94 uint8_t Check_Sum(uint8_t *_pbuff, uint16_t _cmdLen)
china_sn0w 1:7530b7eb757a 95 {
china_sn0w 1:7530b7eb757a 96 uint8_t cmd_sum=0;
china_sn0w 1:7530b7eb757a 97 uint16_t i;
china_sn0w 1:7530b7eb757a 98
china_sn0w 1:7530b7eb757a 99 for(i=0; i<_cmdLen; i++) {
china_sn0w 1:7530b7eb757a 100 cmd_sum += _pbuff[i];
china_sn0w 1:7530b7eb757a 101 }
china_sn0w 1:7530b7eb757a 102 cmd_sum = (~cmd_sum);
china_sn0w 1:7530b7eb757a 103
china_sn0w 1:7530b7eb757a 104 return cmd_sum;
china_sn0w 1:7530b7eb757a 105 }
china_sn0w 1:7530b7eb757a 106
china_sn0w 1:7530b7eb757a 107 //协议相关命令的发送
china_sn0w 1:7530b7eb757a 108 void UART_CmdAckSend(uint8_t ack_cmd, uint8_t register_addr, uint8_t *pbuff, uint16_t pbuff_len)
china_sn0w 1:7530b7eb757a 109 {
china_sn0w 1:7530b7eb757a 110 while(g_cmd.send_flag == 1) {
china_sn0w 1:7530b7eb757a 111 wait_ms(50);
china_sn0w 1:7530b7eb757a 112 }
china_sn0w 1:7530b7eb757a 113
china_sn0w 1:7530b7eb757a 114 g_cmd.send_flag = 1;
china_sn0w 1:7530b7eb757a 115
china_sn0w 1:7530b7eb757a 116 UserTxBufferFS[0] = 0x5a;
china_sn0w 1:7530b7eb757a 117 UserTxBufferFS[1] = 0x00;
china_sn0w 1:7530b7eb757a 118 uint16_t i=1, j=0;
china_sn0w 1:7530b7eb757a 119 uint16_t _uart_pbuff_len = 0;
china_sn0w 1:7530b7eb757a 120
china_sn0w 1:7530b7eb757a 121 _uart_pbuff_len = pbuff_len+1;
china_sn0w 1:7530b7eb757a 122 UserTxBufferFS[i++]= ack_cmd;
china_sn0w 1:7530b7eb757a 123 UserTxBufferFS[i++]= (_uart_pbuff_len&0x00ff);
china_sn0w 1:7530b7eb757a 124 UserTxBufferFS[i++]= (_uart_pbuff_len>>8);
china_sn0w 1:7530b7eb757a 125 UserTxBufferFS[i++]= register_addr;
china_sn0w 1:7530b7eb757a 126 for(j=0; j<pbuff_len; j++) {
china_sn0w 1:7530b7eb757a 127 UserTxBufferFS[i++] = pbuff[j];
china_sn0w 1:7530b7eb757a 128 }
china_sn0w 1:7530b7eb757a 129 UserTxBufferFS[i++] = Check_Sum(&UserTxBufferFS[1], _uart_pbuff_len+3);
china_sn0w 1:7530b7eb757a 130
china_sn0w 1:7530b7eb757a 131 uint16_t k = 0;
china_sn0w 1:7530b7eb757a 132
china_sn0w 1:7530b7eb757a 133 while(i > 64) {
china_sn0w 1:7530b7eb757a 134 microUSB.writeBlock(&UserTxBufferFS[k], 64);
china_sn0w 1:7530b7eb757a 135 i -= 64;
china_sn0w 1:7530b7eb757a 136 k += 64;
china_sn0w 1:7530b7eb757a 137 }
china_sn0w 1:7530b7eb757a 138 microUSB.writeBlock(&UserTxBufferFS[k], i);
china_sn0w 1:7530b7eb757a 139 g_cmd.send_flag = 0;
china_sn0w 1:7530b7eb757a 140 }
china_sn0w 1:7530b7eb757a 141
china_sn0w 1:7530b7eb757a 142
china_sn0w 1:7530b7eb757a 143 void DealUart1OnePackage(uint8_t *pd, uint16_t cmdLen)
china_sn0w 1:7530b7eb757a 144 {
china_sn0w 1:7530b7eb757a 145 uint16_t _pd_len = 0;
china_sn0w 1:7530b7eb757a 146
china_sn0w 1:7530b7eb757a 147 _pd_len = ((pd[3]&0x00ff)<<8) + pd[2];
china_sn0w 1:7530b7eb757a 148
china_sn0w 1:7530b7eb757a 149 if(pd[_pd_len+4] != Check_Sum(&pd[1],_pd_len+3)) { //校验不正确,直接返回
china_sn0w 1:7530b7eb757a 150 return;
china_sn0w 1:7530b7eb757a 151 }
china_sn0w 1:7530b7eb757a 152
china_sn0w 1:7530b7eb757a 153 switch(pd[4]) {
china_sn0w 1:7530b7eb757a 154 case REGISTER_CMD: //寄存器操作
china_sn0w 1:7530b7eb757a 155 if(pd[1] == 0x00) { //读
china_sn0w 1:7530b7eb757a 156 //rLED = LED_ON;
china_sn0w 1:7530b7eb757a 157 if(HandleReadReg() == 0)
china_sn0w 1:7530b7eb757a 158 UART_CmdAckSend(READ_CMD | 0x80, REGISTER_CMD, _uart_send_pbuff, DUT_REG_NUM*2);
china_sn0w 1:7530b7eb757a 159 else
china_sn0w 1:7530b7eb757a 160 UART_CmdAckSend(READ_CMD | 0x10, REGISTER_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 161 } else if(pd[1] == 0x01) { //写
china_sn0w 1:7530b7eb757a 162 //rLED = LED_OFF;
china_sn0w 1:7530b7eb757a 163 if(HandleWriteReg(pd, cmdLen) == 0)
china_sn0w 1:7530b7eb757a 164 UART_CmdAckSend(WRITE_CMD | 0x80, REGISTER_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 165 else
china_sn0w 1:7530b7eb757a 166 UART_CmdAckSend(WRITE_CMD | 0x10, REGISTER_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 167 }
china_sn0w 1:7530b7eb757a 168 break;
china_sn0w 1:7530b7eb757a 169
china_sn0w 1:7530b7eb757a 170 case VAN_SINGLE_MEAS:// 单次测量
china_sn0w 1:7530b7eb757a 171 if(HandleOneTimeMeasure() == 0)
china_sn0w 4:217334c3a5b2 172 UART_CmdAckSend(READ_CMD | 0x80, VAN_SINGLE_MEAS, _uart_send_pbuff, 14);
china_sn0w 1:7530b7eb757a 173 else
china_sn0w 1:7530b7eb757a 174 UART_CmdAckSend(READ_CMD | 0x10, VAN_SINGLE_MEAS, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 175 break;
china_sn0w 1:7530b7eb757a 176
china_sn0w 1:7530b7eb757a 177 case VAN_CONTIU_MEAS:// 连续测量
china_sn0w 1:7530b7eb757a 178 if(HandleContinuousMeasure(pd) == 0)
china_sn0w 1:7530b7eb757a 179 UART_CmdAckSend(READ_CMD | 0x80, VAN_CONTIU_MEAS, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 180 else
china_sn0w 1:7530b7eb757a 181 UART_CmdAckSend(READ_CMD | 0x10, VAN_CONTIU_MEAS, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 182 break;
china_sn0w 1:7530b7eb757a 183
china_sn0w 1:7530b7eb757a 184 case VAN_READ_HIST_CMD://读取Histogram
china_sn0w 1:7530b7eb757a 185 if(pd[5] == 0xF0)
china_sn0w 1:7530b7eb757a 186 {
china_sn0w 1:7530b7eb757a 187 WriteOneReg(REG_SYS_CFG, sys_cfg_save);
china_sn0w 1:7530b7eb757a 188 histogram_mode = 0;
china_sn0w 1:7530b7eb757a 189 }
china_sn0w 1:7530b7eb757a 190 else if(pd[5] == 0xF1)
china_sn0w 1:7530b7eb757a 191 {
china_sn0w 1:7530b7eb757a 192 ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
china_sn0w 1:7530b7eb757a 193 WriteOneReg(REG_SYS_CFG, 0x01);
china_sn0w 1:7530b7eb757a 194 histogram_mode = 2;
china_sn0w 1:7530b7eb757a 195 }
china_sn0w 1:7530b7eb757a 196 else if(pd[5] == 0xF2)
china_sn0w 1:7530b7eb757a 197 {
china_sn0w 1:7530b7eb757a 198 ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
china_sn0w 1:7530b7eb757a 199 WriteOneReg(REG_SYS_CFG, 0x01);
china_sn0w 1:7530b7eb757a 200 histogram_mode = 4;
china_sn0w 1:7530b7eb757a 201 }
china_sn0w 1:7530b7eb757a 202 else
china_sn0w 1:7530b7eb757a 203 {
china_sn0w 1:7530b7eb757a 204 ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
china_sn0w 1:7530b7eb757a 205 WriteOneReg(REG_SYS_CFG, 0x01);
china_sn0w 1:7530b7eb757a 206 histogram_mode = 1;
china_sn0w 1:7530b7eb757a 207 histogram_tdc = pd[5];
china_sn0w 1:7530b7eb757a 208 }
china_sn0w 1:7530b7eb757a 209
china_sn0w 1:7530b7eb757a 210 break;
china_sn0w 1:7530b7eb757a 211 case VAN_STEP_HISTOGRAM_CMD:
china_sn0w 1:7530b7eb757a 212 if(pd[5] == 0xF0) {
china_sn0w 1:7530b7eb757a 213 WriteOneReg(REG_SYS_CFG, sys_cfg_save);
china_sn0w 1:7530b7eb757a 214 histogram_mode = 0;
china_sn0w 1:7530b7eb757a 215 } else if(pd[5] == 0xF1) {
china_sn0w 1:7530b7eb757a 216 ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
china_sn0w 1:7530b7eb757a 217 WriteOneReg(REG_SYS_CFG, 0x01);
china_sn0w 1:7530b7eb757a 218 histogram_mode = 3;
china_sn0w 1:7530b7eb757a 219 histogram_pos_num = pd[6] + pd[7]*256;
china_sn0w 1:7530b7eb757a 220 histogram_per_pos = pd[8] + pd[9]*256;
china_sn0w 1:7530b7eb757a 221 }
china_sn0w 3:35b05d91568d 222 else if(pd[5] == 0xF2)
china_sn0w 3:35b05d91568d 223 {
china_sn0w 3:35b05d91568d 224 histogram_mode = 5;
china_sn0w 3:35b05d91568d 225 range_step_num = pd[6] + pd[7]*256;
china_sn0w 3:35b05d91568d 226 range_per_step = 0;
china_sn0w 3:35b05d91568d 227 //histogram_per_pos = pd[8] + pd[9]*256;
china_sn0w 3:35b05d91568d 228 }
china_sn0w 1:7530b7eb757a 229 break;
china_sn0w 1:7530b7eb757a 230
china_sn0w 1:7530b7eb757a 231 case VAN_MOVING_CTL_CMD:
china_sn0w 1:7530b7eb757a 232 ServoRun(pd[5], pd[6] + pd[7]*256);
china_sn0w 1:7530b7eb757a 233 break;
china_sn0w 1:7530b7eb757a 234
china_sn0w 1:7530b7eb757a 235 case VAN_DOWN_FW_CMD:
china_sn0w 1:7530b7eb757a 236 if(HandleDownloadFW(pd, cmdLen) == 0)
china_sn0w 1:7530b7eb757a 237 UART_CmdAckSend(WRITE_CMD | 0x80, VAN_DOWN_FW_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 238 else
china_sn0w 1:7530b7eb757a 239 UART_CmdAckSend(WRITE_CMD | 0x10, VAN_DOWN_FW_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 240 break;
china_sn0w 1:7530b7eb757a 241
china_sn0w 1:7530b7eb757a 242 case CHIP_RST_CMD:
china_sn0w 1:7530b7eb757a 243 HandleChipReset();
china_sn0w 1:7530b7eb757a 244 break;
china_sn0w 1:7530b7eb757a 245
china_sn0w 1:7530b7eb757a 246 case CHIP_EN_CMD:
china_sn0w 1:7530b7eb757a 247 //HandleChipEnable(pd[5]);
china_sn0w 1:7530b7eb757a 248 break;
china_sn0w 1:7530b7eb757a 249
china_sn0w 1:7530b7eb757a 250 case INT_ENABLE_CMD:
china_sn0w 1:7530b7eb757a 251 if(pd[5] == 1)
china_sn0w 4:217334c3a5b2 252 {
china_sn0w 4:217334c3a5b2 253 work_mode = 1;
china_sn0w 1:7530b7eb757a 254 chip_int.disable_irq();
china_sn0w 4:217334c3a5b2 255 WriteOneReg(0x3D, 0xC8);
china_sn0w 4:217334c3a5b2 256 //WriteOneReg(0x3E, 0x04);
china_sn0w 4:217334c3a5b2 257 TRIM_EN = 1;
china_sn0w 4:217334c3a5b2 258 }
china_sn0w 1:7530b7eb757a 259 break;
china_sn0w 1:7530b7eb757a 260
china_sn0w 1:7530b7eb757a 261 case VAN_DCR_TEST_CMD:
china_sn0w 1:7530b7eb757a 262 if(DCRTest(pd[5], pd[6]) == 0)
china_sn0w 1:7530b7eb757a 263 UART_CmdAckSend(READ_CMD | 0x80, VAN_DCR_TEST_CMD, (uint8_t*)dcr_matrix, 17*9*2);
china_sn0w 1:7530b7eb757a 264 else
china_sn0w 1:7530b7eb757a 265 UART_CmdAckSend(READ_CMD | 0x10, VAN_DCR_TEST_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 266 break;
china_sn0w 1:7530b7eb757a 267
china_sn0w 1:7530b7eb757a 268 case VAN_DELAYLINE_TEST_CMD:
china_sn0w 1:7530b7eb757a 269 if(DelayLineTest(pd[5], _uart_send_pbuff) == 0)
china_sn0w 1:7530b7eb757a 270 UART_CmdAckSend(READ_CMD | 0x80, VAN_DELAYLINE_TEST_CMD, _uart_send_pbuff, 144);
china_sn0w 1:7530b7eb757a 271 else
china_sn0w 1:7530b7eb757a 272 UART_CmdAckSend(READ_CMD | 0x10, VAN_DELAYLINE_TEST_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 273 break;
china_sn0w 1:7530b7eb757a 274
china_sn0w 1:7530b7eb757a 275 case VAN_GET_TDC_PHASE_CMD:
china_sn0w 1:7530b7eb757a 276 if(GetTdcPhase(_uart_send_pbuff) == 0)
china_sn0w 1:7530b7eb757a 277 UART_CmdAckSend(READ_CMD | 0x80, VAN_GET_TDC_PHASE_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 278 else
china_sn0w 1:7530b7eb757a 279 UART_CmdAckSend(READ_CMD | 0x10, VAN_GET_TDC_PHASE_CMD, _uart_send_pbuff, 2);
china_sn0w 1:7530b7eb757a 280 break;
china_sn0w 3:35b05d91568d 281
china_sn0w 3:35b05d91568d 282 case VAN_SET_WINDOW_CMD:
china_sn0w 3:35b05d91568d 283 if(SetWindow(pd) == 0)
china_sn0w 3:35b05d91568d 284 UART_CmdAckSend(WRITE_CMD | 0x80, VAN_SET_WINDOW_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 285 else
china_sn0w 3:35b05d91568d 286 UART_CmdAckSend(WRITE_CMD | 0x10, VAN_SET_WINDOW_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 287 break;
china_sn0w 3:35b05d91568d 288
china_sn0w 3:35b05d91568d 289 case VAN_RCO_TRIM_CMD:
china_sn0w 3:35b05d91568d 290 if(RCO_Trim(_uart_send_pbuff) == 0)
china_sn0w 3:35b05d91568d 291 {
china_sn0w 4:217334c3a5b2 292 WriteOneReg(0x37, _uart_send_pbuff[0]);
china_sn0w 4:217334c3a5b2 293 WriteOneReg(0x3E, 0x40);
china_sn0w 4:217334c3a5b2 294 WriteOneReg(0x3F, 0x8C);
china_sn0w 3:35b05d91568d 295 _uart_send_pbuff[1] = _uart_send_pbuff[0];
china_sn0w 3:35b05d91568d 296 _uart_send_pbuff[0] = 0;
china_sn0w 3:35b05d91568d 297 UART_CmdAckSend(READ_CMD | 0x80, VAN_RCO_TRIM_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 298 }
china_sn0w 3:35b05d91568d 299 else
china_sn0w 3:35b05d91568d 300 {
china_sn0w 3:35b05d91568d 301 _uart_send_pbuff[1] = _uart_send_pbuff[0];
china_sn0w 3:35b05d91568d 302 _uart_send_pbuff[0] = 1;
china_sn0w 3:35b05d91568d 303 UART_CmdAckSend(READ_CMD | 0x10, VAN_RCO_TRIM_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 304 }
china_sn0w 3:35b05d91568d 305 break;
china_sn0w 3:35b05d91568d 306
china_sn0w 3:35b05d91568d 307 case VAN_BVD_TRIM_CMD:
china_sn0w 3:35b05d91568d 308 if(BVD_Trim(_uart_send_pbuff) == 0)
china_sn0w 3:35b05d91568d 309 {
china_sn0w 3:35b05d91568d 310 _uart_send_pbuff[1] = _uart_send_pbuff[0];
china_sn0w 3:35b05d91568d 311 _uart_send_pbuff[0] = 0;
china_sn0w 3:35b05d91568d 312 UART_CmdAckSend(READ_CMD | 0x80, VAN_BVD_TRIM_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 313 }
china_sn0w 3:35b05d91568d 314 else
china_sn0w 3:35b05d91568d 315 {
china_sn0w 3:35b05d91568d 316 _uart_send_pbuff[1] = _uart_send_pbuff[0];
china_sn0w 3:35b05d91568d 317 _uart_send_pbuff[0] = 0;
china_sn0w 3:35b05d91568d 318 UART_CmdAckSend(READ_CMD | 0x10, VAN_BVD_TRIM_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 319 }
china_sn0w 3:35b05d91568d 320 break;
china_sn0w 3:35b05d91568d 321
china_sn0w 3:35b05d91568d 322 case VAN_PIXEL_EN_CMD:
china_sn0w 3:35b05d91568d 323 if(Pixel_Enable(_uart_send_pbuff) == 0)
china_sn0w 3:35b05d91568d 324 UART_CmdAckSend(READ_CMD | 0x80, VAN_PIXEL_EN_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 325 else
china_sn0w 3:35b05d91568d 326 UART_CmdAckSend(READ_CMD | 0x10, VAN_PIXEL_EN_CMD, _uart_send_pbuff, 2);
china_sn0w 3:35b05d91568d 327 break;
china_sn0w 4:217334c3a5b2 328
china_sn0w 4:217334c3a5b2 329 case VAN_MP_SLECTE_CMD:
china_sn0w 4:217334c3a5b2 330 if(MP_Slect(_uart_send_pbuff) == 0)
china_sn0w 4:217334c3a5b2 331 UART_CmdAckSend(READ_CMD | 0x80, VAN_MP_SLECTE_CMD, _uart_send_pbuff, 129);
china_sn0w 4:217334c3a5b2 332 else
china_sn0w 4:217334c3a5b2 333 UART_CmdAckSend(READ_CMD | 0x10, VAN_MP_SLECTE_CMD, _uart_send_pbuff, 2);
china_sn0w 4:217334c3a5b2 334
china_sn0w 4:217334c3a5b2 335 break;
china_sn0w 4:217334c3a5b2 336
china_sn0w 4:217334c3a5b2 337 case VAN_OTP_WRITE_CMD:
china_sn0w 4:217334c3a5b2 338 if(OTP_Write(&pd[5]) == 0)
china_sn0w 4:217334c3a5b2 339 UART_CmdAckSend(READ_CMD | 0x80, VAN_OTP_WRITE_CMD, _uart_send_pbuff, 2);
china_sn0w 4:217334c3a5b2 340 else
china_sn0w 4:217334c3a5b2 341 UART_CmdAckSend(READ_CMD | 0x10, VAN_OTP_WRITE_CMD, _uart_send_pbuff, 2);
china_sn0w 4:217334c3a5b2 342 break;
china_sn0w 4:217334c3a5b2 343
china_sn0w 4:217334c3a5b2 344 case VAN_OTP_READ_CMD:
china_sn0w 4:217334c3a5b2 345 if(OTP_Read(&pd[5], _uart_send_pbuff) == 0)
china_sn0w 4:217334c3a5b2 346 UART_CmdAckSend(READ_CMD | 0x80, VAN_OTP_READ_CMD, _uart_send_pbuff, pd[6]+2);
china_sn0w 4:217334c3a5b2 347 else
china_sn0w 4:217334c3a5b2 348 UART_CmdAckSend(READ_CMD | 0x10, VAN_OTP_READ_CMD, _uart_send_pbuff, 2);
china_sn0w 4:217334c3a5b2 349 break;
china_sn0w 4:217334c3a5b2 350
china_sn0w 1:7530b7eb757a 351
china_sn0w 1:7530b7eb757a 352 default:
china_sn0w 1:7530b7eb757a 353
china_sn0w 1:7530b7eb757a 354 break;
china_sn0w 1:7530b7eb757a 355 }
china_sn0w 1:7530b7eb757a 356
china_sn0w 1:7530b7eb757a 357 }
china_sn0w 1:7530b7eb757a 358
china_sn0w 1:7530b7eb757a 359
china_sn0w 1:7530b7eb757a 360 uint8_t HandleWriteReg(uint8_t *pd, uint16_t cmdLen)
china_sn0w 1:7530b7eb757a 361 {
china_sn0w 1:7530b7eb757a 362 uint16_t _pd_len = 0;
china_sn0w 1:7530b7eb757a 363 uint16_t reg_num = 0;
china_sn0w 1:7530b7eb757a 364
china_sn0w 1:7530b7eb757a 365 _pd_len = ((pd[3]&0x00ff)<<8) + pd[2];
china_sn0w 1:7530b7eb757a 366
china_sn0w 1:7530b7eb757a 367 reg_num = (_pd_len - 1)/2;
china_sn0w 1:7530b7eb757a 368
china_sn0w 1:7530b7eb757a 369 for(uint16_t i = 0; i < reg_num; i++) {
china_sn0w 1:7530b7eb757a 370 //dut_reg[pd[5+2*i]].value = pd[5+2*i+1];
china_sn0w 1:7530b7eb757a 371 WriteOneReg(pd[5+2*i], pd[5+2*i+1]);
china_sn0w 1:7530b7eb757a 372 }
china_sn0w 1:7530b7eb757a 373
china_sn0w 1:7530b7eb757a 374 return 0;
china_sn0w 1:7530b7eb757a 375 }
china_sn0w 1:7530b7eb757a 376
china_sn0w 1:7530b7eb757a 377 uint8_t HandleReadReg(void)
china_sn0w 1:7530b7eb757a 378 {
china_sn0w 1:7530b7eb757a 379 uint8_t ret = 0;
china_sn0w 1:7530b7eb757a 380
china_sn0w 1:7530b7eb757a 381 ret = ReadAllRegToTable();
china_sn0w 1:7530b7eb757a 382 if(ret == 0) {
china_sn0w 1:7530b7eb757a 383 memcpy(_uart_send_pbuff, &dut_reg, DUT_REG_NUM*2);
china_sn0w 1:7530b7eb757a 384 }
china_sn0w 1:7530b7eb757a 385
china_sn0w 1:7530b7eb757a 386 return ret;
china_sn0w 1:7530b7eb757a 387 }
china_sn0w 1:7530b7eb757a 388
china_sn0w 1:7530b7eb757a 389 uint8_t HandleOneTimeMeasure(void)
china_sn0w 1:7530b7eb757a 390 {
china_sn0w 4:217334c3a5b2 391 return OneTimeMeasure((uint16_t*)_uart_send_pbuff, (uint16_t*)(_uart_send_pbuff + 2), (uint32_t*)(_uart_send_pbuff + 4), (uint16_t*)(_uart_send_pbuff + 8), (uint16_t*)(_uart_send_pbuff + 10), (uint16_t*)(_uart_send_pbuff + 12));
china_sn0w 1:7530b7eb757a 392 }
china_sn0w 1:7530b7eb757a 393
china_sn0w 1:7530b7eb757a 394 uint8_t HandleContinuousMeasure(uint8_t *pd)
china_sn0w 4:217334c3a5b2 395 {
china_sn0w 1:7530b7eb757a 396 if(pd[5] == 0x00)
china_sn0w 4:217334c3a5b2 397 {
china_sn0w 1:7530b7eb757a 398 return StopContinuousMeasure();
china_sn0w 4:217334c3a5b2 399 }
china_sn0w 1:7530b7eb757a 400 else
china_sn0w 4:217334c3a5b2 401 {
china_sn0w 1:7530b7eb757a 402 return ContinuousMeasure();
china_sn0w 4:217334c3a5b2 403 }
china_sn0w 1:7530b7eb757a 404 }
china_sn0w 1:7530b7eb757a 405
china_sn0w 1:7530b7eb757a 406 uint8_t HandleReadHistogram(uint8_t tdc_idx)
china_sn0w 1:7530b7eb757a 407 {
china_sn0w 1:7530b7eb757a 408 _uart_send_pbuff[0] = tdc_idx;
china_sn0w 4:217334c3a5b2 409
china_sn0w 4:217334c3a5b2 410 if(tdc_idx == 0xF1)
china_sn0w 4:217334c3a5b2 411 memcpy(&_uart_send_pbuff[1], histogram[0], 1024);
china_sn0w 4:217334c3a5b2 412 else
china_sn0w 4:217334c3a5b2 413 memcpy(&_uart_send_pbuff[1], histogram[tdc_idx], 1024);
china_sn0w 1:7530b7eb757a 414
china_sn0w 4:217334c3a5b2 415
china_sn0w 1:7530b7eb757a 416
china_sn0w 1:7530b7eb757a 417 UART_CmdAckSend(READ_CMD | 0x80, VAN_READ_HIST_CMD, _uart_send_pbuff, 1025);
china_sn0w 1:7530b7eb757a 418 return 0;
china_sn0w 1:7530b7eb757a 419 }
china_sn0w 1:7530b7eb757a 420
china_sn0w 1:7530b7eb757a 421 uint8_t HandleDownloadFW(uint8_t *pd, uint16_t cmdLen)
china_sn0w 1:7530b7eb757a 422 {
china_sn0w 1:7530b7eb757a 423 static uint8_t pkg_num = 0;
china_sn0w 1:7530b7eb757a 424 uint8_t ret = 0;
china_sn0w 1:7530b7eb757a 425 uint8_t pkg_idx = pd[5];
china_sn0w 1:7530b7eb757a 426 uint16_t pkg_len = ((pd[3]&0x00ff)<<8) + pd[2] - 2;
china_sn0w 1:7530b7eb757a 427 static uint16_t pkg_saved = 0;
china_sn0w 1:7530b7eb757a 428
china_sn0w 1:7530b7eb757a 429 if(pkg_idx == 0) { //第一包固件
china_sn0w 1:7530b7eb757a 430 printf("Fist Pkg of FW arrived.\n");
china_sn0w 1:7530b7eb757a 431 memset(Firmware, 0x00, 8192);
china_sn0w 1:7530b7eb757a 432 pkg_num = 0;
china_sn0w 1:7530b7eb757a 433 pkg_saved = 0;
china_sn0w 1:7530b7eb757a 434
china_sn0w 1:7530b7eb757a 435 memcpy(Firmware + pkg_saved, &pd[6], pkg_len);
china_sn0w 1:7530b7eb757a 436 pkg_num++;
china_sn0w 1:7530b7eb757a 437 pkg_saved += pkg_len;
china_sn0w 1:7530b7eb757a 438 } else if(pkg_idx == 0xF0) {
china_sn0w 1:7530b7eb757a 439 printf("Last Pkg of FW arrived. Begin to Download to Chip.\n");
china_sn0w 1:7530b7eb757a 440 //DebugArray(Firmware, pkg_saved);//For Debug
china_sn0w 1:7530b7eb757a 441 ret = WriteFW(pkg_saved);
china_sn0w 1:7530b7eb757a 442 } else if(pkg_idx == 0xF1) {
china_sn0w 1:7530b7eb757a 443 printf("Firmware transmission has been halt.\n");
china_sn0w 1:7530b7eb757a 444 memset(Firmware, 0x00, 8192);
china_sn0w 1:7530b7eb757a 445 pkg_num = 0;
china_sn0w 1:7530b7eb757a 446 pkg_saved = 0;
china_sn0w 1:7530b7eb757a 447 } else {
china_sn0w 1:7530b7eb757a 448 memcpy(Firmware + pkg_saved, &pd[6], pkg_len);
china_sn0w 1:7530b7eb757a 449 pkg_num++;
china_sn0w 1:7530b7eb757a 450 pkg_saved += pkg_len;
china_sn0w 1:7530b7eb757a 451 }
china_sn0w 1:7530b7eb757a 452
china_sn0w 1:7530b7eb757a 453 if(ret == 0) {
china_sn0w 1:7530b7eb757a 454 printf("Firmware download successfully.\n");
china_sn0w 1:7530b7eb757a 455 }
china_sn0w 1:7530b7eb757a 456
china_sn0w 1:7530b7eb757a 457 _uart_send_pbuff[0] = pkg_idx;
china_sn0w 1:7530b7eb757a 458
china_sn0w 1:7530b7eb757a 459 return ret;
china_sn0w 1:7530b7eb757a 460 }
china_sn0w 1:7530b7eb757a 461
china_sn0w 4:217334c3a5b2 462 uint8_t HandleContinuousMeasureReport(uint16_t lsb, uint16_t milimeter, uint32_t peak, uint16_t noise_level, uint16_t ref_lsb, uint16_t ref_milimeter)
china_sn0w 1:7530b7eb757a 463 {
china_sn0w 1:7530b7eb757a 464 memcpy(_uart_send_pbuff, &lsb, 2);
china_sn0w 1:7530b7eb757a 465 memcpy(_uart_send_pbuff + 2, &milimeter, 2);
china_sn0w 3:35b05d91568d 466
china_sn0w 3:35b05d91568d 467 memcpy(_uart_send_pbuff + 4, &peak, 4);
china_sn0w 3:35b05d91568d 468 memcpy(_uart_send_pbuff + 8, &noise_level, 2);
china_sn0w 3:35b05d91568d 469
china_sn0w 4:217334c3a5b2 470 memcpy(_uart_send_pbuff + 10, &ref_lsb, 2);
china_sn0w 4:217334c3a5b2 471 memcpy(_uart_send_pbuff + 12, &ref_milimeter, 2);
china_sn0w 4:217334c3a5b2 472
china_sn0w 1:7530b7eb757a 473
china_sn0w 4:217334c3a5b2 474 UART_CmdAckSend(READ_CMD | 0x80, VAN_CONTIU_MEAS, _uart_send_pbuff, 14);
china_sn0w 1:7530b7eb757a 475
china_sn0w 1:7530b7eb757a 476 return 0;
china_sn0w 1:7530b7eb757a 477 }
china_sn0w 1:7530b7eb757a 478
china_sn0w 1:7530b7eb757a 479 void HandleChipReset(void)
china_sn0w 1:7530b7eb757a 480 {
china_sn0w 4:217334c3a5b2 481 ChipInitReset();
china_sn0w 1:7530b7eb757a 482 }
china_sn0w 1:7530b7eb757a 483
china_sn0w 1:7530b7eb757a 484 void HandleFreqReport(float* V_I_Value)
china_sn0w 1:7530b7eb757a 485 {
china_sn0w 1:7530b7eb757a 486 uint32_t temp_byte = 0;
china_sn0w 1:7530b7eb757a 487
china_sn0w 1:7530b7eb757a 488 _uart_send_pbuff[0] = 0;
china_sn0w 1:7530b7eb757a 489 _uart_send_pbuff[1] = 0;
china_sn0w 1:7530b7eb757a 490 memset(&_uart_send_pbuff[2], 0x00, 11);
china_sn0w 1:7530b7eb757a 491
china_sn0w 1:7530b7eb757a 492 for(uint8_t i = 0; i < 10; i++) {
china_sn0w 1:7530b7eb757a 493 temp_byte = V_I_Value[i];
china_sn0w 1:7530b7eb757a 494 memcpy(_uart_send_pbuff + 13 + 4*i, &temp_byte, 4);
china_sn0w 1:7530b7eb757a 495 }
china_sn0w 1:7530b7eb757a 496
china_sn0w 1:7530b7eb757a 497 UART_CmdAckSend(READ_CMD | 0x80, FREQ_REPORT, _uart_send_pbuff, 53);
china_sn0w 1:7530b7eb757a 498 }