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 //
42 //! \addtogroup pin_api
43 //! @{
44 //
45 //*****************************************************************************
46 
47 #include "inc/hw_types.h"
48 #include "inc/hw_memmap.h"
49 #include "inc/hw_ocp_shared.h"
50 #include "pin.h"
51 
52 //*****************************************************************************
53 // PIN to PAD matrix
54 //*****************************************************************************
55 static const unsigned long g_ulPinToPadMap[64] =
56 {
57 	10,11,12,13,14,15,16,17,255,255,18,
58 	19,20,21,22,23,24,40,28,29,25,255,
59 	255,255,255,255,255,255,255,255,255,255,255,
60 	255,255,255,255,255,255,255,255,255,255,255,
61 	31,255,255,255,255,0,255,32,30,255,1,
62 	255,2,3,4,5,6,7,8,9
63 };
64 
65 
66 //*****************************************************************************
67 //
68 //! Configures pin mux for the specified pin.
69 //!
70 //! \param ulPin is a valid pin.
71 //! \param ulPinMode is one of the valid mode
72 //!
73 //! This function configures the pin mux that selects the peripheral function
74 //! associated with a particular SOC pin. Only one peripheral function at a
75 //! time can be associated with a pin, and each peripheral function should
76 //! only be associated with a single pin at a time.
77 //!
78 //! \return none
79 //
80 //*****************************************************************************
PinModeSet(unsigned long ulPin,unsigned long ulPinMode)81 void PinModeSet(unsigned long ulPin,unsigned long ulPinMode)
82 {
83 
84   unsigned long ulPad;
85 
86   //
87   // Get the corresponding Pad
88   //
89   ulPad = g_ulPinToPadMap[ulPin & 0x3F];
90 
91   //
92   // Calculate the register address
93   //
94   ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
95 
96   //
97   // Set the mode.
98   //
99   HWREG(ulPad) = (((HWREG(ulPad) & ~PAD_MODE_MASK) |  ulPinMode) & ~(3<<10));
100 
101 }
102 
103 //*****************************************************************************
104 //
105 //! Gets current pin mux configuration of specified pin.
106 //!
107 //! \param ulPin is a valid pin.
108 //!
109 //! This function get the current configuration of the pin mux.
110 //!
111 //! \return Returns current pin mode if \e ulPin is valid, 0xFF otherwise.
112 //
113 //*****************************************************************************
PinModeGet(unsigned long ulPin)114 unsigned long PinModeGet(unsigned long ulPin)
115 {
116 
117   unsigned long ulPad;
118 
119 
120   //
121   // Get the corresponding Pad
122   //
123   ulPad = g_ulPinToPadMap[ulPin & 0x3F];
124 
125 
126   //
127   // Calculate the register address
128   //
129   ulPad = ((ulPad << 2) + PAD_CONFIG_BASE) ;
130 
131   //
132   // return the mode.
133   //
134   return (HWREG(ulPad) & PAD_MODE_MASK);
135 
136 }
137 
138 //*****************************************************************************
139 //
140 //! Sets the direction of the specified pin(s).
141 //!
142 //! \param ulPin is one of the valid pin.
143 //! \param ulPinIO is the pin direction and/or mode.
144 //!
145 //! This function configures the specified pin(s) as either input only or
146 //! output only or it configures the pin to be under hardware control.
147 //!
148 //! The parameter \e ulPinIO is an enumerated data type that can be one of
149 //! the following values:
150 //!
151 //! - \b PIN_DIR_MODE_IN
152 //! - \b PIN_DIR_MODE_OUT
153 //! - \b PIN_DIR_MODE_HW
154 //!
155 //! where \b PIN_DIR_MODE_IN specifies that the pin is programmed as a
156 //! input only, \b PIN_DIR_MODE_OUT specifies that the pin is
157 //! programmed output only, and \b PIN_DIR_MODE_HW specifies that the pin is
158 //! placed under hardware control.
159 //!
160 //!
161 //! \return None.
162 //
163 //*****************************************************************************
PinDirModeSet(unsigned long ulPin,unsigned long ulPinIO)164 void PinDirModeSet(unsigned long ulPin, unsigned long ulPinIO)
165 {
166   unsigned long ulPad;
167 
168   //
169   // Get the corresponding Pad
170   //
171   ulPad = g_ulPinToPadMap[ulPin & 0x3F];
172 
173   //
174   // Calculate the register address
175   //
176   ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
177 
178   //
179   // Set the direction
180   //
181   HWREG(ulPad) = ((HWREG(ulPad) & ~0xC00) | ulPinIO);
182 }
183 
184 //*****************************************************************************
185 //
186 //! Gets the direction of a pin.
187 //!
188 //! \param ulPin is one of the valid pin.
189 //!
190 //! This function gets the direction and control mode for a specified pin on
191 //! the selected GPIO port.  The pin can be configured as either an input only
192 //! or output only, or it can be under hardware control.  The type of control
193 //! and direction are returned as an enumerated data type.
194 //!
195 //! \return Returns one of the enumerated data types described for
196 //! GPIODirModeSet().
197 //
198 //*****************************************************************************
PinDirModeGet(unsigned long ulPin)199 unsigned long PinDirModeGet(unsigned long ulPin)
200 {
201   unsigned long ulPad;
202 
203   //
204   // Get the corresponding Pad
205   //
206   ulPad = g_ulPinToPadMap[ulPin & 0x3F];
207 
208   //
209   // Calculate the register address
210   //
211   ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
212 
213   //
214   // Return the direction
215   //
216   return ((HWREG(ulPad) & 0xC00));
217 }
218 
219 //*****************************************************************************
220 //
221 //! Gets Pin output drive strength and Type
222 //!
223 //! \param ulPin is one of the valid pin
224 //! \param pulPinStrength is pointer to storage for output drive strength
225 //! \param pulPinType is pinter to storage for pin type
226 //!
227 //! This function gets the pin type and output drive strength for the pin
228 //! specified by \e ulPin parameter. Parameters \e pulPinStrength and
229 //! \e pulPinType corresponds to the values used in PinConfigSet().
230 //!
231 //!
232 //! \return None.
233 //
234 //*****************************************************************************
PinConfigGet(unsigned long ulPin,unsigned long * pulPinStrength,unsigned long * pulPinType)235 void PinConfigGet(unsigned long ulPin,unsigned long  *pulPinStrength,
236 	       					unsigned long *pulPinType)
237 {
238 
239   unsigned long ulPad;
240 
241 
242   //
243   // Get the corresponding Pad
244   //
245   ulPad = g_ulPinToPadMap[ulPin & 0x3F];
246 
247 
248   //
249   // Calculate the register address
250   //
251   ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
252 
253 
254   //
255   // Get the type
256   //
257   *pulPinType = (HWREG(ulPad) & PAD_TYPE_MASK);
258 
259   //
260   // Get the output drive strength
261   //
262   *pulPinStrength = (HWREG(ulPad) & PAD_STRENGTH_MASK);
263 
264 }
265 
266 //*****************************************************************************
267 //
268 //! Configure Pin output drive strength and Type
269 //!
270 //! \param ulPin is one of the valid pin
271 //! \param ulPinStrength is logical OR of valid output drive strengths.
272 //! \param ulPinType is one of the valid pin type.
273 //!
274 //!  This function sets the pin type and strength for the pin specified by
275 //! \e ulPin parameter.
276 //!
277 //! The parameter \e ulPinStrength should be one of the following
278 //! - \b PIN_STRENGTH_2MA
279 //! - \b PIN_STRENGTH_4MA
280 //! - \b PIN_STRENGTH_6MA
281 //!
282 //!
283 //! The parameter \e ulPinType should be one of the following
284 //! For standard type
285 //!
286 //! - \b PIN_TYPE_STD
287 //! - \b PIN_TYPE_STD_PU
288 //! - \b PIN_TYPE_STD_PD
289 //!
290 //! And for Open drain type
291 //!
292 //! - \b PIN_TYPE_OD
293 //! - \b PIN_TYPE_OD_PU
294 //! - \b PIN_TYPE_OD_PD
295 //!
296 //! \return None.
297 //
298 //*****************************************************************************
PinConfigSet(unsigned long ulPin,unsigned long ulPinStrength,unsigned long ulPinType)299 void PinConfigSet(unsigned long ulPin,unsigned long  ulPinStrength,
300 						unsigned long ulPinType)
301 {
302 
303   unsigned long ulPad;
304 
305   //
306   // Get the corresponding Pad
307   //
308   ulPad = g_ulPinToPadMap[ulPin & 0x3F];
309 
310   //
311   // Write the register
312   //
313   if(ulPinType == PIN_TYPE_ANALOG)
314   {
315     //
316     // Isolate the input
317     //
318     HWREG(0x4402E144) |= ((0x80 << ulPad) & (0x1E << 8));
319 
320     //
321     // Calculate the register address
322     //
323     ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
324 
325     //
326     // Isolate the output
327     //
328     HWREG(ulPad) = 0xC00;
329 
330   }
331   else
332   {
333     //
334     // Enable the input
335     //
336     HWREG(0x4402E144) &= ~((0x80 << ulPad) & (0x1E << 8));
337 
338     //
339     // Calculate the register address
340     //
341     ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
342 
343     //
344     // Write the configuration
345     //
346     HWREG(ulPad) = ((HWREG(ulPad) & ~(PAD_STRENGTH_MASK | PAD_TYPE_MASK)) |
347 		  		(ulPinStrength | ulPinType ));
348   }
349 
350 
351 }
352 
353 //*****************************************************************************
354 //
355 //! Sets the pin mode and configures the pin for use by UART peripheral
356 //!
357 //! \param ulPin is one of the valid pin.
358 //! \param ulPinMode is one of the valid pin mode.
359 //!
360 //! The UART pins must be properly configured for the peripheral to
361 //! function correctly.  This function provides a typical configuration for
362 //! those pin(s); other configurations may work as well depending upon the
363 //! board setup (for example, using the on-chip pull-ups).
364 //!
365 //!
366 //! \note This function cannot be used to turn any pin into a UART pin; it
367 //! only sets the pin mode and configures it for proper UART operation.
368 //!
369 //!
370 //! \return None.
371 //
372 //*****************************************************************************
PinTypeUART(unsigned long ulPin,unsigned long ulPinMode)373 void PinTypeUART(unsigned long ulPin,unsigned long ulPinMode)
374 {
375     //
376     // Set the pin to specified mode
377     //
378     PinModeSet(ulPin,ulPinMode);
379 
380     //
381     // Set the pin for standard operation
382     //
383     PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_STD);
384 }
385 
386 
387 //*****************************************************************************
388 //
389 //! Sets the pin mode and configures the pin for use by I2C peripheral
390 //!
391 //! \param ulPin is one of the valid pin.
392 //! \param ulPinMode is one of the valid pin mode.
393 //!
394 //! The I2C pins must be properly configured for the peripheral to
395 //! function correctly.  This function provides a typical configuration for
396 //! the pin.
397 //!
398 //!
399 //! \note This function cannot be used to turn any pin into a I2C pin; it
400 //! only sets the pin mode and configures it for proper I2C operation.
401 //!
402 //!
403 //! \return None.
404 //
405 //*****************************************************************************
PinTypeI2C(unsigned long ulPin,unsigned long ulPinMode)406 void PinTypeI2C(unsigned long ulPin,unsigned long ulPinMode)
407 {
408     //
409     // Set the pin to specified mode
410     //
411     PinModeSet(ulPin,ulPinMode);
412 
413     //
414     // Set the pin for open-drain operation with a weak pull-up.
415     //
416     PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_OD_PU);
417 }
418 
419 
420 //*****************************************************************************
421 //
422 //! Sets the pin mode and configures the pin for use by SPI peripheral
423 //!
424 //! \param ulPin is one of the valid pin.
425 //! \param ulPinMode is one of the valid pin mode.
426 //!
427 //! The SPI pins must be properly configured for the peripheral to
428 //! function correctly.  This function provides a typical configuration for
429 //! those pin.
430 //!
431 //!
432 //! \note This function cannot be used to turn any pin into a SPI pin; it
433 //! only sets the pin mode and configures it for proper SPI operation.
434 //!
435 //!
436 //! \return None.
437 //
438 //*****************************************************************************
PinTypeSPI(unsigned long ulPin,unsigned long ulPinMode)439 void PinTypeSPI(unsigned long ulPin,unsigned long ulPinMode)
440 {
441 
442     //
443     // Set the pin to specified mode
444     //
445     PinModeSet(ulPin,ulPinMode);
446 
447     //
448     // Set the pin for standard operation
449     //
450     PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
451 
452 }
453 
454 
455 //*****************************************************************************
456 //
457 //! Sets the pin mode and configures the pin for use by I2S peripheral
458 //!
459 //! \param ulPin is one of the valid pin.
460 //! \param ulPinMode is one of the valid pin mode.
461 //!
462 //! The I2S 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 I2S pin; it
468 //! only sets the pin mode and configures it for proper I2S operation.
469 //!
470 //! \return None.
471 //
472 //*****************************************************************************
PinTypeI2S(unsigned long ulPin,unsigned long ulPinMode)473 void PinTypeI2S(unsigned long ulPin,unsigned long ulPinMode)
474 {
475 
476     //
477     // Set the pin to specified mode
478     //
479     PinModeSet(ulPin,ulPinMode);
480 
481     //
482     // Set the pin for standard operation
483     //
484     PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
485 
486 }
487 
488 
489 //*****************************************************************************
490 //
491 //! Sets the pin mode and configures the pin for use by Timer peripheral
492 //!
493 //! \param ulPin is one of the valid pin.
494 //! \param ulPinMode is one of the valid pin mode.
495 //!
496 //! The timer PWM pins must be properly configured for the Timer peripheral to
497 //! function correctly.  This function provides a typical configuration for
498 //! those pin; other configurations may work as well depending upon the
499 //! board setup (for example, using the on-chip pull-ups).
500 //!
501 //!
502 //! \note This function cannot be used to turn any pin into a timer PWM pin; it
503 //! only sets the pin mode and configures it for proper timer PWM operation.
504 //!
505 //! \return None.
506 //
507 //*****************************************************************************
PinTypeTimer(unsigned long ulPin,unsigned long ulPinMode)508 void PinTypeTimer(unsigned long ulPin,unsigned long ulPinMode)
509 {
510 
511     //
512     // Set the pin to specified mode
513     //
514     PinModeSet(ulPin,ulPinMode);
515 
516     //
517     // Set the pin for standard operation
518     //
519     PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
520 }
521 
522 
523 //*****************************************************************************
524 //
525 //! Sets the pin mode and configures the pin for use by Camera peripheral
526 //!
527 //! \param ulPin is one of the valid pin.
528 //! \param ulPinMode is one of the valid pin mode.
529 //!
530 //! The Camera pins must be properly configured for the peripheral to
531 //! function correctly.  This function provides a typical configuration for
532 //! those pin.
533 //!
534 //!
535 //! \note This function cannot be used to turn any pin into a Camera pin; it
536 //! only sets the pin mode and configures it for proper Camera operation.
537 //!
538 //! \return None.
539 //
540 //*****************************************************************************
PinTypeCamera(unsigned long ulPin,unsigned long ulPinMode)541 void PinTypeCamera(unsigned long ulPin,unsigned long ulPinMode)
542 {
543 
544     //
545     // Set the pin to specified mode
546     //
547     PinModeSet(ulPin,ulPinMode);
548 
549     //
550     // Set the pin for standard operation
551     //
552     PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
553 
554 }
555 
556 
557 //*****************************************************************************
558 //
559 //! Sets the pin mode and configures the pin for use by GPIO peripheral
560 //!
561 //! \param ulPin is one of the valid pin.
562 //! \param ulPinMode is one of the valid pin mode.
563 //! \param bOpenDrain is one to decide either OpenDrain or STD
564 //!
565 //! The GPIO pins must be properly configured for the peripheral to
566 //! function correctly.  This function provides a typical configuration for
567 //! those pin.
568 //!
569 //!
570 //! \return None.
571 //
572 //*****************************************************************************
PinTypeGPIO(unsigned long ulPin,unsigned long ulPinMode,tBoolean bOpenDrain)573 void PinTypeGPIO(unsigned long ulPin,unsigned long ulPinMode,tBoolean bOpenDrain)
574 {
575 
576     //
577     // Set the pin for standard push-pull operation.
578     //
579     if(bOpenDrain)
580     {
581             PinConfigSet(ulPin, PIN_STRENGTH_2MA, PIN_TYPE_OD);
582     }
583     else
584     {
585             PinConfigSet(ulPin, PIN_STRENGTH_2MA, PIN_TYPE_STD);
586     }
587 
588     //
589     // Set the pin to specified mode
590     //
591     PinModeSet(ulPin, ulPinMode);
592 
593 }
594 
595 //*****************************************************************************
596 //
597 //! Sets the pin mode and configures the pin for use by ADC
598 //!
599 //! \param ulPin is one of the valid pin.
600 //! \param ulPinMode is one of the valid pin mode.
601 //!
602 //! The ADC pins must be properly configured for the peripheral to
603 //! function correctly.  This function provides a typical configuration for
604 //! those pin.
605 //!
606 //!
607 //! \note This function cannot be used to turn any pin into a ADC pin; it
608 //! only sets the pin mode and configures it for proper ADC operation.
609 //!
610 //! \return None.
611 //
612 //*****************************************************************************
PinTypeADC(unsigned long ulPin,unsigned long ulPinMode)613 void PinTypeADC(unsigned long ulPin,unsigned long ulPinMode)
614 {
615   //
616   // Configure the Pin
617   //
618   PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_ANALOG);
619 }
620 
621 //*****************************************************************************
622 //
623 //! Sets the pin mode and configures the pin for use by SD Host peripheral
624 //!
625 //! \param ulPin is one of the valid pin.
626 //! \param ulPinMode is one of the valid pin mode.
627 //!
628 //! The MMC pins must be properly configured for the peripheral to
629 //! function correctly.  This function provides a typical configuration for
630 //! those pin.
631 //!
632 //!
633 //! \note This function cannot be used to turn any pin into a SD Host pin; it
634 //! only sets the pin mode and configures it for proper SD Host operation.
635 //!
636 //! \return None.
637 //
638 //*****************************************************************************
PinTypeSDHost(unsigned long ulPin,unsigned long ulPinMode)639 void PinTypeSDHost(unsigned long ulPin,unsigned long ulPinMode)
640 {
641   //
642   // Set pin mode
643   //
644   PinModeSet(ulPin,ulPinMode);
645 
646   //
647   // Configure the Pin
648   //
649   PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_STD);
650 
651 }
652 
653 //*****************************************************************************
654 //
655 // Close the Doxygen group.
656 //! @}
657 //
658 //*****************************************************************************
659