gd32f30x_misc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*!
  2. \file gd32f30x_misc.c
  3. \brief MISC driver
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  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 "gd32f30x_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, 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. if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE0_SUB4){
  63. temp_pre=0U;
  64. temp_sub=0x4U;
  65. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE1_SUB3){
  66. temp_pre=1U;
  67. temp_sub=0x3U;
  68. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE2_SUB2){
  69. temp_pre=2U;
  70. temp_sub=0x2U;
  71. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE3_SUB1){
  72. temp_pre=3U;
  73. temp_sub=0x1U;
  74. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE4_SUB0){
  75. temp_pre=4U;
  76. temp_sub=0x0U;
  77. }else{
  78. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  79. temp_pre=2U;
  80. temp_sub=0x2U;
  81. }
  82. /* get the temp_priority to fill the NVIC->IP register */
  83. temp_priority = (uint32_t)nvic_irq_pre_priority << (0x4U - temp_pre);
  84. temp_priority |= nvic_irq_sub_priority &(0x0FU >> (0x4U - temp_sub));
  85. temp_priority = temp_priority << 0x04U;
  86. NVIC->IP[nvic_irq] = (uint8_t)temp_priority;
  87. /* enable the selected IRQ */
  88. NVIC->ISER[nvic_irq >> 0x05U] = (uint32_t)0x01U << (nvic_irq & (uint8_t)0x1FU);
  89. }
  90. /*!
  91. \brief disable NVIC request
  92. \param[in] nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  93. \param[out] none
  94. \retval none
  95. */
  96. void nvic_irq_disable(uint8_t nvic_irq)
  97. {
  98. /* disable the selected IRQ.*/
  99. NVIC->ICER[nvic_irq >> 0x05] = (uint32_t)0x01 << (nvic_irq & (uint8_t)0x1F);
  100. }
  101. /*!
  102. \brief set the NVIC vector table base address
  103. \param[in] nvic_vict_tab: the RAM or FLASH base address
  104. \arg NVIC_VECTTAB_RAM: RAM base address
  105. \are NVIC_VECTTAB_FLASH: Flash base address
  106. \param[in] offset: Vector Table offset
  107. \param[out] none
  108. \retval none
  109. */
  110. void nvic_vector_table_set(uint32_t nvic_vict_tab, uint32_t offset)
  111. {
  112. SCB->VTOR = nvic_vict_tab | (offset & NVIC_VECTTAB_OFFSET_MASK);
  113. __DSB();
  114. }
  115. /*!
  116. \brief set the state of the low power mode
  117. \param[in] lowpower_mode: the low power mode state
  118. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system always enter low power
  119. mode by exiting from ISR
  120. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the DEEPSLEEP mode
  121. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode can be woke up
  122. by all the enable and disable interrupts
  123. \param[out] none
  124. \retval none
  125. */
  126. void system_lowpower_set(uint8_t lowpower_mode)
  127. {
  128. SCB->SCR |= (uint32_t)lowpower_mode;
  129. }
  130. /*!
  131. \brief reset the state of the low power mode
  132. \param[in] lowpower_mode: the low power mode state
  133. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system will exit low power
  134. mode by exiting from ISR
  135. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the SLEEP mode
  136. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode only can be
  137. woke up by the enable interrupts
  138. \param[out] none
  139. \retval none
  140. */
  141. void system_lowpower_reset(uint8_t lowpower_mode)
  142. {
  143. SCB->SCR &= (~(uint32_t)lowpower_mode);
  144. }
  145. /*!
  146. \brief set the systick clock source
  147. \param[in] systick_clksource: the systick clock source needed to choose
  148. \arg SYSTICK_CLKSOURCE_HCLK: systick clock source is from HCLK
  149. \arg SYSTICK_CLKSOURCE_HCLK_DIV8: systick clock source is from HCLK/8
  150. \param[out] none
  151. \retval none
  152. */
  153. void systick_clksource_set(uint32_t systick_clksource)
  154. {
  155. if(SYSTICK_CLKSOURCE_HCLK == systick_clksource ){
  156. /* set the systick clock source from HCLK */
  157. SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
  158. }else{
  159. /* set the systick clock source from HCLK/8 */
  160. SysTick->CTRL &= SYSTICK_CLKSOURCE_HCLK_DIV8;
  161. }
  162. }