1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: app sdio slave demo: shows send and receive data with sdio.
16 */
17
18 #include <hi_early_debug.h>
19 #include <hi_watchdog.h>
20 #include <hi_sdio_device.h>
21 #include <hi_cipher.h>
22 #include <hi_cpu.h>
23
24 #define DATA_BLOCK 32768 /* sdio data block size:32768 */
25 #define SEND_RCV_DATA_SIZE 1024 /* send/recv 1024 byte per cycle */
26 hi_u32 g_sdio_send_data[SEND_RCV_DATA_SIZE] = {0}; /* data array of data to be send */
27 hi_u32* g_sdio_send_data_addr = NULL;
28
app_demo_sdio_read_over_callback(hi_void)29 hi_s32 app_demo_sdio_read_over_callback(hi_void)
30 {
31 printf("app_demo_sdio_read_over_callback\r\n");
32 return HI_ERR_SUCCESS;
33 }
34
app_demo_sdio_read_start_callback(hi_u32 len,hi_u8 * admatable)35 hi_s32 app_demo_sdio_read_start_callback(hi_u32 len, hi_u8* admatable)
36 {
37 hi_watchdog_feed();
38
39 hi_u32 i;
40 hi_u32 remain;
41 hi_u32 index = 0;
42 hi_u32* addr = NULL;
43 g_sdio_send_data_addr = &g_sdio_send_data[0];
44
45 for (i = 0; i < (len / DATA_BLOCK); i++) {
46 addr = g_sdio_send_data_addr + ((DATA_BLOCK >> 2) * i); /* 2 bits for g_download_addr is hi_u32 */
47 if (hi_sdio_set_admatable(admatable, index++, addr, DATA_BLOCK) != 0) {
48 return HI_ERR_FAILURE;
49 }
50 }
51
52 remain = len % DATA_BLOCK;
53 if (remain != 0) {
54 addr = g_sdio_send_data_addr + ((DATA_BLOCK >> 2) * i); /* 2 bits for g_download_addr is hi_u32 */
55 if (hi_sdio_set_admatable(admatable, index++, addr, remain) != 0) {
56 return HI_ERR_FAILURE;
57 }
58 }
59
60 if (hi_sdio_complete_send(admatable, index) != 0) {
61 return HI_ERR_FAILURE;
62 }
63
64 hi_cache_flush();
65 return (hi_s32) index;
66 }
67
68 hi_u32 g_receive_data[SEND_RCV_DATA_SIZE] = {0}; /* data array to receive data */
69 hi_u32* g_receive_data_addr = NULL;
70 hi_u32 g_recevei_data_len = 0;
app_demo_sdio_write_start_callback(hi_u32 len,hi_u8 * admatable)71 hi_s32 app_demo_sdio_write_start_callback(hi_u32 len, hi_u8* admatable)
72 {
73 printf("app_demo_sdio_write_start_callback,len: %d\n", len);
74 hi_watchdog_feed();
75 g_receive_data_addr = &g_receive_data[0];
76 g_recevei_data_len = len;
77
78 hi_u32 i;
79 hi_u32 remain;
80 hi_u32 index = 0;
81 hi_u32* addr = NULL;
82
83 for (i = 0; i < (len / DATA_BLOCK); i++) {
84 addr = g_receive_data_addr + ((DATA_BLOCK >> 2) * i); /* shift 2bits is for hi_u32* reason. */
85 if (hi_sdio_set_admatable(admatable, index++, addr, DATA_BLOCK) != 0) {
86 return HI_ERR_FAILURE;
87 }
88 }
89
90 remain = len % DATA_BLOCK;
91
92 if (remain != 0) {
93 addr = g_receive_data_addr + ((DATA_BLOCK >> 2) * i); /* shift 2bits is for hi_u32* reason. */
94 if (hi_sdio_set_admatable(admatable, index++, addr, remain) != 0) {
95 return HI_ERR_FAILURE;
96 }
97 }
98
99 if (hi_sdio_complete_send(admatable, index) != 0) {
100 return HI_ERR_FAILURE;
101 }
102
103 hi_cache_flush();
104 return (hi_s32) index;
105 }
106
app_demo_sdio_write_over_callback(hi_void)107 hi_s32 app_demo_sdio_write_over_callback(hi_void)
108 {
109 printf("app_demo_sdio_write_over_callback, len:%d\n", g_recevei_data_len);
110
111 hi_u8* received_data = (hi_u8*)(&g_receive_data[0]);
112 for (hi_u32 i = 0; i < g_recevei_data_len; i++) {
113 if (i % 8 == 0) { /* 8:Newline */
114 printf ("\r\n");
115 }
116 printf("0x%x ", received_data[i]);
117 }
118 return HI_ERR_SUCCESS;
119 }
120
app_demo_sdio_receive_msg_callback(hi_u32 msg)121 hi_void app_demo_sdio_receive_msg_callback(hi_u32 msg)
122 {
123 printf("app_demo_sdio_receive_msg_callback:0x%x\n", msg);
124 }
125
app_demo_sdio_read_err_callback(hi_void)126 hi_void app_demo_sdio_read_err_callback(hi_void)
127 {
128 printf("app_demo_sdio_read_err_callback\n");
129 }
130
app_demo_sdio_soft_rst_callback(hi_void)131 hi_void app_demo_sdio_soft_rst_callback(hi_void)
132 {
133 printf("app_demo_sdio_soft_rst_callback\r\n");
134 }
135
app_demo_sdio_send_data(hi_void)136 hi_void app_demo_sdio_send_data(hi_void)
137 {
138 printf("app demo sdio start send data\r\n");
139
140 hi_u8* send_data = (hi_u8*)&g_sdio_send_data[0];
141 hi_cipher_trng_get_random_bytes(send_data, SEND_RCV_DATA_SIZE);
142 hi_sdio_send_data(SEND_RCV_DATA_SIZE);
143
144 return;
145 }
146
app_demo_sdio_send_msg(hi_void)147 hi_void app_demo_sdio_send_msg(hi_void)
148 {
149 hi_sdio_send_sync_msg(0);
150 return;
151 }
152
app_demo_sdio_callback_init(hi_void)153 hi_void app_demo_sdio_callback_init(hi_void)
154 {
155 hi_sdio_intcallback callback;
156
157 callback.rdover_callback = app_demo_sdio_read_over_callback;
158 callback.rdstart_callback = app_demo_sdio_read_start_callback;
159 callback.wrstart_callback = app_demo_sdio_write_start_callback;
160 callback.wrover_callback = app_demo_sdio_write_over_callback;
161 callback.processmsg_callback = app_demo_sdio_receive_msg_callback;
162 callback.rderr_callback = app_demo_sdio_read_err_callback;
163 callback.soft_rst_callback = app_demo_sdio_soft_rst_callback;
164 (hi_void)hi_sdio_register_callback(&callback);
165
166 printf("app_demo_sdio_callback_init success\r\n");
167 }
168
app_demo_sdio(hi_void)169 hi_void app_demo_sdio(hi_void)
170 {
171 hi_u32 ret;
172 /* init sdio */
173 /* should config io in app_io_init first. */
174 ret = hi_sdio_init();
175 if (ret != HI_ERR_SUCCESS) {
176 printf("app demo sdio init fail\r\n");
177 return;
178 }
179
180 /* register sdio interrupt callbak. */
181 app_demo_sdio_callback_init();
182
183 /* sdio send and receive msg */
184 /* when host send msg to device, device will receive msg in
185 * app_demo_sdio_receive_msg_callback
186 */
187 app_demo_sdio_send_msg();
188
189 /* sdio send data */
190 app_demo_sdio_send_data();
191
192 /* sdio receive data */
193 /*
194 * when host send data to device, device will receive data in
195 * app_demo_sdio_write_start_callback, app_demo_sdio_write_over_callback
196 */
197 }
198