gd32f10x_misc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. ******************************************************************************
  3. * @brief MISC functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_misc.h"
  8. /** @addtogroup GD32F10x_Firmware
  9. * @{
  10. */
  11. /** @defgroup MISC
  12. * @brief MISC driver modules
  13. * @{
  14. */
  15. /** @defgroup MISC_Private_Defines
  16. * @{
  17. */
  18. #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
  19. /**
  20. * @}
  21. */
  22. /** @defgroup MISC_Private_Functions
  23. * @{
  24. */
  25. /**
  26. * @brief By the PRIGROUP[10:8] bits of the AIRCR register, Setting the priority grouping:
  27. * pre-emption priority and subpriority.
  28. * @param NVIC_PriGroup: NVIC_PRIGROUP_0, NVIC_PRIGROUP_1,...NVIC_PRIGROUP_4.
  29. * @retval None
  30. */
  31. void NVIC_PRIGroup_Enable(uint32_t NVIC_PRIGroup)
  32. {
  33. /* Set the priority grouping value */
  34. SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PRIGroup;
  35. }
  36. /**
  37. * @brief The NVIC peripheral Initialization.
  38. * @param NVIC_InitStruct: a NVIC_InitPara structure pointer.
  39. * @retval None
  40. */
  41. void NVIC_Init(NVIC_InitPara *NVIC_InitStruct)
  42. {
  43. uint32_t temppriority = 0x00, temppreempt = 0x00, tempsub = 0x00;
  44. if (NVIC_InitStruct->NVIC_IRQEnable != DISABLE) {
  45. if (((SCB->AIRCR) & (uint32_t)0x700) == NVIC_PRIGROUP_0) {
  46. temppreempt = 0;
  47. tempsub = 0x4;
  48. } else if (((SCB->AIRCR) & (uint32_t)0x700) == NVIC_PRIGROUP_1) {
  49. temppreempt = 1;
  50. tempsub = 0x3;
  51. } else if (((SCB->AIRCR) & (uint32_t)0x700) == NVIC_PRIGROUP_2) {
  52. temppreempt = 2;
  53. tempsub = 0x2;
  54. } else if (((SCB->AIRCR) & (uint32_t)0x700) == NVIC_PRIGROUP_3) {
  55. temppreempt = 3;
  56. tempsub = 0x1;
  57. } else if (((SCB->AIRCR) & (uint32_t)0x700) == NVIC_PRIGROUP_4) {
  58. temppreempt = 4;
  59. tempsub = 0x0;
  60. }
  61. temppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQPreemptPriority << (0x4 - temppreempt);
  62. temppriority |= NVIC_InitStruct->NVIC_IRQSubPriority & (0x0F >> (0x4 - tempsub));
  63. temppriority = temppriority << 0x04;
  64. NVIC->IP[NVIC_InitStruct->NVIC_IRQ] = temppriority;
  65. /* Enable the Selected IRQ Channels */
  66. NVIC->ISER[NVIC_InitStruct->NVIC_IRQ >> 0x05] = (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQ & (uint8_t)0x1F);
  67. } else {
  68. /* Disable the Selected IRQ Channels */
  69. NVIC->ICER[NVIC_InitStruct->NVIC_IRQ >> 0x05] = (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQ & (uint8_t)0x1F);
  70. }
  71. }
  72. /**
  73. * @brief Specify the vector table in RAM or FLASH memory and its Offset.
  74. * @param NVIC_VectTab: NVIC_VECTTAB_RAM,NVIC_VECTTAB_FLASH
  75. * @param Offset: Vector Table start address.
  76. * @retval None
  77. */
  78. void NVIC_VectTableSet(uint32_t NVIC_VectTab, uint32_t Offset)
  79. {
  80. SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
  81. }
  82. /**
  83. * @brief Specify the state of the system to enter low power mode.
  84. * @param LowPowerMode: NVIC_LOWPOWER_SEVONPEND,NVIC_LOWPOWER_SLEEPDEEP,NVIC_LOWPOWER_SLEEPONEXIT.
  85. * @param NewValue: new value of Low Power state. This parameter can be: ENABLE or DISABLE.
  86. * @retval None
  87. */
  88. void NVIC_SystemLowPowerConfig(uint8_t LowPowerMode, TypeState NewValue)
  89. {
  90. if (NewValue != DISABLE) {
  91. SCB->SCR |= LowPowerMode;
  92. } else {
  93. SCB->SCR &= (~(uint32_t)LowPowerMode);
  94. }
  95. }
  96. /**
  97. * @brief Specify the SysTick clock source.
  98. * @param SysTick_CKSource: SYSTICK_CKSOURCE_HCLK_DIV8,SYSTICK_CKSOURCE_HCLK.
  99. * @retval None
  100. */
  101. void SysTick_CKSource_Enable(uint32_t SysTick_CKSource)
  102. {
  103. if (SysTick_CKSource == SYSTICK_CKSOURCE_HCLK) {
  104. SysTick->CTRL |= SYSTICK_CKSOURCE_HCLK;
  105. } else {
  106. SysTick->CTRL &= SYSTICK_CKSOURCE_HCLK_DIV8;
  107. }
  108. }
  109. /**
  110. * @}
  111. */
  112. /**
  113. * @}
  114. */
  115. /**
  116. * @}
  117. */