board.c 2.2 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8 9 10 11
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      first implementation
 */
#include <rthw.h>
#include <rtthread.h>
12

M
Ming, Bai 已提交
13 14
#include <stdlib.h>

15 16 17
#include "board.h"
#include "uart_console.h"

M
Ming, Bai 已提交
18 19 20 21 22 23 24 25 26 27 28
/**
 * @addtogroup simulator on win32
 */

rt_uint8_t *rt_hw_sram_init(void)
{
    rt_uint8_t *heap;
    heap = malloc(RT_HEAP_SIZE);
    if (heap == RT_NULL)
    {
        rt_kprintf("there is no memory in pc.");
P
prife 已提交
29
#ifdef _WIN32
M
Ming, Bai 已提交
30
        _exit(1);
P
prife 已提交
31 32 33
#else
        exit(1);
#endif
M
Ming, Bai 已提交
34
    }
35 36 37 38
#ifdef RT_USING_HEAP
    /* init memory system */
    rt_system_heap_init((void*)heap, (void*)&heap[RT_HEAP_SIZE - 1]);
#endif
M
Ming, Bai 已提交
39 40 41
    return heap;
}

P
prife 已提交
42 43 44 45
#ifdef _WIN32
#include <windows.h>
#endif

M
Ming, Bai 已提交
46 47
void rt_hw_win32_low_cpu(void)
{
P
prife 已提交
48 49
#ifdef _WIN32
    /* in windows */
M
Ming, Bai 已提交
50
    Sleep(1000);
P
prife 已提交
51 52 53 54
#else
    /* in linux */
    sleep(1);
#endif
M
Ming, Bai 已提交
55 56
}

57
#ifdef _MSC_VER
M
Ming, Bai 已提交
58 59 60 61 62 63
#ifndef _CRT_TERMINATE_DEFINED
#define _CRT_TERMINATE_DEFINED
_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);
_CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code);
_CRTIMP void __cdecl abort(void);
#endif
P
prife 已提交
64
#endif
M
Ming, Bai 已提交
65 66 67 68

void rt_hw_exit(void)
{
    rt_kprintf("RT-Thread, bye\n");
P
prife 已提交
69 70 71 72 73 74 75 76 77 78 79 80
#if !defined(_WIN32) && defined(__GNUC__)
    /* *
     * getchar reads key from buffer, while finsh need an non-buffer getchar
     * in windows, getch is such an function, in linux, we had to change
     * the behaviour of terminal to get an non-buffer getchar.
     * in usart_sim.c, set_stty is called to do this work
     * */
    {
        extern void restore_stty(void);
        restore_stty();
    }
#endif
M
Ming, Bai 已提交
81 82
    exit(0);
}
83 84 85

#if defined(RT_USING_FINSH)
#include <finsh.h>
M
Ming, Bai 已提交
86
FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread);
87
MSH_CMD_EXPORT_ALIAS(rt_hw_exit, quit, exit rt-thread);
M
Ming, Bai 已提交
88 89 90 91 92
#endif /* RT_USING_FINSH */

/**
 * This function will initial win32
 */
93
int rt_hw_board_init(void)
M
Ming, Bai 已提交
94 95
{
    /* init system memory */
96
    rt_hw_sram_init();
M
Ming, Bai 已提交
97

98 99 100 101 102
    uart_console_init();

#ifdef _WIN32
    rt_thread_idle_sethook(rt_hw_win32_low_cpu);
#endif
P
prife 已提交
103 104

#if defined(RT_USING_CONSOLE)
M
Ming, Bai 已提交
105 106
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
107 108 109 110 111
    /* init board */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif
    return 0;
M
Ming, Bai 已提交
112 113
}
/*@}*/