drv_gpio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * Copyright (c) 2006-2023, 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 series MCU support
  10. * 2022-12-26 luobeihai add apm32F0 series MCU support
  11. * 2022-03-18 luobeihai fix warning about incompatible function pointer types
  12. * 2023-03-27 luobeihai add APM32E1/S1 series MCU support
  13. */
  14. #include <board.h>
  15. #include "drv_gpio.h"
  16. #ifdef RT_USING_PIN
  17. #define PIN_NUM(port, no) (((((port) & 0xFu) << 4) | ((no) & 0xFu)))
  18. #define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
  19. #define PIN_NO(pin) ((uint8_t)((pin) & 0xFu))
  20. #define PIN_APMPORT(pin) ((GPIO_T *)(GPIOA_BASE + (0x400u * PIN_PORT(pin))))
  21. #define PIN_APMPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))
  22. #if defined(GPIOZ)
  23. #define __APM32_PORT_MAX 12u
  24. #elif defined(GPIOK)
  25. #define __APM32_PORT_MAX 11u
  26. #elif defined(GPIOJ)
  27. #define __APM32_PORT_MAX 10u
  28. #elif defined(GPIOI)
  29. #define __APM32_PORT_MAX 9u
  30. #elif defined(GPIOH)
  31. #define __APM32_PORT_MAX 8u
  32. #elif defined(GPIOG)
  33. #define __APM32_PORT_MAX 7u
  34. #elif defined(GPIOF)
  35. #define __APM32_PORT_MAX 6u
  36. #elif defined(GPIOE)
  37. #define __APM32_PORT_MAX 5u
  38. #elif defined(GPIOD)
  39. #define __APM32_PORT_MAX 4u
  40. #elif defined(GPIOC)
  41. #define __APM32_PORT_MAX 3u
  42. #elif defined(GPIOB)
  43. #define __APM32_PORT_MAX 2u
  44. #elif defined(GPIOA)
  45. #define __APM32_PORT_MAX 1u
  46. #else
  47. #define __APM32_PORT_MAX 0u
  48. #error Unsupported APM32 GPIO peripheral.
  49. #endif
  50. #define PIN_APMPORT_MAX __APM32_PORT_MAX
  51. static const struct pin_irq_map pin_irq_map[] =
  52. {
  53. #if defined(SOC_SERIES_APM32F0)
  54. {GPIO_PIN_0, EINT0_1_IRQn},
  55. {GPIO_PIN_1, EINT0_1_IRQn},
  56. {GPIO_PIN_2, EINT2_3_IRQn},
  57. {GPIO_PIN_3, EINT2_3_IRQn},
  58. {GPIO_PIN_4, EINT4_15_IRQn},
  59. {GPIO_PIN_5, EINT4_15_IRQn},
  60. {GPIO_PIN_6, EINT4_15_IRQn},
  61. {GPIO_PIN_7, EINT4_15_IRQn},
  62. {GPIO_PIN_8, EINT4_15_IRQn},
  63. {GPIO_PIN_9, EINT4_15_IRQn},
  64. {GPIO_PIN_10, EINT4_15_IRQn},
  65. {GPIO_PIN_11, EINT4_15_IRQn},
  66. {GPIO_PIN_12, EINT4_15_IRQn},
  67. {GPIO_PIN_13, EINT4_15_IRQn},
  68. {GPIO_PIN_14, EINT4_15_IRQn},
  69. {GPIO_PIN_15, EINT4_15_IRQn},
  70. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  71. || defined(SOC_SERIES_APM32F4)
  72. {GPIO_PIN_0, EINT0_IRQn},
  73. {GPIO_PIN_1, EINT1_IRQn},
  74. {GPIO_PIN_2, EINT2_IRQn},
  75. {GPIO_PIN_3, EINT3_IRQn},
  76. {GPIO_PIN_4, EINT4_IRQn},
  77. {GPIO_PIN_5, EINT9_5_IRQn},
  78. {GPIO_PIN_6, EINT9_5_IRQn},
  79. {GPIO_PIN_7, EINT9_5_IRQn},
  80. {GPIO_PIN_8, EINT9_5_IRQn},
  81. {GPIO_PIN_9, EINT9_5_IRQn},
  82. {GPIO_PIN_10, EINT15_10_IRQn},
  83. {GPIO_PIN_11, EINT15_10_IRQn},
  84. {GPIO_PIN_12, EINT15_10_IRQn},
  85. {GPIO_PIN_13, EINT15_10_IRQn},
  86. {GPIO_PIN_14, EINT15_10_IRQn},
  87. {GPIO_PIN_15, EINT15_10_IRQn},
  88. #endif /* SOC_SERIES_APM32F0 */
  89. };
  90. static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
  91. {
  92. {-1, 0, RT_NULL, RT_NULL},
  93. {-1, 0, RT_NULL, RT_NULL},
  94. {-1, 0, RT_NULL, RT_NULL},
  95. {-1, 0, RT_NULL, RT_NULL},
  96. {-1, 0, RT_NULL, RT_NULL},
  97. {-1, 0, RT_NULL, RT_NULL},
  98. {-1, 0, RT_NULL, RT_NULL},
  99. {-1, 0, RT_NULL, RT_NULL},
  100. {-1, 0, RT_NULL, RT_NULL},
  101. {-1, 0, RT_NULL, RT_NULL},
  102. {-1, 0, RT_NULL, RT_NULL},
  103. {-1, 0, RT_NULL, RT_NULL},
  104. {-1, 0, RT_NULL, RT_NULL},
  105. {-1, 0, RT_NULL, RT_NULL},
  106. {-1, 0, RT_NULL, RT_NULL},
  107. {-1, 0, RT_NULL, RT_NULL},
  108. };
  109. static uint32_t pin_irq_enable_mask = 0;
  110. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  111. static rt_base_t apm32_pin_get(const char *name)
  112. {
  113. rt_base_t pin = 0;
  114. int hw_port_num, hw_pin_num = 0;
  115. int i, name_len;
  116. name_len = rt_strlen(name);
  117. if ((name_len < 4) || (name_len >= 6))
  118. {
  119. return -RT_EINVAL;
  120. }
  121. if ((name[0] != 'P') || (name[2] != '.'))
  122. {
  123. return -RT_EINVAL;
  124. }
  125. if ((name[1] >= 'A') && (name[1] <= 'Z'))
  126. {
  127. hw_port_num = (int)(name[1] - 'A');
  128. }
  129. else
  130. {
  131. return -RT_EINVAL;
  132. }
  133. for (i = 3; i < name_len; i++)
  134. {
  135. hw_pin_num *= 10;
  136. hw_pin_num += name[i] - '0';
  137. }
  138. pin = PIN_NUM(hw_port_num, hw_pin_num);
  139. return pin;
  140. }
  141. static void apm32_pin_write(rt_device_t dev, rt_base_t pin, rt_uint8_t value)
  142. {
  143. GPIO_T *gpio_port;
  144. uint16_t gpio_pin;
  145. if (PIN_PORT(pin) < PIN_APMPORT_MAX)
  146. {
  147. gpio_port = PIN_APMPORT(pin);
  148. gpio_pin = PIN_APMPIN(pin);
  149. #if defined(SOC_SERIES_APM32F0)
  150. GPIO_WriteBitValue(gpio_port, gpio_pin, (GPIO_BSRET_T)value);
  151. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  152. || defined(SOC_SERIES_APM32F4)
  153. GPIO_WriteBitValue(gpio_port, gpio_pin, (uint8_t)value);
  154. #endif
  155. }
  156. }
  157. static rt_ssize_t apm32_pin_read(rt_device_t dev, rt_base_t pin)
  158. {
  159. GPIO_T *gpio_port;
  160. uint16_t gpio_pin;
  161. int value = PIN_LOW;
  162. if (PIN_PORT(pin) < PIN_APMPORT_MAX)
  163. {
  164. gpio_port = PIN_APMPORT(pin);
  165. gpio_pin = PIN_APMPIN(pin);
  166. value = GPIO_ReadInputBit(gpio_port, gpio_pin);
  167. }
  168. return value;
  169. }
  170. static void apm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
  171. {
  172. GPIO_Config_T gpioConfig;
  173. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  174. {
  175. return;
  176. }
  177. /* Configure gpioConfigure */
  178. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  179. gpioConfig.pin = PIN_APMPIN(pin);
  180. gpioConfig.mode = GPIO_MODE_OUT_PP;
  181. gpioConfig.speed = GPIO_SPEED_50MHz;
  182. if (mode == PIN_MODE_OUTPUT)
  183. {
  184. /* output setting */
  185. gpioConfig.mode = GPIO_MODE_OUT_PP;
  186. }
  187. else if (mode == PIN_MODE_INPUT)
  188. {
  189. /* input setting: not pull. */
  190. gpioConfig.mode = GPIO_MODE_IN_PU;
  191. }
  192. else if (mode == PIN_MODE_INPUT_PULLUP)
  193. {
  194. /* input setting: pull up. */
  195. gpioConfig.mode = GPIO_MODE_IN_PU;
  196. }
  197. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  198. {
  199. /* input setting: pull down. */
  200. gpioConfig.mode = GPIO_MODE_IN_PD;
  201. }
  202. else if (mode == PIN_MODE_OUTPUT_OD)
  203. {
  204. /* output setting: od. */
  205. gpioConfig.mode = GPIO_MODE_OUT_OD;
  206. }
  207. #elif defined(SOC_SERIES_APM32F4)
  208. gpioConfig.pin = PIN_APMPIN(pin);
  209. gpioConfig.mode = GPIO_MODE_OUT;
  210. gpioConfig.otype = GPIO_OTYPE_PP;
  211. gpioConfig.speed = GPIO_SPEED_50MHz;
  212. if (mode == PIN_MODE_OUTPUT)
  213. {
  214. /* output setting */
  215. gpioConfig.mode = GPIO_MODE_OUT;
  216. gpioConfig.otype = GPIO_OTYPE_PP;
  217. }
  218. else if (mode == PIN_MODE_INPUT)
  219. {
  220. /* input setting: not pull. */
  221. gpioConfig.mode = GPIO_MODE_IN;
  222. gpioConfig.pupd = GPIO_PUPD_NOPULL;
  223. }
  224. else if (mode == PIN_MODE_INPUT_PULLUP)
  225. {
  226. /* input setting: pull up. */
  227. gpioConfig.mode = GPIO_MODE_IN;
  228. gpioConfig.pupd = GPIO_PUPD_UP;
  229. }
  230. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  231. {
  232. /* input setting: pull down. */
  233. gpioConfig.mode = GPIO_MODE_IN;
  234. gpioConfig.pupd = GPIO_PUPD_DOWN;
  235. }
  236. else if (mode == PIN_MODE_OUTPUT_OD)
  237. {
  238. /* output setting: od. */
  239. gpioConfig.mode = GPIO_MODE_OUT;
  240. gpioConfig.otype = GPIO_OTYPE_OD;
  241. }
  242. #elif defined(SOC_SERIES_APM32F0)
  243. gpioConfig.pin = PIN_APMPIN(pin);
  244. gpioConfig.mode = GPIO_MODE_OUT;
  245. gpioConfig.outtype = GPIO_OUT_TYPE_PP;
  246. gpioConfig.pupd = GPIO_PUPD_NO;
  247. gpioConfig.speed = GPIO_SPEED_50MHz;
  248. if (mode == PIN_MODE_OUTPUT)
  249. {
  250. /* output setting */
  251. gpioConfig.mode = GPIO_MODE_OUT;
  252. gpioConfig.outtype = GPIO_OUT_TYPE_PP;
  253. }
  254. else if (mode == PIN_MODE_INPUT)
  255. {
  256. /* input setting: not pull. */
  257. gpioConfig.mode = GPIO_MODE_IN;
  258. gpioConfig.pupd = GPIO_PUPD_NO;
  259. }
  260. else if (mode == PIN_MODE_INPUT_PULLUP)
  261. {
  262. /* input setting: pull up. */
  263. gpioConfig.mode = GPIO_MODE_IN;
  264. gpioConfig.pupd = GPIO_PUPD_PU;
  265. }
  266. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  267. {
  268. /* input setting: pull down. */
  269. gpioConfig.mode = GPIO_MODE_IN;
  270. gpioConfig.pupd = GPIO_PUPD_PD;
  271. }
  272. else if (mode == PIN_MODE_OUTPUT_OD)
  273. {
  274. /* output setting: od. */
  275. gpioConfig.mode = GPIO_MODE_OUT;
  276. gpioConfig.outtype = GPIO_OUT_TYPE_OD;
  277. }
  278. #endif
  279. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  280. }
  281. rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
  282. {
  283. int i;
  284. for (i = 0; i < 32; i++)
  285. {
  286. if ((0x01 << i) == bit)
  287. {
  288. return i;
  289. }
  290. }
  291. return -1;
  292. }
  293. rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
  294. {
  295. rt_int32_t mapindex = bit2bitno(pinbit);
  296. if (mapindex < 0 || mapindex >= ITEM_NUM(pin_irq_map))
  297. {
  298. return RT_NULL;
  299. }
  300. return &pin_irq_map[mapindex];
  301. };
  302. static rt_err_t apm32_pin_attach_irq(struct rt_device *device, rt_base_t pin,
  303. rt_uint8_t mode, void (*hdr)(void *args), void *args)
  304. {
  305. rt_base_t level;
  306. rt_int32_t irqindex = -1;
  307. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  308. {
  309. return -RT_ENOSYS;
  310. }
  311. irqindex = bit2bitno(PIN_APMPIN(pin));
  312. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  313. {
  314. return -RT_ENOSYS;
  315. }
  316. level = rt_hw_interrupt_disable();
  317. if (pin_irq_hdr_tab[irqindex].pin == pin &&
  318. pin_irq_hdr_tab[irqindex].hdr == hdr &&
  319. pin_irq_hdr_tab[irqindex].mode == mode &&
  320. pin_irq_hdr_tab[irqindex].args == args)
  321. {
  322. rt_hw_interrupt_enable(level);
  323. return RT_EOK;
  324. }
  325. if (pin_irq_hdr_tab[irqindex].pin != -1)
  326. {
  327. rt_hw_interrupt_enable(level);
  328. return -RT_EBUSY;
  329. }
  330. pin_irq_hdr_tab[irqindex].pin = pin;
  331. pin_irq_hdr_tab[irqindex].hdr = hdr;
  332. pin_irq_hdr_tab[irqindex].mode = mode;
  333. pin_irq_hdr_tab[irqindex].args = args;
  334. rt_hw_interrupt_enable(level);
  335. return RT_EOK;
  336. }
  337. static rt_err_t apm32_pin_dettach_irq(struct rt_device *device, rt_base_t pin)
  338. {
  339. rt_base_t level;
  340. rt_int32_t irqindex = -1;
  341. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  342. {
  343. return -RT_ENOSYS;
  344. }
  345. irqindex = bit2bitno(PIN_APMPIN(pin));
  346. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  347. {
  348. return -RT_ENOSYS;
  349. }
  350. level = rt_hw_interrupt_disable();
  351. if (pin_irq_hdr_tab[irqindex].pin == -1)
  352. {
  353. rt_hw_interrupt_enable(level);
  354. return RT_EOK;
  355. }
  356. pin_irq_hdr_tab[irqindex].pin = -1;
  357. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  358. pin_irq_hdr_tab[irqindex].mode = 0;
  359. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  360. rt_hw_interrupt_enable(level);
  361. return RT_EOK;
  362. }
  363. static rt_err_t apm32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
  364. rt_uint8_t enabled)
  365. {
  366. const struct pin_irq_map *irqmap;
  367. rt_base_t level;
  368. rt_int32_t irqindex = -1;
  369. GPIO_Config_T gpioConfig;
  370. EINT_Config_T eintConfig;
  371. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  372. {
  373. return -RT_ENOSYS;
  374. }
  375. if (enabled == PIN_IRQ_ENABLE)
  376. {
  377. irqindex = bit2bitno(PIN_APMPIN(pin));
  378. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  379. {
  380. return -RT_ENOSYS;
  381. }
  382. level = rt_hw_interrupt_disable();
  383. if (pin_irq_hdr_tab[irqindex].pin == -1)
  384. {
  385. rt_hw_interrupt_enable(level);
  386. return -RT_ENOSYS;
  387. }
  388. irqmap = &pin_irq_map[irqindex];
  389. /* Configure gpioConfigure */
  390. gpioConfig.pin = PIN_APMPIN(pin);
  391. gpioConfig.speed = GPIO_SPEED_50MHz;
  392. switch (pin_irq_hdr_tab[irqindex].mode)
  393. {
  394. #if defined(SOC_SERIES_APM32F0)
  395. case PIN_IRQ_MODE_RISING:
  396. gpioConfig.mode = GPIO_MODE_IN;
  397. gpioConfig.pupd = GPIO_PUPD_PD;
  398. eintConfig.trigger = EINT_TRIGGER_RISING;
  399. break;
  400. case PIN_IRQ_MODE_FALLING:
  401. gpioConfig.mode = GPIO_MODE_IN;
  402. gpioConfig.pupd = GPIO_PUPD_PU;
  403. eintConfig.trigger = EINT_TRIGGER_FALLING;
  404. break;
  405. case PIN_IRQ_MODE_RISING_FALLING:
  406. gpioConfig.mode = GPIO_MODE_IN;
  407. gpioConfig.pupd = GPIO_PUPD_NO;
  408. eintConfig.trigger = EINT_TRIGGER_ALL;
  409. break;
  410. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  411. case PIN_IRQ_MODE_RISING:
  412. gpioConfig.mode = GPIO_MODE_IN_PD;
  413. eintConfig.trigger = EINT_TRIGGER_RISING;
  414. break;
  415. case PIN_IRQ_MODE_FALLING:
  416. gpioConfig.mode = GPIO_MODE_IN_PU;
  417. eintConfig.trigger = EINT_TRIGGER_FALLING;
  418. break;
  419. case PIN_IRQ_MODE_RISING_FALLING:
  420. gpioConfig.mode = GPIO_MODE_IN_FLOATING;
  421. eintConfig.trigger = EINT_TRIGGER_RISING_FALLING;
  422. break;
  423. #elif defined(SOC_SERIES_APM32F4)
  424. case PIN_IRQ_MODE_RISING:
  425. gpioConfig.mode = GPIO_MODE_IN;
  426. gpioConfig.pupd = GPIO_PUPD_DOWN;
  427. eintConfig.trigger = EINT_TRIGGER_RISING;
  428. break;
  429. case PIN_IRQ_MODE_FALLING:
  430. gpioConfig.mode = GPIO_MODE_IN;
  431. gpioConfig.pupd = GPIO_PUPD_UP;
  432. eintConfig.trigger = EINT_TRIGGER_FALLING;
  433. break;
  434. case PIN_IRQ_MODE_RISING_FALLING:
  435. gpioConfig.mode = GPIO_MODE_IN;
  436. gpioConfig.pupd = GPIO_PUPD_NOPULL;
  437. eintConfig.trigger = EINT_TRIGGER_RISING_FALLING;
  438. break;
  439. #endif
  440. }
  441. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  442. #if defined(SOC_SERIES_APM32F0)
  443. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
  444. SYSCFG_EINTLine((SYSCFG_PORT_T)(((pin) >> 4) & 0xFu), (SYSCFG_PIN_T)irqindex);
  445. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  446. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  447. GPIO_ConfigEINTLine((GPIO_PORT_SOURCE_T)(((pin) >> 4) & 0xFu), (GPIO_PIN_SOURCE_T)irqindex);
  448. #elif defined(SOC_SERIES_APM32F4)
  449. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
  450. SYSCFG_ConfigEINTLine((SYSCFG_PORT_T)(((pin) >> 4) & 0xFu), (SYSCFG_PIN_T)irqindex);
  451. #endif
  452. eintConfig.line = (EINT_LINE_T)(1u << PIN_NO(pin));
  453. eintConfig.mode = EINT_MODE_INTERRUPT;
  454. eintConfig.lineCmd = ENABLE;
  455. EINT_Config(&eintConfig);
  456. #if defined(SOC_SERIES_APM32F0)
  457. NVIC_EnableIRQRequest(irqmap->irqno, 5);
  458. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  459. || defined(SOC_SERIES_APM32F4)
  460. NVIC_EnableIRQRequest(irqmap->irqno, 5, 0);
  461. #endif
  462. pin_irq_enable_mask |= irqmap->pinbit;
  463. rt_hw_interrupt_enable(level);
  464. }
  465. else if (enabled == PIN_IRQ_DISABLE)
  466. {
  467. irqmap = get_pin_irq_map(PIN_APMPIN(pin));
  468. if (irqmap == RT_NULL)
  469. {
  470. return -RT_ENOSYS;
  471. }
  472. level = rt_hw_interrupt_disable();
  473. pin_irq_enable_mask &= ~irqmap->pinbit;
  474. #if defined(SOC_SERIES_APM32F0)
  475. if ((irqmap->pinbit >= GPIO_PIN_0) && (irqmap->pinbit <= GPIO_PIN_1))
  476. {
  477. if (!(pin_irq_enable_mask & (GPIO_PIN_0 | GPIO_PIN_1)))
  478. {
  479. NVIC_DisableIRQRequest(irqmap->irqno);
  480. }
  481. }
  482. else if ((irqmap->pinbit >= GPIO_PIN_2) && (irqmap->pinbit <= GPIO_PIN_3))
  483. {
  484. if (!(pin_irq_enable_mask & (GPIO_PIN_2 | GPIO_PIN_3)))
  485. {
  486. NVIC_DisableIRQRequest(irqmap->irqno);
  487. }
  488. }
  489. else if ((irqmap->pinbit >= GPIO_PIN_4) && (irqmap->pinbit <= GPIO_PIN_15))
  490. {
  491. if (!(pin_irq_enable_mask & (GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 |
  492. GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)))
  493. {
  494. NVIC_DisableIRQRequest(irqmap->irqno);
  495. }
  496. }
  497. else
  498. {
  499. NVIC_DisableIRQRequest(irqmap->irqno);
  500. }
  501. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  502. || defined(SOC_SERIES_APM32F4)
  503. if ((irqmap->pinbit >= GPIO_PIN_5) && (irqmap->pinbit <= GPIO_PIN_9))
  504. {
  505. if (!(pin_irq_enable_mask & (GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9)))
  506. {
  507. NVIC_DisableIRQRequest(irqmap->irqno);
  508. }
  509. }
  510. else if ((irqmap->pinbit >= GPIO_PIN_10) && (irqmap->pinbit <= GPIO_PIN_15))
  511. {
  512. if (!(pin_irq_enable_mask & (GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)))
  513. {
  514. NVIC_DisableIRQRequest(irqmap->irqno);
  515. }
  516. }
  517. else
  518. {
  519. NVIC_DisableIRQRequest(irqmap->irqno);
  520. }
  521. #endif /* SOC_SERIES_APM32F0 */
  522. rt_hw_interrupt_enable(level);
  523. }
  524. else
  525. {
  526. return -RT_ENOSYS;
  527. }
  528. return RT_EOK;
  529. }
  530. const static struct rt_pin_ops apm32_pin_ops =
  531. {
  532. apm32_pin_mode,
  533. apm32_pin_write,
  534. apm32_pin_read,
  535. apm32_pin_attach_irq,
  536. apm32_pin_dettach_irq,
  537. apm32_pin_irq_enable,
  538. apm32_pin_get,
  539. };
  540. rt_inline void pin_irq_hdr(int irqno)
  541. {
  542. if (pin_irq_hdr_tab[irqno].hdr)
  543. {
  544. pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
  545. }
  546. }
  547. void GPIO_EXTI_IRQHandler(uint8_t exti_line)
  548. {
  549. #if defined(SOC_SERIES_APM32F0)
  550. if (EINT_ReadIntFlag(1U << exti_line) != RESET)
  551. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  552. || defined(SOC_SERIES_APM32F4)
  553. if (EINT_ReadIntFlag((EINT_LINE_T)(1U << exti_line)) != RESET)
  554. #endif
  555. {
  556. EINT_ClearIntFlag(1U << exti_line);
  557. pin_irq_hdr(exti_line);
  558. }
  559. }
  560. #if defined(SOC_SERIES_APM32F0)
  561. void EINT0_1_IRQHandler(void)
  562. {
  563. rt_interrupt_enter();
  564. GPIO_EXTI_IRQHandler(0);
  565. GPIO_EXTI_IRQHandler(1);
  566. rt_interrupt_leave();
  567. }
  568. void EINT2_3_IRQHandler(void)
  569. {
  570. rt_interrupt_enter();
  571. GPIO_EXTI_IRQHandler(2);
  572. GPIO_EXTI_IRQHandler(3);
  573. rt_interrupt_leave();
  574. }
  575. void EINT4_15_IRQHandler(void)
  576. {
  577. rt_interrupt_enter();
  578. GPIO_EXTI_IRQHandler(4);
  579. GPIO_EXTI_IRQHandler(5);
  580. GPIO_EXTI_IRQHandler(6);
  581. GPIO_EXTI_IRQHandler(7);
  582. GPIO_EXTI_IRQHandler(8);
  583. GPIO_EXTI_IRQHandler(9);
  584. GPIO_EXTI_IRQHandler(10);
  585. GPIO_EXTI_IRQHandler(11);
  586. GPIO_EXTI_IRQHandler(12);
  587. GPIO_EXTI_IRQHandler(13);
  588. GPIO_EXTI_IRQHandler(14);
  589. GPIO_EXTI_IRQHandler(15);
  590. rt_interrupt_leave();
  591. }
  592. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  593. || defined(SOC_SERIES_APM32F4)
  594. void EINT0_IRQHandler(void)
  595. {
  596. rt_interrupt_enter();
  597. GPIO_EXTI_IRQHandler(0);
  598. rt_interrupt_leave();
  599. }
  600. void EINT1_IRQHandler(void)
  601. {
  602. rt_interrupt_enter();
  603. GPIO_EXTI_IRQHandler(1);
  604. rt_interrupt_leave();
  605. }
  606. void EINT2_IRQHandler(void)
  607. {
  608. rt_interrupt_enter();
  609. GPIO_EXTI_IRQHandler(2);
  610. rt_interrupt_leave();
  611. }
  612. void EINT3_IRQHandler(void)
  613. {
  614. rt_interrupt_enter();
  615. GPIO_EXTI_IRQHandler(3);
  616. rt_interrupt_leave();
  617. }
  618. void EINT4_IRQHandler(void)
  619. {
  620. rt_interrupt_enter();
  621. GPIO_EXTI_IRQHandler(4);
  622. rt_interrupt_leave();
  623. }
  624. void EINT9_5_IRQHandler(void)
  625. {
  626. rt_interrupt_enter();
  627. GPIO_EXTI_IRQHandler(5);
  628. GPIO_EXTI_IRQHandler(6);
  629. GPIO_EXTI_IRQHandler(7);
  630. GPIO_EXTI_IRQHandler(8);
  631. GPIO_EXTI_IRQHandler(9);
  632. rt_interrupt_leave();
  633. }
  634. void EINT15_10_IRQHandler(void)
  635. {
  636. rt_interrupt_enter();
  637. GPIO_EXTI_IRQHandler(10);
  638. GPIO_EXTI_IRQHandler(11);
  639. GPIO_EXTI_IRQHandler(12);
  640. GPIO_EXTI_IRQHandler(13);
  641. GPIO_EXTI_IRQHandler(14);
  642. GPIO_EXTI_IRQHandler(15);
  643. rt_interrupt_leave();
  644. }
  645. #endif
  646. int rt_hw_pin_init(void)
  647. {
  648. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  649. #ifdef GPIOA
  650. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
  651. #endif
  652. #ifdef GPIOB
  653. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  654. #endif
  655. #ifdef GPIOC
  656. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
  657. #endif
  658. #ifdef GPIOD
  659. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOD);
  660. #endif
  661. #ifdef GPIOE
  662. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);
  663. #endif
  664. #ifdef GPIOF
  665. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOF);
  666. #endif
  667. #ifdef GPIOG
  668. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOG);
  669. #endif
  670. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  671. #elif defined(SOC_SERIES_APM32F4)
  672. #ifdef GPIOA
  673. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
  674. #endif
  675. #ifdef GPIOB
  676. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOB);
  677. #endif
  678. #ifdef GPIOC
  679. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOC);
  680. #endif
  681. #ifdef GPIOD
  682. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOD);
  683. #endif
  684. #ifdef GPIOE
  685. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOE);
  686. #endif
  687. #ifdef GPIOF
  688. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOF);
  689. #endif
  690. #ifdef GPIOG
  691. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOG);
  692. #endif
  693. #ifdef GPIOH
  694. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOH);
  695. #endif
  696. #ifdef GPIOI
  697. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOI);
  698. #endif
  699. #ifdef GPIOJ
  700. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOJ);
  701. #endif
  702. #ifdef GPIOK
  703. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOK);
  704. #endif
  705. #elif defined(SOC_SERIES_APM32F0)
  706. #ifdef GPIOA
  707. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOA);
  708. #endif
  709. #ifdef GPIOB
  710. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOB);
  711. #endif
  712. #ifdef GPIOC
  713. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOC);
  714. #endif
  715. #ifdef GPIOD
  716. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOD);
  717. #endif
  718. #ifdef GPIOE
  719. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOE);
  720. #endif
  721. #ifdef GPIOF
  722. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOF);
  723. #endif
  724. #endif /* SOC_SERIES_APM32F0 */
  725. return rt_device_pin_register("pin", &apm32_pin_ops, RT_NULL);
  726. }
  727. #endif /* RT_USING_PIN */