gpio.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. //*****************************************************************************
  2. //
  3. // gpio.c - API for GPIO ports
  4. //
  5. // Copyright (c) 2005-2009 Luminary Micro, Inc. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Luminary Micro, Inc. (LMI) is supplying this software for use solely and
  9. // exclusively on LMI's microcontroller products.
  10. //
  11. // The software is owned by LMI and/or its suppliers, and is protected under
  12. // applicable copyright laws. All rights are reserved. You may not combine
  13. // this software with "viral" open-source software in order to form a larger
  14. // program. Any use in violation of the foregoing restrictions may subject
  15. // the user to criminal sanctions under applicable laws, as well as to civil
  16. // liability for the breach of the terms and conditions of this license.
  17. //
  18. // THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
  19. // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  20. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
  21. // LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
  22. // CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  23. //
  24. // This is part of revision 4694 of the Stellaris Peripheral Driver Library.
  25. //
  26. //*****************************************************************************
  27. //*****************************************************************************
  28. //
  29. //! \addtogroup gpio_api
  30. //! @{
  31. //
  32. //*****************************************************************************
  33. #include "inc/hw_gpio.h"
  34. #include "inc/hw_ints.h"
  35. #include "inc/hw_memmap.h"
  36. #include "inc/hw_sysctl.h"
  37. #include "inc/hw_types.h"
  38. #include "driverlib/debug.h"
  39. #include "driverlib/gpio.h"
  40. #include "driverlib/interrupt.h"
  41. //*****************************************************************************
  42. //
  43. // The base addresses of all the GPIO modules. Both the APB and AHB apertures
  44. // are provided.
  45. //
  46. //*****************************************************************************
  47. static const unsigned long g_pulGPIOBaseAddrs[] =
  48. {
  49. GPIO_PORTA_BASE, GPIO_PORTA_AHB_BASE,
  50. GPIO_PORTB_BASE, GPIO_PORTB_AHB_BASE,
  51. GPIO_PORTC_BASE, GPIO_PORTC_AHB_BASE,
  52. GPIO_PORTD_BASE, GPIO_PORTD_AHB_BASE,
  53. GPIO_PORTE_BASE, GPIO_PORTE_AHB_BASE,
  54. GPIO_PORTF_BASE, GPIO_PORTF_AHB_BASE,
  55. GPIO_PORTG_BASE, GPIO_PORTG_AHB_BASE,
  56. GPIO_PORTH_BASE, GPIO_PORTH_AHB_BASE,
  57. GPIO_PORTJ_BASE, GPIO_PORTJ_AHB_BASE,
  58. };
  59. //*****************************************************************************
  60. //
  61. //! \internal
  62. //! Checks a GPIO base address.
  63. //!
  64. //! \param ulPort is the base address of the GPIO port.
  65. //!
  66. //! This function determines if a GPIO port base address is valid.
  67. //!
  68. //! \return Returns \b true if the base address is valid and \b false
  69. //! otherwise.
  70. //
  71. //*****************************************************************************
  72. #ifdef DEBUG
  73. static tBoolean
  74. GPIOBaseValid(unsigned long ulPort)
  75. {
  76. return((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTA_AHB_BASE) ||
  77. (ulPort == GPIO_PORTB_BASE) || (ulPort == GPIO_PORTB_AHB_BASE) ||
  78. (ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTC_AHB_BASE) ||
  79. (ulPort == GPIO_PORTD_BASE) || (ulPort == GPIO_PORTD_AHB_BASE) ||
  80. (ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTE_AHB_BASE) ||
  81. (ulPort == GPIO_PORTF_BASE) || (ulPort == GPIO_PORTF_AHB_BASE) ||
  82. (ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTG_AHB_BASE) ||
  83. (ulPort == GPIO_PORTH_BASE) || (ulPort == GPIO_PORTH_AHB_BASE) ||
  84. (ulPort == GPIO_PORTJ_BASE) || (ulPort == GPIO_PORTJ_AHB_BASE));
  85. }
  86. #endif
  87. //*****************************************************************************
  88. //
  89. //! \internal
  90. //! Gets the GPIO interrupt number.
  91. //!
  92. //! \param ulPort is the base address of the GPIO port.
  93. //!
  94. //! Given a GPIO base address, returns the corresponding interrupt number.
  95. //!
  96. //! \return Returns a GPIO interrupt number, or -1 if \e ulPort is invalid.
  97. //
  98. //*****************************************************************************
  99. static long
  100. GPIOGetIntNumber(unsigned long ulPort)
  101. {
  102. unsigned int ulInt;
  103. //
  104. // Determine the GPIO interrupt number for the given module.
  105. //
  106. switch(ulPort)
  107. {
  108. case GPIO_PORTA_BASE:
  109. case GPIO_PORTA_AHB_BASE:
  110. {
  111. ulInt = INT_GPIOA;
  112. break;
  113. }
  114. case GPIO_PORTB_BASE:
  115. case GPIO_PORTB_AHB_BASE:
  116. {
  117. ulInt = INT_GPIOB;
  118. break;
  119. }
  120. case GPIO_PORTC_BASE:
  121. case GPIO_PORTC_AHB_BASE:
  122. {
  123. ulInt = INT_GPIOC;
  124. break;
  125. }
  126. case GPIO_PORTD_BASE:
  127. case GPIO_PORTD_AHB_BASE:
  128. {
  129. ulInt = INT_GPIOD;
  130. break;
  131. }
  132. case GPIO_PORTE_BASE:
  133. case GPIO_PORTE_AHB_BASE:
  134. {
  135. ulInt = INT_GPIOE;
  136. break;
  137. }
  138. case GPIO_PORTF_BASE:
  139. case GPIO_PORTF_AHB_BASE:
  140. {
  141. ulInt = INT_GPIOF;
  142. break;
  143. }
  144. case GPIO_PORTG_BASE:
  145. case GPIO_PORTG_AHB_BASE:
  146. {
  147. ulInt = INT_GPIOG;
  148. break;
  149. }
  150. case GPIO_PORTH_BASE:
  151. case GPIO_PORTH_AHB_BASE:
  152. {
  153. ulInt = INT_GPIOH;
  154. break;
  155. }
  156. case GPIO_PORTJ_BASE:
  157. case GPIO_PORTJ_AHB_BASE:
  158. {
  159. ulInt = INT_GPIOJ;
  160. break;
  161. }
  162. default:
  163. {
  164. return(-1);
  165. }
  166. }
  167. //
  168. // Return GPIO interrupt number.
  169. //
  170. return(ulInt);
  171. }
  172. //*****************************************************************************
  173. //
  174. //! Sets the direction and mode of the specified pin(s).
  175. //!
  176. //! \param ulPort is the base address of the GPIO port
  177. //! \param ucPins is the bit-packed representation of the pin(s).
  178. //! \param ulPinIO is the pin direction and/or mode.
  179. //!
  180. //! This function will set the specified pin(s) on the selected GPIO port
  181. //! as either an input or output under software control, or it will set the
  182. //! pin to be under hardware control.
  183. //!
  184. //! The parameter \e ulPinIO is an enumerated data type that can be one of
  185. //! the following values:
  186. //!
  187. //! - \b GPIO_DIR_MODE_IN
  188. //! - \b GPIO_DIR_MODE_OUT
  189. //! - \b GPIO_DIR_MODE_HW
  190. //!
  191. //! where \b GPIO_DIR_MODE_IN specifies that the pin will be programmed as
  192. //! a software controlled input, \b GPIO_DIR_MODE_OUT specifies that the pin
  193. //! will be programmed as a software controlled output, and
  194. //! \b GPIO_DIR_MODE_HW specifies that the pin will be placed under
  195. //! hardware control.
  196. //!
  197. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  198. //! set identifies the pin to be accessed, and where bit 0 of the byte
  199. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  200. //!
  201. //! \return None.
  202. //
  203. //*****************************************************************************
  204. void
  205. GPIODirModeSet(unsigned long ulPort, unsigned char ucPins,
  206. unsigned long ulPinIO)
  207. {
  208. //
  209. // Check the arguments.
  210. //
  211. ASSERT(GPIOBaseValid(ulPort));
  212. ASSERT((ulPinIO == GPIO_DIR_MODE_IN) || (ulPinIO == GPIO_DIR_MODE_OUT) ||
  213. (ulPinIO == GPIO_DIR_MODE_HW));
  214. //
  215. // Set the pin direction and mode.
  216. //
  217. HWREG(ulPort + GPIO_O_DIR) = ((ulPinIO & 1) ?
  218. (HWREG(ulPort + GPIO_O_DIR) | ucPins) :
  219. (HWREG(ulPort + GPIO_O_DIR) & ~(ucPins)));
  220. HWREG(ulPort + GPIO_O_AFSEL) = ((ulPinIO & 2) ?
  221. (HWREG(ulPort + GPIO_O_AFSEL) | ucPins) :
  222. (HWREG(ulPort + GPIO_O_AFSEL) &
  223. ~(ucPins)));
  224. }
  225. //*****************************************************************************
  226. //
  227. //! Gets the direction and mode of a pin.
  228. //!
  229. //! \param ulPort is the base address of the GPIO port.
  230. //! \param ucPin is the pin number.
  231. //!
  232. //! This function gets the direction and control mode for a specified pin on
  233. //! the selected GPIO port. The pin can be configured as either an input or
  234. //! output under software control, or it can be under hardware control. The
  235. //! type of control and direction are returned as an enumerated data type.
  236. //!
  237. //! \return Returns one of the enumerated data types described for
  238. //! GPIODirModeSet().
  239. //
  240. //*****************************************************************************
  241. unsigned long
  242. GPIODirModeGet(unsigned long ulPort, unsigned char ucPin)
  243. {
  244. unsigned long ulDir, ulAFSEL;
  245. //
  246. // Check the arguments.
  247. //
  248. ASSERT(GPIOBaseValid(ulPort));
  249. ASSERT(ucPin < 8);
  250. //
  251. // Convert from a pin number to a bit position.
  252. //
  253. ucPin = 1 << ucPin;
  254. //
  255. // Return the pin direction and mode.
  256. //
  257. ulDir = HWREG(ulPort + GPIO_O_DIR);
  258. ulAFSEL = HWREG(ulPort + GPIO_O_AFSEL);
  259. return(((ulDir & ucPin) ? 1 : 0) | ((ulAFSEL & ucPin) ? 2 : 0));
  260. }
  261. //*****************************************************************************
  262. //
  263. //! Sets the interrupt type for the specified pin(s).
  264. //!
  265. //! \param ulPort is the base address of the GPIO port.
  266. //! \param ucPins is the bit-packed representation of the pin(s).
  267. //! \param ulIntType specifies the type of interrupt trigger mechanism.
  268. //!
  269. //! This function sets up the various interrupt trigger mechanisms for the
  270. //! specified pin(s) on the selected GPIO port.
  271. //!
  272. //! The parameter \e ulIntType is an enumerated data type that can be one of
  273. //! the following values:
  274. //!
  275. //! - \b GPIO_FALLING_EDGE
  276. //! - \b GPIO_RISING_EDGE
  277. //! - \b GPIO_BOTH_EDGES
  278. //! - \b GPIO_LOW_LEVEL
  279. //! - \b GPIO_HIGH_LEVEL
  280. //!
  281. //! where the different values describe the interrupt detection mechanism
  282. //! (edge or level) and the particular triggering event (falling, rising,
  283. //! or both edges for edge detect, low or high for level detect).
  284. //!
  285. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  286. //! set identifies the pin to be accessed, and where bit 0 of the byte
  287. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  288. //!
  289. //! \note In order to avoid any spurious interrupts, the user must
  290. //! ensure that the GPIO inputs remain stable for the duration of
  291. //! this function.
  292. //!
  293. //! \return None.
  294. //
  295. //*****************************************************************************
  296. void
  297. GPIOIntTypeSet(unsigned long ulPort, unsigned char ucPins,
  298. unsigned long ulIntType)
  299. {
  300. //
  301. // Check the arguments.
  302. //
  303. ASSERT(GPIOBaseValid(ulPort));
  304. ASSERT((ulIntType == GPIO_FALLING_EDGE) ||
  305. (ulIntType == GPIO_RISING_EDGE) || (ulIntType == GPIO_BOTH_EDGES) ||
  306. (ulIntType == GPIO_LOW_LEVEL) || (ulIntType == GPIO_HIGH_LEVEL));
  307. //
  308. // Set the pin interrupt type.
  309. //
  310. HWREG(ulPort + GPIO_O_IBE) = ((ulIntType & 1) ?
  311. (HWREG(ulPort + GPIO_O_IBE) | ucPins) :
  312. (HWREG(ulPort + GPIO_O_IBE) & ~(ucPins)));
  313. HWREG(ulPort + GPIO_O_IS) = ((ulIntType & 2) ?
  314. (HWREG(ulPort + GPIO_O_IS) | ucPins) :
  315. (HWREG(ulPort + GPIO_O_IS) & ~(ucPins)));
  316. HWREG(ulPort + GPIO_O_IEV) = ((ulIntType & 4) ?
  317. (HWREG(ulPort + GPIO_O_IEV) | ucPins) :
  318. (HWREG(ulPort + GPIO_O_IEV) & ~(ucPins)));
  319. }
  320. //*****************************************************************************
  321. //
  322. //! Gets the interrupt type for a pin.
  323. //!
  324. //! \param ulPort is the base address of the GPIO port.
  325. //! \param ucPin is the pin number.
  326. //!
  327. //! This function gets the interrupt type for a specified pin on the selected
  328. //! GPIO port. The pin can be configured as a falling edge, rising edge, or
  329. //! both edge detected interrupt, or it can be configured as a low level or
  330. //! high level detected interrupt. The type of interrupt detection mechanism
  331. //! is returned as an enumerated data type.
  332. //!
  333. //! \return Returns one of the enumerated data types described for
  334. //! GPIOIntTypeSet().
  335. //
  336. //*****************************************************************************
  337. unsigned long
  338. GPIOIntTypeGet(unsigned long ulPort, unsigned char ucPin)
  339. {
  340. unsigned long ulIBE, ulIS, ulIEV;
  341. //
  342. // Check the arguments.
  343. //
  344. ASSERT(GPIOBaseValid(ulPort));
  345. ASSERT(ucPin < 8);
  346. //
  347. // Convert from a pin number to a bit position.
  348. //
  349. ucPin = 1 << ucPin;
  350. //
  351. // Return the pin interrupt type.
  352. //
  353. ulIBE = HWREG(ulPort + GPIO_O_IBE);
  354. ulIS = HWREG(ulPort + GPIO_O_IS);
  355. ulIEV = HWREG(ulPort + GPIO_O_IEV);
  356. return(((ulIBE & ucPin) ? 1 : 0) | ((ulIS & ucPin) ? 2 : 0) |
  357. ((ulIEV & ucPin) ? 4 : 0));
  358. }
  359. //*****************************************************************************
  360. //
  361. //! Sets the pad configuration for the specified pin(s).
  362. //!
  363. //! \param ulPort is the base address of the GPIO port.
  364. //! \param ucPins is the bit-packed representation of the pin(s).
  365. //! \param ulStrength specifies the output drive strength.
  366. //! \param ulPinType specifies the pin type.
  367. //!
  368. //! This function sets the drive strength and type for the specified pin(s)
  369. //! on the selected GPIO port. For pin(s) configured as input ports, the
  370. //! pad is configured as requested, but the only real effect on the input
  371. //! is the configuration of the pull-up or pull-down termination.
  372. //!
  373. //! The parameter \e ulStrength can be one of the following values:
  374. //!
  375. //! - \b GPIO_STRENGTH_2MA
  376. //! - \b GPIO_STRENGTH_4MA
  377. //! - \b GPIO_STRENGTH_8MA
  378. //! - \b GPIO_STRENGTH_8MA_SC
  379. //!
  380. //! where \b GPIO_STRENGTH_xMA specifies either 2, 4, or 8 mA output drive
  381. //! strength, and \b GPIO_OUT_STRENGTH_8MA_SC specifies 8 mA output drive with
  382. //! slew control.
  383. //!
  384. //! The parameter \e ulPinType can be one of the following values:
  385. //!
  386. //! - \b GPIO_PIN_TYPE_STD
  387. //! - \b GPIO_PIN_TYPE_STD_WPU
  388. //! - \b GPIO_PIN_TYPE_STD_WPD
  389. //! - \b GPIO_PIN_TYPE_OD
  390. //! - \b GPIO_PIN_TYPE_OD_WPU
  391. //! - \b GPIO_PIN_TYPE_OD_WPD
  392. //! - \b GPIO_PIN_TYPE_ANALOG
  393. //!
  394. //! where \b GPIO_PIN_TYPE_STD* specifies a push-pull pin, \b GPIO_PIN_TYPE_OD*
  395. //! specifies an open-drain pin, \b *_WPU specifies a weak pull-up, \b *_WPD
  396. //! specifies a weak pull-down, and \b GPIO_PIN_TYPE_ANALOG specifies an
  397. //! analog input (for the comparators).
  398. //!
  399. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  400. //! set identifies the pin to be accessed, and where bit 0 of the byte
  401. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  402. //!
  403. //! \return None.
  404. //
  405. //*****************************************************************************
  406. void
  407. GPIOPadConfigSet(unsigned long ulPort, unsigned char ucPins,
  408. unsigned long ulStrength, unsigned long ulPinType)
  409. {
  410. //
  411. // Check the arguments.
  412. //
  413. ASSERT(GPIOBaseValid(ulPort));
  414. ASSERT((ulStrength == GPIO_STRENGTH_2MA) ||
  415. (ulStrength == GPIO_STRENGTH_4MA) ||
  416. (ulStrength == GPIO_STRENGTH_8MA) ||
  417. (ulStrength == GPIO_STRENGTH_8MA_SC));
  418. ASSERT((ulPinType == GPIO_PIN_TYPE_STD) ||
  419. (ulPinType == GPIO_PIN_TYPE_STD_WPU) ||
  420. (ulPinType == GPIO_PIN_TYPE_STD_WPD) ||
  421. (ulPinType == GPIO_PIN_TYPE_OD) ||
  422. (ulPinType == GPIO_PIN_TYPE_OD_WPU) ||
  423. (ulPinType == GPIO_PIN_TYPE_OD_WPD) ||
  424. (ulPinType == GPIO_PIN_TYPE_ANALOG))
  425. //
  426. // Set the output drive strength.
  427. //
  428. HWREG(ulPort + GPIO_O_DR2R) = ((ulStrength & 1) ?
  429. (HWREG(ulPort + GPIO_O_DR2R) | ucPins) :
  430. (HWREG(ulPort + GPIO_O_DR2R) & ~(ucPins)));
  431. HWREG(ulPort + GPIO_O_DR4R) = ((ulStrength & 2) ?
  432. (HWREG(ulPort + GPIO_O_DR4R) | ucPins) :
  433. (HWREG(ulPort + GPIO_O_DR4R) & ~(ucPins)));
  434. HWREG(ulPort + GPIO_O_DR8R) = ((ulStrength & 4) ?
  435. (HWREG(ulPort + GPIO_O_DR8R) | ucPins) :
  436. (HWREG(ulPort + GPIO_O_DR8R) & ~(ucPins)));
  437. HWREG(ulPort + GPIO_O_SLR) = ((ulStrength & 8) ?
  438. (HWREG(ulPort + GPIO_O_SLR) | ucPins) :
  439. (HWREG(ulPort + GPIO_O_SLR) & ~(ucPins)));
  440. //
  441. // Set the pin type.
  442. //
  443. HWREG(ulPort + GPIO_O_ODR) = ((ulPinType & 1) ?
  444. (HWREG(ulPort + GPIO_O_ODR) | ucPins) :
  445. (HWREG(ulPort + GPIO_O_ODR) & ~(ucPins)));
  446. HWREG(ulPort + GPIO_O_PUR) = ((ulPinType & 2) ?
  447. (HWREG(ulPort + GPIO_O_PUR) | ucPins) :
  448. (HWREG(ulPort + GPIO_O_PUR) & ~(ucPins)));
  449. HWREG(ulPort + GPIO_O_PDR) = ((ulPinType & 4) ?
  450. (HWREG(ulPort + GPIO_O_PDR) | ucPins) :
  451. (HWREG(ulPort + GPIO_O_PDR) & ~(ucPins)));
  452. HWREG(ulPort + GPIO_O_DEN) = ((ulPinType & 8) ?
  453. (HWREG(ulPort + GPIO_O_DEN) | ucPins) :
  454. (HWREG(ulPort + GPIO_O_DEN) & ~(ucPins)));
  455. //
  456. // Set the analog mode select register. This register only appears in
  457. // DustDevil-class (and later) devices, but is a harmless write on
  458. // Sandstorm- and Fury-class devices.
  459. //
  460. HWREG(ulPort + GPIO_O_AMSEL) =
  461. ((ulPinType == GPIO_PIN_TYPE_ANALOG) ?
  462. (HWREG(ulPort + GPIO_O_AMSEL) | ucPins) :
  463. (HWREG(ulPort + GPIO_O_AMSEL) & ~(ucPins)));
  464. }
  465. //*****************************************************************************
  466. //
  467. //! Gets the pad configuration for a pin.
  468. //!
  469. //! \param ulPort is the base address of the GPIO port.
  470. //! \param ucPin is the pin number.
  471. //! \param pulStrength is a pointer to storage for the output drive strength.
  472. //! \param pulPinType is a pointer to storage for the output drive type.
  473. //!
  474. //! This function gets the pad configuration for a specified pin on the
  475. //! selected GPIO port. The values returned in \e pulStrength and
  476. //! \e pulPinType correspond to the values used in GPIOPadConfigSet(). This
  477. //! function also works for pin(s) configured as input pin(s); however, the
  478. //! only meaningful data returned is whether the pin is terminated with a
  479. //! pull-up or down resistor.
  480. //!
  481. //! \return None
  482. //
  483. //*****************************************************************************
  484. void
  485. GPIOPadConfigGet(unsigned long ulPort, unsigned char ucPin,
  486. unsigned long *pulStrength, unsigned long *pulPinType)
  487. {
  488. unsigned long ulTemp1, ulTemp2, ulTemp3, ulTemp4;
  489. //
  490. // Check the arguments.
  491. //
  492. ASSERT(GPIOBaseValid(ulPort));
  493. ASSERT(ucPin < 8);
  494. //
  495. // Convert from a pin number to a bit position.
  496. //
  497. ucPin = (1 << ucPin);
  498. //
  499. // Get the drive strength for this pin.
  500. //
  501. ulTemp1 = HWREG(ulPort + GPIO_O_DR2R);
  502. ulTemp2 = HWREG(ulPort + GPIO_O_DR4R);
  503. ulTemp3 = HWREG(ulPort + GPIO_O_DR8R);
  504. ulTemp4 = HWREG(ulPort + GPIO_O_SLR);
  505. *pulStrength = (((ulTemp1 & ucPin) ? 1 : 0) | ((ulTemp2 & ucPin) ? 2 : 0) |
  506. ((ulTemp3 & ucPin) ? 4 : 0) | ((ulTemp4 & ucPin) ? 8 : 0));
  507. //
  508. // Get the pin type.
  509. //
  510. ulTemp1 = HWREG(ulPort + GPIO_O_ODR);
  511. ulTemp2 = HWREG(ulPort + GPIO_O_PUR);
  512. ulTemp3 = HWREG(ulPort + GPIO_O_PDR);
  513. ulTemp4 = HWREG(ulPort + GPIO_O_DEN);
  514. *pulPinType = (((ulTemp1 & ucPin) ? 1 : 0) | ((ulTemp2 & ucPin) ? 2 : 0) |
  515. ((ulTemp3 & ucPin) ? 4 : 0) | ((ulTemp4 & ucPin) ? 8 : 0));
  516. }
  517. //*****************************************************************************
  518. //
  519. //! Enables interrupts for the specified pin(s).
  520. //!
  521. //! \param ulPort is the base address of the GPIO port.
  522. //! \param ucPins is the bit-packed representation of the pin(s).
  523. //!
  524. //! Unmasks the interrupt for the specified pin(s).
  525. //!
  526. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  527. //! set identifies the pin to be accessed, and where bit 0 of the byte
  528. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  529. //!
  530. //! \return None.
  531. //
  532. //*****************************************************************************
  533. void
  534. GPIOPinIntEnable(unsigned long ulPort, unsigned char ucPins)
  535. {
  536. //
  537. // Check the arguments.
  538. //
  539. ASSERT(GPIOBaseValid(ulPort));
  540. //
  541. // Enable the interrupts.
  542. //
  543. HWREG(ulPort + GPIO_O_IM) |= ucPins;
  544. }
  545. //*****************************************************************************
  546. //
  547. //! Disables interrupts for the specified pin(s).
  548. //!
  549. //! \param ulPort is the base address of the GPIO port.
  550. //! \param ucPins is the bit-packed representation of the pin(s).
  551. //!
  552. //! Masks the interrupt for the specified pin(s).
  553. //!
  554. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  555. //! set identifies the pin to be accessed, and where bit 0 of the byte
  556. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  557. //!
  558. //! \return None.
  559. //
  560. //*****************************************************************************
  561. void
  562. GPIOPinIntDisable(unsigned long ulPort, unsigned char ucPins)
  563. {
  564. //
  565. // Check the arguments.
  566. //
  567. ASSERT(GPIOBaseValid(ulPort));
  568. //
  569. // Disable the interrupts.
  570. //
  571. HWREG(ulPort + GPIO_O_IM) &= ~(ucPins);
  572. }
  573. //*****************************************************************************
  574. //
  575. //! Gets interrupt status for the specified GPIO port.
  576. //!
  577. //! \param ulPort is the base address of the GPIO port.
  578. //! \param bMasked specifies whether masked or raw interrupt status is
  579. //! returned.
  580. //!
  581. //! If \e bMasked is set as \b true, then the masked interrupt status is
  582. //! returned; otherwise, the raw interrupt status will be returned.
  583. //!
  584. //! \return Returns a bit-packed byte, where each bit that is set identifies
  585. //! an active masked or raw interrupt, and where bit 0 of the byte
  586. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  587. //! Bits 31:8 should be ignored.
  588. //
  589. //*****************************************************************************
  590. long
  591. GPIOPinIntStatus(unsigned long ulPort, tBoolean bMasked)
  592. {
  593. //
  594. // Check the arguments.
  595. //
  596. ASSERT(GPIOBaseValid(ulPort));
  597. //
  598. // Return the interrupt status.
  599. //
  600. if(bMasked)
  601. {
  602. return(HWREG(ulPort + GPIO_O_MIS));
  603. }
  604. else
  605. {
  606. return(HWREG(ulPort + GPIO_O_RIS));
  607. }
  608. }
  609. //*****************************************************************************
  610. //
  611. //! Clears the interrupt for the specified pin(s).
  612. //!
  613. //! \param ulPort is the base address of the GPIO port.
  614. //! \param ucPins is the bit-packed representation of the pin(s).
  615. //!
  616. //! Clears the interrupt for the specified pin(s).
  617. //!
  618. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  619. //! set identifies the pin to be accessed, and where bit 0 of the byte
  620. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  621. //!
  622. //! \note Since there is a write buffer in the Cortex-M3 processor, it may take
  623. //! several clock cycles before the interrupt source is actually cleared.
  624. //! Therefore, it is recommended that the interrupt source be cleared early in
  625. //! the interrupt handler (as opposed to the very last action) to avoid
  626. //! returning from the interrupt handler before the interrupt source is
  627. //! actually cleared. Failure to do so may result in the interrupt handler
  628. //! being immediately reentered (since NVIC still sees the interrupt source
  629. //! asserted).
  630. //!
  631. //! \return None.
  632. //
  633. //*****************************************************************************
  634. void
  635. GPIOPinIntClear(unsigned long ulPort, unsigned char ucPins)
  636. {
  637. //
  638. // Check the arguments.
  639. //
  640. ASSERT(GPIOBaseValid(ulPort));
  641. //
  642. // Clear the interrupts.
  643. //
  644. HWREG(ulPort + GPIO_O_ICR) = ucPins;
  645. }
  646. //*****************************************************************************
  647. //
  648. //! Registers an interrupt handler for a GPIO port.
  649. //!
  650. //! \param ulPort is the base address of the GPIO port.
  651. //! \param pfnIntHandler is a pointer to the GPIO port interrupt handling
  652. //! function.
  653. //!
  654. //! This function will ensure that the interrupt handler specified by
  655. //! \e pfnIntHandler is called when an interrupt is detected from the selected
  656. //! GPIO port. This function will also enable the corresponding GPIO interrupt
  657. //! in the interrupt controller; individual pin interrupts and interrupt
  658. //! sources must be enabled with GPIOPinIntEnable().
  659. //!
  660. //! \sa IntRegister() for important information about registering interrupt
  661. //! handlers.
  662. //!
  663. //! \return None.
  664. //
  665. //*****************************************************************************
  666. void
  667. GPIOPortIntRegister(unsigned long ulPort, void (*pfnIntHandler)(void))
  668. {
  669. //
  670. // Check the arguments.
  671. //
  672. ASSERT(GPIOBaseValid(ulPort));
  673. //
  674. // Get the interrupt number associated with the specified GPIO.
  675. //
  676. ulPort = GPIOGetIntNumber(ulPort);
  677. //
  678. // Register the interrupt handler.
  679. //
  680. IntRegister(ulPort, pfnIntHandler);
  681. //
  682. // Enable the GPIO interrupt.
  683. //
  684. IntEnable(ulPort);
  685. }
  686. //*****************************************************************************
  687. //
  688. //! Removes an interrupt handler for a GPIO port.
  689. //!
  690. //! \param ulPort is the base address of the GPIO port.
  691. //!
  692. //! This function will unregister the interrupt handler for the specified
  693. //! GPIO port. This function will also disable the corresponding
  694. //! GPIO port interrupt in the interrupt controller; individual GPIO interrupts
  695. //! and interrupt sources must be disabled with GPIOPinIntDisable().
  696. //!
  697. //! \sa IntRegister() for important information about registering interrupt
  698. //! handlers.
  699. //!
  700. //! \return None.
  701. //
  702. //*****************************************************************************
  703. void
  704. GPIOPortIntUnregister(unsigned long ulPort)
  705. {
  706. //
  707. // Check the arguments.
  708. //
  709. ASSERT(GPIOBaseValid(ulPort));
  710. //
  711. // Get the interrupt number associated with the specified GPIO.
  712. //
  713. ulPort = GPIOGetIntNumber(ulPort);
  714. //
  715. // Disable the GPIO interrupt.
  716. //
  717. IntDisable(ulPort);
  718. //
  719. // Unregister the interrupt handler.
  720. //
  721. IntUnregister(ulPort);
  722. }
  723. //*****************************************************************************
  724. //
  725. //! Reads the values present of the specified pin(s).
  726. //!
  727. //! \param ulPort is the base address of the GPIO port.
  728. //! \param ucPins is the bit-packed representation of the pin(s).
  729. //!
  730. //! The values at the specified pin(s) are read, as specified by \e ucPins.
  731. //! Values are returned for both input and output pin(s), and the value
  732. //! for pin(s) that are not specified by \e ucPins are set to 0.
  733. //!
  734. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  735. //! set identifies the pin to be accessed, and where bit 0 of the byte
  736. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  737. //!
  738. //! \return Returns a bit-packed byte providing the state of the specified
  739. //! pin, where bit 0 of the byte represents GPIO port pin 0, bit 1 represents
  740. //! GPIO port pin 1, and so on. Any bit that is not specified by \e ucPins
  741. //! is returned as a 0. Bits 31:8 should be ignored.
  742. //
  743. //*****************************************************************************
  744. long
  745. GPIOPinRead(unsigned long ulPort, unsigned char ucPins)
  746. {
  747. //
  748. // Check the arguments.
  749. //
  750. ASSERT(GPIOBaseValid(ulPort));
  751. //
  752. // Return the pin value(s).
  753. //
  754. return(HWREG(ulPort + (GPIO_O_DATA + (ucPins << 2))));
  755. }
  756. //*****************************************************************************
  757. //
  758. //! Writes a value to the specified pin(s).
  759. //!
  760. //! \param ulPort is the base address of the GPIO port.
  761. //! \param ucPins is the bit-packed representation of the pin(s).
  762. //! \param ucVal is the value to write to the pin(s).
  763. //!
  764. //! Writes the corresponding bit values to the output pin(s) specified by
  765. //! \e ucPins. Writing to a pin configured as an input pin has no effect.
  766. //!
  767. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  768. //! set identifies the pin to be accessed, and where bit 0 of the byte
  769. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  770. //!
  771. //! \return None.
  772. //
  773. //*****************************************************************************
  774. void
  775. GPIOPinWrite(unsigned long ulPort, unsigned char ucPins, unsigned char ucVal)
  776. {
  777. //
  778. // Check the arguments.
  779. //
  780. ASSERT(GPIOBaseValid(ulPort));
  781. //
  782. // Write the pins.
  783. //
  784. HWREG(ulPort + (GPIO_O_DATA + (ucPins << 2))) = ucVal;
  785. }
  786. //*****************************************************************************
  787. //
  788. //! Configures pin(s) for use as analog-to-digital converter inputs.
  789. //!
  790. //! \param ulPort is the base address of the GPIO port.
  791. //! \param ucPins is the bit-packed representation of the pin(s).
  792. //!
  793. //! The analog-to-digital converter input pins must be properly configured
  794. //! to function correctly on DustDevil-class devices. This function provides
  795. //! the proper configuration for those pin(s).
  796. //!
  797. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  798. //! set identifies the pin to be accessed, and where bit 0 of the byte
  799. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  800. //!
  801. //! \note This cannot be used to turn any pin into an ADC input; it only
  802. //! configures an ADC input pin for proper operation.
  803. //!
  804. //! \return None.
  805. //
  806. //*****************************************************************************
  807. void
  808. GPIOPinTypeADC(unsigned long ulPort, unsigned char ucPins)
  809. {
  810. //
  811. // Check the arguments.
  812. //
  813. ASSERT(GPIOBaseValid(ulPort));
  814. //
  815. // Make the pin(s) be inputs.
  816. //
  817. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_IN);
  818. //
  819. // Set the pad(s) for analog operation.
  820. //
  821. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_ANALOG);
  822. }
  823. //*****************************************************************************
  824. //
  825. //! Configures pin(s) for use as a CAN device.
  826. //!
  827. //! \param ulPort is the base address of the GPIO port.
  828. //! \param ucPins is the bit-packed representation of the pin(s).
  829. //!
  830. //! The CAN pins must be properly configured for the CAN peripherals to
  831. //! function correctly. This function provides a typical configuration for
  832. //! those pin(s); other configurations may work as well depending upon the
  833. //! board setup (for example, using the on-chip pull-ups).
  834. //!
  835. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  836. //! set identifies the pin to be accessed, and where bit 0 of the byte
  837. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  838. //!
  839. //! \note This cannot be used to turn any pin into a CAN pin; it only
  840. //! configures a CAN pin for proper operation.
  841. //!
  842. //! \return None.
  843. //
  844. //*****************************************************************************
  845. void
  846. GPIOPinTypeCAN(unsigned long ulPort, unsigned char ucPins)
  847. {
  848. //
  849. // Check the arguments.
  850. //
  851. ASSERT(GPIOBaseValid(ulPort));
  852. //
  853. // Make the pin(s) be inputs.
  854. //
  855. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  856. //
  857. // Set the pad(s) for standard push-pull operation.
  858. //
  859. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  860. }
  861. //*****************************************************************************
  862. //
  863. //! Configures pin(s) for use as an analog comparator input.
  864. //!
  865. //! \param ulPort is the base address of the GPIO port.
  866. //! \param ucPins is the bit-packed representation of the pin(s).
  867. //!
  868. //! The analog comparator input pins must be properly configured for the analog
  869. //! comparator to function correctly. This function provides the proper
  870. //! configuration for those pin(s).
  871. //!
  872. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  873. //! set identifies the pin to be accessed, and where bit 0 of the byte
  874. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  875. //!
  876. //! \note This cannot be used to turn any pin into an analog comparator input;
  877. //! it only configures an analog comparator pin for proper operation.
  878. //!
  879. //! \return None.
  880. //
  881. //*****************************************************************************
  882. void
  883. GPIOPinTypeComparator(unsigned long ulPort, unsigned char ucPins)
  884. {
  885. //
  886. // Check the arguments.
  887. //
  888. ASSERT(GPIOBaseValid(ulPort));
  889. //
  890. // Make the pin(s) be inputs.
  891. //
  892. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_IN);
  893. //
  894. // Set the pad(s) for analog operation.
  895. //
  896. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_ANALOG);
  897. }
  898. //*****************************************************************************
  899. //
  900. //! Configures pin(s) for use as GPIO inputs.
  901. //!
  902. //! \param ulPort is the base address of the GPIO port.
  903. //! \param ucPins is the bit-packed representation of the pin(s).
  904. //!
  905. //! The GPIO pins must be properly configured in order to function correctly as
  906. //! GPIO inputs; this is especially true of Fury-class devices where the
  907. //! digital input enable is turned off by default. This function provides the
  908. //! proper configuration for those pin(s).
  909. //!
  910. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  911. //! set identifies the pin to be accessed, and where bit 0 of the byte
  912. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  913. //!
  914. //! \return None.
  915. //
  916. //*****************************************************************************
  917. void
  918. GPIOPinTypeGPIOInput(unsigned long ulPort, unsigned char ucPins)
  919. {
  920. //
  921. // Check the arguments.
  922. //
  923. ASSERT(GPIOBaseValid(ulPort));
  924. //
  925. // Make the pin(s) be inputs.
  926. //
  927. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_IN);
  928. //
  929. // Set the pad(s) for standard push-pull operation.
  930. //
  931. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  932. }
  933. //*****************************************************************************
  934. //
  935. //! Configures pin(s) for use as GPIO outputs.
  936. //!
  937. //! \param ulPort is the base address of the GPIO port.
  938. //! \param ucPins is the bit-packed representation of the pin(s).
  939. //!
  940. //! The GPIO pins must be properly configured in order to function correctly as
  941. //! GPIO outputs; this is especially true of Fury-class devices where the
  942. //! digital input enable is turned off by default. This function provides the
  943. //! proper configuration for those pin(s).
  944. //!
  945. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  946. //! set identifies the pin to be accessed, and where bit 0 of the byte
  947. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  948. //!
  949. //! \return None.
  950. //
  951. //*****************************************************************************
  952. void
  953. GPIOPinTypeGPIOOutput(unsigned long ulPort, unsigned char ucPins)
  954. {
  955. //
  956. // Check the arguments.
  957. //
  958. ASSERT(GPIOBaseValid(ulPort));
  959. //
  960. // Make the pin(s) be outputs.
  961. //
  962. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_OUT);
  963. //
  964. // Set the pad(s) for standard push-pull operation.
  965. //
  966. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  967. }
  968. //*****************************************************************************
  969. //
  970. //! Configures pin(s) for use as GPIO open drain outputs.
  971. //!
  972. //! \param ulPort is the base address of the GPIO port.
  973. //! \param ucPins is the bit-packed representation of the pin(s).
  974. //!
  975. //! The GPIO pins must be properly configured in order to function correctly as
  976. //! GPIO outputs; this is especially true of Fury-class devices where the
  977. //! digital input enable is turned off by default. This function provides the
  978. //! proper configuration for those pin(s).
  979. //!
  980. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  981. //! set identifies the pin to be accessed, and where bit 0 of the byte
  982. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  983. //!
  984. //! \return None.
  985. //
  986. //*****************************************************************************
  987. void
  988. GPIOPinTypeGPIOOutputOD(unsigned long ulPort, unsigned char ucPins)
  989. {
  990. //
  991. // Check the arguments.
  992. //
  993. ASSERT(GPIOBaseValid(ulPort));
  994. //
  995. // Make the pin(s) be outputs.
  996. //
  997. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_OUT);
  998. //
  999. // Set the pad(s) for standard push-pull operation.
  1000. //
  1001. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
  1002. }
  1003. //*****************************************************************************
  1004. //
  1005. //! Configures pin(s) for use by the I2C peripheral.
  1006. //!
  1007. //! \param ulPort is the base address of the GPIO port.
  1008. //! \param ucPins is the bit-packed representation of the pin(s).
  1009. //!
  1010. //! The I2C pins must be properly configured for the I2C peripheral to function
  1011. //! correctly. This function provides the proper configuration for those
  1012. //! pin(s).
  1013. //!
  1014. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1015. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1016. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1017. //!
  1018. //! \note This cannot be used to turn any pin into an I2C pin; it only
  1019. //! configures an I2C pin for proper operation.
  1020. //!
  1021. //! \return None.
  1022. //
  1023. //*****************************************************************************
  1024. void
  1025. GPIOPinTypeI2C(unsigned long ulPort, unsigned char ucPins)
  1026. {
  1027. //
  1028. // Check the arguments.
  1029. //
  1030. ASSERT(GPIOBaseValid(ulPort));
  1031. //
  1032. // Make the pin(s) be peripheral controlled.
  1033. //
  1034. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1035. //
  1036. // Set the pad(s) for open-drain operation with a weak pull-up.
  1037. //
  1038. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
  1039. }
  1040. //*****************************************************************************
  1041. //
  1042. //! Configures pin(s) for use by the PWM peripheral.
  1043. //!
  1044. //! \param ulPort is the base address of the GPIO port.
  1045. //! \param ucPins is the bit-packed representation of the pin(s).
  1046. //!
  1047. //! The PWM pins must be properly configured for the PWM peripheral to function
  1048. //! correctly. This function provides a typical configuration for those
  1049. //! pin(s); other configurations may work as well depending upon the board
  1050. //! setup (for example, using the on-chip pull-ups).
  1051. //!
  1052. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1053. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1054. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1055. //!
  1056. //! \note This cannot be used to turn any pin into a PWM pin; it only
  1057. //! configures a PWM pin for proper operation.
  1058. //!
  1059. //! \return None.
  1060. //
  1061. //*****************************************************************************
  1062. void
  1063. GPIOPinTypePWM(unsigned long ulPort, unsigned char ucPins)
  1064. {
  1065. //
  1066. // Check the arguments.
  1067. //
  1068. ASSERT(GPIOBaseValid(ulPort));
  1069. //
  1070. // Make the pin(s) be peripheral controlled.
  1071. //
  1072. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1073. //
  1074. // Set the pad(s) for standard push-pull operation.
  1075. //
  1076. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1077. }
  1078. //*****************************************************************************
  1079. //
  1080. //! Configures pin(s) for use by the QEI peripheral.
  1081. //!
  1082. //! \param ulPort is the base address of the GPIO port.
  1083. //! \param ucPins is the bit-packed representation of the pin(s).
  1084. //!
  1085. //! The QEI pins must be properly configured for the QEI peripheral to function
  1086. //! correctly. This function provides a typical configuration for those
  1087. //! pin(s); other configurations may work as well depending upon the board
  1088. //! setup (for example, not using the on-chip pull-ups).
  1089. //!
  1090. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1091. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1092. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1093. //!
  1094. //! \note This cannot be used to turn any pin into a QEI pin; it only
  1095. //! configures a QEI pin for proper operation.
  1096. //!
  1097. //! \return None.
  1098. //
  1099. //*****************************************************************************
  1100. void
  1101. GPIOPinTypeQEI(unsigned long ulPort, unsigned char ucPins)
  1102. {
  1103. //
  1104. // Check the arguments.
  1105. //
  1106. ASSERT(GPIOBaseValid(ulPort));
  1107. //
  1108. // Make the pin(s) be peripheral controlled.
  1109. //
  1110. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1111. //
  1112. // Set the pad(s) for standard push-pull operation with a weak pull-up.
  1113. //
  1114. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
  1115. }
  1116. //*****************************************************************************
  1117. //
  1118. //! Configures pin(s) for use by the SSI peripheral.
  1119. //!
  1120. //! \param ulPort is the base address of the GPIO port.
  1121. //! \param ucPins is the bit-packed representation of the pin(s).
  1122. //!
  1123. //! The SSI pins must be properly configured for the SSI peripheral to function
  1124. //! correctly. This function provides a typical configuration for those
  1125. //! pin(s); other configurations may work as well depending upon the board
  1126. //! setup (for example, using the on-chip pull-ups).
  1127. //!
  1128. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1129. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1130. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1131. //!
  1132. //! \note This cannot be used to turn any pin into a SSI pin; it only
  1133. //! configures a SSI pin for proper operation.
  1134. //!
  1135. //! \return None.
  1136. //
  1137. //*****************************************************************************
  1138. void
  1139. GPIOPinTypeSSI(unsigned long ulPort, unsigned char ucPins)
  1140. {
  1141. //
  1142. // Check the arguments.
  1143. //
  1144. ASSERT(GPIOBaseValid(ulPort));
  1145. //
  1146. // Make the pin(s) be peripheral controlled.
  1147. //
  1148. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1149. //
  1150. // Set the pad(s) for standard push-pull operation.
  1151. //
  1152. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1153. }
  1154. //*****************************************************************************
  1155. //
  1156. //! Configures pin(s) for use by the Timer peripheral.
  1157. //!
  1158. //! \param ulPort is the base address of the GPIO port.
  1159. //! \param ucPins is the bit-packed representation of the pin(s).
  1160. //!
  1161. //! The CCP pins must be properly configured for the timer peripheral to
  1162. //! function correctly. This function provides a typical configuration for
  1163. //! those pin(s); other configurations may work as well depending upon the
  1164. //! board setup (for example, using the on-chip pull-ups).
  1165. //!
  1166. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1167. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1168. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1169. //!
  1170. //! \note This cannot be used to turn any pin into a timer pin; it only
  1171. //! configures a timer pin for proper operation.
  1172. //!
  1173. //! \return None.
  1174. //
  1175. //*****************************************************************************
  1176. void
  1177. GPIOPinTypeTimer(unsigned long ulPort, unsigned char ucPins)
  1178. {
  1179. //
  1180. // Check the arguments.
  1181. //
  1182. ASSERT(GPIOBaseValid(ulPort));
  1183. //
  1184. // Make the pin(s) be peripheral controlled.
  1185. //
  1186. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1187. //
  1188. // Set the pad(s) for standard push-pull operation.
  1189. //
  1190. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1191. }
  1192. //*****************************************************************************
  1193. //
  1194. //! Configures pin(s) for use by the UART peripheral.
  1195. //!
  1196. //! \param ulPort is the base address of the GPIO port.
  1197. //! \param ucPins is the bit-packed representation of the pin(s).
  1198. //!
  1199. //! The UART pins must be properly configured for the UART peripheral to
  1200. //! function correctly. This function provides a typical configuration for
  1201. //! those pin(s); other configurations may work as well depending upon the
  1202. //! board setup (for example, using the on-chip pull-ups).
  1203. //!
  1204. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1205. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1206. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1207. //!
  1208. //! \note This cannot be used to turn any pin into a UART pin; it only
  1209. //! configures a UART pin for proper operation.
  1210. //!
  1211. //! \return None.
  1212. //
  1213. //*****************************************************************************
  1214. void
  1215. GPIOPinTypeUART(unsigned long ulPort, unsigned char ucPins)
  1216. {
  1217. //
  1218. // Check the arguments.
  1219. //
  1220. ASSERT(GPIOBaseValid(ulPort));
  1221. //
  1222. // Make the pin(s) be peripheral controlled.
  1223. //
  1224. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1225. //
  1226. // Set the pad(s) for standard push-pull operation.
  1227. //
  1228. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1229. }
  1230. //*****************************************************************************
  1231. //
  1232. //! Configures pin(s) for use by the USB peripheral.
  1233. //!
  1234. //! \param ulPort is the base address of the GPIO port.
  1235. //! \param ucPins is the bit-packed representation of the pin(s).
  1236. //!
  1237. //! Some USB digital pins must be properly configured for the USB peripheral to
  1238. //! function correctly. This function provides a typical configuration for
  1239. //! the digital USB pin(s); other configurations may work as well depending
  1240. //! upon the board setup (for example, using the on-chip pull-ups).
  1241. //!
  1242. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1243. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1244. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1245. //!
  1246. //! \note This cannot be used to turn any pin into a USB pin; it only
  1247. //! configures a USB pin for proper operation.
  1248. //!
  1249. //! \return None.
  1250. //
  1251. //*****************************************************************************
  1252. void
  1253. GPIOPinTypeUSBDigital(unsigned long ulPort, unsigned char ucPins)
  1254. {
  1255. //
  1256. // Check the arguments.
  1257. //
  1258. ASSERT(GPIOBaseValid(ulPort));
  1259. //
  1260. // Make the pin(s) be peripheral controlled.
  1261. //
  1262. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1263. //
  1264. // Set the pad(s) for standard push-pull operation.
  1265. //
  1266. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1267. }
  1268. //*****************************************************************************
  1269. //
  1270. //! Configures pin(s) for use by the USB peripheral.
  1271. //!
  1272. //! \param ulPort is the base address of the GPIO port.
  1273. //! \param ucPins is the bit-packed representation of the pin(s).
  1274. //!
  1275. //! Some USB analog pins must be properly configured for the USB peripheral to
  1276. //! function correctly. This function provides the proper configuration for
  1277. //! those pin(s).
  1278. //!
  1279. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1280. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1281. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1282. //!
  1283. //! \note This cannot be used to turn any pin into a USB pin; it only
  1284. //! configures a USB pin for proper operation.
  1285. //!
  1286. //! \return None.
  1287. //
  1288. //*****************************************************************************
  1289. void
  1290. GPIOPinTypeUSBAnalog(unsigned long ulPort, unsigned char ucPins)
  1291. {
  1292. //
  1293. // Check the arguments.
  1294. //
  1295. ASSERT(GPIOBaseValid(ulPort));
  1296. //
  1297. // Make the pin(s) be inputs.
  1298. //
  1299. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_IN);
  1300. //
  1301. // Set the pad(s) for analog operation.
  1302. //
  1303. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_ANALOG);
  1304. }
  1305. //*****************************************************************************
  1306. //
  1307. //! Configures pin(s) for use by the I2S peripheral.
  1308. //!
  1309. //! \param ulPort is the base address of the GPIO port.
  1310. //! \param ucPins is the bit-packed representation of the pin(s).
  1311. //!
  1312. //! Some I2S pins must be properly configured for the I2S peripheral to
  1313. //! function correctly. This function provides a typical configuration for
  1314. //! the digital I2S pin(s); other configurations may work as well depending
  1315. //! upon the board setup (for example, using the on-chip pull-ups).
  1316. //!
  1317. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1318. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1319. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1320. //!
  1321. //! \note This cannot be used to turn any pin into a I2S pin; it only
  1322. //! configures a I2S pin for proper operation.
  1323. //!
  1324. //! \return None.
  1325. //
  1326. //*****************************************************************************
  1327. void
  1328. GPIOPinTypeI2S(unsigned long ulPort, unsigned char ucPins)
  1329. {
  1330. //
  1331. // Check the arguments.
  1332. //
  1333. ASSERT(GPIOBaseValid(ulPort));
  1334. //
  1335. // Make the pin(s) be peripheral controlled.
  1336. //
  1337. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1338. //
  1339. // Set the pad(s) for standard push-pull operation.
  1340. //
  1341. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1342. }
  1343. //*****************************************************************************
  1344. //
  1345. //! Configures the alternate function of a GPIO pin.
  1346. //!
  1347. //! \param ulPinConfig is the pin configuration value, specified as one of the
  1348. //! \b GPIO_P??_??? values.
  1349. //!
  1350. //! This function configures the pin mux that selects the peripheral function
  1351. //! associated with a particular GPIO pin. Only one peripheral function at a
  1352. //! time can be associated with a GPIO pin, and each peripheral function should
  1353. //! only be associated with a single GPIO pin at a time (despite the fact that
  1354. //! many of them can be associated with more than one GPIO pin).
  1355. //!
  1356. //! \note This function is only valid on Tempest-class devices.
  1357. //!
  1358. //! \return None.
  1359. //
  1360. //*****************************************************************************
  1361. void
  1362. GPIOPinConfigure(unsigned long ulPinConfig)
  1363. {
  1364. unsigned long ulBase, ulShift;
  1365. //
  1366. // Check the argument.
  1367. //
  1368. ASSERT(((ulPinConfig >> 16) & 0xff) < 9);
  1369. ASSERT(((ulPinConfig >> 8) & 0xe3) == 0);
  1370. //
  1371. // Extract the base address index from the input value.
  1372. //
  1373. ulBase = (ulPinConfig >> 16) & 0xff;
  1374. //
  1375. // Get the base address of the GPIO module, selecting either the APB or the
  1376. // AHB aperture as appropriate.
  1377. //
  1378. if(HWREG(SYSCTL_GPIOHSCTL) & (1 << ulBase))
  1379. {
  1380. ulBase = g_pulGPIOBaseAddrs[(ulBase << 1) + 1];
  1381. }
  1382. else
  1383. {
  1384. ulBase = g_pulGPIOBaseAddrs[ulBase << 1];
  1385. }
  1386. //
  1387. // Extract the shift from the input value.
  1388. //
  1389. ulShift = (ulPinConfig >> 8) & 0xff;
  1390. //
  1391. // Write the requested pin muxing value for this GPIO pin.
  1392. //
  1393. HWREG(ulBase + GPIO_O_PCTL) = ((HWREG(ulBase + GPIO_O_PCTL) &
  1394. ~(0xf << ulShift)) |
  1395. ((ulPinConfig & 0xf) << ulShift));
  1396. }
  1397. //*****************************************************************************
  1398. //
  1399. // Close the Doxygen group.
  1400. //! @}
  1401. //
  1402. //*****************************************************************************