detail : http://www.mcugear.com/

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCUGear.cpp Source File

MCUGear.cpp

00001 /* MCU Gear Library, only for testing MCUGear without any circuit you connected.
00002  * Copyright (c) 2013, NestEgg Inc., http://www.mcugear.com/
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 
00024 #include "mbed.h"
00025 #include "MCUGear.h"
00026 #include "MCUGearBase.h"
00027 
00028 #ifdef DEBUG
00029 Serial MCUGear_pc(USBTX, USBRX); // tx, rx
00030 #endif
00031 
00032 
00033 unsigned char Layer[12];
00034 unsigned char BankSetting[12][32];
00035 unsigned char BankAndInPins[7][20];
00036 
00037 unsigned char NowBank;
00038 
00039 //init
00040 //#ifdef BANK_MODE
00041 MCUGear::MCUGear(PinName scl, PinName sda, char addr, char NumberOfPin)
00042      : _i2c(scl, sda) {
00043     _i2c.frequency(FPGA_I2C_CLOCK);
00044     _addr = addr;
00045     data = 0x00;
00046     flgReg = 0;
00047     NowBank = 0;
00048     numPin = NumberOfPin;
00049 //    numCon = ConnectorNumber;
00050     if(numPin<=4){
00051         numCon = 1;
00052     }else if((numPin > 4)&&(numPin <= 8)){
00053         numCon = 2;
00054     }else if(numPin > 8){
00055         numCon = 3;
00056     }else{
00057         ;//error
00058     }
00059     
00060     write(0xff);
00061     
00062 }
00063 /*
00064 #else
00065 MCUGear::MCUGear(PinName scl, PinName sda, char a)
00066      : _i2c(scl, sda) {
00067     _i2c.frequency(FPGA_I2C_CLOCK);
00068     _addr = a;
00069     Bank = 0;
00070     data = 0x00;
00071     flgReg = 0;
00072 }
00073 
00074 #endif
00075 */
00076 
00077 //insert data function
00078 
00079 void MCUGear::savePinSetting(uint8_t number, unsigned char CPUPin,unsigned char Direction ,unsigned char ModulePin){
00080     ip[number] = CPUPin;
00081     op[number] = (Direction|ModulePin);
00082 
00083 #ifdef DEBUG
00084     MCUGear_pc.printf("ModulePin = %x :op[%d] = %x : ip[%d] = %x\n",ModulePin, number, op[number], number, ip[number]);
00085     wait(0.1);
00086     //MCUGear_pc.printf("sizeof(ModulePin) = %ubyte\n",sizeof(ModulePin));
00087     //wait(0.1);
00088 #endif
00089 
00090 }
00091 /*
00092 uint8_t MCUGear::disconnectModule(void){
00093 
00094     write(0xff);    //close module gate
00095     
00096     if(Bank == 0){
00097         fpga_write(0x0c,(0|0x80));//regist - Delete all connection on Bank 0.
00098         wait(0.001);
00099         fpga_write(0x10, 0);//enable
00100     }
00101     
00102     return 1;
00103     
00104 }
00105 */
00106 uint8_t MCUGear::disconnectModule(void){
00107 
00108     write(0xff);    //close module gate
00109 
00110     if(Bank == 0){
00111 
00112 //#ifdef test    
00113         fpga_write(0x0c,(Bank|0x80));//regist - Delete all connection on Bank 0.
00114         wait(0.001);
00115         fpga_write(0x10, Bank);//enable
00116 /*#else
00117         int i=0;
00118         
00119         fpga_write(0x0c, 0);//regist
00120         //Rest all IO connction on Bank 0.
00121         
00122         for(i=0; i<16; ++i){
00123             fpga_write(ip[i], (IO_REG_DISABLE | op[i]));
00124             wait(0.0001);
00125         }
00126         fpga_write(0x10, (Bank|0x40));//enable
00127         
00128 #endif
00129 */
00130     }
00131     return 1;
00132     
00133 }
00134 
00135 uint8_t MCUGear::connectModule(void){
00136 
00137 #ifdef DEBUG
00138     MCUGear_pc.printf("_addr = %x, [NowBank = %d ,Bank = %d] \n",_addr, NowBank, Bank);
00139     wait(0.1);
00140 #endif
00141 
00142     int i=0;
00143     
00144     if(flgReg==0){
00145         fpga_write(0x0c,Bank);//init regist
00146         
00147     }else{
00148         if(Bank == NowBank){
00149             ;   //Nothing to do
00150 #ifdef DEBUG
00151     MCUGear_pc.printf("_Nothing to do \n");
00152     wait(0.1);
00153 #endif
00154         }else{
00155             fpga_write(0x0c,Bank);//regist
00156         }
00157     }
00158     
00159 
00160     if(Bank != 0){  //Bank is between 1-7
00161         if(flgReg==0){  //Onetime IO connections
00162         
00163             for(i=0; i<16; ++i){
00164                 fpga_write(ip[i], (IO_REG_EN | op[i]));
00165                 wait(0.0001);
00166                 
00167                 if((numCon==1)&&(i==3)){
00168                     write(0xfe);    //1111 1110
00169                     break;
00170                     
00171                 }else if((numCon==2)&&(i==7)){
00172                     write(0xfc);    //1111 1100
00173                     break;
00174                     
00175                 }else if((numCon==3)&&(i==11)){
00176                     write(0xf8);    //1111 1000
00177                     break;
00178                     
00179                 }else if(i > 12){
00180                     return 2;//error
00181                 }
00182             }
00183             
00184             fpga_write(0x10,Bank);//init regist///////
00185             flgReg = 1;
00186         }else{  //only change module gate
00187         
00188             if(numCon==1){
00189                 write(0xfe);    //1111 1110
00190                 
00191             }else if(numCon==2){
00192                 write(0xfc);    //1111 1100
00193                     
00194             }else if(numCon==3){
00195                 write(0xf8);    //1111 1000
00196                     
00197             }else{
00198                 return 2;//error
00199             }
00200         
00201         }
00202 //#else
00203     }else{  //You need to change IO connections evrytime on Bank 0.
00204         for(i=0; i<16; ++i){
00205             fpga_write(ip[i], (IO_REG_EN | op[i]));
00206             wait(0.0001);
00207             
00208             if((numCon==1)&&(i==3)){
00209                 write(0xfe);    //1111 1110
00210                 break;
00211                     
00212             }else if((numCon==2)&&(i==7)){
00213                 write(0xfc);    //1111 1100
00214                 break;
00215                     
00216             }else if((numCon==3)&&(i==11)){
00217                 write(0xf8);    //1111 1000
00218                 break;
00219                     
00220             }else if(i > 12){
00221                 return 2;//error
00222             }
00223         }
00224         fpga_write(0x10,Bank);//init regist///////
00225     }
00226 //#endif
00227 /*
00228     if(flgReg==0){
00229         flgReg = 1;
00230         fpga_write(0x10,Bank);//init regist
00231         
00232     }else{
00233         if(Bank == NowBank){
00234             ;   //Nothing to do
00235 #ifdef DEBUG
00236     MCUGear_pc.printf("_Nothing to do \n");
00237     wait(0.1);
00238 #endif
00239         }else{
00240             fpga_write(0x10,Bank);//regist
00241         }
00242     }
00243 */
00244     
00245     /*
00246     if((Bank != NowBank)){
00247         fpga_write(0x10,  Bank);    //Bank enable
00248     }
00249     */
00250     NowBank = Bank; //set NowBank
00251     
00252     return 1;
00253 }
00254 
00255 
00256 
00257 void MCUGear::fpga_write(unsigned char adr, unsigned char data) {
00258   char cmd[2];
00259   cmd[0] = adr;
00260   cmd[1] = data;
00261   _i2c.write(FPGA_I2C_ADR, cmd, 2);
00262   //wait(0.01);
00263   //pc.printf("fpga write adr:%x data:%x\n", adr,data);
00264 }
00265 
00266 
00267 void MCUGear::set_addr(char s){
00268     _addr = s;
00269 }
00270 
00271 void MCUGear::set_data(char c){
00272     data = c;
00273 }
00274 
00275 //send I2C signal function
00276 void MCUGear::write(char c){
00277 
00278     char cmd[1];
00279     cmd[0] = c;
00280     _i2c.write(_addr, cmd, 1);
00281     //wait(0.01);
00282     
00283 }
00284 
00285 ////detect module
00286 void MCUGear::detect_module(uint8_t *fio) {
00287     int i;
00288   uint8_t pnum=0xff;
00289   uint8_t iio;
00290   //1pin GND
00291   
00292   write(0x7f);   //0111 1111
00293   //wait(0.1);
00294   pnum =fpga_read(FPGA_I2C_ADR,FPGA_DETECT);
00295 
00296 #ifdef DEBUG
00297   wait(1);
00298     MCUGear_pc.printf("detected. port %d \n",pnum);
00299   wait(0.1);
00300 #endif
00301   write(0xff);    //1111 1111
00302 
00303     iio = pnum;
00304     if(iio != 0xff){
00305         //Resolve boundary value problem
00306         pinArrey[0] = iio;
00307         
00308         iio = iio + 4;
00309         
00310         if((iio > 44)){
00311             iio = 0;;
00312         }
00313         pinArrey[1] = iio;
00314         
00315         
00316         iio = iio + 4;
00317         
00318         if((iio > 44)){
00319             iio = 0;
00320         }
00321         pinArrey[2] = iio;
00322         
00323 #ifdef DEBUG      
00324         MCUGear_pc.printf("detected. pinArrey[0] = %d \n",pinArrey[0]);
00325         wait(0.1);
00326 #endif
00327         
00328         //makeCircuit();
00329     }
00330 
00331     for(i=0;i<12;++i){
00332         if(pnum+i>47){
00333             fio[i] = pnum+i-48;
00334         }else{
00335             fio[i] = pnum+i;
00336         }
00337         
00338   
00339 #ifdef DEBUG      
00340         //MCUGear_pc.printf("detected. fio[%d] = %d \n",i,fio[i]);
00341         //wait(0.1);
00342 #endif
00343 
00344     }
00345 
00346  // return pnum;
00347 }
00348 
00349 
00350 
00351 ///making onecircuit 
00352 void MCUGear::makeCircuit(void){
00353     //unsigned char i = 0;
00354     char i = 0;
00355     unsigned char j = 0;
00356     int k = 0;
00357     unsigned char con = 0;
00358     unsigned char conNum2 = 0;
00359     unsigned char MaxLayer = 0;
00360     
00361     con = (pinArrey[0]/4)+1;    //creat connector number
00362 
00363 #ifdef DEBUG
00364     MCUGear_pc.printf("makeCircuit con = (pinArrey[%d]/4)+1 = %d \n",i,con);
00365     wait(0.1);
00366 #endif
00367 
00368     //find MaxLayer
00369     for(i=0; i<numCon; ++i){
00370         if((con+i) > 12){
00371             ++Layer[con + i - 12];
00372             
00373             if(MaxLayer < Layer[con + i - 12]){
00374                 MaxLayer = Layer[con + i - 12];
00375             }
00376         }else{
00377             ++Layer[con + i];
00378             
00379             if(MaxLayer < Layer[con + i]){
00380                 MaxLayer = Layer[con + i];
00381             }
00382         }
00383     }
00384     
00385     //Check over rap CPU pins on a Bank
00386     //If it find over rap, set other layers.
00387     conNum2 = numCon * 4;
00388     
00389     for(i=0; i<20; ++i){
00390         for(j=0; j<numPin; ++j){
00391             if(BankAndInPins[MaxLayer][i] == ip[j]){
00392                 ++MaxLayer;
00393                 
00394 #ifdef DEBUG
00395     MCUGear_pc.printf("CPU IO over rap! ++MaxLayer : %d \n",MaxLayer);
00396     wait(0.1);
00397 #endif
00398                 //i = 0x20;
00399                 //j = numPin;
00400                 //break;
00401                 i = 0;
00402                 j = 0;
00403             }
00404         }
00405     }
00406     
00407     //Save Datas
00408     for(j=0; j<conNum2; ++j){
00409         k = (int)(ip[j]-0x80);
00410         BankAndInPins[MaxLayer][k] = ip[j];
00411 #ifdef DEBUG
00412     MCUGear_pc.printf("BankAndInPins[%d][%d] = %x \n",MaxLayer,k,ip[j]);
00413     wait(0.1);
00414 #endif
00415     }
00416     
00417     
00418     //save MaxLayer to all connectors
00419     for(i=0; i<numCon; ++i){
00420         if(MaxLayer > Layer[con + i]){
00421             Layer[con + i] = MaxLayer;
00422         }
00423         
00424     }
00425     
00426     
00427 #ifdef DEBUG
00428     MCUGear_pc.printf("MaxLayer = %d \n",MaxLayer);
00429     wait(0.1);
00430 #endif
00431     
00432     for(i=0; i<numCon; ++i){
00433         
00434         if((con + i)>12){
00435             BankSetting[con + i - 12][MaxLayer] = _addr;
00436 
00437 #ifdef DEBUG
00438             MCUGear_pc.printf("BankSetting[CON %d][Layer %d] = %x \n",(con + i - 12),MaxLayer,_addr);
00439             wait(0.1);
00440 #endif
00441         }else{
00442             BankSetting[con + i][MaxLayer] = _addr;
00443 
00444 #ifdef DEBUG
00445             MCUGear_pc.printf("BankSetting[CON %d][Layer %d] = %x \n",(con + i),MaxLayer,_addr);
00446             wait(0.1);
00447 #endif
00448         }
00449     }
00450     
00451     
00452     //make 0-6 BANKs others are chang single pin
00453     //MaxLayer = BANK No
00454     
00455     if(MaxLayer <= BankMaxNum){
00456         fpga_write(0x0c,  MaxLayer);    //regist to Bank
00457         Bank = MaxLayer;
00458     }else{
00459         Bank = 0;
00460 #ifdef DEBUG
00461             MCUGear_pc.printf("set Bank = 0\n");
00462             wait(0.1);
00463 #endif
00464         
00465     }
00466     
00467 }
00468 
00469 
00470 
00471