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
16 #include "app_demo_i2s.h"
17
18 #ifndef I2S_TEST_DEBUG
19 #define i2s_print(ftm...) do {printf(ftm);} while (0);
20 #else
21 #define i2s_print(ftm...)
22 #endif
23
24 audio_map g_audio_map[2] = { /* 2 flash fields */
25 {0x001A1000, 100 * 1024}, /* audio file size: 100 * 1024(100K) */
26 {0x001CE000, 204800}, /* recod size: 200K */
27 };
28
29 hi_u32 g_audio_event;
30 hi_u32 g_audio_task_id;
31 test_audio_attr g_audio_test;
32 hi_u8 g_record_data[AUDIO_RECORD_BUF_SIZE] = {0};
33
es8311_codec_init(const hi_codec_attribute * codec_attr)34 hi_void es8311_codec_init(const hi_codec_attribute *codec_attr)
35 {
36 if (codec_attr == HI_NULL) {
37 return;
38 }
39 hi_u32 ret;
40
41 ret = hi_i2c_init(HI_I2C_IDX_1, 100000); /* 100000: i2s bit rate */
42 if (ret != HI_ERR_SUCCESS) {
43 i2s_print("==ERROR== hi_i2c_init, err = %X\n", ret);
44 return;
45 }
46
47 ret = hi_codec_init(codec_attr);
48 if (ret != HI_ERR_SUCCESS) {
49 i2s_print("==ERROR== Failed to init codec!! err = %X\n", ret);
50 } else {
51 i2s_print("init codec success!\n");
52 }
53 }
54
audio_play(hi_u32 map_index)55 hi_void audio_play(hi_u32 map_index)
56 {
57 hi_u32 ret;
58 hi_u32 play_addr = g_audio_map[map_index].flash_start_addr;
59 hi_u32 total_play_len = g_audio_map[map_index].data_len;
60 hi_u32 time_out = HI_SYS_WAIT_FOREVER;
61
62 /* apply memory */
63 g_audio_test.play_buf = (hi_u8 *) hi_malloc(HI_MOD_ID_DRV, AUDIO_PLAY_BUF_SIZE);
64 if (g_audio_test.play_buf == HI_NULL) {
65 hi_i2s_deinit();
66 i2s_print("==ERROR== play buf malloc fail!!!\n");
67 return;
68 }
69 memset_s(g_audio_test.play_buf, AUDIO_PLAY_BUF_SIZE, 0, AUDIO_PLAY_BUF_SIZE);
70
71 while (total_play_len > 0) {
72 hi_u32 send_len = hi_min(total_play_len, AUDIO_PLAY_BUF_SIZE);
73 ret = hi_flash_read(play_addr, send_len, g_audio_test.play_buf);
74 if (ret != HI_ERR_SUCCESS) {
75 i2s_print("==ERROR== hi_flash_read fail, err = %X\n", ret);
76 }
77
78 ret = hi_i2s_write(g_audio_test.play_buf, send_len, time_out);
79 if (ret != HI_ERR_SUCCESS) {
80 i2s_print("hi_i2s_write fail, err = %X\n", ret);
81 }
82
83 play_addr += send_len;
84 total_play_len -= send_len;
85 }
86
87 i2s_print("Play over....\n");
88
89 hi_free(HI_MOD_ID_DRV, g_audio_test.play_buf);
90 }
91
audio_record_func(hi_u32 map_index)92 hi_void audio_record_func(hi_u32 map_index)
93 {
94 hi_u32 ret;
95 hi_u32 record_addr = g_audio_map[map_index].flash_start_addr;
96 hi_u32 total_record_len = g_audio_map[map_index].data_len;
97 hi_u32 time_out = HI_SYS_WAIT_FOREVER;
98
99 ret = hi_flash_erase(record_addr, total_record_len);
100 if (ret != HI_ERR_SUCCESS) {
101 i2s_print("Failed to erase flash, err = %X\n", ret);
102 return;
103 }
104
105 while (total_record_len > 0) {
106 hi_u32 len = hi_min(AUDIO_RECORD_BUF_SIZE, total_record_len);
107 ret = hi_i2s_read(g_audio_test.record_buf, len, time_out);
108 if (ret != HI_ERR_SUCCESS) {
109 i2s_print("Failed to hi_i2s_read, err = %X\n", ret);
110 return;
111 }
112
113 if (memcpy_s(g_record_data, sizeof(g_record_data), g_audio_test.record_buf, len) != EOK) {
114 return;
115 }
116 hi_event_send(g_audio_event, AUDIO_RECORD_FINISH_BIT);
117
118 record_addr += len;
119 total_record_len -= len;
120 }
121
122 hi_event_send(g_audio_event, ALL_AUDIO_RECORD_FINISH_BIT);
123 }
124
record_n_play_task(hi_void * param)125 hi_void *record_n_play_task(hi_void *param)
126 {
127 hi_u32 ret;
128 hi_u32 event_bit = 0;
129 hi_u32 record_addr = g_audio_map[AUDIO_RECORD_AND_PLAY_MODE].flash_start_addr;
130 hi_u32 total_record_len = g_audio_map[AUDIO_RECORD_AND_PLAY_MODE].data_len;
131 hi_u32 len;
132 hi_unref_param(param);
133
134 while (1) {
135 hi_event_wait(g_audio_event, AUDIO_RECORD_FINISH_BIT | ALL_AUDIO_RECORD_FINISH_BIT, &event_bit,
136 HI_SYS_WAIT_FOREVER, HI_EVENT_WAITMODE_OR | HI_EVENT_WAITMODE_CLR);
137 if (event_bit & ALL_AUDIO_RECORD_FINISH_BIT) {
138 break;
139 }
140 len = hi_min(AUDIO_RECORD_BUF_SIZE, total_record_len);
141
142 ret = hi_flash_write(record_addr, len, g_record_data, HI_FALSE);
143 if (ret != HI_ERR_SUCCESS) {
144 i2s_print("==ERROR== hi_flash_write, err = %X\n", ret);
145 }
146 record_addr += len;
147 total_record_len -= len;
148 }
149
150 i2s_print("Record success!...\n");
151 hi_udelay(1000000); /* 1000000: delay 1s */
152
153 audio_play(AUDIO_RECORD_AND_PLAY_MODE);
154 i2s_print("Play record audio success!...\n");
155
156 return HI_NULL;
157 }
158
159
audio_record_play(hi_u32 map_index)160 hi_void audio_record_play(hi_u32 map_index)
161 {
162 hi_u32 ret;
163
164 hi_task_attr attr = {0};
165 attr.stack_size = I2S_TEST_TASK_STAK_SIZE;
166 attr.task_prio = I2S_TEST_TASK_PRIORITY;
167 attr.task_name = (hi_char*)"audio_record";
168 ret = hi_task_create(&g_audio_task_id, &attr, record_n_play_task, 0);
169 if (ret != HI_ERR_SUCCESS) {
170 i2s_print("Falied to create record task, err = %X\n", ret);
171 }
172
173 /* apply memory */
174 g_audio_test.record_buf = (hi_u8 *) hi_malloc(HI_MOD_ID_DRV, AUDIO_RECORD_BUF_SIZE);
175 if (g_audio_test.record_buf == HI_NULL) {
176 hi_i2s_deinit();
177 i2s_print("==ERROR== record buf malloc fail!!!\n");
178 return;
179 }
180 memset_s(g_audio_test.record_buf, AUDIO_RECORD_BUF_SIZE, 0, AUDIO_RECORD_BUF_SIZE);
181
182 i2s_print("==start record== please say somerthing~~\n");
183 audio_record_func(map_index);
184 }
185
186 /*
187 * i2s_demo: a simple demo implement paly audio file and record audio then play them back function.
188 * -note: If it is in play mode, user need to burn the audio file to the specified location of flash in advance.
189 * max size of audio file: 100K bytes
190 * burn command: burn 1A1000 19000
191 */
i2s_demo(hi_void)192 hi_void i2s_demo(hi_void)
193 {
194 hi_u32 ret;
195
196 ret = hi_flash_init();
197 if (ret == HI_ERR_FLASH_RE_INIT) {
198 i2s_print("Flash has already been initialized!\n");
199 } else if (ret != HI_ERR_SUCCESS) {
200 i2s_print("Falied to init flash, err = %X\n", ret);
201 }
202
203 /* create I2S record event */
204 ret = hi_event_create(&g_audio_event);
205 if (ret != HI_ERR_SUCCESS) {
206 i2s_print("Failed to init g_audio_event! err = %X\n", ret);
207 return;
208 }
209
210 hi_codec_attribute codec_cfg = {
211 .sample_rate = HI_CODEC_SAMPLE_RATE_8K,
212 .resolution = HI_CODEC_RESOLUTION_16BIT,
213 };
214 hi_i2s_attribute i2s_cfg = {
215 .sample_rate = HI_I2S_SAMPLE_RATE_8K,
216 .resolution = HI_I2S_RESOLUTION_16BIT,
217 };
218
219 es8311_codec_init(&codec_cfg);
220
221 ret = hi_i2s_init(&i2s_cfg);
222 if (ret != HI_ERR_SUCCESS) {
223 i2s_print("Failed to init i2s!\n");
224 return;
225 }
226
227 i2s_print("I2s init success!\n");
228
229 /* paly audio file */
230 audio_play(AUDIO_PLAY_FIEL_MODE);
231
232 hi_udelay(5000000); /* 5000000: delay 5s */
233
234 /* record audio then play them back */
235 audio_record_play(AUDIO_RECORD_AND_PLAY_MODE);
236 }
237
238