1d6aed566Sopenharmony_ci/*
2d6aed566Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3d6aed566Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d6aed566Sopenharmony_ci * you may not use this file except in compliance with the License.
5d6aed566Sopenharmony_ci * You may obtain a copy of the License at
6d6aed566Sopenharmony_ci *
7d6aed566Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d6aed566Sopenharmony_ci *
9d6aed566Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d6aed566Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d6aed566Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d6aed566Sopenharmony_ci * See the License for the specific language governing permissions and
13d6aed566Sopenharmony_ci * limitations under the License.
14d6aed566Sopenharmony_ci */
15d6aed566Sopenharmony_ci
16d6aed566Sopenharmony_ci#include "los_tick.h"
17d6aed566Sopenharmony_ci#include "los_task.h"
18d6aed566Sopenharmony_ci#include "los_config.h"
19d6aed566Sopenharmony_ci#include "los_interrupt.h"
20d6aed566Sopenharmony_ci#include "los_debug.h"
21d6aed566Sopenharmony_ci#include "uart.h"
22d6aed566Sopenharmony_ci
23d6aed566Sopenharmony_ci#ifdef LOSCFG_NET_LWIP
24d6aed566Sopenharmony_ci
25d6aed566Sopenharmony_ci#define IFNAMSIZ  IF_NAMESIZE
26d6aed566Sopenharmony_ci#include "lwip/tcpip.h"
27d6aed566Sopenharmony_ci#include "lwip/netif.h"
28d6aed566Sopenharmony_ci#include "lwip/netifapi.h"
29d6aed566Sopenharmony_ci
30d6aed566Sopenharmony_ci#define SLEEP_TIME_MS 60
31d6aed566Sopenharmony_ci#define NETIF_SETUP_OVERTIME 100
32d6aed566Sopenharmony_ci
33d6aed566Sopenharmony_civoid net_init(void)
34d6aed566Sopenharmony_ci{
35d6aed566Sopenharmony_ci    extern void tcpip_init(tcpip_init_done_fn initfunc, void *arg);
36d6aed566Sopenharmony_ci    extern struct netif *VirtnetInit(void);
37d6aed566Sopenharmony_ci
38d6aed566Sopenharmony_ci    static unsigned int overtime = 0;
39d6aed566Sopenharmony_ci
40d6aed566Sopenharmony_ci    printf("tcpip_init start\n");
41d6aed566Sopenharmony_ci    tcpip_init(NULL, NULL);
42d6aed566Sopenharmony_ci    printf("tcpip_init end\n");
43d6aed566Sopenharmony_ci
44d6aed566Sopenharmony_ci    printf("netif init start...\n");
45d6aed566Sopenharmony_ci    struct netif *pnetif = VirtnetInit();
46d6aed566Sopenharmony_ci    if (pnetif) {
47d6aed566Sopenharmony_ci        netif_set_default(pnetif);
48d6aed566Sopenharmony_ci        (void)netifapi_netif_set_up(pnetif);
49d6aed566Sopenharmony_ci        do {
50d6aed566Sopenharmony_ci            LOS_UDelay(SLEEP_TIME_MS);
51d6aed566Sopenharmony_ci            overtime++;
52d6aed566Sopenharmony_ci            if (overtime > NETIF_SETUP_OVERTIME) {
53d6aed566Sopenharmony_ci                PRINT_ERR("netif_is_link_up overtime!\n");
54d6aed566Sopenharmony_ci                break;
55d6aed566Sopenharmony_ci            }
56d6aed566Sopenharmony_ci        } while (netif_is_link_up(pnetif) == 0);
57d6aed566Sopenharmony_ci        if (overtime <= NETIF_SETUP_OVERTIME) {
58d6aed566Sopenharmony_ci            printf("netif init succeed!\n");
59d6aed566Sopenharmony_ci        }
60d6aed566Sopenharmony_ci    } else {
61d6aed566Sopenharmony_ci        printf("netif init failed!\n");
62d6aed566Sopenharmony_ci    }
63d6aed566Sopenharmony_ci}
64d6aed566Sopenharmony_ci#endif /* LOSCFG_NET_LWIP */
65d6aed566Sopenharmony_ci
66d6aed566Sopenharmony_ciUINT32 LosAppInit(VOID);
67d6aed566Sopenharmony_ci
68d6aed566Sopenharmony_ciUINT32 QemuCLZ(UINT32 data)
69d6aed566Sopenharmony_ci{
70d6aed566Sopenharmony_ci    UINT32 count = 32; /* 32-bit data length */
71d6aed566Sopenharmony_ci    if (data == 0) {
72d6aed566Sopenharmony_ci        return count;
73d6aed566Sopenharmony_ci    }
74d6aed566Sopenharmony_ci
75d6aed566Sopenharmony_ci    if (data & 0xFFFF0000) {
76d6aed566Sopenharmony_ci        data = data >> 16; /* 16-bit data length */
77d6aed566Sopenharmony_ci        count -= 16; /* 16-bit data length */
78d6aed566Sopenharmony_ci    }
79d6aed566Sopenharmony_ci
80d6aed566Sopenharmony_ci    if (data & 0xFF00) {
81d6aed566Sopenharmony_ci        data = data >> 8; /* 8-bit data length */
82d6aed566Sopenharmony_ci        count -= 8; /* 8-bit data length */
83d6aed566Sopenharmony_ci    }
84d6aed566Sopenharmony_ci
85d6aed566Sopenharmony_ci    if (data & 0xF0) {
86d6aed566Sopenharmony_ci        data = data >> 4; /* 4-bit data length */
87d6aed566Sopenharmony_ci        count -= 4; /* 4-bit data length */
88d6aed566Sopenharmony_ci    }
89d6aed566Sopenharmony_ci
90d6aed566Sopenharmony_ci    if (data & 0x8) {
91d6aed566Sopenharmony_ci        return (count - 4); /* 4-bit data length */
92d6aed566Sopenharmony_ci    } else if (data & 0x4) {
93d6aed566Sopenharmony_ci        return (count - 3); /* 3-bit data length */
94d6aed566Sopenharmony_ci    } else if (data & 0x2) {
95d6aed566Sopenharmony_ci        return (count - 2); /* 2-bit data length */
96d6aed566Sopenharmony_ci    } else if (data & 0x1) {
97d6aed566Sopenharmony_ci        return (count - 1);
98d6aed566Sopenharmony_ci    }
99d6aed566Sopenharmony_ci
100d6aed566Sopenharmony_ci    return 0;
101d6aed566Sopenharmony_ci}
102d6aed566Sopenharmony_ci
103d6aed566Sopenharmony_ci/*****************************************************************************
104d6aed566Sopenharmony_ci Function    : main
105d6aed566Sopenharmony_ci Description : Main function entry
106d6aed566Sopenharmony_ci Input       : None
107d6aed566Sopenharmony_ci Output      : None
108d6aed566Sopenharmony_ci Return      : None
109d6aed566Sopenharmony_ci *****************************************************************************/
110d6aed566Sopenharmony_ciLITE_OS_SEC_TEXT_INIT INT32 main(VOID)
111d6aed566Sopenharmony_ci{
112d6aed566Sopenharmony_ci    UINT32 ret;
113d6aed566Sopenharmony_ci
114d6aed566Sopenharmony_ci    UartInit();
115d6aed566Sopenharmony_ci    printf("\n OHOS start \n\r");
116d6aed566Sopenharmony_ci
117d6aed566Sopenharmony_ci    ret = LOS_KernelInit();
118d6aed566Sopenharmony_ci    if (ret != LOS_OK) {
119d6aed566Sopenharmony_ci        PRINT_ERR("LiteOS kernel init failed! ERROR: 0x%x\n", ret);
120d6aed566Sopenharmony_ci        goto START_FAILED;
121d6aed566Sopenharmony_ci    }
122d6aed566Sopenharmony_ci
123d6aed566Sopenharmony_ci    HalPlicInit();
124d6aed566Sopenharmony_ci
125d6aed566Sopenharmony_ci    Uart0RxIrqRegister();
126d6aed566Sopenharmony_ci
127d6aed566Sopenharmony_ci#if (LOSCFG_USE_SHELL == 1)
128d6aed566Sopenharmony_ci    ret = LosShellInit();
129d6aed566Sopenharmony_ci    if (ret != LOS_OK) {
130d6aed566Sopenharmony_ci        printf("LosShellInit failed! ERROR: 0x%x\n", ret);
131d6aed566Sopenharmony_ci    }
132d6aed566Sopenharmony_ci#endif
133d6aed566Sopenharmony_ci
134d6aed566Sopenharmony_ci    printf("\nFb init begin...\n");
135d6aed566Sopenharmony_ci    if (fb_init() != LOS_OK) {
136d6aed566Sopenharmony_ci        PRINT_ERR("Fb init failed!");
137d6aed566Sopenharmony_ci    }
138d6aed566Sopenharmony_ci    printf("Fb int end\n");
139d6aed566Sopenharmony_ci
140d6aed566Sopenharmony_ci    printf("\nDeviceManagerStart start ...\n");
141d6aed566Sopenharmony_ci    if (DeviceManagerStart()) {
142d6aed566Sopenharmony_ci        PRINT_ERR("No drivers need load by hdf manager!");
143d6aed566Sopenharmony_ci    }
144d6aed566Sopenharmony_ci    printf("DeviceManagerStart end ...\n");
145d6aed566Sopenharmony_ci
146d6aed566Sopenharmony_ci#ifdef LOSCFG_NET_LWIP
147d6aed566Sopenharmony_ci    net_init();
148d6aed566Sopenharmony_ci#endif /* LOSCFG_NET_LWIP */
149d6aed566Sopenharmony_ci
150d6aed566Sopenharmony_ci    SystemAdapterInit();
151d6aed566Sopenharmony_ci    ret = LosAppInit();
152d6aed566Sopenharmony_ci    if (ret != LOS_OK) {
153d6aed566Sopenharmony_ci        PRINT_ERR("LosAppInit failed! ERROR: 0x%x\n", ret);
154d6aed566Sopenharmony_ci    }
155d6aed566Sopenharmony_ci
156d6aed566Sopenharmony_ci    LOS_Start();
157d6aed566Sopenharmony_ci
158d6aed566Sopenharmony_ciSTART_FAILED:
159d6aed566Sopenharmony_ci    while (1) {
160d6aed566Sopenharmony_ci        __asm volatile("wfi");
161d6aed566Sopenharmony_ci    }
162d6aed566Sopenharmony_ci}
163d6aed566Sopenharmony_ci
164d6aed566Sopenharmony_civoid *ioremap(uintptr_t paddr, unsigned long size)
165d6aed566Sopenharmony_ci{
166d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
167d6aed566Sopenharmony_ci    return (void *)paddr;
168d6aed566Sopenharmony_ci}
169d6aed566Sopenharmony_ci
170d6aed566Sopenharmony_civoid iounmap(void *vaddr)
171d6aed566Sopenharmony_ci{
172d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
173d6aed566Sopenharmony_ci}
174d6aed566Sopenharmony_ci
175d6aed566Sopenharmony_ciint32_t I2cOpen(int16_t number)
176d6aed566Sopenharmony_ci{
177d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
178d6aed566Sopenharmony_ci    return 0;
179d6aed566Sopenharmony_ci}
180d6aed566Sopenharmony_ci
181d6aed566Sopenharmony_ciint32_t GpioRead(uint16_t gpio, uint16_t *val)
182d6aed566Sopenharmony_ci{
183d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
184d6aed566Sopenharmony_ci    return 0;
185d6aed566Sopenharmony_ci}
186d6aed566Sopenharmony_ci
187d6aed566Sopenharmony_ciint32_t GpioSetIrq(uint16_t gpio, uint16_t mode, void *func, void *arg)
188d6aed566Sopenharmony_ci{
189d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
190d6aed566Sopenharmony_ci    return 0;
191d6aed566Sopenharmony_ci}
192d6aed566Sopenharmony_ci
193d6aed566Sopenharmony_ciint32_t GpioSetDir(uint16_t gpio, uint16_t dir)
194d6aed566Sopenharmony_ci{
195d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
196d6aed566Sopenharmony_ci    return 0;
197d6aed566Sopenharmony_ci}
198d6aed566Sopenharmony_ci
199d6aed566Sopenharmony_ciint32_t GpioEnableIrq(uint16_t gpio)
200d6aed566Sopenharmony_ci{
201d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
202d6aed566Sopenharmony_ci    return 0;
203d6aed566Sopenharmony_ci}
204d6aed566Sopenharmony_ci
205d6aed566Sopenharmony_ciint32_t GpioDisableIrq(uint16_t gpio)
206d6aed566Sopenharmony_ci{
207d6aed566Sopenharmony_ci    printf("[WARN] Function to be implemented: %s\n", __FUNCTION__);
208d6aed566Sopenharmony_ci    return 0;
209d6aed566Sopenharmony_ci}
210