gd32f4xx_misc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*!
  2. \file gd32f4xx_misc.c
  3. \brief MISC driver
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "gd32f4xx_misc.h"
  10. /*!
  11. \brief set the priority group
  12. \param[in] nvic_prigroup: the NVIC priority group
  13. \arg NVIC_PRIGROUP_PRE0_SUB4:0 bits for pre-emption priority 4 bits for subpriority
  14. \arg NVIC_PRIGROUP_PRE1_SUB3:1 bits for pre-emption priority 3 bits for subpriority
  15. \arg NVIC_PRIGROUP_PRE2_SUB2:2 bits for pre-emption priority 2 bits for subpriority
  16. \arg NVIC_PRIGROUP_PRE3_SUB1:3 bits for pre-emption priority 1 bits for subpriority
  17. \arg NVIC_PRIGROUP_PRE4_SUB0:4 bits for pre-emption priority 0 bits for subpriority
  18. \param[out] none
  19. \retval none
  20. */
  21. void nvic_priority_group_set(uint32_t nvic_prigroup)
  22. {
  23. /* set the priority group value */
  24. SCB->AIRCR = NVIC_AIRCR_VECTKEY_MASK | nvic_prigroup;
  25. }
  26. /*!
  27. \brief enable NVIC request
  28. \param[in] nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  29. \param[in] nvic_irq_pre_priority: the pre-emption priority needed to set
  30. \param[in] nvic_irq_sub_priority: the subpriority needed to set
  31. \param[out] none
  32. \retval none
  33. */
  34. void nvic_irq_enable(uint8_t nvic_irq, uint8_t nvic_irq_pre_priority,
  35. uint8_t nvic_irq_sub_priority)
  36. {
  37. uint32_t temp_priority = 0x00U, temp_pre = 0x00U, temp_sub = 0x00U;
  38. /* use the priority group value to get the temp_pre and the temp_sub */
  39. if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE0_SUB4){
  40. temp_pre=0U;
  41. temp_sub=0x4U;
  42. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE1_SUB3){
  43. temp_pre=1U;
  44. temp_sub=0x3U;
  45. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE2_SUB2){
  46. temp_pre=2U;
  47. temp_sub=0x2U;
  48. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE3_SUB1){
  49. temp_pre=3U;
  50. temp_sub=0x1U;
  51. }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE4_SUB0){
  52. temp_pre=4U;
  53. temp_sub=0x0U;
  54. }else{
  55. }
  56. /* get the temp_priority to fill the NVIC->IP register */
  57. temp_priority = (uint32_t)nvic_irq_pre_priority << (0x4U - temp_pre);
  58. temp_priority |= nvic_irq_sub_priority &(0x0FU >> (0x4U - temp_sub));
  59. temp_priority = temp_priority << 0x04U;
  60. NVIC->IP[nvic_irq] = (uint8_t)temp_priority;
  61. /* enable the selected IRQ */
  62. NVIC->ISER[nvic_irq >> 0x05U] = (uint32_t)0x01U << (nvic_irq & (uint8_t)0x1FU);
  63. }
  64. /*!
  65. \brief disable NVIC request
  66. \param[in] nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  67. \param[out] none
  68. \retval none
  69. */
  70. void nvic_irq_disable(uint8_t nvic_irq)
  71. {
  72. /* disable the selected IRQ.*/
  73. NVIC->ICER[nvic_irq >> 0x05] = (uint32_t)0x01 << (nvic_irq & (uint8_t)0x1F);
  74. }
  75. /*!
  76. \brief set the NVIC vector table base address
  77. \param[in] nvic_vict_tab: the RAM or FLASH base address
  78. \arg NVIC_VECTTAB_RAM: RAM base address
  79. \are NVIC_VECTTAB_FLASH: Flash base address
  80. \param[in] offset: Vector Table offset
  81. \param[out] none
  82. \retval none
  83. */
  84. void nvic_vector_table_set(uint32_t nvic_vict_tab, uint32_t offset)
  85. {
  86. SCB->VTOR = nvic_vict_tab | (offset & NVIC_VECTTAB_OFFSET_MASK);
  87. }
  88. /*!
  89. \brief set the state of the low power mode
  90. \param[in] lowpower_mode: the low power mode state
  91. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system always enter low power
  92. mode by exiting from ISR
  93. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the DEEPSLEEP mode
  94. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode can be woke up
  95. by all the enable and disable interrupts
  96. \param[out] none
  97. \retval none
  98. */
  99. void system_lowpower_set(uint8_t lowpower_mode)
  100. {
  101. SCB->SCR |= (uint32_t)lowpower_mode;
  102. }
  103. /*!
  104. \brief reset the state of the low power mode
  105. \param[in] lowpower_mode: the low power mode state
  106. \arg SCB_LPM_SLEEP_EXIT_ISR: if chose this para, the system will exit low power
  107. mode by exiting from ISR
  108. \arg SCB_LPM_DEEPSLEEP: if chose this para, the system will enter the SLEEP mode
  109. \arg SCB_LPM_WAKE_BY_ALL_INT: if chose this para, the lowpower mode only can be
  110. woke up by the enable interrupts
  111. \param[out] none
  112. \retval none
  113. */
  114. void system_lowpower_reset(uint8_t lowpower_mode)
  115. {
  116. SCB->SCR &= (~(uint32_t)lowpower_mode);
  117. }
  118. /*!
  119. \brief set the systick clock source
  120. \param[in] systick_clksource: the systick clock source needed to choose
  121. \arg SYSTICK_CLKSOURCE_HCLK: systick clock source is from HCLK
  122. \arg SYSTICK_CLKSOURCE_HCLK_DIV8: systick clock source is from HCLK/8
  123. \param[out] none
  124. \retval none
  125. */
  126. void systick_clksource_set(uint32_t systick_clksource)
  127. {
  128. if(SYSTICK_CLKSOURCE_HCLK == systick_clksource ){
  129. /* set the systick clock source from HCLK */
  130. SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
  131. }else{
  132. /* set the systick clock source from HCLK/8 */
  133. SysTick->CTRL &= SYSTICK_CLKSOURCE_HCLK_DIV8;
  134. }
  135. }