gd32f10x_misc.c 6.9 KB

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