board.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-15 Emuzit first version
  9. */
  10. #include <rthw.h>
  11. #include <drivers/pin.h>
  12. #include "board.h"
  13. #include "ch56x_pfic.h"
  14. #include "ch56x_uart.h"
  15. extern rt_uint32_t rt_thread_switch_interrupt_flag;
  16. /* FIXME: Use rt_interrupt_leave_hook to trigger SWI for context switch.
  17. * Hopefully there's a standard riscv way instead of this clumsy patch.
  18. */
  19. static void irq_leave_hook(void)
  20. {
  21. if (rt_thread_switch_interrupt_flag)
  22. {
  23. pfic_swi_pendset();
  24. }
  25. }
  26. /*
  27. * _start -> handle_reset
  28. * src/components.c/entry() -> rtthread_startup()
  29. * libcpu/risc-v/common/context_gcc.S/rt_hw_interrupt_disable
  30. */
  31. void rt_hw_board_init()
  32. {
  33. volatile struct pfic_registers *pfic = (void *)PFIC_REG_BASE;
  34. /* disable all pfic interrupts */
  35. pfic->IRER[0] = PFIC_IREG1_MASK;
  36. pfic->IRER[1] = PFIC_IREG2_MASK;
  37. /* disable hwstack push/pop & nested interrupt */
  38. pfic->CFGR = cfgr_nest_hwstk(CFGR_NESTCTRL_DISABLE | CFGR_HWSTKCTRL_DISABLE);
  39. /* disable clock input for all peripheral devices */
  40. sys_slp_clk_off0(0xff, SYS_SLP_CLK_OFF);
  41. sys_slp_clk_off1(0xff, SYS_SLP_CLK_OFF);
  42. sys_clk_off_by_irqn(ETH_IRQn, SYS_SLP_CLK_OFF);
  43. sys_clk_off_by_irqn(ECDC_IRQn, SYS_SLP_CLK_OFF);
  44. /* setup HCLK for systick & peripheral devices */
  45. sys_hclk_set(SYS_HCLK_FREQ);
  46. /* set SysTick to RT_TICK_PER_SECOND with current HCLK */
  47. systick_init(0);
  48. /* Note: keep MSTATUS_MIE disabled to prevent SysTick from processing
  49. * thread scheduling, which may not be ready upon 1st systick irq.
  50. * MSTATUS_MIE will be set when rt_system_scheduler_start() starts
  51. * the first thread and copies mstatus from stack_frame.
  52. */
  53. #ifdef RT_USING_HEAP
  54. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  55. #endif
  56. #ifdef RT_USING_COMPONENTS_INIT
  57. rt_components_board_init();
  58. #endif
  59. #ifdef RT_USING_CONSOLE
  60. rt_hw_uart_init();
  61. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  62. #endif
  63. rt_interrupt_leave_sethook(irq_leave_hook);
  64. }