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