lizhanwei
2020-02-11 e0e6f7cf7ebf2a6fbfea13eb0743f5e95b1dc60c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Created by YY on 2019/12/21.
//
 
#include <cstring>
#include <pthread.h>
#include "mcu_if.h"
#include "../common/apptimer.h"
#include "../utils/crc16.h"
#include "../defs.h"
#include "../jni_log.h"
#include "../common/serial_port.h"
 
#define DEBUG(fmt, args...)     LOGD("<mcu_if> <%s>: " fmt, __func__, ##args)
 
#define MCU_UART            UART_1
 
enum parse_status_t {
    SYNC_HEAD_ONE,
    SYNC_HEAD_TWO,
    GET_ID_HI,
    GET_ID_LO,
    GET_LENGTH_HI,
    GET_LENGTH_LO,
    GET_PAYLOAD,
    GET_CRC16_HI,
    GET_CRC16_LO
};
 
#define ID_CM_APP_BOOT          0x0001
#define ID_MC_MCU_BOOT          0x8001
#define ID_CM_INQ_MCU_VER       0x0002
#define ID_MC_MCU_VER           0x8002
#define ID_CM_RW_INFO           0x0003
#define ID_MC_RW_INFO_RSP       0x8003
#define ID_CM_MCU_DFU_REQ       0x0004
#define ID_MC_MCU_DFU_RSP       0x8004
#define ID_CM_MCU_DFU_DATA       0x0005
#define ID_CM_MCU_DFU_DATA_CMP  0x0006
#define ID_MC_MCU_DFU_DATA_RSP  0x8006
#define ID_MC_CAR_INFO           0x8007
#define ID_MC_RTK_DATA          0x8008
#define ID_CM_RTK_DATA            0x0008
#define ID_CM_READ_RFCARD         0x0009
#define ID_MC_RFCARD_RSP        0x8009
 
static parse_status_t parse_status;
 
static struct {
    uint16_t id;
    uint16_t length;
    uint16_t rx_len;
    uint8_t buffer[4096 + 4];
    uint16_t crc16;
}McuPkt;
 
static void *UartThread1(void *p);
static void ParseMcuTimeout(union sigval sig);
static void McuCommandEntry(uint16_t id, const uint8_t *data, int lenth);
 
void ParseMcuInit(void)
{
    parse_status = SYNC_HEAD_ONE;
    AppTimer_delete(ParseMcuTimeout);
 
    SendMcuCommand(ID_CM_APP_BOOT, NULL, 0);
}
 
#define PARSE_BUFF_SIZE         4096
 
static void *UartThread1(void *p) {
    struct serial_config *cfg = (struct serial_config *) p;
 
    int res = InitSerialPort(MCU_UART, cfg->baud, cfg->data_bit, cfg->verify_bit, cfg->stop_bit, cfg->flow_ctrl);
    DEBUG("Serial %s open %d", cfg->name, res);
    uint8_t RxBuf[PARSE_BUFF_SIZE];
    int RxBufLen = 0;
 
    if (res == 0)
        ParseMcuInit();
 
    while (res == 0) {
        int ul = ReadSerialPort(MCU_UART, (uint8_t *)RxBuf + RxBufLen, sizeof(RxBuf) - RxBufLen);
        RxBufLen += ul;
 
        /*{
            static char buffd[16384];
 
            buffd[0] = 0;
            int i = 0;
            for (i = 0; i < ul; i++) {
                if ((i % 32) == 0) {
                    sprintf(buffd + strlen(buffd), "\n");
                }
                sprintf(buffd + strlen(buffd), "%02X ", RxBuf[i]);
                if (strlen(buffd) > 800) {
                    DEBUG("%s <- %s...", "UART", buffd);
                    buffd[0] = 0;
                }
            }
            if (strlen(buffd) > 0)
                DEBUG("%s <- %s", "UART", buffd);
        }*/
 
        if (RxBufLen > 0) {
            DEBUG("RECV LEN %d", RxBufLen);
            ParseMcu(RxBuf, RxBufLen);
            RxBufLen = 0;
        }
    }
    if (res == 0) {
        UninitSerialPort(MCU_UART);
    }
    pthread_exit(NULL);
}
 
void ParseMcu(const uint8_t *data, int length)
{
    int x = 0;
    while (x < length) {
        uint8_t c = data[x];
        switch (parse_status) {
            case SYNC_HEAD_ONE:
                if (c == 0x55) {
                    parse_status = SYNC_HEAD_TWO;
                    AppTimer_delete(ParseMcuTimeout);
                    AppTimer_add(ParseMcuTimeout, D_SEC(5));
                }
                x++;
                break;
            case SYNC_HEAD_TWO:
                if (c == 0xAA) {
                    parse_status = GET_ID_HI;
                } else if (c != 0x55) {
                    parse_status = SYNC_HEAD_ONE;
                }
                x++;
                break;
            case GET_ID_HI:
                McuPkt.id = c;
                parse_status = GET_ID_LO;
                x++;
                break;
            case GET_ID_LO:
                McuPkt.id <<= 8;
                McuPkt.id += c;
                parse_status = GET_LENGTH_HI;
                x++;
                break;
            case GET_LENGTH_HI:
                McuPkt.rx_len = 0;
                McuPkt.length = c;
                parse_status = GET_LENGTH_LO;
                x++;
                break;
            case GET_LENGTH_LO:
                McuPkt.length <<= 8;
                McuPkt.length += c;
                parse_status = GET_PAYLOAD;
 
                if (McuPkt.length >= 1500) {
                    DEBUG("Pkt Too large!");
                    parse_status = SYNC_HEAD_ONE;
                    AppTimer_delete(ParseMcuTimeout);
                }
 
                McuPkt.buffer[0] = HI_UINT16(McuPkt.id);
                McuPkt.buffer[1] = LO_UINT16(McuPkt.id);
                McuPkt.buffer[2] = HI_UINT16(McuPkt.length);
                McuPkt.buffer[3] = LO_UINT16(McuPkt.length);
                x++;
                break;
            case GET_PAYLOAD:
                if (length - x >= McuPkt.length - McuPkt.rx_len) {
                    memcpy(McuPkt.buffer + 4 + McuPkt.rx_len, data + x, McuPkt.length - McuPkt.rx_len);
                    x += McuPkt.length - McuPkt.rx_len;
                    McuPkt.rx_len = McuPkt.length;
                    parse_status = GET_CRC16_HI;
                } else {
                    memcpy(McuPkt.buffer + 4 + McuPkt.rx_len, data + x, length - x);
                    McuPkt.rx_len += length - x;
                    x = length;
                }
                break;
            case GET_CRC16_HI:
                McuPkt.crc16 = c;
                parse_status = GET_CRC16_LO;
                x++;
                break;
            case GET_CRC16_LO: {
                McuPkt.crc16 <<= 8;
                McuPkt.crc16 += c;
 
                uint16_t crc16 = CRCCCITT(McuPkt.buffer, McuPkt.length + 4, 0, 0);
 
                DEBUG("mcuif crc16 but 0x%04X exp 0x%04X", McuPkt.crc16, crc16);
 
                if (McuPkt.crc16 == crc16) {
                    McuCommandEntry(McuPkt.id, McuPkt.buffer + 4, McuPkt.length);
                }
 
                parse_status = SYNC_HEAD_ONE;
                AppTimer_delete(ParseMcuTimeout);
                x++;
                break;
            }
            default:
                break;
        }
    }
}
 
void SendMcuCommand(uint16_t id, const uint8_t *data, int length)
{
    uint8_t buffer[2048];
    int x = 0;
 
    buffer[x++] = 0x55;
    buffer[x++] = 0xAA;
    buffer[x++] = HI_UINT16(id);
    buffer[x++] = LO_UINT16(id);
    buffer[x++] = HI_UINT16(length);
    buffer[x++] = LO_UINT16(length);
 
    if (data != NULL && length > 0) {
        memcpy(buffer + x, data, length);
        x += length;
    }
 
    uint16_t crc16 =  CRCCCITT(buffer + 2, length + 4, 0, 0);
    buffer[x++] = HI_UINT16(crc16);
    buffer[x++] = LO_UINT16(crc16);
 
    WriteSerialPort(MCU_UART, buffer, x);
}
 
void ConfigMCU(void)
{
    // TODO
    static struct serial_config serialConfig;
 
    strcpy(serialConfig.name, "/dev/ttyHSL1");
    serialConfig.baud = 115200;
    serialConfig.data_bit = 8;
    serialConfig.verify_bit = 'N';
    serialConfig.stop_bit = 1;
    serialConfig.flow_ctrl = 0;
 
    pthread_t pid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//detached
    pthread_create(&pid, &attr, UartThread1, &serialConfig);
}
 
void SendRtkToMcu(const uint8_t *data, int length)
{
    SendMcuCommand(ID_CM_RTK_DATA, data, length);
}
 
static void ParseMcuTimeout(union sigval sig) {
    AppTimer_delete(ParseMcuTimeout);
    parse_status = SYNC_HEAD_ONE;
}
 
static void sendrtk(union sigval sig) {
    uint8_t data[486];
 
    memset(data, 0x86, sizeof(data));
 
    SendMcuCommand(ID_CM_RTK_DATA, data, sizeof(data));
 
    SendMcuCommand(ID_CM_READ_RFCARD, NULL, 0);
 
    AppTimer_delete(sendrtk);
    AppTimer_add(sendrtk, D_SEC(1));
}
 
static void McuCommandEntry(uint16_t id, const uint8_t *data, int lenth)
{
    static int ii = 0;
 
    switch (id) {
        case ID_MC_MCU_BOOT:
            DEBUG("MCU BOOT");
            break;
        case ID_MC_MCU_VER:
            DEBUG("MCU VER");
            break;
        case ID_MC_RW_INFO_RSP:
            break;
        case ID_MC_MCU_DFU_RSP:
            break;
        case ID_MC_MCU_DFU_DATA_RSP:
            break;
        case ID_MC_CAR_INFO:
            DEBUG("ID_MC_CAR_INFO");
            break;
        case ID_MC_RTK_DATA:
            DEBUG("ID_MC_RTK_DATA");
            break;
        case ID_MC_RFCARD_RSP:
            DEBUG("ID_MC_RFCARD_RSP");
            break;
        default:
            break;
    }
}