board.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-02 hqfang first version
  9. *
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #define SYSTICK_TICK_CONST (SOC_TIMER_FREQ / RT_TICK_PER_SECOND)
  15. #define RT_KERNEL_INTERRUPT_LEVEL 1
  16. #ifdef RT_USING_SERIAL
  17. #include <drv_usart.h>
  18. #endif
  19. /** _end symbol defined in linker script of Nuclei SDK */
  20. extern void *_end;
  21. /** _heap_end symbol defined in linker script of Nuclei SDK */
  22. extern void *_heap_end;
  23. #define HEAP_BEGIN &_end
  24. #define HEAP_END &_heap_end
  25. /*
  26. * - Implemented and defined in Nuclei SDK system_<Device>.c file
  27. * - Required macro NUCLEI_BANNER set to 0
  28. */
  29. extern void _init(void);
  30. /*
  31. * - Check MCU pin assignment here https://doc.nucleisys.com/nuclei_board_labs/hw/hw.html
  32. * - If you changed menuconfig to use different peripherals such as SPI, ADC, GPIO,
  33. * HWTIMER, I2C, PWM, UART, WDT, RTC, please add or change related pinmux configuration
  34. * code in functions(rt_hw_*_drvinit) below
  35. */
  36. void rt_hw_spi_drvinit(void)
  37. {
  38. }
  39. void rt_hw_adc_drvinit(void)
  40. {
  41. }
  42. void rt_hw_gpio_drvinit(void)
  43. {
  44. // Clock on all the GPIOs and AF
  45. rcu_periph_clock_enable(RCU_GPIOA);
  46. rcu_periph_clock_enable(RCU_GPIOB);
  47. rcu_periph_clock_enable(RCU_GPIOC);
  48. rcu_periph_clock_enable(RCU_GPIOD);
  49. rcu_periph_clock_enable(RCU_GPIOE);
  50. rcu_periph_clock_enable(RCU_AF);
  51. }
  52. void rt_hw_hwtimer_drvinit(void)
  53. {
  54. }
  55. void rt_hw_i2c_drvinit(void)
  56. {
  57. }
  58. void rt_hw_pwm_drvinit(void)
  59. {
  60. }
  61. void rt_hw_rtc_drvinit(void)
  62. {
  63. }
  64. void rt_hw_uart_drvinit(void)
  65. {
  66. /* Notice: Debug UART4 GPIO pins are already initialized in nuclei_sdk */
  67. }
  68. void rt_hw_wdt_drvinit(void)
  69. {
  70. }
  71. void rt_hw_drivers_init(void)
  72. {
  73. #ifdef RT_USING_PIN
  74. rt_hw_gpio_drvinit();
  75. #endif
  76. #ifdef BSP_USING_UART
  77. rt_hw_uart_drvinit();
  78. #endif
  79. #ifdef BSP_USING_SPI
  80. rt_hw_spi_drvinit();
  81. #endif
  82. #ifdef BSP_USING_I2C
  83. rt_hw_i2c_drvinit();
  84. #endif
  85. #ifdef BSP_USING_ADC
  86. rt_hw_adc_drvinit();
  87. #endif
  88. #ifdef BSP_USING_WDT
  89. rt_hw_wdt_drvinit();
  90. #endif
  91. #ifdef BSP_USING_RTC
  92. rt_hw_rtc_drvinit();
  93. #endif
  94. #ifdef BSP_USING_HWTIMER
  95. rt_hw_hwtimer_drvinit();
  96. #endif
  97. #ifdef BSP_USING_PWM
  98. rt_hw_pwm_drvinit();
  99. #endif
  100. }
  101. rt_weak void rt_hw_ticksetup(void)
  102. {
  103. uint64_t ticks = SYSTICK_TICK_CONST;
  104. /* Make SWI and SysTick the lowest priority interrupts. */
  105. /* Stop and clear the SysTimer. SysTimer as Non-Vector Interrupt */
  106. SysTick_Config(ticks);
  107. ECLIC_DisableIRQ(SysTimer_IRQn);
  108. ECLIC_SetLevelIRQ(SysTimer_IRQn, RT_KERNEL_INTERRUPT_LEVEL);
  109. ECLIC_SetShvIRQ(SysTimer_IRQn, ECLIC_NON_VECTOR_INTERRUPT);
  110. ECLIC_EnableIRQ(SysTimer_IRQn);
  111. /* Set SWI interrupt level to lowest level/priority, SysTimerSW as Vector Interrupt */
  112. ECLIC_SetShvIRQ(SysTimerSW_IRQn, ECLIC_VECTOR_INTERRUPT);
  113. ECLIC_SetLevelIRQ(SysTimerSW_IRQn, RT_KERNEL_INTERRUPT_LEVEL);
  114. ECLIC_EnableIRQ(SysTimerSW_IRQn);
  115. }
  116. #define SysTick_Handler eclic_mtip_handler
  117. /**
  118. * @brief This is the timer interrupt service routine.
  119. *
  120. */
  121. void SysTick_Handler(void)
  122. {
  123. /* Reload systimer */
  124. SysTick_Reload(SYSTICK_TICK_CONST);
  125. /* enter interrupt */
  126. rt_interrupt_enter();
  127. /* tick increase */
  128. rt_tick_increase();
  129. /* leave interrupt */
  130. rt_interrupt_leave();
  131. }
  132. /**
  133. * @brief Setup hardware board for rt-thread
  134. *
  135. */
  136. void rt_hw_board_init(void)
  137. {
  138. /* OS Tick Configuration */
  139. rt_hw_ticksetup();
  140. #ifdef RT_USING_HEAP
  141. rt_system_heap_init((void *) HEAP_BEGIN, (void *) HEAP_END);
  142. #endif
  143. _init(); // __libc_init_array is not used in RT-Thread
  144. /* Board hardware drivers initialization */
  145. rt_hw_drivers_init();
  146. /* USART driver initialization is open by default */
  147. #ifdef RT_USING_SERIAL
  148. rt_hw_usart_init();
  149. #endif
  150. /* Set the shell console output device */
  151. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  152. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  153. #endif
  154. /* Board underlying hardware initialization */
  155. #ifdef RT_USING_COMPONENTS_INIT
  156. rt_components_board_init();
  157. #endif
  158. }
  159. /******************** end of file *******************/