summaryrefslogtreecommitdiff
path: root/firmware/lpc17xx/lpc17xx_pinsel.c
blob: c459217786885c62e92779b5226608e9bda5481c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 * @file	: lpc17xx_pinsel.c
 * @brief	: Contains all functions support for Pin connect block firmware
 * 				library on LPC17xx
 * @version	: 1.0
 * @date	: 25. Feb. 2009
 * @author	: HoanTran
 **************************************************************************
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * products. This software is supplied "AS IS" without any warranties.
 * NXP Semiconductors assumes no responsibility or liability for the
 * use of the software, conveys no license or title under any patent,
 * copyright, or mask work right to the product. NXP Semiconductors
 * reserves the right to make changes in the software without
 * notification. NXP Semiconductors also make no representation or
 * warranty that such application will be suitable for the specified
 * use without further testing or modification.
 **********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup PINSEL
 * @{
 */

/* Includes ------------------------------------------------------------------- */
#include "lpc17xx_pinsel.h"


/* Public Functions ----------------------------------------------------------- */
/** @addtogroup PINSEL_Public_Functions
 * @{
 */

/*********************************************************************//**
 * @brief 		Setup the pin selection function
 * @param[in]	portnum PORT number,
 * 				should be one of the following:
 * 				- PINSEL_PORT_0	: Port 0
 * 				- PINSEL_PORT_1	: Port 1
 * 				- PINSEL_PORT_2	: Port 2
 * 				- PINSEL_PORT_3	: Port 3
 *
 * @param[in]	pinnum	Pin number,
 * 				should be one of the following:
				- PINSEL_PIN_0 : Pin 0
				- PINSEL_PIN_1 : Pin 1
				- PINSEL_PIN_2 : Pin 2
				- PINSEL_PIN_3 : Pin 3
				- PINSEL_PIN_4 : Pin 4
				- PINSEL_PIN_5 : Pin 5
				- PINSEL_PIN_6 : Pin 6
				- PINSEL_PIN_7 : Pin 7
				- PINSEL_PIN_8 : Pin 8
				- PINSEL_PIN_9 : Pin 9
				- PINSEL_PIN_10 : Pin 10
				- PINSEL_PIN_11 : Pin 11
				- PINSEL_PIN_12 : Pin 12
				- PINSEL_PIN_13 : Pin 13
				- PINSEL_PIN_14 : Pin 14
				- PINSEL_PIN_15 : Pin 15
				- PINSEL_PIN_16 : Pin 16
				- PINSEL_PIN_17 : Pin 17
				- PINSEL_PIN_18 : Pin 18
				- PINSEL_PIN_19 : Pin 19
				- PINSEL_PIN_20 : Pin 20
				- PINSEL_PIN_21 : Pin 21
				- PINSEL_PIN_22 : Pin 22
				- PINSEL_PIN_23 : Pin 23
				- PINSEL_PIN_24 : Pin 24
				- PINSEL_PIN_25 : Pin 25
				- PINSEL_PIN_26 : Pin 26
				- PINSEL_PIN_27 : Pin 27
				- PINSEL_PIN_28 : Pin 28
				- PINSEL_PIN_29 : Pin 29
				- PINSEL_PIN_30 : Pin 30
				- PINSEL_PIN_31 : Pin 31

 * @param[in] 	funcnum Function number,
 * 				should be one of the following:
 *				- PINSEL_FUNC_0 : default function
 *				- PINSEL_FUNC_1 : first alternate function
 *				- PINSEL_FUNC_2 : second alternate function
 *				- PINSEL_FUNC_3 : third alternate function
 *
 * @return 		None
 **********************************************************************/
void PINSEL_SetPinFunc ( uint8_t portnum, uint8_t pinnum, uint8_t funcnum)
{
	uint32_t pinnum_t = pinnum;
	uint32_t pinselreg_idx = 2 * portnum;
	uint32_t *pPinCon = (uint32_t *)&LPC_PINCON->PINSEL0;

	if (pinnum_t >= 16) {
		pinnum_t -= 16;
		pinselreg_idx++;
	}
	*(uint32_t *)(pPinCon + pinselreg_idx) &= ~(0x03UL << (pinnum_t * 2));
	*(uint32_t *)(pPinCon + pinselreg_idx) |= ((uint32_t)funcnum) << (pinnum_t * 2);
}



/*********************************************************************//**
 * @brief 		Configure trace function
 * @param[in] 	NewState State of the Trace function configuration,
 * 				should be one of the following:
 * 				- ENABLE : Enable Trace Function
 * 				- DISABLE : Disable Trace Function
 *
 * @return 		None
 **********************************************************************/
void PINSEL_ConfigTraceFunc(FunctionalState NewState)
{
	if (NewState == ENABLE) {
		LPC_PINCON->PINSEL10 |= (0x01UL << 3);
	} else if (NewState == DISABLE) {
		LPC_PINCON->PINSEL10 &= ~(0x01UL << 3);
	}
}



/*********************************************************************//**
 * @brief 		Setup resistor mode for each pin
 * @param[in]	portnum PORT number,
 * 				should be one of the following:
 * 				- PINSEL_PORT_0	: Port 0
 * 				- PINSEL_PORT_1	: Port 1
 * 				- PINSEL_PORT_2	: Port 2
 * 				- PINSEL_PORT_3	: Port 3
 * @param[in]	pinnum	Pin number,
 * 				should be one of the following:
				- PINSEL_PIN_0 : Pin 0
				- PINSEL_PIN_1 : Pin 1
				- PINSEL_PIN_2 : Pin 2
				- PINSEL_PIN_3 : Pin 3
				- PINSEL_PIN_4 : Pin 4
				- PINSEL_PIN_5 : Pin 5
				- PINSEL_PIN_6 : Pin 6
				- PINSEL_PIN_7 : Pin 7
				- PINSEL_PIN_8 : Pin 8
				- PINSEL_PIN_9 : Pin 9
				- PINSEL_PIN_10 : Pin 10
				- PINSEL_PIN_11 : Pin 11
				- PINSEL_PIN_12 : Pin 12
				- PINSEL_PIN_13 : Pin 13
				- PINSEL_PIN_14 : Pin 14
				- PINSEL_PIN_15 : Pin 15
				- PINSEL_PIN_16 : Pin 16
				- PINSEL_PIN_17 : Pin 17
				- PINSEL_PIN_18 : Pin 18
				- PINSEL_PIN_19 : Pin 19
				- PINSEL_PIN_20 : Pin 20
				- PINSEL_PIN_21 : Pin 21
				- PINSEL_PIN_22 : Pin 22
				- PINSEL_PIN_23 : Pin 23
				- PINSEL_PIN_24 : Pin 24
				- PINSEL_PIN_25 : Pin 25
				- PINSEL_PIN_26 : Pin 26
				- PINSEL_PIN_27 : Pin 27
				- PINSEL_PIN_28 : Pin 28
				- PINSEL_PIN_29 : Pin 29
				- PINSEL_PIN_30 : Pin 30
				- PINSEL_PIN_31 : Pin 31

 * @param[in] 	modenum: Mode number,
 * 				should be one of the following:
				- PINSEL_PINMODE_PULLUP	: Internal pull-up resistor
				- PINSEL_PINMODE_TRISTATE : Tri-state
				- PINSEL_PINMODE_PULLDOWN : Internal pull-down resistor

 * @return 		None
 **********************************************************************/
void PINSEL_SetResistorMode ( uint8_t portnum, uint8_t pinnum, uint8_t modenum)
{
	uint32_t pinnum_t = pinnum;
	uint32_t pinmodereg_idx = 2 * portnum;
	uint32_t *pPinCon = (uint32_t *)&LPC_PINCON->PINMODE0;

	if (pinnum_t >= 16) {
		pinnum_t -= 16;
		pinmodereg_idx++ ;
	}

	*(uint32_t *)(pPinCon + pinmodereg_idx) &= ~(0x03UL << (pinnum_t * 2));
	*(uint32_t *)(pPinCon + pinmodereg_idx) |= ((uint32_t)modenum) << (pinnum_t * 2);
}



/*********************************************************************//**
 * @brief 		Setup Open drain mode for each pin
 * @param[in]	portnum PORT number,
 * 				should be one of the following:
 * 				- PINSEL_PORT_0	: Port 0
 * 				- PINSEL_PORT_1	: Port 1
 * 				- PINSEL_PORT_2	: Port 2
 * 				- PINSEL_PORT_3	: Port 3
 *
 * @param[in]	pinnum	Pin number,
 * 				should be one of the following:
				- PINSEL_PIN_0 : Pin 0
				- PINSEL_PIN_1 : Pin 1
				- PINSEL_PIN_2 : Pin 2
				- PINSEL_PIN_3 : Pin 3
				- PINSEL_PIN_4 : Pin 4
				- PINSEL_PIN_5 : Pin 5
				- PINSEL_PIN_6 : Pin 6
				- PINSEL_PIN_7 : Pin 7
				- PINSEL_PIN_8 : Pin 8
				- PINSEL_PIN_9 : Pin 9
				- PINSEL_PIN_10 : Pin 10
				- PINSEL_PIN_11 : Pin 11
				- PINSEL_PIN_12 : Pin 12
				- PINSEL_PIN_13 : Pin 13
				- PINSEL_PIN_14 : Pin 14
				- PINSEL_PIN_15 : Pin 15
				- PINSEL_PIN_16 : Pin 16
				- PINSEL_PIN_17 : Pin 17
				- PINSEL_PIN_18 : Pin 18
				- PINSEL_PIN_19 : Pin 19
				- PINSEL_PIN_20 : Pin 20
				- PINSEL_PIN_21 : Pin 21
				- PINSEL_PIN_22 : Pin 22
				- PINSEL_PIN_23 : Pin 23
				- PINSEL_PIN_24 : Pin 24
				- PINSEL_PIN_25 : Pin 25
				- PINSEL_PIN_26 : Pin 26
				- PINSEL_PIN_27 : Pin 27
				- PINSEL_PIN_28 : Pin 28
				- PINSEL_PIN_29 : Pin 29
				- PINSEL_PIN_30 : Pin 30
				- PINSEL_PIN_31 : Pin 31

 * @param[in]	modenum  Open drain mode number,
 * 				should be one of the following:
 * 				- PINSEL_PINMODE_NORMAL : Pin is in the normal (not open drain) mode
 * 				- PINSEL_PINMODE_OPENDRAIN : Pin is in the open drain mode
 *
 * @return 		None
 **********************************************************************/
void PINSEL_SetOpenDrainMode( uint8_t portnum, uint8_t pinnum, uint8_t modenum)
{
	uint32_t *pPinCon = (uint32_t *)&LPC_PINCON->PINMODE_OD0;

	if (modenum == PINSEL_PINMODE_OPENDRAIN){
		*(uint32_t *)(pPinCon + portnum) |= (0x01UL << pinnum);
	} else {
		*(uint32_t *)(pPinCon + portnum) &= ~(0x01UL << pinnum);
	}
}



/*********************************************************************//**
 * @brief 		Setup I2C0 pins
 * @param[in]	i2cPinMode I2C pin mode,
 * 				should be one of the following:
 * 				- PINSEL_I2C_Normal_Mode : The standard drive mode
 * 				- PINSEL_I2C_Fast_Mode : Fast Mode Plus drive mode
 *
 * @param[in]	filterSlewRateEnable  should be:
 * 				- ENABLE: Enable filter and slew rate.
 * 				- DISABLE: Disable filter and slew rate.
 *
 * @return 		None
 **********************************************************************/
void PINSEL_SetI2C0Pins(uint8_t i2cPinMode, FunctionalState filterSlewRateEnable)
{
	uint32_t regVal;

	if (i2cPinMode == PINSEL_I2C_Fast_Mode){
		regVal = PINSEL_I2CPADCFG_SCLDRV0 | PINSEL_I2CPADCFG_SDADRV0;
	}

	if (filterSlewRateEnable == DISABLE){
		regVal = PINSEL_I2CPADCFG_SCLI2C0 | PINSEL_I2CPADCFG_SDAI2C0;
	}
	LPC_PINCON->I2CPADCFG = regVal;
}


/*********************************************************************//**
 * @brief 		Configure Pin corresponding to specified parameters passed
 * 				in the PinCfg
 * @param[in]	PinCfg	Pointer to a PINSEL_CFG_Type structure
 *                    that contains the configuration information for the
 *                    specified pin.
 * @return 		None
 **********************************************************************/
void PINSEL_ConfigPin(PINSEL_CFG_Type *PinCfg)
{
	PINSEL_SetPinFunc(PinCfg->Portnum, PinCfg->Pinnum, PinCfg->Funcnum);
	PINSEL_SetResistorMode(PinCfg->Portnum, PinCfg->Pinnum, PinCfg->Pinmode);
	PINSEL_SetOpenDrainMode(PinCfg->Portnum, PinCfg->Pinnum, PinCfg->OpenDrain);
}


/**
 * @}
 */

/**
 * @}
 */

/* --------------------------------- End Of File ------------------------------ */