You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1640 lines
39 KiB

4 years ago
  1. /*********************************************************
  2. *Copyright (C), 2015, Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *: lib_gpio.c
  4. * : AE Team
  5. * : V1.01
  6. * : 2021/06/09
  7. * : GPIO模块库函数
  8. * :
  9. 使
  10. **********************************************************/
  11. #include <stdint.h>
  12. #include "lib_gpio.h"
  13. /***************************************************************
  14. GPIO_SetFuncxRegFromPin
  15. GPIO引脚的功能复用
  16. Pin
  17. Func
  18. ***************************************************************/
  19. void GPIO_SetFuncxRegFromPin(GPIO_Pin Pin, GPIO_Reuse_Func Func)
  20. {
  21. uint32_t value;
  22. switch (Func)
  23. {
  24. case GPIO_Reuse_Func0:
  25. value = 0;
  26. break;
  27. case GPIO_Reuse_Func1:
  28. value = 1;
  29. break;
  30. case GPIO_Reuse_Func2:
  31. value = 2;
  32. break;
  33. case GPIO_Reuse_Func3:
  34. value = 3;
  35. break;
  36. default:
  37. value = 0xFF;
  38. break;
  39. }
  40. /* 引脚功能设置 */
  41. switch (Pin)
  42. {
  43. case GPIO_Pin_B0:
  44. GPIO->PBFUNC0.PB0 = value;
  45. break;
  46. case GPIO_Pin_B1:
  47. GPIO->PBFUNC0.PB1 = value;
  48. break;
  49. case GPIO_Pin_B8:
  50. GPIO->PBFUNC1.PB8 = value;
  51. break;
  52. case GPIO_Pin_B9:
  53. GPIO->PBFUNC1.PB9 = value;
  54. break;
  55. case GPIO_Pin_B10:
  56. GPIO->PBFUNC1.PB10 = value;
  57. break;
  58. case GPIO_Pin_B11:
  59. GPIO->PBFUNC1.PB11 = value;
  60. break;
  61. case GPIO_Pin_B12:
  62. GPIO->PBFUNC1.PB12 = value;
  63. break;
  64. case GPIO_Pin_B13:
  65. GPIO->PBFUNC1.PB13 = value;
  66. break;
  67. case GPIO_Pin_A1:
  68. GPIO->PAFUNC0.PA1 = value;
  69. break;
  70. case GPIO_Pin_A2:
  71. GPIO->PAFUNC0.PA2 = value;
  72. break;
  73. case GPIO_Pin_A3:
  74. GPIO->PAFUNC0.PA3 = value;
  75. break;
  76. case GPIO_Pin_A4:
  77. GPIO->PAFUNC0.PA4 = value;
  78. break;
  79. case GPIO_Pin_A5:
  80. GPIO->PAFUNC0.PA5 = value;
  81. break;
  82. case GPIO_Pin_A6:
  83. GPIO->PAFUNC0.PA6 = value;
  84. break;
  85. case GPIO_Pin_A7:
  86. GPIO->PAFUNC0.PA7 = value;
  87. break;
  88. case GPIO_Pin_A8:
  89. GPIO->PAFUNC1.PA8 = value;
  90. break;
  91. case GPIO_Pin_A9:
  92. GPIO->PAFUNC1.PA9 = value;
  93. break;
  94. case GPIO_Pin_A10:
  95. GPIO->PAFUNC1.PA10 = value;
  96. break;
  97. case GPIO_Pin_A11:
  98. GPIO->PAFUNC1.PA11 = value;
  99. break;
  100. case GPIO_Pin_A12:
  101. GPIO->PAFUNC1.PA12 = value;
  102. break;
  103. case GPIO_Pin_A13:
  104. GPIO->PAFUNC1.PA13 = value;
  105. break;
  106. case GPIO_Pin_A14:
  107. GPIO->PAFUNC1.PA14 = value;
  108. break;
  109. case GPIO_Pin_A15:
  110. GPIO->PAFUNC1.PA15 = value;
  111. break;
  112. case GPIO_Pin_A16:
  113. GPIO->PAFUNC2.PA16 = value;
  114. break;
  115. case GPIO_Pin_A22:
  116. GPIO->PAFUNC2.PA22 = value;
  117. break;
  118. case GPIO_Pin_A23:
  119. GPIO->PAFUNC2.PA23 = value;
  120. break;
  121. case GPIO_Pin_A24:
  122. GPIO->PAFUNC3.PA24 = value;
  123. break;
  124. case GPIO_Pin_A25:
  125. GPIO->PAFUNC3.PA25 = value;
  126. break;
  127. case GPIO_Pin_A27:
  128. GPIO->PAFUNC3.PA27 = value;
  129. break;
  130. case GPIO_Pin_A28:
  131. GPIO->PAFUNC3.PA28 = value;
  132. break;
  133. default:
  134. break;
  135. }
  136. return;
  137. }
  138. /***************************************************************
  139. GPIO_GetNormalBitOffsetFromPin
  140. Pin:
  141. ***************************************************************/
  142. uint8_t GPIO_GetNormalBitOffsetFromPin(GPIO_Pin Pin)
  143. {
  144. uint8_t result;
  145. switch (Pin)
  146. {
  147. case GPIO_Pin_B0:
  148. result = 0;
  149. break;
  150. case GPIO_Pin_B1:
  151. result = 1;
  152. break;
  153. case GPIO_Pin_B8:
  154. result = 8;
  155. break;
  156. case GPIO_Pin_B9:
  157. result = 9;
  158. break;
  159. case GPIO_Pin_B10:
  160. result = 10;
  161. break;
  162. case GPIO_Pin_B11:
  163. result = 11;
  164. break;
  165. case GPIO_Pin_B12:
  166. result = 12;
  167. break;
  168. case GPIO_Pin_B13:
  169. result = 13;
  170. break;
  171. case GPIO_Pin_A1:
  172. result = 1;
  173. break;
  174. case GPIO_Pin_A2:
  175. result = 2;
  176. break;
  177. case GPIO_Pin_A3:
  178. result = 3;
  179. break;
  180. case GPIO_Pin_A4:
  181. result = 4;
  182. break;
  183. case GPIO_Pin_A5:
  184. result = 5;
  185. break;
  186. case GPIO_Pin_A6:
  187. result = 6;
  188. break;
  189. case GPIO_Pin_A7:
  190. result = 7;
  191. break;
  192. case GPIO_Pin_A8:
  193. result = 8;
  194. break;
  195. case GPIO_Pin_A9:
  196. result = 9;
  197. break;
  198. case GPIO_Pin_A10:
  199. result = 10;
  200. break;
  201. case GPIO_Pin_A11:
  202. result = 11;
  203. break;
  204. case GPIO_Pin_A12:
  205. result = 12;
  206. break;
  207. case GPIO_Pin_A13:
  208. result = 13;
  209. break;
  210. case GPIO_Pin_A14:
  211. result = 14;
  212. break;
  213. case GPIO_Pin_A15:
  214. result = 15;
  215. break;
  216. case GPIO_Pin_A16:
  217. result = 16;
  218. break;
  219. case GPIO_Pin_A22:
  220. result = 22;
  221. break;
  222. case GPIO_Pin_A23:
  223. result = 23;
  224. break;
  225. case GPIO_Pin_A24:
  226. result = 24;
  227. break;
  228. case GPIO_Pin_A25:
  229. result = 25;
  230. break;
  231. case GPIO_Pin_A27:
  232. result = 27;
  233. break;
  234. case GPIO_Pin_A28:
  235. result = 28;
  236. break;
  237. default:
  238. result = 0xFF;
  239. break;
  240. }
  241. return result;
  242. }
  243. /***************************************************************
  244. GPIO_SetDirRegFromPin
  245. Pin:
  246. Dir
  247. ***************************************************************/
  248. void GPIO_SetDirRegFromPin(GPIO_Pin Pin, GPIO_Direction Dir)
  249. {
  250. uint8_t StartBit;
  251. StartBit = GPIO_GetNormalBitOffsetFromPin(Pin);
  252. if (Pin <= GPIO_Pin_B13)
  253. {
  254. if (Dir == GPIO_Direction_Input)
  255. GPIO->PBDIRBSR.Word = (1 << StartBit);
  256. else
  257. GPIO->PBDIRBCR.Word = (1 << StartBit);
  258. }
  259. else
  260. {
  261. if (Dir == GPIO_Direction_Input)
  262. GPIO->PADIRBSR.Word = (1 << StartBit);
  263. else
  264. GPIO->PADIRBCR.Word = (1 << StartBit);
  265. }
  266. return;
  267. }
  268. /***************************************************************
  269. GPIO_SetODERegFromPin
  270. Pin:
  271. ODE:
  272. ***************************************************************/
  273. void GPIO_SetODERegFromPin(GPIO_Pin Pin, GPIO_ODE_Output ODE)
  274. {
  275. uint8_t StartBit;
  276. StartBit = GPIO_GetNormalBitOffsetFromPin(Pin);
  277. if (Pin <= GPIO_Pin_B13)
  278. {
  279. if (ODE == GPIO_ODE_Output_Disable)
  280. GPIO->PBODE.Word &= (~(1uL << StartBit));
  281. else
  282. GPIO->PBODE.Word |= (1uL << StartBit);
  283. }
  284. else
  285. {
  286. if (ODE == GPIO_ODE_Output_Disable)
  287. GPIO->PAODE.Word &= (~(1uL << StartBit));
  288. else
  289. GPIO->PAODE.Word |= (1uL << StartBit);
  290. }
  291. return;
  292. }
  293. /***************************************************************
  294. GPIO_SetDSRegFromPin
  295. Pin:
  296. DS:
  297. ***************************************************************/
  298. void GPIO_SetDSRegFromPin(GPIO_Pin Pin, GPIO_DS_Output DS)
  299. {
  300. uint8_t Start_Bit;
  301. Start_Bit = GPIO_GetNormalBitOffsetFromPin(Pin);
  302. if (Pin <= GPIO_Pin_B13)
  303. {
  304. if (DS == GPIO_DS_Output_Normal)
  305. GPIO->PBDS.Word &= (~(1uL << Start_Bit));
  306. else
  307. GPIO->PBDS.Word |= (1uL << Start_Bit);
  308. }
  309. else
  310. {
  311. if (DS == GPIO_DS_Output_Normal)
  312. GPIO->PADS.Word &= (~(1uL << Start_Bit));
  313. else
  314. GPIO->PADS.Word |= (1uL << Start_Bit);
  315. }
  316. return;
  317. }
  318. /***************************************************************
  319. GPIO_SetPUERegFromPin
  320. Pin:
  321. PUE:
  322. ***************************************************************/
  323. void GPIO_SetPUERegFromPin(GPIO_Pin Pin, GPIO_PUE_Input PUE)
  324. {
  325. uint8_t Start_Bit;
  326. Start_Bit = GPIO_GetNormalBitOffsetFromPin(Pin);
  327. if (Pin <= GPIO_Pin_B13)
  328. {
  329. if (PUE == GPIO_PUE_Input_Disable)
  330. GPIO->PBPUE.Word &= (~(1uL << Start_Bit));
  331. else
  332. GPIO->PBPUE.Word |= (1uL << Start_Bit);
  333. }
  334. else
  335. {
  336. if (PUE == GPIO_PUE_Input_Disable)
  337. GPIO->PAPUE.Word &= (~(1uL << Start_Bit));
  338. else
  339. GPIO->PAPUE.Word |= (1uL << Start_Bit);
  340. }
  341. }
  342. /***************************************************************
  343. GPIO_SetPDERegFromPin
  344. Pin:
  345. PDE
  346. ***************************************************************/
  347. void GPIO_SetPDERegFromPin(GPIO_Pin Pin, GPIO_PDE_Input PDE)
  348. {
  349. uint8_t Start_Bit;
  350. Start_Bit = GPIO_GetNormalBitOffsetFromPin(Pin);
  351. if (Pin <= GPIO_Pin_B13)
  352. {
  353. if (PDE == GPIO_PDE_Input_Disable)
  354. GPIO->PBPDE.Word &= (~(1uL << Start_Bit));
  355. else
  356. GPIO->PBPDE.Word |= (1uL << Start_Bit);
  357. }
  358. else
  359. {
  360. if (PDE == GPIO_PDE_Input_Disable)
  361. GPIO->PAPDE.Word &= (~(1uL << Start_Bit));
  362. else
  363. GPIO->PAPDE.Word |= (1uL << Start_Bit);
  364. }
  365. return;
  366. }
  367. /***************************************************************
  368. GPIO_SetSingalTypeFromPin
  369. Pin:
  370. Signal:
  371. ***************************************************************/
  372. void GPIO_SetSingalTypeFromPin(GPIO_Pin Pin, GPIO_Pin_Signal Signal)
  373. {
  374. uint8_t Start_Bit;
  375. Start_Bit = GPIO_GetNormalBitOffsetFromPin(Pin);
  376. if (Pin <= GPIO_Pin_B13)
  377. {
  378. if (Signal == GPIO_Pin_Signal_Digital)
  379. GPIO->PBINEB.Word &= (~(1uL << Start_Bit));
  380. else
  381. GPIO->PBINEB.Word |= (1uL << Start_Bit);
  382. }
  383. else
  384. {
  385. if (Signal == GPIO_Pin_Signal_Digital)
  386. GPIO->PAINEB.Word &= (~(1uL << Start_Bit));
  387. else
  388. GPIO->PAINEB.Word |= (1uL << Start_Bit);
  389. }
  390. return;
  391. }
  392. /***************************************************************
  393. GPIO_SetPortTypeFromPin
  394. Pin:
  395. Type:
  396. ***************************************************************/
  397. void GPIO_SetPortTypeFromPin(GPIO_Pin Pin, GPIO_Pin_Type Type)
  398. {
  399. uint8_t Start_Bit;
  400. Start_Bit = GPIO_GetNormalBitOffsetFromPin(Pin);
  401. if (Pin <= GPIO_Pin_B13)
  402. {
  403. if (Type == GPIO_Pin_CMOS)
  404. GPIO->PBTYP.Word &= (~(1uL << Start_Bit));
  405. else
  406. GPIO->PBTYP.Word |= (1uL << Start_Bit);
  407. }
  408. else
  409. {
  410. if (Type == GPIO_Pin_CMOS)
  411. GPIO->PATYP.Word &= (~(1uL << Start_Bit));
  412. else
  413. GPIO->PATYP.Word |= (1uL << Start_Bit);
  414. }
  415. return;
  416. }
  417. /***************************************************************
  418. GPIO_SetPortFLTFromPin
  419. Pin:
  420. FLT:
  421. ***************************************************************/
  422. void GPIO_SetPortFLTFromPin(GPIO_Pin Pin, GPIO_Pin_FLT FLT)
  423. {
  424. uint8_t Start_Bit;
  425. Start_Bit = GPIO_GetNormalBitOffsetFromPin(Pin);
  426. if (Pin <= GPIO_Pin_B13)
  427. {
  428. if (FLT == GPIO_FLT_Disable)
  429. GPIO->PBFLT.Word &= (~(1uL << Start_Bit));
  430. else
  431. GPIO->PBFLT.Word |= (1uL << Start_Bit);
  432. }
  433. else
  434. {
  435. if (FLT == GPIO_FLT_Disable)
  436. GPIO->PAFLT.Word &= (~(1uL << Start_Bit));
  437. else
  438. GPIO->PAFLT.Word |= (1uL << Start_Bit);
  439. }
  440. return;
  441. }
  442. /***************************************************************
  443. GPIO_Init
  444. Pin:
  445. InitSet:
  446. ***************************************************************/
  447. void GPIO_Init(GPIO_Pin Pin, GPIO_InitSettingType *InitSet)
  448. {
  449. if (InitSet->Signal == GPIO_Pin_Signal_Analog)
  450. {
  451. GPIO_SetSingalTypeFromPin(Pin, GPIO_Pin_Signal_Analog);
  452. GPIO_SetDirRegFromPin(Pin, GPIO_Direction_Input);
  453. GPIO_SetFuncxRegFromPin(Pin, GPIO_Reuse_Func0);
  454. GPIO_SetPUERegFromPin(Pin, GPIO_PUE_Input_Disable);
  455. GPIO_SetPDERegFromPin(Pin, GPIO_PDE_Input_Disable);
  456. }
  457. else
  458. {
  459. GPIO_SetSingalTypeFromPin(Pin, GPIO_Pin_Signal_Digital);
  460. GPIO_SetFuncxRegFromPin(Pin, InitSet->Func);
  461. GPIO_SetDirRegFromPin(Pin, InitSet->Dir);
  462. if (InitSet->Dir == GPIO_Direction_Output)
  463. {
  464. GPIO_SetDSRegFromPin(Pin, InitSet->DS);
  465. GPIO_SetODERegFromPin(Pin, InitSet->ODE);
  466. }
  467. GPIO_SetPortTypeFromPin(Pin, GPIO_Pin_TTL);
  468. GPIO_SetPUERegFromPin(Pin, InitSet->PUE);
  469. GPIO_SetPDERegFromPin(Pin, InitSet->PDE);
  470. }
  471. GPIO_SetPortFLTFromPin(Pin, GPIO_FLT_Disable);
  472. }
  473. /***************************************************************
  474. GPIO_ReadBit
  475. Pin:
  476. ***************************************************************/
  477. uint32_t GPIO_ReadBit(GPIO_Pin Pin)
  478. {
  479. uint8_t Start_Bit;
  480. uint32_t value;
  481. Start_Bit = GPIO_GetNormalBitOffsetFromPin(Pin);
  482. if (Pin <= GPIO_Pin_B13)
  483. {
  484. value = GPIO->PBPORT.Word;
  485. }
  486. else
  487. {
  488. value = GPIO->PAPORT.Word;
  489. }
  490. value >>= Start_Bit;
  491. value &= 0x01;
  492. return value;
  493. }
  494. /***************************************************************
  495. GPIO_WriteBit
  496. Pin:
  497. bit:
  498. ***************************************************************/
  499. void GPIO_WriteBit(GPIO_Pin Pin, uint32_t bit)
  500. {
  501. uint8_t StartBit;
  502. StartBit = GPIO_GetNormalBitOffsetFromPin(Pin);
  503. if (Pin <= GPIO_Pin_B13)
  504. {
  505. if (bit == 1)
  506. GPIO->PBDATABSR.Word = (1uL << StartBit);
  507. else
  508. GPIO->PBDATABCR.Word = (1uL << StartBit);
  509. }
  510. else
  511. {
  512. if (bit == 1)
  513. GPIO->PADATABSR.Word = (1uL << StartBit);
  514. else
  515. GPIO->PADATABCR.Word = (1uL << StartBit);
  516. }
  517. return;
  518. }
  519. /***************************************************************
  520. GPIO_SetBit
  521. Pin:
  522. ***************************************************************/
  523. void GPIO_SetBit(GPIO_Pin Pin)
  524. {
  525. uint8_t StartBit;
  526. StartBit = GPIO_GetNormalBitOffsetFromPin(Pin);
  527. if (Pin <= GPIO_Pin_B13)
  528. {
  529. GPIO->PBDATABSR.Word = (1uL << StartBit);
  530. }
  531. else
  532. {
  533. GPIO->PADATABSR.Word = (1uL << StartBit);
  534. }
  535. return;
  536. }
  537. /***************************************************************
  538. GPIO_ResetBit
  539. Pin:
  540. ***************************************************************/
  541. void GPIO_ResetBit(GPIO_Pin Pin)
  542. {
  543. uint8_t StartBit;
  544. StartBit = GPIO_GetNormalBitOffsetFromPin(Pin);
  545. if (Pin <= GPIO_Pin_B13)
  546. {
  547. GPIO->PBDATABCR.Word = (1uL << StartBit);
  548. }
  549. else
  550. {
  551. GPIO->PADATABCR.Word = (1uL << StartBit);
  552. }
  553. return;
  554. }
  555. /***************************************************************
  556. GPIO_ToggleBit
  557. Pin:
  558. ***************************************************************/
  559. void GPIO_ToggleBit(GPIO_Pin Pin)
  560. {
  561. uint8_t StartBit;
  562. StartBit = GPIO_GetNormalBitOffsetFromPin(Pin);
  563. if (Pin <= GPIO_Pin_B13)
  564. {
  565. GPIO->PBDATABRR.Word = (1uL << StartBit);
  566. }
  567. else
  568. {
  569. GPIO->PADATABRR.Word = (1uL << StartBit);
  570. }
  571. return;
  572. }
  573. /***************************************************************
  574. PINT_GetPINTxIndexFromPin
  575. Pin:
  576. ***************************************************************/
  577. uint8_t PINT_GetPINTxIndexFromPin(GPIO_Pin Pin)
  578. {
  579. uint8_t index;
  580. switch (Pin)
  581. {
  582. case GPIO_Pin_A8:
  583. index = 0;
  584. break;
  585. case GPIO_Pin_A16:
  586. index = 0;
  587. break;
  588. case GPIO_Pin_A24:
  589. index = 0;
  590. break;
  591. case GPIO_Pin_B0:
  592. index = 0;
  593. break;
  594. case GPIO_Pin_B8:
  595. index = 0;
  596. break;
  597. case GPIO_Pin_A1:
  598. index = 1;
  599. break;
  600. case GPIO_Pin_A9:
  601. index = 1;
  602. break;
  603. case GPIO_Pin_A25:
  604. index = 1;
  605. break;
  606. case GPIO_Pin_B1:
  607. index = 1;
  608. break;
  609. case GPIO_Pin_B9:
  610. index = 1;
  611. break;
  612. case GPIO_Pin_A2:
  613. index = 2;
  614. break;
  615. case GPIO_Pin_A10:
  616. index = 2;
  617. break;
  618. case GPIO_Pin_B10:
  619. index = 2;
  620. break;
  621. case GPIO_Pin_A3:
  622. index = 3;
  623. break;
  624. case GPIO_Pin_A11:
  625. index = 3;
  626. break;
  627. case GPIO_Pin_A27:
  628. index = 3;
  629. break;
  630. case GPIO_Pin_B11:
  631. index = 3;
  632. break;
  633. case GPIO_Pin_A4:
  634. index = 4;
  635. break;
  636. case GPIO_Pin_A12:
  637. index = 4;
  638. break;
  639. case GPIO_Pin_A28:
  640. index = 4;
  641. break;
  642. case GPIO_Pin_B12:
  643. index = 4;
  644. break;
  645. case GPIO_Pin_A5:
  646. index = 5;
  647. break;
  648. case GPIO_Pin_A13:
  649. index = 5;
  650. break;
  651. case GPIO_Pin_B13:
  652. index = 5;
  653. break;
  654. case GPIO_Pin_A6:
  655. index = 6;
  656. break;
  657. case GPIO_Pin_A14:
  658. index = 6;
  659. break;
  660. case GPIO_Pin_A22:
  661. index = 6;
  662. break;
  663. case GPIO_Pin_A7:
  664. index = 7;
  665. break;
  666. case GPIO_Pin_A15:
  667. index = 7;
  668. break;
  669. case GPIO_Pin_A23:
  670. index = 7;
  671. break;
  672. default:
  673. index = 0xFF;
  674. break;
  675. }
  676. return index;
  677. }
  678. /***************************************************************
  679. PINT_SetTriggerStyleFromPin
  680. Pin:
  681. Style:
  682. ***************************************************************/
  683. void PINT_SetTriggerStyleFromPin(GPIO_Pin Pin, PINT_Trigger_Style Style)
  684. {
  685. uint32_t value;
  686. uint8_t index;
  687. index = PINT_GetPINTxIndexFromPin(Pin);
  688. value = GPIO->PINTCFG.Word;
  689. value &= (~(7uL << (index * 4)));
  690. switch (Style)
  691. {
  692. case PINT_Trigger_Rising_Edge:
  693. value |= (0uL << (index * 4));
  694. break;
  695. case PINT_Trigger_Trailing_Edge:
  696. value |= (1uL << (index * 4));
  697. break;
  698. case PINT_Trigger_High_Level:
  699. value |= (2uL << (index * 4));
  700. break;
  701. case PINT_Trigger_Low_Level:
  702. value |= (3uL << (index * 4));
  703. break;
  704. case PINT_Trigger_Both_Edge:
  705. value |= (4uL << (index * 4));
  706. break;
  707. default:
  708. break;
  709. }
  710. GPIO->PINTCFG.Word = value;
  711. return;
  712. }
  713. /***************************************************************
  714. PINT_GetSELxIndexFromPin
  715. Pin:
  716. ***************************************************************/
  717. uint8_t PINT_GetSELxIndexFromPin(GPIO_Pin Pin)
  718. {
  719. uint8_t index;
  720. switch (Pin)
  721. {
  722. case GPIO_Pin_A1:
  723. index = 0;
  724. break;
  725. case GPIO_Pin_A2:
  726. index = 0;
  727. break;
  728. case GPIO_Pin_A3:
  729. index = 0;
  730. break;
  731. case GPIO_Pin_A4:
  732. index = 0;
  733. break;
  734. case GPIO_Pin_A5:
  735. index = 0;
  736. break;
  737. case GPIO_Pin_A6:
  738. index = 0;
  739. break;
  740. case GPIO_Pin_A7:
  741. index = 0;
  742. break;
  743. case GPIO_Pin_A8:
  744. index = 1;
  745. break;
  746. case GPIO_Pin_A9:
  747. index = 1;
  748. break;
  749. case GPIO_Pin_A10:
  750. index = 1;
  751. break;
  752. case GPIO_Pin_A11:
  753. index = 1;
  754. break;
  755. case GPIO_Pin_A12:
  756. index = 1;
  757. break;
  758. case GPIO_Pin_A13:
  759. index = 1;
  760. break;
  761. case GPIO_Pin_A14:
  762. index = 1;
  763. break;
  764. case GPIO_Pin_A15:
  765. index = 1;
  766. break;
  767. case GPIO_Pin_A16:
  768. index = 2;
  769. break;
  770. case GPIO_Pin_A22:
  771. index = 2;
  772. break;
  773. case GPIO_Pin_A23:
  774. index = 2;
  775. break;
  776. case GPIO_Pin_A24:
  777. index = 3;
  778. break;
  779. case GPIO_Pin_A25:
  780. index = 3;
  781. break;
  782. case GPIO_Pin_A27:
  783. index = 3;
  784. break;
  785. case GPIO_Pin_A28:
  786. index = 3;
  787. break;
  788. case GPIO_Pin_B0:
  789. index = 4;
  790. break;
  791. case GPIO_Pin_B1:
  792. index = 4;
  793. break;
  794. case GPIO_Pin_B8:
  795. index = 5;
  796. break;
  797. case GPIO_Pin_B9:
  798. index = 5;
  799. break;
  800. case GPIO_Pin_B10:
  801. index = 5;
  802. break;
  803. case GPIO_Pin_B11:
  804. index = 5;
  805. break;
  806. case GPIO_Pin_B12:
  807. index = 5;
  808. break;
  809. case GPIO_Pin_B13:
  810. index = 5;
  811. break;
  812. default:
  813. index = 0xFF;
  814. break;
  815. }
  816. return index;
  817. }
  818. /***************************************************************
  819. PINT_SetEnableOrNotFromPin
  820. 使
  821. Pin:
  822. IE: 使
  823. ***************************************************************/
  824. void PINT_SetEnableOrNotFromPin(GPIO_Pin Pin, PINT_IE_Set IE)
  825. {
  826. uint8_t Pintx;
  827. uint8_t Selx;
  828. uint32_t value;
  829. /* 选择中断源 */
  830. Pintx = PINT_GetPINTxIndexFromPin(Pin);
  831. Selx = PINT_GetSELxIndexFromPin(Pin);
  832. value = GPIO->PINTSEL.Word;
  833. value &= (~(7uL << (Pintx * 4))); /* 清零PINTx输入选择位 */
  834. value |= (Selx << (Pintx * 4)); /* 置位PINTx输入选择位 */
  835. GPIO->PINTSEL.Word = value;
  836. /* 清除中断标志 */
  837. GPIO->PINTIF.Word = (1uL << Pintx);
  838. /* 使能PINT中断 */
  839. value = GPIO->PINTIE.Word;
  840. if (IE == PINT_IE_Set_Disable)
  841. {
  842. value &= (~(1uL << Pintx)); /* 关闭使能位 */
  843. value |= (1uL << (Pintx + 8)); /* 屏蔽中断 */
  844. GPIO->PINTIE.Word = value;
  845. NVIC->ICER[0] = (1uL << Pintx);
  846. }
  847. else
  848. {
  849. value |= (1uL << Pintx); /* 打开使能位 */
  850. value &= (~(1uL << (Pintx + 8))); /* 使能中断 */
  851. GPIO->PINTIE.Word = value;
  852. NVIC->ISER[0] = (1uL << Pintx);
  853. }
  854. return;
  855. }
  856. /***************************************************************
  857. PINT_Init
  858. Pin:
  859. InitSet:
  860. ***************************************************************/
  861. void PINT_Init(GPIO_Pin Pin, PINT_InitSettingType *InitSet)
  862. {
  863. PINT_SetTriggerStyleFromPin(Pin, InitSet->Trigger_Style);
  864. PINT_SetEnableOrNotFromPin(Pin, InitSet->IE_Set);
  865. }
  866. /***************************************************************
  867. PINT_ClearITFlag
  868. Pin
  869. ***************************************************************/
  870. void PINT_ClearITFlag(GPIO_Pin Pin)
  871. {
  872. uint8_t pintx;
  873. pintx = PINT_GetPINTxIndexFromPin(Pin);
  874. GPIO->PINTIF.Word = (0x01U << pintx);
  875. return;
  876. }
  877. /*************************************
  878. PINT_GetITStatus
  879. 使
  880. Pin:
  881. 使
  882. **************************************/
  883. uint32_t PINT_GetITStatus(GPIO_Pin Pin)
  884. {
  885. uint8_t Pintx;
  886. uint32_t value;
  887. Pintx = PINT_GetPINTxIndexFromPin(Pin);
  888. value = GPIO->PINTIE.Word;
  889. value >>= Pintx;
  890. value &= 0x01;
  891. return value;
  892. }
  893. /***************************************************************
  894. PINT_GetITFlag
  895. Pin:
  896. ***************************************************************/
  897. uint32_t PINT_GetITFlag(GPIO_Pin Pin)
  898. {
  899. uint8_t Pintx;
  900. uint32_t value;
  901. Pintx = PINT_GetPINTxIndexFromPin(Pin);
  902. value = GPIO->PINTIF.Word;
  903. value >>= Pintx;
  904. value &= 0x01;
  905. return value;
  906. }
  907. /***************************************************************
  908. PINT_SetMASK
  909. Pin:
  910. IE:
  911. ***************************************************************/
  912. void PINT_SetPMASK(GPIO_Pin Pin, PINT_IE_Set IE)
  913. {
  914. uint8_t Pintx;
  915. if (PINT_IE_Set_Disable == IE)
  916. {
  917. Pintx = PINT_GetPINTxIndexFromPin(Pin);
  918. GPIO->PINTIE.PMASK &= ~(1uL << Pintx);
  919. }
  920. else
  921. {
  922. Pintx = PINT_GetPINTxIndexFromPin(Pin);
  923. GPIO->PINTIE.PMASK |= (1uL << Pintx);
  924. }
  925. return;
  926. }
  927. /***************************************************************
  928. PINT_SetMASK
  929. Pin:
  930. IE:
  931. 10
  932. ***************************************************************/
  933. uint32_t PINT_GetPMASK(GPIO_Pin Pin)
  934. {
  935. uint8_t Pintx;
  936. uint32_t value;
  937. Pintx = PINT_GetPINTxIndexFromPin(Pin);
  938. value = GPIO->PINTIE.PMASK >> Pintx;
  939. value &= 0x01;
  940. return value;
  941. }
  942. /***************************************************************
  943. KINT_GetKINTxIndexFromPin
  944. Pin:
  945. ***************************************************************/
  946. uint8_t KINT_GetKINTxIndexFromPin(GPIO_Pin Pin)
  947. {
  948. uint8_t index;
  949. switch (Pin)
  950. {
  951. case GPIO_Pin_A8:
  952. case GPIO_Pin_A16:
  953. case GPIO_Pin_A24:
  954. case GPIO_Pin_B0:
  955. case GPIO_Pin_B8:
  956. index = 0;
  957. break;
  958. case GPIO_Pin_A1:
  959. case GPIO_Pin_A9:
  960. case GPIO_Pin_A25:
  961. case GPIO_Pin_B1:
  962. case GPIO_Pin_B9:
  963. index = 1;
  964. break;
  965. case GPIO_Pin_A2:
  966. case GPIO_Pin_A10:
  967. case GPIO_Pin_B10:
  968. index = 2;
  969. break;
  970. case GPIO_Pin_A3:
  971. case GPIO_Pin_A11:
  972. case GPIO_Pin_A27:
  973. case GPIO_Pin_B11:
  974. index = 3;
  975. break;
  976. case GPIO_Pin_A4:
  977. case GPIO_Pin_A12:
  978. case GPIO_Pin_A28:
  979. case GPIO_Pin_B12:
  980. index = 4;
  981. break;
  982. case GPIO_Pin_A5:
  983. case GPIO_Pin_A13:
  984. case GPIO_Pin_B13:
  985. index = 5;
  986. break;
  987. case GPIO_Pin_A6:
  988. case GPIO_Pin_A14:
  989. case GPIO_Pin_A22:
  990. index = 6;
  991. break;
  992. case GPIO_Pin_A7:
  993. case GPIO_Pin_A15:
  994. case GPIO_Pin_A23:
  995. index = 7;
  996. break;
  997. default:
  998. index = 0xFF;
  999. break;
  1000. }
  1001. return index;
  1002. }
  1003. /***************************************************************
  1004. KINT_SetTriggerStyleFromPin
  1005. Pin:
  1006. Style:
  1007. ***************************************************************/
  1008. void KINT_SetTriggerStyleFromPin(GPIO_Pin Pin, KINT_Trigger_Style Style)
  1009. {
  1010. uint8_t Kintx;
  1011. uint32_t value;
  1012. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1013. value = GPIO->KINTCFG.Word;
  1014. value &= (~(7uL << (Kintx * 4)));
  1015. switch (Style)
  1016. {
  1017. case KINT_Trigger_Rising_Edge:
  1018. value |= (0uL << (Kintx * 4));
  1019. break;
  1020. case KINT_Trigger_Trailing_Edge:
  1021. value |= (1uL << (Kintx * 4));
  1022. break;
  1023. case KINT_Trigger_High_Level:
  1024. value |= (2uL << (Kintx * 4));
  1025. break;
  1026. case KINT_Trigger_Low_Level:
  1027. value |= (3uL << (Kintx * 4));
  1028. break;
  1029. case KINT_Trigger_Both_Edge:
  1030. value |= (4uL << (Kintx * 4));
  1031. break;
  1032. default:
  1033. break;
  1034. }
  1035. GPIO->KINTCFG.Word = value;
  1036. return;
  1037. }
  1038. /***************************************************************
  1039. KINT_GetSelxIndexFromPin
  1040. Pin:
  1041. ***************************************************************/
  1042. uint8_t KINT_GetSelxIndexFromPin(GPIO_Pin Pin)
  1043. {
  1044. uint8_t index;
  1045. switch (Pin)
  1046. {
  1047. case GPIO_Pin_A1:
  1048. case GPIO_Pin_A2:
  1049. case GPIO_Pin_A3:
  1050. case GPIO_Pin_A4:
  1051. case GPIO_Pin_A5:
  1052. case GPIO_Pin_A6:
  1053. case GPIO_Pin_A7:
  1054. index = 0;
  1055. break;
  1056. case GPIO_Pin_A8:
  1057. case GPIO_Pin_A9:
  1058. case GPIO_Pin_A10:
  1059. case GPIO_Pin_A11:
  1060. case GPIO_Pin_A12:
  1061. case GPIO_Pin_A13:
  1062. case GPIO_Pin_A14:
  1063. case GPIO_Pin_A15:
  1064. index = 1;
  1065. break;
  1066. case GPIO_Pin_A16:
  1067. case GPIO_Pin_A22:
  1068. case GPIO_Pin_A23:
  1069. index = 2;
  1070. break;
  1071. case GPIO_Pin_A24:
  1072. case GPIO_Pin_A25:
  1073. case GPIO_Pin_A27:
  1074. case GPIO_Pin_A28:
  1075. index = 3;
  1076. break;
  1077. case GPIO_Pin_B0:
  1078. case GPIO_Pin_B1:
  1079. index = 4;
  1080. break;
  1081. case GPIO_Pin_B8:
  1082. case GPIO_Pin_B9:
  1083. case GPIO_Pin_B10:
  1084. case GPIO_Pin_B11:
  1085. case GPIO_Pin_B12:
  1086. case GPIO_Pin_B13:
  1087. index = 5;
  1088. break;
  1089. default:
  1090. index = 0xFF;
  1091. break;
  1092. }
  1093. return index;
  1094. }
  1095. /***************************************************************
  1096. KINT_SetEnableOrNotFromPin
  1097. 使
  1098. Pin:
  1099. IE: 使
  1100. ***************************************************************/
  1101. void KINT_SetEnableOrNotFromPin(GPIO_Pin Pin, KINT_IE_Set IE)
  1102. {
  1103. uint8_t Kintx;
  1104. uint8_t Selx;
  1105. uint32_t value;
  1106. /* 中断输入引脚选择 */
  1107. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1108. Selx = KINT_GetSelxIndexFromPin(Pin);
  1109. value = GPIO->KINTSEL.Word;
  1110. value &= (~(7uL << (Kintx * 4)));
  1111. value |= (Selx << (Kintx * 4));
  1112. GPIO->KINTSEL.Word = value;
  1113. /* 清除中断标志 */
  1114. value = (1uL << Kintx);
  1115. GPIO->KINTIF.Word = value;
  1116. /* 使能PINT中断 */
  1117. value = GPIO->KINTIE.Word;
  1118. if (IE == KINT_IE_Set_Disable)
  1119. {
  1120. value &= (~(1uL << Kintx)); /* 清除使能位 */
  1121. value |= (1uL << (Kintx + 8)); /* 屏蔽中断 */
  1122. GPIO->KINTIE.Word = value;
  1123. NVIC->ICER[0] = (1uL << 18);
  1124. }
  1125. else
  1126. {
  1127. value |= (1uL << Kintx); /* 设置使能位 */
  1128. value &= (~(1uL << (Kintx + 8))); /* 不屏蔽中断 */
  1129. GPIO->KINTIE.Word = value;
  1130. NVIC->ISER[0] = (1uL << 18);
  1131. }
  1132. return;
  1133. }
  1134. /***************************************************************
  1135. KINT_Init
  1136. Pin:
  1137. InitSet:
  1138. ***************************************************************/
  1139. void KINT_Init(GPIO_Pin Pin, KINT_InitSettingType *InitSet)
  1140. {
  1141. KINT_SetTriggerStyleFromPin(Pin, InitSet->Trigger_Style);
  1142. KINT_SetEnableOrNotFromPin(Pin, InitSet->IE_Set);
  1143. }
  1144. /***************************************************************
  1145. KINT_ClearITFlag
  1146. Pin:
  1147. ***************************************************************/
  1148. void KINT_ClearITFlag(GPIO_Pin Pin)
  1149. {
  1150. uint8_t Kintx;
  1151. uint32_t value;
  1152. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1153. value = (1uL << Kintx);
  1154. GPIO->KINTIF.Word = value;
  1155. return;
  1156. }
  1157. /*************************************
  1158. KINT_GetITStatus
  1159. 使
  1160. Pin:
  1161. 使
  1162. **************************************/
  1163. uint32_t KINT_GetITStatus(GPIO_Pin Pin)
  1164. {
  1165. uint8_t Kintx;
  1166. uint32_t value;
  1167. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1168. value = GPIO->KINTIE.Word;
  1169. value >>= Kintx;
  1170. value &= 0x01;
  1171. return value;
  1172. }
  1173. /***************************************************************
  1174. KINT_GetITFlag
  1175. Pin:
  1176. ***************************************************************/
  1177. uint32_t KINT_GetITFlag(GPIO_Pin Pin)
  1178. {
  1179. uint8_t Kintx;
  1180. uint32_t value;
  1181. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1182. value = GPIO->KINTIF.Word;
  1183. value >>= Kintx;
  1184. value &= 0x01;
  1185. return value;
  1186. }
  1187. /***************************************************************
  1188. KINT_SetKMASK
  1189. Pin:
  1190. IE:
  1191. ***************************************************************/
  1192. void KINT_SetKMASK(GPIO_Pin Pin, KINT_IE_Set IE)
  1193. {
  1194. uint8_t Kintx;
  1195. if (KINT_IE_Set_Disable == IE)
  1196. {
  1197. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1198. GPIO->KINTIE.KMASK &= ~(1uL << Kintx);
  1199. }
  1200. else
  1201. {
  1202. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1203. GPIO->KINTIE.KMASK |= (1uL << Kintx);
  1204. }
  1205. return;
  1206. }
  1207. /***************************************************************
  1208. KINT_GetKMASK
  1209. Pin:
  1210. IE:
  1211. 10
  1212. ***************************************************************/
  1213. uint32_t KINT_GetKMASK(GPIO_Pin Pin)
  1214. {
  1215. uint8_t Kintx;
  1216. uint32_t value;
  1217. Kintx = KINT_GetKINTxIndexFromPin(Pin);
  1218. value = GPIO->KINTIE.KMASK >> Kintx;
  1219. value &= 0x01;
  1220. return value;
  1221. }
  1222. /***************************************************************
  1223. UART_TXxConfig
  1224. TXx脉宽调制输出配置
  1225. Plv
  1226. PsPWM脉冲选择
  1227. TxSx:
  1228. NewState Disable或Enable
  1229. ***************************************************************/
  1230. void GPIO_TX0Config(UART_TYPE_TXPLV Plv, UART_TYPE_TX0PS Ps, UART_TYPE_TX0Sx TX0Sx, TYPE_FUNCEN NewState)
  1231. {
  1232. GPIO->TXPWM.TX0PS = Ps;
  1233. GPIO->TXPWM.TX0PLV = Plv;
  1234. if (Ps == UART_TX0PS_NO)
  1235. return;
  1236. switch (TX0Sx)
  1237. {
  1238. case UART_TYPE_TXD0:
  1239. GPIO->TXPWM.TX0_S0 = NewState;
  1240. break;
  1241. case UART_TYPE_T16N0OUT0:
  1242. GPIO->TXPWM.TX0_S1 = NewState;
  1243. break;
  1244. case UART_TYPE_T16N0OUT1:
  1245. GPIO->TXPWM.TX0_S2 = NewState;
  1246. break;
  1247. case UART_TYPE_TX0BUZ:
  1248. GPIO->TXPWM.TX0_S3 = NewState;
  1249. break;
  1250. }
  1251. }