pin.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. //*****************************************************************************
  2. //
  3. // pin.c
  4. //
  5. // Mapping of peripherals to pins.
  6. //
  7. // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  8. //
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions
  12. // are met:
  13. //
  14. // Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. //
  17. // Redistributions in binary form must reproduce the above copyright
  18. // notice, this list of conditions and the following disclaimer in the
  19. // documentation and/or other materials provided with the
  20. // distribution.
  21. //
  22. // Neither the name of Texas Instruments Incorporated nor the names of
  23. // its contributors may be used to endorse or promote products derived
  24. // from this software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup pin_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include "inc/hw_types.h"
  46. #include "inc/hw_memmap.h"
  47. #include "inc/hw_ocp_shared.h"
  48. #include "pin.h"
  49. //*****************************************************************************
  50. // PIN to PAD matrix
  51. //*****************************************************************************
  52. static const unsigned long g_ulPinToPadMap[64] =
  53. {
  54. 10,11,12,13,14,15,16,17,255,255,18,
  55. 19,20,21,22,23,24,40,28,29,25,255,
  56. 255,255,255,255,255,255,255,255,255,255,255,
  57. 255,255,255,255,255,255,255,255,255,255,255,
  58. 31,255,255,255,255,0,255,32,30,255,1,
  59. 255,2,3,4,5,6,7,8,9
  60. };
  61. //*****************************************************************************
  62. //
  63. //! Configures pin mux for the specified pin.
  64. //!
  65. //! \param ulPin is a valid pin.
  66. //! \param ulPinMode is one of the valid mode
  67. //!
  68. //! This function configures the pin mux that selects the peripheral function
  69. //! associated with a particular SOC pin. Only one peripheral function at a
  70. //! time can be associated with a pin, and each peripheral function should
  71. //! only be associated with a single pin at a time.
  72. //!
  73. //! \return none
  74. //
  75. //*****************************************************************************
  76. void PinModeSet(unsigned long ulPin,unsigned long ulPinMode)
  77. {
  78. unsigned long ulPad;
  79. //
  80. // Get the corresponding Pad
  81. //
  82. ulPad = g_ulPinToPadMap[ulPin & 0x3F];
  83. //
  84. // Calculate the register address
  85. //
  86. ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
  87. //
  88. // Set the mode.
  89. //
  90. HWREG(ulPad) = (((HWREG(ulPad) & ~PAD_MODE_MASK) | ulPinMode) & ~(3<<10));
  91. }
  92. //*****************************************************************************
  93. //
  94. //! Gets current pin mux configuration of specified pin.
  95. //!
  96. //! \param ulPin is a valid pin.
  97. //!
  98. //! This function get the current configuration of the pin mux.
  99. //!
  100. //! \return Returns current pin mode if \e ulPin is valid, 0xFF otherwise.
  101. //
  102. //*****************************************************************************
  103. unsigned long PinModeGet(unsigned long ulPin)
  104. {
  105. unsigned long ulPad;
  106. //
  107. // Get the corresponding Pad
  108. //
  109. ulPad = g_ulPinToPadMap[ulPin & 0x3F];
  110. //
  111. // Calculate the register address
  112. //
  113. ulPad = ((ulPad << 2) + PAD_CONFIG_BASE) ;
  114. //
  115. // return the mode.
  116. //
  117. return (HWREG(ulPad) & PAD_MODE_MASK);
  118. }
  119. //*****************************************************************************
  120. //
  121. //! Sets the direction of the specified pin(s).
  122. //!
  123. //! \param ulPin is one of the valid pin.
  124. //! \param ulPinIO is the pin direction and/or mode.
  125. //!
  126. //! This function configures the specified pin(s) as either input only or
  127. //! output only or it configures the pin to be under hardware control.
  128. //!
  129. //! The parameter \e ulPinIO is an enumerated data type that can be one of
  130. //! the following values:
  131. //!
  132. //! - \b PIN_DIR_MODE_IN
  133. //! - \b PIN_DIR_MODE_OUT
  134. //! - \b PIN_DIR_MODE_HW
  135. //!
  136. //! where \b PIN_DIR_MODE_IN specifies that the pin is programmed as a
  137. //! input only, \b PIN_DIR_MODE_OUT specifies that the pin is
  138. //! programmed output only, and \b PIN_DIR_MODE_HW specifies that the pin is
  139. //! placed under hardware control.
  140. //!
  141. //!
  142. //! \return None.
  143. //
  144. //*****************************************************************************
  145. void PinDirModeSet(unsigned long ulPin, unsigned long ulPinIO)
  146. {
  147. unsigned long ulPad;
  148. //
  149. // Get the corresponding Pad
  150. //
  151. ulPad = g_ulPinToPadMap[ulPin & 0x3F];
  152. //
  153. // Calculate the register address
  154. //
  155. ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
  156. //
  157. // Set the direction
  158. //
  159. HWREG(ulPad) = ((HWREG(ulPad) & ~0xC00) | ulPinIO);
  160. }
  161. //*****************************************************************************
  162. //
  163. //! Gets the direction of a pin.
  164. //!
  165. //! \param ulPin is one of the valid pin.
  166. //!
  167. //! This function gets the direction and control mode for a specified pin on
  168. //! the selected GPIO port. The pin can be configured as either an input only
  169. //! or output only, or it can be under hardware control. The type of control
  170. //! and direction are returned as an enumerated data type.
  171. //!
  172. //! \return Returns one of the enumerated data types described for
  173. //! GPIODirModeSet().
  174. //
  175. //*****************************************************************************
  176. unsigned long PinDirModeGet(unsigned long ulPin)
  177. {
  178. unsigned long ulPad;
  179. //
  180. // Get the corresponding Pad
  181. //
  182. ulPad = g_ulPinToPadMap[ulPin & 0x3F];
  183. //
  184. // Calculate the register address
  185. //
  186. ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
  187. //
  188. // Return the direction
  189. //
  190. return ((HWREG(ulPad) & 0xC00));
  191. }
  192. //*****************************************************************************
  193. //
  194. //! Gets Pin output drive strength and Type
  195. //!
  196. //! \param ulPin is one of the valid pin
  197. //! \param pulPinStrength is pointer to storage for output drive strength
  198. //! \param pulPinType is pinter to storage for pin type
  199. //!
  200. //! This function gets the pin type and output drive strength for the pin
  201. //! specified by \e ulPin parameter. Parameters \e pulPinStrength and
  202. //! \e pulPinType corresponds to the values used in PinConfigSet().
  203. //!
  204. //!
  205. //! \return None.
  206. //
  207. //*****************************************************************************
  208. void PinConfigGet(unsigned long ulPin,unsigned long *pulPinStrength,
  209. unsigned long *pulPinType)
  210. {
  211. unsigned long ulPad;
  212. //
  213. // Get the corresponding Pad
  214. //
  215. ulPad = g_ulPinToPadMap[ulPin & 0x3F];
  216. //
  217. // Calculate the register address
  218. //
  219. ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
  220. //
  221. // Get the type
  222. //
  223. *pulPinType = (HWREG(ulPad) & PAD_TYPE_MASK);
  224. //
  225. // Get the output drive strength
  226. //
  227. *pulPinStrength = (HWREG(ulPad) & PAD_STRENGTH_MASK);
  228. }
  229. //*****************************************************************************
  230. //
  231. //! Configure Pin output drive strength and Type
  232. //!
  233. //! \param ulPin is one of the valid pin
  234. //! \param ulPinStrength is logical OR of valid output drive strengths.
  235. //! \param ulPinType is one of the valid pin type.
  236. //!
  237. //! This function sets the pin type and strength for the pin specified by
  238. //! \e ulPin parameter.
  239. //!
  240. //! The parameter \e ulPinStrength should be one of the following
  241. //! - \b PIN_STRENGTH_2MA
  242. //! - \b PIN_STRENGTH_4MA
  243. //! - \b PIN_STRENGTH_6MA
  244. //!
  245. //!
  246. //! The parameter \e ulPinType should be one of the following
  247. //! For standard type
  248. //!
  249. //! - \b PIN_TYPE_STD
  250. //! - \b PIN_TYPE_STD_PU
  251. //! - \b PIN_TYPE_STD_PD
  252. //!
  253. //! And for Open drain type
  254. //!
  255. //! - \b PIN_TYPE_OD
  256. //! - \b PIN_TYPE_OD_PU
  257. //! - \b PIN_TYPE_OD_PD
  258. //!
  259. //! \return None.
  260. //
  261. //*****************************************************************************
  262. void PinConfigSet(unsigned long ulPin,unsigned long ulPinStrength,
  263. unsigned long ulPinType)
  264. {
  265. unsigned long ulPad;
  266. //
  267. // Get the corresponding Pad
  268. //
  269. ulPad = g_ulPinToPadMap[ulPin & 0x3F];
  270. //
  271. // Write the register
  272. //
  273. if(ulPinType == PIN_TYPE_ANALOG)
  274. {
  275. //
  276. // Isolate the input
  277. //
  278. HWREG(0x4402E144) |= ((0x80 << ulPad) & (0x1E << 8));
  279. //
  280. // Calculate the register address
  281. //
  282. ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
  283. //
  284. // Isolate the output
  285. //
  286. HWREG(ulPad) = 0xC00;
  287. }
  288. else
  289. {
  290. //
  291. // Enable the input
  292. //
  293. HWREG(0x4402E144) &= ~((0x80 << ulPad) & (0x1E << 8));
  294. //
  295. // Calculate the register address
  296. //
  297. ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
  298. //
  299. // Write the configuration
  300. //
  301. HWREG(ulPad) = ((HWREG(ulPad) & ~(PAD_STRENGTH_MASK | PAD_TYPE_MASK)) |
  302. (ulPinStrength | ulPinType ));
  303. }
  304. }
  305. //*****************************************************************************
  306. //
  307. //! Sets the pin mode and configures the pin for use by UART peripheral
  308. //!
  309. //! \param ulPin is one of the valid pin.
  310. //! \param ulPinMode is one of the valid pin mode.
  311. //!
  312. //! The UART pins must be properly configured for the peripheral to
  313. //! function correctly. This function provides a typical configuration for
  314. //! those pin(s); other configurations may work as well depending upon the
  315. //! board setup (for example, using the on-chip pull-ups).
  316. //!
  317. //!
  318. //! \note This function cannot be used to turn any pin into a UART pin; it
  319. //! only sets the pin mode and configures it for proper UART operation.
  320. //!
  321. //!
  322. //! \return None.
  323. //
  324. //*****************************************************************************
  325. void PinTypeUART(unsigned long ulPin,unsigned long ulPinMode)
  326. {
  327. //
  328. // Set the pin to specified mode
  329. //
  330. PinModeSet(ulPin,ulPinMode);
  331. //
  332. // Set the pin for standard operation
  333. //
  334. PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_STD);
  335. }
  336. //*****************************************************************************
  337. //
  338. //! Sets the pin mode and configures the pin for use by I2C peripheral
  339. //!
  340. //! \param ulPin is one of the valid pin.
  341. //! \param ulPinMode is one of the valid pin mode.
  342. //!
  343. //! The I2C pins must be properly configured for the peripheral to
  344. //! function correctly. This function provides a typical configuration for
  345. //! the pin.
  346. //!
  347. //!
  348. //! \note This function cannot be used to turn any pin into a I2C pin; it
  349. //! only sets the pin mode and configures it for proper I2C operation.
  350. //!
  351. //!
  352. //! \return None.
  353. //
  354. //*****************************************************************************
  355. void PinTypeI2C(unsigned long ulPin,unsigned long ulPinMode)
  356. {
  357. //
  358. // Set the pin to specified mode
  359. //
  360. PinModeSet(ulPin,ulPinMode);
  361. //
  362. // Set the pin for open-drain operation with a weak pull-up.
  363. //
  364. PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_OD_PU);
  365. }
  366. //*****************************************************************************
  367. //
  368. //! Sets the pin mode and configures the pin for use by SPI peripheral
  369. //!
  370. //! \param ulPin is one of the valid pin.
  371. //! \param ulPinMode is one of the valid pin mode.
  372. //!
  373. //! The SPI pins must be properly configured for the peripheral to
  374. //! function correctly. This function provides a typical configuration for
  375. //! those pin.
  376. //!
  377. //!
  378. //! \note This function cannot be used to turn any pin into a SPI pin; it
  379. //! only sets the pin mode and configures it for proper SPI operation.
  380. //!
  381. //!
  382. //! \return None.
  383. //
  384. //*****************************************************************************
  385. void PinTypeSPI(unsigned long ulPin,unsigned long ulPinMode)
  386. {
  387. //
  388. // Set the pin to specified mode
  389. //
  390. PinModeSet(ulPin,ulPinMode);
  391. //
  392. // Set the pin for standard operation
  393. //
  394. PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
  395. }
  396. //*****************************************************************************
  397. //
  398. //! Sets the pin mode and configures the pin for use by I2S peripheral
  399. //!
  400. //! \param ulPin is one of the valid pin.
  401. //! \param ulPinMode is one of the valid pin mode.
  402. //!
  403. //! The I2S pins must be properly configured for the peripheral to
  404. //! function correctly. This function provides a typical configuration for
  405. //! those pin.
  406. //!
  407. //!
  408. //! \note This function cannot be used to turn any pin into a I2S pin; it
  409. //! only sets the pin mode and configures it for proper I2S operation.
  410. //!
  411. //! \return None.
  412. //
  413. //*****************************************************************************
  414. void PinTypeI2S(unsigned long ulPin,unsigned long ulPinMode)
  415. {
  416. //
  417. // Set the pin to specified mode
  418. //
  419. PinModeSet(ulPin,ulPinMode);
  420. //
  421. // Set the pin for standard operation
  422. //
  423. PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
  424. }
  425. //*****************************************************************************
  426. //
  427. //! Sets the pin mode and configures the pin for use by Timer peripheral
  428. //!
  429. //! \param ulPin is one of the valid pin.
  430. //! \param ulPinMode is one of the valid pin mode.
  431. //!
  432. //! The timer PWM pins must be properly configured for the Timer peripheral to
  433. //! function correctly. This function provides a typical configuration for
  434. //! those pin; other configurations may work as well depending upon the
  435. //! board setup (for example, using the on-chip pull-ups).
  436. //!
  437. //!
  438. //! \note This function cannot be used to turn any pin into a timer PWM pin; it
  439. //! only sets the pin mode and configures it for proper timer PWM operation.
  440. //!
  441. //! \return None.
  442. //
  443. //*****************************************************************************
  444. void PinTypeTimer(unsigned long ulPin,unsigned long ulPinMode)
  445. {
  446. //
  447. // Set the pin to specified mode
  448. //
  449. PinModeSet(ulPin,ulPinMode);
  450. //
  451. // Set the pin for standard operation
  452. //
  453. PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
  454. }
  455. //*****************************************************************************
  456. //
  457. //! Sets the pin mode and configures the pin for use by Camera peripheral
  458. //!
  459. //! \param ulPin is one of the valid pin.
  460. //! \param ulPinMode is one of the valid pin mode.
  461. //!
  462. //! The Camera pins must be properly configured for the peripheral to
  463. //! function correctly. This function provides a typical configuration for
  464. //! those pin.
  465. //!
  466. //!
  467. //! \note This function cannot be used to turn any pin into a Camera pin; it
  468. //! only sets the pin mode and configures it for proper Camera operation.
  469. //!
  470. //! \return None.
  471. //
  472. //*****************************************************************************
  473. void PinTypeCamera(unsigned long ulPin,unsigned long ulPinMode)
  474. {
  475. //
  476. // Set the pin to specified mode
  477. //
  478. PinModeSet(ulPin,ulPinMode);
  479. //
  480. // Set the pin for standard operation
  481. //
  482. PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
  483. }
  484. //*****************************************************************************
  485. //
  486. //! Sets the pin mode and configures the pin for use by GPIO peripheral
  487. //!
  488. //! \param ulPin is one of the valid pin.
  489. //! \param ulPinMode is one of the valid pin mode.
  490. //! \param bOpenDrain is one to decide either OpenDrain or STD
  491. //!
  492. //! The GPIO pins must be properly configured for the peripheral to
  493. //! function correctly. This function provides a typical configuration for
  494. //! those pin.
  495. //!
  496. //!
  497. //! \return None.
  498. //
  499. //*****************************************************************************
  500. void PinTypeGPIO(unsigned long ulPin,unsigned long ulPinMode,tBoolean bOpenDrain)
  501. {
  502. //
  503. // Set the pin for standard push-pull operation.
  504. //
  505. if(bOpenDrain)
  506. {
  507. PinConfigSet(ulPin, PIN_STRENGTH_2MA, PIN_TYPE_OD);
  508. }
  509. else
  510. {
  511. PinConfigSet(ulPin, PIN_STRENGTH_2MA, PIN_TYPE_STD);
  512. }
  513. //
  514. // Set the pin to specified mode
  515. //
  516. PinModeSet(ulPin, ulPinMode);
  517. }
  518. //*****************************************************************************
  519. //
  520. //! Sets the pin mode and configures the pin for use by ADC
  521. //!
  522. //! \param ulPin is one of the valid pin.
  523. //! \param ulPinMode is one of the valid pin mode.
  524. //!
  525. //! The ADC pins must be properly configured for the peripheral to
  526. //! function correctly. This function provides a typical configuration for
  527. //! those pin.
  528. //!
  529. //!
  530. //! \note This function cannot be used to turn any pin into a ADC pin; it
  531. //! only sets the pin mode and configures it for proper ADC operation.
  532. //!
  533. //! \return None.
  534. //
  535. //*****************************************************************************
  536. void PinTypeADC(unsigned long ulPin,unsigned long ulPinMode)
  537. {
  538. //
  539. // Configure the Pin
  540. //
  541. PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_ANALOG);
  542. }
  543. //*****************************************************************************
  544. //
  545. //! Sets the pin mode and configures the pin for use by SD Host peripheral
  546. //!
  547. //! \param ulPin is one of the valid pin.
  548. //! \param ulPinMode is one of the valid pin mode.
  549. //!
  550. //! The MMC pins must be properly configured for the peripheral to
  551. //! function correctly. This function provides a typical configuration for
  552. //! those pin.
  553. //!
  554. //!
  555. //! \note This function cannot be used to turn any pin into a SD Host pin; it
  556. //! only sets the pin mode and configures it for proper SD Host operation.
  557. //!
  558. //! \return None.
  559. //
  560. //*****************************************************************************
  561. void PinTypeSDHost(unsigned long ulPin,unsigned long ulPinMode)
  562. {
  563. //
  564. // Set pin mode
  565. //
  566. PinModeSet(ulPin,ulPinMode);
  567. //
  568. // Configure the Pin
  569. //
  570. PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_STD);
  571. }
  572. //*****************************************************************************
  573. //
  574. // Close the Doxygen group.
  575. //! @}
  576. //
  577. //*****************************************************************************