lpc177x_8x_pinsel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**********************************************************************
  2. * $Id$ lpc177x_8x_pinsel.c 2011-06-02
  3. *//**
  4. * @file lpc177x_8x_pinsel.c
  5. * @brief Contains all functions support for Pin-connection block
  6. * firmware library on LPC177x_8x
  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. **********************************************************************/
  26. /* Peripheral group ----------------------------------------------------------- */
  27. /** @addtogroup PINSEL
  28. * @{
  29. */
  30. /* Includes ------------------------------------------------------------------- */
  31. #include "lpc177x_8x_pinsel.h"
  32. #define PINSEL_I2C_MODE_POS (8)
  33. #define PINSEL_I2C_MODE_NUMBITS (2)
  34. #define PINSEL_I2C_MODE_BITMASK (0x03)
  35. #define PINSEL_BASIC_MODE_POS (3)
  36. #define PINSEL_BASIC_MODE_NUMBITS (2)
  37. #define PINSEL_BASIC_MODE_BITMASK (0x03)
  38. #define PINSEL_DACEN_POS (16)
  39. #define PINSEL_DACEN_BITMASK (0x01)
  40. #define PINSEL_DACEN_NUMBITS (1)
  41. #define PINSEL_GLITCH_FILTER_POS (8)
  42. #define PINSEL_GLITCH_FILTER_BITMASK (0x01)
  43. #define PINSEL_GLITCH_FILTER_NUMBITS (1)
  44. #define PINSEL_ADMODE_POS (7)
  45. #define PINSEL_ADMODE_BITMASK (0x01)
  46. #define PINSEL_ADMODE_NUMBITS (1)
  47. /* Private Functions ---------------------------------------------------------- */
  48. /*********************************************************************//**
  49. * @brief Get pointer to GPIO peripheral due to GPIO port
  50. * @param[in] portnum Port Number value, should be in range from 0..3.
  51. * @param[in] pinnum Pin number value, should be in range from 0..31
  52. * @return Pointer to GPIO peripheral
  53. **********************************************************************/
  54. static uint32_t * PIN_GetPointer(uint8_t portnum, uint8_t pinnum)
  55. {
  56. uint32_t *pPIN = NULL;
  57. pPIN = (uint32_t *)(LPC_IOCON_BASE + ((portnum * 32 + pinnum)*sizeof(uint32_t)));
  58. return pPIN;
  59. }
  60. /* Public Functions ----------------------------------------------------------- */
  61. /** @addtogroup PINSEL_Public_Functions
  62. * @{
  63. */
  64. /*********************************************************************//**
  65. * @brief Setup the pin selection function
  66. * @param[in] portnum PORT number, should be in range: 0..3
  67. * @param[in] pinnum Pin number, should be in range: 0..31
  68. * @param[in] funcnum Function number, should be range: 0..7
  69. * - 0: Select GPIO (Default)
  70. * - 1: Selects the 1st alternate function
  71. * - 2: Selects the 2nd alternate function
  72. * ...
  73. * - 7: Selects the 7th alternate function
  74. * @return None
  75. **********************************************************************/
  76. void PINSEL_ConfigPin ( uint8_t portnum, uint8_t pinnum, uint8_t funcnum)
  77. {
  78. uint32_t *pPIN = NULL;
  79. pPIN = PIN_GetPointer(portnum, pinnum);
  80. *pPIN &= 0x00000007;//Clear function bits
  81. *pPIN |= funcnum;
  82. }
  83. /*********************************************************************//**
  84. * @brief Setup resistor mode for each pin
  85. * @param[in] portnum PORT number, should be in range: 0..3
  86. * @param[in] pinnum Pin number, should be in range: 0..31
  87. * @param[in] modenum: Mode number, should be in range: 0..3
  88. - IOCON_MODE_PLAIN: Plain output
  89. - IOCON_MODE_PULLDOWN: Pull-down enable
  90. - IOCON_MODE_PULLUP: Pull-up enable
  91. - IOCON_MODE_REPEATER: Repeater mode
  92. * @return None
  93. **********************************************************************/
  94. void PINSEL_SetPinMode ( uint8_t portnum, uint8_t pinnum, PinSel_BasicMode modenum)
  95. {
  96. uint32_t *pPIN = NULL;
  97. pPIN = PIN_GetPointer(portnum, pinnum);
  98. *(uint32_t *)pPIN &= ~(3<<3);//Clear function bits
  99. *(uint32_t *)pPIN |= modenum;
  100. }
  101. /*********************************************************************//**
  102. * @brief Setup hysteresis for each pin
  103. * @param[in] portnum Port number, should be in range: 0..3
  104. * @param[in] pinnum Pin number, should be in range: 0..31
  105. * @param[in] NewState new state of Hysteresis mode, should be:
  106. * - ENABLE: Hysteresis enable
  107. * - DISABLE: Hysteresis disable
  108. * @return None
  109. **********************************************************************/
  110. void PINSEL_SetHysMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
  111. {
  112. uint32_t *pPIN = NULL;
  113. pPIN = PIN_GetPointer(portnum, pinnum);
  114. if(NewState == DISABLE)
  115. {
  116. *(uint32_t *)pPIN &= ~IOCON_HYS;//Clear hys bits
  117. }
  118. else
  119. *(uint32_t *)pPIN |= IOCON_HYS;
  120. }
  121. /*********************************************************************//**
  122. * @brief Setup Slew rate for each pin
  123. * @param[in] portnum Port number, should be in range: 0..3
  124. * @param[in] pinnum Pin number, should be in range: 0..31
  125. * @param[in] NewState new state of Slew rate control, should be:
  126. * - ENABLE: Output slew rate control is enable
  127. * - DISABLE: Output slew rate control is disable
  128. * @return None
  129. **********************************************************************/
  130. void PINSEL_SetSlewMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
  131. {
  132. uint32_t *pPIN = NULL;
  133. pPIN = PIN_GetPointer(portnum, pinnum);
  134. if(NewState == DISABLE)
  135. {
  136. *(uint32_t *)pPIN &= ~IOCON_SLEW;//Clear hys bits
  137. }
  138. else
  139. *(uint32_t *)pPIN |= IOCON_SLEW;
  140. }
  141. /*********************************************************************//**
  142. * @brief Setup Input Buffer for each pin
  143. * @param[in] portnum Port number, should be in range: 0..3
  144. * @param[in] pinnum Pin number, should be in range: 0..31
  145. * @param[in] NewState new state of Input buffer mode, should be:
  146. * - ENABLE: The input buffer is enable
  147. * - DISABLE: The input buffer is disable
  148. * @return None
  149. **********************************************************************/
  150. void PINSEL_SetInBufMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
  151. {
  152. uint32_t *pPIN = NULL;
  153. pPIN = PIN_GetPointer(portnum, pinnum);
  154. if(NewState == DISABLE)
  155. {
  156. *(uint32_t *)pPIN &= ~IOCON_INBUF;//Clear hys bits
  157. }
  158. else
  159. *(uint32_t *)pPIN |= IOCON_INBUF;
  160. }
  161. /*********************************************************************//**
  162. * @brief Setup I2CMode for only pins that provide special I2C functionality
  163. * @param[in] portnum Port number, should be in range: 0..3
  164. * @param[in] pinnum Pin number, should be in range: 0..31
  165. * @param[in] I2CMode I2C mode, should be:
  166. * - IOCON_I2CMODE_FAST: Fast mode and standard I2C mode
  167. * - IOCON_I2CMODE_OPENDRAIN: Open drain I/O
  168. * - IOCON_I2CMODE_FASTPLUS: Fast Mode Plus I/O
  169. * - IOCON_I2CMODE_HIGHOPENDRAIN: High drive open drain I/O
  170. * @return None
  171. **********************************************************************/
  172. void PINSEL_SetI2CMode(uint8_t portnum, uint8_t pinnum, PinSel_I2cMode I2CMode)
  173. {
  174. uint32_t *pPIN = NULL;
  175. pPIN = PIN_GetPointer(portnum, pinnum);
  176. *(uint32_t *)pPIN &= ~(PINSEL_I2C_MODE_BITMASK<< PINSEL_I2C_MODE_POS);
  177. *(uint32_t *)pPIN |= (I2CMode << PINSEL_I2C_MODE_POS);
  178. }
  179. /*********************************************************************//**
  180. * @brief Setup Open-drain mode in each pin
  181. * @param[in] portnum Port number, should be in range: 0..3
  182. * @param[in] pinnum Pin number, should be in range: 0..31
  183. * @param[in] NewState new state of Open-drain mode:
  184. * - DISABLE: Normal pin I/O mode
  185. * - ENABLE: Open-drain enable
  186. * @return None
  187. **********************************************************************/
  188. void PINSEL_SetOpenDrainMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
  189. {
  190. uint32_t *pPIN = NULL;
  191. pPIN = PIN_GetPointer(portnum, pinnum);
  192. if(NewState == DISABLE)
  193. {
  194. *(uint32_t *)pPIN &= ~IOCON_ODMODE;//Clear hys bits
  195. }
  196. else
  197. {
  198. *(uint32_t *)pPIN |= IOCON_ODMODE;
  199. }
  200. }
  201. /*********************************************************************//**
  202. * @brief Enable the Analog mode for each pin (default is as Digital pins)
  203. * @param[in] portnum PORT number, should be in range: 0..3
  204. * @param[in] pinnum Pin number, should be in range: 0..31
  205. * @param[in] enable: the state of the pin that is expected to run
  206. - ENABLE: Enable the DAC mode of the pin
  207. - DISABLE: Disable the DAC mode
  208. * @return None
  209. **********************************************************************/
  210. void PINSEL_SetAnalogPinMode (uint8_t portnum, uint8_t pinnum, uint8_t enable)
  211. {
  212. uint32_t *pPIN = NULL;
  213. uint8_t condition = 0;
  214. condition = ((portnum == 0) && (pinnum == 12)) || ((portnum == 0) && (pinnum == 13))
  215. | ((portnum == 0) && (pinnum <= 26) && (pinnum >= 23))
  216. | ((portnum == 1) && (pinnum == 30)) || ((portnum == 1) && (pinnum == 31));
  217. if(!condition)
  218. {
  219. return;
  220. }
  221. pPIN = PIN_GetPointer(portnum, pinnum);
  222. //Clear this bit to set the pin to Analog mode
  223. *(uint32_t *)pPIN &= ~(PINSEL_ADMODE_BITMASK << PINSEL_ADMODE_POS);
  224. if(enable)
  225. {
  226. }
  227. else
  228. {
  229. *(uint32_t *)pPIN |= (1 << PINSEL_ADMODE_POS);//Set 16th bit to one
  230. }
  231. return;
  232. }
  233. /*********************************************************************//**
  234. * @brief Choose the DAC mode for each pin
  235. * @param[in] portnum PORT number, should be in range: 0..3
  236. * @param[in] pinnum Pin number, should be in range: 0..31
  237. * @param[in] enable: the state of the pin that is expected to run
  238. - ENABLE: Enable the DAC mode of the pin
  239. - DISABLE: Disable the DAC mode
  240. * @return None
  241. **********************************************************************/
  242. void PINSEL_DacEnable (uint8_t portnum, uint8_t pinnum, uint8_t enable)
  243. {
  244. uint32_t *pPIN = NULL;
  245. // This setting is only for DAC pin (output pin)
  246. if(!((portnum == 0) && (pinnum == 26)))
  247. {
  248. return;
  249. }
  250. pPIN = PIN_GetPointer(portnum, pinnum);
  251. //Clear DAC Enable function bits
  252. *(uint32_t *)pPIN &= ~(PINSEL_DACEN_BITMASK << PINSEL_DACEN_POS);
  253. if(enable)
  254. {
  255. *(uint32_t *)pPIN |= (1 << PINSEL_DACEN_POS);//Set 16th bit to one
  256. }
  257. else
  258. {
  259. }
  260. return;
  261. }
  262. /*********************************************************************//**
  263. * @brief Control the glitch filter for each pin
  264. * @param[in] portnum PORT number, should be in range: 0..3
  265. * @param[in] pinnum Pin number, should be in range: 0..31
  266. * @param[in] enable: the state of the pin that is expected to run
  267. - ENABLE: The noise pulses below approximately 10ns are filtered out
  268. - DISABLE: No input filtering is done.
  269. * @return None
  270. **********************************************************************/
  271. void PINSEL_SetFilter (uint8_t portnum, uint8_t pinnum, uint8_t enable)
  272. {
  273. uint32_t *pPIN = NULL;
  274. // This setting is only for DAC pin (output pin)
  275. if(!((portnum == 0) && ((pinnum == 7) || (pinnum == 8) || (pinnum == 9))))
  276. {
  277. return;
  278. }
  279. pPIN = PIN_GetPointer(portnum, pinnum);
  280. *(uint32_t *)pPIN |= (1 << 7);//Set 7th bit for normal operation following the UM1.0
  281. //Clear Filter bits
  282. *(uint32_t *)pPIN &= ~(PINSEL_GLITCH_FILTER_BITMASK << PINSEL_GLITCH_FILTER_POS);
  283. if(!enable)
  284. {
  285. *(uint32_t *)pPIN |= (1 << PINSEL_GLITCH_FILTER_POS);//Set 8th bit to one
  286. }
  287. else
  288. {
  289. }
  290. *pPIN = *pPIN;
  291. return;
  292. }
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. /* --------------------------------- End Of File ------------------------------ */