st_sys.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #ifndef __ST_SYS_H
  2. #define __ST_SYS_H
  3. #include <stm32f10x.h>
  4. #include "typedefine.h"
  5. #include "software_timer.h"
  6. #include "hardware_delay.h"
  7. #include "st_dma.h"
  8. #include "st_flash.h"
  9. //////////////////////////////////////////////////////////////////////////////////
  10. //本程序只供学习使用,未经作者许可,不得用于其它任何用途
  11. //ALIENTEK STM32开发板
  12. //系统时钟初始化(适合STM32F10x系列)
  13. //正点原子@ALIENTEK
  14. //技术论坛:www.openedv.com
  15. //创建日期:2010/1/1
  16. //版本:V1.9
  17. //版权所有,盗版必究。
  18. //Copyright(C) 广州市星翼电子科技有限公司 2009-2019
  19. //All rights reserved
  20. //********************************************************************************
  21. //V1.4修改说明
  22. //把NVIC KO了,没有使用任何库文件!
  23. //加入了JTAG_Set函数
  24. //V1.5 20120322
  25. //增加void INTX_DISABLE(void)和void INTX_ENABLE(void)两个函数
  26. //V1.6 20120412
  27. //1,增加MSR_MSP函数
  28. //2,修改VECT_TAB_RAM的默认偏移,设置为0.
  29. //V1.7 20120818
  30. //1,添加ucos支持配置宏SYSTEM_SUPPORT_UCOS
  31. //2,修改了注释
  32. //3,去掉了不常用函数BKP_Write
  33. //V1.8 20131120
  34. //1,修改头文件为stm32f10x.h,不再使用stm32f10x_lib.h及其相关头文件
  35. //V1.9 20150109
  36. //1,修改头文件为MY_NVIC_Init函数部分代码以支持向量号大于63的中断的设置
  37. //2,修改WFI_SET/INTX_DISABLE/INTX_ENABLE等函数的实现方式
  38. //V2.0 20150322
  39. //修改SYSTEM_SUPPORT_UCOS为SYSTEM_SUPPORT_OS
  40. //////////////////////////////////////////////////////////////////////////////////
  41. //0,不支持OS
  42. //1,支持OS
  43. #define SYSTEM_SUPPORT_OS 0 //定义系统文件夹是否支持OS
  44. //位带操作,实现51类似的GPIO控制功能
  45. //具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
  46. //IO口操作宏定义
  47. #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
  48. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
  49. #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
  50. //IO口地址映射
  51. #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
  52. #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
  53. #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
  54. #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
  55. #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
  56. #define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
  57. #define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
  58. #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
  59. #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
  60. #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
  61. #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
  62. #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
  63. #define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
  64. #define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
  65. //IO口操作,只对单一的IO口!
  66. //确保n的值小于16!
  67. #define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
  68. #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
  69. #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
  70. #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
  71. #define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
  72. #define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
  73. #define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
  74. #define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
  75. #define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
  76. #define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
  77. #define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
  78. #define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
  79. #define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
  80. #define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
  81. /////////////////////////////////////////////////////////////////
  82. //Ex_NVIC_Config专用定义
  83. #define GPIO_A 0
  84. #define GPIO_B 1
  85. #define GPIO_C 2
  86. #define GPIO_D 3
  87. #define GPIO_E 4
  88. #define GPIO_F 5
  89. #define GPIO_G 6
  90. #define FTIR 1 //下降沿触发
  91. #define RTIR 2 //上升沿触发
  92. //JTAG模式设置定义
  93. #define JTAG_SWD_DISABLE 0X02
  94. #define SWD_ENABLE 0X01
  95. #define JTAG_SWD_ENABLE 0X00
  96. //GPIO设置专用宏定义
  97. /*
  98. #define GPIO_MODE_IN 0 //普通输入模式
  99. #define GPIO_MODE_OUT 1 //普通输出模式
  100. #define GPIO_MODE_AF 2 //AF功能模式
  101. #define GPIO_MODE_AIN 3 //模拟输入模式
  102. */
  103. #define GPIO_SPEED_2M 2 //GPIO速度2Mhz
  104. #define GPIO_SPEED_10M 1 //GPIO速度25Mhz
  105. #define GPIO_SPEED_50M 3 //GPIO速度50Mhz
  106. //#define GPIO_SPEED_100M 3 //GPIO速度100Mhz
  107. #define GPIO_PUPD_NONE 0 //不带上下拉
  108. #define GPIO_PUPD_PU 1 //上拉
  109. #define GPIO_PUPD_PD 2 //下拉
  110. #define GPIO_PUPD_RES 3 //保留
  111. #define GPIO_OTYPE_PP 0 //推挽输出
  112. #define GPIO_OTYPE_OD 1 //开漏输出
  113. #define GPIO_MODE_AIN 0
  114. #define GPIO_MODE_IN_FLOATING 0x04
  115. #define GPIO_MODE_IPD 0x28
  116. #define GPIO_MODE_IPU 0x48
  117. #define GPIO_MODE_OUT_OD 0x14
  118. #define GPIO_MODE_OUT_PP 0x10
  119. #define GPIO_MODE_AF_OD 0x1C
  120. #define GPIO_MODE_AF_PP 0x18
  121. //GPIO引脚编号定义
  122. #define PIN0 1<<0
  123. #define PIN1 1<<1
  124. #define PIN2 1<<2
  125. #define PIN3 1<<3
  126. #define PIN4 1<<4
  127. #define PIN5 1<<5
  128. #define PIN6 1<<6
  129. #define PIN7 1<<7
  130. #define PIN8 1<<8
  131. #define PIN9 1<<9
  132. #define PIN10 1<<10
  133. #define PIN11 1<<11
  134. #define PIN12 1<<12
  135. #define PIN13 1<<13
  136. #define PIN14 1<<14
  137. #define PIN15 1<<15
  138. #define SW_WAIT_FINISH(x) do{}while(x)
  139. //对管脚重新分配
  140. #define GPIO_Remap_SPI1 ((uint32_t)0x00000001) /*!< SPI1 Alternate Function mapping */
  141. #define GPIO_Remap_I2C1 ((uint32_t)0x00000002) /*!< I2C1 Alternate Function mapping */
  142. #define GPIO_Remap_USART1 ((uint32_t)0x00000004) /*!< USART1 Alternate Function mapping */
  143. #define GPIO_Remap_USART2 ((uint32_t)0x00000008) /*!< USART2 Alternate Function mapping */
  144. #define GPIO_PartialRemap_USART3 ((uint32_t)0x00140010) /*!< USART3 Partial Alternate Function mapping */
  145. #define GPIO_FullRemap_USART3 ((uint32_t)0x00140030) /*!< USART3 Full Alternate Function mapping */
  146. #define GPIO_PartialRemap_TIM1 ((uint32_t)0x00160040) /*!< TIM1 Partial Alternate Function mapping */
  147. #define GPIO_FullRemap_TIM1 ((uint32_t)0x001600C0) /*!< TIM1 Full Alternate Function mapping */
  148. #define GPIO_PartialRemap1_TIM2 ((uint32_t)0x00180100) /*!< TIM2 Partial1 Alternate Function mapping */
  149. #define GPIO_PartialRemap2_TIM2 ((uint32_t)0x00180200) /*!< TIM2 Partial2 Alternate Function mapping */
  150. #define GPIO_FullRemap_TIM2 ((uint32_t)0x00180300) /*!< TIM2 Full Alternate Function mapping */
  151. #define GPIO_PartialRemap_TIM3 ((uint32_t)0x001A0800) /*!< TIM3 Partial Alternate Function mapping */
  152. #define GPIO_FullRemap_TIM3 ((uint32_t)0x001A0C00) /*!< TIM3 Full Alternate Function mapping */
  153. #define GPIO_Remap_TIM4 ((uint32_t)0x00001000) /*!< TIM4 Alternate Function mapping */
  154. #define GPIO_Remap1_CAN1 ((uint32_t)0x001D4000) /*!< CAN1 Alternate Function mapping */
  155. #define GPIO_Remap2_CAN1 ((uint32_t)0x001D6000) /*!< CAN1 Alternate Function mapping */
  156. #define GPIO_Remap_PD01 ((uint32_t)0x00008000) /*!< PD01 Alternate Function mapping */
  157. #define GPIO_Remap_TIM5CH4_LSI ((uint32_t)0x00200001) /*!< LSI connected to TIM5 Channel4 input capture for calibration */
  158. #define GPIO_Remap_ADC1_ETRGINJ ((uint32_t)0x00200002) /*!< ADC1 External Trigger Injected Conversion remapping */
  159. #define GPIO_Remap_ADC1_ETRGREG ((uint32_t)0x00200004) /*!< ADC1 External Trigger Regular Conversion remapping */
  160. #define GPIO_Remap_ADC2_ETRGINJ ((uint32_t)0x00200008) /*!< ADC2 External Trigger Injected Conversion remapping */
  161. #define GPIO_Remap_ADC2_ETRGREG ((uint32_t)0x00200010) /*!< ADC2 External Trigger Regular Conversion remapping */
  162. #define GPIO_Remap_ETH ((uint32_t)0x00200020) /*!< Ethernet remapping (only for Connectivity line devices) */
  163. #define GPIO_Remap_CAN2 ((uint32_t)0x00200040) /*!< CAN2 remapping (only for Connectivity line devices) */
  164. #define GPIO_Remap_SWJ_NoJTRST ((uint32_t)0x00300100) /*!< Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST */
  165. #define GPIO_Remap_SWJ_JTAGDisable ((uint32_t)0x00300200) /*!< JTAG-DP Disabled and SW-DP Enabled */
  166. #define GPIO_Remap_SWJ_Disable ((uint32_t)0x00300400) /*!< Full SWJ Disabled (JTAG-DP + SW-DP) */
  167. #define GPIO_Remap_SPI3 ((uint32_t)0x00201100) /*!< SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices) */
  168. #define GPIO_Remap_TIM2ITR1_PTP_SOF ((uint32_t)0x00202000) /*!< Ethernet PTP output or USB OTG SOF (Start of Frame) connected
  169. to TIM2 Internal Trigger 1 for calibration
  170. (only for Connectivity line devices) */
  171. #define GPIO_Remap_PTP_PPS ((uint32_t)0x00204000) /*!< Ethernet MAC PPS_PTS output on PB05 (only for Connectivity line devices) */
  172. #define GPIO_Remap_TIM15 ((uint32_t)0x80000001) /*!< TIM15 Alternate Function mapping (only for Value line devices) */
  173. #define GPIO_Remap_TIM16 ((uint32_t)0x80000002) /*!< TIM16 Alternate Function mapping (only for Value line devices) */
  174. #define GPIO_Remap_TIM17 ((uint32_t)0x80000004) /*!< TIM17 Alternate Function mapping (only for Value line devices) */
  175. #define GPIO_Remap_CEC ((uint32_t)0x80000008) /*!< CEC Alternate Function mapping (only for Value line devices) */
  176. #define GPIO_Remap_TIM1_DMA ((uint32_t)0x80000010) /*!< TIM1 DMA requests mapping (only for Value line devices) */
  177. #define GPIO_Remap_TIM9 ((uint32_t)0x80000020) /*!< TIM9 Alternate Function mapping (only for XL-density devices) */
  178. #define GPIO_Remap_TIM10 ((uint32_t)0x80000040) /*!< TIM10 Alternate Function mapping (only for XL-density devices) */
  179. #define GPIO_Remap_TIM11 ((uint32_t)0x80000080) /*!< TIM11 Alternate Function mapping (only for XL-density devices) */
  180. #define GPIO_Remap_TIM13 ((uint32_t)0x80000100) /*!< TIM13 Alternate Function mapping (only for High density Value line and XL-density devices) */
  181. #define GPIO_Remap_TIM14 ((uint32_t)0x80000200) /*!< TIM14 Alternate Function mapping (only for High density Value line and XL-density devices) */
  182. #define GPIO_Remap_FSMC_NADV ((uint32_t)0x80000400) /*!< FSMC_NADV Alternate Function mapping (only for High density Value line and XL-density devices) */
  183. #define GPIO_Remap_TIM67_DAC_DMA ((uint32_t)0x80000800) /*!< TIM6/TIM7 and DAC DMA requests remapping (only for High density Value line devices) */
  184. #define GPIO_Remap_TIM12 ((uint32_t)0x80001000) /*!< TIM12 Alternate Function mapping (only for High density Value line devices) */
  185. #define GPIO_Remap_MISC ((uint32_t)0x80002000) /*!< Miscellaneous Remap (DMA2 Channel5 Position and DAC Trigger remapping, */
  186. //////////////////////////////////////////////////////////////////////////////////
  187. void Stm32_Clock_Init(u8 PLL); //时钟初始化
  188. /////////////////////////////////////////////////////////////////
  189. void Sys_Clock_Set(u8 PLL); //时钟初始化
  190. void Sys_Soft_Reset(void); //系统软复位
  191. void Sys_Standby(void); //待机模式
  192. void Sys_NVIC_SetVectorTable(u32 NVIC_VectTab, u32 Offset);//设置偏移地址
  193. void Sys_NVIC_PriorityGroupConfig(u8 NVIC_Group);//设置NVIC分组
  194. void Sys_NVIC_Init(u8 NVIC_PreemptionPriority,u8 NVIC_SubPriority,u8 NVIC_Channel,u8 NVIC_Group);//设置中断
  195. void Ex_NVIC_Config(u8 GPIOx,u8 BITx,u8 TRIM);//外部中断配置函数(只对GPIOA~G)
  196. void JTAG_Set(u8 mode);
  197. void GPIO_Remap_Set(uint32_t remap, u8 newstate); //GPIO复用功能设置
  198. void GPIO_Set(GPIO_TypeDef* GPIOx,u32 BITx,u32 MODE,u32 OSPEED,u32 PUPD);//GPIO设置函数
  199. //////////////////////////////////////////////////////////////////////////////
  200. //以下为汇编函数
  201. void WFI_SET(void); //执行WFI指令
  202. void INTX_DISABLE(void);//关闭所有中断
  203. void INTX_ENABLE(void); //开启所有中断
  204. void MSR_MSP(u32 addr); //设置堆栈地址
  205. #endif