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.

368 lines
10 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
1 year ago
2 years ago
1 year ago
2 years ago
1 year 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 year 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. if (pin == PinNull) return;
  128. m_mirror = mirror;
  129. m_mode = mode;
  130. m_irqtype = irqtype;
  131. m_gpiotype = kType_Input;
  132. m_pin = pin;
  133. m_gpio = chip_get_gpio(pin);
  134. m_pinoff = chip_get_pinoff(pin);
  135. uint32_t pulluptype = 0;
  136. if (mode == kMode_nopull) {
  137. pulluptype = GPIO_NOPULL;
  138. } else if (mode == kMode_pullup) {
  139. pulluptype = GPIO_PULLUP;
  140. } else if (mode == kMode_pulldown) {
  141. pulluptype = GPIO_PULLDOWN;
  142. }
  143. enableClock();
  144. GPIO_InitTypeDef m_GPIO_InitStruct = {0};
  145. if (m_irqtype == kIRQ_noIrq) {
  146. m_GPIO_InitStruct.Pin = m_pinoff;
  147. m_GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  148. m_GPIO_InitStruct.Pull = pulluptype;
  149. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  150. } else if (m_irqtype == kIRQ_risingIrq) {
  151. m_GPIO_InitStruct.Pin = m_pinoff;
  152. m_GPIO_InitStruct.Mode = m_mirror ? GPIO_MODE_IT_FALLING : GPIO_MODE_IT_RISING;
  153. m_GPIO_InitStruct.Pull = pulluptype;
  154. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  155. } else if (m_irqtype == kIRQ_fallingIrq) {
  156. m_GPIO_InitStruct.Pin = m_pinoff;
  157. m_GPIO_InitStruct.Mode = !m_mirror ? GPIO_MODE_IT_FALLING : GPIO_MODE_IT_RISING;
  158. m_GPIO_InitStruct.Pull = pulluptype;
  159. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  160. } else if (m_irqtype == kIRQ_risingAndFallingIrq) {
  161. m_GPIO_InitStruct.Pin = m_pinoff;
  162. m_GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  163. m_GPIO_InitStruct.Pull = pulluptype;
  164. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  165. }
  166. HAL_GPIO_Init(m_gpio, &m_GPIO_InitStruct);
  167. if (m_irqtype != kIRQ_noIrq) {
  168. regIRQGPIO(this);
  169. lastLevel = getState();
  170. HAL_NVIC_SetPriority(getEXTIIRQn(), PC_IRQ_PREEMPTPRIORITY_DEFAULT, 0);
  171. HAL_NVIC_EnableIRQ(getEXTIIRQn());
  172. }
  173. m_initflag = true;
  174. return;
  175. }
  176. void ZGPIO::initAsOutput(Pin_t pin, GPIOMode_t mode, bool mirror, bool initLevel) {
  177. if (pin == PinNull) return;
  178. m_mirror = mirror;
  179. m_mode = mode;
  180. m_irqtype = kIRQ_noIrq;
  181. m_gpiotype = kType_Output;
  182. m_pin = pin;
  183. m_gpio = chip_get_gpio(pin);
  184. m_pinoff = chip_get_pinoff(pin);
  185. enableClock();
  186. GPIO_InitTypeDef m_GPIO_InitStruct = {0};
  187. initLevel = m_mirror ? !initLevel : initLevel;
  188. GPIO_PinState pinState = initLevel ? GPIO_PIN_SET : GPIO_PIN_RESET;
  189. HAL_GPIO_WritePin(m_gpio, m_pinoff, pinState);
  190. if (m_mode == kMode_nopull) {
  191. m_GPIO_InitStruct.Pin = m_pinoff;
  192. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  193. m_GPIO_InitStruct.Pull = GPIO_NOPULL;
  194. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  195. } else if (m_mode == kMode_pullup) {
  196. m_GPIO_InitStruct.Pin = m_pinoff;
  197. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  198. m_GPIO_InitStruct.Pull = GPIO_PULLUP;
  199. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  200. } else if (m_mode == kMode_pulldown) {
  201. m_GPIO_InitStruct.Pin = m_pinoff;
  202. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  203. m_GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  204. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  205. } else if (m_mode == kMode_od) {
  206. m_GPIO_InitStruct.Pin = m_pinoff;
  207. m_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  208. m_GPIO_InitStruct.Pull = 0;
  209. m_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  210. }
  211. HAL_GPIO_Init(m_gpio, &m_GPIO_InitStruct);
  212. m_initflag = true;
  213. return;
  214. }
  215. bool ZGPIO::isItRisingEXITGPIO() { return m_irqtype == kIRQ_risingIrq; }
  216. bool ZGPIO::isItFallingEXITGPIO() { return m_irqtype == kIRQ_fallingIrq; }
  217. bool ZGPIO::isItRisingAndItFallingEXITGPIO() { return m_irqtype == kIRQ_risingAndFallingIrq; }
  218. /*******************************************************************************
  219. * EXT FUNC *
  220. *******************************************************************************/
  221. /**
  222. * @brief
  223. *
  224. * @param checkloop
  225. * @param GPIO_Pin
  226. * @return true
  227. * @return false
  228. *
  229. * STM32的GPIO中断线是共用的GPIO的即时状态判断是否是这个引脚产生的中??
  230. *
  231. * ??:
  232. * GPIO_MODE_IT_RISING ?? GPIO_MODE_IT_FALLING
  233. * GPIO_MODE_IT_RISING_FALLING true
  234. */
  235. bool ZGPIO::tryTriggerIRQ(uint16_t GPIO_Pin) {
  236. bool ret = false;
  237. bool nostate = false;
  238. if (GPIO_Pin != m_pinoff) return false;
  239. if (!(isItRisingEXITGPIO() || isItFallingEXITGPIO() || isItRisingAndItFallingEXITGPIO())) {
  240. return false;
  241. }
  242. nostate = getState();
  243. if (isItRisingEXITGPIO()) {
  244. if (nostate) {
  245. ret = true;
  246. if (m_onirq) {
  247. m_onirq(this, kRisingIrqEvent);
  248. }
  249. }
  250. } else if (isItFallingEXITGPIO()) {
  251. if (!nostate) {
  252. ret = true;
  253. if (m_onirq) {
  254. m_onirq(this, kFallingIrqEvent);
  255. }
  256. }
  257. } else {
  258. if (lastLevel != nostate) {
  259. ret = true;
  260. if (m_onirq) {
  261. if (lastLevel)
  262. m_onirq(this, kRisingIrqEvent);
  263. else
  264. m_onirq(this, kFallingIrqEvent);
  265. }
  266. }
  267. }
  268. lastLevel = nostate;
  269. return ret;
  270. }
  271. void ZGPIO::toggleState() { HAL_GPIO_TogglePin(m_gpio, m_pinoff); }
  272. uint32_t ZGPIO::getStateUint32() {
  273. if (getState())
  274. return 1;
  275. else
  276. return 0;
  277. }
  278. bool ZGPIO::getState() {
  279. if (m_pin == PinNull) return false;
  280. bool ret = false;
  281. if (HAL_GPIO_ReadPin(m_gpio, m_pinoff) == GPIO_PIN_SET) {
  282. ret = true;
  283. } else {
  284. ret = false;
  285. }
  286. if (m_mirror) ret = !ret;
  287. return ret;
  288. }
  289. bool ZGPIO::setState(bool state) {
  290. if (m_pin == PinNull) return true;
  291. if (m_mirror) state = !state;
  292. 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);
  293. if (state) {
  294. HAL_GPIO_WritePin(m_gpio, m_pinoff, GPIO_PIN_SET);
  295. } else {
  296. HAL_GPIO_WritePin(m_gpio, m_pinoff, GPIO_PIN_RESET);
  297. }
  298. return true;
  299. }
  300. IRQn_Type ZGPIO::getEXTIIRQn() {
  301. switch (m_pinoff) {
  302. case GPIO_PIN_0:
  303. return EXTI0_IRQn;
  304. case GPIO_PIN_1:
  305. return EXTI1_IRQn;
  306. case GPIO_PIN_2:
  307. return EXTI2_IRQn;
  308. case GPIO_PIN_3:
  309. return EXTI3_IRQn;
  310. case GPIO_PIN_4:
  311. return EXTI4_IRQn;
  312. case GPIO_PIN_5:
  313. case GPIO_PIN_6:
  314. case GPIO_PIN_7:
  315. case GPIO_PIN_8:
  316. case GPIO_PIN_9:
  317. return EXTI9_5_IRQn;
  318. case GPIO_PIN_10:
  319. case GPIO_PIN_11:
  320. case GPIO_PIN_12:
  321. case GPIO_PIN_13:
  322. case GPIO_PIN_14:
  323. case GPIO_PIN_15:
  324. return EXTI15_10_IRQn;
  325. default:
  326. ZEARLY_ASSERT(0);
  327. }
  328. return EXTI0_IRQn;
  329. }
  330. /**
  331. * @brief GPIO为中断模??
  332. *
  333. * @param pull GPIO_NOPULL, GPIO_PULLUP, GPIO_PULLDOWN
  334. * @param mode GPIO_MODE_IT_RISING, GPIO_MODE_IT_FALLING, GPIO_MODE_IT_RISING_FALLING
  335. * @return true
  336. * @return false
  337. */
  338. } // namespace iflytop