gd32f3x0_misc.c 7.2 KB

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