gd32f30x_misc.c 7.0 KB

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