sys.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. *****************************************************************************
  3. * 系统设置相关的函数
  4. *
  5. * (C) Copyright 2000-2020, ***
  6. * All Rights Reserved
  7. *****************************************************************************
  8. *
  9. * @File : sys.c
  10. * @By : 陈桂东
  11. * @Version : V1.0
  12. * @Date : 2012 / 10 / 20
  13. *
  14. *****************************************************************************
  15. * Update
  16. * @Version : V1.0.1
  17. * @By : 陈桂东
  18. * @Date : 2014 / 03 / 09
  19. * @Brief : 增加GPIO共用初始化函数,方便在移植时直接宏定义即可
  20. *
  21. * @Version : V1.0.2
  22. * @By : 陈桂东
  23. * @Date : 2014 / 05 / 24
  24. * @Brief : 增加对C++环境支持
  25. *
  26. * @Version : V1.1
  27. * @By : 陈桂东
  28. * @Date : 2015 / 10 / 03
  29. * @Brief : 增加103和407之间切换宏定义,直接调用core_cm3.h 或者 core_cm4.h文件中的 __CORTEX_M 宏定义做区别
  30. *
  31. * @Version : V1.2
  32. * @By : 陈桂东
  33. * @Date : 2016 / 04 / 27
  34. * @Brief : A、增加对STM32F0系列支持
  35. * B、增加对IAR环境支持
  36. *
  37. *****************************************************************************
  38. **/
  39. #include "sys.h"
  40. #if !defined (__ICCARM__) //IAR环境
  41. /**
  42. *****************************************************************************
  43. * @Name : THUMB指令不支持汇编内联
  44. *
  45. * @Brief : 采用如下方法实现执行汇编指令WFI
  46. *
  47. * @Input : none
  48. *
  49. * @Output : none
  50. *
  51. * @Return : none
  52. *****************************************************************************
  53. **/
  54. __asm void WFI_SET ( void )
  55. {
  56. WFI;
  57. }
  58. /**
  59. *****************************************************************************
  60. * @Name : 关闭所有中断(但是不包括fault和NMI中断)
  61. *
  62. * @Brief : none
  63. *
  64. * @Input : none
  65. *
  66. * @Output : none
  67. *
  68. * @Return : none
  69. *****************************************************************************
  70. **/
  71. __asm void INTX_DISABLE ( void )
  72. {
  73. CPSID I
  74. BX LR
  75. }
  76. /**
  77. *****************************************************************************
  78. * @Name : 开启所有中断
  79. *
  80. * @Brief : none
  81. *
  82. * @Input : none
  83. *
  84. * @Output : none
  85. *
  86. * @Return : none
  87. *****************************************************************************
  88. **/
  89. __asm void INTX_ENABLE ( void )
  90. {
  91. CPSIE I
  92. BX LR
  93. }
  94. #endif
  95. /**
  96. *****************************************************************************
  97. * @Name : 系统软复位
  98. *
  99. * @Brief : none
  100. *
  101. * @Input : none
  102. *
  103. * @Output : none
  104. *
  105. * @Return : none
  106. *****************************************************************************
  107. **/
  108. void SYS_SoftReset ( void )
  109. {
  110. SCB->AIRCR = 0x05fa0000 | 0x00000004; //具体请参考《Cortex-M3权威指南(中文).pdf》第285页内容
  111. }
  112. /**
  113. *****************************************************************************
  114. * @Name : 设置GPIOx管脚输出高电平
  115. *
  116. * @Brief : none
  117. *
  118. * @Input : GPIOx: IO组
  119. * Pin: IO号
  120. *
  121. * @Output : none
  122. *
  123. * @Return : none
  124. *****************************************************************************
  125. **/
  126. void GPIO_SetLevel ( GPIO_TypeDef* GPIOx, uint8_t Pin )
  127. {
  128. GPIOx->ODR &= ~ ( 1 << Pin );
  129. GPIOx->ODR |= 1 << Pin;
  130. }
  131. /**
  132. *****************************************************************************
  133. * @Name : 设置GPIOx管脚输出低电平
  134. *
  135. * @Brief : none
  136. *
  137. * @Input : GPIOx: IO组
  138. * Pin: IO号
  139. *
  140. * @Output : none
  141. *
  142. * @Return : none
  143. *****************************************************************************
  144. **/
  145. void GPIO_ResetLevel ( GPIO_TypeDef* GPIOx, uint8_t Pin )
  146. {
  147. GPIOx->ODR &= ~ ( 1 << Pin );
  148. }
  149. /**
  150. *****************************************************************************
  151. * @Name : 读取GPIOx管脚电平
  152. *
  153. * @Brief : none
  154. *
  155. * @Input : GPIOx: IO组
  156. * Pin: IO号
  157. * In_Out: 输入寄存器还是输出寄存器
  158. *
  159. * @Output : none
  160. *
  161. * @Return : none
  162. *****************************************************************************
  163. **/
  164. void GPIO_GetLevel ( GPIO_TypeDef* GPIOx, uint8_t Pin, uint8_t In_Out )
  165. {
  166. if ( In_Out ) //读取的是输出寄存器
  167. {
  168. if ( GPIOx->ODR & ( 1 << Pin ) ) printf ( "\r\nGPIOx->Pin Out: 1\r\n" );
  169. else printf ( "\r\nGPIOx->Pin Out: 0\r\n" );
  170. }
  171. else //读取的是输入寄存器
  172. {
  173. if ( GPIOx->IDR & ( 1 << Pin ) ) printf ( "\r\nGPIOx->Pin In: 1\r\n" );
  174. else printf ( "\r\nGPIOx->Pin In: 0\r\n" );
  175. }
  176. }
  177. /**
  178. *****************************************************************************
  179. * @Name : 读取GPIO分组地址
  180. *
  181. * @Brief : none
  182. *
  183. * @Input : none
  184. *
  185. * @Output : none
  186. *
  187. * @Return : none
  188. *****************************************************************************
  189. **/
  190. void GPIO_GetAddress ( void )
  191. {
  192. printf ( "\r\nGPIOA Address: 0x%x\r\n", ( uint32_t ) GPIOA );
  193. printf ( "GPIOB Address: 0x%x\r\n", ( uint32_t ) GPIOB );
  194. printf ( "GPIOC Address: 0x%x\r\n", ( uint32_t ) GPIOC );
  195. printf ( "GPIOD Address: 0x%x\r\n", ( uint32_t ) GPIOD );
  196. printf ( "GPIOE Address: 0x%x\r\n", ( uint32_t ) GPIOE );
  197. printf ( "GPIOA_Base Address: 0x%x\r\n", ( uint32_t ) GPIOA_BASE );
  198. printf ( "GPIOB_Base Address: 0x%x\r\n", ( uint32_t ) GPIOB_BASE );
  199. printf ( "GPIOC_Base Address: 0x%x\r\n", ( uint32_t ) GPIOC_BASE );
  200. printf ( "GPIOD_Base Address: 0x%x\r\n", ( uint32_t ) GPIOD_BASE );
  201. printf ( "GPIOE_Base Address: 0x%x\r\n", ( uint32_t ) GPIOE_BASE );
  202. }