1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2009-01-05     Bernard      first implementation
9  */
10 #include <rthw.h>
11 #include <rtthread.h>
12 
13 #include <stdlib.h>
14 #include <unistd.h>
15 
16 #include "board.h"
17 #include "uart_console.h"
18 
19 /**
20  * @addtogroup simulator on win32
21  */
22 
rt_hw_sram_init(void)23 rt_uint8_t *rt_hw_sram_init(void)
24 {
25     rt_uint8_t *heap;
26     heap = (rt_uint8_t *)malloc(RT_HEAP_SIZE);
27     if (heap == RT_NULL)
28     {
29         rt_kprintf("there is no memory in pc.");
30 #ifdef _WIN32
31         _exit(1);
32 #else
33         exit(1);
34 #endif
35     }
36 #ifdef RT_USING_HEAP
37     /* init memory system */
38     rt_system_heap_init((void*)heap, (void*)&heap[RT_HEAP_SIZE - 1]);
39 #endif
40     return heap;
41 }
42 
43 #ifdef _WIN32
44 #include <windows.h>
45 #endif
46 
rt_hw_win32_low_cpu(void)47 void rt_hw_win32_low_cpu(void)
48 {
49 #ifdef _WIN32
50     /* in windows */
51     Sleep(1000);
52 #else
53     /* in linux */
54     sleep(1);
55 #endif
56 }
57 
58 #ifdef _MSC_VER
59 #ifndef _CRT_TERMINATE_DEFINED
60 #define _CRT_TERMINATE_DEFINED
61 _CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);
62 _CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code);
63 _CRTIMP void __cdecl abort(void);
64 #endif
65 #endif
66 
rt_hw_exit(void)67 void rt_hw_exit(void)
68 {
69     rt_kprintf("RT-Thread, bye\n");
70 #if !defined(_WIN32) && defined(__GNUC__)
71     /* *
72      * getchar reads key from buffer, while finsh need an non-buffer getchar
73      * in windows, getch is such an function, in linux, we had to change
74      * the behaviour of terminal to get an non-buffer getchar.
75      * in usart_sim.c, set_stty is called to do this work
76      * */
77     {
78         extern void restore_stty(void);
79         restore_stty();
80     }
81 #endif
82     exit(0);
83 }
84 
85 #if defined(RT_USING_FINSH)
86 #include <finsh.h>
87 FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread);
88 MSH_CMD_EXPORT_ALIAS(rt_hw_exit, quit, exit rt-thread);
89 #endif /* RT_USING_FINSH */
90 
91 /**
92  * This function will initial win32
93  */
rt_hw_board_init(void)94 int rt_hw_board_init(void)
95 {
96     /* init system memory */
97     rt_hw_sram_init();
98 
99     uart_console_init();
100 
101 #ifdef _WIN32
102     rt_thread_idle_sethook(rt_hw_win32_low_cpu);
103 #endif
104 
105 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
106     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
107 #endif
108     /* init board */
109 #ifdef RT_USING_COMPONENTS_INIT
110     rt_components_board_init();
111 #endif
112     return 0;
113 }
114 
115 /*@}*/
116