1
0

F2837xD_Gpio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. //###########################################################################
  2. //
  3. // FILE: F2837xD_Gpio.c
  4. //
  5. // TITLE: GPIO module support functions
  6. //
  7. //###########################################################################
  8. // $TI Release: F2837xD Support Library v3.05.00.00 $
  9. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  10. // $Copyright:
  11. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions
  15. // are met:
  16. //
  17. // Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // Neither the name of Texas Instruments Incorporated nor the names of
  26. // its contributors may be used to endorse or promote products derived
  27. // from this software without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. // $
  41. //###########################################################################
  42. //
  43. // Included Files
  44. //
  45. #include "F2837xD_device.h"
  46. #include "F2837xD_Examples.h"
  47. //
  48. //Low-level functions for GPIO configuration (CPU1 only)
  49. //
  50. #ifdef CPU1
  51. //
  52. // InitGpio - Sets all pins to be muxed to GPIO in input mode with pull-ups
  53. // enabled. Also resets CPU control to CPU1 and disables open
  54. // drain and polarity inversion and sets the qualification to
  55. // synchronous. Also unlocks all GPIOs. Only one CPU should call
  56. // this function.
  57. //
  58. void InitGpio()
  59. {
  60. volatile Uint32 *gpioBaseAddr;
  61. Uint16 regOffset;
  62. //
  63. //Disable pin locks
  64. //
  65. EALLOW;
  66. GpioCtrlRegs.GPALOCK.all = 0x00000000;
  67. GpioCtrlRegs.GPBLOCK.all = 0x00000000;
  68. GpioCtrlRegs.GPCLOCK.all = 0x00000000;
  69. GpioCtrlRegs.GPDLOCK.all = 0x00000000;
  70. GpioCtrlRegs.GPELOCK.all = 0x00000000;
  71. GpioCtrlRegs.GPFLOCK.all = 0x00000000;
  72. //
  73. // Fill all registers with zeros. Writing to each register separately
  74. // for six GPIO modules would make this function *very* long.
  75. // Fortunately, we'd be writing them all with zeros anyway, so this
  76. // saves a lot of space.
  77. //
  78. gpioBaseAddr = (Uint32 *)&GpioCtrlRegs;
  79. for (regOffset = 0; regOffset < sizeof(GpioCtrlRegs)/2; regOffset++)
  80. {
  81. //
  82. //Hack to avoid enabling pull-ups on all pins. GPyPUD is offset
  83. //0x0C in each register group of 0x40 words. Since this is a
  84. //32-bit pointer, the addresses must be divided by 2.
  85. //
  86. if (regOffset % (0x40/2) != (0x0C/2))
  87. {
  88. gpioBaseAddr[regOffset] = 0x00000000;
  89. }
  90. }
  91. gpioBaseAddr = (Uint32 *)&GpioDataRegs;
  92. for (regOffset = 0; regOffset < sizeof(GpioDataRegs)/2; regOffset++)
  93. {
  94. gpioBaseAddr[regOffset] = 0x00000000;
  95. }
  96. EDIS;
  97. }
  98. //
  99. // GPIO_SetupPinMux - Set the peripheral muxing for the specified pin. The
  100. // appropriate parameters can be found in the GPIO Muxed
  101. // Pins table(4.4) in the datasheet. Use the GPIO index
  102. // row (0 to 15) to select a muxing option for the GPIO.
  103. //
  104. void GPIO_SetupPinMux(Uint16 gpioNumber, Uint16 cpu, Uint16 muxPosition)
  105. {
  106. volatile Uint32 *gpioBaseAddr;
  107. volatile Uint32 *mux, *gmux, *csel;
  108. Uint16 pin32, pin16, pin8;
  109. pin32 = gpioNumber % 32;
  110. pin16 = gpioNumber % 16;
  111. pin8 = gpioNumber % 8;
  112. gpioBaseAddr = (Uint32 *)&GpioCtrlRegs + (gpioNumber/32)*GPY_CTRL_OFFSET;
  113. //
  114. //Sanity check for valid cpu and peripheral values
  115. //
  116. if (cpu > GPIO_MUX_CPU2CLA || muxPosition > 0xF)
  117. return;
  118. //
  119. //Create pointers to the appropriate registers. This is a workaround
  120. //for the way GPIO registers are defined. The standard definition
  121. //in the header file makes it very easy to do named accesses of one
  122. //register or bit, but hard to do arbitrary numerical accesses. It's
  123. //easier to have an array of GPIO modules with identical registers,
  124. //including arrays for multi-register groups like GPyCSEL1-4. But
  125. //the header file doesn't define anything we can turn into an array,
  126. //so manual pointer arithmetic is used instead.
  127. //
  128. mux = gpioBaseAddr + GPYMUX + pin32/16;
  129. gmux = gpioBaseAddr + GPYGMUX + pin32/16;
  130. csel = gpioBaseAddr + GPYCSEL + pin32/8;
  131. //
  132. //Now for the actual function
  133. //
  134. EALLOW;
  135. //
  136. //To change the muxing, set the peripheral mux to 0/GPIO first to avoid
  137. //glitches, then change the group mux, then set the peripheral mux to
  138. //its target value. Finally, set the CPU select. This procedure is
  139. //described in the TRM. Unfortunately, since we don't know the pin in
  140. //advance we can't hardcode a bitfield reference, so there's some
  141. //tricky bit twiddling here.
  142. //
  143. *mux &= ~(0x3UL << (2*pin16));
  144. *gmux &= ~(0x3UL << (2*pin16));
  145. *gmux |= (Uint32)((muxPosition >> 2) & 0x3UL) << (2*pin16);
  146. *mux |= (Uint32)(muxPosition & 0x3UL) << (2*pin16);
  147. *csel &= ~(0x3L << (4*pin8));
  148. *csel |= (Uint32)(cpu & 0x3L) << (4*pin8);
  149. //
  150. //WARNING: This code does not touch the analog mode select registers,
  151. //which are needed to give the USB module control of its IOs.
  152. //
  153. EDIS;
  154. }
  155. //
  156. // GPIO_SetupPinOptions - Setup up the GPIO input/output options for the
  157. // specified pin.
  158. //
  159. //The flags are a 16-bit mask produced by ORing together options.
  160. //For input pins, the valid flags are:
  161. //GPIO_PULLUP Enable pull-up
  162. //GPIO_INVERT Enable input polarity inversion
  163. //GPIO_SYNC Synchronize the input latch to PLLSYSCLK
  164. // (default -- you don't need to specify this)
  165. //GPIO_QUAL3 Use 3-sample qualification
  166. //GPIO_QUAL6 Use 6-sample qualification
  167. //GPIO_ASYNC Do not use synchronization or qualification
  168. //(Note: only one of SYNC, QUAL3, QUAL6, or ASYNC is allowed)
  169. //
  170. //For output pins, the valid flags are:
  171. //GPIO_OPENDRAIN Output in open drain mode
  172. //GPIO_PULLUP If open drain enabled, also enable the pull-up
  173. //and the input qualification flags (SYNC/QUAL3/QUAL6/SYNC) listed above.
  174. //
  175. //With no flags, the default input state is synchronous with no
  176. //pull-up or polarity inversion. The default output state is
  177. //the standard digital output.
  178. //
  179. void GPIO_SetupPinOptions(Uint16 gpioNumber, Uint16 output, Uint16 flags)
  180. {
  181. volatile Uint32 *gpioBaseAddr;
  182. volatile Uint32 *dir, *pud, *inv, *odr, *qsel;
  183. Uint32 pin32, pin16, pinMask, qual;
  184. pin32 = gpioNumber % 32;
  185. pin16 = gpioNumber % 16;
  186. pinMask = 1UL << pin32;
  187. gpioBaseAddr = (Uint32 *)&GpioCtrlRegs + (gpioNumber/32)*GPY_CTRL_OFFSET;
  188. //
  189. //Create pointers to the appropriate registers. This is a workaround
  190. //for the way GPIO registers are defined. The standard definition
  191. //in the header file makes it very easy to do named accesses of one
  192. //register or bit, but hard to do arbitrary numerical accesses. It's
  193. //easier to have an array of GPIO modules with identical registers,
  194. //including arrays for multi-register groups like GPyQSEL1-2. But
  195. //the header file doesn't define anything we can turn into an array,
  196. //so manual pointer arithmetic is used instead.
  197. //
  198. dir = gpioBaseAddr + GPYDIR;
  199. pud = gpioBaseAddr + GPYPUD;
  200. inv = gpioBaseAddr + GPYINV;
  201. odr = gpioBaseAddr + GPYODR;
  202. qsel = gpioBaseAddr + GPYQSEL + pin32/16;
  203. EALLOW;
  204. //
  205. //Set the data direction
  206. //
  207. *dir &= ~pinMask;
  208. if (output == 1)
  209. {
  210. //
  211. //Output, with optional open drain mode and pull-up
  212. //
  213. *dir |= pinMask;
  214. //
  215. //Enable open drain if necessary
  216. //
  217. if (flags & GPIO_OPENDRAIN)
  218. {
  219. *odr |= pinMask;
  220. }
  221. else
  222. {
  223. *odr &= ~pinMask;
  224. }
  225. //
  226. //Enable pull-up if necessary. Open drain mode must be active.
  227. //
  228. if (flags & (GPIO_OPENDRAIN | GPIO_PULLUP))
  229. {
  230. *pud &= ~pinMask;
  231. }
  232. else
  233. {
  234. *pud |= pinMask;
  235. }
  236. }
  237. else
  238. {
  239. //
  240. //Input, with optional pull-up, qualification, and polarity
  241. //inversion
  242. //
  243. *dir &= ~pinMask;
  244. //
  245. //Enable pull-up if necessary
  246. //
  247. if (flags & GPIO_PULLUP)
  248. {
  249. *pud &= ~pinMask;
  250. }
  251. else
  252. {
  253. *pud |= pinMask;
  254. }
  255. //
  256. //Invert polarity if necessary
  257. //
  258. if (flags & GPIO_INVERT)
  259. {
  260. *inv |= pinMask;
  261. }
  262. else
  263. {
  264. *inv &= ~pinMask;
  265. }
  266. }
  267. //
  268. //Extract the qualification parameter and load it into the register.
  269. //This is also needed for open drain outputs, so we might as well do it
  270. //all the time.
  271. //
  272. qual = (flags & GPIO_ASYNC) / GPIO_QUAL3;
  273. *qsel &= ~(0x3L << (2 * pin16));
  274. if (qual != 0x0)
  275. {
  276. *qsel |= qual << (2 * pin16);
  277. }
  278. EDIS;
  279. }
  280. //
  281. // GPIO_SetupLock - Enable or disable the GPIO register bit lock for the
  282. // specified pin.
  283. // The valid flags are:
  284. // GPIO_UNLOCK - Unlock the pin setup register bits for
  285. // the specified pin
  286. // GPIO_LOCK - Lock the pin setup register bits for the
  287. // specified pin
  288. //
  289. void GPIO_SetupLock(Uint16 gpioNumber, Uint16 flags)
  290. {
  291. volatile Uint32 *gpioBaseAddr;
  292. volatile Uint32 *lock;
  293. Uint32 pin32, pinMask;
  294. pin32 = gpioNumber % 32;
  295. pinMask = 1UL << pin32;
  296. gpioBaseAddr = (Uint32 *)&GpioCtrlRegs + (gpioNumber/32)*GPY_CTRL_OFFSET;
  297. //
  298. //Create pointers to the appropriate registers. This is a workaround
  299. //for the way GPIO registers are defined. The standard definition
  300. //in the header file makes it very easy to do named accesses of one
  301. //register or bit, but hard to do arbitrary numerical accesses. It's
  302. //easier to have an array of GPIO modules with identical registers,
  303. //including arrays for multi-register groups like GPyQSEL1-2. But
  304. //the header file doesn't define anything we can turn into an array,
  305. //so manual pointer arithmetic is used instead.
  306. //
  307. lock = gpioBaseAddr + GPYLOCK;
  308. EALLOW;
  309. if(flags)
  310. {
  311. //Lock the pin
  312. *lock |= pinMask;
  313. }
  314. else
  315. {
  316. //Unlock the pin
  317. *lock &= ~pinMask;
  318. }
  319. EDIS;
  320. }
  321. //
  322. //External interrupt setup
  323. //
  324. void GPIO_SetupXINT1Gpio(Uint16 gpioNumber)
  325. {
  326. EALLOW;
  327. InputXbarRegs.INPUT4SELECT = gpioNumber; //Set XINT1 source to GPIO-pin
  328. EDIS;
  329. }
  330. void GPIO_SetupXINT2Gpio(Uint16 gpioNumber)
  331. {
  332. EALLOW;
  333. InputXbarRegs.INPUT5SELECT = gpioNumber; //Set XINT2 source to GPIO-pin
  334. EDIS;
  335. }
  336. void GPIO_SetupXINT3Gpio(Uint16 gpioNumber)
  337. {
  338. EALLOW;
  339. InputXbarRegs.INPUT6SELECT = gpioNumber; //Set XINT3 source to GPIO-pin
  340. EDIS;
  341. }
  342. void GPIO_SetupXINT4Gpio(Uint16 gpioNumber)
  343. {
  344. EALLOW;
  345. InputXbarRegs.INPUT13SELECT = gpioNumber; //Set XINT4 source to GPIO-pin
  346. EDIS;
  347. }
  348. void GPIO_SetupXINT5Gpio(Uint16 gpioNumber)
  349. {
  350. EALLOW;
  351. InputXbarRegs.INPUT14SELECT = gpioNumber; //Set XINT5 source to GPIO-pin
  352. EDIS;
  353. }
  354. //
  355. //GPIO_EnableUnbondedIOPullupsFor176Pin - Enable pullups for the unbonded
  356. // GPIOs on the 176PTP package:
  357. // GPIOs Grp Bits
  358. // 95-132 C 31
  359. // D 31:0
  360. // E 4:0
  361. // 134-168 E 31:6
  362. // F 8:0
  363. //
  364. void GPIO_EnableUnbondedIOPullupsFor176Pin()
  365. {
  366. EALLOW;
  367. GpioCtrlRegs.GPCPUD.all = ~0x80000000; //GPIO 95
  368. GpioCtrlRegs.GPDPUD.all = ~0xFFFFFFF7; //GPIOs 96-127
  369. GpioCtrlRegs.GPEPUD.all = ~0xFFFFFFDF; //GPIOs 128-159 except for 133
  370. GpioCtrlRegs.GPFPUD.all = ~0x000001FF; //GPIOs 160-168
  371. EDIS;
  372. }
  373. //
  374. // GPIO_EnableUnbondedIOPullupsFor100Pin - Enable pullups for the unbonded
  375. // GPIOs on the 100PZ package:
  376. // GPIOs Grp Bits
  377. // 0-1 A 1:0
  378. // 5-9 A 9:5
  379. // 22-40 A 31:22
  380. // B 8:0
  381. // 44-57 B 25:12
  382. // 67-68 C 4:3
  383. // 74-77 C 13:10
  384. // 79-83 C 19:15
  385. // 93-168 C 31:29
  386. // D 31:0
  387. // E 31:0
  388. // F 8:0
  389. //
  390. void GPIO_EnableUnbondedIOPullupsFor100Pin()
  391. {
  392. EALLOW;
  393. GpioCtrlRegs.GPAPUD.all = ~0xFFC003E3; //GPIOs 0-1, 5-9, 22-31
  394. GpioCtrlRegs.GPBPUD.all = ~0x03FFF1FF; //GPIOs 32-40, 44-57
  395. GpioCtrlRegs.GPCPUD.all = ~0xE10FBC18; //GPIOs 67-68, 74-77, 79-83, 93-95
  396. GpioCtrlRegs.GPDPUD.all = ~0xFFFFFFF7; //GPIOs 96-127
  397. GpioCtrlRegs.GPEPUD.all = ~0xFFFFFFFF; //GPIOs 128-159
  398. GpioCtrlRegs.GPFPUD.all = ~0x000001FF; //GPIOs 160-168
  399. EDIS;
  400. }
  401. //
  402. // GPIO_EnableUnbondedIOPullups - InitSysCtrl would call this function
  403. // this takes care of enabling IO pullups.
  404. //
  405. void GPIO_EnableUnbondedIOPullups()
  406. {
  407. //
  408. //bits 8-10 have pin count
  409. //
  410. unsigned char pin_count = ((DevCfgRegs.PARTIDL.all & 0x00000700) >> 8) ;
  411. //
  412. //5 = 100 pin
  413. //6 = 176 pin
  414. //7 = 337 pin
  415. //
  416. if(pin_count == 5)
  417. {
  418. GPIO_EnableUnbondedIOPullupsFor100Pin();
  419. }
  420. else if (pin_count == 6)
  421. {
  422. GPIO_EnableUnbondedIOPullupsFor176Pin();
  423. }
  424. else
  425. {
  426. //do nothing - this is 337 pin package
  427. }
  428. }
  429. #endif //CPU1
  430. //
  431. // GPIO_ReadPin - Read the GPyDAT register bit for the specified pin. Note that
  432. // this returns the actual state of the pin, not the state of
  433. // the output latch.
  434. //
  435. Uint16 GPIO_ReadPin(Uint16 gpioNumber)
  436. {
  437. volatile Uint32 *gpioDataReg;
  438. Uint16 pinVal;
  439. gpioDataReg = (volatile Uint32 *)&GpioDataRegs + (gpioNumber/32)*GPY_DATA_OFFSET;
  440. pinVal = (gpioDataReg[GPYDAT] >> (gpioNumber % 32)) & 0x1;
  441. return pinVal;
  442. }
  443. //
  444. // GPIO_WritePin - Set the GPyDAT register bit for the specified pin.
  445. //
  446. void GPIO_WritePin(Uint16 gpioNumber, Uint16 outVal)
  447. {
  448. volatile Uint32 *gpioDataReg;
  449. Uint32 pinMask;
  450. gpioDataReg = (volatile Uint32 *)&GpioDataRegs + (gpioNumber/32)*GPY_DATA_OFFSET;
  451. pinMask = 1UL << (gpioNumber % 32);
  452. if (outVal == 0)
  453. {
  454. gpioDataReg[GPYCLEAR] = pinMask;
  455. }
  456. else
  457. {
  458. gpioDataReg[GPYSET] = pinMask;
  459. }
  460. }
  461. //
  462. // End of file
  463. //