interface.h 11 KB

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