1
0

drv_gpio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. else
  169. {
  170. return -RT_EINVAL;
  171. }
  172. return value;
  173. }
  174. static void apm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
  175. {
  176. GPIO_Config_T gpioConfig;
  177. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  178. {
  179. return;
  180. }
  181. /* Configure gpioConfigure */
  182. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  183. gpioConfig.pin = PIN_APMPIN(pin);
  184. gpioConfig.mode = GPIO_MODE_OUT_PP;
  185. gpioConfig.speed = GPIO_SPEED_50MHz;
  186. if (mode == PIN_MODE_OUTPUT)
  187. {
  188. /* output setting */
  189. gpioConfig.mode = GPIO_MODE_OUT_PP;
  190. }
  191. else if (mode == PIN_MODE_INPUT)
  192. {
  193. /* input setting: not pull. */
  194. gpioConfig.mode = GPIO_MODE_IN_PU;
  195. }
  196. else if (mode == PIN_MODE_INPUT_PULLUP)
  197. {
  198. /* input setting: pull up. */
  199. gpioConfig.mode = GPIO_MODE_IN_PU;
  200. }
  201. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  202. {
  203. /* input setting: pull down. */
  204. gpioConfig.mode = GPIO_MODE_IN_PD;
  205. }
  206. else if (mode == PIN_MODE_OUTPUT_OD)
  207. {
  208. /* output setting: od. */
  209. gpioConfig.mode = GPIO_MODE_OUT_OD;
  210. }
  211. #elif defined(SOC_SERIES_APM32F4)
  212. gpioConfig.pin = PIN_APMPIN(pin);
  213. gpioConfig.mode = GPIO_MODE_OUT;
  214. gpioConfig.otype = GPIO_OTYPE_PP;
  215. gpioConfig.speed = GPIO_SPEED_50MHz;
  216. if (mode == PIN_MODE_OUTPUT)
  217. {
  218. /* output setting */
  219. gpioConfig.mode = GPIO_MODE_OUT;
  220. gpioConfig.otype = GPIO_OTYPE_PP;
  221. }
  222. else if (mode == PIN_MODE_INPUT)
  223. {
  224. /* input setting: not pull. */
  225. gpioConfig.mode = GPIO_MODE_IN;
  226. gpioConfig.pupd = GPIO_PUPD_NOPULL;
  227. }
  228. else if (mode == PIN_MODE_INPUT_PULLUP)
  229. {
  230. /* input setting: pull up. */
  231. gpioConfig.mode = GPIO_MODE_IN;
  232. gpioConfig.pupd = GPIO_PUPD_UP;
  233. }
  234. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  235. {
  236. /* input setting: pull down. */
  237. gpioConfig.mode = GPIO_MODE_IN;
  238. gpioConfig.pupd = GPIO_PUPD_DOWN;
  239. }
  240. else if (mode == PIN_MODE_OUTPUT_OD)
  241. {
  242. /* output setting: od. */
  243. gpioConfig.mode = GPIO_MODE_OUT;
  244. gpioConfig.otype = GPIO_OTYPE_OD;
  245. }
  246. #elif defined(SOC_SERIES_APM32F0)
  247. gpioConfig.pin = PIN_APMPIN(pin);
  248. gpioConfig.mode = GPIO_MODE_OUT;
  249. gpioConfig.outtype = GPIO_OUT_TYPE_PP;
  250. gpioConfig.pupd = GPIO_PUPD_NO;
  251. gpioConfig.speed = GPIO_SPEED_50MHz;
  252. if (mode == PIN_MODE_OUTPUT)
  253. {
  254. /* output setting */
  255. gpioConfig.mode = GPIO_MODE_OUT;
  256. gpioConfig.outtype = GPIO_OUT_TYPE_PP;
  257. }
  258. else if (mode == PIN_MODE_INPUT)
  259. {
  260. /* input setting: not pull. */
  261. gpioConfig.mode = GPIO_MODE_IN;
  262. gpioConfig.pupd = GPIO_PUPD_NO;
  263. }
  264. else if (mode == PIN_MODE_INPUT_PULLUP)
  265. {
  266. /* input setting: pull up. */
  267. gpioConfig.mode = GPIO_MODE_IN;
  268. gpioConfig.pupd = GPIO_PUPD_PU;
  269. }
  270. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  271. {
  272. /* input setting: pull down. */
  273. gpioConfig.mode = GPIO_MODE_IN;
  274. gpioConfig.pupd = GPIO_PUPD_PD;
  275. }
  276. else if (mode == PIN_MODE_OUTPUT_OD)
  277. {
  278. /* output setting: od. */
  279. gpioConfig.mode = GPIO_MODE_OUT;
  280. gpioConfig.outtype = GPIO_OUT_TYPE_OD;
  281. }
  282. #endif
  283. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  284. }
  285. rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
  286. {
  287. int i;
  288. for (i = 0; i < 32; i++)
  289. {
  290. if ((0x01 << i) == bit)
  291. {
  292. return i;
  293. }
  294. }
  295. return -1;
  296. }
  297. rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
  298. {
  299. rt_int32_t mapindex = bit2bitno(pinbit);
  300. if (mapindex < 0 || mapindex >= ITEM_NUM(pin_irq_map))
  301. {
  302. return RT_NULL;
  303. }
  304. return &pin_irq_map[mapindex];
  305. };
  306. static rt_err_t apm32_pin_attach_irq(struct rt_device *device, rt_base_t pin,
  307. rt_uint8_t mode, void (*hdr)(void *args), void *args)
  308. {
  309. rt_base_t level;
  310. rt_int32_t irqindex = -1;
  311. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  312. {
  313. return -RT_ENOSYS;
  314. }
  315. irqindex = bit2bitno(PIN_APMPIN(pin));
  316. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  317. {
  318. return -RT_ENOSYS;
  319. }
  320. level = rt_hw_interrupt_disable();
  321. if (pin_irq_hdr_tab[irqindex].pin == pin &&
  322. pin_irq_hdr_tab[irqindex].hdr == hdr &&
  323. pin_irq_hdr_tab[irqindex].mode == mode &&
  324. pin_irq_hdr_tab[irqindex].args == args)
  325. {
  326. rt_hw_interrupt_enable(level);
  327. return RT_EOK;
  328. }
  329. if (pin_irq_hdr_tab[irqindex].pin != -1)
  330. {
  331. rt_hw_interrupt_enable(level);
  332. return -RT_EBUSY;
  333. }
  334. pin_irq_hdr_tab[irqindex].pin = pin;
  335. pin_irq_hdr_tab[irqindex].hdr = hdr;
  336. pin_irq_hdr_tab[irqindex].mode = mode;
  337. pin_irq_hdr_tab[irqindex].args = args;
  338. rt_hw_interrupt_enable(level);
  339. return RT_EOK;
  340. }
  341. static rt_err_t apm32_pin_dettach_irq(struct rt_device *device, rt_base_t pin)
  342. {
  343. rt_base_t level;
  344. rt_int32_t irqindex = -1;
  345. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  346. {
  347. return -RT_ENOSYS;
  348. }
  349. irqindex = bit2bitno(PIN_APMPIN(pin));
  350. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  351. {
  352. return -RT_ENOSYS;
  353. }
  354. level = rt_hw_interrupt_disable();
  355. if (pin_irq_hdr_tab[irqindex].pin == -1)
  356. {
  357. rt_hw_interrupt_enable(level);
  358. return RT_EOK;
  359. }
  360. pin_irq_hdr_tab[irqindex].pin = -1;
  361. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  362. pin_irq_hdr_tab[irqindex].mode = 0;
  363. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  364. rt_hw_interrupt_enable(level);
  365. return RT_EOK;
  366. }
  367. static rt_err_t apm32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
  368. rt_uint8_t enabled)
  369. {
  370. const struct pin_irq_map *irqmap;
  371. rt_base_t level;
  372. rt_int32_t irqindex = -1;
  373. GPIO_Config_T gpioConfig;
  374. EINT_Config_T eintConfig;
  375. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  376. {
  377. return -RT_ENOSYS;
  378. }
  379. if (enabled == PIN_IRQ_ENABLE)
  380. {
  381. irqindex = bit2bitno(PIN_APMPIN(pin));
  382. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  383. {
  384. return -RT_ENOSYS;
  385. }
  386. level = rt_hw_interrupt_disable();
  387. if (pin_irq_hdr_tab[irqindex].pin == -1)
  388. {
  389. rt_hw_interrupt_enable(level);
  390. return -RT_ENOSYS;
  391. }
  392. irqmap = &pin_irq_map[irqindex];
  393. /* Configure gpioConfigure */
  394. gpioConfig.pin = PIN_APMPIN(pin);
  395. gpioConfig.speed = GPIO_SPEED_50MHz;
  396. switch (pin_irq_hdr_tab[irqindex].mode)
  397. {
  398. #if defined(SOC_SERIES_APM32F0)
  399. case PIN_IRQ_MODE_RISING:
  400. gpioConfig.mode = GPIO_MODE_IN;
  401. gpioConfig.pupd = GPIO_PUPD_PD;
  402. eintConfig.trigger = EINT_TRIGGER_RISING;
  403. break;
  404. case PIN_IRQ_MODE_FALLING:
  405. gpioConfig.mode = GPIO_MODE_IN;
  406. gpioConfig.pupd = GPIO_PUPD_PU;
  407. eintConfig.trigger = EINT_TRIGGER_FALLING;
  408. break;
  409. case PIN_IRQ_MODE_RISING_FALLING:
  410. gpioConfig.mode = GPIO_MODE_IN;
  411. gpioConfig.pupd = GPIO_PUPD_NO;
  412. eintConfig.trigger = EINT_TRIGGER_ALL;
  413. break;
  414. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  415. case PIN_IRQ_MODE_RISING:
  416. gpioConfig.mode = GPIO_MODE_IN_PD;
  417. eintConfig.trigger = EINT_TRIGGER_RISING;
  418. break;
  419. case PIN_IRQ_MODE_FALLING:
  420. gpioConfig.mode = GPIO_MODE_IN_PU;
  421. eintConfig.trigger = EINT_TRIGGER_FALLING;
  422. break;
  423. case PIN_IRQ_MODE_RISING_FALLING:
  424. gpioConfig.mode = GPIO_MODE_IN_FLOATING;
  425. eintConfig.trigger = EINT_TRIGGER_RISING_FALLING;
  426. break;
  427. #elif defined(SOC_SERIES_APM32F4)
  428. case PIN_IRQ_MODE_RISING:
  429. gpioConfig.mode = GPIO_MODE_IN;
  430. gpioConfig.pupd = GPIO_PUPD_DOWN;
  431. eintConfig.trigger = EINT_TRIGGER_RISING;
  432. break;
  433. case PIN_IRQ_MODE_FALLING:
  434. gpioConfig.mode = GPIO_MODE_IN;
  435. gpioConfig.pupd = GPIO_PUPD_UP;
  436. eintConfig.trigger = EINT_TRIGGER_FALLING;
  437. break;
  438. case PIN_IRQ_MODE_RISING_FALLING:
  439. gpioConfig.mode = GPIO_MODE_IN;
  440. gpioConfig.pupd = GPIO_PUPD_NOPULL;
  441. eintConfig.trigger = EINT_TRIGGER_RISING_FALLING;
  442. break;
  443. #endif
  444. }
  445. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  446. #if defined(SOC_SERIES_APM32F0)
  447. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
  448. SYSCFG_EINTLine((SYSCFG_PORT_T)(((pin) >> 4) & 0xFu), (SYSCFG_PIN_T)irqindex);
  449. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  450. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  451. GPIO_ConfigEINTLine((GPIO_PORT_SOURCE_T)(((pin) >> 4) & 0xFu), (GPIO_PIN_SOURCE_T)irqindex);
  452. #elif defined(SOC_SERIES_APM32F4)
  453. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
  454. SYSCFG_ConfigEINTLine((SYSCFG_PORT_T)(((pin) >> 4) & 0xFu), (SYSCFG_PIN_T)irqindex);
  455. #endif
  456. eintConfig.line = (EINT_LINE_T)(1u << PIN_NO(pin));
  457. eintConfig.mode = EINT_MODE_INTERRUPT;
  458. eintConfig.lineCmd = ENABLE;
  459. EINT_Config(&eintConfig);
  460. #if defined(SOC_SERIES_APM32F0)
  461. NVIC_EnableIRQRequest(irqmap->irqno, 5);
  462. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  463. || defined(SOC_SERIES_APM32F4)
  464. NVIC_EnableIRQRequest(irqmap->irqno, 5, 0);
  465. #endif
  466. pin_irq_enable_mask |= irqmap->pinbit;
  467. rt_hw_interrupt_enable(level);
  468. }
  469. else if (enabled == PIN_IRQ_DISABLE)
  470. {
  471. irqmap = get_pin_irq_map(PIN_APMPIN(pin));
  472. if (irqmap == RT_NULL)
  473. {
  474. return -RT_ENOSYS;
  475. }
  476. level = rt_hw_interrupt_disable();
  477. pin_irq_enable_mask &= ~irqmap->pinbit;
  478. #if defined(SOC_SERIES_APM32F0)
  479. if ((irqmap->pinbit >= GPIO_PIN_0) && (irqmap->pinbit <= GPIO_PIN_1))
  480. {
  481. if (!(pin_irq_enable_mask & (GPIO_PIN_0 | GPIO_PIN_1)))
  482. {
  483. NVIC_DisableIRQRequest(irqmap->irqno);
  484. }
  485. }
  486. else if ((irqmap->pinbit >= GPIO_PIN_2) && (irqmap->pinbit <= GPIO_PIN_3))
  487. {
  488. if (!(pin_irq_enable_mask & (GPIO_PIN_2 | GPIO_PIN_3)))
  489. {
  490. NVIC_DisableIRQRequest(irqmap->irqno);
  491. }
  492. }
  493. else if ((irqmap->pinbit >= GPIO_PIN_4) && (irqmap->pinbit <= GPIO_PIN_15))
  494. {
  495. if (!(pin_irq_enable_mask & (GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 |
  496. GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)))
  497. {
  498. NVIC_DisableIRQRequest(irqmap->irqno);
  499. }
  500. }
  501. else
  502. {
  503. NVIC_DisableIRQRequest(irqmap->irqno);
  504. }
  505. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  506. || defined(SOC_SERIES_APM32F4)
  507. if ((irqmap->pinbit >= GPIO_PIN_5) && (irqmap->pinbit <= GPIO_PIN_9))
  508. {
  509. if (!(pin_irq_enable_mask & (GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9)))
  510. {
  511. NVIC_DisableIRQRequest(irqmap->irqno);
  512. }
  513. }
  514. else if ((irqmap->pinbit >= GPIO_PIN_10) && (irqmap->pinbit <= GPIO_PIN_15))
  515. {
  516. if (!(pin_irq_enable_mask & (GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)))
  517. {
  518. NVIC_DisableIRQRequest(irqmap->irqno);
  519. }
  520. }
  521. else
  522. {
  523. NVIC_DisableIRQRequest(irqmap->irqno);
  524. }
  525. #endif /* SOC_SERIES_APM32F0 */
  526. rt_hw_interrupt_enable(level);
  527. }
  528. else
  529. {
  530. return -RT_ENOSYS;
  531. }
  532. return RT_EOK;
  533. }
  534. const static struct rt_pin_ops apm32_pin_ops =
  535. {
  536. apm32_pin_mode,
  537. apm32_pin_write,
  538. apm32_pin_read,
  539. apm32_pin_attach_irq,
  540. apm32_pin_dettach_irq,
  541. apm32_pin_irq_enable,
  542. apm32_pin_get,
  543. };
  544. rt_inline void pin_irq_hdr(int irqno)
  545. {
  546. if (pin_irq_hdr_tab[irqno].hdr)
  547. {
  548. pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
  549. }
  550. }
  551. void GPIO_EXTI_IRQHandler(uint8_t exti_line)
  552. {
  553. #if defined(SOC_SERIES_APM32F0)
  554. if (EINT_ReadIntFlag(1U << exti_line) != RESET)
  555. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  556. || defined(SOC_SERIES_APM32F4)
  557. if (EINT_ReadIntFlag((EINT_LINE_T)(1U << exti_line)) != RESET)
  558. #endif
  559. {
  560. EINT_ClearIntFlag(1U << exti_line);
  561. pin_irq_hdr(exti_line);
  562. }
  563. }
  564. #if defined(SOC_SERIES_APM32F0)
  565. void EINT0_1_IRQHandler(void)
  566. {
  567. rt_interrupt_enter();
  568. GPIO_EXTI_IRQHandler(0);
  569. GPIO_EXTI_IRQHandler(1);
  570. rt_interrupt_leave();
  571. }
  572. void EINT2_3_IRQHandler(void)
  573. {
  574. rt_interrupt_enter();
  575. GPIO_EXTI_IRQHandler(2);
  576. GPIO_EXTI_IRQHandler(3);
  577. rt_interrupt_leave();
  578. }
  579. void EINT4_15_IRQHandler(void)
  580. {
  581. rt_interrupt_enter();
  582. GPIO_EXTI_IRQHandler(4);
  583. GPIO_EXTI_IRQHandler(5);
  584. GPIO_EXTI_IRQHandler(6);
  585. GPIO_EXTI_IRQHandler(7);
  586. GPIO_EXTI_IRQHandler(8);
  587. GPIO_EXTI_IRQHandler(9);
  588. GPIO_EXTI_IRQHandler(10);
  589. GPIO_EXTI_IRQHandler(11);
  590. GPIO_EXTI_IRQHandler(12);
  591. GPIO_EXTI_IRQHandler(13);
  592. GPIO_EXTI_IRQHandler(14);
  593. GPIO_EXTI_IRQHandler(15);
  594. rt_interrupt_leave();
  595. }
  596. #elif defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1) \
  597. || defined(SOC_SERIES_APM32F4)
  598. void EINT0_IRQHandler(void)
  599. {
  600. rt_interrupt_enter();
  601. GPIO_EXTI_IRQHandler(0);
  602. rt_interrupt_leave();
  603. }
  604. void EINT1_IRQHandler(void)
  605. {
  606. rt_interrupt_enter();
  607. GPIO_EXTI_IRQHandler(1);
  608. rt_interrupt_leave();
  609. }
  610. void EINT2_IRQHandler(void)
  611. {
  612. rt_interrupt_enter();
  613. GPIO_EXTI_IRQHandler(2);
  614. rt_interrupt_leave();
  615. }
  616. void EINT3_IRQHandler(void)
  617. {
  618. rt_interrupt_enter();
  619. GPIO_EXTI_IRQHandler(3);
  620. rt_interrupt_leave();
  621. }
  622. void EINT4_IRQHandler(void)
  623. {
  624. rt_interrupt_enter();
  625. GPIO_EXTI_IRQHandler(4);
  626. rt_interrupt_leave();
  627. }
  628. void EINT9_5_IRQHandler(void)
  629. {
  630. rt_interrupt_enter();
  631. GPIO_EXTI_IRQHandler(5);
  632. GPIO_EXTI_IRQHandler(6);
  633. GPIO_EXTI_IRQHandler(7);
  634. GPIO_EXTI_IRQHandler(8);
  635. GPIO_EXTI_IRQHandler(9);
  636. rt_interrupt_leave();
  637. }
  638. void EINT15_10_IRQHandler(void)
  639. {
  640. rt_interrupt_enter();
  641. GPIO_EXTI_IRQHandler(10);
  642. GPIO_EXTI_IRQHandler(11);
  643. GPIO_EXTI_IRQHandler(12);
  644. GPIO_EXTI_IRQHandler(13);
  645. GPIO_EXTI_IRQHandler(14);
  646. GPIO_EXTI_IRQHandler(15);
  647. rt_interrupt_leave();
  648. }
  649. #endif
  650. int rt_hw_pin_init(void)
  651. {
  652. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32E1) || defined(SOC_SERIES_APM32S1)
  653. #ifdef GPIOA
  654. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
  655. #endif
  656. #ifdef GPIOB
  657. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  658. #endif
  659. #ifdef GPIOC
  660. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
  661. #endif
  662. #ifdef GPIOD
  663. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOD);
  664. #endif
  665. #ifdef GPIOE
  666. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);
  667. #endif
  668. #ifdef GPIOF
  669. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOF);
  670. #endif
  671. #ifdef GPIOG
  672. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOG);
  673. #endif
  674. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  675. #elif defined(SOC_SERIES_APM32F4)
  676. #ifdef GPIOA
  677. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
  678. #endif
  679. #ifdef GPIOB
  680. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOB);
  681. #endif
  682. #ifdef GPIOC
  683. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOC);
  684. #endif
  685. #ifdef GPIOD
  686. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOD);
  687. #endif
  688. #ifdef GPIOE
  689. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOE);
  690. #endif
  691. #ifdef GPIOF
  692. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOF);
  693. #endif
  694. #ifdef GPIOG
  695. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOG);
  696. #endif
  697. #ifdef GPIOH
  698. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOH);
  699. #endif
  700. #ifdef GPIOI
  701. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOI);
  702. #endif
  703. #ifdef GPIOJ
  704. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOJ);
  705. #endif
  706. #ifdef GPIOK
  707. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOK);
  708. #endif
  709. #elif defined(SOC_SERIES_APM32F0)
  710. #ifdef GPIOA
  711. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOA);
  712. #endif
  713. #ifdef GPIOB
  714. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOB);
  715. #endif
  716. #ifdef GPIOC
  717. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOC);
  718. #endif
  719. #ifdef GPIOD
  720. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOD);
  721. #endif
  722. #ifdef GPIOE
  723. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOE);
  724. #endif
  725. #ifdef GPIOF
  726. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOF);
  727. #endif
  728. #endif /* SOC_SERIES_APM32F0 */
  729. return rt_device_pin_register("pin", &apm32_pin_ops, RT_NULL);
  730. }
  731. #endif /* RT_USING_PIN */