• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include <fcntl.h>
24 #include <sys/mount.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
28 #include <los_task.h>
29 #include <los_memory.h>
30 
31 #include <devmgr_service_start.h>
32 #include <gpio_if.h>
33 #include <hiview_log.h>
34 #include <hiview_output_log.h>
35 #include <utils_file.h>
36 
37 #include <board_config.h>
38 
39 #include <b91_irq.h>
40 #include <system_b91.h>
41 #include <power_b91.h>
42 
43 #include <B91/clock.h>
44 #include <B91/gpio.h>
45 #include <B91/uart.h>
46 
47 #include <B91/gpio_default.h>
48 
49 #include <../vendor/common/blt_common.h>
50 
51 #include "canary.h"
52 
53 #define HZ_IN_MHZ (1000 * 1000)
54 
55 #define DEBUG_UART_PORT      UART0
56 #define DEBUG_UART_PIN_TX    UART0_TX_PB2
57 #define DEBUG_UART_PIN_RX    UART0_RX_PB3
58 #define DEBUG_UART_PARITY    UART_PARITY_NONE
59 #define DEBUG_UART_STOP_BITS UART_STOP_BIT_ONE
60 #define DEBUG_UART_BAUDRATE  921600
61 
62 #define B91_SYSTEM_INIT_TASK_STACKSIZE (1024 * 32)
63 #define B91_SYSTEM_INIT_TASK_PRIO      7
64 #define B91_SYSTEM_INIT_TASK_NAME      "B91SystemInit"
65 
66 extern UserErrFunc g_userErrFunc;
67 
__wrap_malloc(size_t s)68 void *__wrap_malloc(size_t s)
69 {
70     if (s == 0) {
71         return NULL;
72     }
73     return LOS_MemAlloc(OS_SYS_MEM_ADDR, s);
74 }
75 
__wrap_free(void * ptr)76 void __wrap_free(void *ptr)
77 {
78     if (ptr == NULL) {
79         return;
80     }
81     return LOS_MemFree(OS_SYS_MEM_ADDR, ptr);
82 }
83 
84 void OHOS_SystemInit(void);
85 struct PartitionCfg *LittlefsConfigGet(void);
86 
LittlefsInit(VOID)87 STATIC VOID LittlefsInit(VOID)
88 {
89 #define DIR_DATA "/data"
90 #define PAR_DATA 0
91 #define DIR_PERMISSIONS 0777
92 
93     int res;
94 
95     printf("LittleFS_Init \r\n");
96 
97     struct PartitionCfg *cfg = LittlefsConfigGet();
98 
99     res = mount(PAR_DATA, DIR_DATA, "littlefs", 0, cfg);
100     printf("mount = %d\r\n", res);
101 
102     struct stat sinfo;
103     if (!(stat(DIR_DATA, &sinfo) == 0 && S_ISDIR(sinfo.st_mode))) {
104         res = mkdir(DIR_DATA, DIR_PERMISSIONS);
105         printf("mkdir = %d\r\n", res);
106     }
107 }
108 
HardwareInit(VOID)109 VOID HardwareInit(VOID)
110 {
111     SystemInit();
112 }
113 
IoTWatchDogKick(VOID)114 VOID IoTWatchDogKick(VOID)
115 {
116 }
117 
B91SystemInit(VOID)118 STATIC VOID B91SystemInit(VOID)
119 {
120     OHOS_SystemInit();
121     LittlefsInit();
122 }
123 
LosAppInit(VOID)124 UINT32 LosAppInit(VOID)
125 {
126     UINT32 ret = LOS_OK;
127 
128     B91IrqInit();
129 
130     unsigned int taskID_ohos;
131     TSK_INIT_PARAM_S task_ohos = {0};
132 
133     task_ohos.pfnTaskEntry = (TSK_ENTRY_FUNC)B91SystemInit;
134     task_ohos.uwStackSize = B91_SYSTEM_INIT_TASK_STACKSIZE;
135     task_ohos.pcName = B91_SYSTEM_INIT_TASK_NAME;
136     task_ohos.usTaskPrio = B91_SYSTEM_INIT_TASK_PRIO;
137     ret = LOS_TaskCreate(&taskID_ohos, &task_ohos);
138     if (ret != LOS_OK) {
139         printf("Create Task failed! ERROR: 0x%x\r\n", ret);
140     }
141 
142     return ret;
143 }
144 
_sbrk(ptrdiff_t incr)145 __attribute__((weak)) void *_sbrk(ptrdiff_t incr)
146 {
147     extern char _end[];
148     extern char _heap_end[];
149     static char *curbrk = _end;
150 
151     if ((curbrk + incr < _end) || (curbrk + incr > _heap_end)) {
152         return (void *)(-1);
153     }
154 
155     curbrk += incr;
156     return (void *)(curbrk - incr);
157 }
158 
UsartInit(VOID)159 VOID UsartInit(VOID)
160 {
161     unsigned short div;
162     unsigned char bwpc;
163 
164     uart_set_pin(DEBUG_UART_PIN_TX, DEBUG_UART_PIN_RX);
165     uart_reset(DEBUG_UART_PORT);
166     uart_cal_div_and_bwpc(DEBUG_UART_BAUDRATE, sys_clk.pclk * HZ_IN_MHZ, &div, &bwpc);
167     uart_init(DEBUG_UART_PORT, div, bwpc, DEBUG_UART_PARITY, DEBUG_UART_STOP_BITS);
168     uart_rx_irq_trig_level(DEBUG_UART_PORT, 1);
169 }
170 
_write(int handle,char * data,int size)171 int _write(int handle, char *data, int size)
172 {
173     UNUSED(handle);
174     UNUSED(data);
175     UNUSED(size);
176 
177     int ret = 0;
178 
179     switch (handle) {
180         case STDOUT_FILENO:
181         case STDERR_FILENO: {
182             uart_send(DEBUG_UART_PORT, (unsigned char *)data, size);
183             while (uart_tx_is_busy(DEBUG_UART_PORT)) {
184             }
185             ret = size;
186             break;
187         }
188         default: {
189             break;
190         }
191     }
192 
193     return ret;
194 }
195 
hilog(const HiLogContent * hilogContent,uint32 len)196 static boolean hilog(const HiLogContent *hilogContent, uint32 len)
197 {
198     UNUSED(len);
199     static char buf[400];
200     int32 bytes = LogContentFmt(buf, sizeof(buf), (const uint8 *)hilogContent);
201     _write(STDOUT_FILENO, buf, bytes);
202     return TRUE;
203 }
204 
__assert_func(const char * file,int line,const char * func,const char * expr)205 __attribute__((noreturn)) void __assert_func(const char *file, int line, const char *func, const char *expr)
206 {
207     printf("Assertion failed: %s (%s: %s: %d)\r\n", expr, file, func, line);
208     fflush(NULL);
209     abort();
210 }
211 
UserErrFuncImpl(CHAR * fileName,UINT32 lineNo,UINT32 errorNo,UINT32 paraLen,VOID * para)212 STATIC VOID UserErrFuncImpl(CHAR *fileName, UINT32 lineNo, UINT32 errorNo, UINT32 paraLen, VOID *para)
213 {
214     printf("ERROR: \"/%s\" <<< line: %x err: %x para[%u]: ", fileName, lineNo, errorNo, paraLen);
215 
216     const u8 *pc = (const u8 *)para;
217     for (UINT32 i = 0; i < paraLen; ++i) {
218         printf("%02x", pc[i]);
219     }
220 
221     printf("\r\n");
222 }
223 
224 /**
225  * @brief Inner part of main function without disabled stack guard check
226  * @return
227  */
SafeMain(VOID)228 STATIC INT32 SafeMain(VOID)
229 {
230     UINT32 ret;
231 
232     HardwareInit();
233     UsartInit();
234 
235     printf("\r\n OHOS start \r\n");
236 
237     g_userErrFunc.pfnHook = UserErrFuncImpl;
238     HiLogRegisterProc(hilog);
239 
240     ret = LOS_KernelInit();
241     if (ret != LOS_OK) {
242         printf("Liteos kernel init failed! ERROR: 0x%x\r\n", ret);
243         goto START_FAILED;
244     }
245 
246     if (DeviceManagerStart()) {
247         printf("DeviceManagerStart failed!\r\n");
248     }
249 
250     ret = LosAppInit();
251     if (ret != LOS_OK) {
252         printf("LosAppInit failed! ERROR: 0x%x\r\n", ret);
253         goto START_FAILED;
254     }
255 
256     B91SuspendSleepInit();
257     LOS_Start();
258 
259 START_FAILED:
260     while (1) {
261         __asm__ volatile("wfi");
262     }
263 
264     return 0;
265 }
266 
267 #pragma GCC push_options
268 #pragma GCC optimize("-fno-stack-protector")
main(VOID)269 INT32 main(VOID)
270 {
271 #ifdef __GNUC__
272     ArchStackGuardInit();
273 #endif
274 
275     return SafeMain();
276 }
277 #pragma GCC pop_options
278