uart.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* @brief This is a simple application for testing UART communication on a RedPitaya
  2. * @Author Luka Golinar <luka.golinar@redpitaya.com>
  3. *
  4. * (c) Red Pitaya http://www.redpitaya.com
  5. *
  6. * This part of code is written in C programming language.
  7. * Please visit http://en.wikipedia.org/wiki/C_(programming_language)
  8. * for more details on the language used herein.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h> //Used for UART
  14. #include <fcntl.h> //Used for UART
  15. #include <termios.h> //Used for UART
  16. #include <errno.h>
  17. /* Inline function definition */
  18. static int uart_init();
  19. static int release();
  20. static int uart_read(int size);
  21. static int uart_write();
  22. /* File descriptor definition */
  23. int uart_fd = -1;
  24. static int uart_init(){
  25. uart_fd = open("/dev/ttyPS1", O_RDWR | O_NOCTTY | O_NDELAY);
  26. if(uart_fd == -1){
  27. fprintf(stderr, "Failed to open uart.\n");
  28. return -1;
  29. }
  30. struct termios settings;
  31. tcgetattr(uart_fd, &settings);
  32. /* CONFIGURE THE UART
  33. * The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
  34. * Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
  35. * CSIZE:- CS5, CS6, CS7, CS8
  36. * CLOCAL - Ignore modem status lines
  37. * CREAD - Enable receiver
  38. * IGNPAR = Ignore characters with parity errors
  39. * ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
  40. * PARENB - Parity enable
  41. * PARODD - Odd parity (else even) */
  42. /* Set baud rate - default set to 9600Hz */
  43. speed_t baud_rate = B9600;
  44. /* Baud rate fuctions
  45. * cfsetospeed - Set output speed
  46. * cfsetispeed - Set input speed
  47. * cfsetspeed - Set both output and input speed */
  48. cfsetspeed(&settings, baud_rate);
  49. settings.c_cflag &= ~PARENB; /* no parity */
  50. settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
  51. settings.c_cflag &= ~CSIZE;
  52. settings.c_cflag |= CS8 | CLOCAL; /* 8 bits */
  53. settings.c_lflag = ICANON; /* canonical mode */
  54. settings.c_oflag &= ~OPOST; /* raw output */
  55. /* Setting attributes */
  56. tcflush(uart_fd, TCIFLUSH);
  57. tcsetattr(uart_fd, TCSANOW, &settings);
  58. return 0;
  59. }
  60. static int uart_read(int size){
  61. /* Read some sample data from RX UART */
  62. /* Don't block serial read */
  63. fcntl(uart_fd, F_SETFL, FNDELAY);
  64. while(1){
  65. if(uart_fd == -1){
  66. fprintf(stderr, "Failed to read from UART.\n");
  67. return -1;
  68. }
  69. unsigned char rx_buffer[size];
  70. int rx_length = read(uart_fd, (void*)rx_buffer, size);
  71. if (rx_length < 0){
  72. /* No data yet avaliable, check again */
  73. if(errno == EAGAIN){
  74. fprintf(stderr, "AGAIN!\n");
  75. continue;
  76. /* Error differs */
  77. }else{
  78. fprintf(stderr, "Error!\n");
  79. return -1;
  80. }
  81. }else if (rx_length == 0){
  82. fprintf(stderr, "No data waiting\n");
  83. /* Print data and exit while loop */
  84. }else{
  85. rx_buffer[rx_length] = '\0';
  86. printf("%i bytes read : %s\n", rx_length, rx_buffer);
  87. break;
  88. }
  89. }
  90. return 0;
  91. }
  92. static int uart_write(char *data){
  93. /* Write some sample data into UART */
  94. /* ----- TX BYTES ----- */
  95. int msg_len = strlen(data);
  96. int count = 0;
  97. char tx_buffer[msg_len+1];
  98. strncpy(tx_buffer, data, msg_len);
  99. tx_buffer[msg_len++] = 0x0a; //New line numerical value
  100. if(uart_fd != -1){
  101. count = write(uart_fd, &tx_buffer, (msg_len));
  102. }
  103. if(count < 0){
  104. fprintf(stderr, "UART TX error.\n");
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. static int release(){
  110. tcflush(uart_fd, TCIFLUSH);
  111. close(uart_fd);
  112. return 0;
  113. }
  114. int main(int argc, char *argv[]){
  115. char *data = "HELLO WOLRD!";
  116. /* uart init */
  117. if(uart_init() < 0){
  118. printf("Uart init error.\n");
  119. return -1;
  120. }
  121. /* Sample write */
  122. if(uart_write(data) < 0){
  123. printf("Uart write error\n");
  124. return -1;
  125. }
  126. /* Sample read */
  127. if(uart_read(strlen(data)) < 0){
  128. printf("Uart read error\n");
  129. return -1;
  130. }
  131. /* CLOSING UART */
  132. release();
  133. return 0;
  134. }