TEST_CODE_ApplyTo2V1_API

Dependencies:   SDFileSystem max32630fthr USBDevice

Committer:
china_sn0w
Date:
Thu May 28 02:30:39 2020 +0000
Revision:
1:7530b7eb757a
Child:
3:35b05d91568d
V1.1

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