liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame^] | 1 | #include "ql/ql_spi.h" |
| 2 | |
| 3 | |
| 4 | int main(int argc, char *argv[]) |
| 5 | { |
| 6 | char send_data[64] = {0}; |
| 7 | char read_data[64] = {0}; |
| 8 | char crc = 0; |
| 9 | int i = 0; |
| 10 | int j = 0; |
| 11 | |
| 12 | //system("echo PB6 > /sys/kernel/debug/sunxi_pinctrl/sunxi_pin"); |
| 13 | //system("echo PB6 1 > /sys/kernel/debug/sunxi_pinctrl/function"); |
| 14 | //system("echo PB6 0 > /sys/kernel/debug/sunxi_pinctrl/data"); |
| 15 | |
| 16 | #if 0 |
| 17 | static const char *device = "/dev/spidev1.0\0"; |
| 18 | static uint8_t mode = 3; /* SPI通信使用全双工,设置CPOL=0,CPHA=0。 */ |
| 19 | static uint8_t bits = 8; /* 8bits读写,MSB first。*/ |
| 20 | static uint32_t speed = 100 * 1000;/* 设置0.5M传输速度 */ |
| 21 | static uint16_t delay = 500; |
| 22 | #endif |
| 23 | int fd = -1; |
| 24 | /* spi 初始化程序 */ |
| 25 | if((fd = Ql_SPI_Init("/dev/spidev1.0", SPIMODE3, 8, S_13M)) <= 0) |
| 26 | { |
| 27 | printf("Ql_SPI_Init() fail.\n"); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | send_data[0] = 0x55; |
| 32 | send_data[1] = 0x00; |
| 33 | send_data[2] = 0x84; |
| 34 | send_data[3] = 0x00; |
| 35 | send_data[4] = 0x08; |
| 36 | send_data[5] = 0x00; |
| 37 | send_data[6] = 0x00; |
| 38 | |
| 39 | crc = send_data[1]; |
| 40 | for (i = 2; i < 7; i++) |
| 41 | { |
| 42 | crc ^= send_data[i]; |
| 43 | } |
| 44 | crc = ~crc; |
| 45 | |
| 46 | send_data[7] = crc; |
| 47 | |
| 48 | printf("send data:"); |
| 49 | for (i = 0; i < 8; i++) |
| 50 | { |
| 51 | printf("%#x, ", send_data[i]); |
| 52 | } |
| 53 | printf("\n"); |
| 54 | |
| 55 | /* spi 发送数据 */ |
| 56 | if(Ql_SPI_Write_Read(fd, send_data,read_data, 8)) { |
| 57 | printf("Ql_SPI_Write_Read() fail.\n"); |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | #if 0 |
| 62 | printf("read data:"); |
| 63 | for (j = 0; j < 20; j++) |
| 64 | { |
| 65 | printf("%#x, ", read_data[j]); |
| 66 | } |
| 67 | |
| 68 | usleep(10000); |
| 69 | |
| 70 | memset(read_data, 0, sizeof(read_data)); |
| 71 | memset(send_data, 0, sizeof(send_data)); |
| 72 | /* spi 读取数据 */ |
| 73 | if(Ql_SPI_Write_Read(fd, send_data,read_data, 16)) { |
| 74 | printf("Ql_SPI_Write_Read() fail.\n"); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | printf("read data:"); |
| 79 | for (j = 0; j < 20; j++) |
| 80 | { |
| 81 | printf("%#x, ", read_data[j]); |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | if(Ql_SPI_DeInit(fd)) { |
| 86 | printf("Ql_SPI_DeInit() fail.\n"); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | printf("success!!!\n"); |
| 91 | return 0; |
| 92 | } |