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.

85 lines
2.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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. //
  17. #include <map>
  18. #include <set>
  19. #include <string>
  20. #include "uart.h"
  21. #define MAX_RECV_SIZE 235
  22. using namespace std;
  23. map<string, uint32_t> g_baundmap = {
  24. {"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
  25. {"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
  26. {"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
  27. {"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
  28. {"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
  29. {"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
  30. {"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
  31. {"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017},
  32. };
  33. void openuart(struct UartDevice *device, char *devname, int rate) {
  34. device->name = devname;
  35. device->rate = rate;
  36. printf("UART open %s\n", device->name);
  37. int rc = uartStart(device, 0);
  38. if (rc) {
  39. printf("uartStart %s fail\n", device->name);
  40. exit(-1);
  41. }
  42. }
  43. int main(int argc, char *argv[]) {
  44. struct UartDevice dev = {0};
  45. if (argc != 3) {
  46. printf("Usage: %s /dev/ttyUSB0 115200\n", argv[0]);
  47. return -1;
  48. }
  49. printf("device name:%s\n", argv[1]);
  50. printf("baundrate :%s\n", argv[2]);
  51. auto baundrate_find_result = g_baundmap.find(argv[2]);
  52. if (baundrate_find_result == g_baundmap.end()) {
  53. printf("unsupport baundrate\n");
  54. return -1;
  55. };
  56. openuart(&dev, argv[1], baundrate_find_result->second);
  57. char buf[1024] = {0};
  58. memset(buf, 0x55, 1024);
  59. for (size_t i = 0;;) {
  60. int ret1, ret2;
  61. ret1 = uartReceive(&dev, buf, 2 /*接收的字节数2-1*/); // send the received text over UART
  62. if (ret1 != 0) {
  63. printf("%x", buf[0]);
  64. i++;
  65. if (i % 20 == 0) {
  66. printf("\n");
  67. }
  68. }
  69. fflush(stdout);
  70. }
  71. while (1) {
  72. sleep(1);
  73. }
  74. return 0;
  75. }