gpio.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. //*****************************************************************************
  2. //
  3. // gpio.c - API for GPIO ports
  4. //
  5. // Copyright (c) 2005-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 6459 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup gpio_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_gpio.h"
  31. #include "inc/hw_ints.h"
  32. #include "inc/hw_memmap.h"
  33. #include "inc/hw_sysctl.h"
  34. #include "inc/hw_types.h"
  35. #include "driverlib/debug.h"
  36. #include "driverlib/gpio.h"
  37. #include "driverlib/interrupt.h"
  38. //*****************************************************************************
  39. //
  40. // The base addresses of all the GPIO modules. Both the APB and AHB apertures
  41. // are provided.
  42. //
  43. //*****************************************************************************
  44. static const unsigned long g_pulGPIOBaseAddrs[] =
  45. {
  46. GPIO_PORTA_BASE, GPIO_PORTA_AHB_BASE,
  47. GPIO_PORTB_BASE, GPIO_PORTB_AHB_BASE,
  48. GPIO_PORTC_BASE, GPIO_PORTC_AHB_BASE,
  49. GPIO_PORTD_BASE, GPIO_PORTD_AHB_BASE,
  50. GPIO_PORTE_BASE, GPIO_PORTE_AHB_BASE,
  51. GPIO_PORTF_BASE, GPIO_PORTF_AHB_BASE,
  52. GPIO_PORTG_BASE, GPIO_PORTG_AHB_BASE,
  53. GPIO_PORTH_BASE, GPIO_PORTH_AHB_BASE,
  54. GPIO_PORTJ_BASE, GPIO_PORTJ_AHB_BASE,
  55. };
  56. //*****************************************************************************
  57. //
  58. //! \internal
  59. //! Checks a GPIO base address.
  60. //!
  61. //! \param ulPort is the base address of the GPIO port.
  62. //!
  63. //! This function determines if a GPIO port base address is valid.
  64. //!
  65. //! \return Returns \b true if the base address is valid and \b false
  66. //! otherwise.
  67. //
  68. //*****************************************************************************
  69. #ifdef DEBUG
  70. static tBoolean
  71. GPIOBaseValid(unsigned long ulPort)
  72. {
  73. return((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTA_AHB_BASE) ||
  74. (ulPort == GPIO_PORTB_BASE) || (ulPort == GPIO_PORTB_AHB_BASE) ||
  75. (ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTC_AHB_BASE) ||
  76. (ulPort == GPIO_PORTD_BASE) || (ulPort == GPIO_PORTD_AHB_BASE) ||
  77. (ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTE_AHB_BASE) ||
  78. (ulPort == GPIO_PORTF_BASE) || (ulPort == GPIO_PORTF_AHB_BASE) ||
  79. (ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTG_AHB_BASE) ||
  80. (ulPort == GPIO_PORTH_BASE) || (ulPort == GPIO_PORTH_AHB_BASE) ||
  81. (ulPort == GPIO_PORTJ_BASE) || (ulPort == GPIO_PORTJ_AHB_BASE));
  82. }
  83. #endif
  84. //*****************************************************************************
  85. //
  86. //! \internal
  87. //! Gets the GPIO interrupt number.
  88. //!
  89. //! \param ulPort is the base address of the GPIO port.
  90. //!
  91. //! Given a GPIO base address, returns the corresponding interrupt number.
  92. //!
  93. //! \return Returns a GPIO interrupt number, or -1 if \e ulPort is invalid.
  94. //
  95. //*****************************************************************************
  96. static long
  97. GPIOGetIntNumber(unsigned long ulPort)
  98. {
  99. unsigned int ulInt;
  100. //
  101. // Determine the GPIO interrupt number for the given module.
  102. //
  103. switch(ulPort)
  104. {
  105. case GPIO_PORTA_BASE:
  106. case GPIO_PORTA_AHB_BASE:
  107. {
  108. ulInt = INT_GPIOA;
  109. break;
  110. }
  111. case GPIO_PORTB_BASE:
  112. case GPIO_PORTB_AHB_BASE:
  113. {
  114. ulInt = INT_GPIOB;
  115. break;
  116. }
  117. case GPIO_PORTC_BASE:
  118. case GPIO_PORTC_AHB_BASE:
  119. {
  120. ulInt = INT_GPIOC;
  121. break;
  122. }
  123. case GPIO_PORTD_BASE:
  124. case GPIO_PORTD_AHB_BASE:
  125. {
  126. ulInt = INT_GPIOD;
  127. break;
  128. }
  129. case GPIO_PORTE_BASE:
  130. case GPIO_PORTE_AHB_BASE:
  131. {
  132. ulInt = INT_GPIOE;
  133. break;
  134. }
  135. case GPIO_PORTF_BASE:
  136. case GPIO_PORTF_AHB_BASE:
  137. {
  138. ulInt = INT_GPIOF;
  139. break;
  140. }
  141. case GPIO_PORTG_BASE:
  142. case GPIO_PORTG_AHB_BASE:
  143. {
  144. ulInt = INT_GPIOG;
  145. break;
  146. }
  147. case GPIO_PORTH_BASE:
  148. case GPIO_PORTH_AHB_BASE:
  149. {
  150. ulInt = INT_GPIOH;
  151. break;
  152. }
  153. case GPIO_PORTJ_BASE:
  154. case GPIO_PORTJ_AHB_BASE:
  155. {
  156. ulInt = INT_GPIOJ;
  157. break;
  158. }
  159. default:
  160. {
  161. return(-1);
  162. }
  163. }
  164. //
  165. // Return GPIO interrupt number.
  166. //
  167. return(ulInt);
  168. }
  169. //*****************************************************************************
  170. //
  171. //! Sets the direction and mode of the specified pin(s).
  172. //!
  173. //! \param ulPort is the base address of the GPIO port
  174. //! \param ucPins is the bit-packed representation of the pin(s).
  175. //! \param ulPinIO is the pin direction and/or mode.
  176. //!
  177. //! This function will set the specified pin(s) on the selected GPIO port
  178. //! as either an input or output under software control, or it will set the
  179. //! pin to be under hardware control.
  180. //!
  181. //! The parameter \e ulPinIO is an enumerated data type that can be one of
  182. //! the following values:
  183. //!
  184. //! - \b GPIO_DIR_MODE_IN
  185. //! - \b GPIO_DIR_MODE_OUT
  186. //! - \b GPIO_DIR_MODE_HW
  187. //!
  188. //! where \b GPIO_DIR_MODE_IN specifies that the pin will be programmed as
  189. //! a software controlled input, \b GPIO_DIR_MODE_OUT specifies that the pin
  190. //! will be programmed as a software controlled output, and
  191. //! \b GPIO_DIR_MODE_HW specifies that the pin will be placed under
  192. //! hardware control.
  193. //!
  194. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  195. //! set identifies the pin to be accessed, and where bit 0 of the byte
  196. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  197. //!
  198. //! \note GPIOPadConfigSet() must also be used to configure the corresponding
  199. //! pad(s) in order for them to propagate the signal to/from the GPIO.
  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. //! This function should only be used with EPEN and PFAULT pins as all other
  1243. //! USB pins are analog in nature or are not used in devices without OTG
  1244. //! functionality.
  1245. //!
  1246. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1247. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1248. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1249. //!
  1250. //! \note This cannot be used to turn any pin into a USB pin; it only
  1251. //! configures a USB pin for proper operation.
  1252. //!
  1253. //! \return None.
  1254. //
  1255. //*****************************************************************************
  1256. void
  1257. GPIOPinTypeUSBDigital(unsigned long ulPort, unsigned char ucPins)
  1258. {
  1259. //
  1260. // Check the arguments.
  1261. //
  1262. ASSERT(GPIOBaseValid(ulPort));
  1263. //
  1264. // Make the pin(s) be peripheral controlled.
  1265. //
  1266. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1267. //
  1268. // Set the pad(s) for standard push-pull operation.
  1269. //
  1270. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1271. }
  1272. //*****************************************************************************
  1273. //
  1274. //! Configures pin(s) for use by the USB peripheral.
  1275. //!
  1276. //! \param ulPort is the base address of the GPIO port.
  1277. //! \param ucPins is the bit-packed representation of the pin(s).
  1278. //!
  1279. //! Some USB analog pins must be properly configured for the USB peripheral to
  1280. //! function correctly. This function provides the proper configuration for
  1281. //! any USB pin(s). This can also be used to configure the EPEN and PFAULT pins
  1282. //! so that they are no longer used by the USB controller.
  1283. //!
  1284. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1285. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1286. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1287. //!
  1288. //! \note This cannot be used to turn any pin into a USB pin; it only
  1289. //! configures a USB pin for proper operation.
  1290. //!
  1291. //! \return None.
  1292. //
  1293. //*****************************************************************************
  1294. void
  1295. GPIOPinTypeUSBAnalog(unsigned long ulPort, unsigned char ucPins)
  1296. {
  1297. //
  1298. // Check the arguments.
  1299. //
  1300. ASSERT(GPIOBaseValid(ulPort));
  1301. //
  1302. // Make the pin(s) be inputs.
  1303. //
  1304. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_IN);
  1305. //
  1306. // Set the pad(s) for analog operation.
  1307. //
  1308. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_ANALOG);
  1309. }
  1310. //*****************************************************************************
  1311. //
  1312. //! Configures pin(s) for use by the I2S peripheral.
  1313. //!
  1314. //! \param ulPort is the base address of the GPIO port.
  1315. //! \param ucPins is the bit-packed representation of the pin(s).
  1316. //!
  1317. //! Some I2S pins must be properly configured for the I2S peripheral to
  1318. //! function correctly. This function provides a typical configuration for
  1319. //! the digital I2S pin(s); other configurations may work as well depending
  1320. //! upon the board setup (for example, using the on-chip pull-ups).
  1321. //!
  1322. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1323. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1324. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1325. //!
  1326. //! \note This cannot be used to turn any pin into a I2S pin; it only
  1327. //! configures a I2S pin for proper operation.
  1328. //!
  1329. //! \return None.
  1330. //
  1331. //*****************************************************************************
  1332. void
  1333. GPIOPinTypeI2S(unsigned long ulPort, unsigned char ucPins)
  1334. {
  1335. //
  1336. // Check the arguments.
  1337. //
  1338. ASSERT(GPIOBaseValid(ulPort));
  1339. //
  1340. // Make the pin(s) be peripheral controlled.
  1341. //
  1342. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1343. //
  1344. // Set the pad(s) for standard push-pull operation.
  1345. //
  1346. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1347. }
  1348. //*****************************************************************************
  1349. //
  1350. //! Configures pin(s) for use by the Ethernet peripheral as LED signals.
  1351. //!
  1352. //! \param ulPort is the base address of the GPIO port.
  1353. //! \param ucPins is the bit-packed representation of the pin(s).
  1354. //!
  1355. //! The Ethernet peripheral provides two signals that can be used to drive
  1356. //! an LED (e.g. for link status/activity). This function provides a typical
  1357. //! configuration for the pins.
  1358. //!
  1359. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1360. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1361. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1362. //!
  1363. //! \note This cannot be used to turn any pin into an Ethernet LED pin; it only
  1364. //! configures an Ethernet LED pin for proper operation.
  1365. //!
  1366. //! \return None.
  1367. //
  1368. //*****************************************************************************
  1369. void
  1370. GPIOPinTypeEthernetLED(unsigned long ulPort, unsigned char ucPins)
  1371. {
  1372. //
  1373. // Check the arguments.
  1374. //
  1375. ASSERT(GPIOBaseValid(ulPort));
  1376. //
  1377. // Make the pin(s) be peripheral controlled.
  1378. //
  1379. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1380. //
  1381. // Set the pad(s) for standard push-pull operation.
  1382. //
  1383. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  1384. }
  1385. //*****************************************************************************
  1386. //
  1387. //! Configures pin(s) for use by the external peripheral interface.
  1388. //!
  1389. //! \param ulPort is the base address of the GPIO port.
  1390. //! \param ucPins is the bit-packed representation of the pin(s).
  1391. //!
  1392. //! The external peripheral interface pins must be properly configured for the
  1393. //! external peripheral interface to function correctly. This function
  1394. //! provides a typica configuration for those pin(s); other configurations may
  1395. //! work as well depending upon the board setup (for exampe, using the on-chip
  1396. //! pull-ups).
  1397. //!
  1398. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1399. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1400. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1401. //!
  1402. //! \note This cannot be used to turn any pin into an external peripheral
  1403. //! interface pin; it only configures an external peripheral interface pin for
  1404. //! proper operation.
  1405. //!
  1406. //! \return None.
  1407. //
  1408. //*****************************************************************************
  1409. void
  1410. GPIOPinTypeEPI(unsigned long ulPort, unsigned char ucPins)
  1411. {
  1412. //
  1413. // Check the arguments.
  1414. //
  1415. ASSERT(GPIOBaseValid(ulPort));
  1416. //
  1417. // Make the pin(s) be peripheral controlled.
  1418. //
  1419. GPIODirModeSet(ulPort, ucPins, GPIO_DIR_MODE_HW);
  1420. //
  1421. // Set the pad(s) for standard push-pull operation.
  1422. //
  1423. GPIOPadConfigSet(ulPort, ucPins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  1424. }
  1425. //*****************************************************************************
  1426. //
  1427. //! Configures the alternate function of a GPIO pin.
  1428. //!
  1429. //! \param ulPinConfig is the pin configuration value, specified as only one of
  1430. //! the \b GPIO_P??_??? values.
  1431. //!
  1432. //! This function configures the pin mux that selects the peripheral function
  1433. //! associated with a particular GPIO pin. Only one peripheral function at a
  1434. //! time can be associated with a GPIO pin, and each peripheral function should
  1435. //! only be associated with a single GPIO pin at a time (despite the fact that
  1436. //! many of them can be associated with more than one GPIO pin).
  1437. //!
  1438. //! \note This function is only valid on Tempest-class devices.
  1439. //!
  1440. //! \return None.
  1441. //
  1442. //*****************************************************************************
  1443. void
  1444. GPIOPinConfigure(unsigned long ulPinConfig)
  1445. {
  1446. unsigned long ulBase, ulShift;
  1447. //
  1448. // Check the argument.
  1449. //
  1450. ASSERT(((ulPinConfig >> 16) & 0xff) < 9);
  1451. ASSERT(((ulPinConfig >> 8) & 0xe3) == 0);
  1452. //
  1453. // Extract the base address index from the input value.
  1454. //
  1455. ulBase = (ulPinConfig >> 16) & 0xff;
  1456. //
  1457. // Get the base address of the GPIO module, selecting either the APB or the
  1458. // AHB aperture as appropriate.
  1459. //
  1460. if(HWREG(SYSCTL_GPIOHSCTL) & (1 << ulBase))
  1461. {
  1462. ulBase = g_pulGPIOBaseAddrs[(ulBase << 1) + 1];
  1463. }
  1464. else
  1465. {
  1466. ulBase = g_pulGPIOBaseAddrs[ulBase << 1];
  1467. }
  1468. //
  1469. // Extract the shift from the input value.
  1470. //
  1471. ulShift = (ulPinConfig >> 8) & 0xff;
  1472. //
  1473. // Write the requested pin muxing value for this GPIO pin.
  1474. //
  1475. HWREG(ulBase + GPIO_O_PCTL) = ((HWREG(ulBase + GPIO_O_PCTL) &
  1476. ~(0xf << ulShift)) |
  1477. ((ulPinConfig & 0xf) << ulShift));
  1478. }
  1479. //*****************************************************************************
  1480. //
  1481. // Close the Doxygen group.
  1482. //! @}
  1483. //
  1484. //*****************************************************************************