interface.h 11 KB

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