summaryrefslogtreecommitdiff
path: root/bsp/nxp/lpc/lpc178x/drivers/lpc177x_8x_pinsel.c
blob: 062224decf33907280605ebce53467fc03005aa2 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**********************************************************************
* $Id$      lpc177x_8x_pinsel.c         2011-06-02
*//**
* @file     lpc177x_8x_pinsel.c
* @brief    Contains all functions support for Pin-connection block
*           firmware library on LPC177x_8x
* @version  1.0
* @date     02. June. 2011
* @author   NXP MCU SW Application Team
*
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* 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 "lpc177x_8x_pinsel.h"

#define PINSEL_I2C_MODE_POS             (8)
#define PINSEL_I2C_MODE_NUMBITS         (2)
#define PINSEL_I2C_MODE_BITMASK         (0x03)

#define PINSEL_BASIC_MODE_POS           (3)
#define PINSEL_BASIC_MODE_NUMBITS       (2)
#define PINSEL_BASIC_MODE_BITMASK       (0x03)

#define PINSEL_DACEN_POS                (16)
#define PINSEL_DACEN_BITMASK            (0x01)
#define PINSEL_DACEN_NUMBITS            (1)

#define PINSEL_GLITCH_FILTER_POS                (8)
#define PINSEL_GLITCH_FILTER_BITMASK            (0x01)
#define PINSEL_GLITCH_FILTER_NUMBITS            (1)

#define PINSEL_ADMODE_POS               (7)
#define PINSEL_ADMODE_BITMASK           (0x01)
#define PINSEL_ADMODE_NUMBITS           (1)

/* Private Functions ---------------------------------------------------------- */

/*********************************************************************//**
 * @brief       Get pointer to GPIO peripheral due to GPIO port
 * @param[in]   portnum     Port Number value, should be in range from 0..3.
 * @param[in]   pinnum      Pin number value, should be in range from 0..31
 * @return      Pointer to GPIO peripheral
 **********************************************************************/
static uint32_t * PIN_GetPointer(uint8_t portnum, uint8_t pinnum)
{
    uint32_t *pPIN = NULL;
    pPIN = (uint32_t *)(LPC_IOCON_BASE + ((portnum * 32 + pinnum)*sizeof(uint32_t)));
    return pPIN;
}

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

/*********************************************************************//**
 * @brief       Setup the pin selection function
 * @param[in]   portnum PORT number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   funcnum Function number, should be range: 0..7
 *              - 0: Select GPIO (Default)
 *              - 1: Selects the 1st alternate function
 *              - 2: Selects the 2nd alternate function
 *              ...
 *              - 7: Selects the 7th alternate function
 * @return      None
 **********************************************************************/
void PINSEL_ConfigPin ( uint8_t portnum, uint8_t pinnum, uint8_t funcnum)
{
    uint32_t *pPIN = NULL;
    pPIN = PIN_GetPointer(portnum, pinnum);
    *pPIN &= 0x00000007;//Clear function bits
    *pPIN |= funcnum;
}


/*********************************************************************//**
 * @brief       Setup resistor mode for each pin
 * @param[in]   portnum PORT number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   modenum: Mode number, should be in range: 0..3
                - IOCON_MODE_PLAIN: Plain output
                - IOCON_MODE_PULLDOWN: Pull-down enable
                - IOCON_MODE_PULLUP: Pull-up enable
                - IOCON_MODE_REPEATER: Repeater mode
 * @return      None
 **********************************************************************/
void PINSEL_SetPinMode ( uint8_t portnum, uint8_t pinnum, PinSel_BasicMode modenum)
{
    uint32_t *pPIN = NULL;
    pPIN = PIN_GetPointer(portnum, pinnum);
    *(uint32_t *)pPIN &= ~(3<<3);//Clear function bits
    *(uint32_t *)pPIN |= modenum;
}

/*********************************************************************//**
 * @brief       Setup hysteresis for each pin
 * @param[in]   portnum Port number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   NewState new state of Hysteresis mode, should be:
 *              - ENABLE: Hysteresis enable
 *              - DISABLE: Hysteresis disable
 * @return      None
 **********************************************************************/
void PINSEL_SetHysMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
{
    uint32_t *pPIN = NULL;
    pPIN = PIN_GetPointer(portnum, pinnum);
    if(NewState == DISABLE)
    {
        *(uint32_t *)pPIN &= ~IOCON_HYS;//Clear hys bits
    }
    else
        *(uint32_t *)pPIN |= IOCON_HYS;
}

/*********************************************************************//**
 * @brief       Setup Slew rate for each pin
 * @param[in]   portnum Port number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   NewState new state of Slew rate control, should be:
 *              - ENABLE: Output slew rate control is enable
 *              - DISABLE: Output slew rate control is disable
 * @return      None
 **********************************************************************/
void PINSEL_SetSlewMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
{
    uint32_t *pPIN = NULL;
    pPIN = PIN_GetPointer(portnum, pinnum);
    if(NewState == DISABLE)
    {
        *(uint32_t *)pPIN &= ~IOCON_SLEW;//Clear hys bits
    }
    else
        *(uint32_t *)pPIN |= IOCON_SLEW;
}

/*********************************************************************//**
 * @brief       Setup Input Buffer for each pin
 * @param[in]   portnum Port number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   NewState new state of Input buffer mode, should be:
 *              - ENABLE: The input buffer is enable
 *              - DISABLE: The input buffer is disable
 * @return      None
 **********************************************************************/
void PINSEL_SetInBufMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
{
    uint32_t *pPIN = NULL;
    pPIN = PIN_GetPointer(portnum, pinnum);
    if(NewState == DISABLE)
    {
        *(uint32_t *)pPIN &= ~IOCON_INBUF;//Clear hys bits
    }
    else
        *(uint32_t *)pPIN |= IOCON_INBUF;
}

/*********************************************************************//**
 * @brief       Setup I2CMode for only pins that provide special I2C functionality
 * @param[in]   portnum Port number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   I2CMode I2C mode, should be:
 *              - IOCON_I2CMODE_FAST: Fast mode and standard I2C mode
 *              - IOCON_I2CMODE_OPENDRAIN: Open drain I/O
 *              - IOCON_I2CMODE_FASTPLUS: Fast Mode Plus I/O
 *              - IOCON_I2CMODE_HIGHOPENDRAIN: High drive open drain I/O
 * @return      None
 **********************************************************************/
void PINSEL_SetI2CMode(uint8_t portnum, uint8_t pinnum, PinSel_I2cMode I2CMode)
{
    uint32_t *pPIN = NULL;
    pPIN = PIN_GetPointer(portnum, pinnum);

    *(uint32_t *)pPIN &= ~(PINSEL_I2C_MODE_BITMASK<< PINSEL_I2C_MODE_POS);
    *(uint32_t *)pPIN |= (I2CMode << PINSEL_I2C_MODE_POS);
}

/*********************************************************************//**
 * @brief       Setup Open-drain mode in each pin
 * @param[in]   portnum Port number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   NewState new state of Open-drain mode:
 *              - DISABLE: Normal pin I/O mode
 *              - ENABLE: Open-drain enable
 * @return      None
 **********************************************************************/
void PINSEL_SetOpenDrainMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
{
    uint32_t *pPIN = NULL;
    pPIN = PIN_GetPointer(portnum, pinnum);
    if(NewState == DISABLE)
    {
        *(uint32_t *)pPIN &= ~IOCON_ODMODE;//Clear hys bits
    }
    else
    {
        *(uint32_t *)pPIN |= IOCON_ODMODE;
    }
}

/*********************************************************************//**
 * @brief       Enable the Analog mode for each pin (default is as Digital pins)
 * @param[in]   portnum PORT number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   enable: the state of the pin that is expected to run
                - ENABLE: Enable the DAC mode of the pin
                - DISABLE: Disable the DAC mode
 * @return      None
 **********************************************************************/
void PINSEL_SetAnalogPinMode (uint8_t portnum, uint8_t pinnum, uint8_t enable)
{
    uint32_t *pPIN = NULL;

    uint8_t condition = 0;

    condition = ((portnum == 0) && (pinnum == 12)) || ((portnum == 0) && (pinnum == 13))
                    | ((portnum == 0) && (pinnum <= 26) && (pinnum >= 23))
                    | ((portnum == 1) && (pinnum == 30)) || ((portnum == 1) && (pinnum == 31));

    if(!condition)
    {
        return;
    }

    pPIN = PIN_GetPointer(portnum, pinnum);

    //Clear this bit to set the pin to Analog mode
    *(uint32_t *)pPIN &= ~(PINSEL_ADMODE_BITMASK << PINSEL_ADMODE_POS);

    if(enable)
    {

    }
    else
    {
        *(uint32_t *)pPIN |= (1 << PINSEL_ADMODE_POS);//Set 16th bit to one
    }

    return;
}



/*********************************************************************//**
 * @brief       Choose the DAC mode for each pin
 * @param[in]   portnum PORT number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   enable: the state of the pin that is expected to run
                - ENABLE: Enable the DAC mode of the pin
                - DISABLE: Disable the DAC mode
 * @return      None
 **********************************************************************/
void PINSEL_DacEnable (uint8_t portnum, uint8_t pinnum, uint8_t enable)
{
    uint32_t *pPIN = NULL;

    // This setting is only for DAC pin (output pin)
    if(!((portnum == 0) && (pinnum == 26)))
    {
        return;
    }

    pPIN = PIN_GetPointer(portnum, pinnum);

    //Clear DAC Enable function bits
    *(uint32_t *)pPIN &= ~(PINSEL_DACEN_BITMASK << PINSEL_DACEN_POS);

    if(enable)
    {
        *(uint32_t *)pPIN |= (1 << PINSEL_DACEN_POS);//Set 16th bit to one
    }
    else
    {

    }

    return;
}

/*********************************************************************//**
 * @brief       Control the glitch filter for each pin
 * @param[in]   portnum PORT number, should be in range: 0..3
 * @param[in]   pinnum  Pin number, should be in range: 0..31
 * @param[in]   enable: the state of the pin that is expected to run
                - ENABLE: The noise pulses below approximately 10ns are filtered out
                - DISABLE: No input filtering is done.
 * @return      None
 **********************************************************************/
void PINSEL_SetFilter (uint8_t portnum, uint8_t pinnum, uint8_t enable)
{
    uint32_t *pPIN = NULL;

    // This setting is only for DAC pin (output pin)
    if(!((portnum == 0) && ((pinnum == 7) || (pinnum == 8) || (pinnum == 9))))
    {
        return;
    }

    pPIN = PIN_GetPointer(portnum, pinnum);

    *(uint32_t *)pPIN |= (1 << 7);//Set 7th bit for normal operation following the UM1.0

    //Clear Filter bits
    *(uint32_t *)pPIN &= ~(PINSEL_GLITCH_FILTER_BITMASK << PINSEL_GLITCH_FILTER_POS);

    if(!enable)
    {
        *(uint32_t *)pPIN |= (1 << PINSEL_GLITCH_FILTER_POS);//Set 8th bit to one
    }
    else
    {

    }

    *pPIN = *pPIN;

    return;
}

/**
 * @}
 */

/**
 * @}
 */

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