Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. /*******************************************************
  2. * USB-ISS test application for Linux. *
  3. * *
  4. * Sets USB-ISS into spi mode and reads and writes from *
  5. * microchips 23k256 *
  6. * *
  7. * Compiled using gcc, tested on Ubunto 10.4 LTS. *
  8. * *
  9. * By James Henderson, 2012. *
  10. *******************************************************/
  11.  
  12. //
  13. // Credit goes to the above person for this short but awesome program.
  14. // I modified it slightly to read the information string from the
  15. // Alphasense OPC-N2 sensor.
  16. //
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <termios.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27.  
  28. void display_version(void);
  29. void set_spi_mode(void);
  30. void read_data(void);
  31.  
  32. void wait_comseq() {
  33. struct timespec msec10;
  34. msec10.tv_sec = 0;
  35. msec10.tv_nsec = 10000000;
  36. nanosleep(&msec10, NULL);
  37. }
  38.  
  39. int fd;
  40. int error = 0;
  41. unsigned char sbuf[64]; // serial buffer for r/w
  42.  
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46. printf("******************************\n");
  47. printf("USB ISS linux test application\n");
  48. printf("By James Henderson, 2012\n");
  49. printf("******************************\n\n");
  50.  
  51. if(argc != 2)
  52. printf("Incorrect input!\n");
  53. else
  54. {
  55. struct termios defaults; // to store innitial default port settings
  56. struct termios config; // These will be our new settings
  57. const char *device = "/dev/ttyACM0";
  58. fd = open(device, O_RDWR | O_NOCTTY);
  59. if(fd == -1) {
  60. printf( "failed to open port\n" );
  61. } else {
  62. if (tcgetattr(fd, &defaults) < 0) perror("tcgetattr"); // Grab snapshot of current settings for port
  63. cfmakeraw(&config); // make options for raw data
  64. if (tcsetattr(fd, TCSANOW, &config) < 0) perror("tcsetattr config"); // Set options for port
  65.  
  66. display_version();
  67. set_spi_mode();
  68.  
  69. if (!error) read_data();
  70.  
  71. if (tcsetattr(fd, TCSANOW, &defaults) < 0) perror("tcsetattr defaults"); // Restore port default before closing
  72. }
  73. close(fd);
  74. }
  75. return 0;
  76. }
  77.  
  78. void display_version(void)
  79. {
  80. sbuf[0] = 0x5A; // USB_ISS byte
  81. sbuf[1] = 0x01; // Software return byte
  82.  
  83. if (write(fd, sbuf, 2) < 0) perror("display_version write"); // Write data to USB-ISS
  84. if (tcdrain(fd) < 0) perror("display_version tcdrain");
  85. if (read(fd, sbuf, 3) < 0) perror("display_version read"); // Read data back from USB-ISS, module ID and software version
  86.  
  87. printf("USB-ISS Module ID: %u \n", sbuf[0]);
  88. printf("USB-ISS Software v: %u \n\n", sbuf[1]);
  89. }
  90.  
  91. void set_spi_mode(void)
  92. {
  93. sbuf[0] = 0x5A;
  94. sbuf[1] = 0x02; // Set mode command
  95. sbuf[2] = 0x92; // Set SPI mode
  96. sbuf[3] = 0x0B; // 500KHz clock speed
  97.  
  98. if (write(fd, sbuf, 4) < 0) perror("set_spi_mode write"); // Write data to USB-ISS
  99. if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain");
  100. if (read(fd, sbuf, 2) < 0) perror("set_spi_mode read"); // Read back error byte
  101. if(sbuf[0] != 0xFF) // If first returned byte is not 0xFF then an error has occured
  102. {
  103. printf("**set_spi_mode: Error setting spi mode!** 0x%02x\n\n", sbuf[0]);
  104. error = 0xFF; // Set error byte
  105. }
  106. }
  107.  
  108.  
  109. void read_data(void)
  110. {
  111.  
  112. for (int i=0;i<64;i++) {
  113. sbuf[i] = 0;
  114. }
  115.  
  116. sbuf[0] = 0x61;
  117. sbuf[1] = 0x3F;
  118. sbuf[2] = 0x00;
  119. sbuf[3] = 0x00;
  120. sbuf[4] = 0x00;
  121.  
  122.  
  123.  
  124. if (write(fd, sbuf, 2) < 0) perror("set_spi_mode write"); // Write data to USB-ISS
  125. if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain");
  126. // wait_comseq();
  127. if (read(fd, sbuf, 2) < 0) perror("set_spi_mode read"); // Read back error byte
  128.  
  129. sbuf[0] = 0x61;
  130. sbuf[1] = 0x3F;
  131. wait_comseq();
  132. // if (write(fd, sbuf, 2) < 0) perror("set_spi_mode write"); // Write data to USB-ISS
  133. // if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain");
  134. if (write(fd, sbuf, 61) < 0) perror("set_spi_mode write"); // Write data to USB-ISS
  135. if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain");
  136. // wait_comseq();
  137. if (read(fd, &sbuf, 61) < 0) perror("set_spi_mode read"); // Read back error byte
  138.  
  139. for (int i=0;i<61;i++) {
  140. // if (read(fd, &sbuf[i], 1) < 0) perror("set_spi_mode read"); // Read back error byte
  141. printf("%02x ", sbuf[i]);
  142. }
  143. printf("\n");
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement