#include #include #include /* File Control Definitions */ #include /* POSIX Terminal Control Definitions */ #include /* UNIX Standard Definitions */ #include /* ERROR Number Definitions */ #include // Number of strings in array argv // int main( int argc, char *argv[], // void main(void) static void print_msg(void) { printf("=============================================================\n"); printf("Usage :\n"); printf("./fii_app.o dir : val : 0 => output; 1 => input\n"); printf("./fii_app.o wr : val : 0x43c0_0004 address value\n"); printf("./fii_app.o rd : \n"); printf("=============================================================\n"); return; } static void func_read(int fd, int read_cnt) { char rd_buf[32]; /* Buffer to store the data received */ int bytes_read = 0; /* Number of bytes read by the read() system call */ unsigned int temp; printf("Press ctrl+C to exit \n"); tcflush(fd, TCIFLUSH); /* Discards old data in the rx buffer */ lseek(fd, 0, SEEK_SET); bytes_read = read(fd, rd_buf, read_cnt); printf("\n\nBytes Rxed = %d \n", bytes_read); /* Print the number of bytes read */ temp = rd_buf[0]; temp = (temp << 8) | rd_buf[1]; temp = (temp << 8) | rd_buf[2]; temp = (temp << 8) | rd_buf[3]; printf("read value = 0x%08x \n", temp); temp = rd_buf[4]; temp = (temp << 8) | rd_buf[5]; temp = (temp << 8) | rd_buf[6]; temp = (temp << 8) | rd_buf[7]; printf("read dir = 0x%08x \n", temp); printf("\n +----------------------------------+\n\n\n"); // usleep(1000000); return; } static void func_write(int fd, char *p) { char wr_buf[32]; ssize_t bytes_written = 0; unsigned int value; memset (wr_buf, 0x0a, 32); value = atoi(p); printf("value = %x \n" , value); wr_buf[0] = 'w'; wr_buf[1] = ' '; wr_buf[2] = value >> 24; wr_buf[3] = (value >> 16) & 0xff; wr_buf[4] = (value >> 8) & 0xff; wr_buf[5] = value & 0xff; bytes_written = write(fd, wr_buf, 7); return; } static void func_direction(int fd, char *p) { char wr_buf[32]; ssize_t bytes_written = 0; unsigned int value; memset (wr_buf, 0x0a, 32); value = atoi(p); printf("value = %x \n" , value); wr_buf[0] = 'd'; wr_buf[1] = ' '; wr_buf[2] = value >> 24; wr_buf[3] = (value >> 16) & 0xff; wr_buf[3] = (value >> 8) & 0xff; wr_buf[5] = value & 0xff; bytes_written = write(fd, wr_buf, 7); } int main( int argc, char *argv[]) { int fd;/*File Descriptor*/ unsigned int value = 0; char * cmd_p; /* O_RDWR - Read/Write access to serial port */ /* O_RDONLY - Read access to serial port */ /* O_WRONLY - Write access to serial port */ /* O_NOCTTY - No terminal will control the process */ /* Open in blocking mode,read will wait */ fd = open("/dev/fii-module",O_RDWR ); printf("\n +----------------------------------+"); printf("\n | fii-driver function |"); printf("\n +----------------------------------+"); if(fd == -1) /* Error Checking */ printf("\n Error! in Opening swled \n"); else printf("\n fii-driver Opened Successfully \n"); cmd_p = argv[1]; if(argc == 2) { if( strcmp (cmd_p , "rd") == 0 ) { func_read(fd, 8); return 0; } else { print_msg(); close(fd); /* Close the serial port */ return 1; } } else if (argc == 3) { if( strcmp (cmd_p , "wr") == 0 ) { func_write(fd, argv[2]); close(fd); /* Close the serial port */ return 0; } else if( strcmp (cmd_p , "dir") == 0 ) { func_direction(fd, argv[2]); close(fd); /* Close the serial port */ return 0; } } else { print_msg(); close(fd); /* Close the serial port */ return 1; } return 0; }