sys.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. *****************************************************************************
  3. * 系统设置相关的函数
  4. *
  5. * (C) Copyright 2000-2020, ***
  6. * All Rights Reserved
  7. *****************************************************************************
  8. *
  9. * @File : sys.h
  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. #ifndef _sys_h
  40. #define _sys_h
  41. /**
  42. ****************************** Support C++ **********************************
  43. **/
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. *****************************************************************************
  49. **/
  50. /******************************************************************************
  51. 外部函数头文件
  52. ******************************************************************************/
  53. #include "stdio.h" //printf函数头文件
  54. #if defined (STM32F030) || defined (STM32F031) || \
  55. defined (STM32F051) || \
  56. defined (STM32F072) || \
  57. defined (STM32F042) || \
  58. defined (STM32F091) || \
  59. defined (STM32F070xB) || defined (STM32F070x6) || \
  60. defined (STM32F030xC)
  61. #include "stm32f0xx.h"
  62. #endif
  63. #if defined (STM32F10X_LD) || defined (STM32F10X_LD_VL) || \
  64. defined (STM32F10X_MD) || defined (STM32F10X_MD_VL) || \
  65. defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || \
  66. defined (STM32F10X_XL) || defined (STM32F10X_CL)
  67. #include "stm32f10x.h"
  68. #endif
  69. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F411xE) || defined(STM32F446xx)
  70. #include "stm32f4xx.h"
  71. #endif
  72. /******************************************************************************
  73. 定义是否支持嵌入式操作系统
  74. ******************************************************************************/
  75. #define _SYSTEM_SUPPORT_ROTS 0 /* 嵌入式操作系统支持开关 */
  76. //0:不支持
  77. //1:支持
  78. #define _RTOS_Type_Support 0
  79. //0: uCOS
  80. //1: RAW-OS
  81. /******************************************************************************
  82. 位带操作相关宏定义
  83. 参考《CM3权威指南》第87 ~ 92页
  84. ******************************************************************************/
  85. #if __CORTEX_M == 0x00 //Cortex-M0
  86. typedef struct
  87. {
  88. unsigned short int b0 : 1;
  89. unsigned short int b1 : 1;
  90. unsigned short int b2 : 1;
  91. unsigned short int b3 : 1;
  92. unsigned short int b4 : 1;
  93. unsigned short int b5 : 1;
  94. unsigned short int b6 : 1;
  95. unsigned short int b7 : 1;
  96. unsigned short int b8 : 1;
  97. unsigned short int b9 : 1;
  98. unsigned short int b10 : 1;
  99. unsigned short int b11 : 1;
  100. unsigned short int b12 : 1;
  101. unsigned short int b13 : 1;
  102. unsigned short int b14 : 1;
  103. unsigned short int b15 : 1;
  104. } REG16_TypeDef;
  105. #elif __CORTEX_M == 0x03 || __CORTEX_M == 0x04 //Cortex-M3 or Cortex-M4
  106. #define BITBAND(addr, bitnum) ((addr & 0xF0000000) + 0x2000000 + ((addr & 0xFFFFF) << 5) + (bitnum << 2))
  107. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
  108. #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
  109. #endif
  110. /******************************************************************************
  111. GPIO地址映射
  112. 基地址加上寄存器偏移地址组成
  113. ******************************************************************************/
  114. #if __CORTEX_M == 0x00 //Cortex-M0
  115. #define GPIOA_ODR_Addr (GPIOA_BASE + 0x14) //0x48000014
  116. #define GPIOB_ODR_Addr (GPIOB_BASE + 0x14) //0x48000414
  117. #define GPIOC_ODR_Addr (GPIOC_BASE + 0x14) //0x48000814
  118. #define GPIOD_ODR_Addr (GPIOD_BASE + 0x14) //0x48000C14
  119. #define GPIOE_ODR_Addr (GPIOE_BASE + 0x14) //0x48001014
  120. #define GPIOF_ODR_Addr (GPIOF_BASE + 0x14) //0x48001414
  121. #define GPIOA_IDR_Addr (GPIOA_BASE + 0x10) //0x48000010
  122. #define GPIOB_IDR_Addr (GPIOB_BASE + 0x10) //0x48000410
  123. #define GPIOC_IDR_Addr (GPIOC_BASE + 0x10) //0x48000810
  124. #define GPIOD_IDR_Addr (GPIOD_BASE + 0x10) //0x48000C10
  125. #define GPIOE_IDR_Addr (GPIOE_BASE + 0x10) //0x48001010
  126. #define GPIOF_IDR_Addr (GPIOF_BASE + 0x10) //0x48001410
  127. #elif __CORTEX_M == 0x03 //Cortex-M3
  128. #define GPIOA_ODR_Addr (GPIOA_BASE + 0x0C) //0x4001080C
  129. #define GPIOB_ODR_Addr (GPIOB_BASE + 0x0C) //0x40010C0C
  130. #define GPIOC_ODR_Addr (GPIOC_BASE + 0x0C) //0x4001100C
  131. #define GPIOD_ODR_Addr (GPIOD_BASE + 0x0C) //0x4001140C
  132. #define GPIOE_ODR_Addr (GPIOE_BASE + 0x0C) //0x4001180C
  133. #define GPIOF_ODR_Addr (GPIOF_BASE + 0x0C) //0x40011A0C
  134. #define GPIOG_ODR_Addr (GPIOG_BASE + 0x0C) //0x40011E0C
  135. #define GPIOA_IDR_Addr (GPIOA_BASE + 0x08) //0x40010808
  136. #define GPIOB_IDR_Addr (GPIOB_BASE + 0x08) //0x40010C08
  137. #define GPIOC_IDR_Addr (GPIOC_BASE + 0x08) //0x40011008
  138. #define GPIOD_IDR_Addr (GPIOD_BASE + 0x08) //0x40011408
  139. #define GPIOE_IDR_Addr (GPIOE_BASE + 0x08) //0x40011808
  140. #define GPIOF_IDR_Addr (GPIOF_BASE + 0x08) //0x40011A08
  141. #define GPIOG_IDR_Addr (GPIOG_BASE + 0x08) //0x40011E08
  142. #elif __CORTEX_M == 0x04 //Cortex-M4
  143. #define GPIOA_ODR_Addr (GPIOA_BASE + 0x14) //0x40020014
  144. #define GPIOB_ODR_Addr (GPIOB_BASE + 0x14) //0x40020414
  145. #define GPIOC_ODR_Addr (GPIOC_BASE + 0x14) //0x40020814
  146. #define GPIOD_ODR_Addr (GPIOD_BASE + 0x14) //0x40020C14
  147. #define GPIOE_ODR_Addr (GPIOE_BASE + 0x14) //0x40021014
  148. #define GPIOF_ODR_Addr (GPIOF_BASE + 0x14) //0x40021414
  149. #define GPIOG_ODR_Addr (GPIOG_BASE + 0x14) //0x40021814
  150. #define GPIOA_IDR_Addr (GPIOA_BASE + 0x10) //0x40020010
  151. #define GPIOB_IDR_Addr (GPIOB_BASE + 0x10) //0x40020410
  152. #define GPIOC_IDR_Addr (GPIOC_BASE + 0x10) //0x40020810
  153. #define GPIOD_IDR_Addr (GPIOD_BASE + 0x10) //0x40020C10
  154. #define GPIOE_IDR_Addr (GPIOE_BASE + 0x10) //0x40021010
  155. #define GPIOF_IDR_Addr (GPIOF_BASE + 0x10) //0x40021410
  156. #define GPIOG_IDR_Addr (GPIOG_BASE + 0x10) //0x40021810
  157. #endif
  158. /******************************************************************************
  159. 实现单一IO操作,类似于51的IO操作
  160. n值要小于IO具体数目
  161. ******************************************************************************/
  162. #if __CORTEX_M == 0x00 //Cortex-M0
  163. #define PAout ((volatile REG16_TypeDef *)GPIOA_ODR_Addr)
  164. #define PAin ((volatile REG16_TypeDef *)GPIOA_IDR_Addr)
  165. #define PBout ((volatile REG16_TypeDef *)GPIOB_ODR_Addr)
  166. #define PBin ((volatile REG16_TypeDef *)GPIOB_IDR_Addr)
  167. #define PCout ((volatile REG16_TypeDef *)GPIOC_ODR_Addr)
  168. #define PCin ((volatile REG16_TypeDef *)GPIOC_IDR_Addr)
  169. #define PDout ((volatile REG16_TypeDef *)GPIOD_ODR_Addr)
  170. #define PDin ((volatile REG16_TypeDef *)GPIOD_IDR_Addr)
  171. #define PEout ((volatile REG16_TypeDef *)GPIOE_ODR_Addr)
  172. #define PEin ((volatile REG16_TypeDef *)GPIOE_IDR_Addr)
  173. #define PFout ((volatile REG16_TypeDef *)GPIOF_ODR_Addr)
  174. #define PFin ((volatile REG16_TypeDef *)GPIOF_IDR_Addr)
  175. #elif __CORTEX_M == 0x03 || __CORTEX_M == 0x04 //Cortex-M3 or Cortex-M4
  176. #define PAout(n) BIT_ADDR(GPIOA_ODR_Addr, n) //输出
  177. #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr, n) //输入
  178. #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr, n) //输出
  179. #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr, n) //输入
  180. #define PCout(n) BIT_ADDR(GPIOC_ODR_Addr, n) //输出
  181. #define PCin(n) BIT_ADDR(GPIOC_IDR_Addr, n) //输入
  182. #define PDout(n) BIT_ADDR(GPIOD_ODR_Addr, n) //输出
  183. #define PDin(n) BIT_ADDR(GPIOD_IDR_Addr, n) //输入
  184. #define PEout(n) BIT_ADDR(GPIOE_ODR_Addr, n) //输出
  185. #define PEin(n) BIT_ADDR(GPIOE_IDR_Addr, n) //输入
  186. #define PFout(n) BIT_ADDR(GPIOF_ODR_Addr, n) //输出
  187. #define PFin(n) BIT_ADDR(GPIOF_IDR_Addr, n) //输入
  188. #define PGout(n) BIT_ADDR(GPIOG_ODR_Addr, n) //输出
  189. #define PGin(n) BIT_ADDR(GPIOG_IDR_Addr, n) //输入
  190. #endif
  191. //typedef enum {FALSE = 0, TRUE = !FALSE} bool;
  192. #if __CORTEX_M == 0x00 //Cortex-M0
  193. typedef unsigned char u8;
  194. typedef unsigned short int u16;
  195. typedef unsigned int u32;
  196. typedef const unsigned char uc8;
  197. typedef const unsigned short int uc16;
  198. typedef const unsigned int uc32;
  199. #endif
  200. /******************************************************************************
  201. 外部调用功能函数
  202. ******************************************************************************/
  203. #if !defined (__ICCARM__) //IAR环境
  204. void INTX_DISABLE ( void ); //关闭所有中断(但是不包括fault和NMI中断)
  205. void INTX_ENABLE ( void ); //开启所有中断
  206. #endif
  207. void SYS_SoftReset ( void ); //系统软复位
  208. void GPIO_SetLevel ( GPIO_TypeDef* GPIOx, uint8_t Pin ); //设置GPIOx管脚输出高电平
  209. void GPIO_ResetLevel ( GPIO_TypeDef* GPIOx, uint8_t Pin ); //设置GPIOx管脚输出低电平
  210. void GPIO_GetLevel ( GPIO_TypeDef* GPIOx, uint8_t Pin, uint8_t In_Out ); //读取GPIOx管脚电平
  211. void GPIO_GetAddress ( void ); //读取GPIO分组地址
  212. /**
  213. ****************************** Support C++ **********************************
  214. **/
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. /**
  219. *****************************************************************************
  220. **/
  221. #endif /* end sys.h */