drv_gpio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file drv_gpio.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include <rtdevice.h>
  35. #include <rthw.h>
  36. #include "board.h"
  37. #ifdef RT_USING_PIN
  38. static const struct pin_index pins[] =
  39. {
  40. #if defined(GPIOA)
  41. __N32_PIN(0 , GPIOA, GPIO_PIN_0 ),
  42. __N32_PIN(1 , GPIOA, GPIO_PIN_1 ),
  43. __N32_PIN(2 , GPIOA, GPIO_PIN_2 ),
  44. __N32_PIN(3 , GPIOA, GPIO_PIN_3 ),
  45. __N32_PIN(4 , GPIOA, GPIO_PIN_4 ),
  46. __N32_PIN(5 , GPIOA, GPIO_PIN_5 ),
  47. __N32_PIN(6 , GPIOA, GPIO_PIN_6 ),
  48. __N32_PIN(7 , GPIOA, GPIO_PIN_7 ),
  49. __N32_PIN(8 , GPIOA, GPIO_PIN_8 ),
  50. __N32_PIN(9 , GPIOA, GPIO_PIN_9 ),
  51. __N32_PIN(10, GPIOA, GPIO_PIN_10),
  52. __N32_PIN(11, GPIOA, GPIO_PIN_11),
  53. __N32_PIN(12, GPIOA, GPIO_PIN_12),
  54. __N32_PIN(13, GPIOA, GPIO_PIN_13),
  55. __N32_PIN(14, GPIOA, GPIO_PIN_14),
  56. __N32_PIN(15, GPIOA, GPIO_PIN_15),
  57. #if defined(GPIOB)
  58. __N32_PIN(16, GPIOB, GPIO_PIN_0),
  59. __N32_PIN(17, GPIOB, GPIO_PIN_1),
  60. __N32_PIN(18, GPIOB, GPIO_PIN_2),
  61. __N32_PIN(19, GPIOB, GPIO_PIN_3),
  62. __N32_PIN(20, GPIOB, GPIO_PIN_4),
  63. __N32_PIN(21, GPIOB, GPIO_PIN_5),
  64. __N32_PIN(22, GPIOB, GPIO_PIN_6),
  65. __N32_PIN(23, GPIOB, GPIO_PIN_7),
  66. __N32_PIN(24, GPIOB, GPIO_PIN_8),
  67. __N32_PIN(25, GPIOB, GPIO_PIN_9),
  68. __N32_PIN(26, GPIOB, GPIO_PIN_10),
  69. __N32_PIN(27, GPIOB, GPIO_PIN_11),
  70. __N32_PIN(28, GPIOB, GPIO_PIN_12),
  71. __N32_PIN(29, GPIOB, GPIO_PIN_13),
  72. __N32_PIN(30, GPIOB, GPIO_PIN_14),
  73. __N32_PIN(31, GPIOB, GPIO_PIN_15),
  74. #if defined(GPIOC)
  75. __N32_PIN(32, GPIOC, GPIO_PIN_0),
  76. __N32_PIN(33, GPIOC, GPIO_PIN_1),
  77. __N32_PIN(34, GPIOC, GPIO_PIN_2),
  78. __N32_PIN(35, GPIOC, GPIO_PIN_3),
  79. __N32_PIN(36, GPIOC, GPIO_PIN_4),
  80. __N32_PIN(37, GPIOC, GPIO_PIN_5),
  81. __N32_PIN(38, GPIOC, GPIO_PIN_6),
  82. __N32_PIN(39, GPIOC, GPIO_PIN_7),
  83. __N32_PIN(40, GPIOC, GPIO_PIN_8),
  84. __N32_PIN(41, GPIOC, GPIO_PIN_9),
  85. __N32_PIN(42, GPIOC, GPIO_PIN_10),
  86. __N32_PIN(43, GPIOC, GPIO_PIN_11),
  87. __N32_PIN(44, GPIOC, GPIO_PIN_12),
  88. __N32_PIN(45, GPIOC, GPIO_PIN_13),
  89. __N32_PIN(46, GPIOC, GPIO_PIN_14),
  90. __N32_PIN(47, GPIOC, GPIO_PIN_15),
  91. #if defined(GPIOD)
  92. __N32_PIN(48, GPIOD, GPIO_PIN_0),
  93. __N32_PIN(49, GPIOD, GPIO_PIN_1),
  94. __N32_PIN(50, GPIOD, GPIO_PIN_2),
  95. __N32_PIN(51, GPIOD, GPIO_PIN_3),
  96. __N32_PIN(52, GPIOD, GPIO_PIN_4),
  97. __N32_PIN(53, GPIOD, GPIO_PIN_5),
  98. __N32_PIN(54, GPIOD, GPIO_PIN_6),
  99. __N32_PIN(55, GPIOD, GPIO_PIN_7),
  100. __N32_PIN(56, GPIOD, GPIO_PIN_8),
  101. __N32_PIN(57, GPIOD, GPIO_PIN_9),
  102. __N32_PIN(58, GPIOD, GPIO_PIN_10),
  103. __N32_PIN(59, GPIOD, GPIO_PIN_11),
  104. __N32_PIN(60, GPIOD, GPIO_PIN_12),
  105. __N32_PIN(61, GPIOD, GPIO_PIN_13),
  106. __N32_PIN(62, GPIOD, GPIO_PIN_14),
  107. __N32_PIN(63, GPIOD, GPIO_PIN_15),
  108. #if defined(GPIOE)
  109. __N32_PIN(64, GPIOE, GPIO_PIN_0),
  110. __N32_PIN(65, GPIOE, GPIO_PIN_1),
  111. __N32_PIN(66, GPIOE, GPIO_PIN_2),
  112. __N32_PIN(67, GPIOE, GPIO_PIN_3),
  113. __N32_PIN(68, GPIOE, GPIO_PIN_4),
  114. __N32_PIN(69, GPIOE, GPIO_PIN_5),
  115. __N32_PIN(70, GPIOE, GPIO_PIN_6),
  116. __N32_PIN(71, GPIOE, GPIO_PIN_7),
  117. __N32_PIN(72, GPIOE, GPIO_PIN_8),
  118. __N32_PIN(73, GPIOE, GPIO_PIN_9),
  119. __N32_PIN(74, GPIOE, GPIO_PIN_10),
  120. __N32_PIN(75, GPIOE, GPIO_PIN_11),
  121. __N32_PIN(76, GPIOE, GPIO_PIN_12),
  122. __N32_PIN(77, GPIOE, GPIO_PIN_13),
  123. __N32_PIN(78, GPIOE, GPIO_PIN_14),
  124. __N32_PIN(79, GPIOE, GPIO_PIN_15),
  125. #if defined(GPIOF)
  126. __N32_PIN(80, GPIOF, GPIO_PIN_0),
  127. __N32_PIN(81, GPIOF, GPIO_PIN_1),
  128. __N32_PIN(82, GPIOF, GPIO_PIN_2),
  129. __N32_PIN(83, GPIOF, GPIO_PIN_3),
  130. __N32_PIN(84, GPIOF, GPIO_PIN_4),
  131. __N32_PIN(85, GPIOF, GPIO_PIN_5),
  132. __N32_PIN(86, GPIOF, GPIO_PIN_6),
  133. __N32_PIN(87, GPIOF, GPIO_PIN_7),
  134. __N32_PIN(88, GPIOF, GPIO_PIN_8),
  135. __N32_PIN(89, GPIOF, GPIO_PIN_9),
  136. __N32_PIN(90, GPIOF, GPIO_PIN_10),
  137. __N32_PIN(91, GPIOF, GPIO_PIN_11),
  138. __N32_PIN(92, GPIOF, GPIO_PIN_12),
  139. __N32_PIN(93, GPIOF, GPIO_PIN_13),
  140. __N32_PIN(94, GPIOF, GPIO_PIN_14),
  141. __N32_PIN(95, GPIOF, GPIO_PIN_15),
  142. #if defined(GPIOG)
  143. __N32_PIN(96, GPIOG, GPIO_PIN_0),
  144. __N32_PIN(97, GPIOG, GPIO_PIN_1),
  145. __N32_PIN(98, GPIOG, GPIO_PIN_2),
  146. __N32_PIN(99, GPIOG, GPIO_PIN_3),
  147. __N32_PIN(100, GPIOG, GPIO_PIN_4),
  148. __N32_PIN(101, GPIOG, GPIO_PIN_5),
  149. __N32_PIN(102, GPIOG, GPIO_PIN_6),
  150. __N32_PIN(103, GPIOG, GPIO_PIN_7),
  151. __N32_PIN(104, GPIOG, GPIO_PIN_8),
  152. __N32_PIN(105, GPIOG, GPIO_PIN_9),
  153. __N32_PIN(106, GPIOG, GPIO_PIN_10),
  154. __N32_PIN(107, GPIOG, GPIO_PIN_11),
  155. __N32_PIN(108, GPIOG, GPIO_PIN_12),
  156. __N32_PIN(109, GPIOG, GPIO_PIN_13),
  157. __N32_PIN(110, GPIOG, GPIO_PIN_14),
  158. __N32_PIN(111, GPIOG, GPIO_PIN_15),
  159. #endif /* defined(GPIOG) */
  160. #endif /* defined(GPIOF) */
  161. #endif /* defined(GPIOE) */
  162. #endif /* defined(GPIOD) */
  163. #endif /* defined(GPIOC) */
  164. #endif /* defined(GPIOB) */
  165. #endif /* defined(GPIOA) */
  166. };
  167. static const struct pin_irq_map pin_irq_map[] =
  168. {
  169. {GPIO_PIN_0, EXTI0_IRQn},
  170. {GPIO_PIN_1, EXTI1_IRQn},
  171. {GPIO_PIN_2, EXTI2_IRQn},
  172. {GPIO_PIN_3, EXTI3_IRQn},
  173. {GPIO_PIN_4, EXTI4_IRQn},
  174. {GPIO_PIN_5, EXTI9_5_IRQn},
  175. {GPIO_PIN_6, EXTI9_5_IRQn},
  176. {GPIO_PIN_7, EXTI9_5_IRQn},
  177. {GPIO_PIN_8, EXTI9_5_IRQn},
  178. {GPIO_PIN_9, EXTI9_5_IRQn},
  179. {GPIO_PIN_10, EXTI15_10_IRQn},
  180. {GPIO_PIN_11, EXTI15_10_IRQn},
  181. {GPIO_PIN_12, EXTI15_10_IRQn},
  182. {GPIO_PIN_13, EXTI15_10_IRQn},
  183. {GPIO_PIN_14, EXTI15_10_IRQn},
  184. {GPIO_PIN_15, EXTI15_10_IRQn},
  185. };
  186. static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
  187. {
  188. {-1, 0, RT_NULL, RT_NULL},
  189. {-1, 0, RT_NULL, RT_NULL},
  190. {-1, 0, RT_NULL, RT_NULL},
  191. {-1, 0, RT_NULL, RT_NULL},
  192. {-1, 0, RT_NULL, RT_NULL},
  193. {-1, 0, RT_NULL, RT_NULL},
  194. {-1, 0, RT_NULL, RT_NULL},
  195. {-1, 0, RT_NULL, RT_NULL},
  196. {-1, 0, RT_NULL, RT_NULL},
  197. {-1, 0, RT_NULL, RT_NULL},
  198. {-1, 0, RT_NULL, RT_NULL},
  199. {-1, 0, RT_NULL, RT_NULL},
  200. {-1, 0, RT_NULL, RT_NULL},
  201. {-1, 0, RT_NULL, RT_NULL},
  202. {-1, 0, RT_NULL, RT_NULL},
  203. {-1, 0, RT_NULL, RT_NULL},
  204. };
  205. static uint32_t pin_irq_enable_mask=0;
  206. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  207. static const struct pin_index *get_pin(uint8_t pin)
  208. {
  209. const struct pin_index *index;
  210. if (pin < ITEM_NUM(pins))
  211. {
  212. index = &pins[pin];
  213. if (index->index == -1)
  214. index = RT_NULL;
  215. }
  216. else
  217. {
  218. index = RT_NULL;
  219. }
  220. return index;
  221. };
  222. static void n32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  223. {
  224. const struct pin_index *index;
  225. index = get_pin(pin);
  226. if (index == RT_NULL)
  227. {
  228. return;
  229. }
  230. GPIO_WriteBit(index->gpio, index->pin, (Bit_OperateType)value);
  231. }
  232. static int n32_pin_read(rt_device_t dev, rt_base_t pin)
  233. {
  234. int value;
  235. const struct pin_index *index;
  236. value = PIN_LOW;
  237. index = get_pin(pin);
  238. if (index == RT_NULL)
  239. {
  240. return value;
  241. }
  242. value = GPIO_ReadInputDataBit(index->gpio, index->pin);
  243. return value;
  244. }
  245. static void n32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  246. {
  247. const struct pin_index *index;
  248. GPIO_InitType GPIO_InitStructure;
  249. index = get_pin(pin);
  250. if (index == RT_NULL)
  251. {
  252. return;
  253. }
  254. GPIO_InitStruct(&GPIO_InitStructure);
  255. /* Configure GPIO_InitStructure */
  256. GPIO_InitStructure.Pin = index->pin;
  257. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  258. if (mode == PIN_MODE_OUTPUT)
  259. {
  260. /* output setting */
  261. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  262. }
  263. else if (mode == PIN_MODE_INPUT)
  264. {
  265. /* input setting: not pull. */
  266. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  267. }
  268. else if (mode == PIN_MODE_INPUT_PULLUP)
  269. {
  270. /* input setting: pull up. */
  271. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  272. }
  273. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  274. {
  275. /* input setting: pull down. */
  276. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  277. }
  278. else if (mode == PIN_MODE_OUTPUT_OD)
  279. {
  280. /* output setting: od. */
  281. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  282. }
  283. GPIOInit(index->gpio, GPIO_InitStructure.GPIO_Mode, GPIO_InitStructure.GPIO_Speed, GPIO_InitStructure.Pin);
  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 rt_int32_t port2portsource(GPIO_Module* module)
  298. {
  299. if(module == GPIOA)
  300. {
  301. return GPIOA_PORT_SOURCE;
  302. }
  303. else if(module == GPIOB)
  304. {
  305. return GPIOB_PORT_SOURCE;
  306. }
  307. else if(module == GPIOC)
  308. {
  309. return GPIOC_PORT_SOURCE;
  310. }
  311. else if(module == GPIOD)
  312. {
  313. return GPIOD_PORT_SOURCE;
  314. }
  315. else if(module == GPIOE)
  316. {
  317. return GPIOE_PORT_SOURCE;
  318. }
  319. else if(module == GPIOF)
  320. {
  321. return GPIOF_PORT_SOURCE;
  322. }
  323. else if(module == GPIOG)
  324. {
  325. return GPIOG_PORT_SOURCE;
  326. }
  327. else
  328. {
  329. return GPIOA_PORT_SOURCE;
  330. }
  331. }
  332. rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
  333. {
  334. rt_int32_t mapindex = bit2bitno(pinbit);
  335. if (mapindex < 0 || mapindex >= ITEM_NUM(pin_irq_map))
  336. {
  337. return RT_NULL;
  338. }
  339. return &pin_irq_map[mapindex];
  340. };
  341. static rt_err_t n32_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
  342. rt_uint32_t mode, void (*hdr)(void *args), void *args)
  343. {
  344. const struct pin_index *index;
  345. rt_base_t level;
  346. rt_int32_t irqindex = -1;
  347. index = get_pin(pin);
  348. if (index == RT_NULL)
  349. {
  350. return RT_ENOSYS;
  351. }
  352. irqindex = bit2bitno(index->pin);
  353. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  354. {
  355. return RT_ENOSYS;
  356. }
  357. level = rt_hw_interrupt_disable();
  358. if (pin_irq_hdr_tab[irqindex].pin == pin &&
  359. pin_irq_hdr_tab[irqindex].hdr == hdr &&
  360. pin_irq_hdr_tab[irqindex].mode == mode &&
  361. pin_irq_hdr_tab[irqindex].args == args)
  362. {
  363. rt_hw_interrupt_enable(level);
  364. return RT_EOK;
  365. }
  366. if (pin_irq_hdr_tab[irqindex].pin != -1)
  367. {
  368. rt_hw_interrupt_enable(level);
  369. return RT_EBUSY;
  370. }
  371. pin_irq_hdr_tab[irqindex].pin = pin;
  372. pin_irq_hdr_tab[irqindex].hdr = hdr;
  373. pin_irq_hdr_tab[irqindex].mode = mode;
  374. pin_irq_hdr_tab[irqindex].args = args;
  375. rt_hw_interrupt_enable(level);
  376. return RT_EOK;
  377. }
  378. static rt_err_t n32_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
  379. {
  380. const struct pin_index *index;
  381. rt_base_t level;
  382. rt_int32_t irqindex = -1;
  383. index = get_pin(pin);
  384. if (index == RT_NULL)
  385. {
  386. return RT_ENOSYS;
  387. }
  388. irqindex = bit2bitno(index->pin);
  389. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  390. {
  391. return RT_ENOSYS;
  392. }
  393. level = rt_hw_interrupt_disable();
  394. if (pin_irq_hdr_tab[irqindex].pin == -1)
  395. {
  396. rt_hw_interrupt_enable(level);
  397. return RT_EOK;
  398. }
  399. pin_irq_hdr_tab[irqindex].pin = -1;
  400. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  401. pin_irq_hdr_tab[irqindex].mode = 0;
  402. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  403. rt_hw_interrupt_enable(level);
  404. return RT_EOK;
  405. }
  406. static rt_err_t n32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
  407. rt_uint32_t enabled)
  408. {
  409. const struct pin_index *index;
  410. const struct pin_irq_map *irqmap;
  411. rt_base_t level;
  412. rt_int32_t irqindex = -1;
  413. GPIO_InitType GPIO_InitStructure;
  414. EXTI_InitType EXTI_InitStructure;
  415. index = get_pin(pin);
  416. if (index == RT_NULL)
  417. {
  418. return RT_ENOSYS;
  419. }
  420. if (enabled == PIN_IRQ_ENABLE)
  421. {
  422. irqindex = bit2bitno(index->pin);
  423. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  424. {
  425. return RT_ENOSYS;
  426. }
  427. level = rt_hw_interrupt_disable();
  428. if (pin_irq_hdr_tab[irqindex].pin == -1)
  429. {
  430. rt_hw_interrupt_enable(level);
  431. return RT_ENOSYS;
  432. }
  433. irqmap = &pin_irq_map[irqindex];
  434. /* Configure GPIO_InitStructure */
  435. GPIO_InitStruct(&GPIO_InitStructure);
  436. GPIO_InitStructure.Pin = index->pin;
  437. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  438. switch (pin_irq_hdr_tab[irqindex].mode)
  439. {
  440. case PIN_IRQ_MODE_RISING:
  441. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  442. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  443. break;
  444. case PIN_IRQ_MODE_FALLING:
  445. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  446. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  447. break;
  448. case PIN_IRQ_MODE_RISING_FALLING:
  449. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  450. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  451. break;
  452. }
  453. GPIO_InitPeripheral(index->gpio, &GPIO_InitStructure);
  454. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE);
  455. /* configure EXTI line */
  456. GPIO_ConfigEXTILine(port2portsource(index->gpio), irqindex);
  457. /*Configure key EXTI line*/
  458. EXTI_InitStructure.EXTI_Line = index->pin;
  459. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  460. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  461. EXTI_InitPeripheral(&EXTI_InitStructure);
  462. EXTI_ClrITPendBit(index->pin);
  463. NVIC_SetPriority(irqmap->irqno, 5);
  464. NVIC_EnableIRQ(irqmap->irqno);
  465. pin_irq_enable_mask |= irqmap->pinbit;
  466. rt_hw_interrupt_enable(level);
  467. }
  468. else if (enabled == PIN_IRQ_DISABLE)
  469. {
  470. irqmap = get_pin_irq_map(index->pin);
  471. if (irqmap == RT_NULL)
  472. {
  473. return RT_ENOSYS;
  474. }
  475. level = rt_hw_interrupt_disable();
  476. pin_irq_enable_mask &= ~irqmap->pinbit;
  477. if (( irqmap->pinbit>=GPIO_PIN_5 )&&( irqmap->pinbit<=GPIO_PIN_9 ))
  478. {
  479. if(!(pin_irq_enable_mask&(GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9)))
  480. {
  481. NVIC_DisableIRQ(irqmap->irqno);
  482. }
  483. }
  484. else if (( irqmap->pinbit>=GPIO_PIN_10 )&&( irqmap->pinbit<=GPIO_PIN_15 ))
  485. {
  486. if(!(pin_irq_enable_mask&(GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15)))
  487. {
  488. NVIC_DisableIRQ(irqmap->irqno);
  489. }
  490. }
  491. else
  492. {
  493. NVIC_DisableIRQ(irqmap->irqno);
  494. }
  495. rt_hw_interrupt_enable(level);
  496. }
  497. else
  498. {
  499. return -RT_ENOSYS;
  500. }
  501. return RT_EOK;
  502. }
  503. const static struct rt_pin_ops _n32_pin_ops =
  504. {
  505. n32_pin_mode,
  506. n32_pin_write,
  507. n32_pin_read,
  508. n32_pin_attach_irq,
  509. n32_pin_dettach_irq,
  510. n32_pin_irq_enable,
  511. };
  512. int rt_hw_pin_init(void)
  513. {
  514. #if defined(RCC_GPIOA_CLK_ENABLE)
  515. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  516. #endif
  517. #if defined(RCC_GPIOB_CLK_ENABLE)
  518. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  519. #endif
  520. #if defined(RCC_GPIOC_CLK_ENABLE)
  521. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  522. #endif
  523. #if defined(RCC_GPIOD_CLK_ENABLE)
  524. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
  525. #endif
  526. #if defined(RCC_GPIOE_CLK_ENABLE)
  527. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
  528. #endif
  529. #if defined(RCC_GPIOF_CLK_ENABLE)
  530. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);
  531. #endif
  532. #if defined(RCC_GPIOG_CLK_ENABLE)
  533. __RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOG, ENABLE);
  534. #endif
  535. return rt_device_pin_register("pin", &_n32_pin_ops, RT_NULL);
  536. }
  537. INIT_BOARD_EXPORT(rt_hw_pin_init);
  538. rt_inline void pin_irq_hdr(int irqno)
  539. {
  540. if (pin_irq_hdr_tab[irqno].hdr)
  541. {
  542. pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
  543. }
  544. }
  545. void N32_GPIO_EXTI_IRQHandler(rt_int8_t exti_line)
  546. {
  547. if(RESET != EXTI_GetITStatus(1 << exti_line))
  548. {
  549. pin_irq_hdr(exti_line);
  550. EXTI_ClrITPendBit(1 << exti_line);
  551. }
  552. }
  553. void EXTI0_IRQHandler(void)
  554. {
  555. rt_interrupt_enter();
  556. N32_GPIO_EXTI_IRQHandler(0);
  557. rt_interrupt_leave();
  558. }
  559. void EXTI1_IRQHandler(void)
  560. {
  561. rt_interrupt_enter();
  562. N32_GPIO_EXTI_IRQHandler(1);
  563. rt_interrupt_leave();
  564. }
  565. void EXTI2_IRQHandler(void)
  566. {
  567. rt_interrupt_enter();
  568. N32_GPIO_EXTI_IRQHandler(2);
  569. rt_interrupt_leave();
  570. }
  571. void EXTI3_IRQHandler(void)
  572. {
  573. rt_interrupt_enter();
  574. N32_GPIO_EXTI_IRQHandler(3);
  575. rt_interrupt_leave();
  576. }
  577. void EXTI4_IRQHandler(void)
  578. {
  579. rt_interrupt_enter();
  580. N32_GPIO_EXTI_IRQHandler(4);
  581. rt_interrupt_leave();
  582. }
  583. void EXTI9_5_IRQHandler(void)
  584. {
  585. rt_interrupt_enter();
  586. N32_GPIO_EXTI_IRQHandler(5);
  587. N32_GPIO_EXTI_IRQHandler(6);
  588. N32_GPIO_EXTI_IRQHandler(7);
  589. N32_GPIO_EXTI_IRQHandler(8);
  590. N32_GPIO_EXTI_IRQHandler(9);
  591. rt_interrupt_leave();
  592. }
  593. void EXTI15_10_IRQHandler(void)
  594. {
  595. rt_interrupt_enter();
  596. N32_GPIO_EXTI_IRQHandler(10);
  597. N32_GPIO_EXTI_IRQHandler(11);
  598. N32_GPIO_EXTI_IRQHandler(12);
  599. N32_GPIO_EXTI_IRQHandler(13);
  600. N32_GPIO_EXTI_IRQHandler(14);
  601. N32_GPIO_EXTI_IRQHandler(15);
  602. rt_interrupt_leave();
  603. }
  604. void GPIOInit(GPIO_Module* GPIOx, GPIO_ModeType mode, GPIO_SpeedType speed, uint16_t Pin)
  605. {
  606. GPIO_InitType GPIO_InitStructure;
  607. /* Check the parameters */
  608. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  609. /* Enable the GPIO Clock */
  610. if (GPIOx == GPIOA)
  611. {
  612. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  613. }
  614. else if (GPIOx == GPIOB)
  615. {
  616. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  617. }
  618. else if (GPIOx == GPIOC)
  619. {
  620. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  621. }
  622. else if (GPIOx == GPIOD)
  623. {
  624. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
  625. }
  626. else if (GPIOx == GPIOE)
  627. {
  628. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
  629. }
  630. else if (GPIOx == GPIOF)
  631. {
  632. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);
  633. }
  634. else
  635. {
  636. if (GPIOx == GPIOG)
  637. {
  638. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOG, ENABLE);
  639. }
  640. }
  641. /* Configure the GPIO pin */
  642. GPIO_InitStructure.Pin = Pin;
  643. GPIO_InitStructure.GPIO_Mode = mode;
  644. GPIO_InitStructure.GPIO_Speed = speed;
  645. GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
  646. }
  647. #endif /* RT_USING_PIN */