1 /*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <pthread.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <termios.h>
26 #include <poll.h>
27 #include "hisignalling.h"
28
29 #define MSG(args...) printf(args)
30 pthread_t hisignallingMsgHandleID;
31 #define HISIGNALLING_HEAD_1 (0xaa)
32 #define HISIGNALLING_HEAD_2 (0x55)
33
34 /*
35 * @berf 用于将指定编号的引脚导出,作为GPIO使用
36 * @param pin:指定导出的引脚
37 *
38 * @berf It is used to export the pin with the specified number and use it as GPIO
39 * @param pin: Specify the exported pin
40 */
GpioExport(int pin)41 static int GpioExport(int pin)
42 {
43 char buffer[64] = {0};
44 int len = -1;
45 int fd = -1;
46
47 fd = open("/sys/class/gpio/export", O_WRONLY);
48 if (fd < 0) {
49 MSG("Failed to open export for writing!\n");
50 close(fd);
51 return -1;
52 }
53
54 len = snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, "%d", pin);
55 if (len < 0) {
56 MSG("printf msg failed\r\n");
57 }
58 if (write(fd, buffer, len) < 0) {
59 MSG("Failed to export gpio!");
60 close(fd);
61 return -1;
62 }
63
64 close(fd);
65 return 0;
66 }
67
68 /*
69 * @berf 用于将导出的GPIO删除掉
70 * @param pin:指定删除的引脚
71 *
72 * @berf Used to delete the exported GPIO
73 * @param pin: Specifies the pin to delete
74 */
GpioUnexport(int pin)75 static int GpioUnexport(int pin)
76 {
77 char buffer[64] = {0};
78 int len = -1;
79 int fd = -1;
80
81 fd = open("/sys/class/gpio/unexport", O_WRONLY);
82 if (fd < 0) {
83 MSG("Failed to open unexport for writing!\n");
84 close(fd);
85 return -1;
86 }
87
88 len = snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, "%d", pin);
89 if (len < 0) {
90 MSG("printf msg failed\r\n");
91 }
92 if (write(fd, buffer, len) < 0) {
93 MSG("Failed to unexport gpio!");
94 close(fd);
95 return -1;
96 }
97
98 close(fd);
99 return 0;
100 }
101
102 /*
103 * direction设置输出还是输入模式,0-->IN为输入,1-->OUT为输出
104 * direction sets the output or input mode, 0-->IN is input, 1-->OUT is output
105 */
GpioDirection(int pin,int dir)106 static int GpioDirection(int pin, int dir)
107 {
108 static const char dirStr[] = "in\0out";
109 char path[64] = {0};
110 int fd = -1;
111
112 int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/direction", pin);
113 if (len < 0) {
114 MSG("printf msg failed\r\n");
115 }
116 fd = open(path, O_WRONLY);
117 if (fd < 0) {
118 MSG("Failed to open gpio direction for writing!\n");
119 close(fd);
120 return -1;
121 }
122
123 if (write(fd, &dirStr[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0) { /* 3, 2, 3 gpio register */
124 MSG("Failed to set direction!\n");
125 close(fd);
126 return -1;
127 }
128
129 close(fd);
130 return 0;
131 }
132
133 /*
134 * @brief 设置输出值
135 * @param pin: 引脚
136 * @param value: 输出值
137 *
138 * @brief set output value
139 * @param pin: pin
140 * @param value: output value
141 */
GpioWrite(int pin,int value)142 static int GpioWrite(int pin, int value)
143 {
144 static const char valuesStr[] = "01";
145 char path[64] = {0};
146 int fd = -1;
147
148 int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/value", pin);
149 if (len < 0) {
150 MSG("printf Msg failed\r\n");
151 }
152 fd = open(path, O_WRONLY);
153 if (fd < 0) {
154 MSG("Failed to open gpio value for writing!\n");
155 close(fd);
156 return -1;
157 }
158
159 if (write(fd, &valuesStr[value == 0 ? 0 : 1], 1) < 0) { /* 1, 1 gpio register */
160 MSG("Failed to write value!\n");
161 close(fd);
162 return -1;
163 }
164
165 close(fd);
166 return 0;
167 }
168
169 /*
170 * @brief 读取输入值
171 * @param pin: 引脚
172 *
173 * @brief reads the input value
174 * @param pin: pin
175 */
GpioRead(int pin)176 static int GpioRead(int pin)
177 {
178 char path[64] = {0};
179 char value_str[3] = {0};
180 int fd = -1;
181
182 int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/value", pin);
183 if (len < 0) {
184 MSG("printf msg failed\r\n");
185 }
186 fd = open(path, O_RDONLY);
187 if (fd < 0) {
188 MSG("Failed to open gpio value for reading!\n");
189 close(fd);
190 return -1;
191 }
192
193 if (read(fd, value_str, 3) < 0) { /* 3: gpio register */
194 MSG("Failed to read value!\n");
195 close(fd);
196 return -1;
197 }
198
199 close(fd);
200 return (atoi(value_str));
201 }
202
203 /*
204 * none表示引脚为输入,不是中断引脚
205 * rising表示引脚为中断输入,上升沿触发
206 * falling表示引脚为中断输入,下降沿触发
207 * both表示引脚为中断输入,边沿触发
208 *
209 * none indicates that the pin is an input, not an interrupt pin
210 * rising indicates that the pin is an interrupt input and is triggered by a rising edge
211 * falling means that the pin is an interrupt input, triggered by a falling edge
212 * both indicates that the pin is an interrupt input, edge-triggered
213 * 0-->none, 1-->rising, 2-->falling, 3-->both
214 */
GpioEdge(int pin,int edge)215 static int GpioEdge(int pin, int edge)
216 {
217 const char dirStr[] = "none\0rising\0falling\0both";
218 char ptr = 0;
219 char path[64] = {0};
220 int fd = -1;
221
222 switch (edge) {
223 case EAGE_0:
224 ptr = 0;
225 break;
226 case EAGE_1:
227 ptr = 5; /* 5: gpio register */
228 break;
229 case EAGE_2:
230 ptr = 12; /* 12: gpio register */
231 break;
232 case EAGE_3:
233 ptr = 20; /* 20: gpio register */
234 break;
235 default:
236 ptr = 0;
237 }
238 int len = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/sys/class/gpio/gpio%d/edge", pin);
239 if (len < 0) {
240 MSG("printf Msg failed\r\n");
241 }
242
243 fd = open(path, O_WRONLY);
244 if (fd < 0) {
245 MSG("Failed to open gpio edge for writing!\n");
246 close(fd);
247 return -1;
248 }
249
250 if (write(fd, &dirStr[ptr], strlen(&dirStr[ptr])) < 0) {
251 MSG("Failed to set edge!\n");
252 close(fd);
253 return -1;
254 }
255
256 close(fd);
257 return 0;
258 }
259
260 /*
261 * 初始化按键1
262 * Initialize button 1
263 */
InitGpio1(void)264 void InitGpio1(void)
265 {
266 MSG("\n =============== InitGpio1 start ========== \n");
267 GpioUnexport(1); /* 1: gpio pin */
268 GpioExport(1); /* 1: gpio pin */
269 GpioDirection(1, 0); /* 1, 0: gpio pin */
270 GpioEdge(1, 2); /* 1, 2: gpio pin */
271 MSG("\n =============== InitGpio1 end ========== \n");
272 }
273
274 /*
275 * 初始化按键2
276 * Initialize button 2
277 */
InitGpio2(void)278 void InitGpio2(void)
279 {
280 MSG("\n =============== InitGpio2 start ========== \n");
281 GpioUnexport(2); /* 2: gpio pin */
282 GpioExport(2); /* 2: gpio pin */
283 GpioDirection(2, 0); /* 2, 0: gpio pin */
284 GpioEdge(2, 2); /* 2, 2: gpio pin */
285 MSG("\n =============== InitGpio2 end ========== \n");
286 }
287
288 /*
289 * 串口设置
290 * Serial port settings
291 */
Uart1Config(int fd)292 int Uart1Config(int fd)
293 {
294 struct termios newtio = {0}, oldtio = {0};
295 /*
296 * 获取原有串口配置
297 * Get the original serial port configuration
298 */
299 if (tcgetattr(fd, &oldtio) != 0) {
300 perror("SetupSerial 1");
301 return -1;
302 }
303 (void)memset_s(&newtio, sizeof(newtio), 0, sizeof(newtio));
304 /*
305 * CREAD开启串行数据接收,CLOCAL打开本地连接模式
306 * CREAD opens serial data reception, CLOCAL opens local connection mode
307 */
308 newtio.c_cflag |= CLOCAL | CREAD;
309
310 /*
311 * 设置数据位
312 * set data bit
313 */
314 newtio.c_cflag &= ~CSIZE;
315 newtio.c_cflag |= CS8;
316 /*
317 * 设置奇偶校验位
318 * set parity bit
319 */
320 newtio.c_cflag &= ~PARENB; // 无奇偶校验
321 /*
322 * 设置波特率 115200
323 * set baud rate 115200
324 */
325 cfsetispeed(&newtio, B115200);
326 cfsetospeed(&newtio, B115200);
327
328 /*
329 * 设置停止位
330 * set stop bit
331 */
332 newtio.c_cflag &= ~CSTOPB; /* 默认为一位停止位 */
333 /*
334 * 设置最少字符和等待时间,对于接收字符和等待时间没有特别的要求时
335 *
336 * Set the minimum characters and waiting time,
337 * when there are no special requirements for receiving characters and waiting time
338 */
339 newtio.c_cc[VTIME] = 0; /* 非规范模式读取时的超时时间 */
340 newtio.c_cc[VMIN] = 0; /* 非规范模式读取时的最小字符数 */
341 /*
342 * tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来
343 *
344 * tcflush clears the unfinished input/output requests and data of the terminal;
345 * TCIFLUSH means clearing the data being received and not reading it out
346 */
347 tcflush(fd, TCIFLUSH);
348 if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
349 perror("com set error");
350 return -1;
351 }
352 return 0;
353 }
354
355 /*
356 * @berf uart 发送数据
357 * @param int fd: 串口文件描述符
358 * @param void *buf:发送数据buffer
359 * @param int len:数据缓冲长度
360 *
361 * @berf uart send
362 * @param int fd: uart file descriptor
363 * @param void *buf:send data buf
364 * @param int len:data buf len
365 */
UartSend(int fd,char * buf,int len)366 int UartSend(int fd, char *buf, int len)
367 {
368 int ret = 0;
369 int count = 0;
370 char *sendBuf = buf;
371 int sendLen = len;
372
373 tcflush(fd, TCIFLUSH);
374
375 while (sendLen > 0) {
376 ret = write(fd, (char*)sendBuf + count, sendLen);
377 if (ret < 1) {
378 printf("write data below 1 byte % d\r\n", ret);
379 break;
380 }
381 count += ret;
382 sendLen = sendLen - ret;
383 }
384
385 return count;
386 }
387
388 /*
389 * @berf uart 读取数据
390 * @param int uart_fd: 串口文件描述符
391 * @param void *buf: 读取数据buffer
392 * @param int len: 数据缓冲区长度
393 * @param int timeoutMs: 读取数据时间
394 *
395 * @berf uart read
396 * @param int uart_fd: uart file descriptor
397 * @param void *buf: read data buf
398 * @param int len: data buf len
399 * @param int timeoutMs: read data time
400 */
UartRead(int uartFd,char * buf,int len,int timeoutMs)401 int UartRead(int uartFd, char *buf, int len, int timeoutMs)
402 {
403 int ret = 0;
404 size_t rsum = 0;
405 ret = 0;
406 fd_set rset;
407 struct timeval time;
408 int timeout = timeoutMs;
409 char *readBuf = buf;
410 int readLen = len;
411
412 while (rsum < readLen) {
413 time.tv_sec = timeout / 1000; /* 1000:转换成秒 */
414 time.tv_usec = (timeout - time.tv_sec * 1000) * 1000; /* 1000, 1000:转换为微秒 */
415 FD_ZERO(&rset);
416 FD_SET(uartFd, &rset);
417 ret = select(uartFd + 1, &rset, NULL, NULL, &time);
418 if (ret <= 0) {
419 if (ret == 0) {
420 printf("time over!\r\n");
421 return -1;
422 }
423 if (ret == -1) {
424 printf("select error!\r\n");
425 continue; // 信号中断
426 }
427 return -1;
428 } else {
429 ret = read(uartFd, (char *)readBuf + rsum, readLen - rsum);
430 if (ret < 0) {
431 printf("read data failed\r\n");
432 return ret;
433 } else {
434 rsum += ret;
435 }
436 }
437 }
438 return rsum;
439 }
440
441 /*
442 * crc32校验
443 * crc32 Verification implementation
444 */
445 static const unsigned int crc32table[] = {
446 };
447
448 /*
449 * @berf CRC校验
450 * @param const unsigned char *buf: 待校验数据buff
451 * @param unsigned int size: 待校验数据长度
452 *
453 * @berf CRC check
454 * @param const unsigned char *buf: Data to be verified buff
455 * @param unsigned int size: Data to be verified length
456 */
crc32(const unsigned char * buf,unsigned int size)457 static unsigned int crc32(const unsigned char *buf, unsigned int size)
458 {
459 unsigned int i, crc = 0xFFFFFFFF;
460
461 for (i = 0; i < size; i++) {
462 crc = crc32table[(crc ^ buf[i]) & 0xff] ^ (crc >> 8); /* 8: 右移8bit */
463 }
464 return crc ^ 0xFFFFFFFF;
465 }
466
467 #define RIGHT_MOVE_8_BIT (8)
468 #define RIGHT_MOVE_16_BIT (16)
469 #define RIGHT_MOVE_24_BIT (24)
470
471 /*
472 * 对数据进行打包
473 * Pack the data
474 */
HisignallingDataPackage(HisignallingProtocalType * buf,unsigned int len,unsigned char * hisignallingDataBuf)475 static unsigned int HisignallingDataPackage(HisignallingProtocalType *buf,
476 unsigned int len, unsigned char *hisignallingDataBuf)
477 {
478 unsigned int crcCheckSend = 0x1010;
479 unsigned int packageLen = 0;
480 unsigned int DataPackLen = len;
481
482 (void)memcpy_s(hisignallingDataBuf, HISGNALLING_MSG_FRAME_HEADER_LEN,
483 buf->frameHeader, HISGNALLING_MSG_FRAME_HEADER_LEN);
484 (void)memcpy_s(&hisignallingDataBuf[HISGNALLING_MSG_FRAME_HEADER_LEN],
485 DataPackLen, buf->hisignallingMsgBuf, DataPackLen);
486 (void)memcpy_s(&hisignallingDataBuf[HISGNALLING_MSG_FRAME_HEADER_LEN + DataPackLen],
487 HISIGNALLING_MSG_HEADER_LEN, &(buf->endOfFrame), HISIGNALLING_MSG_HEADER_LEN);
488 crcCheckSend = crc32(hisignallingDataBuf, (DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN));
489 hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN] =
490 (unsigned char)((crcCheckSend & 0xff000000) >> RIGHT_MOVE_24_BIT);
491 hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 1] = /* 1: addr offset */
492 (unsigned char)((crcCheckSend & 0x00ff0000) >> RIGHT_MOVE_16_BIT);
493 hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 2] = /* 2: addr offset */
494 (unsigned char)((crcCheckSend & 0x0000ff00) >> RIGHT_MOVE_8_BIT);
495 hisignallingDataBuf[DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 3] = /* 3: addr offset */
496 (unsigned char)crcCheckSend;
497 packageLen = DataPackLen + HISIGNALLING_MSG_HEADER_TAIL_LEN + 4; /* 4: crc check lenght */
498 return packageLen;
499 }
500
501 /*
502 * Hi3561DV300接收数据
503 * Hi3561DV300 message recevice
504 */
HisignallingMsgReceive(int fd,unsigned char * buf,unsigned int len)505 static HisignallingErrorType HisignallingMsgReceive(int fd, unsigned char *buf, unsigned int len)
506 {
507 unsigned int crcCheckReceived = 0;
508 int i = 0, readLen = 0;
509 unsigned int RecvLen = len;
510
511 /*
512 * Hi3516dv300 uart串口读数据
513 * Hi3516dv300 uart read data
514 */
515 readLen = UartRead(fd, buf, RecvLen, 1000); /* 1000 :time out */
516 if (readLen <= 0) {
517 printf("uart_read data failed\r\n");
518 return HISIGNALLING_RET_VAL_ERROR;
519 }
520 printf("read_len=%d\r\n", readLen);
521 /*
522 * 输出收到的数据
523 * output received data
524 */
525 for (i = 0; i < RecvLen; i++) {
526 printf("0x%x ", buf[i]);
527 }
528 printf("\r\n");
529
530 return HISIGNALLING_RET_VAL_CORRECT;
531 }
532
533 /*
534 * @berf hisignalling 协议发送消息
535 * @param void *buf: 发送数据缓冲区
536 * @param unsigned int data_len: 发送数据长度
537 *
538 * @berf hisignalling protocol send msg
539 * @param void *buf: send data buff
540 * @param unsigned int data_len: send data length
541 */
HisignallingMsgSend(int fd,char * buf,unsigned int dataLen)542 static unsigned int HisignallingMsgSend(int fd, char *buf, unsigned int dataLen)
543 {
544 unsigned int ret = 0;
545 HisignallingProtocalType hisignallingMsg = {0};
546 unsigned char hisignallingSendBuf[HISIGNALLING_MSG_BUFF_LEN] = {0};
547 unsigned int hisignallingPackageLen = 0;
548 unsigned int writeDataLen = 0;
549
550 hisignallingMsg.frameHeader[0]= 0xAA; /* Protocol head data 1 */
551 hisignallingMsg.frameHeader[1]= 0x55; /* Protocol head data 2 */
552 (void)memcpy_s(hisignallingMsg.hisignallingMsgBuf, dataLen, buf, dataLen);
553 hisignallingMsg.endOfFrame = 0xff; /* Protocol tail data */
554
555 hisignallingPackageLen = HisignallingDataPackage(&hisignallingMsg, dataLen, hisignallingSendBuf);
556 if (!hisignallingPackageLen) {
557 printf("hisignalling_data_package failed\r\n");
558 return -1;
559 }
560 if (*hisignallingSendBuf == 0) {
561 printf("hisignalling send buf is null!\r\n");
562 return -1;
563 }
564
565 ret = UartSend(fd, hisignallingSendBuf, hisignallingPackageLen);
566 if (ret < 0) {
567 printf("write data failed\r\n");
568 return -1;
569 }
570
571 return 0;
572 }
573
574 /*
575 * hisignalling消息句柄
576 * hisignalling message handle
577 */
HisignallingMsgHandle(char * param)578 void *HisignallingMsgHandle(char *param)
579 {
580 unsigned int err = 0;
581
582 while (1) {
583 printf("hisignalling_msg_handle\r\n");
584 usleep(HISGNALLING_FREE_TASK_TIME);
585 }
586 err = pthread_join(hisignallingMsgHandleID, NULL);
587 if (err != 0) {
588 printf("Failed to delete hisignalling msg task\r\n");
589 }
590 }
591
592 #define HISIGNALLING_TASK_STACK_SIZE (2048)
593
594 /*
595 * hisignalling消息任务
596 * hisignalling message task
597 */
HisignallingMsgTask(void)598 unsigned int HisignallingMsgTask(void)
599 {
600 unsigned int ret = 0;
601 int err = 0, stacksize = HISIGNALLING_TASK_STACK_SIZE;
602
603 pthread_attr_t hisignallingAttr = {0};
604 err = pthread_attr_init(&hisignallingAttr);
605 err = pthread_attr_setstacksize(&hisignallingAttr, stacksize);
606 ret = pthread_create(&hisignallingMsgHandleID, &hisignallingAttr, HisignallingMsgHandle, NULL);
607 if (ret != 0) {
608 printf("Failed to create hisignalling msg task\r\n");
609 return -1;
610 }
611 return 0;
612 }
613
UartProcess(int uartFd,int Gpio1Fd,int Gpio2Fd,struct pollfd fdS1,struct pollfd fdS2)614 static void UartProcess(int uartFd, int Gpio1Fd, int Gpio2Fd, struct pollfd fdS1, struct pollfd fdS2)
615 {
616 int ret = 0;
617 char buff[10] = {0};
618 unsigned char writeBuffer[4] = {0, 2, 0, 3};
619 unsigned char writeBuffer2[4] = {0, 2, 0, 4};
620 unsigned char writeBuffer3[4] = {0, 2, 0, 5};
621 unsigned char readBuff[16] = {0};
622
623 Uart1Config(uartFd);
624 while (1) {
625 /*
626 * 按键操作
627 * key operation
628 */
629 ret = read(Gpio1Fd, buff, 10); /* 10:read data lenght */
630 if (ret == -1) {
631 MSG("gpio1 read error\n");
632 }
633 ret = poll(&fdS1, 1, 0); /* 1: 监视一个文件描述符 */
634 if (ret == -1) {
635 MSG("gpio1 poll error\n");
636 }
637 if (fdS1.revents & POLLPRI) {
638 ret = lseek(Gpio1Fd, 0, SEEK_SET);
639 if (ret == -1) {
640 MSG("gpio1 lseek error\n");
641 }
642 MSG("sw2 Pressed \n");
643 /*
644 * 按键触发发送
645 * key trigger sending
646 */
647 HisignallingMsgSend(uartFd, writeBuffer2, sizeof(writeBuffer2) / sizeof(writeBuffer2[0]));
648 }
649 ret = read(Gpio2Fd, buff, 10); /* 10:read data lenght */
650 if (ret == -1) {
651 MSG("gpio2 read error\n");
652 }
653 ret = poll(&fdS2, 1, 0); /* 1: 监视一个文件描述符 */
654 if (ret == -1) {
655 MSG("gpio2 poll error\n");
656 }
657 if (fdS2.revents & POLLPRI) {
658 ret = lseek(Gpio2Fd, 0, SEEK_SET);
659 if (ret == -1) {
660 MSG("gpio1 lseek error\n");
661 }
662 MSG("sw1 Pressed \n");
663 /*
664 * 按键触发发送
665 * key trigger sending
666 */
667 #ifdef EXPANSION_BOARD
668 HisignallingMsgSend(uartFd, writeBuffer3, sizeof(writeBuffer3) / sizeof(writeBuffer3[0]));
669 #elif defined (ROBOT_BOARD)
670 HisignallingMsgSend(uartFd, writeBuffer, sizeof(writeBuffer) / sizeof(writeBuffer[0]));
671 #endif
672 }
673 /*
674 * 串口读操作
675 * Serial read operation
676 */
677 HisignallingMsgReceive(uartFd, readBuff, HISIGNALLING_MSG_MOTOR_ENGINE_LEN);
678 usleep(HISGNALLING_FREE_TASK_TIME);
679 }
680 }
681
UartOpenInit(void)682 unsigned int UartOpenInit(void)
683 {
684 int fd;
685 char *uart1 = "/dev/ttyAMA1";
686
687 if ((fd = open(uart1, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
688 printf("open %s is failed", uart1);
689 return -1;
690 } else {
691 Uart1Config(fd);
692 }
693 return fd;
694 }
695
UartSendRead(int fd,refuseClassification refuseType)696 void UartSendRead(int fd, refuseClassification refuseType)
697 {
698 /*
699 * 测试buffer
700 * Test buffer
701 */
702 unsigned char writeBuffer2[4] = {0, 2, 0, 1};
703 unsigned char writeBuffer3[4] = {0, 2, 0, 2};
704 unsigned char writeBuffer4[4] = {0, 2, 0, 3};
705 unsigned char writeBuffer5[4] = {0, 2, 0, 4};
706 unsigned char writeBuffer6[4] = {0, 2, 0, 5};
707 unsigned char writeBuffer7[4] = {0, 2, 0, 6};
708 unsigned char writeBuffer8[4] = {0, 2, 0, 7};
709 unsigned char writeBuffer9[4] = {0, 2, 0, 8};
710 unsigned char readBuff[16] = {0};
711
712 #ifdef EXPANSION_BOARD
713 switch (refuseType) {
714 case FistGesture:
715 HisignallingMsgSend(fd, writeBuffer2, sizeof(writeBuffer2)/sizeof(writeBuffer2[0]));
716 printf("send gesture status:FistGesture\r\n");
717 break;
718 case ForefingerGesture:
719 HisignallingMsgSend(fd, writeBuffer3, sizeof(writeBuffer3)/sizeof(writeBuffer3[0]));
720 printf("send gesture status:ForefingerGesture\r\n");
721 break;
722 case OkGesture:
723 HisignallingMsgSend(fd, writeBuffer4, sizeof(writeBuffer4)/sizeof(writeBuffer4[0]));
724 printf("send gesture status:OkGesture\r\n");
725 break;
726 case PalmGesture:
727 HisignallingMsgSend(fd, writeBuffer5, sizeof(writeBuffer5)/sizeof(writeBuffer5[0]));
728 printf("send gesture status:PalmGesture\r\n");
729 break;
730 case YesGesture:
731 HisignallingMsgSend(fd, writeBuffer6, sizeof(writeBuffer6)/sizeof(writeBuffer6[0]));
732 printf("send gesture status:YesGesture\r\n");
733 break;
734 case ForefingerAndThumbGesture:
735 HisignallingMsgSend(fd, writeBuffer7, sizeof(writeBuffer7)/sizeof(writeBuffer7[0]));
736 printf("send gesture status:ForefingerAndThumbGesture\r\n");
737 break;
738 case LittleFingerAndThumbGesture:
739 HisignallingMsgSend(fd, writeBuffer8, sizeof(writeBuffer8)/sizeof(writeBuffer8[0]));
740 printf("send gesture status:LittleFingerAndThumbGesture\r\n");
741 break;
742 case InvalidGesture:
743 HisignallingMsgSend(fd, writeBuffer9, sizeof(writeBuffer9)/sizeof(writeBuffer9[0]));
744 printf("send gesture status:InvalidGesture\r\n");
745 break;
746 }
747 #endif
748 /*
749 * 串口读操作
750 * Serial read operation
751 */
752 if (readBuff[0] == HISIGNALLING_HEAD_1 && readBuff[1] == HISIGNALLING_HEAD_2) {
753 HisignallingMsgReceive(fd, readBuff, HISIGNALLING_MSG_MOTOR_ENGINE_LEN);
754 }
755 }
756
AiUartTransmit(void)757 int AiUartTransmit(void)
758 {
759 /*
760 * 按键初始化定义
761 * Key initialization definition
762 */
763 int gpio1Fd = -1;
764 int gpio2Fd = -1;
765 int ret1 = -1;
766 int ret2 = -1;
767 struct pollfd fds1;
768 struct pollfd fds2;
769 /*
770 * 串口初始化定义
771 * Serial port initialization definition
772 */
773 int fd = 0;
774 char *uart1 = "/dev/ttyAMA1";
775
776 printf("hisignal task start\r\n");
777 /*
778 * 按键操作
779 * key operation
780 */
781 InitGpio1();
782 InitGpio2();
783 gpio1Fd = open("/sys/class/gpio/gpio1/value", O_RDONLY);
784 if (gpio1Fd < 0) {
785 MSG("Failed to open gpio1 !\n");
786 return -1;
787 }
788 fds1.fd = gpio1Fd;
789 fds1.events = POLLPRI;
790
791 gpio2Fd = open("/sys/class/gpio/gpio2/value", O_RDONLY);
792 if (gpio2Fd < 0) {
793 MSG("Failed to open gpio1 !\n");
794 return -1;
795 }
796 fds2.fd = gpio2Fd;
797 fds2.events = POLLPRI;
798 /*
799 * 串口读写
800 * Serial read and write
801 */
802 if ((fd = open(uart1, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
803 printf("open %s is failed", uart1);
804 return -1;
805 } else {
806 UartProcess(fd, gpio1Fd, gpio2Fd, fds1, fds2);
807 }
808 return 0;
809 }
810