You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.8 KiB

4 years ago
  1. /*
  2. * Created on: Aug 5, 2019
  3. * Author: Cristian Fatu
  4. * This project implements a basic UART communication over UART on
  5. * Petalinux, unsing Uart Lite linux driver. The hardware platform implements an
  6. * UART Lite IP core and the device tree (in pl.dtsi) sets the appropriate
  7. * parameters. After booting linux, the "/dev/ttyUL1" must be present.
  8. */
  9. #include <fcntl.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <termios.h>
  15. #include <unistd.h>
  16. #include "uart.h"
  17. #define MAX_RECV_SIZE 235
  18. void openuart(struct UartDevice *device, char *devname, int rate) {
  19. device->name = devname;
  20. device->rate = rate;
  21. printf("UART open %s\n", device->name);
  22. int rc = uartStart(device, 0);
  23. if (rc) {
  24. printf("uartStart %s fail\n", device->name);
  25. exit(-1);
  26. }
  27. }
  28. int main(int argc, char *argv[]) {
  29. struct UartDevice dev = {0};
  30. // struct UartDevice dev2 = {0};
  31. if (argc != 2) {
  32. printf("Usage:%s dev1\n", argv[0]);
  33. // printf("Demo:%s /dev/ttyUSB0 /dev/ttyUSB1\n", argv[0]);
  34. exit(-1);
  35. }
  36. openuart(&dev, argv[1], B9600);
  37. // openuart(&dev, argv[1], B115200);
  38. // openuart(&dev2, argv[2], B9600);
  39. char buf[1024] = {0};
  40. memset(buf, 0x55, 1024);
  41. for (size_t i = 0;; i++) {
  42. int ret1, ret2;
  43. ret1 = uartReceive(&dev, buf, 2/*接收的字节数2-1*/); // send the received text over UART
  44. if (ret1 != 0) {
  45. // printf("receive %d %x characters %d\n", ret1, buf[0], i);
  46. printf("%x\n", buf[0]);
  47. if (buf[0] != 0x12 && buf[0] != 0x34 && buf[0] != 0x56 &&
  48. buf[0] != 0x78 && buf[0] != 0x9a) {
  49. printf("....................................................%x\n", buf[0]);
  50. }
  51. }
  52. // ret2 = uartSend(&dev2, buf, 1); // send the received text over UART
  53. // usleep(1*1000);
  54. }
  55. while (1) {
  56. sleep(1);
  57. }
  58. return 0;
  59. }