gd32f30x_misc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*!
  2. \file gd32f30x_misc.c
  3. \brief MISC driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32f30x_misc.h"
  30. /*!
  31. \brief set the priority group
  32. \param[in] nvic_prigroup: the NVIC priority group
  33. \arg NVIC_PRIGROUP_PRE0_SUB4:0 bits for pre-emption priority 4 bits for subpriority
  34. \arg NVIC_PRIGROUP_PRE1_SUB3:1 bits for pre-emption priority 3 bits for subpriority
  35. \arg NVIC_PRIGROUP_PRE2_SUB2:2 bits for pre-emption priority 2 bits for subpriority
  36. \arg NVIC_PRIGROUP_PRE3_SUB1:3 bits for pre-emption priority 1 bits for subpriority
  37. \arg NVIC_PRIGROUP_PRE4_SUB0:4 bits for pre-emption priority 0 bits for subpriority
  38. \param[out] none
  39. \retval none
  40. */
  41. void nvic_priority_group_set(uint32_t nvic_prigroup)
  42. {
  43. /* set the priority group value */
  44. SCB->AIRCR = NVIC_AIRCR_VECTKEY_MASK | nvic_prigroup;
  45. }
  46. /*!
  47. \brief enable NVIC request
  48. \param[in] nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  49. \param[in] nvic_irq_pre_priority: the pre-emption priority needed to set
  50. \param[in] nvic_irq_sub_priority: the subpriority needed to set
  51. \param[out] none
  52. \retval none
  53. */
  54. void nvic_irq_enable(uint8_t nvic_irq, uint8_t nvic_irq_pre_priority,
  55. uint8_t nvic_irq_sub_priority)
  56. {
  57. uint32_t temp_priority = 0x00U, temp_pre = 0x00U, temp_sub = 0x00U;
  58. /* use the priority group value to get the temp_pre and the temp_sub */
  59. if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE0_SUB4){
  60. temp_pre=0U;
  61. temp_sub=0x4U;
  62. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE1_SUB3){
  63. temp_pre=1U;
  64. temp_sub=0x3U;
  65. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE2_SUB2){
  66. temp_pre=2U;
  67. temp_sub=0x2U;
  68. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE3_SUB1){
  69. temp_pre=3U;
  70. temp_sub=0x1U;
  71. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE4_SUB0){
  72. temp_pre=4U;
  73. temp_sub=0x0U;
  74. }else{
  75. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  76. temp_pre=2U;
  77. temp_sub=0x2U;
  78. }
  79. /* get the temp_priority to fill the NVIC->IP register */
  80. temp_priority = (uint32_t)nvic_irq_pre_priority << (0x4U - temp_pre);
  81. temp_priority |= nvic_irq_sub_priority &(0x0FU >> (0x4U - temp_sub));
  82. temp_priority = temp_priority << 0x04U;
  83. NVIC->IP[nvic_irq] = (uint8_t)temp_priority;
  84. /* enable the selected IRQ */
  85. NVIC->ISER[nvic_irq >> 0x05U] = (uint32_t)0x01U << (nvic_irq & (uint8_t)0x1FU);
  86. }
  87. /*!
  88. \brief disable NVIC request
  89. \param[in] nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  90. \param[out] none
  91. \retval none
  92. */
  93. void nvic_irq_disable(uint8_t nvic_irq)
  94. {
  95. /* disable the selected IRQ.*/
  96. NVIC->ICER[nvic_irq >> 0x05] = (uint32_t)0x01 << (nvic_irq & (uint8_t)0x1F);
  97. }
  98. /*!
  99. \brief set the NVIC vector table base address
  100. \param[in] nvic_vict_tab: the RAM or FLASH base address
  101. \arg NVIC_VECTTAB_RAM: RAM base address
  102. \are NVIC_VECTTAB_FLASH: Flash base address
  103. \param[in] offset: Vector Table offset
  104. \param[out] none
  105. \retval none
  106. */
  107. void nvic_vector_table_set(uint32_t nvic_vict_tab, uint32_t offset)
  108. {
  109. SCB->VTOR = nvic_vict_tab | (offset & NVIC_VECTTAB_OFFSET_MASK);
  110. __DSB();
  111. }
  112. /*!
  113. \brief set the state of the low power mode
  114. \param[in] lowpower_mode: the low power mode state
  115. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system always enter low power
  116. mode by exiting from ISR
  117. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the DEEPSLEEP mode
  118. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode can be woke up
  119. by all the enable and disable interrupts
  120. \param[out] none
  121. \retval none
  122. */
  123. void system_lowpower_set(uint8_t lowpower_mode)
  124. {
  125. SCB->SCR |= (uint32_t)lowpower_mode;
  126. }
  127. /*!
  128. \brief reset the state of the low power mode
  129. \param[in] lowpower_mode: the low power mode state
  130. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system will exit low power
  131. mode by exiting from ISR
  132. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the SLEEP mode
  133. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode only can be
  134. woke up by the enable interrupts
  135. \param[out] none
  136. \retval none
  137. */
  138. void system_lowpower_reset(uint8_t lowpower_mode)
  139. {
  140. SCB->SCR &= (~(uint32_t)lowpower_mode);
  141. }
  142. /*!
  143. \brief set the systick clock source
  144. \param[in] systick_clksource: the systick clock source needed to choose
  145. \arg SYSTICK_CLKSOURCE_HCLK: systick clock source is from HCLK
  146. \arg SYSTICK_CLKSOURCE_HCLK_DIV8: systick clock source is from HCLK/8
  147. \param[out] none
  148. \retval none
  149. */
  150. void systick_clksource_set(uint32_t systick_clksource)
  151. {
  152. if(SYSTICK_CLKSOURCE_HCLK == systick_clksource ){
  153. /* set the systick clock source from HCLK */
  154. SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
  155. }else{
  156. /* set the systick clock source from HCLK/8 */
  157. SysTick->CTRL &= SYSTICK_CLKSOURCE_HCLK_DIV8;
  158. }
  159. }