1 /**********************************************************************
2 * $Id$      lpc177x_8x_pinsel.c         2011-06-02
3 *//**
4 * @file     lpc177x_8x_pinsel.c
5 * @brief    Contains all functions support for Pin-connection block
6 *           firmware library on LPC177x_8x
7 * @version  1.0
8 * @date     02. June. 2011
9 * @author   NXP MCU SW Application Team
10 *
11 * Copyright(C) 2011, 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 **********************************************************************/
26 
27 /* Peripheral group ----------------------------------------------------------- */
28 /** @addtogroup PINSEL
29  * @{
30  */
31 
32 /* Includes ------------------------------------------------------------------- */
33 #include "lpc177x_8x_pinsel.h"
34 
35 #define PINSEL_I2C_MODE_POS             (8)
36 #define PINSEL_I2C_MODE_NUMBITS         (2)
37 #define PINSEL_I2C_MODE_BITMASK         (0x03)
38 
39 #define PINSEL_BASIC_MODE_POS           (3)
40 #define PINSEL_BASIC_MODE_NUMBITS       (2)
41 #define PINSEL_BASIC_MODE_BITMASK       (0x03)
42 
43 #define PINSEL_DACEN_POS                (16)
44 #define PINSEL_DACEN_BITMASK            (0x01)
45 #define PINSEL_DACEN_NUMBITS            (1)
46 
47 #define PINSEL_GLITCH_FILTER_POS                (8)
48 #define PINSEL_GLITCH_FILTER_BITMASK            (0x01)
49 #define PINSEL_GLITCH_FILTER_NUMBITS            (1)
50 
51 #define PINSEL_ADMODE_POS               (7)
52 #define PINSEL_ADMODE_BITMASK           (0x01)
53 #define PINSEL_ADMODE_NUMBITS           (1)
54 
55 /* Private Functions ---------------------------------------------------------- */
56 
57 /*********************************************************************//**
58  * @brief       Get pointer to GPIO peripheral due to GPIO port
59  * @param[in]   portnum     Port Number value, should be in range from 0..3.
60  * @param[in]   pinnum      Pin number value, should be in range from 0..31
61  * @return      Pointer to GPIO peripheral
62  **********************************************************************/
PIN_GetPointer(uint8_t portnum,uint8_t pinnum)63 static uint32_t * PIN_GetPointer(uint8_t portnum, uint8_t pinnum)
64 {
65     uint32_t *pPIN = NULL;
66     pPIN = (uint32_t *)(LPC_IOCON_BASE + ((portnum * 32 + pinnum)*sizeof(uint32_t)));
67     return pPIN;
68 }
69 
70 /* Public Functions ----------------------------------------------------------- */
71 /** @addtogroup PINSEL_Public_Functions
72  * @{
73  */
74 
75 /*********************************************************************//**
76  * @brief       Setup the pin selection function
77  * @param[in]   portnum PORT number, should be in range: 0..3
78  * @param[in]   pinnum  Pin number, should be in range: 0..31
79  * @param[in]   funcnum Function number, should be range: 0..7
80  *              - 0: Select GPIO (Default)
81  *              - 1: Selects the 1st alternate function
82  *              - 2: Selects the 2nd alternate function
83  *              ...
84  *              - 7: Selects the 7th alternate function
85  * @return      None
86  **********************************************************************/
PINSEL_ConfigPin(uint8_t portnum,uint8_t pinnum,uint8_t funcnum)87 void PINSEL_ConfigPin ( uint8_t portnum, uint8_t pinnum, uint8_t funcnum)
88 {
89     uint32_t *pPIN = NULL;
90     pPIN = PIN_GetPointer(portnum, pinnum);
91     *pPIN &= 0x00000007;//Clear function bits
92     *pPIN |= funcnum;
93 }
94 
95 
96 /*********************************************************************//**
97  * @brief       Setup resistor mode for each pin
98  * @param[in]   portnum PORT number, should be in range: 0..3
99  * @param[in]   pinnum  Pin number, should be in range: 0..31
100  * @param[in]   modenum: Mode number, should be in range: 0..3
101                 - IOCON_MODE_PLAIN: Plain output
102                 - IOCON_MODE_PULLDOWN: Pull-down enable
103                 - IOCON_MODE_PULLUP: Pull-up enable
104                 - IOCON_MODE_REPEATER: Repeater mode
105  * @return      None
106  **********************************************************************/
PINSEL_SetPinMode(uint8_t portnum,uint8_t pinnum,PinSel_BasicMode modenum)107 void PINSEL_SetPinMode ( uint8_t portnum, uint8_t pinnum, PinSel_BasicMode modenum)
108 {
109     uint32_t *pPIN = NULL;
110     pPIN = PIN_GetPointer(portnum, pinnum);
111     *(uint32_t *)pPIN &= ~(3<<3);//Clear function bits
112     *(uint32_t *)pPIN |= modenum;
113 }
114 
115 /*********************************************************************//**
116  * @brief       Setup hysteresis for each pin
117  * @param[in]   portnum Port number, should be in range: 0..3
118  * @param[in]   pinnum  Pin number, should be in range: 0..31
119  * @param[in]   NewState new state of Hysteresis mode, should be:
120  *              - ENABLE: Hysteresis enable
121  *              - DISABLE: Hysteresis disable
122  * @return      None
123  **********************************************************************/
PINSEL_SetHysMode(uint8_t portnum,uint8_t pinnum,FunctionalState NewState)124 void PINSEL_SetHysMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
125 {
126     uint32_t *pPIN = NULL;
127     pPIN = PIN_GetPointer(portnum, pinnum);
128     if(NewState == DISABLE)
129     {
130         *(uint32_t *)pPIN &= ~IOCON_HYS;//Clear hys bits
131     }
132     else
133         *(uint32_t *)pPIN |= IOCON_HYS;
134 }
135 
136 /*********************************************************************//**
137  * @brief       Setup Slew rate for each pin
138  * @param[in]   portnum Port number, should be in range: 0..3
139  * @param[in]   pinnum  Pin number, should be in range: 0..31
140  * @param[in]   NewState new state of Slew rate control, should be:
141  *              - ENABLE: Output slew rate control is enable
142  *              - DISABLE: Output slew rate control is disable
143  * @return      None
144  **********************************************************************/
PINSEL_SetSlewMode(uint8_t portnum,uint8_t pinnum,FunctionalState NewState)145 void PINSEL_SetSlewMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
146 {
147     uint32_t *pPIN = NULL;
148     pPIN = PIN_GetPointer(portnum, pinnum);
149     if(NewState == DISABLE)
150     {
151         *(uint32_t *)pPIN &= ~IOCON_SLEW;//Clear hys bits
152     }
153     else
154         *(uint32_t *)pPIN |= IOCON_SLEW;
155 }
156 
157 /*********************************************************************//**
158  * @brief       Setup Input Buffer for each pin
159  * @param[in]   portnum Port number, should be in range: 0..3
160  * @param[in]   pinnum  Pin number, should be in range: 0..31
161  * @param[in]   NewState new state of Input buffer mode, should be:
162  *              - ENABLE: The input buffer is enable
163  *              - DISABLE: The input buffer is disable
164  * @return      None
165  **********************************************************************/
PINSEL_SetInBufMode(uint8_t portnum,uint8_t pinnum,FunctionalState NewState)166 void PINSEL_SetInBufMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
167 {
168     uint32_t *pPIN = NULL;
169     pPIN = PIN_GetPointer(portnum, pinnum);
170     if(NewState == DISABLE)
171     {
172         *(uint32_t *)pPIN &= ~IOCON_INBUF;//Clear hys bits
173     }
174     else
175         *(uint32_t *)pPIN |= IOCON_INBUF;
176 }
177 
178 /*********************************************************************//**
179  * @brief       Setup I2CMode for only pins that provide special I2C functionality
180  * @param[in]   portnum Port number, should be in range: 0..3
181  * @param[in]   pinnum  Pin number, should be in range: 0..31
182  * @param[in]   I2CMode I2C mode, should be:
183  *              - IOCON_I2CMODE_FAST: Fast mode and standard I2C mode
184  *              - IOCON_I2CMODE_OPENDRAIN: Open drain I/O
185  *              - IOCON_I2CMODE_FASTPLUS: Fast Mode Plus I/O
186  *              - IOCON_I2CMODE_HIGHOPENDRAIN: High drive open drain I/O
187  * @return      None
188  **********************************************************************/
PINSEL_SetI2CMode(uint8_t portnum,uint8_t pinnum,PinSel_I2cMode I2CMode)189 void PINSEL_SetI2CMode(uint8_t portnum, uint8_t pinnum, PinSel_I2cMode I2CMode)
190 {
191     uint32_t *pPIN = NULL;
192     pPIN = PIN_GetPointer(portnum, pinnum);
193 
194     *(uint32_t *)pPIN &= ~(PINSEL_I2C_MODE_BITMASK<< PINSEL_I2C_MODE_POS);
195     *(uint32_t *)pPIN |= (I2CMode << PINSEL_I2C_MODE_POS);
196 }
197 
198 /*********************************************************************//**
199  * @brief       Setup Open-drain mode in each pin
200  * @param[in]   portnum Port number, should be in range: 0..3
201  * @param[in]   pinnum  Pin number, should be in range: 0..31
202  * @param[in]   NewState new state of Open-drain mode:
203  *              - DISABLE: Normal pin I/O mode
204  *              - ENABLE: Open-drain enable
205  * @return      None
206  **********************************************************************/
PINSEL_SetOpenDrainMode(uint8_t portnum,uint8_t pinnum,FunctionalState NewState)207 void PINSEL_SetOpenDrainMode(uint8_t portnum, uint8_t pinnum, FunctionalState NewState)
208 {
209     uint32_t *pPIN = NULL;
210     pPIN = PIN_GetPointer(portnum, pinnum);
211     if(NewState == DISABLE)
212     {
213         *(uint32_t *)pPIN &= ~IOCON_ODMODE;//Clear hys bits
214     }
215     else
216     {
217         *(uint32_t *)pPIN |= IOCON_ODMODE;
218     }
219 }
220 
221 /*********************************************************************//**
222  * @brief       Enable the Analog mode for each pin (default is as Digital pins)
223  * @param[in]   portnum PORT number, should be in range: 0..3
224  * @param[in]   pinnum  Pin number, should be in range: 0..31
225  * @param[in]   enable: the state of the pin that is expected to run
226                 - ENABLE: Enable the DAC mode of the pin
227                 - DISABLE: Disable the DAC mode
228  * @return      None
229  **********************************************************************/
PINSEL_SetAnalogPinMode(uint8_t portnum,uint8_t pinnum,uint8_t enable)230 void PINSEL_SetAnalogPinMode (uint8_t portnum, uint8_t pinnum, uint8_t enable)
231 {
232     uint32_t *pPIN = NULL;
233 
234     uint8_t condition = 0;
235 
236     condition = ((portnum == 0) && (pinnum == 12)) || ((portnum == 0) && (pinnum == 13))
237                     | ((portnum == 0) && (pinnum <= 26) && (pinnum >= 23))
238                     | ((portnum == 1) && (pinnum == 30)) || ((portnum == 1) && (pinnum == 31));
239 
240     if(!condition)
241     {
242         return;
243     }
244 
245     pPIN = PIN_GetPointer(portnum, pinnum);
246 
247     //Clear this bit to set the pin to Analog mode
248     *(uint32_t *)pPIN &= ~(PINSEL_ADMODE_BITMASK << PINSEL_ADMODE_POS);
249 
250     if(enable)
251     {
252 
253     }
254     else
255     {
256         *(uint32_t *)pPIN |= (1 << PINSEL_ADMODE_POS);//Set 16th bit to one
257     }
258 
259     return;
260 }
261 
262 
263 
264 /*********************************************************************//**
265  * @brief       Choose the DAC mode for each pin
266  * @param[in]   portnum PORT number, should be in range: 0..3
267  * @param[in]   pinnum  Pin number, should be in range: 0..31
268  * @param[in]   enable: the state of the pin that is expected to run
269                 - ENABLE: Enable the DAC mode of the pin
270                 - DISABLE: Disable the DAC mode
271  * @return      None
272  **********************************************************************/
PINSEL_DacEnable(uint8_t portnum,uint8_t pinnum,uint8_t enable)273 void PINSEL_DacEnable (uint8_t portnum, uint8_t pinnum, uint8_t enable)
274 {
275     uint32_t *pPIN = NULL;
276 
277     // This setting is only for DAC pin (output pin)
278     if(!((portnum == 0) && (pinnum == 26)))
279     {
280         return;
281     }
282 
283     pPIN = PIN_GetPointer(portnum, pinnum);
284 
285     //Clear DAC Enable function bits
286     *(uint32_t *)pPIN &= ~(PINSEL_DACEN_BITMASK << PINSEL_DACEN_POS);
287 
288     if(enable)
289     {
290         *(uint32_t *)pPIN |= (1 << PINSEL_DACEN_POS);//Set 16th bit to one
291     }
292     else
293     {
294 
295     }
296 
297     return;
298 }
299 
300 /*********************************************************************//**
301  * @brief       Control the glitch filter for each pin
302  * @param[in]   portnum PORT number, should be in range: 0..3
303  * @param[in]   pinnum  Pin number, should be in range: 0..31
304  * @param[in]   enable: the state of the pin that is expected to run
305                 - ENABLE: The noise pulses below approximately 10ns are filtered out
306                 - DISABLE: No input filtering is done.
307  * @return      None
308  **********************************************************************/
PINSEL_SetFilter(uint8_t portnum,uint8_t pinnum,uint8_t enable)309 void PINSEL_SetFilter (uint8_t portnum, uint8_t pinnum, uint8_t enable)
310 {
311     uint32_t *pPIN = NULL;
312 
313     // This setting is only for DAC pin (output pin)
314     if(!((portnum == 0) && ((pinnum == 7) || (pinnum == 8) || (pinnum == 9))))
315     {
316         return;
317     }
318 
319     pPIN = PIN_GetPointer(portnum, pinnum);
320 
321     *(uint32_t *)pPIN |= (1 << 7);//Set 7th bit for normal operation following the UM1.0
322 
323     //Clear Filter bits
324     *(uint32_t *)pPIN &= ~(PINSEL_GLITCH_FILTER_BITMASK << PINSEL_GLITCH_FILTER_POS);
325 
326     if(!enable)
327     {
328         *(uint32_t *)pPIN |= (1 << PINSEL_GLITCH_FILTER_POS);//Set 8th bit to one
329     }
330     else
331     {
332 
333     }
334 
335     *pPIN = *pPIN;
336 
337     return;
338 }
339 
340 /**
341  * @}
342  */
343 
344 /**
345  * @}
346  */
347 
348 /* --------------------------------- End Of File ------------------------------ */
349