gd32f3x0_misc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*!
  2. \file gd32f3x0_misc.c
  3. \brief MISC driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0_misc.h"
  10. /*!
  11. \brief set the priority group
  12. \param[in] nvic_prigroup: the NVIC priority group
  13. \arg NVIC_PRIGROUP_PRE0_SUB4:0 bits for pre-emption priority 4 bits for subpriority
  14. \arg NVIC_PRIGROUP_PRE1_SUB3:1 bits for pre-emption priority 3 bits for subpriority
  15. \arg NVIC_PRIGROUP_PRE2_SUB2:2 bits for pre-emption priority 2 bits for subpriority
  16. \arg NVIC_PRIGROUP_PRE3_SUB1:3 bits for pre-emption priority 1 bits for subpriority
  17. \arg NVIC_PRIGROUP_PRE4_SUB0:4 bits for pre-emption priority 0 bits for subpriority
  18. \param[out] none
  19. \retval none
  20. */
  21. void nvic_priority_group_set(uint32_t nvic_prigroup)
  22. {
  23. /* set the priority group value */
  24. SCB->AIRCR = NVIC_AIRCR_VECTKEY_MASK | nvic_prigroup;
  25. }
  26. /*!
  27. \brief enable NVIC request
  28. \param[in] nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  29. \param[in] nvic_irq_pre_priority: the pre-emption priority needed to set
  30. \param[in] nvic_irq_sub_priority: the subpriority needed to set
  31. \param[out] none
  32. \retval none
  33. */
  34. void nvic_irq_enable(uint8_t nvic_irq,
  35. uint8_t nvic_irq_pre_priority,
  36. uint8_t nvic_irq_sub_priority)
  37. {
  38. uint32_t temp_priority = 0x00U, temp_pre = 0x00U, temp_sub = 0x00U;
  39. /* use the priority group value to get the temp_pre and the temp_sub */
  40. switch ((SCB->AIRCR) & (uint32_t)0x700U) {
  41. case NVIC_PRIGROUP_PRE0_SUB4:
  42. temp_pre = 0U;
  43. temp_sub = 0x4U;
  44. break;
  45. case NVIC_PRIGROUP_PRE1_SUB3:
  46. temp_pre = 1U;
  47. temp_sub = 0x3U;
  48. break;
  49. case NVIC_PRIGROUP_PRE2_SUB2:
  50. temp_pre = 2U;
  51. temp_sub = 0x2U;
  52. break;
  53. case NVIC_PRIGROUP_PRE3_SUB1:
  54. temp_pre = 3U;
  55. temp_sub = 0x1U;
  56. break;
  57. case NVIC_PRIGROUP_PRE4_SUB0:
  58. temp_pre = 4U;
  59. temp_sub = 0x0U;
  60. break;
  61. default:
  62. break;
  63. }
  64. /* get the temp_priority to fill the NVIC->IP register */
  65. temp_priority = (uint32_t)nvic_irq_pre_priority << (0x4U - temp_pre);
  66. temp_priority |= nvic_irq_sub_priority &(0x0FU >> (0x4U - temp_sub));
  67. temp_priority = temp_priority << 0x04U;
  68. NVIC->IP[nvic_irq] = (uint8_t)temp_priority;
  69. /* enable the selected IRQ */
  70. NVIC->ISER[nvic_irq >> 0x05U] = (uint32_t)0x01U << (nvic_irq & (uint8_t)0x1FU);
  71. }
  72. /*!
  73. \brief disable NVIC request
  74. \param[in] nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  75. \param[out] none
  76. \retval none
  77. */
  78. void nvic_irq_disable(uint8_t nvic_irq)
  79. {
  80. /* disable the selected IRQ.*/
  81. NVIC->ICER[nvic_irq >> 0x05] = (uint32_t)0x01 << (nvic_irq & (uint8_t)0x1F);
  82. }
  83. /*!
  84. \brief set the NVIC vector table base address
  85. \param[in] nvic_vict_tab: the RAM or FLASH base address
  86. \arg NVIC_VECTTAB_RAM: RAM base address
  87. \are NVIC_VECTTAB_FLASH: Flash base address
  88. \param[in] offset: Vector Table offset
  89. \param[out] none
  90. \retval none
  91. */
  92. void nvic_vector_table_set(uint32_t nvic_vict_tab, uint32_t offset)
  93. {
  94. SCB->VTOR = nvic_vict_tab | (offset & NVIC_VECTTAB_OFFSET_MASK);
  95. }
  96. /*!
  97. \brief set the state of the low power mode
  98. \param[in] lowpower_mode: the low power mode state
  99. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system always enter low power
  100. mode by exiting from ISR
  101. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the DEEPSLEEP mode
  102. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode can be woke up
  103. by all the enable and disable interrupts
  104. \param[out] none
  105. \retval none
  106. */
  107. void system_lowpower_set(uint8_t lowpower_mode)
  108. {
  109. SCB->SCR |= (uint32_t)lowpower_mode;
  110. }
  111. /*!
  112. \brief reset the state of the low power mode
  113. \param[in] lowpower_mode: the low power mode state
  114. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system will exit low power
  115. mode by exiting from ISR
  116. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the SLEEP mode
  117. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode only can be
  118. woke up by the enable interrupts
  119. \param[out] none
  120. \retval none
  121. */
  122. void system_lowpower_reset(uint8_t lowpower_mode)
  123. {
  124. SCB->SCR &= (~(uint32_t)lowpower_mode);
  125. }
  126. /*!
  127. \brief set the systick clock source
  128. \param[in] systick_clksource: the systick clock source needed to choose
  129. \arg SYSTICK_CLKSOURCE_HCLK: systick clock source is from HCLK
  130. \arg SYSTICK_CLKSOURCE_HCLK_DIV8: systick clock source is from HCLK/8
  131. \param[out] none
  132. \retval none
  133. */
  134. void systick_clksource_set(uint32_t systick_clksource)
  135. {
  136. if(SYSTICK_CLKSOURCE_HCLK == systick_clksource ){
  137. /* set the systick clock source from HCLK */
  138. SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
  139. }else{
  140. /* set the systick clock source from HCLK/8 */
  141. SysTick->CTRL &= SYSTICK_CLKSOURCE_HCLK_DIV8;
  142. }
  143. }