drv_gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-31 ZYH first version
  9. * 2018-12-10 Zohar_Lee 修复bug
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. #include <SWM320_port.h>
  15. #include <SWM320_gpio.h>
  16. #include <SWM320_exti.h>
  17. #include <rthw.h>
  18. typedef void (*pin_callback_t)(void *args);
  19. struct pin
  20. {
  21. uint32_t package_index;
  22. const char *name;
  23. GPIO_TypeDef *port;
  24. uint32_t group_index;
  25. IRQn_Type irq;
  26. rt_uint32_t irq_mode;
  27. pin_callback_t callback;
  28. void *callback_args;
  29. };
  30. typedef struct pin pin_t;
  31. #define SWM32_PIN(a, b, c, d) \
  32. { \
  33. a, #b, GPIO##c, d, GPIO##c##_IRQn \
  34. }
  35. #define GPIO0 ((GPIO_TypeDef *)(0))
  36. #define GPIO0_IRQn (GPIOA0_IRQn)
  37. const static pin_t swm32_pin_map[] =
  38. {
  39. SWM32_PIN(0, None, 0, 0),
  40. SWM32_PIN(1, ADC0 CH3, 0, 0),
  41. SWM32_PIN(2, ADC0 REFP, 0, 0),
  42. SWM32_PIN(3, Cap0, 0, 0),
  43. SWM32_PIN(4, B12, B, 12),
  44. SWM32_PIN(5, RTC VDD, 0, 0),
  45. SWM32_PIN(6, N14, N, 14),
  46. SWM32_PIN(7, N13, N, 13),
  47. SWM32_PIN(8, N12, N, 12),
  48. SWM32_PIN(9, N11, N, 11),
  49. SWM32_PIN(10, VDD 3.3V, 0, 0),
  50. SWM32_PIN(11, VSS 3.3V, 0, 0),
  51. SWM32_PIN(12, Cap 2, 0, 0),
  52. SWM32_PIN(13, N9, N, 9),
  53. SWM32_PIN(14, N10, N, 10),
  54. SWM32_PIN(15, Cap 1, 0, 0),
  55. SWM32_PIN(16, AVSS, 0, 0),
  56. SWM32_PIN(17, AVDD, 0, 0),
  57. SWM32_PIN(18, N2, N, 2),
  58. SWM32_PIN(19, N1, N, 1),
  59. SWM32_PIN(20, N0, N, 0),
  60. SWM32_PIN(21, C4, C, 4),
  61. SWM32_PIN(22, C5, C, 5),
  62. SWM32_PIN(23, C6, C, 6),
  63. SWM32_PIN(24, C7, C, 7),
  64. SWM32_PIN(25, C2, C, 2),
  65. SWM32_PIN(26, C3, C, 3),
  66. SWM32_PIN(27, XHIN, 0, 0),
  67. SWM32_PIN(28, XHOUT, 0, 0),
  68. SWM32_PIN(29, RESET, 0, 0),
  69. SWM32_PIN(30, M2, M, 2),
  70. SWM32_PIN(31, M3, M, 3),
  71. SWM32_PIN(32, M4, M, 4),
  72. SWM32_PIN(33, M5, M, 5),
  73. SWM32_PIN(34, M6, M, 6),
  74. SWM32_PIN(35, M7, M, 7),
  75. SWM32_PIN(36, M8, M, 8),
  76. SWM32_PIN(37, M9, M, 9),
  77. SWM32_PIN(38, M10, M, 10),
  78. SWM32_PIN(39, M11, M, 11),
  79. SWM32_PIN(40, M12, M, 12),
  80. SWM32_PIN(41, M13, M, 13),
  81. SWM32_PIN(42, M14, M, 14),
  82. SWM32_PIN(43, M15, M, 15),
  83. SWM32_PIN(44, M16, M, 16),
  84. SWM32_PIN(45, M17, M, 17),
  85. SWM32_PIN(46, M18, M, 18),
  86. SWM32_PIN(47, M19, M, 19),
  87. SWM32_PIN(48, M20, M, 20),
  88. SWM32_PIN(49, M21, M, 21),
  89. SWM32_PIN(50, VDDIO, 0, 0),
  90. SWM32_PIN(51, M1, M, 1),
  91. SWM32_PIN(52, M0, M, 0),
  92. SWM32_PIN(53, P0, P, 0),
  93. SWM32_PIN(54, P1, P, 1),
  94. SWM32_PIN(55, P2, P, 2),
  95. SWM32_PIN(56, P3, P, 3),
  96. SWM32_PIN(57, P4, P, 4),
  97. SWM32_PIN(58, P5, P, 5),
  98. SWM32_PIN(59, P6, P, 6),
  99. SWM32_PIN(60, P7, P, 7),
  100. SWM32_PIN(61, P8, P, 8),
  101. SWM32_PIN(62, P9, P, 9),
  102. SWM32_PIN(63, P10, P, 10),
  103. SWM32_PIN(64, P11, P, 11),
  104. SWM32_PIN(65, P12, P, 12),
  105. SWM32_PIN(66, P13, P, 13),
  106. SWM32_PIN(67, P14, P, 14),
  107. SWM32_PIN(68, P15, P, 15),
  108. SWM32_PIN(69, P16, P, 16),
  109. SWM32_PIN(70, P17, P, 17),
  110. SWM32_PIN(71, P18, P, 18),
  111. SWM32_PIN(72, P19, P, 19),
  112. SWM32_PIN(73, P20, P, 20),
  113. SWM32_PIN(74, P21, P, 21),
  114. SWM32_PIN(75, P22, P, 22),
  115. SWM32_PIN(76, P23, P, 23),
  116. SWM32_PIN(77, B0, B, 0),
  117. SWM32_PIN(78, A0, A, 0),
  118. SWM32_PIN(79, A1, A, 1),
  119. SWM32_PIN(80, A2, A, 2),
  120. SWM32_PIN(81, A3, A, 3),
  121. SWM32_PIN(82, A4, A, 4),
  122. SWM32_PIN(83, A5, A, 5),
  123. SWM32_PIN(84, VSSIO, 0, 0),
  124. SWM32_PIN(85, C1, C, 1),
  125. SWM32_PIN(86, N19, N, 19),
  126. SWM32_PIN(87, N18, N, 18),
  127. SWM32_PIN(88, N17, N, 17),
  128. SWM32_PIN(89, N16, N, 16),
  129. SWM32_PIN(90, N15, N, 15),
  130. SWM32_PIN(91, N8, N, 8),
  131. SWM32_PIN(92, N7, N, 7),
  132. SWM32_PIN(93, N6, N, 6),
  133. SWM32_PIN(94, N5, N, 5),
  134. SWM32_PIN(95, N4, N, 4),
  135. SWM32_PIN(96, N3, N, 3),
  136. SWM32_PIN(97, A9, A, 9),
  137. SWM32_PIN(98, A10, A, 10),
  138. SWM32_PIN(99, A11, A, 11),
  139. SWM32_PIN(100, A12, A, 12)
  140. };
  141. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  142. static pin_t *get_pin(uint8_t pin)
  143. {
  144. pin_t *index;
  145. if (pin < ITEM_NUM(swm32_pin_map))
  146. {
  147. index = (pin_t *)&swm32_pin_map[pin];
  148. if (index->port == GPIO0)
  149. index = RT_NULL;
  150. }
  151. else
  152. {
  153. index = RT_NULL;
  154. }
  155. return index;
  156. };
  157. static void swm320_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  158. {
  159. pin_t *index;
  160. index = get_pin(pin);
  161. if (index == RT_NULL)
  162. {
  163. return;
  164. }
  165. if (value)
  166. {
  167. GPIO_SetBit(index->port, index->group_index);
  168. }
  169. else
  170. {
  171. GPIO_ClrBit(index->port, index->group_index);
  172. }
  173. }
  174. static int swm320_pin_read(rt_device_t dev, rt_base_t pin)
  175. {
  176. pin_t *index;
  177. index = get_pin(pin);
  178. if (index == RT_NULL)
  179. {
  180. return PIN_LOW;
  181. }
  182. return GPIO_GetBit(index->port, index->group_index);
  183. }
  184. static void swm320_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  185. {
  186. pin_t *index;
  187. int dir = 0;
  188. int pull_up = 0;
  189. int pull_down = 0;
  190. index = get_pin(pin);
  191. if (index == RT_NULL)
  192. {
  193. return;
  194. }
  195. /* Configure GPIO_InitStructure */
  196. if (mode == PIN_MODE_OUTPUT)
  197. {
  198. /* output setting */
  199. dir = 1;
  200. }
  201. else if (mode == PIN_MODE_INPUT)
  202. {
  203. /* input setting: not pull. */
  204. dir = 0;
  205. }
  206. else if (mode == PIN_MODE_INPUT_PULLUP)
  207. {
  208. /* input setting: pull up. */
  209. dir = 0;
  210. pull_up = 1;
  211. }
  212. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  213. {
  214. /* input setting: pull down. */
  215. dir = 0;
  216. pull_down = 1;
  217. }
  218. else if (mode == PIN_MODE_OUTPUT_OD)
  219. {
  220. /* output setting: od. */
  221. dir = 1;
  222. pull_up = 1;
  223. }
  224. GPIO_Init(index->port, index->group_index, dir, pull_up, pull_down);
  225. }
  226. static rt_err_t swm320_pin_attach_irq(struct rt_device *device,
  227. rt_int32_t pin,
  228. rt_uint32_t mode,
  229. pin_callback_t cb,
  230. void *args)
  231. {
  232. pin_t *index;
  233. rt_base_t level;
  234. index = get_pin(pin);
  235. if (index == RT_NULL)
  236. {
  237. return RT_EINVAL;
  238. }
  239. level = rt_hw_interrupt_disable();
  240. index->callback = cb;
  241. index->callback_args = args;
  242. index->irq_mode = mode;
  243. rt_hw_interrupt_enable(level);
  244. return RT_EOK;
  245. }
  246. static rt_err_t swm320_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
  247. {
  248. pin_t *index;
  249. rt_base_t level;
  250. index = get_pin(pin);
  251. if (index == RT_NULL)
  252. {
  253. return RT_EINVAL;
  254. }
  255. level = rt_hw_interrupt_disable();
  256. index->callback = 0;
  257. index->callback_args = 0;
  258. index->irq_mode = 0;
  259. rt_hw_interrupt_enable(level);
  260. return RT_EOK;
  261. }
  262. static rt_err_t swm320_pin_irq_enable(struct rt_device *device,
  263. rt_base_t pin,
  264. rt_uint32_t enabled)
  265. {
  266. pin_t *index;
  267. rt_base_t level = 0;
  268. index = get_pin(pin);
  269. if (index == RT_NULL)
  270. {
  271. return RT_EINVAL;
  272. }
  273. if (enabled == PIN_IRQ_ENABLE)
  274. {
  275. switch (index->irq_mode)
  276. {
  277. case PIN_IRQ_MODE_RISING:
  278. GPIO_Init(index->port, index->group_index, 0, 0, 1);
  279. EXTI_Init(index->port, index->group_index, EXTI_RISE_EDGE);
  280. break;
  281. case PIN_IRQ_MODE_FALLING:
  282. GPIO_Init(index->port, index->group_index, 0, 1, 0);
  283. EXTI_Init(index->port, index->group_index, EXTI_FALL_EDGE);
  284. break;
  285. case PIN_IRQ_MODE_RISING_FALLING:
  286. GPIO_Init(index->port, index->group_index, 0, 1, 1);
  287. EXTI_Init(index->port, index->group_index, EXTI_BOTH_EDGE);
  288. break;
  289. case PIN_IRQ_MODE_HIGH_LEVEL:
  290. GPIO_Init(index->port, index->group_index, 0, 0, 1);
  291. EXTI_Init(index->port, index->group_index, EXTI_HIGH_LEVEL);
  292. break;
  293. case PIN_IRQ_MODE_LOW_LEVEL:
  294. GPIO_Init(index->port, index->group_index, 0, 1, 0);
  295. EXTI_Init(index->port, index->group_index, EXTI_LOW_LEVEL);
  296. break;
  297. default:
  298. rt_hw_interrupt_enable(level);
  299. return RT_EINVAL;
  300. }
  301. level = rt_hw_interrupt_disable();
  302. NVIC_EnableIRQ(index->irq);
  303. EXTI_Open(index->port, index->group_index);
  304. rt_hw_interrupt_enable(level);
  305. }
  306. else if (enabled == PIN_IRQ_DISABLE)
  307. {
  308. NVIC_DisableIRQ(index->irq);
  309. EXTI_Close(index->port, index->group_index);
  310. }
  311. else
  312. {
  313. return RT_ENOSYS;
  314. }
  315. return RT_EOK;
  316. }
  317. const static struct rt_pin_ops swm320_pin_ops =
  318. {
  319. swm320_pin_mode,
  320. swm320_pin_write,
  321. swm320_pin_read,
  322. swm320_pin_attach_irq,
  323. swm320_pin_detach_irq,
  324. swm320_pin_irq_enable,
  325. RT_NULL,
  326. };
  327. int rt_hw_pin_init(void)
  328. {
  329. int result;
  330. result = rt_device_pin_register("pin", &swm320_pin_ops, RT_NULL);
  331. return result;
  332. }
  333. INIT_BOARD_EXPORT(rt_hw_pin_init);
  334. void GPIOA_Handler(void)
  335. {
  336. static int gpio[24];
  337. int index = 0;
  338. static int init = 0;
  339. pin_t *pin;
  340. /* enter interrupt */
  341. rt_interrupt_enter();
  342. if (init == 0)
  343. {
  344. init = 1;
  345. for (pin = (pin_t *)&swm32_pin_map[1];
  346. pin->package_index < ITEM_NUM(swm32_pin_map);
  347. pin++)
  348. {
  349. if (pin->port == GPIOA)
  350. {
  351. gpio[index] = pin->package_index;
  352. index++;
  353. RT_ASSERT(index <= 24)
  354. }
  355. }
  356. }
  357. for (index = 0; index < 24; index++)
  358. {
  359. pin = get_pin(gpio[index]);
  360. if (index != RT_NULL)
  361. {
  362. if (EXTI_State(pin->port, pin->group_index))
  363. {
  364. EXTI_Clear(pin->port, pin->group_index);
  365. if (pin->callback)
  366. {
  367. pin->callback(pin->callback_args);
  368. }
  369. }
  370. }
  371. }
  372. /* leave interrupt */
  373. rt_interrupt_leave();
  374. }
  375. void GPIOB_Handler(void)
  376. {
  377. static int gpio[24];
  378. int index = 0;
  379. static int init = 0;
  380. pin_t *pin;
  381. /* enter interrupt */
  382. rt_interrupt_enter();
  383. if (init == 0)
  384. {
  385. init = 1;
  386. for (pin = (pin_t *)&swm32_pin_map[1];
  387. pin->package_index < ITEM_NUM(swm32_pin_map);
  388. pin++)
  389. {
  390. if (pin->port == GPIOB)
  391. {
  392. gpio[index] = pin->package_index;
  393. index++;
  394. RT_ASSERT(index <= 24)
  395. }
  396. }
  397. }
  398. for (index = 0; index < 24; index++)
  399. {
  400. pin = get_pin(gpio[index]);
  401. if (index != RT_NULL)
  402. {
  403. if (EXTI_State(pin->port, pin->group_index))
  404. {
  405. EXTI_Clear(pin->port, pin->group_index);
  406. if (pin->callback)
  407. {
  408. pin->callback(pin->callback_args);
  409. }
  410. }
  411. }
  412. }
  413. /* leave interrupt */
  414. rt_interrupt_leave();
  415. }
  416. void GPIOC_Handler(void)
  417. {
  418. static int gpio[24];
  419. int index = 0;
  420. static int init = 0;
  421. pin_t *pin;
  422. /* enter interrupt */
  423. rt_interrupt_enter();
  424. if (init == 0)
  425. {
  426. init = 1;
  427. for (pin = (pin_t *)&swm32_pin_map[1];
  428. pin->package_index < ITEM_NUM(swm32_pin_map);
  429. pin++)
  430. {
  431. if (pin->port == GPIOC)
  432. {
  433. gpio[index] = pin->package_index;
  434. index++;
  435. RT_ASSERT(index <= 24)
  436. }
  437. }
  438. }
  439. for (index = 0; index < 24; index++)
  440. {
  441. pin = get_pin(gpio[index]);
  442. if (index != RT_NULL)
  443. {
  444. if (EXTI_State(pin->port, pin->group_index))
  445. {
  446. EXTI_Clear(pin->port, pin->group_index);
  447. if (pin->callback)
  448. {
  449. pin->callback(pin->callback_args);
  450. }
  451. }
  452. }
  453. }
  454. /* leave interrupt */
  455. rt_interrupt_leave();
  456. }
  457. void GPIOM_Handler(void)
  458. {
  459. static int gpio[24];
  460. int index = 0;
  461. static int init = 0;
  462. pin_t *pin;
  463. /* enter interrupt */
  464. rt_interrupt_enter();
  465. if (init == 0)
  466. {
  467. init = 1;
  468. for (pin = (pin_t *)&swm32_pin_map[1];
  469. pin->package_index < ITEM_NUM(swm32_pin_map);
  470. pin++)
  471. {
  472. if (pin->port == GPIOM)
  473. {
  474. gpio[index] = pin->package_index;
  475. index++;
  476. RT_ASSERT(index <= 24)
  477. }
  478. }
  479. }
  480. for (index = 0; index < 24; index++)
  481. {
  482. pin = get_pin(gpio[index]);
  483. if (index != RT_NULL)
  484. {
  485. if (EXTI_State(pin->port, pin->group_index))
  486. {
  487. EXTI_Clear(pin->port, pin->group_index);
  488. if (pin->callback)
  489. {
  490. pin->callback(pin->callback_args);
  491. }
  492. }
  493. }
  494. }
  495. /* leave interrupt */
  496. rt_interrupt_leave();
  497. }
  498. void GPION_Handler(void)
  499. {
  500. static int gpio[24];
  501. int index = 0;
  502. static int init = 0;
  503. pin_t *pin;
  504. /* enter interrupt */
  505. rt_interrupt_enter();
  506. if (init == 0)
  507. {
  508. init = 1;
  509. for (pin = (pin_t *)&swm32_pin_map[1];
  510. pin->package_index < ITEM_NUM(swm32_pin_map);
  511. pin++)
  512. {
  513. if (pin->port == GPION)
  514. {
  515. gpio[index] = pin->package_index;
  516. index++;
  517. RT_ASSERT(index <= 24)
  518. }
  519. }
  520. }
  521. for (index = 0; index < 24; index++)
  522. {
  523. pin = get_pin(gpio[index]);
  524. if (index != RT_NULL)
  525. {
  526. if (EXTI_State(pin->port, pin->group_index))
  527. {
  528. EXTI_Clear(pin->port, pin->group_index);
  529. if (pin->callback)
  530. {
  531. pin->callback(pin->callback_args);
  532. }
  533. }
  534. }
  535. }
  536. /* leave interrupt */
  537. rt_interrupt_leave();
  538. }
  539. void GPIOP_Handler(void)
  540. {
  541. static int gpio[24];
  542. int index = 0;
  543. static int init = 0;
  544. pin_t *pin;
  545. /* enter interrupt */
  546. rt_interrupt_enter();
  547. if (init == 0)
  548. {
  549. init = 1;
  550. for (pin = (pin_t *)&swm32_pin_map[1];
  551. pin->package_index < ITEM_NUM(swm32_pin_map);
  552. pin++)
  553. {
  554. if (pin->port == GPIOP)
  555. {
  556. gpio[index] = pin->package_index;
  557. index++;
  558. RT_ASSERT(index <= 24)
  559. }
  560. }
  561. }
  562. for (index = 0; index < 24; index++)
  563. {
  564. pin = get_pin(gpio[index]);
  565. if (index != RT_NULL)
  566. {
  567. if (EXTI_State(pin->port, pin->group_index))
  568. {
  569. EXTI_Clear(pin->port, pin->group_index);
  570. if (pin->callback)
  571. {
  572. pin->callback(pin->callback_args);
  573. }
  574. }
  575. }
  576. }
  577. /* leave interrupt */
  578. rt_interrupt_leave();
  579. }