uart_loopback.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "rp_hw.h"
  14. int main(int argc, char *argv[]){
  15. char *buffer = "TEST string";
  16. char rx_buf[255];
  17. memset(rx_buf,0,255);
  18. int size = 255;
  19. int res = rp_UartInit(); // init uart api
  20. printf("Init result: %d\n",res);
  21. res = rp_UartSetTimeout(10); // set timeout in 1/10 sec. 10 = 1 sec
  22. printf("Set timeout: %d\n",res);
  23. res = rp_UartSetSpeed(115200); // set uart speed
  24. printf("Set speed: %d\n",res);
  25. res = rp_UartSetBits(RP_UART_CS8); // set word size
  26. printf("Set CS8: %d\n",res);
  27. res = rp_UartSetStopBits(RP_UART_STOP2); // set stop bits
  28. printf("Set Stop Bits 2: %d\n",res);
  29. res = rp_UartSetParityMode(RP_UART_MARK); // set parity
  30. printf("Set Parity Mode: %d\n",res);
  31. res = rp_UartSetSettings(); // apply settings to uart
  32. printf("Set settings: %d\n",res);
  33. res = rp_UartWrite((unsigned char*)buffer,strlen(buffer)); // write buffer to uart
  34. printf("Write result: %d\n",res);
  35. res = rp_UartRead((unsigned char*)rx_buf,&size); // read from uart
  36. printf("Read result: %d\n",res);
  37. printf("Size: %d (%s)\n",size,rx_buf);
  38. res = rp_UartRelease(); // close uart api
  39. printf("UnInit result: %d\n",res);
  40. return 0;
  41. }