drv_gpio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 Abbcc first version
  9. * 2022-07-15 Aligagago add apm32F4 serie MCU support
  10. */
  11. #include <board.h>
  12. #include "drv_gpio.h"
  13. #ifdef RT_USING_PIN
  14. #define PIN_NUM(port, no) (((((port) & 0xFu) << 4) | ((no) & 0xFu)))
  15. #define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
  16. #define PIN_NO(pin) ((uint8_t)((pin) & 0xFu))
  17. #define PIN_APMPORT(pin) ((GPIO_T *)(GPIOA_BASE + (0x400u * PIN_PORT(pin))))
  18. #define PIN_APMPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))
  19. #if defined(GPIOZ)
  20. #define __APM32_PORT_MAX 12u
  21. #elif defined(GPIOK)
  22. #define __APM32_PORT_MAX 11u
  23. #elif defined(GPIOJ)
  24. #define __APM32_PORT_MAX 10u
  25. #elif defined(GPIOI)
  26. #define __APM32_PORT_MAX 9u
  27. #elif defined(GPIOH)
  28. #define __APM32_PORT_MAX 8u
  29. #elif defined(GPIOG)
  30. #define __APM32_PORT_MAX 7u
  31. #elif defined(GPIOF)
  32. #define __APM32_PORT_MAX 6u
  33. #elif defined(GPIOE)
  34. #define __APM32_PORT_MAX 5u
  35. #elif defined(GPIOD)
  36. #define __APM32_PORT_MAX 4u
  37. #elif defined(GPIOC)
  38. #define __APM32_PORT_MAX 3u
  39. #elif defined(GPIOB)
  40. #define __APM32_PORT_MAX 2u
  41. #elif defined(GPIOA)
  42. #define __APM32_PORT_MAX 1u
  43. #else
  44. #define __APM32_PORT_MAX 0u
  45. #error Unsupported APM32 GPIO peripheral.
  46. #endif
  47. #define PIN_APMPORT_MAX __APM32_PORT_MAX
  48. static const struct pin_irq_map pin_irq_map[] =
  49. {
  50. {GPIO_PIN_0, EINT0_IRQn},
  51. {GPIO_PIN_1, EINT1_IRQn},
  52. {GPIO_PIN_2, EINT2_IRQn},
  53. {GPIO_PIN_3, EINT3_IRQn},
  54. {GPIO_PIN_4, EINT4_IRQn},
  55. {GPIO_PIN_5, EINT9_5_IRQn},
  56. {GPIO_PIN_6, EINT9_5_IRQn},
  57. {GPIO_PIN_7, EINT9_5_IRQn},
  58. {GPIO_PIN_8, EINT9_5_IRQn},
  59. {GPIO_PIN_9, EINT9_5_IRQn},
  60. {GPIO_PIN_10, EINT15_10_IRQn},
  61. {GPIO_PIN_11, EINT15_10_IRQn},
  62. {GPIO_PIN_12, EINT15_10_IRQn},
  63. {GPIO_PIN_13, EINT15_10_IRQn},
  64. {GPIO_PIN_14, EINT15_10_IRQn},
  65. {GPIO_PIN_15, EINT15_10_IRQn},
  66. };
  67. static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
  68. {
  69. {-1, 0, RT_NULL, RT_NULL},
  70. {-1, 0, RT_NULL, RT_NULL},
  71. {-1, 0, RT_NULL, RT_NULL},
  72. {-1, 0, RT_NULL, RT_NULL},
  73. {-1, 0, RT_NULL, RT_NULL},
  74. {-1, 0, RT_NULL, RT_NULL},
  75. {-1, 0, RT_NULL, RT_NULL},
  76. {-1, 0, RT_NULL, RT_NULL},
  77. {-1, 0, RT_NULL, RT_NULL},
  78. {-1, 0, RT_NULL, RT_NULL},
  79. {-1, 0, RT_NULL, RT_NULL},
  80. {-1, 0, RT_NULL, RT_NULL},
  81. {-1, 0, RT_NULL, RT_NULL},
  82. {-1, 0, RT_NULL, RT_NULL},
  83. {-1, 0, RT_NULL, RT_NULL},
  84. {-1, 0, RT_NULL, RT_NULL},
  85. };
  86. static uint32_t pin_irq_enable_mask = 0;
  87. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  88. static rt_base_t _pin_get(const char *name)
  89. {
  90. rt_base_t pin = 0;
  91. int hw_port_num, hw_pin_num = 0;
  92. int i, name_len;
  93. name_len = rt_strlen(name);
  94. if ((name_len < 4) || (name_len >= 6))
  95. {
  96. return -RT_EINVAL;
  97. }
  98. if ((name[0] != 'P') || (name[2] != '.'))
  99. {
  100. return -RT_EINVAL;
  101. }
  102. if ((name[1] >= 'A') && (name[1] <= 'Z'))
  103. {
  104. hw_port_num = (int)(name[1] - 'A');
  105. }
  106. else
  107. {
  108. return -RT_EINVAL;
  109. }
  110. for (i = 3; i < name_len; i++)
  111. {
  112. hw_pin_num *= 10;
  113. hw_pin_num += name[i] - '0';
  114. }
  115. pin = PIN_NUM(hw_port_num, hw_pin_num);
  116. return pin;
  117. }
  118. static void _pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  119. {
  120. GPIO_T *gpio_port;
  121. uint16_t gpio_pin;
  122. if (PIN_PORT(pin) < PIN_APMPORT_MAX)
  123. {
  124. gpio_port = PIN_APMPORT(pin);
  125. gpio_pin = PIN_APMPIN(pin);
  126. GPIO_WriteBitValue(gpio_port, gpio_pin, (uint8_t)value);
  127. }
  128. }
  129. static int _pin_read(rt_device_t dev, rt_base_t pin)
  130. {
  131. GPIO_T *gpio_port;
  132. uint16_t gpio_pin;
  133. int value = PIN_LOW;
  134. if (PIN_PORT(pin) < PIN_APMPORT_MAX)
  135. {
  136. gpio_port = PIN_APMPORT(pin);
  137. gpio_pin = PIN_APMPIN(pin);
  138. value = GPIO_ReadInputBit(gpio_port, gpio_pin);
  139. }
  140. return value;
  141. }
  142. static void _pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  143. {
  144. GPIO_Config_T gpioConfig;
  145. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  146. {
  147. return;
  148. }
  149. /* Configure gpioConfigure */
  150. #if defined(APM32F10X_HD)
  151. gpioConfig.pin = PIN_APMPIN(pin);
  152. gpioConfig.mode = GPIO_MODE_OUT_PP;
  153. gpioConfig.speed = GPIO_SPEED_50MHz;
  154. if (mode == PIN_MODE_OUTPUT)
  155. {
  156. /* output setting */
  157. gpioConfig.mode = GPIO_MODE_OUT_PP;
  158. }
  159. else if (mode == PIN_MODE_INPUT)
  160. {
  161. /* input setting: not pull. */
  162. gpioConfig.mode = GPIO_MODE_IN_PU;
  163. }
  164. else if (mode == PIN_MODE_INPUT_PULLUP)
  165. {
  166. /* input setting: pull up. */
  167. gpioConfig.mode = GPIO_MODE_IN_PU;
  168. }
  169. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  170. {
  171. /* input setting: pull down. */
  172. gpioConfig.mode = GPIO_MODE_IN_PD;
  173. }
  174. else if (mode == PIN_MODE_OUTPUT_OD)
  175. {
  176. /* output setting: od. */
  177. gpioConfig.mode = GPIO_MODE_OUT_OD;
  178. }
  179. #elif defined( APM32F40X)
  180. gpioConfig.pin = PIN_APMPIN(pin);
  181. gpioConfig.mode = GPIO_MODE_OUT;
  182. gpioConfig.otype = GPIO_OTYPE_PP;
  183. gpioConfig.speed = GPIO_SPEED_50MHz;
  184. if (mode == PIN_MODE_OUTPUT)
  185. {
  186. /* output setting */
  187. gpioConfig.mode = GPIO_MODE_OUT;
  188. gpioConfig.otype = GPIO_OTYPE_PP;
  189. }
  190. else if (mode == PIN_MODE_INPUT)
  191. {
  192. /* input setting: not pull. */
  193. gpioConfig.mode = GPIO_MODE_IN;
  194. gpioConfig.pupd = GPIO_PUPD_NOPULL;
  195. }
  196. else if (mode == PIN_MODE_INPUT_PULLUP)
  197. {
  198. /* input setting: pull up. */
  199. gpioConfig.mode = GPIO_MODE_IN;
  200. gpioConfig.pupd = GPIO_PUPD_UP;
  201. }
  202. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  203. {
  204. /* input setting: pull down. */
  205. gpioConfig.mode = GPIO_MODE_IN;
  206. gpioConfig.pupd = GPIO_PUPD_DOWN;
  207. }
  208. else if (mode == PIN_MODE_OUTPUT_OD)
  209. {
  210. /* output setting: od. */
  211. gpioConfig.mode = GPIO_MODE_OUT;
  212. gpioConfig.otype = GPIO_OTYPE_OD;
  213. }
  214. #endif
  215. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  216. }
  217. rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
  218. {
  219. int i;
  220. for (i = 0; i < 32; i++)
  221. {
  222. if ((0x01 << i) == bit)
  223. {
  224. return i;
  225. }
  226. }
  227. return -1;
  228. }
  229. rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
  230. {
  231. rt_int32_t mapindex = bit2bitno(pinbit);
  232. if (mapindex < 0 || mapindex >= ITEM_NUM(pin_irq_map))
  233. {
  234. return RT_NULL;
  235. }
  236. return &pin_irq_map[mapindex];
  237. };
  238. static rt_err_t _pin_attach_irq(struct rt_device *device, rt_int32_t pin,
  239. rt_uint32_t mode, void (*hdr)(void *args), void *args)
  240. {
  241. rt_base_t level;
  242. rt_int32_t irqindex = -1;
  243. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  244. {
  245. return -RT_ENOSYS;
  246. }
  247. irqindex = bit2bitno(PIN_APMPIN(pin));
  248. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  249. {
  250. return RT_ENOSYS;
  251. }
  252. level = rt_hw_interrupt_disable();
  253. if (pin_irq_hdr_tab[irqindex].pin == pin &&
  254. pin_irq_hdr_tab[irqindex].hdr == hdr &&
  255. pin_irq_hdr_tab[irqindex].mode == mode &&
  256. pin_irq_hdr_tab[irqindex].args == args)
  257. {
  258. rt_hw_interrupt_enable(level);
  259. return RT_EOK;
  260. }
  261. if (pin_irq_hdr_tab[irqindex].pin != -1)
  262. {
  263. rt_hw_interrupt_enable(level);
  264. return RT_EBUSY;
  265. }
  266. pin_irq_hdr_tab[irqindex].pin = pin;
  267. pin_irq_hdr_tab[irqindex].hdr = hdr;
  268. pin_irq_hdr_tab[irqindex].mode = mode;
  269. pin_irq_hdr_tab[irqindex].args = args;
  270. rt_hw_interrupt_enable(level);
  271. return RT_EOK;
  272. }
  273. static rt_err_t _pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
  274. {
  275. rt_base_t level;
  276. rt_int32_t irqindex = -1;
  277. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  278. {
  279. return -RT_ENOSYS;
  280. }
  281. irqindex = bit2bitno(PIN_APMPIN(pin));
  282. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  283. {
  284. return RT_ENOSYS;
  285. }
  286. level = rt_hw_interrupt_disable();
  287. if (pin_irq_hdr_tab[irqindex].pin == -1)
  288. {
  289. rt_hw_interrupt_enable(level);
  290. return RT_EOK;
  291. }
  292. pin_irq_hdr_tab[irqindex].pin = -1;
  293. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  294. pin_irq_hdr_tab[irqindex].mode = 0;
  295. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  296. rt_hw_interrupt_enable(level);
  297. return RT_EOK;
  298. }
  299. static rt_err_t _pin_irq_enable(struct rt_device *device, rt_base_t pin,
  300. rt_uint32_t enabled)
  301. {
  302. const struct pin_irq_map *irqmap;
  303. rt_base_t level;
  304. rt_int32_t irqindex = -1;
  305. GPIO_Config_T gpioConfig;
  306. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  307. {
  308. return -RT_ENOSYS;
  309. }
  310. if (enabled == PIN_IRQ_ENABLE)
  311. {
  312. irqindex = bit2bitno(PIN_APMPIN(pin));
  313. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  314. {
  315. return RT_ENOSYS;
  316. }
  317. level = rt_hw_interrupt_disable();
  318. if (pin_irq_hdr_tab[irqindex].pin == -1)
  319. {
  320. rt_hw_interrupt_enable(level);
  321. return RT_ENOSYS;
  322. }
  323. irqmap = &pin_irq_map[irqindex];
  324. /* Configure gpioConfigure */
  325. gpioConfig.pin = PIN_APMPIN(pin);
  326. gpioConfig.speed = GPIO_SPEED_50MHz;
  327. switch (pin_irq_hdr_tab[irqindex].mode)
  328. {
  329. #if defined(APM32F10X_HD)
  330. case PIN_IRQ_MODE_RISING:
  331. gpioConfig.mode = GPIO_MODE_IN_PD;
  332. break;
  333. case PIN_IRQ_MODE_FALLING:
  334. gpioConfig.mode = GPIO_MODE_IN_PU;
  335. break;
  336. case PIN_IRQ_MODE_RISING_FALLING:
  337. gpioConfig.mode = GPIO_MODE_IN_FLOATING;
  338. break;
  339. #elif defined( APM32F40X)
  340. case PIN_IRQ_MODE_RISING:
  341. gpioConfig.mode = GPIO_MODE_IN;
  342. gpioConfig.pupd = GPIO_PUPD_DOWN;
  343. break;
  344. case PIN_IRQ_MODE_FALLING:
  345. gpioConfig.mode = GPIO_MODE_IN;
  346. gpioConfig.pupd = GPIO_PUPD_UP;
  347. break;
  348. case PIN_IRQ_MODE_RISING_FALLING:
  349. gpioConfig.mode = GPIO_MODE_IN;
  350. gpioConfig.pupd = GPIO_PUPD_NOPULL;
  351. break;
  352. #endif
  353. }
  354. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  355. NVIC_EnableIRQRequest(irqmap->irqno, 5, 0);
  356. pin_irq_enable_mask |= irqmap->pinbit;
  357. rt_hw_interrupt_enable(level);
  358. }
  359. else if (enabled == PIN_IRQ_DISABLE)
  360. {
  361. irqmap = get_pin_irq_map(PIN_APMPIN(pin));
  362. if (irqmap == RT_NULL)
  363. {
  364. return RT_ENOSYS;
  365. }
  366. level = rt_hw_interrupt_disable();
  367. pin_irq_enable_mask &= ~irqmap->pinbit;
  368. if ((irqmap->pinbit >= GPIO_PIN_5) && (irqmap->pinbit <= GPIO_PIN_9))
  369. {
  370. if (!(pin_irq_enable_mask & (GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9)))
  371. {
  372. NVIC_DisableIRQRequest(irqmap->irqno);
  373. }
  374. }
  375. else if ((irqmap->pinbit >= GPIO_PIN_10) && (irqmap->pinbit <= GPIO_PIN_15))
  376. {
  377. if (!(pin_irq_enable_mask & (GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)))
  378. {
  379. NVIC_DisableIRQRequest(irqmap->irqno);
  380. }
  381. }
  382. else
  383. {
  384. NVIC_DisableIRQRequest(irqmap->irqno);
  385. }
  386. rt_hw_interrupt_enable(level);
  387. }
  388. else
  389. {
  390. return -RT_ENOSYS;
  391. }
  392. return RT_EOK;
  393. }
  394. const static struct rt_pin_ops _apm32_pin_ops =
  395. {
  396. _pin_mode,
  397. _pin_write,
  398. _pin_read,
  399. _pin_attach_irq,
  400. _pin_dettach_irq,
  401. _pin_irq_enable,
  402. _pin_get,
  403. };
  404. rt_inline void pin_irq_hdr(int irqno)
  405. {
  406. if (pin_irq_hdr_tab[irqno].hdr)
  407. {
  408. pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
  409. }
  410. }
  411. void GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  412. {
  413. pin_irq_hdr(bit2bitno(GPIO_Pin));
  414. }
  415. void EINT0_IRQHandler(void)
  416. {
  417. rt_interrupt_enter();
  418. if (EINT_ReadIntFlag(EINT_LINE_0))
  419. {
  420. EINT_ClearIntFlag(EINT_LINE_0);
  421. GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  422. }
  423. rt_interrupt_leave();
  424. }
  425. void EINT1_IRQHandler(void)
  426. {
  427. rt_interrupt_enter();
  428. if (EINT_ReadIntFlag(EINT_LINE_1))
  429. {
  430. EINT_ClearIntFlag(EINT_LINE_1);
  431. GPIO_EXTI_IRQHandler(GPIO_PIN_1);
  432. }
  433. rt_interrupt_leave();
  434. }
  435. void EINT2_IRQHandler(void)
  436. {
  437. rt_interrupt_enter();
  438. if (EINT_ReadIntFlag(EINT_LINE_2))
  439. {
  440. EINT_ClearIntFlag(EINT_LINE_2);
  441. GPIO_EXTI_IRQHandler(GPIO_PIN_2);
  442. }
  443. rt_interrupt_leave();
  444. }
  445. void EINT3_IRQHandler(void)
  446. {
  447. rt_interrupt_enter();
  448. if (EINT_ReadIntFlag(EINT_LINE_3))
  449. {
  450. EINT_ClearIntFlag(EINT_LINE_3);
  451. GPIO_EXTI_IRQHandler(GPIO_PIN_3);
  452. }
  453. rt_interrupt_leave();
  454. }
  455. void EINT4_IRQHandler(void)
  456. {
  457. rt_interrupt_enter();
  458. if (EINT_ReadIntFlag(EINT_LINE_4))
  459. {
  460. EINT_ClearIntFlag(EINT_LINE_4);
  461. GPIO_EXTI_IRQHandler(GPIO_PIN_4);
  462. }
  463. rt_interrupt_leave();
  464. }
  465. void EINT9_5_IRQHandler(void)
  466. {
  467. rt_interrupt_enter();
  468. if (EINT_ReadIntFlag(EINT_LINE_5))
  469. {
  470. EINT_ClearIntFlag(EINT_LINE_5);
  471. GPIO_EXTI_IRQHandler(GPIO_PIN_5);
  472. }
  473. if (EINT_ReadIntFlag(EINT_LINE_6))
  474. {
  475. EINT_ClearIntFlag(EINT_LINE_6);
  476. GPIO_EXTI_IRQHandler(GPIO_PIN_6);
  477. }
  478. if (EINT_ReadIntFlag(EINT_LINE_7))
  479. {
  480. EINT_ClearIntFlag(EINT_LINE_7);
  481. GPIO_EXTI_IRQHandler(GPIO_PIN_7);
  482. }
  483. if (EINT_ReadIntFlag(EINT_LINE_8))
  484. {
  485. EINT_ClearIntFlag(EINT_LINE_8);
  486. GPIO_EXTI_IRQHandler(GPIO_PIN_8);
  487. }
  488. if (EINT_ReadIntFlag(EINT_LINE_9))
  489. {
  490. EINT_ClearIntFlag(EINT_LINE_9);
  491. GPIO_EXTI_IRQHandler(GPIO_PIN_9);
  492. }
  493. rt_interrupt_leave();
  494. }
  495. void EINT15_10_IRQHandler(void)
  496. {
  497. rt_interrupt_enter();
  498. if (EINT_ReadIntFlag(EINT_LINE_10))
  499. {
  500. EINT_ClearIntFlag(EINT_LINE_10);
  501. GPIO_EXTI_IRQHandler(GPIO_PIN_10);
  502. }
  503. if (EINT_ReadIntFlag(EINT_LINE_11))
  504. {
  505. EINT_ClearIntFlag(EINT_LINE_11);
  506. GPIO_EXTI_IRQHandler(GPIO_PIN_11);
  507. }
  508. if (EINT_ReadIntFlag(EINT_LINE_12))
  509. {
  510. EINT_ClearIntFlag(EINT_LINE_12);
  511. GPIO_EXTI_IRQHandler(GPIO_PIN_12);
  512. }
  513. if (EINT_ReadIntFlag(EINT_LINE_13))
  514. {
  515. EINT_ClearIntFlag(EINT_LINE_13);
  516. GPIO_EXTI_IRQHandler(GPIO_PIN_13);
  517. }
  518. if (EINT_ReadIntFlag(EINT_LINE_14))
  519. {
  520. EINT_ClearIntFlag(EINT_LINE_14);
  521. GPIO_EXTI_IRQHandler(GPIO_PIN_14);
  522. }
  523. if (EINT_ReadIntFlag(EINT_LINE_15))
  524. {
  525. EINT_ClearIntFlag(EINT_LINE_15);
  526. GPIO_EXTI_IRQHandler(GPIO_PIN_15);
  527. }
  528. rt_interrupt_leave();
  529. }
  530. int rt_hw_pin_init(void)
  531. {
  532. #if defined(APM32F10X_HD)
  533. #ifdef GPIOA
  534. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
  535. #endif
  536. #ifdef GPIOB
  537. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  538. #endif
  539. #ifdef GPIOC
  540. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
  541. #endif
  542. #ifdef GPIOD
  543. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOD);
  544. #endif
  545. #ifdef GPIOE
  546. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);
  547. #endif
  548. #ifdef GPIOF
  549. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOF);
  550. #endif
  551. #ifdef GPIOG
  552. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOG);
  553. #endif
  554. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  555. #elif defined(APM32F40X)
  556. #ifdef GPIOA
  557. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
  558. #endif
  559. #ifdef GPIOB
  560. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOB);
  561. #endif
  562. #ifdef GPIOC
  563. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOC);
  564. #endif
  565. #ifdef GPIOD
  566. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOD);
  567. #endif
  568. #ifdef GPIOE
  569. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOE);
  570. #endif
  571. #ifdef GPIOF
  572. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOF);
  573. #endif
  574. #ifdef GPIOG
  575. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOG);
  576. #endif
  577. #ifdef GPIOH
  578. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOH);
  579. #endif
  580. #ifdef GPIOI
  581. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOI);
  582. #endif
  583. #ifdef GPIOJ
  584. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOJ);
  585. #endif
  586. #ifdef GPIOK
  587. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOK);
  588. #endif
  589. #endif
  590. return rt_device_pin_register("pin", &_apm32_pin_ops, RT_NULL);
  591. }
  592. #endif /* RT_USING_PIN */