D1-H Tinalinux下spi1 slave模式如何使用
-
请问Tina Linux 5.4环境下,spi1的slave模式应该如何使用?参考Linux SPI开发指南,修改dts
&spi1 { clock-frequency = <100000000>; pinctrl-0 = <&spi1_pins_a &spi1_pins_b>; pinctrl-1 = <&spi1_pins_c>; pinctrl-names = "default", "sleep"; spi_slave_mode = <1>; spi1_cs_number = <1>; spi1_cs_bitmap = <1>; status = "okay"; spi_board1@0 { device_type = "spi_board1"; compatible = "rohm,dh2228fv"; spi-max-frequency = <0x5f5e100>; reg = <0x0>; spi-rx-bus-width = <0x1>; spi-tx-bus-width = <0x1>; status = "okay"; }; };
无spidev设备生成,文档slave参考示例没法用
-
@wjx163208 同问,我刚要发贴子,发现已经有人发了。
-
@wjx163208 关注中,希望全志官网能有好的解决方案。
-
@wjx163208 主要是没有设备生成。/dev目录下没有。/sys/class/spi_slave目录也也没有设备。
master模式是没有问题的。两个目录都有生成设备 -
4.2 Slave 模式驱动范例
需要在 board.dts 中相应的 SPI 节点设备配置 spi_slave_mode = <1>。
4.2.1 Slave 写数据
以 spidev1.0 设备为例,发送 0~9 十个数据:1 #define DEVICE_NAME "/dev/spidev1.0" 2 #define HEAD_LEN 5 3 #define PKT_MAX_LEN 0x40 4 #define STATUS_LEN 0x01 5 6 #define SUNXI_OP_WRITE 0x01 7 #define SUNXI_OP_READ 0x03 8 9 #define STATUS_WRITABLE 0x02 10 #define STATUS_READABLE 0x04 11 12 #define WRITE_DELAY 200 13 #define READ_DELAY 100000 14 15 void dump_data(unsigned char *buf, unsigned int len) 16 { 17 unsigned int i; 18 unsigned char tmp[len*2], cnt = 0; 19 20 for (i = 0; i < len; i++) { 21 if (i%0x10== 0) 22 cnt += sprintf(tmp + cnt, "0x%08x: ", i); 23 24 cnt += sprintf(tmp + cnt, "%02x ", buf[i]); 25 26 if ( (i%0x10== 0x0f) || (i == (len -1)) ) { 27 printf("%s\n", tmp); 28 cnt = 0; 29 } 30 } 31 } 32 33 void batch_rand(char *buf, unsigned int length) 34 { 35 unsigned int i; 36 srand(time(0)); 37 38 for(i = 0; i < length; i++) { 39 *(buf + i) = rand() % 256; 40 } 41 } 42 43 int main(int argc, const char *argv[]) 44 { 45 unsigned int length = 0, test_len; 46 char wbuf_head[HEAD_LEN] = {SUNXI_OP_WRITE, 0x00, 0x00, 0x00, 0x00}; 47 char rbuf_head[HEAD_LEN] = {SUNXI_OP_READ, 0x00, 0x00, 0x00, 0x00}; 48 char wbuf[PKT_MAX_LEN], rbuf[PKT_MAX_LEN], i, time; 49 int fd, ret; 50 51 test_len = 10;//send 10 numbers 52 if (test_len > PKT_MAX_LEN) { 53 printf("invalid argument, numbers must less 64B\n"); 54 return -1; 55 } 56 57 wbuf_head[4] = test_len; 58 rbuf_head[4] = test_len; 59 60 for (i = 0; i < test_len; i++) 61 wbuf[i] = i; 62 printf("wbuf:\n"); 63 dump_data(wbuf, test_len); 64 65 fd = open(DEVICE_NAME, O_RDWR); 66 if (fd <= 0) { 67 printf("Fail to to open %s\n", DEVICE_NAME); 68 ret = -1; 69 return ret; 70 } 71 72 {//write 73 if (write(fd, wbuf_head, HEAD_LEN) != HEAD_LEN) { 74 printf("W Fail to write head\n"); 75 ret = -1; 76 goto err; 77 } else 78 printf("W write head successful\n"); 79 80 usleep(WRITE_DELAY); 81 82 if (write(fd, wbuf, test_len) != test_len) { 83 printf("W Fail to write data\n"); 84 ret = -1; 85 goto err; 86 } else 87 printf("W write data successful\n"); 88 89 usleep(READ_DELAY); 90 } 91 92 err: 93 if (fd > 0) 94 close(fd); 95 96 return ret; 97 }
4.2.2 Slave 读数据
以 spidev1.0 设备为例,读十个数据:1 #define DEVICE_NAME "/dev/spidev1.0" 2 #define HEAD_LEN 5 3 #define PKT_MAX_LEN 0x40 4 #define STATUS_LEN 0x01 5 6 #define SUNXI_OP_WRITE 0x01 7 #define SUNXI_OP_READ 0x03 8 9 #define STATUS_WRITABLE 0x02 10 #define STATUS_READABLE 0x04 11 12 #define WRITE_DELAY 200 13 #define READ_DELAY 100000 15 void dump_data(unsigned char *buf, unsigned int len) 16 { 17 unsigned int i; 18 unsigned char tmp[len*2], cnt = 0; 19 20 for (i = 0; i < len; i++) { 21 if (i%0x10== 0) 22 cnt += sprintf(tmp + cnt, "0x%08x: ", i); 23 24 cnt += sprintf(tmp + cnt, "%02x ", buf[i]); 25 26 if ( (i%0x10== 0x0f) || (i == (len -1)) ) { 27 printf("%s\n", tmp); 28 cnt = 0; 29 } 30 } 31 } 32 33 void batch_rand(char *buf, unsigned int length) 34 { 35 unsigned int i; 36 srand(time(0)); 37 38 for(i = 0; i < length; i++) { 39 *(buf + i) = rand() % 256; 40 } 41 } 42 43 int main(int argc, const char *argv[]) 44 { 45 unsigned int length = 0, test_len; 46 char wbuf_head[HEAD_LEN] = {SUNXI_OP_WRITE, 0x00, 0x00, 0x00, 0x00}; 47 char rbuf_head[HEAD_LEN] = {SUNXI_OP_READ, 0x00, 0x00, 0x00, 0x00}; 48 char wbuf[PKT_MAX_LEN], rbuf[PKT_MAX_LEN], i, time; 49 int fd, ret; 50 51 test_len = 10; 52 if (test_len > PKT_MAX_LEN) { 53 printf("inval argument, numbers must less 64B\n"); 54 return -1; 55 } 56 57 wbuf_head[4] = test_len; 58 rbuf_head[4] = test_len; 59 60 fd = open(DEVICE_NAME, O_RDWR); 61 if (fd <= 0) { 62 printf("Fail to to open %s\n", DEVICE_NAME); 63 ret = -1; 64 return ret; 65 } 66 67 {//read 68 if (write(fd, rbuf_head, HEAD_LEN) != HEAD_LEN) { 69 printf("R Fail to write head\n"); 70 ret = -1; 71 goto err; 72 } else 73 printf("R write head successful\n"); 75 usleep(READ_DELAY); 76 77 if (read(fd, rbuf, test_len) != test_len) { 78 printf("R Fail to read data\n"); 79 ret = -1; 80 goto err; 81 } else 82 printf("R read data successful\n"); 83 84 usleep(READ_DELAY); 85 86 } 87 88 printf("rbuf:\n"); 89 dump_data(rbuf, test_len); 90 91 err: 92 if (fd > 0) 93 close(fd); 94 95 return ret; 96 }
-
@whycan 就是参考这个文档,但关键的是dts设置slave模式后,就没有spidev设备,示例代码没法用
-
@wjx163208
请仔细阅读上面的PDF文档,关于kernel配置那部分。 -
@whycan kernel已经按照要求配置了SPIDEV,上面已经提到设置master模式是有设备的。你们可以拿开发板亲自试试按照文档设置slave模式能否出现spidev设备,给点实操可行的建议,谢谢!
-
@whycan 在 D1-H Tinalinux下spi1 slave模式如何使用 中说:
@wjx163208
请仔细阅读上面的PDF文档,关于kernel配置那部分。你有没有试过,/dev/目录下根本就没有spi设备怎么操作?根据你这个帖子感觉你还是没有实操作过。
-
@wjx163208 我也卡在这一块了,slave模式下,设备出不来
只能期待官方回复,或有大神回复了。 -
@wjx163208 在 D1-H Tinalinux下spi1 slave模式如何使用 中说:
请问Tina Linux 5.4环境下,spi1的slave模式应该如何使用?参考Linux SPI开发指南,修改dts
&spi1 { clock-frequency = <100000000>; pinctrl-0 = <&spi1_pins_a &spi1_pins_b>; pinctrl-1 = <&spi1_pins_c>; pinctrl-names = "default", "sleep"; spi_slave_mode = <1>; spi1_cs_number = <1>; spi1_cs_bitmap = <1>; status = "okay"; spi_board1@0 { device_type = "spi_board1"; compatible = "rohm,dh2228fv"; spi-max-frequency = <0x5f5e100>; reg = <0x0>; spi-rx-bus-width = <0x1>; spi-tx-bus-width = <0x1>; status = "okay"; }; };
无spidev设备生成,文档slave参考示例没法用
期待高手的指点
-
勾选了吗
-
@whycanservice 选不选系统起来后都找不到SPI设备
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号