2 * -------------------------------------------
3 * CC3220 SDK - v0.10.00.00
4 * -------------------------------------------
6 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
37 //*****************************************************************************
41 // Mapping of peripherals to pins.
43 //*****************************************************************************
45 //*****************************************************************************
47 //! \addtogroup pin_api
50 //*****************************************************************************
52 #include "inc/hw_types.h"
53 #include "inc/hw_memmap.h"
54 #include "inc/hw_ocp_shared.h"
57 //*****************************************************************************
59 //*****************************************************************************
60 #define PAD_MODE_MASK 0x0000000F
61 #define PAD_STRENGTH_MASK 0x000000E0
62 #define PAD_TYPE_MASK 0x00000310
63 #define PAD_CONFIG_BASE ((OCP_SHARED_BASE + \
64 OCP_SHARED_O_GPIO_PAD_CONFIG_0))
66 //*****************************************************************************
68 //*****************************************************************************
69 static const unsigned long g_ulPinToPadMap[64] =
71 10,11,12,13,14,15,16,17,255,255,18,
72 19,20,21,22,23,24,40,28,29,25,255,
73 255,255,255,255,255,255,255,255,255,255,255,
74 255,255,255,255,255,255,255,255,255,255,255,
75 31,255,255,255,255,0,255,32,30,255,1,
80 //*****************************************************************************
82 //! Configures pin mux for the specified pin.
84 //! \param ulPin is a valid pin.
85 //! \param ulPinMode is one of the valid mode
87 //! This function configures the pin mux that selects the peripheral function
88 //! associated with a particular SOC pin. Only one peripheral function at a
89 //! time can be associated with a pin, and each peripheral function should
90 //! only be associated with a single pin at a time.
94 //*****************************************************************************
95 void PinModeSet(unsigned long ulPin,unsigned long ulPinMode)
101 // Get the corresponding Pad
103 ulPad = g_ulPinToPadMap[ulPin & 0x3F];
106 // Calculate the register address
108 ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
113 HWREG(ulPad) = (((HWREG(ulPad) & ~PAD_MODE_MASK) | ulPinMode) & ~(3<<10));
117 //*****************************************************************************
119 //! Gets current pin mux configuration of specified pin.
121 //! \param ulPin is a valid pin.
123 //! This function get the current configuration of the pin mux.
125 //! \return Returns current pin mode if \e ulPin is valid, 0xFF otherwise.
127 //*****************************************************************************
128 unsigned long PinModeGet(unsigned long ulPin)
135 // Get the corresponding Pad
137 ulPad = g_ulPinToPadMap[ulPin & 0x3F];
141 // Calculate the register address
143 ulPad = ((ulPad << 2) + PAD_CONFIG_BASE) ;
148 return (HWREG(ulPad) & PAD_MODE_MASK);
152 //*****************************************************************************
154 //! Sets the direction of the specified pin(s).
156 //! \param ulPin is one of the valid pin.
157 //! \param ulPinIO is the pin direction and/or mode.
159 //! This function configures the specified pin(s) as either input only or
160 //! output only or it configures the pin to be under hardware control.
162 //! The parameter \e ulPinIO is an enumerated data type that can be one of
163 //! the following values:
165 //! - \b PIN_DIR_MODE_IN
166 //! - \b PIN_DIR_MODE_OUT
167 //! - \b PIN_DIR_MODE_HW
169 //! where \b PIN_DIR_MODE_IN specifies that the pin is programmed as a
170 //! input only, \b PIN_DIR_MODE_OUT specifies that the pin is
171 //! programmed output only, and \b PIN_DIR_MODE_HW specifies that the pin is
172 //! placed under hardware control.
177 //*****************************************************************************
178 void PinDirModeSet(unsigned long ulPin, unsigned long ulPinIO)
183 // Get the corresponding Pad
185 ulPad = g_ulPinToPadMap[ulPin & 0x3F];
188 // Calculate the register address
190 ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
195 HWREG(ulPad) = ((HWREG(ulPad) & ~0xC00) | ulPinIO);
198 //*****************************************************************************
200 //! Gets the direction of a pin.
202 //! \param ulPin is one of the valid pin.
204 //! This function gets the direction and control mode for a specified pin on
205 //! the selected GPIO port. The pin can be configured as either an input only
206 //! or output only, or it can be under hardware control. The type of control
207 //! and direction are returned as an enumerated data type.
209 //! \return Returns one of the enumerated data types described for
210 //! GPIODirModeSet().
212 //*****************************************************************************
213 unsigned long PinDirModeGet(unsigned long ulPin)
218 // Get the corresponding Pad
220 ulPad = g_ulPinToPadMap[ulPin & 0x3F];
223 // Calculate the register address
225 ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
228 // Return the direction
230 return ((HWREG(ulPad) & 0xC00));
233 //*****************************************************************************
235 //! Gets Pin output drive strength and Type
237 //! \param ulPin is one of the valid pin
238 //! \param pulPinStrength is pointer to storage for output drive strength
239 //! \param pulPinType is pinter to storage for pin type
241 //! This function gets the pin type and output drive strength for the pin
242 //! specified by \e ulPin parameter. Parameters \e pulPinStrength and
243 //! \e pulPinType corresponds to the values used in PinConfigSet().
248 //*****************************************************************************
249 void PinConfigGet(unsigned long ulPin,unsigned long *pulPinStrength,
250 unsigned long *pulPinType)
257 // Get the corresponding Pad
259 ulPad = g_ulPinToPadMap[ulPin & 0x3F];
263 // Calculate the register address
265 ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
271 *pulPinType = (HWREG(ulPad) & PAD_TYPE_MASK);
274 // Get the output drive strength
276 *pulPinStrength = (HWREG(ulPad) & PAD_STRENGTH_MASK);
280 //*****************************************************************************
282 //! Configure Pin output drive strength and Type
284 //! \param ulPin is one of the valid pin
285 //! \param ulPinStrength is logical OR of valid output drive strengths.
286 //! \param ulPinType is one of the valid pin type.
288 //! This function sets the pin type and strength for the pin specified by
289 //! \e ulPin parameter.
291 //! The parameter \e ulPinStrength should be one of the following
292 //! - \b PIN_STRENGTH_2MA
293 //! - \b PIN_STRENGTH_4MA
294 //! - \b PIN_STRENGTH_6MA
297 //! The parameter \e ulPinType should be one of the following
298 //! For standard type
300 //! - \b PIN_TYPE_STD
301 //! - \b PIN_TYPE_STD_PU
302 //! - \b PIN_TYPE_STD_PD
304 //! And for Open drain type
307 //! - \b PIN_TYPE_OD_PU
308 //! - \b PIN_TYPE_OD_PD
312 //*****************************************************************************
313 void PinConfigSet(unsigned long ulPin,unsigned long ulPinStrength,
314 unsigned long ulPinType)
320 // Get the corresponding Pad
322 ulPad = g_ulPinToPadMap[ulPin & 0x3F];
325 // Write the register
327 if(ulPinType == PIN_TYPE_ANALOG)
332 HWREG(0x4402E144) |= ((0x80 << ulPad) & (0x1E << 8));
335 // Calculate the register address
337 ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
340 // Isolate the output
342 HWREG(ulPad) = 0xC00;
350 HWREG(0x4402E144) &= ~((0x80 << ulPad) & (0x1E << 8));
353 // Calculate the register address
355 ulPad = ((ulPad << 2) + PAD_CONFIG_BASE);
358 // Write the configuration
360 HWREG(ulPad) = ((HWREG(ulPad) & ~(PAD_STRENGTH_MASK | PAD_TYPE_MASK)) |
361 (ulPinStrength | ulPinType ));
367 //*****************************************************************************
369 //! Sets the pin mode and configures the pin for use by UART peripheral
371 //! \param ulPin is one of the valid pin.
372 //! \param ulPinMode is one of the valid pin mode.
374 //! The UART pins must be properly configured for the peripheral to
375 //! function correctly. This function provides a typical configuration for
376 //! those pin(s); other configurations may work as well depending upon the
377 //! board setup (for example, using the on-chip pull-ups).
380 //! \note This function cannot be used to turn any pin into a UART pin; it
381 //! only sets the pin mode and configures it for proper UART operation.
386 //*****************************************************************************
387 void PinTypeUART(unsigned long ulPin,unsigned long ulPinMode)
390 // Set the pin to specified mode
392 PinModeSet(ulPin,ulPinMode);
395 // Set the pin for standard operation
397 PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_STD);
401 //*****************************************************************************
403 //! Sets the pin mode and configures the pin for use by I2C peripheral
405 //! \param ulPin is one of the valid pin.
406 //! \param ulPinMode is one of the valid pin mode.
408 //! The I2C pins must be properly configured for the peripheral to
409 //! function correctly. This function provides a typical configuration for
413 //! \note This function cannot be used to turn any pin into a I2C pin; it
414 //! only sets the pin mode and configures it for proper I2C operation.
419 //*****************************************************************************
420 void PinTypeI2C(unsigned long ulPin,unsigned long ulPinMode)
423 // Set the pin to specified mode
425 PinModeSet(ulPin,ulPinMode);
428 // Set the pin for open-drain operation with a weak pull-up.
430 PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_OD_PU);
434 //*****************************************************************************
436 //! Sets the pin mode and configures the pin for use by SPI peripheral
438 //! \param ulPin is one of the valid pin.
439 //! \param ulPinMode is one of the valid pin mode.
441 //! The SPI pins must be properly configured for the peripheral to
442 //! function correctly. This function provides a typical configuration for
446 //! \note This function cannot be used to turn any pin into a SPI pin; it
447 //! only sets the pin mode and configures it for proper SPI operation.
452 //*****************************************************************************
453 void PinTypeSPI(unsigned long ulPin,unsigned long ulPinMode)
457 // Set the pin to specified mode
459 PinModeSet(ulPin,ulPinMode);
462 // Set the pin for standard operation
464 PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
469 //*****************************************************************************
471 //! Sets the pin mode and configures the pin for use by I2S peripheral
473 //! \param ulPin is one of the valid pin.
474 //! \param ulPinMode is one of the valid pin mode.
476 //! The I2S pins must be properly configured for the peripheral to
477 //! function correctly. This function provides a typical configuration for
481 //! \note This function cannot be used to turn any pin into a I2S pin; it
482 //! only sets the pin mode and configures it for proper I2S operation.
486 //*****************************************************************************
487 void PinTypeI2S(unsigned long ulPin,unsigned long ulPinMode)
491 // Set the pin to specified mode
493 PinModeSet(ulPin,ulPinMode);
496 // Set the pin for standard operation
498 PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
503 //*****************************************************************************
505 //! Sets the pin mode and configures the pin for use by Timer peripheral
507 //! \param ulPin is one of the valid pin.
508 //! \param ulPinMode is one of the valid pin mode.
510 //! The timer PWM pins must be properly configured for the Timer peripheral to
511 //! function correctly. This function provides a typical configuration for
512 //! those pin; other configurations may work as well depending upon the
513 //! board setup (for example, using the on-chip pull-ups).
516 //! \note This function cannot be used to turn any pin into a timer PWM pin; it
517 //! only sets the pin mode and configures it for proper timer PWM operation.
521 //*****************************************************************************
522 void PinTypeTimer(unsigned long ulPin,unsigned long ulPinMode)
526 // Set the pin to specified mode
528 PinModeSet(ulPin,ulPinMode);
531 // Set the pin for standard operation
533 PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
537 //*****************************************************************************
539 //! Sets the pin mode and configures the pin for use by Camera peripheral
541 //! \param ulPin is one of the valid pin.
542 //! \param ulPinMode is one of the valid pin mode.
544 //! The Camera pins must be properly configured for the peripheral to
545 //! function correctly. This function provides a typical configuration for
549 //! \note This function cannot be used to turn any pin into a Camera pin; it
550 //! only sets the pin mode and configures it for proper Camera operation.
554 //*****************************************************************************
555 void PinTypeCamera(unsigned long ulPin,unsigned long ulPinMode)
559 // Set the pin to specified mode
561 PinModeSet(ulPin,ulPinMode);
564 // Set the pin for standard operation
566 PinConfigSet(ulPin,PIN_STRENGTH_2MA|PIN_STRENGTH_4MA,PIN_TYPE_STD);
571 //*****************************************************************************
573 //! Sets the pin mode and configures the pin for use by GPIO peripheral
575 //! \param ulPin is one of the valid pin.
576 //! \param ulPinMode is one of the valid pin mode.
577 //! \param bOpenDrain is one to decide either OpenDrain or STD
579 //! The GPIO pins must be properly configured for the peripheral to
580 //! function correctly. This function provides a typical configuration for
586 //*****************************************************************************
587 void PinTypeGPIO(unsigned long ulPin,unsigned long ulPinMode,tBoolean bOpenDrain)
591 // Set the pin for standard push-pull operation.
595 PinConfigSet(ulPin, PIN_STRENGTH_2MA, PIN_TYPE_OD);
599 PinConfigSet(ulPin, PIN_STRENGTH_2MA, PIN_TYPE_STD);
603 // Set the pin to specified mode
605 PinModeSet(ulPin, ulPinMode);
609 //*****************************************************************************
611 //! Sets the pin mode and configures the pin for use by ADC
613 //! \param ulPin is one of the valid pin.
614 //! \param ulPinMode is one of the valid pin mode.
616 //! The ADC pins must be properly configured for the peripheral to
617 //! function correctly. This function provides a typical configuration for
621 //! \note This function cannot be used to turn any pin into a ADC pin; it
622 //! only sets the pin mode and configures it for proper ADC operation.
626 //*****************************************************************************
627 void PinTypeADC(unsigned long ulPin,unsigned long ulPinMode)
632 PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_ANALOG);
635 //*****************************************************************************
637 //! Sets the pin mode and configures the pin for use by SD Host peripheral
639 //! \param ulPin is one of the valid pin.
640 //! \param ulPinMode is one of the valid pin mode.
642 //! The MMC pins must be properly configured for the peripheral to
643 //! function correctly. This function provides a typical configuration for
647 //! \note This function cannot be used to turn any pin into a SD Host pin; it
648 //! only sets the pin mode and configures it for proper SD Host operation.
652 //*****************************************************************************
653 void PinTypeSDHost(unsigned long ulPin,unsigned long ulPinMode)
658 PinModeSet(ulPin,ulPinMode);
663 PinConfigSet(ulPin,PIN_STRENGTH_2MA,PIN_TYPE_STD);
668 //*****************************************************************************
670 //! Sets the hysteresis for all the pins
672 //! \param ulHysteresis is one of the valid predefined hysterisys values
674 //! This function sets the hysteresis vlaue for all the pins. The parameter
675 //! \e ulHysteresis can be on one the following:
676 //! -\b PIN_HYSTERESIS_OFF - To turn Off hysteresis, default on POR
677 //! -\b PIN_HYSTERESIS_10 - To turn On hysteresis, 10%
678 //! -\b PIN_HYSTERESIS_20 - To turn On hysteresis, 20%
679 //! -\b PIN_HYSTERESIS_30 - To turn On hysteresis, 30%
680 //! -\b PIN_HYSTERESIS_40 - To turn On hysteresis, 40%
684 //*****************************************************************************
685 void PinHysteresisSet(unsigned long ulHysteresis)
687 unsigned long ulRegValue;
690 // Read the current value
692 ulRegValue = (HWREG( OCP_SHARED_BASE + OCP_SHARED_O_GPIO_PAD_CMN_CONFIG )
696 // Set the new Hysteresis
698 if( ulHysteresis != PIN_HYSTERESIS_OFF )
700 ulRegValue |= (ulHysteresis & 0x0000001C);
704 // Write the new value
706 HWREG( OCP_SHARED_BASE + OCP_SHARED_O_GPIO_PAD_CMN_CONFIG ) = ulRegValue;
709 //*****************************************************************************
711 //! Sets the level of the pin when locked
713 //! \param ulPin is one of the valid pin.
714 //! \param ucLevel is the level the pin drives when locked
716 //! This function sets the pin level when the pin is locked using
717 //! \sa PinLock() API.
719 //! By default all pins are set to drive 0.
721 //! \note Use case is to park the pins when entering LPDS
725 //*****************************************************************************
726 void PinLockLevelSet(unsigned long ulPin, unsigned char ucLevel)
731 // Supported only in ES2.00 and Later devices i.e. ROM Version 2.x.x or greater
733 if( (HWREG(0x00000400) & 0xFFFF) >= 2 )
736 // Get the corresponding Pad
738 ulPad = g_ulPinToPadMap[ulPin & 0x3F];
741 // Get the required bit
747 HWREG( OCP_SHARED_BASE + OCP_SHARED_O_SPARE_REG_6 ) |= ulPad;
751 HWREG( OCP_SHARED_BASE + OCP_SHARED_O_SPARE_REG_6 ) &= ~ulPad;
756 //*****************************************************************************
758 //! Locks all the pins to configured level(s).
760 //! \param ulOutEnable the bit-packed representation of pins to be set as output
762 //! This function locks all the pins to the pre-configure level. By default
763 //! the pins are set to drive 0. Default level can be changed using
764 //! \sa PinLockLevelSet() API.
766 //! The \e ulOutEnable paramter is bit-packed representation of pins that
767 //! are required to be enabled as output. If a bit is set 1, the corresponding
768 //! pin (as shown below) are set and locked as output.
770 //! |------|-----------------------------------------------|
771 //! | Bit |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|
772 //! |------|-----------------------------------------------|
773 //! | Pin |xx|xx|20|19|30|29|21|17|16|15|14|13|12|11|08|07|
774 //! |------|-----------------------------------------------|
776 //! |------|-----------------------------------------------|
777 //! | Bit |15|14|13|12|11|10|09|08|07|06|05|04|03|02|01|00|
778 //! |------|-----------------------------------------------|
779 //! | Pin |06|05|04|03|02|01|64|63|62|61|60|59|58|57|55|50|
780 //! |------|-----------------------------------------------|
783 //! \note Use case is to park the pins when entering LPDS
787 //*****************************************************************************
788 void PinLock(unsigned long ulOutEnable)
791 // Supported only in ES2.00 and Later devices i.e. ROM Version 2.x.x or greater
793 if( (HWREG(0x00000400) & 0xFFFF) >= 2 )
796 // Enable/disable the pin(s) output
798 HWREG( OCP_SHARED_BASE + OCP_SHARED_O_SPARE_REG_7 ) = ~ulOutEnable;
801 // Lock the pins to selected levels
803 HWREG( OCP_SHARED_BASE + OCP_SHARED_O_SPARE_REG_5 ) |= (3 << 24);
807 //*****************************************************************************
809 //! Unlocks all the pins.
811 //! This function unlocks all the pins and can be used for peripheral function.
813 //! By default all the pins are in unlocked state.
815 //! \note Use case is to un-park the pins when exiting LPDS
819 //*****************************************************************************
823 // Supported only in ES2.00 and Later devices i.e. ROM Version 2.x.x or greater
825 if( (HWREG(0x00000400) & 0xFFFF) >= 2 )
830 HWREG( OCP_SHARED_BASE + OCP_SHARED_O_SPARE_REG_5 ) &= ~(3 << 24);
834 //*****************************************************************************
836 // Gets pad number from pin number
838 // \param ulPin is a valid pin number
840 // This function return the pad corresponding to the specified pin
842 // \return Pad number on success, 0xFF otherwise
844 //*****************************************************************************
845 unsigned long PinToPadGet(unsigned long ulPin)
848 // Return the corresponding Pad
850 return g_ulPinToPadMap[ulPin & 0x3F];
854 //*****************************************************************************
856 // Gets pin number from pad number
858 // \param ulPad is a valid pad number
860 // This function return the pin corresponding to the specified pad
862 // \return Pin number on success, 0xFF otherwise
864 //*****************************************************************************
865 unsigned long PinFromPadGet(unsigned long ulPad)
870 // search and return the pin number
872 for(ulPin=0; ulPin < sizeof(g_ulPinToPadMap)/4; ulPin++)
874 if(g_ulPinToPadMap[ulPin] == ulPad)
883 //*****************************************************************************
885 // Close the Doxygen group.
888 //*****************************************************************************