gd32f4xx_misc.c 6.9 KB

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