SDIO使用实例

SDIO设备完整的使用示例如下所示,首先打开总线号为1的SDIO控制器,然后独占HOST、使能设备、注册中断,接着进行SDIO通信(读写等),通信完成之后,释放中断、去使能设备、释放HOST,最后关闭SDIO控制器。


1. #include "hdf_log.h"
2. #include "sdio_if.h"

4. #define TEST_FUNC_NUM 1              /* 本测试用例中,使用编号为1的I/O function */
5. #define TEST_FBR_BASE_ADDR 0x100     /* 编号为1的I/O function的FBR基地址 */
6. #define TEST_ADDR_OFFSET 9           /* 本测试用例中,需要读写的寄存器的地址偏移 */
7. #define TEST_DATA_LEN 3              /* 本测试用例中,读写数据的长度 */
8. #define TEST_BLOCKSIZE 2             /* 本测试用例中,数据块的大小,单位字节 */

10. /* 中断服务函数,需要根据各自平台的情况去实现 */
11. static void SdioIrqFunc(void *data)
12. {
13. if (data == NULL) {
14. HDF_LOGE("SdioIrqFunc: data is NULL.\n");
15. return;
16. }
17. /* 需要开发者自行添加具体的实现 */
18. }

20. void SdioTestSample(void)
21. {
22. int32_t ret;
23. struct DevHandle *handle = NULL;
24. uint8_t data[TEST_DATA_LEN] = {0};
25. int16_t busNum = 1;
26. uint8_t val;
27. uint32_t addr;

29. /* 打开总线号为1的SDIO设备 */
30. handle = SdioOpen(busNum);
31. if (handle == NULL) {
32. HDF_LOGE("SdioOpen: failed!\n");
33. return;
34. }
35. /* 独占HOST */
36. SdioClaimHost(handle);
37. /* 使能SDIO设备 */
38. ret = SdioEnableFunc(handle);
39. if (ret != 0) {
40. HDF_LOGE("SdioEnableFunc: failed, ret %d\n", ret);
41. goto ENABLE_ERR;
42. }
43. /* 注册中断 */
44. ret = SdioClaimIrq(handle, SdioIrqFunc);
45. if (ret != 0) {
46. HDF_LOGE("SdioClaimIrq: failed, ret %d\n", ret);
47. goto CLAIM_IRQ_ERR;
48. }
49. /* 设置块大小为2字节 */
50. ret = SdioSetBlockSize(handle, TEST_BLOCKSIZE);
51. if (ret != 0) {
52. HDF_LOGE("SdioSetBlockSize: failed, ret %d\n", ret);
53. goto COMM_ERR;
54. }
55. /* 从SDIO设备增量地址读取3字节的数据 */
56. addr = TEST_FBR_BASE_ADDR * TEST_FUNC_NUM + TEST_ADDR_OFFSET;
57. ret = SdioReadBytes(handle, data, addr, TEST_DATA_LEN, 0);
58. if (ret != 0) {
59. HDF_LOGE("SdioReadBytes: failed, ret %d\n", ret);
60. goto COMM_ERR;
61. }
62. /* 向SDIO设备增量地址写入3字节的数据 */
63. ret = SdioWriteBytes(handle, data, addr, TEST_DATA_LEN, 0);
64. if (ret != 0) {
65. HDF_LOGE("SdioWriteBytes: failed, ret %d\n", ret);
66. goto COMM_ERR;
67. }
68. /* 从SDIO设备读取1字节的数据 */
69. ret = SdioReadBytes(handle, &val, addr, 1, 0);
70. if (ret != 0) {
71. HDF_LOGE("SdioReadBytes: failed, ret %d\n", ret);
72. goto COMM_ERR;
73. }
74. /* 向SDIO设备写入1字节的数据 */
75. ret = SdioWriteBytes(handle, &val, addr, 1, 0);
76. if (ret != 0) {
77. HDF_LOGE("SdioWriteBytes: failed, ret %d\n", ret);
78. goto COMM_ERR;
79. }
80. /* 从SDIO设备固定地址读取3字节的数据 */
81. ret = SdioReadBytesFromFixedAddr(handle, data, addr, TEST_DATA_LEN, 0);
82. if (ret != 0) {
83. HDF_LOGE("SdioReadBytesFromFixedAddr: failed, ret %d\n", ret);
84. goto COMM_ERR;
85. }
86. /* 向SDIO设备固定地址写入1字节的数据 */
87. ret = SdioWriteBytesToFixedAddr(handle, data, addr, 1, 0);
88. if (ret != 0) {
89. HDF_LOGE("SdioWriteBytesToFixedAddr: failed, ret %d\n", ret);
90. goto COMM_ERR;
91. }
92. /* 从SDIO function 0读取1字节的数据 */
93. addr = 0x02;
94. ret = SdioReadBytesFromFunc0(handle, &val, addr, 1, 0);
95. if (ret != 0) {
96. HDF_LOGE("SdioReadBytesFromFunc0: failed, ret %d\n", ret);
97. goto COMM_ERR;
98. }
99. /* 向SDIO function 0写入1字节的数据 */
100. ret = SdioWriteBytesToFunc0(handle, &val, addr, 1, 0);
101. if (ret != 0) {
102. HDF_LOGE("SdioWriteBytesToFunc0: failed, ret %d\n", ret);
103. goto COMM_ERR;
104. }
105. COMM_ERR:
106. /* 释放中断 */
107. ret = SdioReleaseIrq(handle);
108. if (ret != 0) {
109. HDF_LOGE("SdioReleaseIrq: failed, ret %d\n", ret);
110. }
111. CLAIM_IRQ_ERR:
112. /* 去使能SDIO设备 */
113. ret = SdioDisableFunc(handle);
114. if (ret != 0) {
115. HDF_LOGE("SdioDisableFunc: failed, ret %d\n", ret);
116. }
117. ENABLE_ERR:
118. /* 释放HOST */
119. SdioReleaseHost(handle);
120. /* 关闭SDIO设备 */
121. SdioClose(handle);
122. }