lpc_gpio.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /**********************************************************************
  2. * $Id$ lpc_gpio.c 2011-06-02
  3. *//**
  4. * @file lpc_gpio.c
  5. * @brief Contains all functions support for GPIO firmware library
  6. * on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup GPIO
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _GPIO
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_gpio.h"
  44. #include "lpc_clkpwr.h"
  45. /* definitions ------------------------------------------------------------------- */
  46. #define GPIO_IS_ENABLED(x) (((x) != 0)? ENABLE:DISABLE)
  47. /* Private Functions ---------------------------------------------------------- */
  48. static LPC_GPIO_TypeDef *GPIO_GetPointer(uint8_t portNum);
  49. static GPIO_HalfWord_TypeDef *FIO_HalfWordGetPointer(uint8_t portNum);
  50. static GPIO_Byte_TypeDef *FIO_ByteGetPointer(uint8_t portNum);
  51. /*********************************************************************//**
  52. * @brief Get pointer to GPIO peripheral due to GPIO port
  53. * @param[in] portNum Port Number value, should be in range from 0 to 4.
  54. * @return Pointer to GPIO peripheral
  55. **********************************************************************/
  56. static LPC_GPIO_TypeDef *GPIO_GetPointer(uint8_t portNum)
  57. {
  58. LPC_GPIO_TypeDef *pGPIO = NULL;
  59. switch (portNum)
  60. {
  61. case 0:
  62. pGPIO = LPC_GPIO0;
  63. break;
  64. case 1:
  65. pGPIO = LPC_GPIO1;
  66. break;
  67. case 2:
  68. pGPIO = LPC_GPIO2;
  69. break;
  70. case 3:
  71. pGPIO = LPC_GPIO3;
  72. break;
  73. case 4:
  74. pGPIO = LPC_GPIO4;
  75. break;
  76. case 5:
  77. pGPIO = LPC_GPIO5;
  78. break;
  79. default:
  80. break;
  81. }
  82. return pGPIO;
  83. }
  84. /*********************************************************************//**
  85. * @brief Get pointer to FIO peripheral in halfword accessible style
  86. * due to FIO port
  87. * @param[in] portNum Port Number value, should be in range from 0 to 4.
  88. * @return Pointer to FIO peripheral
  89. **********************************************************************/
  90. static GPIO_HalfWord_TypeDef *FIO_HalfWordGetPointer(uint8_t portNum)
  91. {
  92. GPIO_HalfWord_TypeDef *pFIO = NULL;
  93. switch (portNum)
  94. {
  95. case 0:
  96. pFIO = GPIO0_HalfWord;
  97. break;
  98. case 1:
  99. pFIO = GPIO1_HalfWord;
  100. break;
  101. case 2:
  102. pFIO = GPIO2_HalfWord;
  103. break;
  104. case 3:
  105. pFIO = GPIO3_HalfWord;
  106. break;
  107. case 4:
  108. pFIO = GPIO4_HalfWord;
  109. break;
  110. case 5:
  111. pFIO = GPIO5_HalfWord;
  112. break;
  113. default:
  114. break;
  115. }
  116. return pFIO;
  117. }
  118. /*********************************************************************//**
  119. * @brief Get pointer to FIO peripheral in byte accessible style
  120. * due to FIO port
  121. * @param[in] portNum Port Number value, should be in range from 0 to 4.
  122. * @return Pointer to FIO peripheral
  123. **********************************************************************/
  124. static GPIO_Byte_TypeDef *FIO_ByteGetPointer(uint8_t portNum)
  125. {
  126. GPIO_Byte_TypeDef *pFIO = NULL;
  127. switch (portNum)
  128. {
  129. case 0:
  130. pFIO = GPIO0_Byte;
  131. break;
  132. case 1:
  133. pFIO = GPIO1_Byte;
  134. break;
  135. case 2:
  136. pFIO = GPIO2_Byte;
  137. break;
  138. case 3:
  139. pFIO = GPIO3_Byte;
  140. break;
  141. case 4:
  142. pFIO = GPIO4_Byte;
  143. break;
  144. case 5:
  145. pFIO = GPIO5_Byte;
  146. break;
  147. default:
  148. break;
  149. }
  150. return pFIO;
  151. }
  152. /* End of Private Functions --------------------------------------------------- */
  153. /* Public Functions ----------------------------------------------------------- */
  154. /** @addtogroup GPIO_Public_Functions
  155. * @{
  156. */
  157. /* GPIO ------------------------------------------------------------------------------ */
  158. /*********************************************************************//**
  159. * @brief Initialize the GPIO component by power on the clock
  160. * supplied to it.
  161. * @return None
  162. *
  163. * Note:
  164. **********************************************************************/
  165. void GPIO_Init(void)
  166. {
  167. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCGPIO, ENABLE);
  168. return;
  169. }
  170. /*********************************************************************//**
  171. * @brief De-initialize the GPIO component by power off the clock
  172. * supplied to it.
  173. * @return None
  174. *
  175. * Note:
  176. **********************************************************************/
  177. void GPIO_Deinit(void)
  178. {
  179. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCGPIO, DISABLE);
  180. return;
  181. }
  182. /*********************************************************************//**
  183. * @brief Set Direction for GPIO port.
  184. * @param[in] portNum Port Number value, should be in range from 0 to 4
  185. * @param[in] bitValue Value that contains all bits to set direction,
  186. * in range from 0 to 0xFFFFFFFF.
  187. * example: value 0x5 to set direction for bit 0 and bit 1.
  188. * @param[in] dir Direction value, should be:
  189. * - 0: Input.
  190. * - 1: Output.
  191. * @return None
  192. *
  193. * Note: All remaining bits that are not activated in bitValue (value '0')
  194. * will not be effected by this function.
  195. **********************************************************************/
  196. void GPIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir)
  197. {
  198. LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);
  199. if (pGPIO != NULL)
  200. {
  201. // Enable Output
  202. if (dir)
  203. {
  204. pGPIO->DIR |= bitValue;
  205. }
  206. // Enable Input
  207. else
  208. {
  209. pGPIO->DIR &= ~bitValue;
  210. }
  211. }
  212. }
  213. /*********************************************************************//**
  214. * @brief Set Value for bits that have output direction on GPIO port.
  215. * @param[in] portNum Port number value, should be in range from 0 to 4
  216. * @param[in] bitValue Value that contains all bits on GPIO to set,
  217. * in range from 0 to 0xFFFFFFFF.
  218. * example: value 0x5 to set bit 0 and bit 1.
  219. * @return None
  220. *
  221. * Note:
  222. * - For all bits that has been set as input direction, this function will
  223. * not effect.
  224. * - For all remaining bits that are not activated in bitValue (value '0')
  225. * will not be effected by this function.
  226. **********************************************************************/
  227. void GPIO_SetValue(uint8_t portNum, uint32_t bitValue)
  228. {
  229. LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);
  230. if (pGPIO != NULL)
  231. {
  232. pGPIO->SET = bitValue;
  233. }
  234. }
  235. /*********************************************************************//**
  236. * @brief Clear Value for bits that have output direction on GPIO port.
  237. * @param[in] portNum Port number value, should be in range from 0 to 4
  238. * @param[in] bitValue Value that contains all bits on GPIO to clear,
  239. * in range from 0 to 0xFFFFFFFF.
  240. * example: value 0x5 to clear bit 0 and bit 1.
  241. * @return None
  242. *
  243. * Note:
  244. * - For all bits that has been set as input direction, this function will
  245. * not effect.
  246. * - For all remaining bits that are not activated in bitValue (value '0')
  247. * will not be effected by this function.
  248. **********************************************************************/
  249. void GPIO_ClearValue(uint8_t portNum, uint32_t bitValue)
  250. {
  251. LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);
  252. if (pGPIO != NULL)
  253. {
  254. pGPIO->CLR = bitValue;
  255. }
  256. }
  257. /*********************************************************************//**
  258. * @brief Output to the GPIO pin an expected value
  259. * @param[in] portNum Port number value, should be in range from 0 to 4
  260. * @param[in] bitValue Value that contains all bits on GPIO to clear,
  261. * in range from 0 to 0xFFFFFFFF.
  262. * example: value 0x5 to clear bit 0 and bit 1.
  263. * @return None
  264. *
  265. * Note:
  266. * - For all bits that has been set as input direction, this function will
  267. * not effect.
  268. * - For all remaining bits that are not activated in bitValue (value '0')
  269. * will not be effected by this function.
  270. **********************************************************************/
  271. void GPIO_OutputValue(uint8_t portNum, uint32_t bitMask, uint8_t value)
  272. {
  273. if (value == 0)
  274. {
  275. GPIO_ClearValue(portNum, bitMask);
  276. }
  277. else
  278. {
  279. GPIO_SetValue(portNum, bitMask);
  280. }
  281. }
  282. /*********************************************************************//**
  283. * @brief Read Current state on port pin that have input direction of GPIO
  284. * @param[in] portNum Port number to read value, in range from 0 to 4
  285. * @return Current value of GPIO port.
  286. *
  287. * Note: Return value contain state of each port pin (bit) on that GPIO regardless
  288. * its direction is input or output.
  289. **********************************************************************/
  290. uint32_t GPIO_ReadValue(uint8_t portNum)
  291. {
  292. LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum);
  293. if (pGPIO != NULL)
  294. {
  295. return pGPIO->PIN;
  296. }
  297. return (0);
  298. }
  299. /*********************************************************************//**
  300. * @brief Enable GPIO interrupt (just used for P0.0-P0.30, P2.0-P2.13)
  301. * @param[in] portNum Port number to read value, should be: 0 or 2
  302. * @param[in] bitValue Value that contains all bits on GPIO to enable,
  303. * in range from 0 to 0xFFFFFFFF.
  304. * @param[in] edgeState state of edge, should be:
  305. * - 0: Rising edge
  306. * - 1: Falling edge
  307. * @return None
  308. **********************************************************************/
  309. void GPIO_IntCmd(uint8_t portNum, uint32_t bitValue, uint8_t edgeState)
  310. {
  311. if((portNum == 0)&&(edgeState == 0))
  312. LPC_GPIOINT->IO0IntEnR = bitValue;
  313. else if ((portNum == 2)&&(edgeState == 0))
  314. LPC_GPIOINT->IO2IntEnR = bitValue;
  315. else if ((portNum == 0)&&(edgeState == 1))
  316. LPC_GPIOINT->IO0IntEnF = bitValue;
  317. else if ((portNum == 2)&&(edgeState == 1))
  318. LPC_GPIOINT->IO2IntEnF = bitValue;
  319. else
  320. //Error
  321. while(1);
  322. }
  323. /*********************************************************************//**
  324. * @brief Get GPIO Interrupt Status (just used for P0.0-P0.30, P2.0-P2.13)
  325. * @param[in] portNum Port number to read value, should be: 0 or 2
  326. * @param[in] pinNum Pin number, should be: 0..30(with port 0) and 0..13
  327. * (with port 2)
  328. * @param[in] edgeState state of edge, should be:
  329. * - 0: Rising edge
  330. * - 1: Falling edge
  331. * @return Bool could be:
  332. * - ENABLE: Interrupt has been generated due to a rising
  333. * edge on P0.0
  334. * - DISABLE: A rising edge has not been detected on P0.0
  335. **********************************************************************/
  336. FunctionalState GPIO_GetIntStatus(uint8_t portNum, uint32_t pinNum, uint8_t edgeState)
  337. {
  338. if((portNum == 0) && (edgeState == 0))//Rising Edge
  339. return GPIO_IS_ENABLED(((LPC_GPIOINT->IO0IntStatR)>>pinNum)& 0x1);
  340. else if ((portNum == 2) && (edgeState == 0))
  341. return GPIO_IS_ENABLED(((LPC_GPIOINT->IO2IntStatR)>>pinNum)& 0x1);
  342. else if ((portNum == 0) && (edgeState == 1))//Falling Edge
  343. return GPIO_IS_ENABLED(((LPC_GPIOINT->IO0IntStatF)>>pinNum)& 0x1);
  344. else if ((portNum == 2) && (edgeState == 1))
  345. return GPIO_IS_ENABLED(((LPC_GPIOINT->IO2IntStatF)>>pinNum)& 0x1);
  346. else
  347. //Error
  348. while(1);
  349. }
  350. /*********************************************************************//**
  351. * @brief Clear GPIO interrupt (just used for P0.0-P0.30, P2.0-P2.13)
  352. * @param[in] portNum Port number to read value, should be: 0 or 2
  353. * @param[in] bitValue Value that contains all bits on GPIO to enable,
  354. * in range from 0 to 0xFFFFFFFF.
  355. * @return None
  356. **********************************************************************/
  357. void GPIO_ClearInt(uint8_t portNum, uint32_t bitValue)
  358. {
  359. if(portNum == 0)
  360. LPC_GPIOINT->IO0IntClr = bitValue;
  361. else if (portNum == 2)
  362. LPC_GPIOINT->IO2IntClr = bitValue;
  363. else
  364. //Invalid portNum
  365. while(1);
  366. }
  367. /* FIO word accessible ----------------------------------------------------------------- */
  368. /* Stub function for FIO (word-accessible) style */
  369. /**
  370. * @brief The same with GPIO_SetDir()
  371. */
  372. void FIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir)
  373. {
  374. GPIO_SetDir(portNum, bitValue, dir);
  375. }
  376. /**
  377. * @brief The same with GPIO_SetValue()
  378. */
  379. void FIO_SetValue(uint8_t portNum, uint32_t bitValue)
  380. {
  381. GPIO_SetValue(portNum, bitValue);
  382. }
  383. /**
  384. * @brief The same with GPIO_ClearValue()
  385. */
  386. void FIO_ClearValue(uint8_t portNum, uint32_t bitValue)
  387. {
  388. GPIO_ClearValue(portNum, bitValue);
  389. }
  390. /**
  391. * @brief The same with GPIO_ReadValue()
  392. */
  393. uint32_t FIO_ReadValue(uint8_t portNum)
  394. {
  395. return (GPIO_ReadValue(portNum));
  396. }
  397. /**
  398. * @brief The same with GPIO_IntCmd()
  399. */
  400. void FIO_IntCmd(uint8_t portNum, uint32_t bitValue, uint8_t edgeState)
  401. {
  402. GPIO_IntCmd(portNum, bitValue, edgeState);
  403. }
  404. /**
  405. * @brief The same with GPIO_GetIntStatus()
  406. */
  407. FunctionalState FIO_GetIntStatus(uint8_t portNum, uint32_t pinNum, uint8_t edgeState)
  408. {
  409. return (GPIO_GetIntStatus(portNum, pinNum, edgeState));
  410. }
  411. /**
  412. * @brief The same with GPIO_ClearInt()
  413. */
  414. void FIO_ClearInt(uint8_t portNum, uint32_t bitValue)
  415. {
  416. GPIO_ClearInt(portNum, bitValue);
  417. }
  418. /*********************************************************************//**
  419. * @brief Set mask value for bits in FIO port
  420. * @param[in] portNum Port number, in range from 0 to 4
  421. * @param[in] bitValue Value that contains all bits in to set,
  422. * in range from 0 to 0xFFFFFFFF.
  423. * @param[in] maskValue Mask value contains state value for each bit:
  424. * - 0: not mask.
  425. * - 1: mask.
  426. * @return None
  427. *
  428. * Note:
  429. * - All remaining bits that are not activated in bitValue (value '0')
  430. * will not be effected by this function.
  431. * - After executing this function, in mask register, value '0' on each bit
  432. * enables an access to the corresponding physical pin via a read or write access,
  433. * while value '1' on bit (masked) that corresponding pin will not be changed
  434. * with write access and if read, will not be reflected in the updated pin.
  435. **********************************************************************/
  436. void FIO_SetMask(uint8_t portNum, uint32_t bitValue, uint8_t maskValue)
  437. {
  438. LPC_GPIO_TypeDef *pFIO = GPIO_GetPointer(portNum);
  439. if(pFIO != NULL)
  440. {
  441. // Mask
  442. if (maskValue)
  443. {
  444. pFIO->MASK |= bitValue;
  445. }
  446. // Un-mask
  447. else
  448. {
  449. pFIO->MASK &= ~bitValue;
  450. }
  451. }
  452. }
  453. /* FIO halfword accessible ------------------------------------------------------------- */
  454. /*********************************************************************//**
  455. * @brief Set direction for FIO port in halfword accessible style
  456. * @param[in] portNum Port number, in range from 0 to 4
  457. * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
  458. * @param[in] bitValue Value that contains all bits in to set direction,
  459. * in range from 0 to 0xFFFF.
  460. * @param[in] dir Direction value, should be:
  461. * - 0: Input.
  462. * - 1: Output.
  463. * @return None
  464. *
  465. * Note: All remaining bits that are not activated in bitValue (value '0')
  466. * will not be effected by this function.
  467. **********************************************************************/
  468. void FIO_HalfWordSetDir(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t dir)
  469. {
  470. GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
  471. if(pFIO != NULL)
  472. {
  473. // Output direction
  474. if (dir)
  475. {
  476. // Upper
  477. if(halfwordNum)
  478. {
  479. pFIO->FIODIRU |= bitValue;
  480. }
  481. // lower
  482. else
  483. {
  484. pFIO->FIODIRL |= bitValue;
  485. }
  486. }
  487. // Input direction
  488. else
  489. {
  490. // Upper
  491. if(halfwordNum)
  492. {
  493. pFIO->FIODIRU &= ~bitValue;
  494. }
  495. // lower
  496. else
  497. {
  498. pFIO->FIODIRL &= ~bitValue;
  499. }
  500. }
  501. }
  502. }
  503. /*********************************************************************//**
  504. * @brief Set mask value for bits in FIO port in halfword accessible style
  505. * @param[in] portNum Port number, in range from 0 to 4
  506. * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
  507. * @param[in] bitValue Value that contains all bits in to set,
  508. * in range from 0 to 0xFFFF.
  509. * @param[in] maskValue Mask value contains state value for each bit:
  510. * - 0: not mask.
  511. * - 1: mask.
  512. * @return None
  513. *
  514. * Note:
  515. * - All remaining bits that are not activated in bitValue (value '0')
  516. * will not be effected by this function.
  517. * - After executing this function, in mask register, value '0' on each bit
  518. * enables an access to the corresponding physical pin via a read or write access,
  519. * while value '1' on bit (masked) that corresponding pin will not be changed
  520. * with write access and if read, will not be reflected in the updated pin.
  521. **********************************************************************/
  522. void FIO_HalfWordSetMask(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t maskValue)
  523. {
  524. GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
  525. if(pFIO != NULL)
  526. {
  527. // Mask
  528. if (maskValue)
  529. {
  530. // Upper
  531. if(halfwordNum)
  532. {
  533. pFIO->FIOMASKU |= bitValue;
  534. }
  535. // lower
  536. else
  537. {
  538. pFIO->FIOMASKL |= bitValue;
  539. }
  540. }
  541. // Un-mask
  542. else
  543. {
  544. // Upper
  545. if(halfwordNum)
  546. {
  547. pFIO->FIOMASKU &= ~bitValue;
  548. }
  549. // lower
  550. else
  551. {
  552. pFIO->FIOMASKL &= ~bitValue;
  553. }
  554. }
  555. }
  556. }
  557. /*********************************************************************//**
  558. * @brief Set bits for FIO port in halfword accessible style
  559. * @param[in] portNum Port number, in range from 0 to 4
  560. * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
  561. * @param[in] bitValue Value that contains all bits in to set,
  562. * in range from 0 to 0xFFFF.
  563. * @return None
  564. *
  565. * Note:
  566. * - For all bits that has been set as input direction, this function will
  567. * not effect.
  568. * - For all remaining bits that are not activated in bitValue (value '0')
  569. * will not be effected by this function.
  570. **********************************************************************/
  571. void FIO_HalfWordSetValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue)
  572. {
  573. GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
  574. if(pFIO != NULL)
  575. {
  576. // Upper
  577. if(halfwordNum)
  578. {
  579. pFIO->FIOSETU = bitValue;
  580. }
  581. // lower
  582. else
  583. {
  584. pFIO->FIOSETL = bitValue;
  585. }
  586. }
  587. }
  588. /*********************************************************************//**
  589. * @brief Clear bits for FIO port in halfword accessible style
  590. * @param[in] portNum Port number, in range from 0 to 4
  591. * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
  592. * @param[in] bitValue Value that contains all bits in to clear,
  593. * in range from 0 to 0xFFFF.
  594. * @return None
  595. *
  596. * Note:
  597. * - For all bits that has been set as input direction, this function will
  598. * not effect.
  599. * - For all remaining bits that are not activated in bitValue (value '0')
  600. * will not be effected by this function.
  601. **********************************************************************/
  602. void FIO_HalfWordClearValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue)
  603. {
  604. GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
  605. if(pFIO != NULL)
  606. {
  607. // Upper
  608. if(halfwordNum)
  609. {
  610. pFIO->FIOCLRU = bitValue;
  611. }
  612. // lower
  613. else
  614. {
  615. pFIO->FIOCLRL = bitValue;
  616. }
  617. }
  618. }
  619. /*********************************************************************//**
  620. * @brief Read Current state on port pin that have input direction of GPIO
  621. * in halfword accessible style.
  622. * @param[in] portNum Port number, in range from 0 to 4
  623. * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
  624. * @return Current value of FIO port pin of specified halfword.
  625. * Note: Return value contain state of each port pin (bit) on that FIO regardless
  626. * its direction is input or output.
  627. **********************************************************************/
  628. uint16_t FIO_HalfWordReadValue(uint8_t portNum, uint8_t halfwordNum)
  629. {
  630. GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
  631. if(pFIO != NULL)
  632. {
  633. // Upper
  634. if(halfwordNum)
  635. {
  636. return (pFIO->FIOPINU);
  637. }
  638. // lower
  639. else
  640. {
  641. return (pFIO->FIOPINL);
  642. }
  643. }
  644. return (0);
  645. }
  646. /* FIO Byte accessible ------------------------------------------------------------ */
  647. /*********************************************************************//**
  648. * @brief Set direction for FIO port in byte accessible style
  649. * @param[in] portNum Port number, in range from 0 to 4
  650. * @param[in] byteNum Byte part number, should be in range from 0 to 3
  651. * @param[in] bitValue Value that contains all bits in to set direction,
  652. * in range from 0 to 0xFF.
  653. * @param[in] dir Direction value, should be:
  654. * - 0: Input.
  655. * - 1: Output.
  656. * @return None
  657. *
  658. * Note: All remaining bits that are not activated in bitValue (value '0')
  659. * will not be effected by this function.
  660. **********************************************************************/
  661. void FIO_ByteSetDir(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t dir)
  662. {
  663. GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
  664. if(pFIO != NULL)
  665. {
  666. // Output direction
  667. if (dir)
  668. {
  669. if (byteNum <= 3)
  670. {
  671. pFIO->FIODIR[byteNum] |= bitValue;
  672. }
  673. }
  674. // Input direction
  675. else
  676. {
  677. if (byteNum <= 3)
  678. {
  679. pFIO->FIODIR[byteNum] &= ~bitValue;
  680. }
  681. }
  682. }
  683. }
  684. /*********************************************************************//**
  685. * @brief Set mask value for bits in FIO port in byte accessible style
  686. * @param[in] portNum Port number, in range from 0 to 4
  687. * @param[in] byteNum Byte part number, should be in range from 0 to 3
  688. * @param[in] bitValue Value that contains all bits in to set mask,
  689. * in range from 0 to 0xFF.
  690. * @param[in] maskValue Mask value contains state value for each bit:
  691. * - 0: not mask.
  692. * - 1: mask.
  693. * @return None
  694. *
  695. * Note:
  696. * - All remaining bits that are not activated in bitValue (value '0')
  697. * will not be effected by this function.
  698. * - After executing this function, in mask register, value '0' on each bit
  699. * enables an access to the corresponding physical pin via a read or write access,
  700. * while value '1' on bit (masked) that corresponding pin will not be changed
  701. * with write access and if read, will not be reflected in the updated pin.
  702. **********************************************************************/
  703. void FIO_ByteSetMask(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t maskValue)
  704. {
  705. GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
  706. if(pFIO != NULL)
  707. {
  708. // Mask
  709. if (maskValue)
  710. {
  711. if (byteNum <= 3)
  712. {
  713. pFIO->FIOMASK[byteNum] |= bitValue;
  714. }
  715. }
  716. // Un-mask
  717. else {
  718. if (byteNum <= 3)
  719. {
  720. pFIO->FIOMASK[byteNum] &= ~bitValue;
  721. }
  722. }
  723. }
  724. }
  725. /*********************************************************************//**
  726. * @brief Set bits for FIO port in byte accessible style
  727. * @param[in] portNum Port number, in range from 0 to 4
  728. * @param[in] byteNum Byte part number, should be in range from 0 to 3
  729. * @param[in] bitValue Value that contains all bits in to set,
  730. * in range from 0 to 0xFF.
  731. * @return None
  732. *
  733. * Note:
  734. * - For all bits that has been set as input direction, this function will
  735. * not effect.
  736. * - For all remaining bits that are not activated in bitValue (value '0')
  737. * will not be effected by this function.
  738. **********************************************************************/
  739. void FIO_ByteSetValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue)
  740. {
  741. GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
  742. if (pFIO != NULL) {
  743. if (byteNum <= 3)
  744. {
  745. pFIO->FIOSET[byteNum] = bitValue;
  746. }
  747. }
  748. }
  749. /*********************************************************************//**
  750. * @brief Clear bits for FIO port in byte accessible style
  751. * @param[in] portNum Port number, in range from 0 to 4
  752. * @param[in] byteNum Byte part number, should be in range from 0 to 3
  753. * @param[in] bitValue Value that contains all bits in to clear,
  754. * in range from 0 to 0xFF.
  755. * @return None
  756. *
  757. * Note:
  758. * - For all bits that has been set as input direction, this function will
  759. * not effect.
  760. * - For all remaining bits that are not activated in bitValue (value '0')
  761. * will not be effected by this function.
  762. **********************************************************************/
  763. void FIO_ByteClearValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue)
  764. {
  765. GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
  766. if (pFIO != NULL)
  767. {
  768. if (byteNum <= 3)
  769. {
  770. pFIO->FIOCLR[byteNum] = bitValue;
  771. }
  772. }
  773. }
  774. /*********************************************************************//**
  775. * @brief Read Current state on port pin that have input direction of GPIO
  776. * in byte accessible style.
  777. * @param[in] portNum Port number, in range from 0 to 4
  778. * @param[in] byteNum Byte part number, should be in range from 0 to 3
  779. * @return Current value of FIO port pin of specified byte part.
  780. * Note: Return value contain state of each port pin (bit) on that FIO regardless
  781. * its direction is input or output.
  782. **********************************************************************/
  783. uint8_t FIO_ByteReadValue(uint8_t portNum, uint8_t byteNum)
  784. {
  785. GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
  786. if (pFIO != NULL)
  787. {
  788. if (byteNum <= 3)
  789. {
  790. return (pFIO->FIOPIN[byteNum]);
  791. }
  792. }
  793. return (0);
  794. }
  795. /**
  796. * @}
  797. */
  798. #endif /* _GPIO */
  799. /**
  800. * @}
  801. */
  802. /* --------------------------------- End Of File ------------------------------ */