| #include <unistd.h> |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #include <fcntl.h> |
| #include <string.h> |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <signal.h> |
| |
| #define SI_SPI_BASE_DEV "/dev/spidev1.0" |
| |
| #define CHAN_NUM_TO_CID(c) ((((c) << 4) & 0x10) | \ |
| (((c) << 2) & 0x8) | \ |
| (((c) >> 2) & 0x2) | \ |
| (((c) >> 4) & 0x1) | \ |
| ((c) & 0x4)) |
| |
| static char slic_read_reg(int fd, char channel, char regAddr) |
| { |
| char buf[3]; |
| char data; |
| |
| memset(buf, 0x00, sizeof(buf)); |
| |
| /* Convert channel to CID and or in the read bit */ |
| buf[0] = CHAN_NUM_TO_CID(channel) | 0x60; |
| buf[1] = regAddr; |
| |
| write(fd, buf,2); |
| read(fd, &data,1); |
| |
| return data; |
| } |
| |
| static char slic_write_reg(int fd, char channel, char regAddr, char regVal) |
| { |
| char buf[3]; |
| |
| memset(buf, 0x00, sizeof(buf)); |
| |
| /* Convert channel to CID and or in the read bit */ |
| buf[0] = CHAN_NUM_TO_CID(channel) | 0x20; |
| buf[1] = regAddr; |
| buf[2] = regVal; |
| |
| write(fd, buf, 3); |
| |
| return 0; |
| } |
| |
| int main(int argc, char **argv) |
| { |
| int i=0; |
| int fd = 0; |
| char dev_name[20] = {0}; |
| char channel = 0; |
| char reg = 0x00;/* slic ID */ |
| char value = 0; |
| int cycle = 0; |
| |
| sprintf(dev_name,"%s",SI_SPI_BASE_DEV); |
| fd = open(dev_name,O_RDWR,0); |
| if (fd < 0) |
| { |
| printf("fail to open fd.\n"); |
| return -1; |
| } |
| |
| if (argc >= 3) { |
| /* write the register */ |
| reg = strtol(argv[1], NULL, 16); |
| value = strtol(argv[2], NULL, 16); |
| |
| printf("write reg[0x%02x]=[0x%02x].\n", reg, value); |
| //slic_write_reg(fd, channel, reg, value); |
| |
| while(1) |
| { |
| sleep(1); |
| |
| /* cycle to write */ |
| slic_write_reg(fd, channel, reg, value); |
| } |
| |
| } else if (argc == 2) { |
| /* read the registers */ |
| reg = strtol(argv[1], NULL, 16); |
| |
| if (0xFF == reg) |
| { |
| /* dump the registers */ |
| printf("dump the registers.\n"); |
| } |
| else |
| { |
| /* cycle to read */ |
| printf("cycle to read reg[0x%02x].\n", reg); |
| } |
| |
| while(1) |
| { |
| sleep(1); |
| |
| if (0XFF == reg) |
| { |
| printf("------dump the registers.-------\n"); |
| for (i=0; i<127; i++) |
| { |
| reg = i; |
| value = slic_read_reg(fd, channel, reg); |
| printf("reg[0x%02x]=[0x%02x].\n", reg, value); |
| } |
| |
| break; |
| } |
| else |
| { |
| value = slic_read_reg(fd, channel, reg); |
| printf("reg[0x%02x]=[0x%02x].\n", reg, value); |
| } |
| } |
| |
| } else { |
| /* only open fd for test SPI driver */ |
| while(1) |
| { |
| sleep(1); |
| } |
| } |
| |
| close(fd); |
| return 0; |
| } |