interface.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. kADC2,
  13. kSPI2,
  14. kUart1,
  15. kUart3,
  16. kCAN1,
  17. kCAN2,
  18. kEthernet,
  19. kInput,
  20. kOutput,
  21. kPMU,
  22. kDMA1,
  23. // kTIMER1,
  24. kInterfaceEnd,
  25. } interface_type;
  26. typedef enum
  27. {
  28. RCC_APB1Periph,
  29. RCC_APB2Periph,
  30. RCC_AHB1Periph,
  31. RCC_AHB2Periph,
  32. RCC_AHB3Periph,
  33. } enum_clock_type;
  34. typedef struct
  35. {
  36. interface_type type;
  37. enum_clock_type RCC_AXBPeriph;
  38. uint32_t AF_Clock;
  39. } Gpio_Clock;
  40. typedef enum
  41. {
  42. kRunLed,
  43. kPRelayCtr,
  44. kNRelayCtr,
  45. kPreRelayCtr,
  46. kFanCtr,
  47. kOut1Ctr, // charger
  48. kOut2Ctr, // charger heat
  49. kExtWatchDog,
  50. kExtRelayLed,
  51. kExtFaultLed,
  52. kISOPRelayCtr,
  53. kISONRelayCtr,
  54. kSoftI2C3_SDA,
  55. kSoftI2C3_SCL,
  56. kSoftI2C4_SDA,
  57. kSoftI2C4_SCL,
  58. kOutputEnd,
  59. } Output_Type;
  60. typedef enum
  61. {
  62. kLDetect,
  63. kInputEnd,
  64. } Input_type;
  65. typedef struct
  66. {
  67. GPIO_TypeDef *GPIOx;
  68. INT16U GPIO_PinSource;
  69. INT8U GPIO_AF;
  70. } GPIO_AF_Info;
  71. typedef struct
  72. {
  73. interface_type type;
  74. Output_Type Out_Type;
  75. Input_type In_Type;
  76. GPIO_TypeDef *GPIOx;
  77. INT32U GPIO_Pin;
  78. GPIOMode_TypeDef GPIO_Mode;
  79. GPIO_AF_Info AF_Info;
  80. GPIOOType_TypeDef GPIO_OType;
  81. GPIOSpeed_TypeDef GPIO_Speed;
  82. GPIOPuPd_TypeDef GPIO_PuPd;
  83. } Interface_struct;
  84. // // soft I2C
  85. // #define SI2C3_SDA_PIN GPIO_PIN_3
  86. // #define SI2C3_SDA_PORT GPIOD
  87. // #define SI2C3_SCL_PIN GPIO_PIN_2
  88. // #define SI2C3_SCL_PORT GPIOD
  89. // #define SI2C4_SDA_PIN GPIO_PIN_15
  90. // #define SI2C4_SDA_PORT GPIOE
  91. // #define SI2C4_SCL_PIN GPIO_PIN_14
  92. // #define SI2C4_SCL_PORT GPIOE
  93. // #define SI2C3_SDA_HIGH GPIO_SetBits(SI2C3_SDA_PORT, SI2C3_SDA_PIN)
  94. // #define SI2C3_SDA_LOW GPIO_ResetBits(SI2C3_SDA_PORT, SI2C3_SDA_PIN)
  95. // #define SI2C3_SCL_HIGH GPIO_SetBits(SI2C3_SCL_PORT, SI2C3_SCL_PIN)
  96. // #define SI2C3_SCL_LOW GPIO_ResetBits(SI2C3_SCL_PORT, SI2C3_SCL_PIN)
  97. // #define SI2C4_SDA_HIGH GPIO_SetBits(SI2C4_SDA_PORT, SI2C4_SDA_PIN)
  98. // #define SI2C4_SDA_LOW GPIO_ResetBits(SI2C4_SDA_PORT, SI2C4_SDA_PIN)
  99. // #define SI2C4_SCL_HIGH GPIO_SetBits(SI2C4_SCL_PORT, SI2C4_SCL_PIN)
  100. // #define SI2C4_SCL_LOW GPIO_ResetBits(SI2C4_SCL_PORT, SI2C4_SCL_PIN)
  101. // #define SI2C3_SDA_OUT gpio_mode_set(SI2C3_SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, SI2C3_SDA_PIN)
  102. // #define SI2C3_SDA_IN gpio_mode_set(SI2C3_SDA_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SI2C3_SDA_PIN)
  103. // #define SI2C3_SDA_READ gpio_input_bit_get(SI2C3_SDA_PORT, SI2C3_SDA_PIN)
  104. // #define SI2C4_SDA_OUT gpio_mode_set(SI2C4_SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, SI2C4_SDA_PIN)
  105. // #define SI2C4_SDA_IN gpio_mode_set(SI2C4_SDA_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SI2C4_SDA_PIN)
  106. // #define SI2C4_SDA_READ gpio_input_bit_get(SI2C4_SDA_PORT, SI2C4_SDA_PIN)
  107. // uart
  108. #define UART1_ENABLE_PORT GPIOA
  109. #define UART1_ENABLE_PIN GPIO_Pin_15
  110. #define UART3_ENABLE_PORT GPIOB
  111. #define UART3_ENABLE_PIN GPIO_Pin_2
  112. #define UART1_TX_ENABLE GPIO_SetBits(UART1_ENABLE_PORT, UART1_ENABLE_PIN)
  113. #define UART1_RX_ENABLE GPIO_ResetBits(UART1_ENABLE_PORT, UART1_ENABLE_PIN)
  114. #define UART3_TX_ENABLE GPIO_SetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
  115. #define UART3_RX_ENABLE GPIO_ResetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
  116. // // SPI
  117. // #define SPI2_CS_PORT GPIOA
  118. // #define SPI2_CS_PIN GPIO_PIN_15
  119. // #define SPI2_CS_HIGH GPIO_SetBits(SPI2_CS_PORT, SPI2_CS_PIN)
  120. // #define SPI2_CS_LOW GPIO_ResetBits(SPI2_CS_PORT, SPI2_CS_PIN)
  121. // // ethernet
  122. // //#define ENET_REF_CLK GPIO_PIN_1
  123. // //#define ENET_REF_CLK_Port GPIOA
  124. // //#define ENET_MDC GPIO_PIN_1
  125. // //#define ENET_MDC_Port GPIOC
  126. // //#define ENET_MDIO GPIO_PIN_2
  127. // //#define ENET_MDIO_Port GPIOA
  128. // //#define ENET_CRSDV GPIO_PIN_7
  129. // //#define ENET_CRSDV_Port GPIOA
  130. // //#define ENET_RXD0 GPIO_PIN_4
  131. // //#define ENET_RXD0_Port GPIOC
  132. // //#define ENET_RXD1 GPIO_PIN_5
  133. // //#define ENET_RXD1_Port GPIOC
  134. // //#define ENET_TXEN GPIO_PIN_11
  135. // //#define ENET_TXEN_Port GPIOG
  136. // //#define ENET_RXER GPIO_PIN_12
  137. // //#define ENET_RXER_Port GPIOG
  138. // //#define ENET_TXD0 GPIO_PIN_13
  139. // //#define ENET_TXD0_Port GPIOG
  140. // //#define ENET_TXD1 GPIO_PIN_14
  141. // //#define ENET_TXD1_Port GPIOG
  142. #define ETH_RESET_PORT GPIOH
  143. #define ETH_RESET_PIN GPIO_Pin_6
  144. // #define ETH_RXER_PORT GPIOG
  145. // #define ETH_RXER_PIN GPIO_PIN_12
  146. #define ETH_RESET_ON GPIO_ResetBits(ETH_RESET_PORT, ETH_RESET_PIN)
  147. #define ETH_RESET_OFF GPIO_SetBits(ETH_RESET_PORT, ETH_RESET_PIN)
  148. // output
  149. #define LED0_RUN_PORT GPIOC
  150. #define LED0_RUN_PIN GPIO_Pin_2
  151. #define LED1_RUN_PORT GPIOF
  152. #define LED1_RUN_PIN GPIO_Pin_7
  153. #define LED2_RUN_PORT GPIOF
  154. #define LED2_RUN_PIN GPIO_Pin_8
  155. #define LED3_RUN_PORT GPIOI
  156. #define LED3_RUN_PIN GPIO_Pin_10
  157. // #define PRelayCtr_PORT GPIOD
  158. // #define PRelayCtr_PIN GPIO_PIN_6
  159. // #define NRelayCtr_PORT GPIOD
  160. // #define NRelayCtr_PIN GPIO_PIN_5
  161. // #define PreRelayCtr_PORT GPIOG
  162. // #define PreRelayCtr_PIN GPIO_PIN_9
  163. // #define Fan_PORT GPIOG
  164. // #define Fan_PIN GPIO_PIN_10
  165. // #define OUT1CTR_PORT GPIOD
  166. // #define OUT1CTR_PIN GPIO_PIN_7
  167. // #define OUT2CTR_PORT GPIOE
  168. // #define OUT2CTR_PIN GPIO_PIN_1
  169. // #define EXTWATCHDOG_PORT GPIOC
  170. // #define EXTWATCHDOG_PIN GPIO_PIN_3
  171. // #define ExtRelayLed_PORT GPIOD
  172. // #define ExtRelayLed_PIN GPIO_PIN_11
  173. // #define ExtFaultLed_PORT GPIOD
  174. // #define ExtFaultLed_PIN GPIO_PIN_10
  175. // #define ISOPRelayCtr_PORT GPIOF
  176. // #define ISOPRelayCtr_PIN GPIO_PIN_14
  177. // #define ISONRelayCtr_PORT GPIOF
  178. // #define ISONRelayCtr_PIN GPIO_PIN_15
  179. // // input
  180. // #define LDetect1_PORT GPIOD
  181. // #define LDetect1_PIN GPIO_PIN_13
  182. // output operation
  183. #define LED0_RUN_ON GPIO_SetBits(LED0_RUN_PORT, LED0_RUN_PIN)
  184. #define LED0_RUN_OFF GPIO_ResetBits(LED0_RUN_PORT, LED0_RUN_PIN)
  185. #define LED0_RUN_TOGGLE GPIO_ToggleBits(LED0_RUN_PORT, LED0_RUN_PIN)
  186. #define LED1_RUN_ON GPIO_SetBits(LED1_RUN_PORT, LED1_RUN_PIN)
  187. #define LED1_RUN_OFF GPIO_ResetBits(LED1_RUN_PORT, LED1_RUN_PIN)
  188. #define LED1_RUN_TOGGLE GPIO_ToggleBits(LED1_RUN_PORT, LED1_RUN_PIN)
  189. #define LED2_RUN_ON GPIO_SetBits(LED2_RUN_PORT, LED2_RUN_PIN)
  190. #define LED2_RUN_OFF GPIO_ResetBits(LED2_RUN_PORT, LED2_RUN_PIN)
  191. #define LED2_RUN_TOGGLE GPIO_ToggleBits(LED2_RUN_PORT, LED2_RUN_PIN)
  192. #define LED3_RUN_ON GPIO_SetBits(LED3_RUN_PORT, LED3_RUN_PIN)
  193. #define LED3_RUN_OFF GPIO_ResetBits(LED3_RUN_PORT, LED3_RUN_PIN)
  194. #define LED3_RUN_TOGGLE GPIO_ToggleBits(LED3_RUN_PORT, LED3_RUN_PIN)
  195. // #define PRelayCtr_ON GPIO_SetBits(PRelayCtr_PORT, PRelayCtr_PIN)
  196. // #define PRelayCtr_OFF GPIO_ResetBits(PRelayCtr_PORT, PRelayCtr_PIN)
  197. // #define NRelayCtr_ON GPIO_SetBits(NRelayCtr_PORT, NRelayCtr_PIN)
  198. // #define NRelayCtr_OFF GPIO_ResetBits(NRelayCtr_PORT, NRelayCtr_PIN)
  199. // #define PreRelayCtr_ON GPIO_SetBits(PreRelayCtr_PORT, PreRelayCtr_PIN)
  200. // #define PreRelayCtr_OFF GPIO_ResetBits(PreRelayCtr_PORT, PreRelayCtr_PIN)
  201. // #define FAN_ON GPIO_SetBits(Fan_PORT, Fan_PIN)
  202. // #define FAN_OFF GPIO_ResetBits(Fan_PORT, Fan_PIN)
  203. // #define OUT1CTR_ON GPIO_SetBits(OUT1CTR_PORT, OUT1CTR_PIN)
  204. // #define OUT1CTR_OFF GPIO_ResetBits(OUT1CTR_PORT, OUT1CTR_PIN)
  205. // #define OUT2CTR_ON GPIO_SetBits(OUT2CTR_PORT, OUT2CTR_PIN)
  206. // #define OUT2CTR_OFF GPIO_ResetBits(OUT2CTR_PORT, OUT2CTR_PIN)
  207. // #define FEED_DOG_ON GPIO_SetBits(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN)
  208. // #define FEED_DOG_OFF GPIO_ResetBits(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN)
  209. // #define FEED_DOG_TOGGLE GPIO_WriteBit(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN, (FlagStatus)(1 - (gpio_output_bit_get(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN))))
  210. // #define ExtRelayLed_ON GPIO_SetBits(ExtRelayLed_PORT, ExtRelayLed_PIN)
  211. // #define ExtRelayLed_OFF GPIO_ResetBits(ExtRelayLed_PORT, ExtRelayLed_PIN)
  212. // #define ExtFaultLed_ON GPIO_SetBits(ExtFaultLed_PORT, ExtFaultLed_PIN)
  213. // #define ExtFaultLed_OFF GPIO_ResetBits(ExtFaultLed_PORT, ExtFaultLed_PIN)
  214. // #define ISOPRelay_ON GPIO_SetBits(ISOPRelayCtr_PORT, ISOPRelayCtr_PIN)
  215. // #define ISOPRelay_OFF GPIO_ResetBits(ISOPRelayCtr_PORT, ISOPRelayCtr_PIN)
  216. // #define ISONRelay_ON GPIO_SetBits(ISONRelayCtr_PORT, ISONRelayCtr_PIN)
  217. // #define ISONRelay_OFF GPIO_ResetBits(ISONRelayCtr_PORT, ISONRelayCtr_PIN)
  218. // // input result
  219. // #define L_IN1_IS_LOW (gpio_input_bit_get(LDetect1_PORT, LDetect1_PIN) == RESET)
  220. void interface_init(void);
  221. void gpio_clock_init(void);
  222. #endif