ch56x_pfic.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <rtdebug.h>
  12. #include "ch56x_pfic.h"
  13. #include "ch56x_sys.h"
  14. #include "isr_sp.h"
  15. void rt_hw_interrupt_mask(int vector)
  16. {
  17. pfic_interrupt_mask(vector);
  18. }
  19. void rt_hw_interrupt_umask(int vector)
  20. {
  21. pfic_interrupt_umask(vector);
  22. }
  23. /**
  24. * @brief Trigger software interrupt.
  25. */
  26. void pfic_swi_pendset(void)
  27. {
  28. volatile struct pfic_registers *pfic = (void *)PFIC_REG_BASE;
  29. _pfic_ireg_bit_set(pfic, IPSR, SWI_IRQn);
  30. }
  31. /**
  32. * @brief Clear software interrupt.
  33. */
  34. void pfic_swi_pendreset(void)
  35. {
  36. volatile struct pfic_registers *pfic = (void *)PFIC_REG_BASE;
  37. _pfic_ireg_bit_set(pfic, IPRR, SWI_IRQn);
  38. }
  39. /**
  40. * @brief Write PFIC interrupt configuration register.
  41. *
  42. * @param key_bit is (PFIC_CFGR_KEYx + bit_position), one of the following :
  43. * PFIC_CFGR_NMISET / PFIC_CFGR_NMIRESET
  44. * PFIC_CFGR_EXCSET / PFIC_CFGR_EXCRESET
  45. * PFIC_CFGR_PFICRESET
  46. * PFIC_CFGR_SYSRESET
  47. * All others are treated as NEST/HWSTK (B.1/B.0) write.
  48. */
  49. void pfic_cfgr_set(uint32_t key_bit)
  50. {
  51. volatile struct pfic_registers *pfic = (void *)PFIC_REG_BASE;
  52. uint32_t u32v;
  53. switch (key_bit)
  54. {
  55. case PFIC_CFGR_NMISET:
  56. case PFIC_CFGR_NMIRESET:
  57. case PFIC_CFGR_EXCSET:
  58. case PFIC_CFGR_EXCRESET:
  59. case PFIC_CFGR_PFICRESET:
  60. case PFIC_CFGR_SYSRESET:
  61. pfic->CFGR = key_bit;
  62. default:
  63. /* B.1/B.0 hold NEST/HWSTK, key ignored */
  64. u32v = key_bit & (CFGR_NESTCTRL_MASK | CFGR_HWSTKCTRL_MASK);
  65. pfic->CFGR = cfgr_nest_hwstk(u32v);
  66. }
  67. }
  68. /**
  69. * @brief Make SysTick ready, systick/swi irq are enabled.
  70. *
  71. * @param count is (HCLK/8) clocks count to generate systick irq.
  72. * if 0 => calculate with current HCLK and RT_TICK_PER_SECOND
  73. */
  74. void systick_init(uint32_t count)
  75. {
  76. volatile struct systick_registers *systick = (void *)SysTick_REG_BASE;
  77. volatile struct pfic_registers *pfic = (void *)PFIC_REG_BASE;
  78. if (count == 0)
  79. count = sys_hclk_get() / 8 / RT_TICK_PER_SECOND;
  80. _pfic_irqn_disable(pfic, SysTick_IRQn);
  81. pfic->IPRIOR[SysTick_IRQn] = 0xe0;
  82. pfic->IPRIOR[SWI_IRQn] = 0xf0;
  83. systick->CTLR.reg = 0;
  84. systick->CNTL = 0;
  85. systick->CNTH = 0;
  86. systick->CMPLR = count - 1;
  87. systick->CMPHR = 0;
  88. systick->CNTFG.cntif = 0;
  89. /* enable & reload SysTick, with HCLK/8 */
  90. systick->CTLR.reg = RB_STKCTL_STRELOAD | RB_STKCTL_STIE | RB_STKCTL_STE;
  91. _pfic_irqn_enable(pfic, SysTick_IRQn);
  92. _pfic_irqn_enable(pfic, SWI_IRQn);
  93. }
  94. void systick_handler(void) __attribute__((interrupt()));
  95. void systick_handler(void)
  96. {
  97. volatile struct systick_registers *systick;
  98. isr_sp_enter();
  99. rt_interrupt_enter();
  100. rt_tick_increase();
  101. systick = (struct systick_registers *)SysTick_REG_BASE;
  102. /* clear count-to-zero flag */
  103. systick->CNTFG.cntif = 0;
  104. rt_interrupt_leave();
  105. isr_sp_leave();
  106. }