You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
9.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "zgpio.hpp"
  2. #define TAG "GPIO"
  3. namespace iflytop {
  4. /*******************************************************************************
  5. * LISTENER *
  6. *******************************************************************************/
  7. static ZGPIO *s_irqGPIO[20];
  8. int s_irqGPIO_num = 0;
  9. extern "C" {
  10. /**
  11. * @brief This function handles EXTI line3 interrupt.
  12. */
  13. void EXTI0_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); }
  14. void EXTI1_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1); }
  15. void EXTI2_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2); }
  16. void EXTI3_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3); }
  17. void EXTI4_IRQHandler() { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); }
  18. void EXTI9_5_IRQHandler(void) {
  19. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
  20. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);
  21. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_7);
  22. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_8);
  23. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);
  24. }
  25. void EXTI15_10_IRQHandler(void) {
  26. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10);
  27. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
  28. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12);
  29. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
  30. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_14);
  31. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15);
  32. }
  33. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
  34. for (int i = 0; i < s_irqGPIO_num; i++) {
  35. s_irqGPIO[i]->tryTriggerIRQ(GPIO_Pin);
  36. }
  37. }
  38. }
  39. void ZGPIO::regListener(onirq_t listener) { m_onirq = listener; }
  40. /*******************************************************************************
  41. * GPIOIMPL *
  42. *******************************************************************************/
  43. /*******************************************************************************
  44. * BASE_FUNC *
  45. *******************************************************************************/
  46. bool ZGPIO::enableClock() {
  47. #ifdef GPIOA
  48. if (m_gpio == GPIOA) {
  49. __HAL_RCC_GPIOA_CLK_ENABLE();
  50. return true;
  51. }
  52. #endif
  53. #ifdef GPIOB
  54. if (m_gpio == GPIOB) {
  55. __HAL_RCC_GPIOB_CLK_ENABLE();
  56. return true;
  57. }
  58. #endif
  59. #ifdef GPIOC
  60. if (m_gpio == GPIOC) {
  61. __HAL_RCC_GPIOC_CLK_ENABLE();
  62. return true;
  63. }
  64. #endif
  65. #ifdef GPIOD
  66. if (m_gpio == GPIOD) {
  67. __HAL_RCC_GPIOD_CLK_ENABLE();
  68. return true;
  69. }
  70. #endif
  71. #ifdef GPIOE
  72. if (m_gpio == GPIOE) {
  73. __HAL_RCC_GPIOE_CLK_ENABLE();
  74. return true;
  75. }
  76. #endif
  77. #ifdef GPIOF
  78. if (m_gpio == GPIOF) {
  79. __HAL_RCC_GPIOF_CLK_ENABLE();
  80. return true;
  81. }
  82. #endif
  83. #ifdef GPIOG
  84. if (m_gpio == GPIOG) {
  85. __HAL_RCC_GPIOG_CLK_ENABLE();
  86. return true;
  87. }
  88. #endif
  89. #ifdef GPIOH
  90. if (m_gpio == GPIOH) {
  91. __HAL_RCC_GPIOH_CLK_ENABLE();
  92. return true;
  93. }
  94. #endif
  95. #ifdef GPIOI
  96. if (m_gpio == GPIOI) {
  97. __HAL_RCC_GPIOI_CLK_ENABLE();
  98. return true;
  99. }
  100. #endif
  101. #ifdef GPIOJ
  102. if (m_gpio == GPIOJ) {
  103. __HAL_RCC_GPIOJ_CLK_ENABLE();
  104. return true;
  105. }
  106. #endif
  107. #ifdef GPIOK
  108. if (m_gpio == GPIOK) {
  109. __HAL_RCC_GPIOK_CLK_ENABLE();
  110. return true;
  111. }
  112. #endif
  113. return false;
  114. }
  115. void regIRQGPIO(ZGPIO *gpio) {
  116. for (int i = 0; i < s_irqGPIO_num; i++) {
  117. if (s_irqGPIO[i] == gpio) {
  118. return;
  119. }
  120. }
  121. EARLY_ASSERT((s_irqGPIO_num + 1) < (int)ZARRAY_SIZE(s_irqGPIO));
  122. s_irqGPIO[s_irqGPIO_num] = gpio;
  123. s_irqGPIO_num++;
  124. }
  125. bool ZGPIO::isMirror() { return m_mirror; }
  126. void ZGPIO::initAsInput(Pin_t pin, GPIOMode_t mode, GPIOIrqType_t irqtype, bool mirror) {
  127. ZEARLY_ASSERT(pin != PinNull);
  128. m_mirror = mirror;
  129. m_mode = mode;
  130. m_irqtype = irqtype;
  131. m_gpiotype = kType_Input;
  132. m_gpio = chip_get_gpio(pin);
  133. m_pinoff = chip_get_pinoff(pin);
  134. uint32_t pulluptype = 0;
  135. if (mode == kMode_nopull) {
  136. pulluptype = GPIO_NOPULL;
  137. } else if (mode == kMode_pullup) {
  138. pulluptype = GPIO_PULLUP;
  139. } else if (mode == kMode_pulldown) {
  140. pulluptype = GPIO_PULLDOWN;
  141. }
  142. enableClock();
  143. GPIO_InitTypeDef m_GPIO_InitStruct = {0};
  144. if (m_irqtype == kIRQ_noIrq) {
  145. m_GPIO_InitStruct.Pin = m_pinoff;
  146. m_GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  147. m_GPIO_InitStruct.Pull = pulluptype;
  148. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  149. } else if (m_irqtype == kIRQ_risingIrq) {
  150. m_GPIO_InitStruct.Pin = m_pinoff;
  151. m_GPIO_InitStruct.Mode = m_mirror ? GPIO_MODE_IT_FALLING : GPIO_MODE_IT_RISING;
  152. m_GPIO_InitStruct.Pull = pulluptype;
  153. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  154. } else if (m_irqtype == kIRQ_fallingIrq) {
  155. m_GPIO_InitStruct.Pin = m_pinoff;
  156. m_GPIO_InitStruct.Mode = !m_mirror ? GPIO_MODE_IT_FALLING : GPIO_MODE_IT_RISING;
  157. m_GPIO_InitStruct.Pull = pulluptype;
  158. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  159. } else if (m_irqtype == kIRQ_risingAndFallingIrq) {
  160. m_GPIO_InitStruct.Pin = m_pinoff;
  161. m_GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  162. m_GPIO_InitStruct.Pull = pulluptype;
  163. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  164. }
  165. HAL_GPIO_Init(m_gpio, &m_GPIO_InitStruct);
  166. if (m_irqtype != kIRQ_noIrq) {
  167. regIRQGPIO(this);
  168. lastLevel = getState();
  169. HAL_NVIC_SetPriority(getEXTIIRQn(), IFLYTOP_PREEMPTPRIORITY_DEFAULT, 0);
  170. HAL_NVIC_EnableIRQ(getEXTIIRQn());
  171. }
  172. m_initflag = true;
  173. return;
  174. }
  175. void ZGPIO::initAsOutput(Pin_t pin, GPIOMode_t mode, bool mirror, bool initLevel) {
  176. ZEARLY_ASSERT(pin != PinNull);
  177. m_mirror = mirror;
  178. m_mode = mode;
  179. m_irqtype = kIRQ_noIrq;
  180. m_gpiotype = kType_Output;
  181. m_gpio = chip_get_gpio(pin);
  182. m_pinoff = chip_get_pinoff(pin);
  183. enableClock();
  184. GPIO_InitTypeDef m_GPIO_InitStruct = {0};
  185. initLevel = m_mirror ? !initLevel : initLevel;
  186. GPIO_PinState pinState = initLevel ? GPIO_PIN_SET : GPIO_PIN_RESET;
  187. HAL_GPIO_WritePin(m_gpio, m_pinoff, pinState);
  188. if (m_mode == kMode_nopull) {
  189. m_GPIO_InitStruct.Pin = m_pinoff;
  190. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  191. m_GPIO_InitStruct.Pull = GPIO_NOPULL;
  192. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  193. } else if (m_mode == kMode_pullup) {
  194. m_GPIO_InitStruct.Pin = m_pinoff;
  195. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  196. m_GPIO_InitStruct.Pull = GPIO_PULLUP;
  197. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  198. } else if (m_mode == kMode_pulldown) {
  199. m_GPIO_InitStruct.Pin = m_pinoff;
  200. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  201. m_GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  202. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  203. } else if (m_mode == kMode_od) {
  204. m_GPIO_InitStruct.Pin = m_pinoff;
  205. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  206. m_GPIO_InitStruct.Pull = 0;
  207. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  208. }
  209. HAL_GPIO_Init(m_gpio, &m_GPIO_InitStruct);
  210. m_initflag = true;
  211. return;
  212. }
  213. bool ZGPIO::isItRisingEXITGPIO() { return m_irqtype == kIRQ_risingIrq; }
  214. bool ZGPIO::isItFallingEXITGPIO() { return m_irqtype == kIRQ_fallingIrq; }
  215. bool ZGPIO::isItRisingAndItFallingEXITGPIO() { return m_irqtype == kIRQ_risingAndFallingIrq; }
  216. /*******************************************************************************
  217. * EXT FUNC *
  218. *******************************************************************************/
  219. /**
  220. * @brief жϵǰǷŲж
  221. *
  222. * @param checkloop
  223. * @param GPIO_Pin
  224. * @return true
  225. * @return false
  226. *
  227. * STM32GPIOжǹõģҪGPIOļʱ״̬жǷŲ??
  228. *
  229. * ж߼??:
  230. * жжģʽ GPIO_MODE_IT_RISING ?? GPIO_MODE_IT_FALLING ţǰƽжǷŲж
  231. * жжģʽ GPIO_MODE_IT_RISING_FALLING ţֱӷtrue
  232. */
  233. bool ZGPIO::tryTriggerIRQ(uint16_t GPIO_Pin) {
  234. bool ret = false;
  235. bool nostate = false;
  236. if (GPIO_Pin != m_pinoff) return false;
  237. if (!(isItRisingEXITGPIO() || isItFallingEXITGPIO() || isItRisingAndItFallingEXITGPIO())) {
  238. return false;
  239. }
  240. nostate = getState();
  241. if (isItRisingEXITGPIO()) {
  242. if (nostate) {
  243. ret = true;
  244. if (m_onirq) {
  245. m_onirq(this, kRisingIrqEvent);
  246. }
  247. }
  248. } else if (isItFallingEXITGPIO()) {
  249. if (!nostate) {
  250. ret = true;
  251. if (m_onirq) {
  252. m_onirq(this, kFallingIrqEvent);
  253. }
  254. }
  255. } else {
  256. if (lastLevel != nostate) {
  257. ret = true;
  258. if (m_onirq) {
  259. if (lastLevel)
  260. m_onirq(this, kRisingIrqEvent);
  261. else
  262. m_onirq(this, kFallingIrqEvent);
  263. }
  264. }
  265. }
  266. lastLevel = nostate;
  267. return ret;
  268. }
  269. void ZGPIO::toggleState() { HAL_GPIO_TogglePin(m_gpio, m_pinoff); }
  270. uint32_t ZGPIO::getStateUint32() {
  271. if (getState())
  272. return 1;
  273. else
  274. return 0;
  275. }
  276. bool ZGPIO::getState() {
  277. bool ret = false;
  278. if (HAL_GPIO_ReadPin(m_gpio, m_pinoff) == GPIO_PIN_SET) {
  279. ret = true;
  280. } else {
  281. ret = false;
  282. }
  283. if (m_mirror) ret = !ret;
  284. return ret;
  285. }
  286. bool ZGPIO::setState(bool state) {
  287. if (m_mirror) state = !state;
  288. if (m_log_when_setstate) ZEARLY_LOGI(TAG, "%s:%s set %d", chip_gpio_group_get_name(m_gpio), chip_pinoff_get_name(m_pinoff), state);
  289. if (state) {
  290. HAL_GPIO_WritePin(m_gpio, m_pinoff, GPIO_PIN_SET);
  291. } else {
  292. HAL_GPIO_WritePin(m_gpio, m_pinoff, GPIO_PIN_RESET);
  293. }
  294. return true;
  295. }
  296. IRQn_Type ZGPIO::getEXTIIRQn() {
  297. switch (m_pinoff) {
  298. case GPIO_PIN_0:
  299. return EXTI0_IRQn;
  300. case GPIO_PIN_1:
  301. return EXTI1_IRQn;
  302. case GPIO_PIN_2:
  303. return EXTI2_IRQn;
  304. case GPIO_PIN_3:
  305. return EXTI3_IRQn;
  306. case GPIO_PIN_4:
  307. return EXTI4_IRQn;
  308. case GPIO_PIN_5:
  309. case GPIO_PIN_6:
  310. case GPIO_PIN_7:
  311. case GPIO_PIN_8:
  312. case GPIO_PIN_9:
  313. return EXTI9_5_IRQn;
  314. case GPIO_PIN_10:
  315. case GPIO_PIN_11:
  316. case GPIO_PIN_12:
  317. case GPIO_PIN_13:
  318. case GPIO_PIN_14:
  319. case GPIO_PIN_15:
  320. return EXTI15_10_IRQn;
  321. default:
  322. ZEARLY_ASSERT(0);
  323. }
  324. return EXTI0_IRQn;
  325. }
  326. /**
  327. * @brief ʼGPIOΪжģ??
  328. *
  329. * @param pull GPIO_NOPULL, GPIO_PULLUP, GPIO_PULLDOWN
  330. * @param mode GPIO_MODE_IT_RISING, GPIO_MODE_IT_FALLING, GPIO_MODE_IT_RISING_FALLING
  331. * @return true
  332. * @return false
  333. */
  334. } // namespace iflytop