drv_gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. };
  326. int rt_hw_pin_init(void)
  327. {
  328. int result;
  329. result = rt_device_pin_register("pin", &swm320_pin_ops, RT_NULL);
  330. return result;
  331. }
  332. INIT_BOARD_EXPORT(rt_hw_pin_init);
  333. void GPIOA_Handler(void)
  334. {
  335. static int gpio[24];
  336. int index = 0;
  337. static int init = 0;
  338. pin_t *pin;
  339. /* enter interrupt */
  340. rt_interrupt_enter();
  341. if (init == 0)
  342. {
  343. init = 1;
  344. for (pin = (pin_t *)&swm32_pin_map[1];
  345. pin->package_index < ITEM_NUM(swm32_pin_map);
  346. pin++)
  347. {
  348. if (pin->port == GPIOA)
  349. {
  350. gpio[index] = pin->package_index;
  351. index++;
  352. RT_ASSERT(index <= 24)
  353. }
  354. }
  355. }
  356. for (index = 0; index < 24; index++)
  357. {
  358. pin = get_pin(gpio[index]);
  359. if (index != RT_NULL)
  360. {
  361. if (EXTI_State(pin->port, pin->group_index))
  362. {
  363. EXTI_Clear(pin->port, pin->group_index);
  364. if (pin->callback)
  365. {
  366. pin->callback(pin->callback_args);
  367. }
  368. }
  369. }
  370. }
  371. /* leave interrupt */
  372. rt_interrupt_leave();
  373. }
  374. void GPIOB_Handler(void)
  375. {
  376. static int gpio[24];
  377. int index = 0;
  378. static int init = 0;
  379. pin_t *pin;
  380. /* enter interrupt */
  381. rt_interrupt_enter();
  382. if (init == 0)
  383. {
  384. init = 1;
  385. for (pin = (pin_t *)&swm32_pin_map[1];
  386. pin->package_index < ITEM_NUM(swm32_pin_map);
  387. pin++)
  388. {
  389. if (pin->port == GPIOB)
  390. {
  391. gpio[index] = pin->package_index;
  392. index++;
  393. RT_ASSERT(index <= 24)
  394. }
  395. }
  396. }
  397. for (index = 0; index < 24; index++)
  398. {
  399. pin = get_pin(gpio[index]);
  400. if (index != RT_NULL)
  401. {
  402. if (EXTI_State(pin->port, pin->group_index))
  403. {
  404. EXTI_Clear(pin->port, pin->group_index);
  405. if (pin->callback)
  406. {
  407. pin->callback(pin->callback_args);
  408. }
  409. }
  410. }
  411. }
  412. /* leave interrupt */
  413. rt_interrupt_leave();
  414. }
  415. void GPIOC_Handler(void)
  416. {
  417. static int gpio[24];
  418. int index = 0;
  419. static int init = 0;
  420. pin_t *pin;
  421. /* enter interrupt */
  422. rt_interrupt_enter();
  423. if (init == 0)
  424. {
  425. init = 1;
  426. for (pin = (pin_t *)&swm32_pin_map[1];
  427. pin->package_index < ITEM_NUM(swm32_pin_map);
  428. pin++)
  429. {
  430. if (pin->port == GPIOC)
  431. {
  432. gpio[index] = pin->package_index;
  433. index++;
  434. RT_ASSERT(index <= 24)
  435. }
  436. }
  437. }
  438. for (index = 0; index < 24; index++)
  439. {
  440. pin = get_pin(gpio[index]);
  441. if (index != RT_NULL)
  442. {
  443. if (EXTI_State(pin->port, pin->group_index))
  444. {
  445. EXTI_Clear(pin->port, pin->group_index);
  446. if (pin->callback)
  447. {
  448. pin->callback(pin->callback_args);
  449. }
  450. }
  451. }
  452. }
  453. /* leave interrupt */
  454. rt_interrupt_leave();
  455. }
  456. void GPIOM_Handler(void)
  457. {
  458. static int gpio[24];
  459. int index = 0;
  460. static int init = 0;
  461. pin_t *pin;
  462. /* enter interrupt */
  463. rt_interrupt_enter();
  464. if (init == 0)
  465. {
  466. init = 1;
  467. for (pin = (pin_t *)&swm32_pin_map[1];
  468. pin->package_index < ITEM_NUM(swm32_pin_map);
  469. pin++)
  470. {
  471. if (pin->port == GPIOM)
  472. {
  473. gpio[index] = pin->package_index;
  474. index++;
  475. RT_ASSERT(index <= 24)
  476. }
  477. }
  478. }
  479. for (index = 0; index < 24; index++)
  480. {
  481. pin = get_pin(gpio[index]);
  482. if (index != RT_NULL)
  483. {
  484. if (EXTI_State(pin->port, pin->group_index))
  485. {
  486. EXTI_Clear(pin->port, pin->group_index);
  487. if (pin->callback)
  488. {
  489. pin->callback(pin->callback_args);
  490. }
  491. }
  492. }
  493. }
  494. /* leave interrupt */
  495. rt_interrupt_leave();
  496. }
  497. void GPION_Handler(void)
  498. {
  499. static int gpio[24];
  500. int index = 0;
  501. static int init = 0;
  502. pin_t *pin;
  503. /* enter interrupt */
  504. rt_interrupt_enter();
  505. if (init == 0)
  506. {
  507. init = 1;
  508. for (pin = (pin_t *)&swm32_pin_map[1];
  509. pin->package_index < ITEM_NUM(swm32_pin_map);
  510. pin++)
  511. {
  512. if (pin->port == GPION)
  513. {
  514. gpio[index] = pin->package_index;
  515. index++;
  516. RT_ASSERT(index <= 24)
  517. }
  518. }
  519. }
  520. for (index = 0; index < 24; index++)
  521. {
  522. pin = get_pin(gpio[index]);
  523. if (index != RT_NULL)
  524. {
  525. if (EXTI_State(pin->port, pin->group_index))
  526. {
  527. EXTI_Clear(pin->port, pin->group_index);
  528. if (pin->callback)
  529. {
  530. pin->callback(pin->callback_args);
  531. }
  532. }
  533. }
  534. }
  535. /* leave interrupt */
  536. rt_interrupt_leave();
  537. }
  538. void GPIOP_Handler(void)
  539. {
  540. static int gpio[24];
  541. int index = 0;
  542. static int init = 0;
  543. pin_t *pin;
  544. /* enter interrupt */
  545. rt_interrupt_enter();
  546. if (init == 0)
  547. {
  548. init = 1;
  549. for (pin = (pin_t *)&swm32_pin_map[1];
  550. pin->package_index < ITEM_NUM(swm32_pin_map);
  551. pin++)
  552. {
  553. if (pin->port == GPIOP)
  554. {
  555. gpio[index] = pin->package_index;
  556. index++;
  557. RT_ASSERT(index <= 24)
  558. }
  559. }
  560. }
  561. for (index = 0; index < 24; index++)
  562. {
  563. pin = get_pin(gpio[index]);
  564. if (index != RT_NULL)
  565. {
  566. if (EXTI_State(pin->port, pin->group_index))
  567. {
  568. EXTI_Clear(pin->port, pin->group_index);
  569. if (pin->callback)
  570. {
  571. pin->callback(pin->callback_args);
  572. }
  573. }
  574. }
  575. }
  576. /* leave interrupt */
  577. rt_interrupt_leave();
  578. }