Thomas Hamilton / PinConnectBlock
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PinConnectBlock.c Source File

PinConnectBlock.c

00001 #include "PinConnectBlock.h"
00002 
00003 unsigned char PinConnectBlock::ValidateInput(
00004     unsigned char Port, unsigned char Pin, unsigned char Mode)
00005 {
00006     switch (Port)
00007     {
00008         case 0:
00009             if (Pin < 2)
00010             {
00011                 return 0x00;
00012             }
00013             if ((Pin > 3) && (Pin < 12))
00014             {
00015                 return 0x00;
00016             }
00017             if ((Pin > 14) && (Pin < 19))
00018             {
00019                 return 0x00;
00020             }
00021             if ((Pin > 22) && (Pin < 27))
00022             {
00023                 return 0x00;
00024             }
00025             if ((Mode & 0x03) != 0x03)
00026             {
00027                 if (Pin < 4)
00028                 {
00029                     return 0x00;
00030                 }
00031                 if ((Pin > 26) && (Pin < 29) && !(Mode & 0x1C))
00032                 {
00033                     return 0x00;
00034                 }
00035             }
00036             if ((Pin > 18) && (Pin < 23) && ((Mode & 0x03) != 0x02))
00037             {
00038                 return 0x00;
00039             }
00040             if ((Pin > 28) && (Pin < 31) && !(Mode & 0x0E))
00041             {
00042                 return 0x00;
00043             }
00044             if (Pin < 4)
00045             {
00046                 return 0x0C;
00047             }
00048             if (Pin < 15)
00049             {
00050                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00051                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00052                     | ((Mode & 0x10) ? 0x24 : 0x00);
00053             }
00054             if (Pin < 23)
00055             {
00056                 return 0x0C;
00057             }
00058             if (Pin < 29)
00059             {
00060                 return (((Mode & 0x03) == 0x03) ? 0x0C : 0x00)
00061                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00062                     | ((Mode & 0x10) ? 0x24 : 0x00);
00063             }
00064             if (Pin < 31)
00065             {
00066                 return ((Mode & 0x02) ? 0x0C : 0x00)
00067                     | ((Mode & 0x0C) ? 0x14 : 0x00);
00068             }
00069             if (Pin > 30)
00070             {
00071                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00072                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00073                     | ((Mode & 0x10) ? 0x24 : 0x00);
00074             }
00075             break;
00076         case 1:
00077             if ((Pin > 17) && (Pin < 25))
00078             {
00079                 return 0x00;
00080             }
00081             if ((Pin > 25) && (Pin < 30))
00082             {
00083                 return 0x00;
00084             }
00085             if (!(Mode & 0x02))
00086             {
00087                 if (Pin < 2)
00088                 {
00089                     return 0x00;
00090                 }
00091                 if (Pin == 4)
00092                 {
00093                     return 0x00;
00094                 }
00095                 if ((Pin > 7) && (Pin < 11))
00096                 {
00097                     return 0x00;
00098                 }
00099                 if ((Pin > 13) && (Pin < 18))
00100                 {
00101                     return 0x00;
00102                 }
00103             }
00104             if ((Pin == 25) && ((Mode & 0x03) != 0x02))
00105             {
00106                 return 0x00;
00107             }
00108             if ((Pin > 29) && (Pin < 32) && ((Mode & 0x03) != 0x01))
00109             {
00110                 return 0x00;
00111             }
00112             if (Pin < 2)
00113             {
00114                 return 0x0C;
00115             }
00116             if (Pin < 4)
00117             {
00118                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00119                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00120                     | ((Mode & 0x10) ? 0x24 : 0x00);
00121             }
00122             if (Pin == 4)
00123             {
00124                 return 0x0C;
00125             }
00126             if (Pin < 8)
00127             {
00128                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00129                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00130                     | ((Mode & 0x10) ? 0x24 : 0x00);
00131             }
00132             if (Pin < 11)
00133             {
00134                 return 0x0C;
00135             }
00136             if (Pin < 14)
00137             {
00138                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00139                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00140                     | ((Mode & 0x10) ? 0x24 : 0x00);
00141             }
00142             if (Pin < 18)
00143             {
00144                 return 0x0C;
00145             }
00146             if ((Pin == 25) && ((Mode & 0x03) == 0x02))
00147             {
00148                 return 0x0C;
00149             }
00150             if ((Pin > 29) && (Pin < 32) && ((Mode & 0x03) == 0x01))
00151             {
00152                 return 0x0C;
00153             }
00154             if (Pin > 31)
00155             {
00156                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00157                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00158                     | ((Mode & 0x10) ? 0x24 : 0x00);
00159             }
00160             break;
00161         case 2:
00162             if ((Pin > 7) && (Pin < 10))
00163             {
00164                 return 0x00;
00165             }
00166             if ((Pin < 11) && ((Mode & 0x03) != 0x03))
00167             {
00168                 return 0x00;
00169             }
00170             if ((Pin > 10) && (Pin < 14) && ((Mode & 0x03) != 0x02))
00171             {
00172                 return 0x00;
00173             }
00174             if (Pin < 14)
00175             {
00176                 return 0x0C;
00177             }
00178             if (Pin > 13)
00179             {
00180                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00181                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00182                     | ((Mode & 0x10) ? 0x24 : 0x00);
00183             }
00184             break;
00185         case 3:
00186             if (Pin == 26)
00187             {
00188                 return 0x00;
00189             }
00190             if ((Pin == 25) && ((Mode & 0x03) != 0x01))
00191             {
00192                 return 0x00;
00193             }
00194             if (Pin < 25)
00195             {
00196                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00197                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00198                     | ((Mode & 0x10) ? 0x24 : 0x00);
00199             }
00200             if (Pin == 25)
00201             {
00202                 return 0x0C;
00203             }
00204             if (Pin > 26)
00205             {
00206                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00207                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00208                     | ((Mode & 0x10) ? 0x24 : 0x00);
00209             }
00210             break;
00211         case 4:
00212             if ((Pin > 27) && (Pin < 30))
00213             {
00214                 return 0x00;
00215             }
00216             if (Pin < 28)
00217             {
00218                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00219                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00220                     | ((Mode & 0x10) ? 0x24 : 0x00);
00221             }
00222             if (Pin > 29)
00223             {
00224                 return 0x02 | ((Mode & 0x03) ? 0x0C : 0x00)
00225                     | ((Mode & 0x0C) ? 0x14 : 0x00)
00226                     | ((Mode & 0x10) ? 0x24 : 0x00);
00227             }
00228             break;
00229         default:
00230             return 0x01 | ((Pin > 31) ? 0x02 : 0x00)
00231                 | ((Mode & 0x03) ? 0x0C : 0x00)
00232                 | ((Mode & 0x0C) ? 0x14 : 0x00)
00233                 | ((Mode & 0x10) ? 0x24 : 0x00);
00234     }
00235     return 0xFF;
00236 }
00237 
00238 void PinConnectBlock::SetPinConnection(
00239     unsigned char Port, unsigned char Pin, unsigned char Mode)
00240 {
00241     if (!((Port == 1) && (Pin < 18) && (Mode & 0x02)))
00242     {
00243         SetFunctionMode(Port, Pin, Mode & 0x03);
00244     }
00245     if (!((Port == 0) && (Pin > 26)))
00246     {
00247         SetResistorMode(Port, Pin, (Mode & 0x0C) >> 2);
00248     }
00249     if (!((Port == 0) && (Pin > 26) && (Pin < 29)))
00250     {
00251         SetOpenDrainMode(Port, Pin, Mode & 0x10);
00252     }
00253     return;
00254 }
00255 
00256 unsigned char PinConnectBlock::GetPinConnection(
00257     unsigned char Port, unsigned char Pin)
00258 {
00259     return GetFunctionMode(Port, Pin)
00260         | (GetResistorMode(Port, Pin) << 2)
00261         | (GetOpenDrainMode(Port, Pin) ? 0x10 : 0x00);
00262 }
00263 
00264 void PinConnectBlock::SetFunctionMode(
00265     unsigned char Port, unsigned char Pin, unsigned char Mode)
00266 {
00267     Port *= 2;
00268     if (Pin >= 16)
00269     {
00270         Port++;
00271         Pin -= 16;
00272     }
00273     Pin *= 2;
00274     *((unsigned long*)&LPC_PINCON->PINSEL0 + Port)
00275         &= ~(0x00000003 << Pin);
00276     *((unsigned long*)&LPC_PINCON->PINSEL0 + Port)
00277         |= (unsigned long)Mode << Pin;
00278 }
00279 
00280 unsigned char PinConnectBlock::GetFunctionMode(
00281     unsigned char Port, unsigned char Pin)
00282 {
00283     Port *= 2;
00284     if (Pin >= 16)
00285     {
00286         Port++;
00287         Pin -= 16;
00288     }
00289     Pin *= 2;
00290     return (*((unsigned long*)&LPC_PINCON->PINSEL0 + Port)
00291         & (0x00000003 << Pin)) >> Pin;
00292 }
00293 
00294 void PinConnectBlock::SetResistorMode(
00295     unsigned char Port, unsigned char Pin, unsigned char Mode)
00296 {
00297     Port *= 2;
00298     if (Pin >= 16)
00299     {
00300         Port++;
00301         Pin -= 16;
00302     }
00303     Pin *= 2;
00304     *((unsigned long*)&LPC_PINCON->PINMODE0 + Port)
00305         &= ~(0x00000003 << Pin);
00306     *((unsigned long*)&LPC_PINCON->PINMODE0 + Port)
00307         |= (unsigned long)Mode << Pin;
00308     return;
00309 }
00310 
00311 unsigned char PinConnectBlock::GetResistorMode(
00312     unsigned char Port, unsigned char Pin)
00313 {
00314     Port *= 2;
00315     if (Pin >= 16)
00316     {
00317         Port++;
00318         Pin -= 16;
00319     }
00320     Pin *= 2;
00321     return (*((unsigned long*)&LPC_PINCON->PINMODE0 + Port)
00322         & (0x00000003 << Pin)) >> Pin;
00323 }
00324 
00325 void PinConnectBlock::SetOpenDrainMode(
00326     unsigned char Port, unsigned char Pin, bool Mode)
00327 {
00328     if (Mode)
00329     {
00330         *((unsigned long*)&LPC_PINCON->PINMODE_OD0 + Port)
00331             |= 0x00000001 << Pin;
00332     }
00333     else
00334     {
00335         *((unsigned long*)&LPC_PINCON->PINMODE_OD0 + Port)
00336             &= ~(0x00000001 << Pin);
00337     }
00338     return;
00339 }
00340 
00341 bool PinConnectBlock::GetOpenDrainMode(unsigned char Port, unsigned char Pin)
00342 {
00343     return (*((unsigned long*)&LPC_PINCON->PINMODE_OD0 + Port)
00344         & (0x00000001 << Pin)) >> Pin;
00345 }
00346 
00347 void PinConnectBlock::SetTraceMode(bool Mode)
00348 {
00349     if (Mode)
00350     {
00351         LPC_PINCON->PINSEL10 |= 0x00000008;
00352     }
00353     else
00354     {
00355         LPC_PINCON->PINSEL10 &= ~0x00000008;
00356     }
00357     return;
00358 }
00359 
00360 bool PinConnectBlock::GetTraceMode()
00361 {
00362     return (LPC_PINCON->PINSEL10 & 0x00000008) != 0x00000000;
00363 }
00364 
00365 void PinConnectBlock::SetI2C0Mode(unsigned char Mode)
00366 {
00367 
00368     if (!(Mode & 0x01))
00369     {
00370         LPC_PINCON->I2CPADCFG |= 0x0000000A;
00371     }
00372     else
00373     {
00374         LPC_PINCON->I2CPADCFG &= ~0x0000000A;
00375     }
00376     if (Mode & 0x02)
00377     {
00378         LPC_PINCON->I2CPADCFG |= 0x00000005;
00379     }
00380     else
00381     {
00382         LPC_PINCON->I2CPADCFG &= ~0x00000005;
00383     }
00384     return;
00385 }
00386 
00387 unsigned char PinConnectBlock::GetI2C0Mode()
00388 {
00389     unsigned char Result = 0x00;
00390 
00391     if (!(LPC_PINCON->I2CPADCFG & 0x0000000A))
00392     {
00393         Result |= 0x01;
00394     }
00395     else if (!(LPC_PINCON->I2CPADCFG & 0x00000002))
00396     {
00397         Result |= 0x04;
00398     }
00399     else if (!(LPC_PINCON->I2CPADCFG & 0x00000008))
00400     {
00401         Result |= 0x08;
00402     }
00403     if ((LPC_PINCON->I2CPADCFG & 0x00000005) == 0x00000005)
00404     {
00405         Result |= 0x02;
00406     }
00407     else if (LPC_PINCON->I2CPADCFG & 0x00000001)
00408     {
00409         Result |= 0x10;
00410     }
00411     else if (LPC_PINCON->I2CPADCFG & 0x00000004)
00412     {
00413         Result |= 0x20;
00414     }
00415     return Result;
00416 }