1 /**
2  * File : aos.c
3  */
4 
5 #include "cmsis.h"
6 #include "stdarg.h"
7 #include "aos/init.h"
8 #include "aos/kernel.h"
9 //#include "uagent.h"
10 #include "ulog/ulog.h"
11 #include "ulog_config.h"
12 #include <k_api.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "aos/hal/gpio.h"
16 #include "aos/hal/uart.h"
17 #if(AOS_COMP_WIFI > 0)
18 #include "aos/hal/wifi.h"
19 #endif
20 #include "hal_cmu.h"
21 #include "app_utils.h"
22 #include "ota_port.h"
23 #include "hal_uart.h"
24 #include "hal_trace.h"
25 
26 #if AOS_COMP_CLI
27 #include "aos/cli.h"
28 #endif
29 
30 extern uart_dev_t uart_0;
31 
32 #define WRAP_P_BUF_LEN 2048
33 uint8_t _wrap_p_buf[WRAP_P_BUF_LEN];
34 int max_p_size = 0;
35 int g_dirver_trace_flag = 1;
36 
37 #if AOS_COMP_CLI
char2data(const char ch)38 static uint8_t char2data(const char ch)
39 {
40     if ((ch >= '0') && (ch <= '9')) {
41         return (uint8_t)(ch - '0');
42     }
43     if ((ch >= 'a') && (ch <= 'f')) {
44         return (uint8_t)(ch - 'a' + 10);
45     }
46     if ((ch >= 'A') && (ch <= 'F')) {
47         return (uint8_t)(ch - 'A' + 10);
48     }
49 
50     return 0;
51 }
str2mac(const char * sz_mac,uint8_t * pmac)52 static void str2mac(const char *sz_mac, uint8_t *pmac)
53 {
54     const char *ptemp = sz_mac;
55     for (int i = 0; i < 6; ++i) {
56         pmac[i] = char2data(*ptemp++) * 16;
57         pmac[i] += char2data(*ptemp++);
58         ptemp++;
59     }
60 }
61 extern int factory_section_set_wifi_address(uint8_t *wifi_addr);
62 extern uint8_t *factory_section_get_wifi_address(void);
63 extern int factory_section_set_bt_address(uint8_t *bt_addr);
64 extern uint8_t *factory_section_get_bt_address(void);
65 
handle_aos_mac_cmd(int argc,char ** argv)66 static void handle_aos_mac_cmd(int argc, char **argv)
67 {
68     int ret = 0;
69     const char *rtype = argc > 1 ? argv[1] : "";
70     const char *mac   = argc > 2 ? argv[2] : NULL;
71     uint8_t pmac[6];
72 
73     if (mac != NULL) {
74         str2mac(mac, pmac);
75         printf("will set mac %02x:%02x:%02x:%02x:%02x:%02x\n",
76                 pmac[0], pmac[1], pmac[2], pmac[3], pmac[4], pmac[5]);
77     }
78 
79     if (strcmp(rtype, "WIFI") == 0) {
80         if (mac != NULL) {
81             ret = factory_section_set_wifi_address(pmac);
82             if (ret == 0) {
83                 printf("set WIFI mac success!\n");
84             } else {
85                 printf("set WIFI mac fail!\n");
86             }
87         } else {
88             uint8_t *_mac = factory_section_get_wifi_address();
89             if (_mac == NULL) {
90                 printf("get WIFI mac fail\n");
91             } else {
92                 printf("WIFI mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
93                         _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]);
94             }
95         }
96     } else if (strcmp(rtype, "BT") == 0) {
97         if (mac != NULL) {
98             ret = factory_section_set_bt_address(pmac);
99             if (ret == 0) {
100                 printf("set BT mac success!\n");
101             } else {
102                 printf("set BT mac fail!\n");
103             }
104         } else {
105             uint8_t *_mac = factory_section_get_bt_address();
106             if (_mac == NULL) {
107                 printf("get BT mac fail\n");
108             } else {
109                 printf("BT mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
110                         _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]);
111             }
112         }
113     } else {
114         printf("Usage: aos_mac [WIFI/BT] [XX:XX:XX:XX:XX:XX]");
115     }
116 }
117 
118 ALIOS_CLI_CMD_REGISTER(handle_aos_mac_cmd, aos_mac, "aos_mac [WIFI/BT] [mac]")
119 #endif
120 
121 
__wrap_printf(const char * fmt,...)122 int __wrap_printf(const char *fmt, ...)
123 {
124     int i;
125     int len = 0;
126     int crlf = 0;
127     char *p = NULL;
128     va_list args;
129     uint32_t lock = int_lock();
130     va_start(args, fmt);
131     if (strcmp(fmt, "%s") == 0) {
132         p = va_arg(args, char *);
133         len = strlen(p);
134     } else if (strstr(fmt, "%") == NULL) {
135         p = fmt;
136         len = strlen(p);
137     } else {
138         p = _wrap_p_buf;
139         len = vsnprintf(_wrap_p_buf, WRAP_P_BUF_LEN-1, fmt, args);
140         max_p_size = (len > max_p_size) ? len : max_p_size;
141     }
142 
143     hal_uart_send(&uart_0, p, len, 300000);
144 
145     va_end(args);
146     int_unlock(lock);
147     return len;
148 }
149 
150 #ifdef AOS_COMP_PWRMGMT
151 int pwrmgmt_init();
152 #endif
153 
154 /* use for printk */
alios_debug_print(const char * buf,int size)155 int alios_debug_print(const char *buf, int size)
156 {
157     hal_trace_output_block((const unsigned char *)buf, size);
158     /*
159     uint32_t i;
160     for (i = 0; i < size; i++) {
161         hal_uart_blocked_putc(0, buf[i]);
162     } */
163 
164     return size;
165 }
166 
alios_cli_panic_hook()167 void alios_cli_panic_hook()
168 {
169     hal_watchdog_disable();
170     hal_panic_uart_open();
171 }
172 
uart_input_read(void)173 char uart_input_read(void)
174 {
175     return hal_uart_blocked_getc(0);
176 }
177 
178 
179 /*  check pc available  0:available  other:not available */
180 extern uint32_t __sram_text_start__[];
181 extern uint32_t __sram_text_end__[];
182 extern uint32_t __flashx_text_start__[];
183 extern uint32_t __flashx_text_end__[];
184 // extern uint32_t __psramx_text_start__[];
185 // extern uint32_t __psramx_text_end__[];
186 
alios_debug_pc_check(char * pc)187 int alios_debug_pc_check(char *pc)
188 {
189     if ( (((uint32_t)pc > (uint32_t)__sram_text_start__) &&
190           ((uint32_t)pc < (uint32_t)__sram_text_end__)) ||
191          (((uint32_t)pc > (uint32_t)__flashx_text_start__) &&
192           ((uint32_t)pc < (uint32_t)__flashx_text_end__))
193 //        || (((uint32_t)pc > (uint32_t)__psramx_text_start__) &&
194 //          ((uint32_t)pc < (uint32_t)__psramx_text_end__))
195         ) {
196         return 0;
197     } else {
198         return -1;
199     }
200 }
201 
202 #if AOS_COMP_CLI
alios_debug_pc_show(int argc,char ** argv)203 void alios_debug_pc_show(int argc, char **argv)
204 {
205     cli_printf("----- PC Addr ------\r\n");
206     cli_printf("addr 1 : 0x%08x ~ 0x%08x\r\n", (uint32_t)__sram_text_start__, (uint32_t)__sram_text_end__);
207     cli_printf("addr 2 : 0x%08x ~ 0x%08x\r\n", (uint32_t)__flashx_text_start__, (uint32_t)__flashx_text_end__);
208 //    cli_printf("addr 3 : 0x%08x ~ 0x%08x\r\n", (uint32_t)__psramx_text_start__, (uint32_t)__psramx_text_end__);
209 }
210 #endif
211 
212 #ifdef AOS_COMP_LITTLEFS
213 #include "littlefs.h"
214 
littlefs_fetch_cfg_param(struct littlefs_cfg_param * cfg_param)215 int32_t littlefs_fetch_cfg_param(struct littlefs_cfg_param * cfg_param)
216 {
217     cfg_param->block_size = 4096;
218 #ifdef CFG_HW_ALI_MODULE
219     cfg_param->block_count = 520;
220 #else
221     cfg_param->block_count = 1198;
222 #endif
223     cfg_param->prog_size = 1024;
224     cfg_param->read_size = 256;
225     cfg_param->block_cycles = 1000;
226     cfg_param->cache_size = 1024;
227     cfg_param->lookahead_size = 16;
228     return 0;
229 }
230 #endif /* AOS_COMP_LITTLEFS */
231 
232 #if CONFIG_A7_DSP_ENABLE
233 #include "a7_cmd.h"
234 #include "aud_dump.h"
mcu_first_handshake(void)235 void mcu_first_handshake(void)
236 {
237     transq_msg_onoff(1);
238     A7_CMD_T a7_cmd;
239     a7_cmd.type = A7_CMD_TYPE_HANDSHAKE;
240     a7_cmd.p1 = (tg_ota_get_odm_type()==ALI_C5A01)?48000:16000;
241     a7_cmd.p2 = (tg_ota_get_odm_type()==ALI_C5A01)?48:64;
242     mcu_cmd_send(&a7_cmd);
243     printf("%s A7_CMD_TYPE_HANDSHAKE to a7: mic_samplerate %d, period_ms %d\n", __FUNCTION__, a7_cmd.p1, a7_cmd.p2);
244 }
245 #endif
246 
do_ulog(const unsigned char s,const char * mod,const char * f,const unsigned long l,const char * fmt,va_list args)247 int WEAK do_ulog(const unsigned char s, const char *mod, const char *f, const unsigned long l, const char *fmt, va_list args)
248 {
249     return 0;
250 }
aos_printf_hook(const char * tag,const char * fmt,enum PRINTF_FLAG_T flag,va_list ap)251 int aos_printf_hook(const char *tag, const char *fmt, enum PRINTF_FLAG_T flag, va_list ap)
252 {
253     int ret = 0;
254 
255     if (!g_dirver_trace_flag) {
256         return 1;
257     }
258 
259     if (fmt) {
260         if (flag == 0) {
261             uint32_t lock = int_lock();
262             ret = vprintf(fmt, ap);
263             int_unlock(lock);
264         } else {
265             ret = do_ulog(LOG_INFO, tag?tag:"SDK_TRACE", ULOG_TAG, fmt, ap);
266         }
267     } else {
268         ret = check_pass_pop_out(ulog_session_std, LOG_ERR) ? 1 : -1;
269     }
270 
271     return ret;
272 }
273 
aos_set_driver_trace_flag(int flag)274 void aos_set_driver_trace_flag(int flag)
275 {
276     g_dirver_trace_flag = flag;
277 }
278 
hal_watchdog_disable(void)279 void hal_watchdog_disable(void)
280 {
281 }
282 
hal_watchdog_reset(int timeout)283 void hal_watchdog_reset(int timeout)
284 {
285 #ifndef CONFIG_GENIE_DEBUG
286 	app_wdt_ping();
287 #endif
288 }
289 
hal_reset_cpu(void)290 void hal_reset_cpu(void)
291 {
292     hal_reboot();
293 }
294 
295 
296 
soc_peripheral_init()297 void soc_peripheral_init()
298 {
299     app_sysfreq_req(APP_SYSFREQ_USER_APP_15, APP_SYSFREQ_390M);
300     printf("sys freq calc : %u \n", hal_sys_timer_calc_cpu_freq(5, 0));
301 }
302 
aos_trace_notify(enum HAL_TRACE_STATE_T state)303 void aos_trace_notify(enum HAL_TRACE_STATE_T state)
304 {
305     if (state == HAL_TRACE_STATE_CRASH_END)
306         abort();
307 }
308 
309 #if AOS_COMP_CLI
310 #ifdef CUSTOM_CLI_ENABLED
char2data(const char ch)311 static uint8_t char2data(const char ch)
312 {
313     if ((ch >= '0') && (ch <= '9')) {
314         return (uint8_t)(ch - '0');
315     }
316     if ((ch >= 'a') && (ch <= 'f')) {
317         return (uint8_t)(ch - 'a' + 10);
318     }
319     if ((ch >= 'A') && (ch <= 'F')) {
320         return (uint8_t)(ch - 'A' + 10);
321     }
322 
323     return 0;
324 }
str2mac(const char * sz_mac,uint8_t * pmac)325 static void str2mac(const char * sz_mac, uint8_t * pmac)
326 {
327     const char * ptemp = sz_mac;
328     for (int i = 0; i < 6; ++i)
329     {
330         pmac[i] = char2data(*ptemp++) * 16;
331         pmac[i] += char2data(*ptemp++);
332         ptemp++;
333     }
334 }
335 extern int factory_section_set_wifi_address(uint8_t *wifi_addr);
336 extern uint8_t* factory_section_get_wifi_address(void);
337 extern int factory_section_set_bt_address(uint8_t *bt_addr);
338 extern uint8_t* factory_section_get_bt_address(void);
339 
handle_aos_mac_cmd(char * pwbuf,int blen,int argc,char ** argv)340 static void handle_aos_mac_cmd(char *pwbuf, int blen, int argc, char **argv)
341 {
342     int ret = 0;
343     const char *rtype = argc > 1 ? argv[1] : "";
344     const char *mac   = argc > 2 ? argv[2] : NULL;
345     uint8_t pmac[6];
346 
347     if(mac != NULL) {
348         str2mac(mac, pmac);
349         printf("will set mac %02x:%02x:%02x:%02x:%02x:%02x\n",
350                 pmac[0], pmac[1], pmac[2], pmac[3], pmac[4], pmac[5]);
351     }
352 
353     if (strcmp(rtype, "WIFI") == 0) {
354         if(mac != NULL) {
355             ret = factory_section_set_wifi_address(pmac);
356             if(ret == 0) {
357                 printf("set WIFI mac success!\n");
358             } else {
359                 printf("set WIFI mac fail!\n");
360             }
361         } else {
362             uint8_t *_mac = factory_section_get_wifi_address();
363             if(_mac == NULL) {
364                 printf("get WIFI mac fail\n");
365             } else {
366                 printf("WIFI mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
367                         _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]);
368             }
369         }
370     } else if (strcmp(rtype, "BT") == 0) {
371         if(mac != NULL) {
372             ret = factory_section_set_bt_address(pmac);
373             if(ret == 0) {
374                 printf("set BT mac success!\n");
375             } else {
376                 printf("set BT mac fail!\n");
377             }
378         } else {
379             uint8_t *_mac = factory_section_get_bt_address();
380             if(_mac == NULL) {
381                 printf("get BT mac fail\n");
382             } else {
383                 printf("BT mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
384                         _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]);
385             }
386         }
387     } else {
388         printf("Usage: aos_mac [WIFI/BT] [XX:XX:XX:XX:XX:XX]");
389     }
390 }
391 
392 static struct cli_command aos_mac_cmd = {
393     .name     = "aos_mac",
394     .help     = "aos_mac [WIFI/BT] [mac]",
395     .function = handle_aos_mac_cmd
396 };
397 
aos_custom_cli_register()398 void aos_custom_cli_register()
399 {
400     aos_cli_register_command(&aos_mac_cmd);
401 }
402 #endif
403 #endif
404 
aos_init_done_hook(void)405 void aos_init_done_hook(void)
406 {
407 #ifdef SWD_ENABLE_AS_DEFAULT
408     // enable swd as default
409     hal_iomux_set_jtag();
410     krhino_idle_hook_onoff(0);
411 #endif
412 
413 #if CONFIG_A7_DSP_ENABLE
414     //heartbeat_init();
415     bes_kv_init();
416 #endif
417     hal_trace_register_hook(aos_printf_hook);
418 
419 #if 0//def CONFIG_A7_DSP_ENABLE
420     // for dsp audiodump
421     aud_dump_init();
422     mcu_audiodump_register(_AUDIO_DUMP_SRC_BEFORE_ALG_, aud_dump_cb_register, aud_dump_cb_unregister, 3, 256, 3000);
423     mcu_audiodump_register(_AUDIO_DUMP_SRC_INTER_ALG_, aud_dump_cb_register, aud_dump_cb_unregister, 1, 16, 10000);
424     mcu_audiodump_register(_AUDIO_DUMP_SRC_AFTER_ALG_, aud_dump_cb_register, aud_dump_cb_unregister, 1, 16, 10000);
425 #endif
426 
427 }
428 
uart_puts(const char * str)429 int uart_puts(const char *str)
430 {
431     return hal_trace_output(str, strlen(str));
432 }
433 
434 // tmp for build, TBD
get_random(void)435 uint32_t get_random(void)
436 {
437     static uint32_t dns_txid;
438     return (++dns_txid);
439 }
440 
sys_thread_exit(void)441 void sys_thread_exit(void)
442 {
443 #if LWIP_NETCONN_SEM_PER_THREAD
444     netconn_thread_cleanup();
445 #endif
446 
447     osThreadExitPub();
448 }
449 
450 #if CONFIG_A7_DSP_ENABLE
451 #include "hal_aud.h"
a7_dsp_reboot()452 void a7_dsp_reboot()
453 {
454     printf("%s", __FUNCTION__);
455     //sendTrace("crashReport","dspreboot",NULL,NULL,NULL);
456     transq_msg_onoff(0);
457     transq_msg_flush();
458 
459     af_stream_stop(AUD_STREAM_ID_0, AUD_STREAM_CAPTURE);
460     af_stream_close(AUD_STREAM_ID_0, AUD_STREAM_CAPTURE);
461 
462     hal_cmu_dsp_stop_cpu();
463 
464     transq_msg_reinit();
465     heartbeat_init();
466 
467     a7_dsp_boot();
468 }
a7_dsp_recover_report()469 void a7_dsp_recover_report()
470 {
471     printf("%s", __FUNCTION__);
472     //sendTrace("crashReport","dspfinish",NULL,NULL,NULL);
473 }
474 #endif
475