Created
October 4, 2015 15:13
-
-
Save johncant/72628935d4c8a57e34af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************* | |
* USB-ISS test application for Linux. * | |
* * | |
* Sets USB-ISS into spi mode and reads and writes from * | |
* microchips 23k256 * | |
* * | |
* Compiled using gcc, tested on Ubunto 10.4 LTS. * | |
* * | |
* By James Henderson, 2012. * | |
*******************************************************/ | |
// | |
// Credit goes to the above person for this short but awesome program. | |
// I modified it slightly to read the information string from the | |
// Alphasense OPC-N2 sensor. | |
// | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <termios.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
#include <time.h> | |
#include <unistd.h> | |
void display_version(void); | |
void set_spi_mode(void); | |
void read_data(void); | |
void wait_comseq() { | |
struct timespec msec10; | |
msec10.tv_sec = 0; | |
msec10.tv_nsec = 10000000; | |
nanosleep(&msec10, NULL); | |
} | |
int fd; | |
int error = 0; | |
unsigned char sbuf[64]; // serial buffer for r/w | |
int main(int argc, char *argv[]) | |
{ | |
printf("******************************\n"); | |
printf("USB ISS linux test application\n"); | |
printf("By James Henderson, 2012\n"); | |
printf("******************************\n\n"); | |
if(argc != 2) | |
printf("Incorrect input!\n"); | |
else | |
{ | |
struct termios defaults; // to store innitial default port settings | |
struct termios config; // These will be our new settings | |
const char *device = "/dev/ttyACM0"; | |
fd = open(device, O_RDWR | O_NOCTTY); | |
if(fd == -1) { | |
printf( "failed to open port\n" ); | |
} else { | |
if (tcgetattr(fd, &defaults) < 0) perror("tcgetattr"); // Grab snapshot of current settings for port | |
cfmakeraw(&config); // make options for raw data | |
if (tcsetattr(fd, TCSANOW, &config) < 0) perror("tcsetattr config"); // Set options for port | |
display_version(); | |
set_spi_mode(); | |
if (!error) read_data(); | |
if (tcsetattr(fd, TCSANOW, &defaults) < 0) perror("tcsetattr defaults"); // Restore port default before closing | |
} | |
close(fd); | |
} | |
return 0; | |
} | |
void display_version(void) | |
{ | |
sbuf[0] = 0x5A; // USB_ISS byte | |
sbuf[1] = 0x01; // Software return byte | |
if (write(fd, sbuf, 2) < 0) perror("display_version write"); // Write data to USB-ISS | |
if (tcdrain(fd) < 0) perror("display_version tcdrain"); | |
if (read(fd, sbuf, 3) < 0) perror("display_version read"); // Read data back from USB-ISS, module ID and software version | |
printf("USB-ISS Module ID: %u \n", sbuf[0]); | |
printf("USB-ISS Software v: %u \n\n", sbuf[1]); | |
} | |
void set_spi_mode(void) | |
{ | |
sbuf[0] = 0x5A; | |
sbuf[1] = 0x02; // Set mode command | |
sbuf[2] = 0x92; // Set SPI mode | |
sbuf[3] = 0x0B; // 500KHz clock speed | |
if (write(fd, sbuf, 4) < 0) perror("set_spi_mode write"); // Write data to USB-ISS | |
if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain"); | |
if (read(fd, sbuf, 2) < 0) perror("set_spi_mode read"); // Read back error byte | |
if(sbuf[0] != 0xFF) // If first returned byte is not 0xFF then an error has occured | |
{ | |
printf("**set_spi_mode: Error setting spi mode!** 0x%02x\n\n", sbuf[0]); | |
error = 0xFF; // Set error byte | |
} | |
} | |
void read_data(void) | |
{ | |
for (int i=0;i<64;i++) { | |
sbuf[i] = 0; | |
} | |
sbuf[0] = 0x61; | |
sbuf[1] = 0x3F; | |
sbuf[2] = 0x00; | |
sbuf[3] = 0x00; | |
sbuf[4] = 0x00; | |
if (write(fd, sbuf, 2) < 0) perror("set_spi_mode write"); // Write data to USB-ISS | |
if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain"); | |
// wait_comseq(); | |
if (read(fd, sbuf, 2) < 0) perror("set_spi_mode read"); // Read back error byte | |
sbuf[0] = 0x61; | |
sbuf[1] = 0x3F; | |
wait_comseq(); | |
// if (write(fd, sbuf, 2) < 0) perror("set_spi_mode write"); // Write data to USB-ISS | |
// if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain"); | |
if (write(fd, sbuf, 61) < 0) perror("set_spi_mode write"); // Write data to USB-ISS | |
if (tcdrain(fd) < 0) perror("set_spi_mode tcdrain"); | |
// wait_comseq(); | |
if (read(fd, &sbuf, 61) < 0) perror("set_spi_mode read"); // Read back error byte | |
for (int i=0;i<61;i++) { | |
// if (read(fd, &sbuf[i], 1) < 0) perror("set_spi_mode read"); // Read back error byte | |
printf("%02x ", sbuf[i]); | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment