interface.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 UART3_ENABLE_PORT GPIOB
  109. #define UART3_ENABLE_PIN GPIO_Pin_2
  110. #define UART3_TX_ENABLE GPIO_SetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
  111. #define UART3_RX_ENABLE GPIO_ResetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
  112. // // SPI
  113. // #define SPI2_CS_PORT GPIOA
  114. // #define SPI2_CS_PIN GPIO_PIN_15
  115. // #define SPI2_CS_HIGH GPIO_SetBits(SPI2_CS_PORT, SPI2_CS_PIN)
  116. // #define SPI2_CS_LOW GPIO_ResetBits(SPI2_CS_PORT, SPI2_CS_PIN)
  117. // ethernet
  118. #define ETH_RESET_PORT GPIOH
  119. #define ETH_RESET_PIN GPIO_Pin_6
  120. // #define ETH_RXER_PORT GPIOG
  121. // #define ETH_RXER_PIN GPIO_PIN_12
  122. #define ETH_RESET_ON GPIO_ResetBits(ETH_RESET_PORT, ETH_RESET_PIN)
  123. #define ETH_RESET_OFF GPIO_SetBits(ETH_RESET_PORT, ETH_RESET_PIN)
  124. // output
  125. #define LED0_RUN_PORT GPIOC
  126. #define LED0_RUN_PIN GPIO_Pin_2
  127. #define LED1_RUN_PORT GPIOF
  128. #define LED1_RUN_PIN GPIO_Pin_7
  129. #define LED2_RUN_PORT GPIOF
  130. #define LED2_RUN_PIN GPIO_Pin_8
  131. #define LED3_RUN_PORT GPIOI
  132. #define LED3_RUN_PIN GPIO_Pin_10
  133. // #define PRelayCtr_PORT GPIOD
  134. // #define PRelayCtr_PIN GPIO_PIN_6
  135. // #define NRelayCtr_PORT GPIOD
  136. // #define NRelayCtr_PIN GPIO_PIN_5
  137. // #define PreRelayCtr_PORT GPIOG
  138. // #define PreRelayCtr_PIN GPIO_PIN_9
  139. // #define Fan_PORT GPIOG
  140. // #define Fan_PIN GPIO_PIN_10
  141. // #define OUT1CTR_PORT GPIOD
  142. // #define OUT1CTR_PIN GPIO_PIN_7
  143. // #define OUT2CTR_PORT GPIOE
  144. // #define OUT2CTR_PIN GPIO_PIN_1
  145. // #define EXTWATCHDOG_PORT GPIOC
  146. // #define EXTWATCHDOG_PIN GPIO_PIN_3
  147. // #define ExtRelayLed_PORT GPIOD
  148. // #define ExtRelayLed_PIN GPIO_PIN_11
  149. // #define ExtFaultLed_PORT GPIOD
  150. // #define ExtFaultLed_PIN GPIO_PIN_10
  151. // #define ISOPRelayCtr_PORT GPIOF
  152. // #define ISOPRelayCtr_PIN GPIO_PIN_14
  153. // #define ISONRelayCtr_PORT GPIOF
  154. // #define ISONRelayCtr_PIN GPIO_PIN_15
  155. // // input
  156. // #define LDetect1_PORT GPIOD
  157. // #define LDetect1_PIN GPIO_PIN_13
  158. // output operation
  159. #define LED0_RUN_ON GPIO_SetBits(LED0_RUN_PORT, LED0_RUN_PIN)
  160. #define LED0_RUN_OFF GPIO_ResetBits(LED0_RUN_PORT, LED0_RUN_PIN)
  161. #define LED0_RUN_TOGGLE GPIO_ToggleBits(LED0_RUN_PORT, LED0_RUN_PIN)
  162. #define LED1_RUN_ON GPIO_SetBits(LED1_RUN_PORT, LED1_RUN_PIN)
  163. #define LED1_RUN_OFF GPIO_ResetBits(LED1_RUN_PORT, LED1_RUN_PIN)
  164. #define LED1_RUN_TOGGLE GPIO_ToggleBits(LED1_RUN_PORT, LED1_RUN_PIN)
  165. #define LED2_RUN_ON GPIO_SetBits(LED2_RUN_PORT, LED2_RUN_PIN)
  166. #define LED2_RUN_OFF GPIO_ResetBits(LED2_RUN_PORT, LED2_RUN_PIN)
  167. #define LED2_RUN_TOGGLE GPIO_ToggleBits(LED2_RUN_PORT, LED2_RUN_PIN)
  168. #define LED3_RUN_ON GPIO_SetBits(LED3_RUN_PORT, LED3_RUN_PIN)
  169. #define LED3_RUN_OFF GPIO_ResetBits(LED3_RUN_PORT, LED3_RUN_PIN)
  170. #define LED3_RUN_TOGGLE GPIO_ToggleBits(LED3_RUN_PORT, LED3_RUN_PIN)
  171. // #define PRelayCtr_ON GPIO_SetBits(PRelayCtr_PORT, PRelayCtr_PIN)
  172. // #define PRelayCtr_OFF GPIO_ResetBits(PRelayCtr_PORT, PRelayCtr_PIN)
  173. // #define NRelayCtr_ON GPIO_SetBits(NRelayCtr_PORT, NRelayCtr_PIN)
  174. // #define NRelayCtr_OFF GPIO_ResetBits(NRelayCtr_PORT, NRelayCtr_PIN)
  175. // #define PreRelayCtr_ON GPIO_SetBits(PreRelayCtr_PORT, PreRelayCtr_PIN)
  176. // #define PreRelayCtr_OFF GPIO_ResetBits(PreRelayCtr_PORT, PreRelayCtr_PIN)
  177. // #define FAN_ON GPIO_SetBits(Fan_PORT, Fan_PIN)
  178. // #define FAN_OFF GPIO_ResetBits(Fan_PORT, Fan_PIN)
  179. // #define OUT1CTR_ON GPIO_SetBits(OUT1CTR_PORT, OUT1CTR_PIN)
  180. // #define OUT1CTR_OFF GPIO_ResetBits(OUT1CTR_PORT, OUT1CTR_PIN)
  181. // #define OUT2CTR_ON GPIO_SetBits(OUT2CTR_PORT, OUT2CTR_PIN)
  182. // #define OUT2CTR_OFF GPIO_ResetBits(OUT2CTR_PORT, OUT2CTR_PIN)
  183. // #define FEED_DOG_ON GPIO_SetBits(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN)
  184. // #define FEED_DOG_OFF GPIO_ResetBits(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN)
  185. // #define FEED_DOG_TOGGLE GPIO_WriteBit(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN, (FlagStatus)(1 - (gpio_output_bit_get(EXTWATCHDOG_PORT, EXTWATCHDOG_PIN))))
  186. // #define ExtRelayLed_ON GPIO_SetBits(ExtRelayLed_PORT, ExtRelayLed_PIN)
  187. // #define ExtRelayLed_OFF GPIO_ResetBits(ExtRelayLed_PORT, ExtRelayLed_PIN)
  188. // #define ExtFaultLed_ON GPIO_SetBits(ExtFaultLed_PORT, ExtFaultLed_PIN)
  189. // #define ExtFaultLed_OFF GPIO_ResetBits(ExtFaultLed_PORT, ExtFaultLed_PIN)
  190. // #define ISOPRelay_ON GPIO_SetBits(ISOPRelayCtr_PORT, ISOPRelayCtr_PIN)
  191. // #define ISOPRelay_OFF GPIO_ResetBits(ISOPRelayCtr_PORT, ISOPRelayCtr_PIN)
  192. // #define ISONRelay_ON GPIO_SetBits(ISONRelayCtr_PORT, ISONRelayCtr_PIN)
  193. // #define ISONRelay_OFF GPIO_ResetBits(ISONRelayCtr_PORT, ISONRelayCtr_PIN)
  194. // // input result
  195. // #define L_IN1_IS_LOW (gpio_input_bit_get(LDetect1_PORT, LDetect1_PIN) == RESET)
  196. void interface_init(void);
  197. void gpio_clock_init(void);
  198. #endif