board.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include "board.h"
  15. #include "uart_console.h"
  16. /**
  17. * @addtogroup simulator on win32
  18. */
  19. rt_uint8_t *rt_hw_sram_init(void)
  20. {
  21. rt_uint8_t *heap;
  22. heap = (rt_uint8_t *)malloc(RT_HEAP_SIZE);
  23. if (heap == RT_NULL)
  24. {
  25. rt_kprintf("there is no memory in pc.");
  26. #ifdef _WIN32
  27. _exit(1);
  28. #else
  29. exit(1);
  30. #endif
  31. }
  32. #ifdef RT_USING_HEAP
  33. /* init memory system */
  34. rt_system_heap_init((void*)heap, (void*)&heap[RT_HEAP_SIZE - 1]);
  35. #endif
  36. return heap;
  37. }
  38. #ifdef _WIN32
  39. #include <windows.h>
  40. #endif
  41. void rt_hw_win32_low_cpu(void)
  42. {
  43. #ifdef _WIN32
  44. /* in windows */
  45. Sleep(1000);
  46. #else
  47. /* in linux */
  48. sleep(1);
  49. #endif
  50. }
  51. #ifdef _MSC_VER
  52. #ifndef _CRT_TERMINATE_DEFINED
  53. #define _CRT_TERMINATE_DEFINED
  54. _CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);
  55. _CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code);
  56. _CRTIMP void __cdecl abort(void);
  57. #endif
  58. #endif
  59. void rt_hw_exit(void)
  60. {
  61. rt_kprintf("RT-Thread, bye\n");
  62. #if !defined(_WIN32) && defined(__GNUC__)
  63. /* *
  64. * getchar reads key from buffer, while finsh need an non-buffer getchar
  65. * in windows, getch is such an function, in linux, we had to change
  66. * the behaviour of terminal to get an non-buffer getchar.
  67. * in usart_sim.c, set_stty is called to do this work
  68. * */
  69. {
  70. extern void restore_stty(void);
  71. restore_stty();
  72. }
  73. #endif
  74. exit(0);
  75. }
  76. #if defined(RT_USING_FINSH)
  77. MSH_CMD_EXPORT_ALIAS(rt_hw_exit, quit, exit rt-thread);
  78. #endif /* RT_USING_FINSH */
  79. /**
  80. * This function will initial win32
  81. */
  82. int rt_hw_board_init(void)
  83. {
  84. /* init system memory */
  85. rt_hw_sram_init();
  86. uart_console_init();
  87. #ifdef _WIN32
  88. rt_thread_idle_sethook(rt_hw_win32_low_cpu);
  89. #endif
  90. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  91. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  92. #endif
  93. /* init board */
  94. #ifdef RT_USING_COMPONENTS_INIT
  95. rt_components_board_init();
  96. #endif
  97. return 0;
  98. }
  99. /*@}*/