lpc-field

Template project for programming NXP's LPC1768 MCUs
git clone git://git.mdnr.space/lpc-field
Log | Files | Refs | README | LICENSE

lpc17xx_pinsel.c (10696B)


      1 /**********************************************************************
      2 * $Id$		lpc17xx_pinsel.c				2010-05-21
      3 *//**
      4 * @file		lpc17xx_pinsel.c
      5 * @brief	Contains all functions support for Pin connect block firmware
      6 * 			library on LPC17xx
      7 * @version	2.0
      8 * @date		21. May. 2010
      9 * @author	NXP MCU SW Application Team
     10 *
     11 * Copyright(C) 2010, NXP Semiconductor
     12 * All rights reserved.
     13 *
     14 ***********************************************************************
     15 * Software that is described herein is for illustrative purposes only
     16 * which provides customers with programming information regarding the
     17 * products. This software is supplied "AS IS" without any warranties.
     18 * NXP Semiconductors assumes no responsibility or liability for the
     19 * use of the software, conveys no license or title under any patent,
     20 * copyright, or mask work right to the product. NXP Semiconductors
     21 * reserves the right to make changes in the software without
     22 * notification. NXP Semiconductors also make no representation or
     23 * warranty that such application will be suitable for the specified
     24 * use without further testing or modification.
     25 * Permission to use, copy, modify, and distribute this software and its
     26 * documentation is hereby granted, under NXP Semiconductors'
     27 * relevant copyright in the software, without fee, provided that it
     28 * is used in conjunction with NXP Semiconductors microcontrollers.  This
     29 * copyright, permission, and disclaimer notice must appear in all copies of
     30 * this code.
     31 **********************************************************************/
     32 
     33 /* Peripheral group ----------------------------------------------------------- */
     34 /** @addtogroup PINSEL
     35  * @{
     36  */
     37 
     38 /* Includes ------------------------------------------------------------------- */
     39 #include "lpc17xx_pinsel.h"
     40 
     41 /* Public Functions ----------------------------------------------------------- */
     42 
     43 static void set_PinFunc ( uint8_t portnum, uint8_t pinnum, uint8_t funcnum);
     44 static void set_ResistorMode ( uint8_t portnum, uint8_t pinnum, uint8_t modenum);
     45 static void set_OpenDrainMode( uint8_t portnum, uint8_t pinnum, uint8_t modenum);
     46 
     47 /*********************************************************************//**
     48  * @brief 		Setup the pin selection function
     49  * @param[in]	portnum PORT number,
     50  * 				should be one of the following:
     51  * 				- PINSEL_PORT_0	: Port 0
     52  * 				- PINSEL_PORT_1	: Port 1
     53  * 				- PINSEL_PORT_2	: Port 2
     54  * 				- PINSEL_PORT_3	: Port 3
     55  *
     56  * @param[in]	pinnum	Pin number,
     57  * 				should be one of the following:
     58 				- PINSEL_PIN_0 : Pin 0
     59 				- PINSEL_PIN_1 : Pin 1
     60 				- PINSEL_PIN_2 : Pin 2
     61 				- PINSEL_PIN_3 : Pin 3
     62 				- PINSEL_PIN_4 : Pin 4
     63 				- PINSEL_PIN_5 : Pin 5
     64 				- PINSEL_PIN_6 : Pin 6
     65 				- PINSEL_PIN_7 : Pin 7
     66 				- PINSEL_PIN_8 : Pin 8
     67 				- PINSEL_PIN_9 : Pin 9
     68 				- PINSEL_PIN_10 : Pin 10
     69 				- PINSEL_PIN_11 : Pin 11
     70 				- PINSEL_PIN_12 : Pin 12
     71 				- PINSEL_PIN_13 : Pin 13
     72 				- PINSEL_PIN_14 : Pin 14
     73 				- PINSEL_PIN_15 : Pin 15
     74 				- PINSEL_PIN_16 : Pin 16
     75 				- PINSEL_PIN_17 : Pin 17
     76 				- PINSEL_PIN_18 : Pin 18
     77 				- PINSEL_PIN_19 : Pin 19
     78 				- PINSEL_PIN_20 : Pin 20
     79 				- PINSEL_PIN_21 : Pin 21
     80 				- PINSEL_PIN_22 : Pin 22
     81 				- PINSEL_PIN_23 : Pin 23
     82 				- PINSEL_PIN_24 : Pin 24
     83 				- PINSEL_PIN_25 : Pin 25
     84 				- PINSEL_PIN_26 : Pin 26
     85 				- PINSEL_PIN_27 : Pin 27
     86 				- PINSEL_PIN_28 : Pin 28
     87 				- PINSEL_PIN_29 : Pin 29
     88 				- PINSEL_PIN_30 : Pin 30
     89 				- PINSEL_PIN_31 : Pin 31
     90 
     91  * @param[in] 	funcnum Function number,
     92  * 				should be one of the following:
     93  *				- PINSEL_FUNC_0 : default function
     94  *				- PINSEL_FUNC_1 : first alternate function
     95  *				- PINSEL_FUNC_2 : second alternate function
     96  *				- PINSEL_FUNC_3 : third alternate function
     97  *
     98  * @return 		None
     99  **********************************************************************/
    100 static void set_PinFunc ( uint8_t portnum, uint8_t pinnum, uint8_t funcnum)
    101 {
    102 	uint32_t pinnum_t = pinnum;
    103 	uint32_t pinselreg_idx = 2 * portnum;
    104 	uint32_t *pPinCon = (uint32_t *)&LPC_PINCON->PINSEL0;
    105 
    106 	if (pinnum_t >= 16) {
    107 		pinnum_t -= 16;
    108 		pinselreg_idx++;
    109 	}
    110 	*(uint32_t *)(pPinCon + pinselreg_idx) &= ~(0x03UL << (pinnum_t * 2));
    111 	*(uint32_t *)(pPinCon + pinselreg_idx) |= ((uint32_t)funcnum) << (pinnum_t * 2);
    112 }
    113 
    114 /*********************************************************************//**
    115  * @brief 		Setup resistor mode for each pin
    116  * @param[in]	portnum PORT number,
    117  * 				should be one of the following:
    118  * 				- PINSEL_PORT_0	: Port 0
    119  * 				- PINSEL_PORT_1	: Port 1
    120  * 				- PINSEL_PORT_2	: Port 2
    121  * 				- PINSEL_PORT_3	: Port 3
    122  * @param[in]	pinnum	Pin number,
    123  * 				should be one of the following:
    124 				- PINSEL_PIN_0 : Pin 0
    125 				- PINSEL_PIN_1 : Pin 1
    126 				- PINSEL_PIN_2 : Pin 2
    127 				- PINSEL_PIN_3 : Pin 3
    128 				- PINSEL_PIN_4 : Pin 4
    129 				- PINSEL_PIN_5 : Pin 5
    130 				- PINSEL_PIN_6 : Pin 6
    131 				- PINSEL_PIN_7 : Pin 7
    132 				- PINSEL_PIN_8 : Pin 8
    133 				- PINSEL_PIN_9 : Pin 9
    134 				- PINSEL_PIN_10 : Pin 10
    135 				- PINSEL_PIN_11 : Pin 11
    136 				- PINSEL_PIN_12 : Pin 12
    137 				- PINSEL_PIN_13 : Pin 13
    138 				- PINSEL_PIN_14 : Pin 14
    139 				- PINSEL_PIN_15 : Pin 15
    140 				- PINSEL_PIN_16 : Pin 16
    141 				- PINSEL_PIN_17 : Pin 17
    142 				- PINSEL_PIN_18 : Pin 18
    143 				- PINSEL_PIN_19 : Pin 19
    144 				- PINSEL_PIN_20 : Pin 20
    145 				- PINSEL_PIN_21 : Pin 21
    146 				- PINSEL_PIN_22 : Pin 22
    147 				- PINSEL_PIN_23 : Pin 23
    148 				- PINSEL_PIN_24 : Pin 24
    149 				- PINSEL_PIN_25 : Pin 25
    150 				- PINSEL_PIN_26 : Pin 26
    151 				- PINSEL_PIN_27 : Pin 27
    152 				- PINSEL_PIN_28 : Pin 28
    153 				- PINSEL_PIN_29 : Pin 29
    154 				- PINSEL_PIN_30 : Pin 30
    155 				- PINSEL_PIN_31 : Pin 31
    156 
    157  * @param[in] 	modenum: Mode number,
    158  * 				should be one of the following:
    159 				- PINSEL_PINMODE_PULLUP	: Internal pull-up resistor
    160 				- PINSEL_PINMODE_TRISTATE : Tri-state
    161 				- PINSEL_PINMODE_PULLDOWN : Internal pull-down resistor
    162 
    163  * @return 		None
    164  **********************************************************************/
    165 void set_ResistorMode ( uint8_t portnum, uint8_t pinnum, uint8_t modenum)
    166 {
    167 	uint32_t pinnum_t = pinnum;
    168 	uint32_t pinmodereg_idx = 2 * portnum;
    169 	uint32_t *pPinCon = (uint32_t *)&LPC_PINCON->PINMODE0;
    170 
    171 	if (pinnum_t >= 16) {
    172 		pinnum_t -= 16;
    173 		pinmodereg_idx++ ;
    174 	}
    175 
    176 	*(uint32_t *)(pPinCon + pinmodereg_idx) &= ~(0x03UL << (pinnum_t * 2));
    177 	*(uint32_t *)(pPinCon + pinmodereg_idx) |= ((uint32_t)modenum) << (pinnum_t * 2);
    178 }
    179 
    180 /*********************************************************************//**
    181  * @brief 		Setup Open drain mode for each pin
    182  * @param[in]	portnum PORT number,
    183  * 				should be one of the following:
    184  * 				- PINSEL_PORT_0	: Port 0
    185  * 				- PINSEL_PORT_1	: Port 1
    186  * 				- PINSEL_PORT_2	: Port 2
    187  * 				- PINSEL_PORT_3	: Port 3
    188  *
    189  * @param[in]	pinnum	Pin number,
    190  * 				should be one of the following:
    191 				- PINSEL_PIN_0 : Pin 0
    192 				- PINSEL_PIN_1 : Pin 1
    193 				- PINSEL_PIN_2 : Pin 2
    194 				- PINSEL_PIN_3 : Pin 3
    195 				- PINSEL_PIN_4 : Pin 4
    196 				- PINSEL_PIN_5 : Pin 5
    197 				- PINSEL_PIN_6 : Pin 6
    198 				- PINSEL_PIN_7 : Pin 7
    199 				- PINSEL_PIN_8 : Pin 8
    200 				- PINSEL_PIN_9 : Pin 9
    201 				- PINSEL_PIN_10 : Pin 10
    202 				- PINSEL_PIN_11 : Pin 11
    203 				- PINSEL_PIN_12 : Pin 12
    204 				- PINSEL_PIN_13 : Pin 13
    205 				- PINSEL_PIN_14 : Pin 14
    206 				- PINSEL_PIN_15 : Pin 15
    207 				- PINSEL_PIN_16 : Pin 16
    208 				- PINSEL_PIN_17 : Pin 17
    209 				- PINSEL_PIN_18 : Pin 18
    210 				- PINSEL_PIN_19 : Pin 19
    211 				- PINSEL_PIN_20 : Pin 20
    212 				- PINSEL_PIN_21 : Pin 21
    213 				- PINSEL_PIN_22 : Pin 22
    214 				- PINSEL_PIN_23 : Pin 23
    215 				- PINSEL_PIN_24 : Pin 24
    216 				- PINSEL_PIN_25 : Pin 25
    217 				- PINSEL_PIN_26 : Pin 26
    218 				- PINSEL_PIN_27 : Pin 27
    219 				- PINSEL_PIN_28 : Pin 28
    220 				- PINSEL_PIN_29 : Pin 29
    221 				- PINSEL_PIN_30 : Pin 30
    222 				- PINSEL_PIN_31 : Pin 31
    223 
    224  * @param[in]	modenum  Open drain mode number,
    225  * 				should be one of the following:
    226  * 				- PINSEL_PINMODE_NORMAL : Pin is in the normal (not open drain) mode
    227  * 				- PINSEL_PINMODE_OPENDRAIN : Pin is in the open drain mode
    228  *
    229  * @return 		None
    230  **********************************************************************/
    231 void set_OpenDrainMode( uint8_t portnum, uint8_t pinnum, uint8_t modenum)
    232 {
    233 	uint32_t *pPinCon = (uint32_t *)&LPC_PINCON->PINMODE_OD0;
    234 
    235 	if (modenum == PINSEL_PINMODE_OPENDRAIN){
    236 		*(uint32_t *)(pPinCon + portnum) |= (0x01UL << pinnum);
    237 	} else {
    238 		*(uint32_t *)(pPinCon + portnum) &= ~(0x01UL << pinnum);
    239 	}
    240 }
    241 
    242 /* End of Public Functions ---------------------------------------------------- */
    243 
    244 /* Public Functions ----------------------------------------------------------- */
    245 /** @addtogroup PINSEL_Public_Functions
    246  * @{
    247  */
    248 /*********************************************************************//**
    249  * @brief 		Configure trace function
    250  * @param[in] 	NewState State of the Trace function configuration,
    251  * 				should be one of the following:
    252  * 				- ENABLE : Enable Trace Function
    253  * 				- DISABLE : Disable Trace Function
    254  *
    255  * @return 		None
    256  **********************************************************************/
    257 void PINSEL_ConfigTraceFunc(FunctionalState NewState)
    258 {
    259 	if (NewState == ENABLE) {
    260 		LPC_PINCON->PINSEL10 |= (0x01UL << 3);
    261 	} else if (NewState == DISABLE) {
    262 		LPC_PINCON->PINSEL10 &= ~(0x01UL << 3);
    263 	}
    264 }
    265 
    266 /*********************************************************************//**
    267  * @brief 		Setup I2C0 pins
    268  * @param[in]	i2cPinMode I2C pin mode,
    269  * 				should be one of the following:
    270  * 				- PINSEL_I2C_Normal_Mode : The standard drive mode
    271  * 				- PINSEL_I2C_Fast_Mode : Fast Mode Plus drive mode
    272  *
    273  * @param[in]	filterSlewRateEnable  should be:
    274  * 				- ENABLE: Enable filter and slew rate.
    275  * 				- DISABLE: Disable filter and slew rate.
    276  *
    277  * @return 		None
    278  **********************************************************************/
    279 void PINSEL_SetI2C0Pins(uint8_t i2cPinMode, FunctionalState filterSlewRateEnable)
    280 {
    281 	uint32_t regVal;
    282 
    283 	if (i2cPinMode == PINSEL_I2C_Fast_Mode){
    284 		regVal = PINSEL_I2CPADCFG_SCLDRV0 | PINSEL_I2CPADCFG_SDADRV0;
    285 	}
    286 
    287 	if (filterSlewRateEnable == DISABLE){
    288 		regVal = PINSEL_I2CPADCFG_SCLI2C0 | PINSEL_I2CPADCFG_SDAI2C0;
    289 	}
    290 	LPC_PINCON->I2CPADCFG = regVal;
    291 }
    292 
    293 
    294 /*********************************************************************//**
    295  * @brief 		Configure Pin corresponding to specified parameters passed
    296  * 				in the PinCfg
    297  * @param[in]	PinCfg	Pointer to a PINSEL_CFG_Type structure
    298  *                    that contains the configuration information for the
    299  *                    specified pin.
    300  * @return 		None
    301  **********************************************************************/
    302 void PINSEL_ConfigPin(PINSEL_CFG_Type *PinCfg)
    303 {
    304 	set_PinFunc(PinCfg->Portnum, PinCfg->Pinnum, PinCfg->Funcnum);
    305 	set_ResistorMode(PinCfg->Portnum, PinCfg->Pinnum, PinCfg->Pinmode);
    306 	set_OpenDrainMode(PinCfg->Portnum, PinCfg->Pinnum, PinCfg->OpenDrain);
    307 }
    308 
    309 
    310 /**
    311  * @}
    312  */
    313 
    314 /**
    315  * @}
    316  */
    317 
    318 /* --------------------------------- End Of File ------------------------------ */