interface.h 11 KB

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