gpio.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  1. //*****************************************************************************
  2. //
  3. // gpio.c - API for GPIO ports
  4. //
  5. // Copyright (c) 2005-2017 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //*****************************************************************************
  37. //*****************************************************************************
  38. //
  39. //! \addtogroup gpio_api
  40. //! @{
  41. //
  42. //*****************************************************************************
  43. #include <msp432e401y.h>
  44. #include "types.h"
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_gpio.h"
  48. #include "inc/hw_sysctl.h"
  49. #include "debug.h"
  50. #include "gpio.h"
  51. #include "interrupt.h"
  52. //*****************************************************************************
  53. //
  54. // A mapping of GPIO port address to interrupt number.
  55. //
  56. //*****************************************************************************
  57. static const uint32_t g_ppui32GPIOIntMap[][2] =
  58. {
  59. { GPIO_PORTA_BASE, INT_GPIOA },
  60. { GPIO_PORTB_BASE, INT_GPIOB },
  61. { GPIO_PORTC_BASE, INT_GPIOC },
  62. { GPIO_PORTD_BASE, INT_GPIOD },
  63. { GPIO_PORTE_BASE, INT_GPIOE },
  64. { GPIO_PORTF_BASE, INT_GPIOF },
  65. { GPIO_PORTG_BASE, INT_GPIOG },
  66. { GPIO_PORTH_BASE, INT_GPIOH },
  67. { GPIO_PORTJ_BASE, INT_GPIOJ },
  68. { GPIO_PORTK_BASE, INT_GPIOK },
  69. { GPIO_PORTL_BASE, INT_GPIOL },
  70. { GPIO_PORTM_BASE, INT_GPIOM },
  71. { GPIO_PORTN_BASE, INT_GPION },
  72. { GPIO_PORTP_BASE, INT_GPIOP0 },
  73. { GPIO_PORTQ_BASE, INT_GPIOQ0 },
  74. #ifdef __MCU_HAS_GPIOR__
  75. { GPIO_PORTR_BASE, INT_GPIOR },
  76. #endif
  77. #ifdef __MCU_HAS_GPIOS__
  78. { GPIO_PORTS_BASE, INT_GPIOS },
  79. #endif
  80. #ifdef __MCU_HAS_GPIOT__
  81. { GPIO_PORTT_BASE, INT_GPIOT },
  82. #endif
  83. };
  84. static const uint_fast32_t g_ui32GPIOIntMapRows =
  85. (sizeof(g_ppui32GPIOIntMap) /
  86. sizeof(g_ppui32GPIOIntMap[0]));
  87. //*****************************************************************************
  88. //
  89. // The base addresses of all the GPIO modules.
  90. //
  91. //*****************************************************************************
  92. static const uint32_t g_pui32GPIOBaseAddrs[] =
  93. {
  94. GPIO_PORTA_BASE,
  95. GPIO_PORTB_BASE,
  96. GPIO_PORTC_BASE,
  97. GPIO_PORTD_BASE,
  98. GPIO_PORTE_BASE,
  99. GPIO_PORTF_BASE,
  100. GPIO_PORTG_BASE,
  101. GPIO_PORTH_BASE,
  102. GPIO_PORTJ_BASE,
  103. GPIO_PORTK_BASE,
  104. GPIO_PORTL_BASE,
  105. GPIO_PORTM_BASE,
  106. GPIO_PORTN_BASE,
  107. GPIO_PORTP_BASE,
  108. GPIO_PORTQ_BASE,
  109. #ifdef __MCU_HAS_GPIOR__
  110. GPIO_PORTR_BASE,
  111. #endif
  112. #ifdef __MCU_HAS_GPIOS__
  113. GPIO_PORTS_BASE,
  114. #endif
  115. #ifdef __MCU_HAS_GPIOT__
  116. GPIO_PORTT_BASE,
  117. #endif
  118. };
  119. //*****************************************************************************
  120. //
  121. //! \internal
  122. //! Checks a GPIO base address.
  123. //!
  124. //! \param ui32Port is the base address of the GPIO port.
  125. //!
  126. //! This function determines if a GPIO port base address is valid.
  127. //!
  128. //! \return Returns \b true if the base address is valid and \b false
  129. //! otherwise.
  130. //
  131. //*****************************************************************************
  132. #ifdef DEBUG
  133. static bool
  134. _GPIOBaseValid(uint32_t ui32Port)
  135. {
  136. return ((ui32Port == GPIO_PORTA_BASE) ||
  137. (ui32Port == GPIO_PORTB_BASE) ||
  138. (ui32Port == GPIO_PORTC_BASE) ||
  139. (ui32Port == GPIO_PORTD_BASE) ||
  140. (ui32Port == GPIO_PORTE_BASE) ||
  141. (ui32Port == GPIO_PORTF_BASE) ||
  142. (ui32Port == GPIO_PORTG_BASE) ||
  143. (ui32Port == GPIO_PORTH_BASE) ||
  144. (ui32Port == GPIO_PORTJ_BASE) ||
  145. (ui32Port == GPIO_PORTK_BASE) ||
  146. (ui32Port == GPIO_PORTL_BASE) ||
  147. (ui32Port == GPIO_PORTM_BASE) ||
  148. (ui32Port == GPIO_PORTN_BASE) ||
  149. (ui32Port == GPIO_PORTP_BASE) ||
  150. (ui32Port == GPIO_PORTQ_BASE) ||
  151. (ui32Port == GPIO_PORTR_BASE) ||
  152. (ui32Port == GPIO_PORTS_BASE) ||
  153. (ui32Port == GPIO_PORTT_BASE));
  154. }
  155. #endif
  156. //*****************************************************************************
  157. //
  158. //! Gets the GPIO interrupt number.
  159. //!
  160. //! \param ui32Port is the base address of the GPIO port.
  161. //!
  162. //! Given a GPIO base address, this function returns the corresponding
  163. //! interrupt number.
  164. //!
  165. //! \return Returns a GPIO interrupt number, or 0 if \e ui32Port is invalid.
  166. //
  167. //*****************************************************************************
  168. static uint32_t
  169. _GPIOIntNumberGet(uint32_t ui32Port)
  170. {
  171. uint_fast32_t ui32Idx, ui32Rows;
  172. const uint32_t (*ppui32GPIOIntMap)[2];
  173. //
  174. // Check the arguments.
  175. //
  176. ASSERT(_GPIOBaseValid(ui32Port));
  177. ppui32GPIOIntMap = g_ppui32GPIOIntMap;
  178. ui32Rows = g_ui32GPIOIntMapRows;
  179. //
  180. // Loop through the table that maps GPIO base addresses to interrupt
  181. // numbers.
  182. //
  183. for (ui32Idx = 0; ui32Idx < ui32Rows; ui32Idx++)
  184. {
  185. //
  186. // See if this base address matches.
  187. //
  188. if (ppui32GPIOIntMap[ui32Idx][0] == ui32Port)
  189. {
  190. //
  191. // Return the corresponding interrupt number.
  192. //
  193. return (ppui32GPIOIntMap[ui32Idx][1]);
  194. }
  195. }
  196. //
  197. // The base address could not be found, so return an error.
  198. //
  199. return (0);
  200. }
  201. //*****************************************************************************
  202. //
  203. //! Sets the direction and mode of the specified pin(s).
  204. //!
  205. //! \param ui32Port is the base address of the GPIO port
  206. //! \param ui8Pins is the bit-packed representation of the pin(s).
  207. //! \param ui32PinIO is the pin direction and/or mode.
  208. //!
  209. //! This function configures the specified pin(s) on the selected GPIO port
  210. //! as either input or output under software control, or it configures the
  211. //! pin to be under hardware control.
  212. //!
  213. //! The parameter \e ui32PinIO is an enumerated data type that can be one of
  214. //! the following values:
  215. //!
  216. //! - \b GPIO_DIR_MODE_IN
  217. //! - \b GPIO_DIR_MODE_OUT
  218. //! - \b GPIO_DIR_MODE_HW
  219. //!
  220. //! where \b GPIO_DIR_MODE_IN specifies that the pin is programmed as a
  221. //! software controlled input, \b GPIO_DIR_MODE_OUT specifies that the pin is
  222. //! programmed as a software controlled output, and \b GPIO_DIR_MODE_HW
  223. //! specifies that the pin is placed under hardware control.
  224. //!
  225. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  226. //! set identifies the pin to be accessed, and where bit 0 of the byte
  227. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  228. //!
  229. //! \note GPIOPadConfigSet() must also be used to configure the corresponding
  230. //! pad(s) in order for them to propagate the signal to/from the GPIO.
  231. //!
  232. //! \note A subset of GPIO pins, notably those used by the
  233. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  234. //! locked against inadvertent reconfiguration. These pins must be unlocked
  235. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  236. //! registers before this function can be called.
  237. //!
  238. //! \return None.
  239. //
  240. //*****************************************************************************
  241. void
  242. GPIODirModeSet(uint32_t ui32Port, uint8_t ui8Pins, uint32_t ui32PinIO)
  243. {
  244. //
  245. // Check the arguments.
  246. //
  247. ASSERT(_GPIOBaseValid(ui32Port));
  248. ASSERT((ui32PinIO == GPIO_DIR_MODE_IN) ||
  249. (ui32PinIO == GPIO_DIR_MODE_OUT) ||
  250. (ui32PinIO == GPIO_DIR_MODE_HW));
  251. //
  252. // Set the pin direction and mode.
  253. //
  254. HWREG(ui32Port + GPIO_O_DIR) = ((ui32PinIO & 1) ?
  255. (HWREG(ui32Port + GPIO_O_DIR) | ui8Pins) :
  256. (HWREG(ui32Port + GPIO_O_DIR) & ~(ui8Pins)));
  257. HWREG(ui32Port + GPIO_O_AFSEL) = ((ui32PinIO & 2) ?
  258. (HWREG(ui32Port + GPIO_O_AFSEL) |
  259. ui8Pins) :
  260. (HWREG(ui32Port + GPIO_O_AFSEL) &
  261. ~(ui8Pins)));
  262. }
  263. //*****************************************************************************
  264. //
  265. //! Gets the direction and mode of a pin.
  266. //!
  267. //! \param ui32Port is the base address of the GPIO port.
  268. //! \param ui8Pin is the pin number.
  269. //!
  270. //! This function gets the direction and control mode for a specified pin on
  271. //! the selected GPIO port. The pin can be configured as either an input or
  272. //! output under software control, or it can be under hardware control. The
  273. //! type of control and direction are returned as an enumerated data type.
  274. //!
  275. //! \return Returns one of the enumerated data types described for
  276. //! GPIODirModeSet().
  277. //
  278. //*****************************************************************************
  279. uint32_t
  280. GPIODirModeGet(uint32_t ui32Port, uint8_t ui8Pin)
  281. {
  282. uint32_t ui32Dir, ui32AFSEL;
  283. //
  284. // Check the arguments.
  285. //
  286. ASSERT(_GPIOBaseValid(ui32Port));
  287. ASSERT(ui8Pin < 8);
  288. //
  289. // Convert from a pin number to a bit position.
  290. //
  291. ui8Pin = 1 << ui8Pin;
  292. //
  293. // Return the pin direction and mode.
  294. //
  295. ui32Dir = HWREG(ui32Port + GPIO_O_DIR);
  296. ui32AFSEL = HWREG(ui32Port + GPIO_O_AFSEL);
  297. return (((ui32Dir & ui8Pin) ? 1 : 0) | ((ui32AFSEL & ui8Pin) ? 2 : 0));
  298. }
  299. //*****************************************************************************
  300. //
  301. //! Sets the interrupt type for the specified pin(s).
  302. //!
  303. //! \param ui32Port is the base address of the GPIO port.
  304. //! \param ui8Pins is the bit-packed representation of the pin(s).
  305. //! \param ui32IntType specifies the type of interrupt trigger mechanism.
  306. //!
  307. //! This function sets up the various interrupt trigger mechanisms for the
  308. //! specified pin(s) on the selected GPIO port.
  309. //!
  310. //! One of the following flags can be used to define the \e ui32IntType
  311. //! parameter:
  312. //!
  313. //! - \b GPIO_FALLING_EDGE sets detection to edge and trigger to falling
  314. //! - \b GPIO_RISING_EDGE sets detection to edge and trigger to rising
  315. //! - \b GPIO_BOTH_EDGES sets detection to both edges
  316. //! - \b GPIO_LOW_LEVEL sets detection to low level
  317. //! - \b GPIO_HIGH_LEVEL sets detection to high level
  318. //!
  319. //! In addition to the above flags, the following flag can be OR'd in to the
  320. //! \e ui32IntType parameter:
  321. //!
  322. //! - \b GPIO_DISCRETE_INT sets discrete interrupts for each pin on a GPIO
  323. //! port.
  324. //!
  325. //! The \b GPIO_DISCRETE_INT is only available on ports P and Q.
  326. //!
  327. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  328. //! set identifies the pin to be accessed, and where bit 0 of the byte
  329. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  330. //!
  331. //! \note In order to avoid any spurious interrupts, the user must ensure that
  332. //! the GPIO inputs remain stable for the duration of this function.
  333. //!
  334. //! \return None.
  335. //
  336. //*****************************************************************************
  337. void
  338. GPIOIntTypeSet(uint32_t ui32Port, uint8_t ui8Pins,
  339. uint32_t ui32IntType)
  340. {
  341. //
  342. // Check the arguments.
  343. //
  344. ASSERT(_GPIOBaseValid(ui32Port));
  345. ASSERT(((ui32IntType & 0xF) == GPIO_FALLING_EDGE) ||
  346. ((ui32IntType & 0xF) == GPIO_RISING_EDGE) ||
  347. ((ui32IntType & 0xF) == GPIO_BOTH_EDGES) ||
  348. ((ui32IntType & 0xF) == GPIO_LOW_LEVEL) ||
  349. ((ui32IntType & 0xF) == GPIO_HIGH_LEVEL));
  350. ASSERT(((ui32IntType & 0x000F0000) == 0) ||
  351. (((ui32IntType & 0x000F0000) == GPIO_DISCRETE_INT) &&
  352. ((ui32Port == GPIO_PORTP_BASE) || (ui32Port == GPIO_PORTQ_BASE))));
  353. //
  354. // Set the pin interrupt type.
  355. //
  356. HWREG(ui32Port + GPIO_O_IBE) = ((ui32IntType & 1) ?
  357. (HWREG(ui32Port + GPIO_O_IBE) | ui8Pins) :
  358. (HWREG(ui32Port + GPIO_O_IBE) & ~(ui8Pins)));
  359. HWREG(ui32Port + GPIO_O_IS) = ((ui32IntType & 2) ?
  360. (HWREG(ui32Port + GPIO_O_IS) | ui8Pins) :
  361. (HWREG(ui32Port + GPIO_O_IS) & ~(ui8Pins)));
  362. HWREG(ui32Port + GPIO_O_IEV) = ((ui32IntType & 4) ?
  363. (HWREG(ui32Port + GPIO_O_IEV) | ui8Pins) :
  364. (HWREG(ui32Port + GPIO_O_IEV) & ~(ui8Pins)));
  365. //
  366. // Set or clear the discrete interrupt feature.
  367. //
  368. HWREG(ui32Port + GPIO_O_SI) = ((ui32IntType & 0x10000) ?
  369. (HWREG(ui32Port + GPIO_O_SI) | 0x01) :
  370. (HWREG(ui32Port + GPIO_O_SI) & ~(0x01)));
  371. }
  372. //*****************************************************************************
  373. //
  374. //! Gets the interrupt type for a pin.
  375. //!
  376. //! \param ui32Port is the base address of the GPIO port.
  377. //! \param ui8Pin is the pin number.
  378. //!
  379. //! This function gets the interrupt type for a specified pin on the selected
  380. //! GPIO port. The pin can be configured as a falling-edge, rising-edge, or
  381. //! both-edges detected interrupt, or it can be configured as a low-level or
  382. //! high-level detected interrupt. The type of interrupt detection mechanism
  383. //! is returned and can include the \b GPIO_DISCRETE_INT flag.
  384. //!
  385. //! \return Returns one of the flags described for GPIOIntTypeSet().
  386. //
  387. //*****************************************************************************
  388. uint32_t
  389. GPIOIntTypeGet(uint32_t ui32Port, uint8_t ui8Pin)
  390. {
  391. uint32_t ui32IBE, ui32IS, ui32IEV, ui32SI;
  392. //
  393. // Check the arguments.
  394. //
  395. ASSERT(_GPIOBaseValid(ui32Port));
  396. ASSERT(ui8Pin < 8);
  397. //
  398. // Convert from a pin number to a bit position.
  399. //
  400. ui8Pin = 1 << ui8Pin;
  401. //
  402. // Return the pin interrupt type.
  403. //
  404. ui32IBE = HWREG(ui32Port + GPIO_O_IBE);
  405. ui32IS = HWREG(ui32Port + GPIO_O_IS);
  406. ui32IEV = HWREG(ui32Port + GPIO_O_IEV);
  407. ui32SI = HWREG(ui32Port + GPIO_O_SI);
  408. return (((ui32IBE & ui8Pin) ? 1 : 0) | ((ui32IS & ui8Pin) ? 2 : 0) |
  409. ((ui32IEV & ui8Pin) ? 4 : 0) | (ui32SI & 0x01) ? 0x10000 : 0);
  410. }
  411. //*****************************************************************************
  412. //
  413. //! Sets the pad configuration for the specified pin(s).
  414. //!
  415. //! \param ui32Port is the base address of the GPIO port.
  416. //! \param ui8Pins is the bit-packed representation of the pin(s).
  417. //! \param ui32Strength specifies the output drive strength.
  418. //! \param ui32PinType specifies the pin type.
  419. //!
  420. //! This function sets the drive strength and type for the specified pin(s)
  421. //! on the selected GPIO port. For pin(s) configured as input ports, the
  422. //! pad is configured as requested, but the only real effect on the input
  423. //! is the configuration of the pull-up or pull-down termination.
  424. //!
  425. //! The parameter \e ui32Strength can be one of the following values:
  426. //!
  427. //! - \b GPIO_STRENGTH_2MA
  428. //! - \b GPIO_STRENGTH_4MA
  429. //! - \b GPIO_STRENGTH_8MA
  430. //! - \b GPIO_STRENGTH_8MA_SC
  431. //! - \b GPIO_STRENGTH_6MA
  432. //! - \b GPIO_STRENGTH_10MA
  433. //! - \b GPIO_STRENGTH_12MA
  434. //!
  435. //! where \b GPIO_STRENGTH_xMA specifies either 2, 4, or 8 mA output drive
  436. //! strength, and \b GPIO_OUT_STRENGTH_8MA_SC specifies 8 mA output drive with
  437. //! slew control.
  438. //!
  439. //! It can also support output drive strengths of 6, 10, and 12
  440. //! mA.
  441. //!
  442. //! The parameter \e ui32PinType can be one of the following values:
  443. //!
  444. //! - \b GPIO_PIN_TYPE_STD
  445. //! - \b GPIO_PIN_TYPE_STD_WPU
  446. //! - \b GPIO_PIN_TYPE_STD_WPD
  447. //! - \b GPIO_PIN_TYPE_OD
  448. //! - \b GPIO_PIN_TYPE_ANALOG
  449. //! - \b GPIO_PIN_TYPE_WAKE_HIGH
  450. //! - \b GPIO_PIN_TYPE_WAKE_LOW
  451. //!
  452. //! where \b GPIO_PIN_TYPE_STD* specifies a push-pull pin, \b GPIO_PIN_TYPE_OD*
  453. //! specifies an open-drain pin, \b *_WPU specifies a weak pull-up, \b *_WPD
  454. //! specifies a weak pull-down, and \b GPIO_PIN_TYPE_ANALOG specifies an analog
  455. //! input.
  456. //!
  457. //! The \b GPIO_PIN_TYPE_WAKE_* settings specify the pin to be used as a
  458. //! hibernation wake source. The pin sense level can be high or low.
  459. //!
  460. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  461. //! set identifies the pin to be accessed, and where bit 0 of the byte
  462. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  463. //!
  464. //! \note A subset of GPIO pins, notably those used by the
  465. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  466. //! locked against inadvertent reconfiguration. These pins must be unlocked
  467. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  468. //! registers before this function can be called.
  469. //!
  470. //! \return None.
  471. //
  472. //*****************************************************************************
  473. void
  474. GPIOPadConfigSet(uint32_t ui32Port, uint8_t ui8Pins,
  475. uint32_t ui32Strength, uint32_t ui32PinType)
  476. {
  477. uint8_t ui8Bit;
  478. //
  479. // Check the arguments.
  480. //
  481. ASSERT(_GPIOBaseValid(ui32Port));
  482. ASSERT((ui32Strength == GPIO_STRENGTH_2MA) ||
  483. (ui32Strength == GPIO_STRENGTH_4MA) ||
  484. (ui32Strength == GPIO_STRENGTH_6MA) ||
  485. (ui32Strength == GPIO_STRENGTH_8MA) ||
  486. (ui32Strength == GPIO_STRENGTH_8MA_SC) ||
  487. (ui32Strength == GPIO_STRENGTH_10MA) ||
  488. (ui32Strength == GPIO_STRENGTH_12MA));
  489. ASSERT((ui32PinType == GPIO_PIN_TYPE_STD) ||
  490. (ui32PinType == GPIO_PIN_TYPE_STD_WPU) ||
  491. (ui32PinType == GPIO_PIN_TYPE_STD_WPD) ||
  492. (ui32PinType == GPIO_PIN_TYPE_OD) ||
  493. (ui32PinType == GPIO_PIN_TYPE_WAKE_LOW) ||
  494. (ui32PinType == GPIO_PIN_TYPE_WAKE_HIGH) ||
  495. (ui32PinType == GPIO_PIN_TYPE_ANALOG));
  496. //
  497. // Set the GPIO peripheral configuration register first as required.
  498. // Walk pins 0-7 and clear or set the provided PC[EDMn] encoding.
  499. //
  500. for (ui8Bit = 0; ui8Bit < 8; ui8Bit++)
  501. {
  502. if (ui8Pins & (1 << ui8Bit))
  503. {
  504. HWREG(ui32Port + GPIO_O_PC) = (HWREG(ui32Port + GPIO_O_PC) &
  505. ~(0x3 << (2 * ui8Bit)));
  506. HWREG(ui32Port + GPIO_O_PC) |= (((ui32Strength >> 5) & 0x3) <<
  507. (2 * ui8Bit));
  508. }
  509. }
  510. //
  511. // Set the output drive strength.
  512. //
  513. HWREG(ui32Port + GPIO_O_DR2R) = ((ui32Strength & 1) ?
  514. (HWREG(ui32Port + GPIO_O_DR2R) |
  515. ui8Pins) :
  516. (HWREG(ui32Port + GPIO_O_DR2R) &
  517. ~(ui8Pins)));
  518. HWREG(ui32Port + GPIO_O_DR4R) = ((ui32Strength & 2) ?
  519. (HWREG(ui32Port + GPIO_O_DR4R) |
  520. ui8Pins) :
  521. (HWREG(ui32Port + GPIO_O_DR4R) &
  522. ~(ui8Pins)));
  523. HWREG(ui32Port + GPIO_O_DR8R) = ((ui32Strength & 4) ?
  524. (HWREG(ui32Port + GPIO_O_DR8R) |
  525. ui8Pins) :
  526. (HWREG(ui32Port + GPIO_O_DR8R) &
  527. ~(ui8Pins)));
  528. HWREG(ui32Port + GPIO_O_SLR) = ((ui32Strength & 8) ?
  529. (HWREG(ui32Port + GPIO_O_SLR) |
  530. ui8Pins) :
  531. (HWREG(ui32Port + GPIO_O_SLR) &
  532. ~(ui8Pins)));
  533. //
  534. // Set the 12-mA drive select register.
  535. //
  536. HWREG(ui32Port + GPIO_O_DR12R) = ((ui32Strength & 0x10) ?
  537. (HWREG(ui32Port + GPIO_O_DR12R) |
  538. ui8Pins) :
  539. (HWREG(ui32Port + GPIO_O_DR12R) &
  540. ~(ui8Pins)));
  541. //
  542. // Set the pin type.
  543. //
  544. HWREG(ui32Port + GPIO_O_ODR) = ((ui32PinType & 1) ?
  545. (HWREG(ui32Port + GPIO_O_ODR) | ui8Pins) :
  546. (HWREG(ui32Port + GPIO_O_ODR) & ~(ui8Pins)));
  547. HWREG(ui32Port + GPIO_O_PUR) = ((ui32PinType & 2) ?
  548. (HWREG(ui32Port + GPIO_O_PUR) | ui8Pins) :
  549. (HWREG(ui32Port + GPIO_O_PUR) & ~(ui8Pins)));
  550. HWREG(ui32Port + GPIO_O_PDR) = ((ui32PinType & 4) ?
  551. (HWREG(ui32Port + GPIO_O_PDR) | ui8Pins) :
  552. (HWREG(ui32Port + GPIO_O_PDR) & ~(ui8Pins)));
  553. HWREG(ui32Port + GPIO_O_DEN) = ((ui32PinType & 8) ?
  554. (HWREG(ui32Port + GPIO_O_DEN) | ui8Pins) :
  555. (HWREG(ui32Port + GPIO_O_DEN) & ~(ui8Pins)));
  556. //
  557. // Set the wake pin enable register and the wake level register.
  558. //
  559. HWREG(ui32Port + GPIO_O_WAKELVL) = ((ui32PinType & 0x200) ?
  560. (HWREG(ui32Port + GPIO_O_WAKELVL) |
  561. ui8Pins) :
  562. (HWREG(ui32Port + GPIO_O_WAKELVL) &
  563. ~(ui8Pins)));
  564. HWREG(ui32Port + GPIO_O_WAKEPEN) = ((ui32PinType & 0x300) ?
  565. (HWREG(ui32Port + GPIO_O_WAKEPEN) |
  566. ui8Pins) :
  567. (HWREG(ui32Port + GPIO_O_WAKEPEN) &
  568. ~(ui8Pins)));
  569. //
  570. // Set the analog mode select register.
  571. //
  572. HWREG(ui32Port + GPIO_O_AMSEL) =
  573. ((ui32PinType == GPIO_PIN_TYPE_ANALOG) ?
  574. (HWREG(ui32Port + GPIO_O_AMSEL) | ui8Pins) :
  575. (HWREG(ui32Port + GPIO_O_AMSEL) & ~(ui8Pins)));
  576. }
  577. //*****************************************************************************
  578. //
  579. //! Gets the pad configuration for a pin.
  580. //!
  581. //! \param ui32Port is the base address of the GPIO port.
  582. //! \param ui8Pin is the pin number.
  583. //! \param pui32Strength is a pointer to storage for the output drive strength.
  584. //! \param pui32PinType is a pointer to storage for the output drive type.
  585. //!
  586. //! This function gets the pad configuration for a specified pin on the
  587. //! selected GPIO port. The values returned in \e pui32Strength and
  588. //! \e pui32PinType correspond to the values used in GPIOPadConfigSet(). This
  589. //! function also works for pin(s) configured as input pin(s); however, the
  590. //! only meaningful data returned is whether the pin is terminated with a
  591. //! pull-up or down resistor.
  592. //!
  593. //! \return None
  594. //
  595. //*****************************************************************************
  596. void
  597. GPIOPadConfigGet(uint32_t ui32Port, uint8_t ui8Pin,
  598. uint32_t *pui32Strength, uint32_t *pui32PinType)
  599. {
  600. uint32_t ui32PinType, ui32Strength;
  601. //
  602. // Check the arguments.
  603. //
  604. ASSERT(_GPIOBaseValid(ui32Port));
  605. ASSERT(ui8Pin < 8);
  606. //
  607. // Convert from a pin number to a bit position.
  608. //
  609. ui8Pin = (1 << ui8Pin);
  610. //
  611. // Get the drive strength for this pin.
  612. //
  613. ui32Strength = ((HWREG(ui32Port + GPIO_O_DR2R) & ui8Pin) ? 1 : 0);
  614. ui32Strength |= ((HWREG(ui32Port + GPIO_O_DR4R) & ui8Pin) ? 2 : 0);
  615. ui32Strength |= ((HWREG(ui32Port + GPIO_O_DR8R) & ui8Pin) ? 4 : 0);
  616. ui32Strength |= ((HWREG(ui32Port + GPIO_O_SLR) & ui8Pin) ? 8 : 0);
  617. ui32Strength |= ((HWREG(ui32Port + GPIO_O_DR12R) & ui8Pin) ? 0x10 : 0);
  618. ui32Strength |= (((HWREG(ui32Port + GPIO_O_PC) >>
  619. (2 * ui8Pin)) & 0x3) << 5);
  620. *pui32Strength = ui32Strength;
  621. //
  622. // Get the pin type.
  623. //
  624. ui32PinType = ((HWREG(ui32Port + GPIO_O_ODR) & ui8Pin) ? 1 : 0);
  625. ui32PinType |= ((HWREG(ui32Port + GPIO_O_PUR) & ui8Pin) ? 2 : 0);
  626. ui32PinType |= ((HWREG(ui32Port + GPIO_O_PDR) & ui8Pin) ? 4 : 0);
  627. ui32PinType |= ((HWREG(ui32Port + GPIO_O_DEN) & ui8Pin) ? 8 : 0);
  628. if (HWREG(ui32Port + GPIO_O_WAKEPEN) & ui8Pin)
  629. {
  630. ui32PinType |= ((HWREG(ui32Port + GPIO_O_WAKELVL) & ui8Pin) ?
  631. 0x200 : 0x100);
  632. }
  633. *pui32PinType = ui32PinType;
  634. }
  635. //*****************************************************************************
  636. //
  637. //! Enables the specified GPIO interrupts.
  638. //!
  639. //! \param ui32Port is the base address of the GPIO port.
  640. //! \param ui32IntFlags is the bit mask of the interrupt sources to enable.
  641. //!
  642. //! This function enables the indicated GPIO interrupt sources. Only the
  643. //! sources that are enabled can be reflected to the processor interrupt;
  644. //! disabled sources have no effect on the processor.
  645. //!
  646. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  647. //!
  648. //! - \b GPIO_INT_PIN_0 - interrupt due to activity on Pin 0.
  649. //! - \b GPIO_INT_PIN_1 - interrupt due to activity on Pin 1.
  650. //! - \b GPIO_INT_PIN_2 - interrupt due to activity on Pin 2.
  651. //! - \b GPIO_INT_PIN_3 - interrupt due to activity on Pin 3.
  652. //! - \b GPIO_INT_PIN_4 - interrupt due to activity on Pin 4.
  653. //! - \b GPIO_INT_PIN_5 - interrupt due to activity on Pin 5.
  654. //! - \b GPIO_INT_PIN_6 - interrupt due to activity on Pin 6.
  655. //! - \b GPIO_INT_PIN_7 - interrupt due to activity on Pin 7.
  656. //! - \b GPIO_INT_DMA - interrupt due to DMA activity on this GPIO module.
  657. //!
  658. //! \note If this call is being used to enable summary interrupts on GPIO port
  659. //! P or Q (GPIOIntTypeSet() with GPIO_DISCRETE_INT not enabled), then all
  660. //! individual interrupts for these ports must be enabled in the GPIO module
  661. //! using GPIOIntEnable() and all but the interrupt for pin 0 must be disabled
  662. //! in the NVIC using the IntDisable() function. The summary interrupts for
  663. //! the ports are routed to the INT_GPIOP0 or INT_GPIOQ0 which must be enabled
  664. //! to handle the interrupt. If this is not done then any individual GPIO pin
  665. //! interrupts that are left enabled also trigger the individual interrupts.
  666. //!
  667. //! \return None.
  668. //
  669. //*****************************************************************************
  670. void
  671. GPIOIntEnable(uint32_t ui32Port, uint32_t ui32IntFlags)
  672. {
  673. //
  674. // Check the arguments.
  675. //
  676. ASSERT(_GPIOBaseValid(ui32Port));
  677. //
  678. // Enable the interrupts.
  679. //
  680. HWREG(ui32Port + GPIO_O_IM) |= ui32IntFlags;
  681. }
  682. //*****************************************************************************
  683. //
  684. //! Disables the specified GPIO interrupts.
  685. //!
  686. //! \param ui32Port is the base address of the GPIO port.
  687. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  688. //!
  689. //! This function disables the indicated GPIO interrupt sources. Only the
  690. //! sources that are enabled can be reflected to the processor interrupt;
  691. //! disabled sources have no effect on the processor.
  692. //!
  693. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  694. //!
  695. //! - \b GPIO_INT_PIN_0 - interrupt due to activity on Pin 0.
  696. //! - \b GPIO_INT_PIN_1 - interrupt due to activity on Pin 1.
  697. //! - \b GPIO_INT_PIN_2 - interrupt due to activity on Pin 2.
  698. //! - \b GPIO_INT_PIN_3 - interrupt due to activity on Pin 3.
  699. //! - \b GPIO_INT_PIN_4 - interrupt due to activity on Pin 4.
  700. //! - \b GPIO_INT_PIN_5 - interrupt due to activity on Pin 5.
  701. //! - \b GPIO_INT_PIN_6 - interrupt due to activity on Pin 6.
  702. //! - \b GPIO_INT_PIN_7 - interrupt due to activity on Pin 7.
  703. //! - \b GPIO_INT_DMA - interrupt due to DMA activity on this GPIO module.
  704. //!
  705. //! \return None.
  706. //
  707. //*****************************************************************************
  708. void
  709. GPIOIntDisable(uint32_t ui32Port, uint32_t ui32IntFlags)
  710. {
  711. //
  712. // Check the arguments.
  713. //
  714. ASSERT(_GPIOBaseValid(ui32Port));
  715. //
  716. // Disable the interrupts.
  717. //
  718. HWREG(ui32Port + GPIO_O_IM) &= ~(ui32IntFlags);
  719. }
  720. //*****************************************************************************
  721. //
  722. //! Gets interrupt status for the specified GPIO port.
  723. //!
  724. //! \param ui32Port is the base address of the GPIO port.
  725. //! \param bMasked specifies whether masked or raw interrupt status is
  726. //! returned.
  727. //!
  728. //! If \e bMasked is set as \b true, then the masked interrupt status is
  729. //! returned; otherwise, the raw interrupt status is returned.
  730. //!
  731. //! \return Returns the current interrupt status for the specified GPIO module.
  732. //! The value returned is the logical OR of the \b GPIO_INT_* values that are
  733. //! currently active.
  734. //
  735. //*****************************************************************************
  736. uint32_t
  737. GPIOIntStatus(uint32_t ui32Port, bool bMasked)
  738. {
  739. //
  740. // Check the arguments.
  741. //
  742. ASSERT(_GPIOBaseValid(ui32Port));
  743. //
  744. // Return the interrupt status.
  745. //
  746. if (bMasked)
  747. {
  748. return (HWREG(ui32Port + GPIO_O_MIS));
  749. }
  750. else
  751. {
  752. return (HWREG(ui32Port + GPIO_O_RIS));
  753. }
  754. }
  755. //*****************************************************************************
  756. //
  757. //! Clears the specified interrupt sources.
  758. //!
  759. //! \param ui32Port is the base address of the GPIO port.
  760. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  761. //!
  762. //! Clears the interrupt for the specified interrupt source(s).
  763. //!
  764. //! The \e ui32IntFlags parameter is the logical OR of the \b GPIO_INT_*
  765. //! values.
  766. //!
  767. //! \note Because there is a write buffer in the Cortex-M processor, it may
  768. //! take several clock cycles before the interrupt source is actually cleared.
  769. //! Therefore, it is recommended that the interrupt source be cleared early in
  770. //! the interrupt handler (as opposed to the very last action) to avoid
  771. //! returning from the interrupt handler before the interrupt source is
  772. //! actually cleared. Failure to do so may result in the interrupt handler
  773. //! being immediately reentered (because the interrupt controller still sees
  774. //! the interrupt source asserted).
  775. //!
  776. //! \return None.
  777. //
  778. //*****************************************************************************
  779. void
  780. GPIOIntClear(uint32_t ui32Port, uint32_t ui32IntFlags)
  781. {
  782. //
  783. // Check the arguments.
  784. //
  785. ASSERT(_GPIOBaseValid(ui32Port));
  786. //
  787. // Clear the interrupts.
  788. //
  789. HWREG(ui32Port + GPIO_O_ICR) = ui32IntFlags;
  790. }
  791. //*****************************************************************************
  792. //
  793. //! Registers an interrupt handler for a GPIO port.
  794. //!
  795. //! \param ui32Port is the base address of the GPIO port.
  796. //! \param pfnIntHandler is a pointer to the GPIO port interrupt handling
  797. //! function.
  798. //!
  799. //! This function ensures that the interrupt handler specified by
  800. //! \e pfnIntHandler is called when an interrupt is detected from the selected
  801. //! GPIO port. This function also enables the corresponding GPIO interrupt
  802. //! in the interrupt controller; individual pin interrupts and interrupt
  803. //! sources must be enabled with GPIOIntEnable().
  804. //!
  805. //! \sa IntRegister() for important information about registering interrupt
  806. //! handlers.
  807. //!
  808. //! \return None.
  809. //
  810. //*****************************************************************************
  811. void
  812. GPIOIntRegister(uint32_t ui32Port, void (*pfnIntHandler)(void))
  813. {
  814. uint32_t ui32Int;
  815. //
  816. // Check the arguments.
  817. //
  818. ASSERT(_GPIOBaseValid(ui32Port));
  819. //
  820. // Get the interrupt number associated with the specified GPIO.
  821. //
  822. ui32Int = _GPIOIntNumberGet(ui32Port);
  823. ASSERT(ui32Int != 0);
  824. //
  825. // Register the interrupt handler.
  826. //
  827. IntRegister(ui32Int, pfnIntHandler);
  828. //
  829. // Enable the GPIO interrupt.
  830. //
  831. IntEnable(ui32Int);
  832. }
  833. //*****************************************************************************
  834. //
  835. //! Removes an interrupt handler for a GPIO port.
  836. //!
  837. //! \param ui32Port is the base address of the GPIO port.
  838. //!
  839. //! This function unregisters the interrupt handler for the specified
  840. //! GPIO port. This function also disables the corresponding
  841. //! GPIO port interrupt in the interrupt controller; individual GPIO interrupts
  842. //! and interrupt sources must be disabled with GPIOIntDisable().
  843. //!
  844. //! \sa IntRegister() for important information about registering interrupt
  845. //! handlers.
  846. //!
  847. //! \return None.
  848. //
  849. //*****************************************************************************
  850. void
  851. GPIOIntUnregister(uint32_t ui32Port)
  852. {
  853. uint32_t ui32Int;
  854. //
  855. // Check the arguments.
  856. //
  857. ASSERT(_GPIOBaseValid(ui32Port));
  858. //
  859. // Get the interrupt number associated with the specified GPIO.
  860. //
  861. ui32Int = _GPIOIntNumberGet(ui32Port);
  862. ASSERT(ui32Int != 0);
  863. //
  864. // Disable the GPIO interrupt.
  865. //
  866. IntDisable(ui32Int);
  867. //
  868. // Unregister the interrupt handler.
  869. //
  870. IntUnregister(ui32Int);
  871. }
  872. //*****************************************************************************
  873. //
  874. //! Registers an interrupt handler for an individual pin of a GPIO port.
  875. //!
  876. //! \param ui32Port is the base address of the GPIO port.
  877. //! \param ui32Pin is the pin whose interrupt is to be registered.
  878. //! \param pfnIntHandler is a pointer to the GPIO port interrupt handling
  879. //! function.
  880. //!
  881. //! This function ensures that the interrupt handler specified by
  882. //! \e pfnIntHandler is called when an interrupt is detected from the selected
  883. //! pin of a GPIO port. This function also enables the corresponding GPIO pin
  884. //! interrupt in the interrupt controller.
  885. //!
  886. //! \sa IntRegister() for important information about registering interrupt
  887. //! handlers.
  888. //!
  889. //! \return None.
  890. //
  891. //*****************************************************************************
  892. void
  893. GPIOIntRegisterPin(uint32_t ui32Port, uint32_t ui32Pin,
  894. void (*pfnIntHandler)(void))
  895. {
  896. uint32_t ui32Int;
  897. //
  898. // Check the arguments.
  899. //
  900. ASSERT((ui32Port == GPIO_PORTP_BASE) || (ui32Port == GPIO_PORTQ_BASE));
  901. ASSERT((ui32Pin > 0) && (ui32Pin < 8));
  902. ASSERT(pfnIntHandler != 0);
  903. //
  904. // Get the interrupt number associated with the specified GPIO.
  905. //
  906. ui32Int = _GPIOIntNumberGet(ui32Port);
  907. //
  908. // Register the interrupt handler.
  909. //
  910. IntRegister((ui32Int + ui32Pin), pfnIntHandler);
  911. //
  912. // Enable the GPIO pin interrupt.
  913. //
  914. IntEnable(ui32Int + ui32Pin);
  915. }
  916. //*****************************************************************************
  917. //
  918. //! Removes an interrupt handler for an individual pin of a GPIO port.
  919. //!
  920. //! \param ui32Port is the base address of the GPIO port.
  921. //! \param ui32Pin is the pin whose interrupt is to be unregistered.
  922. //!
  923. //! This function unregisters the interrupt handler for the specified pin of a
  924. //! GPIO port. This function also disables the corresponding GPIO pin
  925. //! interrupt in the interrupt controller.
  926. //!
  927. //! \sa IntRegister() for important information about registering interrupt
  928. //! handlers.
  929. //!
  930. //! \return None.
  931. //
  932. //*****************************************************************************
  933. void
  934. GPIOIntUnregisterPin(uint32_t ui32Port, uint32_t ui32Pin)
  935. {
  936. uint32_t ui32Int;
  937. //
  938. // Check the arguments.
  939. //
  940. ASSERT((ui32Port == GPIO_PORTP_BASE) || (ui32Port == GPIO_PORTQ_BASE));
  941. ASSERT((ui32Pin > 0) && (ui32Pin < 8));
  942. //
  943. // Get the interrupt number associated with the specified GPIO.
  944. //
  945. ui32Int = _GPIOIntNumberGet(ui32Port);
  946. //
  947. // Disable the GPIO pin interrupt.
  948. //
  949. IntDisable(ui32Int + ui32Pin);
  950. //
  951. // UnRegister the interrupt handler.
  952. //
  953. IntUnregister(ui32Int + ui32Pin);
  954. }
  955. //*****************************************************************************
  956. //
  957. //! Reads the values present of the specified pin(s).
  958. //!
  959. //! \param ui32Port is the base address of the GPIO port.
  960. //! \param ui8Pins is the bit-packed representation of the pin(s).
  961. //!
  962. //! The values at the specified pin(s) are read, as specified by \e ui8Pins.
  963. //! Values are returned for both input and output pin(s), and the value
  964. //! for pin(s) that are not specified by \e ui8Pins are set to 0.
  965. //!
  966. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  967. //! set identifies the pin to be accessed, and where bit 0 of the byte
  968. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  969. //!
  970. //! \return Returns a bit-packed byte providing the state of the specified
  971. //! pin, where bit 0 of the byte represents GPIO port pin 0, bit 1 represents
  972. //! GPIO port pin 1, and so on. Any bit that is not specified by \e ui8Pins
  973. //! is returned as a 0. Bits 31:8 should be ignored.
  974. //
  975. //*****************************************************************************
  976. int32_t
  977. GPIOPinRead(uint32_t ui32Port, uint8_t ui8Pins)
  978. {
  979. //
  980. // Check the arguments.
  981. //
  982. ASSERT(_GPIOBaseValid(ui32Port));
  983. //
  984. // Return the pin value(s).
  985. //
  986. return (HWREG(ui32Port + (GPIO_O_DATA + (ui8Pins << 2))));
  987. }
  988. //*****************************************************************************
  989. //
  990. //! Writes a value to the specified pin(s).
  991. //!
  992. //! \param ui32Port is the base address of the GPIO port.
  993. //! \param ui8Pins is the bit-packed representation of the pin(s).
  994. //! \param ui8Val is the value to write to the pin(s).
  995. //!
  996. //! Writes the corresponding bit values to the output pin(s) specified by
  997. //! \e ui8Pins. Writing to a pin configured as an input pin has no effect.
  998. //!
  999. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1000. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1001. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1002. //!
  1003. //! \return None.
  1004. //
  1005. //*****************************************************************************
  1006. void
  1007. GPIOPinWrite(uint32_t ui32Port, uint8_t ui8Pins, uint8_t ui8Val)
  1008. {
  1009. //
  1010. // Check the arguments.
  1011. //
  1012. ASSERT(_GPIOBaseValid(ui32Port));
  1013. //
  1014. // Write the pins.
  1015. //
  1016. HWREG(ui32Port + (GPIO_O_DATA + (ui8Pins << 2))) = ui8Val;
  1017. }
  1018. //*****************************************************************************
  1019. //
  1020. //! Configures pin(s) for use as analog-to-digital converter inputs.
  1021. //!
  1022. //! \param ui32Port is the base address of the GPIO port.
  1023. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1024. //!
  1025. //! The analog-to-digital converter input pins must be properly configured for
  1026. //! the analog-to-digital peripheral to function correctly. This function
  1027. //! provides the proper configuration for those pin(s).
  1028. //!
  1029. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1030. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1031. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1032. //!
  1033. //! \note This function cannot be used to turn any pin into an ADC input; it
  1034. //! only configures an ADC input pin for proper operation.
  1035. //!
  1036. //! \note A subset of GPIO pins, notably those used by the
  1037. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1038. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1039. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1040. //! registers before this function can be called.
  1041. //!
  1042. //! \return None.
  1043. //
  1044. //*****************************************************************************
  1045. void
  1046. GPIOPinTypeADC(uint32_t ui32Port, uint8_t ui8Pins)
  1047. {
  1048. //
  1049. // Check the arguments.
  1050. //
  1051. ASSERT(_GPIOBaseValid(ui32Port));
  1052. //
  1053. // Make the pin(s) be inputs.
  1054. //
  1055. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_IN);
  1056. //
  1057. // Set the pad(s) for analog operation.
  1058. //
  1059. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA,
  1060. GPIO_PIN_TYPE_ANALOG);
  1061. }
  1062. //*****************************************************************************
  1063. //
  1064. //! Configures pin(s) for use as a CAN device.
  1065. //!
  1066. //! \param ui32Port is the base address of the GPIO port.
  1067. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1068. //!
  1069. //! The CAN pins must be properly configured for the CAN peripherals to
  1070. //! function correctly. This function provides a typical configuration for
  1071. //! those pin(s); other configurations may work as well depending upon the
  1072. //! board setup (for example, using the on-chip pull-ups).
  1073. //!
  1074. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1075. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1076. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1077. //!
  1078. //! \note This function cannot be used to turn any pin into a CAN pin; it only
  1079. //! configures a CAN pin for proper operation. Note that a GPIOPinConfigure()
  1080. //! function call is also required to properly configure a pin for the CAN
  1081. //! function.
  1082. //!
  1083. //! \note A subset of GPIO pins, notably those used by the
  1084. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1085. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1086. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1087. //! registers before this function can be called.
  1088. //!
  1089. //! \return None.
  1090. //
  1091. //*****************************************************************************
  1092. void
  1093. GPIOPinTypeCAN(uint32_t ui32Port, uint8_t ui8Pins)
  1094. {
  1095. //
  1096. // Check the arguments.
  1097. //
  1098. ASSERT(_GPIOBaseValid(ui32Port));
  1099. //
  1100. // Make the pin(s) be inputs.
  1101. //
  1102. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1103. //
  1104. // Set the pad(s) for standard push-pull operation.
  1105. //
  1106. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  1107. }
  1108. //*****************************************************************************
  1109. //
  1110. //! Configures pin(s) for use as an analog comparator input.
  1111. //!
  1112. //! \param ui32Port is the base address of the GPIO port.
  1113. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1114. //!
  1115. //! The analog comparator input pins must be properly configured for the analog
  1116. //! comparator to function correctly. This function provides the proper
  1117. //! configuration for those pin(s).
  1118. //!
  1119. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1120. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1121. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1122. //!
  1123. //! \note This function cannot be used to turn any pin into an analog
  1124. //! comparator input; it only configures an analog comparator pin for proper
  1125. //! operation. Note that a GPIOPinConfigure() function call is also required
  1126. //! to properly configure a pin for the analog comparator function.
  1127. //!
  1128. //! \note A subset of GPIO pins, notably those used by the
  1129. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1130. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1131. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1132. //! registers before this function can be called.
  1133. //!
  1134. //! \return None.
  1135. //
  1136. //*****************************************************************************
  1137. void
  1138. GPIOPinTypeComparator(uint32_t ui32Port, uint8_t ui8Pins)
  1139. {
  1140. //
  1141. // Check the arguments.
  1142. //
  1143. ASSERT(_GPIOBaseValid(ui32Port));
  1144. //
  1145. // Make the pin(s) be inputs.
  1146. //
  1147. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_IN);
  1148. //
  1149. // Set the pad(s) for analog operation.
  1150. //
  1151. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA,
  1152. GPIO_PIN_TYPE_ANALOG);
  1153. }
  1154. //*****************************************************************************
  1155. //
  1156. //! Configures pin(s) for use as an analog comparator output.
  1157. //!
  1158. //! \param ui32Port is the base address of the GPIO port.
  1159. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1160. //!
  1161. //! The analog comparator output pins must be properly configured for the analog
  1162. //! comparator to function correctly. This function provides the proper
  1163. //! configuration for those pin(s).
  1164. //!
  1165. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1166. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1167. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1168. //!
  1169. //! \return None.
  1170. //
  1171. //*****************************************************************************
  1172. void GPIOPinTypeComparatorOutput(uint32_t ui32Port, uint8_t ui8Pins)
  1173. {
  1174. //
  1175. // Check the arguments.
  1176. //
  1177. ASSERT(_GPIOBaseValid(ui32Port));
  1178. //
  1179. // Make the pin(s) be inputs.
  1180. //
  1181. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1182. //
  1183. // Set the pad(s) for standard push-pull operation.
  1184. //
  1185. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1186. }
  1187. //*****************************************************************************
  1188. //
  1189. //! Configures pin(s) for use as an clock to be output from the device.
  1190. //!
  1191. //! \param ui32Port is the base address of the GPIO port.
  1192. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1193. //!
  1194. //! The system control output pin must be properly configured for the DIVSCLK to
  1195. //! function correctly. This function provides the proper configuration for
  1196. //! those pin(s).
  1197. //!
  1198. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1199. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1200. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1201. //!
  1202. //! \return None.
  1203. //
  1204. //*****************************************************************************
  1205. void GPIOPinTypeDIVSCLK(uint32_t ui32Port, uint8_t ui8Pins)
  1206. {
  1207. //
  1208. // Check the arguments.
  1209. //
  1210. ASSERT(_GPIOBaseValid(ui32Port));
  1211. //
  1212. // Make the pin(s) be inputs.
  1213. //
  1214. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1215. //
  1216. // Set the pad(s) for standard push-pull operation.
  1217. //
  1218. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1219. }
  1220. //*****************************************************************************
  1221. //
  1222. //! Configures pin(s) for use by the external peripheral interface.
  1223. //!
  1224. //! \param ui32Port is the base address of the GPIO port.
  1225. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1226. //!
  1227. //! The external peripheral interface pins must be properly configured for the
  1228. //! external peripheral interface to function correctly. This function
  1229. //! provides a typical configuration for those pin(s); other configurations may
  1230. //! work as well depending upon the board setup (for example, using the on-chip
  1231. //! pull-ups).
  1232. //!
  1233. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1234. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1235. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1236. //!
  1237. //! \note This function cannot be used to turn any pin into an external
  1238. //! peripheral interface pin; it only configures an external peripheral
  1239. //! interface pin for proper operation. Note that a GPIOPinConfigure()
  1240. //! function call is also required to properly configure a pin for the
  1241. //! external peripheral interface function.
  1242. //!
  1243. //! \note A subset of GPIO pins, notably those used by the
  1244. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1245. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1246. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1247. //! registers before this function can be called.
  1248. //!
  1249. //! \return None.
  1250. //
  1251. //*****************************************************************************
  1252. void
  1253. GPIOPinTypeEPI(uint32_t ui32Port, uint8_t ui8Pins)
  1254. {
  1255. //
  1256. // Check the arguments.
  1257. //
  1258. ASSERT(_GPIOBaseValid(ui32Port));
  1259. //
  1260. // Make the pin(s) be peripheral controlled.
  1261. //
  1262. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1263. //
  1264. // Set the pad(s) for standard push-pull operation.
  1265. //
  1266. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  1267. }
  1268. //*****************************************************************************
  1269. //
  1270. //! Configures pin(s) for use by the Ethernet peripheral as LED signals.
  1271. //!
  1272. //! \param ui32Port is the base address of the GPIO port.
  1273. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1274. //!
  1275. //! The Ethernet peripheral provides four signals that can be used to drive
  1276. //! an LED (for example, for link status/activity). This function provides a
  1277. //! typical configuration for the pins.
  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 function cannot be used to turn any pin into an Ethernet LED
  1284. //! pin; it only configures an Ethernet LED pin for proper operation. Note
  1285. //! that a GPIOPinConfigure() function call is also required to properly
  1286. //! configure the pin for the Ethernet LED function.
  1287. //!
  1288. //! \note A subset of GPIO pins, notably those used by the
  1289. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1290. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1291. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1292. //! registers before this function can be called.
  1293. //!
  1294. //! \return None.
  1295. //
  1296. //*****************************************************************************
  1297. void
  1298. GPIOPinTypeEthernetLED(uint32_t ui32Port, uint8_t ui8Pins)
  1299. {
  1300. //
  1301. // Check the arguments.
  1302. //
  1303. ASSERT(_GPIOBaseValid(ui32Port));
  1304. //
  1305. // Make the pin(s) be peripheral controlled.
  1306. //
  1307. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1308. //
  1309. // Set the pad(s) for standard push-pull operation.
  1310. //
  1311. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  1312. }
  1313. //*****************************************************************************
  1314. //
  1315. //! Configures pin(s) for use by the Ethernet peripheral as MII signals.
  1316. //!
  1317. //! \param ui32Port is the base address of the GPIO port.
  1318. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1319. //!
  1320. //! The Ethernet peripheral provides a set of MII signals that
  1321. //! are used to connect to an external PHY. This function provides a typical
  1322. //! configuration for the pins.
  1323. //!
  1324. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1325. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1326. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1327. //!
  1328. //! \note This function cannot be used to turn any pin into an Ethernet MII
  1329. //! pin; it only configures an Ethernet MII pin for proper operation. Note
  1330. //! that a GPIOPinConfigure() function call is also required to properly
  1331. //! configure the pin for the Ethernet MII function.
  1332. //!
  1333. //! \note A subset of GPIO pins, notably those used by the
  1334. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1335. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1336. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1337. //! registers before this function can be called.
  1338. //!
  1339. //! \return None.
  1340. //
  1341. //*****************************************************************************
  1342. void
  1343. GPIOPinTypeEthernetMII(uint32_t ui32Port, uint8_t ui8Pins)
  1344. {
  1345. //
  1346. // Check the arguments.
  1347. //
  1348. ASSERT(_GPIOBaseValid(ui32Port));
  1349. //
  1350. // Make the pin(s) be peripheral controlled.
  1351. //
  1352. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1353. //
  1354. // Set the pad(s) for standard push-pull operation.
  1355. //
  1356. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  1357. }
  1358. //*****************************************************************************
  1359. //
  1360. //! Configures pin(s) for use as GPIO inputs.
  1361. //!
  1362. //! \param ui32Port is the base address of the GPIO port.
  1363. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1364. //!
  1365. //! The GPIO pins must be properly configured in order to function correctly as
  1366. //! GPIO inputs. This function provides the proper configuration for those
  1367. //! pin(s).
  1368. //!
  1369. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1370. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1371. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1372. //!
  1373. //! \note A subset of GPIO pins, notably those used by the
  1374. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1375. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1376. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1377. //! registers before this function can be called.
  1378. //!
  1379. //! \return None.
  1380. //
  1381. //*****************************************************************************
  1382. void
  1383. GPIOPinTypeGPIOInput(uint32_t ui32Port, uint8_t ui8Pins)
  1384. {
  1385. //
  1386. // Check the arguments.
  1387. //
  1388. ASSERT(_GPIOBaseValid(ui32Port));
  1389. //
  1390. // Make the pin(s) be inputs.
  1391. //
  1392. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_IN);
  1393. //
  1394. // Set the pad(s) for standard push-pull operation.
  1395. //
  1396. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1397. }
  1398. //*****************************************************************************
  1399. //
  1400. //! Configures pin(s) for use as GPIO outputs.
  1401. //!
  1402. //! \param ui32Port is the base address of the GPIO port.
  1403. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1404. //!
  1405. //! The GPIO pins must be properly configured in order to function correctly as
  1406. //! GPIO outputs. This function provides the proper configuration for those
  1407. //! pin(s).
  1408. //!
  1409. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1410. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1411. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1412. //!
  1413. //! \note A subset of GPIO pins, notably those used by the
  1414. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1415. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1416. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1417. //! registers before this function can be called.
  1418. //!
  1419. //! \return None.
  1420. //
  1421. //*****************************************************************************
  1422. void
  1423. GPIOPinTypeGPIOOutput(uint32_t ui32Port, uint8_t ui8Pins)
  1424. {
  1425. //
  1426. // Check the arguments.
  1427. //
  1428. ASSERT(_GPIOBaseValid(ui32Port));
  1429. //
  1430. // Set the pad(s) for standard push-pull operation.
  1431. //
  1432. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1433. //
  1434. // Make the pin(s) be outputs.
  1435. //
  1436. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_OUT);
  1437. }
  1438. //*****************************************************************************
  1439. //
  1440. //! Configures pin(s) for use as GPIO open drain outputs.
  1441. //!
  1442. //! \param ui32Port is the base address of the GPIO port.
  1443. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1444. //!
  1445. //! The GPIO pins must be properly configured in order to function correctly as
  1446. //! GPIO outputs. This function provides the proper configuration for those
  1447. //! pin(s).
  1448. //!
  1449. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1450. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1451. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1452. //!
  1453. //! \note A subset of GPIO pins, notably those used by the
  1454. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1455. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1456. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1457. //! registers before this function can be called.
  1458. //!
  1459. //! \return None.
  1460. //
  1461. //*****************************************************************************
  1462. void
  1463. GPIOPinTypeGPIOOutputOD(uint32_t ui32Port, uint8_t ui8Pins)
  1464. {
  1465. //
  1466. // Check the arguments.
  1467. //
  1468. ASSERT(_GPIOBaseValid(ui32Port));
  1469. //
  1470. // Set the pad(s) for standard push-pull operation.
  1471. //
  1472. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
  1473. //
  1474. // Make the pin(s) be outputs.
  1475. //
  1476. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_OUT);
  1477. }
  1478. //*****************************************************************************
  1479. //
  1480. //! Configures pin(s) for use as an Hibernate RTC Clock.
  1481. //!
  1482. //! \param ui32Port is the base address of the GPIO port.
  1483. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1484. //!
  1485. //! The hibernate output pin must be properly configured for the RTCCLK to
  1486. //! function correctly. This function provides the proper configuration for the
  1487. //! RTC Clock to be output from the device.
  1488. //!
  1489. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1490. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1491. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1492. //!
  1493. //! \return None.
  1494. //
  1495. //*****************************************************************************
  1496. void GPIOPinTypeHibernateRTCCLK(uint32_t ui32Port, uint8_t ui8Pins)
  1497. {
  1498. //
  1499. // Check the arguments.
  1500. //
  1501. ASSERT(_GPIOBaseValid(ui32Port));
  1502. //
  1503. // Make the pin(s) be inputs.
  1504. //
  1505. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1506. //
  1507. // Set the pad(s) for standard push-pull operation.
  1508. //
  1509. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1510. }
  1511. //*****************************************************************************
  1512. //
  1513. //! Configures pin for use as SDA by the I2C peripheral.
  1514. //!
  1515. //! \param ui32Port is the base address of the GPIO port.
  1516. //! \param ui8Pins is the bit-packed representation of the pin.
  1517. //!
  1518. //! The I2C pins must be properly configured for the I2C peripheral to function
  1519. //! correctly. This function provides the proper configuration for the SDA
  1520. //! pin.
  1521. //!
  1522. //! The pin is specified using a bit-packed byte, where each bit that is
  1523. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1524. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1525. //!
  1526. //! \note This function cannot be used to turn any pin into an I2C SDA pin; it
  1527. //! only configures an I2C SDA pin for proper operation. Note that a
  1528. //! GPIOPinConfigure() function call is also required to properly configure a
  1529. //! pin for the I2C SDA function.
  1530. //!
  1531. //! \note A subset of GPIO pins, notably those used by the
  1532. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1533. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1534. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1535. //! registers before this function can be called.
  1536. //!
  1537. //! \return None.
  1538. //
  1539. //*****************************************************************************
  1540. void
  1541. GPIOPinTypeI2C(uint32_t ui32Port, uint8_t ui8Pins)
  1542. {
  1543. //
  1544. // Check the arguments.
  1545. //
  1546. ASSERT(_GPIOBaseValid(ui32Port));
  1547. //
  1548. // Make the pin(s) be peripheral controlled.
  1549. //
  1550. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1551. //
  1552. // Set the pad(s) for open-drain operation with a weak pull-up.
  1553. //
  1554. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
  1555. }
  1556. //*****************************************************************************
  1557. //
  1558. //! Configures pin for use as SCL by the I2C peripheral.
  1559. //!
  1560. //! \param ui32Port is the base address of the GPIO port.
  1561. //! \param ui8Pins is the bit-packed representation of the pin.
  1562. //!
  1563. //! The I2C pins must be properly configured for the I2C peripheral to function
  1564. //! correctly. This function provides the proper configuration for the SCL
  1565. //! pin.
  1566. //!
  1567. //! The pin is specified using a bit-packed byte, where each bit that is
  1568. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1569. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1570. //!
  1571. //! \note This function cannot be used to turn any pin into an I2C SCL pin; it
  1572. //! only configures an I2C SCL pin for proper operation. Note that a
  1573. //! GPIOPinConfigure() function call is also required to properly configure a
  1574. //! pin for the I2C SCL function.
  1575. //!
  1576. //! \note A subset of GPIO pins, notably those used by the
  1577. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1578. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1579. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1580. //! registers before this function can be called.
  1581. //!
  1582. //! \return None.
  1583. //
  1584. //*****************************************************************************
  1585. void
  1586. GPIOPinTypeI2CSCL(uint32_t ui32Port, uint8_t ui8Pins)
  1587. {
  1588. //
  1589. // Check the arguments.
  1590. //
  1591. ASSERT(_GPIOBaseValid(ui32Port));
  1592. //
  1593. // Make the pin(s) be peripheral controlled.
  1594. //
  1595. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1596. //
  1597. // Set the pad(s) for push-pull operation.
  1598. //
  1599. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1600. }
  1601. //*****************************************************************************
  1602. //
  1603. //! Configures pin(s) for use by the LCD Controller.
  1604. //!
  1605. //! \param ui32Port is the base address of the GPIO port.
  1606. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1607. //!
  1608. //! The LCD controller pins must be properly configured for the LCD controller
  1609. //! to function correctly. This function provides a typical configuration for
  1610. //! those pin(s); other configurations may work as well depending upon the
  1611. //! board setup (for example, using the on-chip pull-ups).
  1612. //!
  1613. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1614. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1615. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1616. //!
  1617. //! \note This function cannot be used to turn any pin into an LCD pin; it only
  1618. //! configures an LCD pin for proper operation. Note that a GPIOPinConfigure()
  1619. //! function call is also required to properly configure a pin for the LCD
  1620. //! controller function.
  1621. //!
  1622. //! \note A subset of GPIO pins, notably those used by the
  1623. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1624. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1625. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1626. //! registers before this function can be called.
  1627. //!
  1628. //! \return None.
  1629. //
  1630. //*****************************************************************************
  1631. void
  1632. GPIOPinTypeLCD(uint32_t ui32Port, uint8_t ui8Pins)
  1633. {
  1634. //
  1635. // Check the arguments.
  1636. //
  1637. ASSERT(_GPIOBaseValid(ui32Port));
  1638. //
  1639. // Make the pin(s) be peripheral controlled.
  1640. //
  1641. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1642. //
  1643. // Set the pad(s) for standard push-pull operation and beefed up drive.
  1644. //
  1645. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
  1646. }
  1647. //*****************************************************************************
  1648. //
  1649. //! Configures pin(s) for use by the 1-Wire module.
  1650. //!
  1651. //! \param ui32Port is the base address of the GPIO port.
  1652. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1653. //!
  1654. //! The 1-Wire pin must be properly configured for the 1-Wire peripheral to
  1655. //! function correctly. This function provides a typical configuration for
  1656. //! those pin(s); other configurations may work as well depending upon the
  1657. //! board setup (for example, using the on-chip pull-ups).
  1658. //!
  1659. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1660. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1661. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1662. //!
  1663. //! \note This function cannot be used to turn any pin into a 1-Wire pin; it
  1664. //! only configures a 1-Wire pin for proper operation. Note that a
  1665. //! GPIOPinConfigure() function call is also required to properly configure a
  1666. //! pin for the 1-Wire function.
  1667. //!
  1668. //! \note A subset of GPIO pins, notably those used by the
  1669. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1670. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1671. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1672. //! registers before this function can be called.
  1673. //!
  1674. //! \return None.
  1675. //
  1676. //*****************************************************************************
  1677. void
  1678. GPIOPinTypeOneWire(uint32_t ui32Port, uint8_t ui8Pins)
  1679. {
  1680. //
  1681. // Check the arguments.
  1682. //
  1683. ASSERT(_GPIOBaseValid(ui32Port));
  1684. //
  1685. // Make the pin(s) be peripheral controlled.
  1686. //
  1687. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1688. //
  1689. // Set the pad(s) for standard push-pull operation.
  1690. //
  1691. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1692. }
  1693. //*****************************************************************************
  1694. //
  1695. //! Configures pin(s) for use by the PWM peripheral.
  1696. //!
  1697. //! \param ui32Port is the base address of the GPIO port.
  1698. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1699. //!
  1700. //! The PWM pins must be properly configured for the PWM peripheral to function
  1701. //! correctly. This function provides a typical configuration for those
  1702. //! pin(s); other configurations may work as well depending upon the board
  1703. //! setup (for example, using the on-chip pull-ups).
  1704. //!
  1705. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1706. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1707. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1708. //!
  1709. //! \note This function cannot be used to turn any pin into a PWM pin; it only
  1710. //! configures a PWM pin for proper operation. Note that a GPIOPinConfigure()
  1711. //! function call is also required to properly configure a pin for the PWM
  1712. //! function.
  1713. //!
  1714. //! \note A subset of GPIO pins, notably those used by the
  1715. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1716. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1717. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1718. //! registers before this function can be called.
  1719. //!
  1720. //! \return None.
  1721. //
  1722. //*****************************************************************************
  1723. void
  1724. GPIOPinTypePWM(uint32_t ui32Port, uint8_t ui8Pins)
  1725. {
  1726. //
  1727. // Check the arguments.
  1728. //
  1729. ASSERT(_GPIOBaseValid(ui32Port));
  1730. //
  1731. // Make the pin(s) be peripheral controlled.
  1732. //
  1733. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1734. //
  1735. // Set the pad(s) for standard push-pull operation.
  1736. //
  1737. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1738. }
  1739. //*****************************************************************************
  1740. //
  1741. //! Configures pin(s) for use by the QEI peripheral.
  1742. //!
  1743. //! \param ui32Port is the base address of the GPIO port.
  1744. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1745. //!
  1746. //! The QEI pins must be properly configured for the QEI peripheral to function
  1747. //! correctly. This function provides a typical configuration for those
  1748. //! pin(s); other configurations may work as well depending upon the board
  1749. //! setup (for example, not using the on-chip pull-ups).
  1750. //!
  1751. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1752. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1753. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1754. //!
  1755. //! \note This function cannot be used to turn any pin into a QEI pin; it only
  1756. //! configures a QEI pin for proper operation. Note that a GPIOPinConfigure()
  1757. //! function call is also required to properly configure a pin for the QEI
  1758. //! function.
  1759. //!
  1760. //! \note A subset of GPIO pins, notably those used by the
  1761. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1762. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1763. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1764. //! registers before this function can be called.
  1765. //!
  1766. //! \return None.
  1767. //
  1768. //*****************************************************************************
  1769. void
  1770. GPIOPinTypeQEI(uint32_t ui32Port, uint8_t ui8Pins)
  1771. {
  1772. //
  1773. // Check the arguments.
  1774. //
  1775. ASSERT(_GPIOBaseValid(ui32Port));
  1776. //
  1777. // Make the pin(s) be peripheral controlled.
  1778. //
  1779. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1780. //
  1781. // Set the pad(s) for standard push-pull operation with a weak pull-up.
  1782. //
  1783. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA,
  1784. GPIO_PIN_TYPE_STD_WPU);
  1785. }
  1786. //*****************************************************************************
  1787. //
  1788. //! Configures pin(s) for use by the SSI peripheral.
  1789. //!
  1790. //! \param ui32Port is the base address of the GPIO port.
  1791. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1792. //!
  1793. //! The SSI pins must be properly configured for the SSI peripheral to function
  1794. //! correctly. This function provides a typical configuration for those
  1795. //! pin(s); other configurations may work as well depending upon the board
  1796. //! setup (for example, using the on-chip pull-ups).
  1797. //!
  1798. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1799. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1800. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1801. //!
  1802. //! \note This function cannot be used to turn any pin into a SSI pin; it only
  1803. //! configures a SSI pin for proper operation. Note that a GPIOPinConfigure()
  1804. //! function call is also required to properly configure a pin for the SSI
  1805. //! function.
  1806. //!
  1807. //! \note A subset of GPIO pins, notably those used by the
  1808. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1809. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1810. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1811. //! registers before this function can be called.
  1812. //!
  1813. //! \return None.
  1814. //
  1815. //*****************************************************************************
  1816. void
  1817. GPIOPinTypeSSI(uint32_t ui32Port, uint8_t ui8Pins)
  1818. {
  1819. //
  1820. // Check the arguments.
  1821. //
  1822. ASSERT(_GPIOBaseValid(ui32Port));
  1823. //
  1824. // Make the pin(s) be peripheral controlled.
  1825. //
  1826. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1827. //
  1828. // Set the pad(s) for standard push-pull operation.
  1829. //
  1830. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1831. }
  1832. //*****************************************************************************
  1833. //
  1834. //! Configures pin(s) for use by the Timer peripheral.
  1835. //!
  1836. //! \param ui32Port is the base address of the GPIO port.
  1837. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1838. //!
  1839. //! The CCP pins must be properly configured for the timer peripheral to
  1840. //! function correctly. This function provides a typical configuration for
  1841. //! those pin(s); other configurations may work as well depending upon the
  1842. //! board setup (for example, using the on-chip pull-ups).
  1843. //!
  1844. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1845. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1846. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1847. //!
  1848. //! \note This function cannot be used to turn any pin into a timer pin; it
  1849. //! only configures a timer pin for proper operation. Note that a
  1850. //! GPIOPinConfigure() function call is also required to properly configure a
  1851. //! pin for the CCP function.
  1852. //!
  1853. //! \note A subset of GPIO pins, notably those used by the
  1854. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1855. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1856. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1857. //! registers before this function can be called.
  1858. //!
  1859. //! \return None.
  1860. //
  1861. //*****************************************************************************
  1862. void
  1863. GPIOPinTypeTimer(uint32_t ui32Port, uint8_t ui8Pins)
  1864. {
  1865. //
  1866. // Check the arguments.
  1867. //
  1868. ASSERT(_GPIOBaseValid(ui32Port));
  1869. //
  1870. // Make the pin(s) be peripheral controlled.
  1871. //
  1872. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1873. //
  1874. // Set the pad(s) for standard push-pull operation.
  1875. //
  1876. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1877. }
  1878. //*****************************************************************************
  1879. //
  1880. //! Configures pin(s) for use by the Trace peripheral.
  1881. //!
  1882. //! \param ui32Port is the base address of the GPIO port.
  1883. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1884. //!
  1885. //! The Trace pins must be properly configured for the Trace peripheral to
  1886. //! function correctly. This function provides a typical configuration for
  1887. //! those pin(s).
  1888. //!
  1889. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1890. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1891. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1892. //!
  1893. //! \note This function cannot be used to turn any pin into a trace pin; it
  1894. //! only configures a trace pin for proper operation. Note that a
  1895. //! GPIOPinConfigure() function call is also required to properly configure a
  1896. //! pin for the Trace function.
  1897. //!
  1898. //! \note A subset of GPIO pins, notably those used by the
  1899. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1900. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1901. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1902. //! registers before this function can be called.
  1903. //!
  1904. //! \return None.
  1905. //
  1906. //*****************************************************************************
  1907. void
  1908. GPIOPinTypeTrace(uint32_t ui32Port, uint8_t ui8Pins)
  1909. {
  1910. //
  1911. // Check the arguments.
  1912. //
  1913. ASSERT(_GPIOBaseValid(ui32Port));
  1914. //
  1915. // Make the pin(s) be peripheral controlled.
  1916. //
  1917. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1918. //
  1919. // Set the pad(s) for standard push-pull operation.
  1920. //
  1921. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1922. }
  1923. //*****************************************************************************
  1924. //
  1925. //! Configures pin(s) for use by the UART peripheral.
  1926. //!
  1927. //! \param ui32Port is the base address of the GPIO port.
  1928. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1929. //!
  1930. //! The UART pins must be properly configured for the UART peripheral to
  1931. //! function correctly. This function provides a typical configuration for
  1932. //! those pin(s); other configurations may work as well depending upon the
  1933. //! board setup (for example, using the on-chip pull-ups).
  1934. //!
  1935. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1936. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1937. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1938. //!
  1939. //! \note This function cannot be used to turn any pin into a UART pin; it
  1940. //! only configures a UART pin for proper operation. Note that a
  1941. //! GPIOPinConfigure() function call is also required to properly configure a
  1942. //! pin for the UART function.
  1943. //!
  1944. //! \note A subset of GPIO pins, notably those used by the
  1945. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1946. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1947. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1948. //! registers before this function can be called.
  1949. //!
  1950. //! \return None.
  1951. //
  1952. //*****************************************************************************
  1953. void
  1954. GPIOPinTypeUART(uint32_t ui32Port, uint8_t ui8Pins)
  1955. {
  1956. //
  1957. // Check the arguments.
  1958. //
  1959. ASSERT(_GPIOBaseValid(ui32Port));
  1960. //
  1961. // Make the pin(s) be peripheral controlled.
  1962. //
  1963. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  1964. //
  1965. // Set the pad(s) for standard push-pull operation.
  1966. //
  1967. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  1968. }
  1969. //*****************************************************************************
  1970. //
  1971. //! Configures pin(s) for use by the USB peripheral.
  1972. //!
  1973. //! \param ui32Port is the base address of the GPIO port.
  1974. //! \param ui8Pins is the bit-packed representation of the pin(s).
  1975. //!
  1976. //! USB analog pins must be properly configured for the USB peripheral to
  1977. //! function correctly. This function provides the proper configuration for
  1978. //! any USB analog pin(s).
  1979. //!
  1980. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  1981. //! set identifies the pin to be accessed, and where bit 0 of the byte
  1982. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  1983. //!
  1984. //! \note This function cannot be used to turn any pin into a USB pin; it only
  1985. //! configures a USB pin for proper operation. Note that a GPIOPinConfigure()
  1986. //! function call is also required to properly configure a pin for the USB
  1987. //! function.
  1988. //!
  1989. //! \note A subset of GPIO pins, notably those used by the
  1990. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  1991. //! locked against inadvertent reconfiguration. These pins must be unlocked
  1992. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  1993. //! registers before this function can be called.
  1994. //!
  1995. //! \return None.
  1996. //
  1997. //*****************************************************************************
  1998. void
  1999. GPIOPinTypeUSBAnalog(uint32_t ui32Port, uint8_t ui8Pins)
  2000. {
  2001. //
  2002. // Check the arguments.
  2003. //
  2004. ASSERT(_GPIOBaseValid(ui32Port));
  2005. //
  2006. // Make the pin(s) be inputs.
  2007. //
  2008. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_IN);
  2009. //
  2010. // Set the pad(s) for analog operation.
  2011. //
  2012. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA,
  2013. GPIO_PIN_TYPE_ANALOG);
  2014. }
  2015. //*****************************************************************************
  2016. //
  2017. //! Configures pin(s) for use by the USB peripheral.
  2018. //!
  2019. //! \param ui32Port is the base address of the GPIO port.
  2020. //! \param ui8Pins is the bit-packed representation of the pin(s).
  2021. //!
  2022. //! USB digital pins must be properly configured for the USB peripheral to
  2023. //! function correctly. This function provides a typical configuration for
  2024. //! the digital USB pin(s); other configurations may work as well depending
  2025. //! upon the board setup (for example, using the on-chip pull-ups).
  2026. //!
  2027. //! This function should only be used with EPEN and PFAULT pins as all other
  2028. //! USB pins are analog in nature.
  2029. //!
  2030. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  2031. //! set identifies the pin to be accessed, and where bit 0 of the byte
  2032. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  2033. //!
  2034. //! \note This function cannot be used to turn any pin into a USB pin; it only
  2035. //! configures a USB pin for proper operation. Note that a GPIOPinConfigure()
  2036. //! function call is also required to properly configure a pin for the USB
  2037. //! function.
  2038. //!
  2039. //! \note A subset of GPIO pins, notably those used by the
  2040. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  2041. //! locked against inadvertent reconfiguration. These pins must be unlocked
  2042. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  2043. //! registers before this function can be called.
  2044. //!
  2045. //! \return None.
  2046. //
  2047. //*****************************************************************************
  2048. void
  2049. GPIOPinTypeUSBDigital(uint32_t ui32Port, uint8_t ui8Pins)
  2050. {
  2051. //
  2052. // Check the arguments.
  2053. //
  2054. ASSERT(_GPIOBaseValid(ui32Port));
  2055. //
  2056. // Make the pin(s) be peripheral controlled.
  2057. //
  2058. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
  2059. //
  2060. // Set the pad(s) for standard push-pull operation.
  2061. //
  2062. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
  2063. }
  2064. //*****************************************************************************
  2065. //
  2066. //! Configures pin(s) for use as a hibernate wake-on-high source.
  2067. //!
  2068. //! \param ui32Port is the base address of the GPIO port.
  2069. //! \param ui8Pins is the bit-packed representation of the pin(s).
  2070. //!
  2071. //! The GPIO pins must be properly configured in order to function correctly as
  2072. //! hibernate wake-high inputs. This function provides the proper
  2073. //! configuration for those pin(s).
  2074. //!
  2075. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  2076. //! set identifies the pin to be accessed, and where bit 0 of the byte
  2077. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  2078. //!
  2079. //! \note A subset of GPIO pins, notably those used by the
  2080. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  2081. //! locked against inadvertent reconfiguration. These pins must be unlocked
  2082. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  2083. //! registers before this function can be called.
  2084. //!
  2085. //! \return None.
  2086. //
  2087. //*****************************************************************************
  2088. void
  2089. GPIOPinTypeWakeHigh(uint32_t ui32Port, uint8_t ui8Pins)
  2090. {
  2091. //
  2092. // Check the arguments.
  2093. //
  2094. ASSERT(_GPIOBaseValid(ui32Port));
  2095. //
  2096. // Make the pin(s) inputs.
  2097. //
  2098. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_IN);
  2099. //
  2100. // Set the pad(s) for wake-high operation.
  2101. //
  2102. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA,
  2103. GPIO_PIN_TYPE_WAKE_HIGH);
  2104. }
  2105. //*****************************************************************************
  2106. //
  2107. //! Configures pin(s) for use as a hibernate wake-on-low source.
  2108. //!
  2109. //! \param ui32Port is the base address of the GPIO port.
  2110. //! \param ui8Pins is the bit-packed representation of the pin(s).
  2111. //!
  2112. //! The GPIO pins must be properly configured in order to function correctly as
  2113. //! hibernate wake-low inputs. This function provides the proper
  2114. //! configuration for those pin(s).
  2115. //!
  2116. //! The pin(s) are specified using a bit-packed byte, where each bit that is
  2117. //! set identifies the pin to be accessed, and where bit 0 of the byte
  2118. //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
  2119. //!
  2120. //! \note A subset of GPIO pins, notably those used by the
  2121. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  2122. //! locked against inadvertent reconfiguration. These pins must be unlocked
  2123. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  2124. //! registers before this function can be called.
  2125. //!
  2126. //! \return None.
  2127. //
  2128. //*****************************************************************************
  2129. void
  2130. GPIOPinTypeWakeLow(uint32_t ui32Port, uint8_t ui8Pins)
  2131. {
  2132. //
  2133. // Check the arguments.
  2134. //
  2135. ASSERT(_GPIOBaseValid(ui32Port));
  2136. //
  2137. // Make the pin(s) inputs.
  2138. //
  2139. GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_IN);
  2140. //
  2141. // Set the pad(s) for wake-high operation.
  2142. //
  2143. GPIOPadConfigSet(ui32Port, ui8Pins, GPIO_STRENGTH_2MA,
  2144. GPIO_PIN_TYPE_WAKE_LOW);
  2145. }
  2146. //*****************************************************************************
  2147. //
  2148. //! Retrieves the wake pins status.
  2149. //!
  2150. //! \param ui32Port is the base address of the GPIO port.
  2151. //!
  2152. //! This function returns the GPIO wake pin status values. The returned
  2153. //! bitfield shows low or high pin state via a value of 0 or 1.
  2154. //!
  2155. //! \note A subset of GPIO pins, notably those used by the
  2156. //! JTAG/SWD interface and any pin capable of acting as an NMI input, are
  2157. //! locked against inadvertent reconfiguration. These pins must be unlocked
  2158. //! using direct register writes to the relevant GPIO_O_LOCK and GPIO_O_CR
  2159. //! registers before this function can be called.
  2160. //!
  2161. //! \return Returns the wake pin status.
  2162. //
  2163. //*****************************************************************************
  2164. uint32_t
  2165. GPIOPinWakeStatus(uint32_t ui32Port)
  2166. {
  2167. return (HWREG(ui32Port + GPIO_O_WAKESTAT));
  2168. }
  2169. //*****************************************************************************
  2170. //
  2171. //! Configures the alternate function of a GPIO pin.
  2172. //!
  2173. //! \param ui32PinConfig is the pin configuration value, specified as only one
  2174. //! of the \b GPIO_P??_??? values.
  2175. //!
  2176. //! This function configures the pin mux that selects the peripheral function
  2177. //! associated with a particular GPIO pin. Only one peripheral function at a
  2178. //! time can be associated with a GPIO pin, and each peripheral function should
  2179. //! only be associated with a single GPIO pin at a time (despite the fact that
  2180. //! many of them can be associated with more than one GPIO pin). To fully
  2181. //! configure a pin, a GPIOPinType*() function should also be called.
  2182. //!
  2183. //! The available mappings are supplied on a per-device basis in
  2184. //! <tt>pin_map.h</tt>.
  2185. //!
  2186. //! \note If the same signal is assigned to two different GPIO port
  2187. //! pins, the signal is assigned to the port with the lowest letter and the
  2188. //! assignment to the higher letter port is ignored.
  2189. //!
  2190. //! \return None.
  2191. //
  2192. //*****************************************************************************
  2193. void
  2194. GPIOPinConfigure(uint32_t ui32PinConfig)
  2195. {
  2196. uint32_t ui32Base, ui32Shift;
  2197. //
  2198. // Check the argument.
  2199. //
  2200. ASSERT(((ui32PinConfig >> 16) & 0xff) < 18);
  2201. ASSERT(((ui32PinConfig >> 8) & 0xe3) == 0);
  2202. //
  2203. // Extract the base address index from the input value.
  2204. //
  2205. ui32Base = (ui32PinConfig >> 16) & 0xff;
  2206. //
  2207. // Get the base address of the GPIO module.
  2208. //
  2209. ui32Base = g_pui32GPIOBaseAddrs[ui32Base];
  2210. //
  2211. // Extract the shift from the input value.
  2212. //
  2213. ui32Shift = (ui32PinConfig >> 8) & 0xff;
  2214. //
  2215. // Write the requested pin muxing value for this GPIO pin.
  2216. //
  2217. HWREG(ui32Base + GPIO_O_PCTL) = ((HWREG(ui32Base + GPIO_O_PCTL) &
  2218. ~(0xf << ui32Shift)) |
  2219. ((ui32PinConfig & 0xf) << ui32Shift));
  2220. }
  2221. //*****************************************************************************
  2222. //
  2223. //! Enables a GPIO pin as a trigger to start a DMA transaction.
  2224. //!
  2225. //! \param ui32Port is the base address of the GPIO port.
  2226. //! \param ui8Pins is the bit-packed representation of the pin(s).
  2227. //!
  2228. //! This function enables a GPIO pin to be used as a trigger to start a uDMA
  2229. //! transaction. Any GPIO pin can be configured to be an external trigger for
  2230. //! the uDMA. The GPIO pin still generates interrupts if the interrupt is
  2231. //! enabled for the selected pin.
  2232. //!
  2233. //! \return None.
  2234. //
  2235. //*****************************************************************************
  2236. void
  2237. GPIODMATriggerEnable(uint32_t ui32Port, uint8_t ui8Pins)
  2238. {
  2239. //
  2240. // Check the arguments.
  2241. //
  2242. ASSERT(_GPIOBaseValid(ui32Port));
  2243. //
  2244. // Set the pin as a DMA trigger.
  2245. //
  2246. HWREGB(ui32Port + GPIO_O_DMACTL) |= ui8Pins;
  2247. }
  2248. //*****************************************************************************
  2249. //
  2250. //! Disables a GPIO pin as a trigger to start a DMA transaction.
  2251. //!
  2252. //! \param ui32Port is the base address of the GPIO port.
  2253. //! \param ui8Pins is the bit-packed representation of the pin(s).
  2254. //!
  2255. //! This function disables a GPIO pin from being used as a trigger to start a
  2256. //! uDMA transaction. This function can be used to disable this feature if it
  2257. //! was enabled via a call to GPIODMATriggerEnable().
  2258. //!
  2259. //! \return None.
  2260. //
  2261. //*****************************************************************************
  2262. void
  2263. GPIODMATriggerDisable(uint32_t ui32Port, uint8_t ui8Pins)
  2264. {
  2265. //
  2266. // Check the arguments.
  2267. //
  2268. ASSERT(_GPIOBaseValid(ui32Port));
  2269. //
  2270. // Set the pin as a DMA trigger.
  2271. //
  2272. HWREGB(ui32Port + GPIO_O_DMACTL) &= (~ui8Pins);
  2273. }
  2274. //*****************************************************************************
  2275. //
  2276. //! Enables a GPIO pin as a trigger to start an ADC capture.
  2277. //!
  2278. //! \param ui32Port is the base address of the GPIO port.
  2279. //! \param ui8Pins is the bit-packed representation of the pin(s).
  2280. //!
  2281. //! This function enables a GPIO pin to be used as a trigger to start an ADC
  2282. //! sequence. Any GPIO pin can be configured to be an external trigger for an
  2283. //! ADC sequence. The GPIO pin still generates interrupts if the interrupt is
  2284. //! enabled for the selected pin. To enable the use of a GPIO pin to trigger
  2285. //! the ADC module, the ADCSequenceConfigure() function must be called with the
  2286. //! \b ADC_TRIGGER_EXTERNAL parameter.
  2287. //!
  2288. //! \return None.
  2289. //
  2290. //*****************************************************************************
  2291. void
  2292. GPIOADCTriggerEnable(uint32_t ui32Port, uint8_t ui8Pins)
  2293. {
  2294. //
  2295. // Check the arguments.
  2296. //
  2297. ASSERT(_GPIOBaseValid(ui32Port));
  2298. //
  2299. // Set the pin as a DMA trigger.
  2300. //
  2301. HWREGB(ui32Port + GPIO_O_ADCCTL) |= ui8Pins;
  2302. }
  2303. //*****************************************************************************
  2304. //
  2305. //! Disable a GPIO pin as a trigger to start an ADC capture.
  2306. //!
  2307. //! \param ui32Port is the base address of the GPIO port.
  2308. //! \param ui8Pins is the bit-packed representation of the pin(s).
  2309. //!
  2310. //! This function disables a GPIO pin to be used as a trigger to start an ADC
  2311. //! sequence. This function can be used to disable this feature if it was
  2312. //! enabled via a call to GPIOADCTriggerEnable().
  2313. //!
  2314. //! \return None.
  2315. //
  2316. //*****************************************************************************
  2317. void
  2318. GPIOADCTriggerDisable(uint32_t ui32Port, uint8_t ui8Pins)
  2319. {
  2320. //
  2321. // Check the arguments.
  2322. //
  2323. ASSERT(_GPIOBaseValid(ui32Port));
  2324. //
  2325. // Set the pin as a DMA trigger.
  2326. //
  2327. HWREGB(ui32Port + GPIO_O_ADCCTL) &= (~ui8Pins);
  2328. }
  2329. //*****************************************************************************
  2330. //
  2331. // Close the Doxygen group.
  2332. //! @}
  2333. //
  2334. //*****************************************************************************