misc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file misc.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "misc.h"
  35. /** @addtogroup N32G45X_StdPeriph_Driver
  36. * @{
  37. */
  38. /** @addtogroup MISC
  39. * @brief MISC driver modules
  40. * @{
  41. */
  42. /** @addtogroup MISC_Private_TypesDefinitions
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /** @addtogroup MISC_Private_Defines
  49. * @{
  50. */
  51. #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
  52. /**
  53. * @}
  54. */
  55. /** @addtogroup MISC_Private_Macros
  56. * @{
  57. */
  58. /**
  59. * @}
  60. */
  61. /** @addtogroup MISC_Private_Variables
  62. * @{
  63. */
  64. /**
  65. * @}
  66. */
  67. /** @addtogroup MISC_Private_FunctionPrototypes
  68. * @{
  69. */
  70. /**
  71. * @}
  72. */
  73. /** @addtogroup MISC_Private_Functions
  74. * @{
  75. */
  76. /**
  77. * @brief Configures the priority grouping: pre-emption priority and subpriority.
  78. * @param NVIC_PriorityGroup specifies the priority grouping bits length.
  79. * This parameter can be one of the following values:
  80. * @arg NVIC_PriorityGroup_0 0 bits for pre-emption priority
  81. * 4 bits for subpriority
  82. * @arg NVIC_PriorityGroup_1 1 bits for pre-emption priority
  83. * 3 bits for subpriority
  84. * @arg NVIC_PriorityGroup_2 2 bits for pre-emption priority
  85. * 2 bits for subpriority
  86. * @arg NVIC_PriorityGroup_3 3 bits for pre-emption priority
  87. * 1 bits for subpriority
  88. * @arg NVIC_PriorityGroup_4 4 bits for pre-emption priority
  89. * 0 bits for subpriority
  90. */
  91. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
  92. {
  93. /* Check the parameters */
  94. assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
  95. /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  96. SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
  97. }
  98. /**
  99. * @brief Initializes the NVIC peripheral according to the specified
  100. * parameters in the NVIC_InitStruct.
  101. * @param NVIC_InitStruct pointer to a NVIC_InitType structure that contains
  102. * the configuration information for the specified NVIC peripheral.
  103. */
  104. void NVIC_Init(NVIC_InitType* NVIC_InitStruct)
  105. {
  106. uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
  107. /* Check the parameters */
  108. assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
  109. assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
  110. assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
  111. if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
  112. {
  113. /* Compute the Corresponding IRQ Priority --------------------------------*/
  114. tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700)) >> 0x08;
  115. tmppre = (0x4 - tmppriority);
  116. tmpsub = tmpsub >> tmppriority;
  117. tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
  118. tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
  119. tmppriority = tmppriority << 0x04;
  120. NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
  121. /* Enable the Selected IRQ Channels --------------------------------------*/
  122. NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01
  123. << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  124. }
  125. else
  126. {
  127. /* Disable the Selected IRQ Channels -------------------------------------*/
  128. NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01
  129. << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  130. }
  131. }
  132. /**
  133. * @brief Sets the vector table location and Offset.
  134. * @param NVIC_VectTab specifies if the vector table is in RAM or FLASH memory.
  135. * This parameter can be one of the following values:
  136. * @arg NVIC_VectTab_RAM
  137. * @arg NVIC_VectTab_FLASH
  138. * @param Offset Vector Table base offset field. This value must be a multiple
  139. * of 0x200.
  140. */
  141. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
  142. {
  143. /* Check the parameters */
  144. assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
  145. assert_param(IS_NVIC_OFFSET(Offset));
  146. SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
  147. }
  148. /**
  149. * @brief Selects the condition for the system to enter low power mode.
  150. * @param LowPowerMode Specifies the new mode for the system to enter low power mode.
  151. * This parameter can be one of the following values:
  152. * @arg NVIC_LP_SEVONPEND
  153. * @arg NVIC_LP_SLEEPDEEP
  154. * @arg NVIC_LP_SLEEPONEXIT
  155. * @param Cmd new state of LP condition. This parameter can be: ENABLE or DISABLE.
  156. */
  157. void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState Cmd)
  158. {
  159. /* Check the parameters */
  160. assert_param(IS_NVIC_LP(LowPowerMode));
  161. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  162. if (Cmd != DISABLE)
  163. {
  164. SCB->SCR |= LowPowerMode;
  165. }
  166. else
  167. {
  168. SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
  169. }
  170. }
  171. /**
  172. * @brief Configures the SysTick clock source.
  173. * @param SysTick_CLKSource specifies the SysTick clock source.
  174. * This parameter can be one of the following values:
  175. * @arg SysTick_CLKSource_HCLK AHB clock selected as SysTick clock source.
  176. */
  177. void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
  178. {
  179. /* Check the parameters */
  180. assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  181. if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
  182. {
  183. SysTick->CTRL |= SysTick_CLKSource_HCLK;
  184. }
  185. // else
  186. // {
  187. // SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
  188. // }
  189. }
  190. /**
  191. * @}
  192. */
  193. /**
  194. * @}
  195. */
  196. /**
  197. * @}
  198. */