分类: linux basics linux develop 2012-10-06 16:57 6243人阅读 评论(0) 收藏 举报 ============================================ 作者:yuanlulu [cpp] view plaincopyprint? 1. struct spi_device { 2. structdevice dev; 3. structspi_master *master; 4. u32 max_speed_hz; /* 通信时钟最大频率 */ 5. u8 chip_select; /* 片选号 */ 6. u8 mode; /*SPI设备的模式,下面的宏是它各bit的含义 */ 7. #define SPI_CPHA 0x01 /* 采样的时钟相位 */ 8. #define SPI_CPOL 0x02 /* 时钟信号起始相位:高或者是低电平*/ 9. #define SPI_MODE_0 (0|0) 10. #define SPI_MODE_1 (0|SPI_CPHA) 11. #define SPI_MODE_2 (SPI_CPOL|0) 12. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 13. #define SPI_CS_HIGH 0x04 /* 为1时片选的有效信号是高电平*/ 14. #define SPI_LSB_FIRST 0x08 /* 发送时低比特在前 */ 15. #define SPI_3WIRE 0x10 /* 输入输出信号使用同一根信号线 */ 16. #define SPI_LOOP 0x20 /* 回环模式 */ 17. u8 bits_per_word; /* 每个通信字的字长(比特数) */ 18. int irq; /*使用到的中断 */ 19. void *controller_state; 20. void *controller_data; 21. char modalias[32]; /* 设备驱动的名字*/ 22. }; struct spi_device { structdevice dev; structspi_master *master; u32 max_speed_hz; /* 通信时钟最大频率 */ u8 chip_select; /* 片选号 */ u8 mode; /*SPI设备的模式,下面的宏是它各bit的含义 */ #define SPI_CPHA 0x01 /* 采样的时钟相位 */ #define SPI_CPOL 0x02 /* 时钟信号起始相位:高或者是低电平*/ #define SPI_MODE_0 (0|0) #define SPI_MODE_1 (0|SPI_CPHA) #define SPI_MODE_2 (SPI_CPOL|0) #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) #define SPI_CS_HIGH 0x04 /* 为1时片选的有效信号是高电平*/ #define SPI_LSB_FIRST 0x08 /* 发送时低比特在前 */ #define SPI_3WIRE 0x10 /* 输入输出信号使用同一根信号线 */ #define SPI_LOOP 0x20 /* 回环模式 */ u8 bits_per_word; /* 每个通信字的字长(比特数) */ int irq; /*使用到的中断 */ void *controller_state; void *controller_data; char modalias[32]; /* 设备驱动的名字*/ };
[cpp] view plaincopyprint? 1. struct spi_ioc_transfer { 2. __u64 tx_buf; /* 写数据缓冲 */ 3. __u64 rx_buf; /* 读数据缓冲 */ 4. 5. __u32 len; /* 缓冲的长度 */ 6. __u32 speed_hz; /* 通信的时钟频率 */ 7. 8. __u16 delay_usecs; /* 两个spi_ioc_transfer之间的延时 */ 9. __u8 bits_per_word; /* 字长(比特数) */ 10. __u8 cs_change; /* 是否改变片选 */ 11. __u32 pad; 12. }; struct spi_ioc_transfer { __u64 tx_buf; /* 写数据缓冲 */ __u64 rx_buf; /* 读数据缓冲 */ __u32 len; /* 缓冲的长度 */ __u32 speed_hz; /* 通信的时钟频率 */ __u16 delay_usecs; /* 两个spi_ioc_transfer之间的延时 */ __u8 bits_per_word; /* 字长(比特数) */ __u8 cs_change; /* 是否改变片选 */ __u32 pad; };
[cpp] view plaincopyprint? 1. struct spi_board_info { 2. char modalias[32]; /* 要绑定的驱动的名字 */ 3. constvoid *platform_data; 4. void *controller_data; 5. int irq; 6. 7. u32 max_speed_hz; /* 通信时钟最大速率 */ 8. 9. u16 bus_num; /* 总线编号 */ 10. u16 chip_select; /* 片选号 */ 11. 12. u8 mode; /* 和spi_device中的mode成员类似 */ 13. }; struct spi_board_info { char modalias[32]; /* 要绑定的驱动的名字 */ constvoid *platform_data; void *controller_data; int irq; u32 max_speed_hz; /* 通信时钟最大速率 */ u16 bus_num; /* 总线编号 */ u16 chip_select; /* 片选号 */ u8 mode; /* 和spi_device中的mode成员类似 */ };
[cpp] view plaincopyprint? 1. static int __init smartarm3250_spi_usp_register(void) 2. { 3. structspi_board_info info = 4. { 5. .modalias= "spidev", 6. .max_speed_hz= 5000000, 7. .bus_num= 0, 8. .chip_select= 0, 9. }; 10. 11. returnspi_register_board_info(&info, 1); 12. } 13. arch_initcall(smartarm3250_spi_usp_register); static int __init smartarm3250_spi_usp_register(void) { structspi_board_info info = { .modalias= "spidev", .max_speed_hz= 5000000, .bus_num= 0, .chip_select= 0, }; returnspi_register_board_info(&info, 1); } arch_initcall(smartarm3250_spi_usp_register);
[cpp] view plaincopyprint? 1. struct spi_board_info info = 2. { 3. .modalias = "spidev", 4. .max_speed_hz = 5000000, 5. .bus_num = 0, 6. .chip_select = 0, 7. }; struct spi_board_info info = { .modalias = "spidev", .max_speed_hz = 5000000, .bus_num = 0, .chip_select = 0, };
[cpp] view plaincopyprint? 1. struct spi_ioc_transfer { 2. __u64 tx_buf; 3. __u64 rx_buf; 4. __u32 len; 5. __u32 speed_hz; 6. __u16 delay_usecs; 7. __u8 bits_per_word; 8. __u8 cs_change; 9. __u32 pad; 10. /* If the contents of 'struct spi_ioc_transfer' ever change 11. * incompatibly, then the ioctl number (currently 0) must change; 12. * ioctls with constant size fields get a bit more in the way of 13. * error checking than ones (like this) where that field varies. 14. * 15. * NOTE: struct layout is the same in 64bit and 32bit userspace. 16. */ 17. }; struct spi_ioc_transfer { __u64 tx_buf; __u64 rx_buf; __u32 len; __u32 speed_hz; __u16 delay_usecs; __u8 bits_per_word; __u8 cs_change; __u32 pad; /* If the contents of 'struct spi_ioc_transfer' ever change * incompatibly, then the ioctl number (currently 0) must change; * ioctls with constant size fields get a bit more in the way of * error checking than ones (like this) where that field varies. * * NOTE: struct layout is the same in 64bit and 32bit userspace. */ };
[cpp] view plaincopyprint? 1. static void do_msg(int fd, int len) 2. { 3. struct spi_ioc_transfer xfer[2]; 4. unsigned char buf[32], *bp; 5. int status; 6. memset(xfer, 0, sizeof xfer); 7. memset(buf, 0, sizeof buf); 8. if (len > sizeof buf) 9. len = sizeof buf; 10. buf[0] = 0xaa; 11. xfer[0].tx_buf = (__u64) buf; 12. xfer[0].len = 1; 13. xfer[1].rx_buf = (__u64) buf; 14. xfer[1].len = len; 15. status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer); 16. if (status < 0) { 17. perror("SPI_IOC_MESSAGE"); 18. return; 19. } 20. printf("response(%2d, %2d): ", len, status); 21. for (bp = buf; len; len--) 22. printf(" %02x", *bp++); 23. printf("/n"); 24. } static void do_msg(int fd, int len) { struct spi_ioc_transfer xfer[2]; unsigned char buf[32], *bp; int status; memset(xfer, 0, sizeof xfer); memset(buf, 0, sizeof buf); if (len > sizeof buf) len = sizeof buf; buf[0] = 0xaa; xfer[0].tx_buf = (__u64) buf; xfer[0].len = 1; xfer[1].rx_buf = (__u64) buf; xfer[1].len = len; status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer); if (status < 0) { perror("SPI_IOC_MESSAGE"); return; } printf("response(%2d, %2d): ", len, status); for (bp = buf; len; len--) printf(" %02x", *bp++); printf("/n"); }
|
|
来自: 昵称28625772 > 《LInux SPI》