interface.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #ifndef __INTERFACE_H
  2. #define __INTERFACE_H
  3. #include "includes.h"
  4. #include "stm32f4xx_rcc.h"
  5. #include <stm32f4xx.h>
  6. typedef enum
  7. {
  8. kI2C0 = 0,
  9. kI2C1,
  10. kI2C2,
  11. kADC0,
  12. kADC3,
  13. kSPI1,
  14. kUart1,
  15. kUart3,
  16. kCAN1,
  17. kCAN2,
  18. kEthernet_dm9101,
  19. kFSMC,
  20. kInput,
  21. kOutput,
  22. kPMU,
  23. kDMA1,
  24. kAM2303,
  25. // kTIMER1,
  26. kInterfaceEnd,
  27. } interface_type;
  28. typedef enum
  29. {
  30. RCC_APB1Periph,
  31. RCC_APB2Periph,
  32. RCC_AHB1Periph,
  33. RCC_AHB2Periph,
  34. RCC_AHB3Periph,
  35. } enum_clock_type;
  36. typedef struct
  37. {
  38. interface_type type;
  39. enum_clock_type RCC_AXBPeriph;
  40. uint32_t AF_Clock;
  41. } Gpio_Clock;
  42. typedef enum
  43. {
  44. kTFTLCD,
  45. kRunLed,
  46. kAd7606,
  47. kPRelayCtr,
  48. kNRelayCtr,
  49. kPreRelayCtr,
  50. kFanCtr,
  51. kOut1Ctr, // charger
  52. kOut2Ctr, // charger heat
  53. kExtWatchDog,
  54. kExtRelayLed,
  55. kExtFaultLed,
  56. kISOPRelayCtr,
  57. kISONRelayCtr,
  58. kSoftI2C3_SDA,
  59. kSoftI2C3_SCL,
  60. kSoftI2C4_SDA,
  61. kSoftI2C4_SCL,
  62. kOutputEnd,
  63. } Output_Type;
  64. typedef enum
  65. {
  66. DM9K_INIT,
  67. NOR_FLASH_STATE,
  68. kInputEnd,
  69. kKey,
  70. } Input_type;
  71. typedef struct
  72. {
  73. GPIO_TypeDef *GPIOx;
  74. INT16U GPIO_PinSource;
  75. INT8U GPIO_AF;
  76. } GPIO_AF_Info;
  77. typedef struct
  78. {
  79. interface_type type;
  80. Output_Type Out_Type;
  81. Input_type In_Type;
  82. GPIO_TypeDef *GPIOx;
  83. INT32U GPIO_Pin;
  84. GPIOMode_TypeDef GPIO_Mode;
  85. GPIO_AF_Info AF_Info;
  86. GPIOOType_TypeDef GPIO_OType;
  87. GPIOSpeed_TypeDef GPIO_Speed;
  88. GPIOPuPd_TypeDef GPIO_PuPd;
  89. } Interface_struct;
  90. // // soft I2C
  91. // #define SI2C3_SDA_PIN GPIO_PIN_3
  92. // #define SI2C3_SDA_PORT GPIOD
  93. // #define SI2C3_SCL_PIN GPIO_PIN_2
  94. // #define SI2C3_SCL_PORT GPIOD
  95. // #define SI2C4_SDA_PIN GPIO_PIN_15
  96. // #define SI2C4_SDA_PORT GPIOE
  97. // #define SI2C4_SCL_PIN GPIO_PIN_14
  98. // #define SI2C4_SCL_PORT GPIOE
  99. // #define SI2C3_SDA_HIGH GPIO_SetBits(SI2C3_SDA_PORT, SI2C3_SDA_PIN)
  100. // #define SI2C3_SDA_LOW GPIO_ResetBits(SI2C3_SDA_PORT, SI2C3_SDA_PIN)
  101. // #define SI2C3_SCL_HIGH GPIO_SetBits(SI2C3_SCL_PORT, SI2C3_SCL_PIN)
  102. // #define SI2C3_SCL_LOW GPIO_ResetBits(SI2C3_SCL_PORT, SI2C3_SCL_PIN)
  103. // #define SI2C4_SDA_HIGH GPIO_SetBits(SI2C4_SDA_PORT, SI2C4_SDA_PIN)
  104. // #define SI2C4_SDA_LOW GPIO_ResetBits(SI2C4_SDA_PORT, SI2C4_SDA_PIN)
  105. // #define SI2C4_SCL_HIGH GPIO_SetBits(SI2C4_SCL_PORT, SI2C4_SCL_PIN)
  106. // #define SI2C4_SCL_LOW GPIO_ResetBits(SI2C4_SCL_PORT, SI2C4_SCL_PIN)
  107. // #define SI2C3_SDA_OUT gpio_mode_set(SI2C3_SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, SI2C3_SDA_PIN)
  108. // #define SI2C3_SDA_IN gpio_mode_set(SI2C3_SDA_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SI2C3_SDA_PIN)
  109. // #define SI2C3_SDA_READ gpio_input_bit_get(SI2C3_SDA_PORT, SI2C3_SDA_PIN)
  110. // #define SI2C4_SDA_OUT gpio_mode_set(SI2C4_SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, SI2C4_SDA_PIN)
  111. // #define SI2C4_SDA_IN gpio_mode_set(SI2C4_SDA_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SI2C4_SDA_PIN)
  112. // #define SI2C4_SDA_READ gpio_input_bit_get(SI2C4_SDA_PORT, SI2C4_SDA_PIN)
  113. // uart
  114. #define UART3_ENABLE_PORT GPIOB
  115. #define UART3_ENABLE_PIN GPIO_Pin_2
  116. #define UART3_TX_ENABLE GPIO_SetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
  117. #define UART3_RX_ENABLE GPIO_ResetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
  118. // SPI
  119. #define SPI1_CS_PORT GPIOF
  120. #define SPI1_CS_PIN GPIO_Pin_8
  121. #define SPI1_CS_HIGH GPIO_SetBits(SPI2_CS_PORT, SPI2_CS_PIN)
  122. #define SPI1_CS_LOW GPIO_ResetBits(SPI2_CS_PORT, SPI2_CS_PIN)
  123. // ethernet
  124. #define ETH_RESET_PORT GPIOH
  125. #define ETH_RESET_PIN GPIO_Pin_6
  126. // #define ETH_RXER_PORT GPIOG
  127. // #define ETH_RXER_PIN GPIO_PIN_12
  128. #define ETH_RESET_ON GPIO_ResetBits(ETH_RESET_PORT, ETH_RESET_PIN)
  129. #define ETH_RESET_OFF GPIO_SetBits(ETH_RESET_PORT, ETH_RESET_PIN)
  130. // input
  131. #define DM9K_RESET_PORT GPIOA
  132. #define DM9K_RESET_PIN GPIO_Pin_15
  133. #define DM9K_RESET_ON GPIO_ResetBits(DM9K_RESET_PORT, DM9K_RESET_PIN)
  134. #define DM9K_RESET_OFF GPIO_SetBits(DM9K_RESET_PORT, DM9K_RESET_PIN)
  135. #define NOR_FLASH_STATE_PORT GPIOD
  136. #define NOR_FLASH_STATE_PIN GPIO_Pin_6
  137. #define NOR_FLASH_STATE_ON GPIO_ResetBits(NOR_FLASH_STATE_PORT, NOR_FLASH_STATE_PIN)
  138. #define NOR_FLASH_STATE_OFF GPIO_SetBits(NOR_FLASH_STATE_PORT, NOR_FLASH_STATE_PIN)
  139. #define GPIO_PORT_K1 GPIOI
  140. #define GPIO_PIN_K1 GPIO_Pin_8
  141. #define GPIO_PORT_K2 GPIOC
  142. #define GPIO_PIN_K2 GPIO_Pin_13
  143. #define GPIO_PORT_K3 GPIOI
  144. #define GPIO_PIN_K3 GPIO_Pin_11
  145. #define GPIO_PORT_K4 GPIOH
  146. #define GPIO_PIN_K4 GPIO_Pin_2
  147. #define GPIO_PORT_K5 GPIOH
  148. #define GPIO_PIN_K5 GPIO_Pin_3
  149. #define GPIO_PORT_K6 GPIOF
  150. #define GPIO_PIN_K6 GPIO_Pin_11
  151. #define GPIO_PORT_K7 GPIOG
  152. #define GPIO_PIN_K7 GPIO_Pin_7
  153. #define GPIO_PORT_K8 GPIOH
  154. #define GPIO_PIN_K8 GPIO_Pin_15
  155. // output
  156. #define LED0_RUN_PORT GPIOC
  157. #define LED0_RUN_PIN GPIO_Pin_2
  158. #define LED1_RUN_PORT GPIOF
  159. #define LED1_RUN_PIN GPIO_Pin_7
  160. #define LED2_RUN_PORT GPIOF
  161. #define LED2_RUN_PIN GPIO_Pin_8
  162. #define LED3_RUN_PORT GPIOI
  163. #define LED3_RUN_PIN GPIO_Pin_10
  164. #define LCD_PWM_PORT GPIOF
  165. #define LCD_PWM_PIN GPIO_Pin_6
  166. #define LCD_BUSY_PORT GPIOD
  167. #define LCD_BUSY_PIN GPIO_Pin_3
  168. // #define LED2_RUN_PORT GPIOF
  169. // #define LED2_RUN_PIN GPIO_Pin_8
  170. // #define LED3_RUN_PORT GPIOI
  171. // #define LED3_RUN_PIN GPIO_Pin_10
  172. /* 配置几个控制用的GPIO
  173. PH9/DCMI_D0/AD7606_OS0 ---> AD7606_OS0 OS2:OS0 选择数字滤波参数
  174. PH10/DCMI_D1/AD7606_OS1 ---> AD7606_OS1
  175. PH11/DCMI_D2/AD7606_OS2 ---> AD7606_OS2
  176. PH12/DCMI_D3/AD7606_CONVST ---> AD7606_CONVST 启动ADC转换
  177. PH14/DCMI_D4/AD7606_RAGE ---> AD7606_RAGE 输入模拟电压量程,正负5V或正负10V
  178. PI4/DCMI_D5/AD7606_RESET ---> AD7606_RESET 复位
  179. PI6/DCMI_D6/AD7606_BUSY ---> AD7606_BUSY 转换结束的信号
  180. */
  181. #define AD7606_OS0_PORT GPIOH
  182. #define AD7606_OS0_PIN GPIO_Pin_9
  183. #define AD7606_OS1_PORT GPIOH
  184. #define AD7606_OS1_PIN GPIO_Pin_10
  185. #define AD7606_OS2_PORT GPIOH
  186. #define AD7606_OS2_PIN GPIO_Pin_11
  187. #define AD7606_CONVST_PORT GPIOH
  188. #define AD7606_CONVST_PIN GPIO_Pin_12
  189. #define AD7606_RAGE_PORT GPIOH
  190. #define AD7606_RAGE_PIN GPIO_Pin_14
  191. #define AD7606_RESET_PORT GPIOI
  192. #define AD7606_RESET_PIN GPIO_Pin_4
  193. #define AD7606_BUSY_PORT GPIOI
  194. #define AD7606_BUSY_PIN GPIO_Pin_6
  195. #define AM2303_PORT GPIOB
  196. #define AM2303_PIN GPIO_Pin_1
  197. // #define PRelayCtr_PORT GPIOD
  198. // #define PRelayCtr_PIN GPIO_PIN_6
  199. // #define NRelayCtr_PORT GPIOD
  200. // #define NRelayCtr_PIN GPIO_PIN_5
  201. // #define PreRelayCtr_PORT GPIOG
  202. // #define PreRelayCtr_PIN GPIO_PIN_9
  203. // #define Fan_PORT GPIOG
  204. // #define Fan_PIN GPIO_PIN_10
  205. // #define OUT1CTR_PORT GPIOD
  206. // #define OUT1CTR_PIN GPIO_PIN_7
  207. // #define OUT2CTR_PORT GPIOE
  208. // #define OUT2CTR_PIN GPIO_PIN_1
  209. // #define EXTWATCHDOG_PORT GPIOC
  210. // #define EXTWATCHDOG_PIN GPIO_PIN_3
  211. // #define ExtRelayLed_PORT GPIOD
  212. // #define ExtRelayLed_PIN GPIO_PIN_11
  213. // #define ExtFaultLed_PORT GPIOD
  214. // #define ExtFaultLed_PIN GPIO_PIN_10
  215. // #define ISOPRelayCtr_PORT GPIOF
  216. // #define ISOPRelayCtr_PIN GPIO_PIN_14
  217. // #define ISONRelayCtr_PORT GPIOF
  218. // #define ISONRelayCtr_PIN GPIO_PIN_15
  219. // // input
  220. // #define LDetect1_PORT GPIOD
  221. // #define LDetect1_PIN GPIO_PIN_13
  222. // output operation
  223. #define LED0_RUN_ON GPIO_SetBits(LED0_RUN_PORT, LED0_RUN_PIN)
  224. #define LED0_RUN_OFF GPIO_ResetBits(LED0_RUN_PORT, LED0_RUN_PIN)
  225. #define LED0_RUN_TOGGLE GPIO_ToggleBits(LED0_RUN_PORT, LED0_RUN_PIN)
  226. #define LED1_RUN_ON GPIO_SetBits(LED1_RUN_PORT, LED1_RUN_PIN)
  227. #define LED1_RUN_OFF GPIO_ResetBits(LED1_RUN_PORT, LED1_RUN_PIN)
  228. #define LED1_RUN_TOGGLE GPIO_ToggleBits(LED1_RUN_PORT, LED1_RUN_PIN)
  229. #define LED2_RUN_ON GPIO_SetBits(LED2_RUN_PORT, LED2_RUN_PIN)
  230. #define LED2_RUN_OFF GPIO_ResetBits(LED2_RUN_PORT, LED2_RUN_PIN)
  231. #define LED2_RUN_TOGGLE GPIO_ToggleBits(LED2_RUN_PORT, LED2_RUN_PIN)
  232. // TFTLCD
  233. #define LCD_PWM_HIGH GPIO_SetBits(LCD_PWM_PORT, LCD_PWM_PIN)
  234. #define LCD_PWM_LOW GPIO_ResetBits(LCD_PWM_PORT, LCD_PWM_PIN)
  235. #define AM2303_DO_HIGH GPIO_SetBits(AM2303_PORT, AM2303_PIN)
  236. #define AM2303_DO_LOW GPIO_ResetBits(AM2303_PORT, AM2303_PIN)
  237. #define AM2303_TOGGLE GPIO_ToggleBits(AM2303_PORT, AM2303_PIN)
  238. // IO方向设置
  239. #define AM2303_IO_IN \
  240. { \
  241. GPIOB->MODER &= ~(3 << (1 * 2)); \
  242. GPIOB->MODER |= 0 << (1 * 2); \
  243. }
  244. #define AM2303_IO_OUT \
  245. { \
  246. GPIOB->MODER &= ~(3 << (1 * 2)); \
  247. GPIOB->MODER |= 1 << (1 * 2); \
  248. }
  249. #define DQ_IS_LOW() ((AM2303_PORT->IDR & AM2303_PIN) == 0)
  250. // #define LED3_RUN_ON GPIO_SetBits(LED3_RUN_PORT, LED3_RUN_PIN)
  251. // #define LED3_RUN_OFF GPIO_ResetBits(LED3_RUN_PORT, LED3_RUN_PIN)
  252. // #define LED3_RUN_TOGGLE GPIO_ToggleBits(LED3_RUN_PORT, LED3_RUN_PIN)
  253. // #define PRelayCtr_ON GPIO_SetBits(PRelayCtr_PORT, PRelayCtr_PIN)
  254. // #define PRelayCtr_OFF GPIO_ResetBits(PRelayCtr_PORT, PRelayCtr_PIN)
  255. // #define NRelayCtr_ON GPIO_SetBits(NRelayCtr_PORT, NRelayCtr_PIN)
  256. // #define NRelayCtr_OFF GPIO_ResetBits(NRelayCtr_PORT, NRelayCtr_PIN)
  257. // #define PreRelayCtr_ON GPIO_SetBits(PreRelayCtr_PORT, PreRelayCtr_PIN)
  258. // #define PreRelayCtr_OFF GPIO_ResetBits(PreRelayCtr_PORT, PreRelayCtr_PIN)
  259. // #define FAN_ON GPIO_SetBits(Fan_PORT, Fan_PIN)
  260. // #define FAN_OFF GPIO_ResetBits(Fan_PORT, Fan_PIN)
  261. // #define OUT1CTR_ON GPIO_SetBits(OUT1CTR_PORT, OUT1CTR_PIN)
  262. // #define OUT1CTR_OFF GPIO_ResetBits(OUT1CTR_PORT, OUT1CTR_PIN)
  263. // #define OUT2CTR_ON GPIO_SetBits(OUT2CTR_PORT, OUT2CTR_PIN)
  264. // #define OUT2CTR_OFF GPIO_ResetBits(OUT2CTR_PORT, OUT2CTR_PIN)
  265. // #define FEED_DOG_ON GPIO_SetBits(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN)
  266. // #define FEED_DOG_OFF GPIO_ResetBits(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN)
  267. // #define FEED_DOG_TOGGLE GPIO_WriteBit(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN, (FlagStatus)(1 - (gpio_output_bit_get(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN))))
  268. // #define ExtRelayLed_ON GPIO_SetBits(ExtRelayLed_PORT, ExtRelayLed_PIN)
  269. // #define ExtRelayLed_OFF GPIO_ResetBits(ExtRelayLed_PORT, ExtRelayLed_PIN)
  270. // #define ExtFaultLed_ON GPIO_SetBits(ExtFaultLed_PORT, ExtFaultLed_PIN)
  271. // #define ExtFaultLed_OFF GPIO_ResetBits(ExtFaultLed_PORT, ExtFaultLed_PIN)
  272. // #define ISOPRelay_ON GPIO_SetBits(ISOPRelayCtr_PORT, ISOPRelayCtr_PIN)
  273. // #define ISOPRelay_OFF GPIO_ResetBits(ISOPRelayCtr_PORT, ISOPRelayCtr_PIN)
  274. // #define ISONRelay_ON GPIO_SetBits(ISONRelayCtr_PORT, ISONRelayCtr_PIN)
  275. // #define ISONRelay_OFF GPIO_ResetBits(ISONRelayCtr_PORT, ISONRelayCtr_PIN)
  276. // // input result
  277. // #define L_IN1_IS_LOW (gpio_input_bit_get(LDetect1_PORT, LDetect1_PIN) == RESET)
  278. void interface_init(void);
  279. void gpio_clock_init(void);
  280. #endif