FormazioneSitael / Mbed 2 deprecated I2C_PASSED

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2C.cpp Source File

I2C.cpp

00001 #include "mbed.h"
00002 #include "TYPE_Def.h"
00003 #include "CRC32BIT_LOOKUP_Ext.h"
00004 
00005 
00006 #define add (0x22)
00007 #define idstr (0x14)
00008 #define idspr (0x16)
00009 
00010 
00011 I2C i2c1(I2C_SDA, I2C_SCL);
00012 Serial pc(USBTX, USBRX); // tx, rx
00013 DigitalOut myled(LED2);
00014 DigitalOut pin(D12);
00015 
00016 UINT32_T crc;
00017 ERROR_T ERROR_Value;
00018 CRC32BIT_LOOKUP_TS _Crc32Table_TGP_DUT;
00019 BYTE_T vettore[13];
00020 char crcchar[4];
00021 
00022 int i,j,n;
00023 
00024 char UINT32_PacketSize [4];
00025 char ENUM_Packet_ID;
00026 char UINT16_PacketCounter [2];
00027 char UINT32_Timestamp [4];
00028 char UINT16_PortNumber [2];
00029 char UINT32_BaudRate [4];
00030 char UINT32_CRC [4];
00031 
00032 int hz;
00033 int ackr;
00034 int ackw;
00035 
00036 char packetsize[4] = {0x00 ,0x00 , 0x00 , 0x11};
00037 
00038 void crc_cal();
00039 void TGP_CMD();
00040 void TGP_START_READ_ACK();
00041 void TGP_STOP_READ_ACK();
00042 void TGP_START_WRITE_ACK();
00043 void TGP_STOP_WRITE_ACK();
00044 
00045 char data;
00046 char dataout;
00047 
00048 int main()
00049 {
00050     pin = 0;
00051     
00052     ENUM_Packet_ID = 0;
00053     data = 0;
00054     dataout = 0;
00055     
00056     
00057     ackr = 1;
00058     ackw = 1;
00059     
00060     pc.attach(&TGP_CMD,Serial::RxIrq);
00061     
00062     j=0;
00063     n=0;
00064     
00065     
00066     
00067 }
00068 
00069 void crc_cal()
00070 {   
00071     
00072     //def vettore
00073     j=0;
00074     for(i=0; i<4; i++){
00075             vettore[j] = packetsize[i];
00076             j++;
00077     }
00078     
00079     vettore[j] = ENUM_Packet_ID+1;
00080     j++;
00081     
00082     for(i=0; i<2; i++){
00083             vettore[j] = UINT16_PacketCounter[i];
00084             j++;
00085         }
00086         
00087     for(i=0; i<4; i++){
00088             vettore[j] = UINT32_Timestamp[i];
00089             j++;
00090         }
00091         
00092     for(i=0;i<2;i++)
00093     {
00094         vettore[j] = UINT16_PortNumber[i];   
00095         j++;
00096     }    
00097     
00098     
00099     _Crc32Table_TGP_DUT.UINT32_Poly = 0xE7994FEB;
00100    _Crc32Table_TGP_DUT.UINT32_Init =  0xFFFFFFFF;
00101    _Crc32Table_TGP_DUT.BOOL_RefIn = FALSE;
00102    _Crc32Table_TGP_DUT.BOOL_RefOut =  FALSE;
00103    _Crc32Table_TGP_DUT.UINT32_XorOut = 0x00000000;
00104 
00105    /* Inizializzazione della tabella */
00106    ERROR_Value = CRC32BIT_LOOKUP_Init(&_Crc32Table_TGP_DUT);
00107    
00108    crc = CRC32BIT_LOOKUP_Compute(vettore,13,&_Crc32Table_TGP_DUT);
00109    
00110    crcchar[0] = (crc >> 24) & 0xFF;
00111     crcchar[1] = (crc >> 16) & 0xFF;
00112     crcchar[2] = (crc >> 8) & 0xFF;
00113     crcchar[3] = crc & 0xFF;
00114    
00115 }
00116 
00117 void TGP_CMD()
00118 {
00119     myled = 0;
00120     
00121     for(i=0; i<4; i++)
00122     {
00123             UINT32_PacketSize[i] = pc.getc();
00124     }
00125     
00126     ENUM_Packet_ID = pc.getc();
00127     
00128     if(ENUM_Packet_ID == idstr)
00129     {
00130         for(i=0; i<2; i++){
00131             UINT16_PacketCounter[i]=pc.getc();
00132         }
00133         
00134         for(i=0; i<4; i++){
00135             UINT32_Timestamp[i]=pc.getc();
00136         }
00137         
00138         for(i=0; i<2; i++){
00139             UINT16_PortNumber[i]=pc.getc();
00140         }
00141         
00142         for(i=0; i<4; i++){
00143             UINT32_BaudRate[i]=pc.getc();
00144         }
00145         
00146         for(i=0; i<4; i++){
00147             UINT32_CRC[i]=pc.getc();
00148         }
00149         
00150         myled = 1;
00151         
00152         TGP_START_READ_ACK();
00153         
00154     } 
00155     else if(ENUM_Packet_ID == idspr)
00156     {
00157         for(i=0; i<2; i++){
00158             UINT16_PacketCounter[i]=pc.getc();
00159         }
00160         
00161         for(i=0; i<4; i++){
00162             UINT32_Timestamp[i]=pc.getc();
00163         }
00164         
00165         for(i=0; i<2; i++){
00166             UINT16_PortNumber[i]=pc.getc();
00167         }
00168         
00169         for(i=0; i<4; i++){
00170             UINT32_CRC[i]=pc.getc();
00171         }
00172         
00173         myled = 1;
00174         
00175         TGP_STOP_READ_ACK();
00176         
00177     }
00178     
00179     else 
00180     {
00181         while(1)
00182         {
00183             myled = 1;
00184             wait(0.1);
00185             myled = 0;
00186             wait(0.1);   
00187         }
00188     }
00189     
00190     
00191 }
00192 
00193 
00194 void TGP_START_READ_ACK()
00195 {
00196     for(i=0; i<4; i++)
00197     {
00198         pc.printf("%c",packetsize[i]); 
00199     }
00200     
00201     pc.printf("%c",ENUM_Packet_ID+1);
00202     
00203     for(i=0; i<2; i++)
00204     {
00205         pc.printf("%c",UINT16_PacketCounter[i]);
00206     }
00207         
00208     for(i=0; i<4; i++)
00209     {
00210         pc.printf("%c",UINT32_Timestamp[i]);
00211     }
00212     
00213     for(i=0; i<2; i++)
00214     {
00215         pc.putc(UINT16_PortNumber[i]);
00216     }
00217     
00218     crc_cal();
00219     
00220     for(i=0; i<4; i++)
00221     {
00222         pc.putc(crcchar[i]);
00223     }
00224     
00225     myled = 0;
00226     pin = 1;
00227 }
00228 
00229 void TGP_STOP_READ_ACK()
00230 {
00231     for(i=0; i<4; i++)
00232     {
00233         pc.printf("%c",packetsize[i]); 
00234     }
00235     
00236     pc.printf("%c",ENUM_Packet_ID+1);
00237     
00238     for(i=0; i<2; i++)
00239     {
00240         pc.printf("%c",UINT16_PacketCounter[i]);
00241     }
00242         
00243     for(i=0; i<4; i++)
00244     {
00245         pc.printf("%c",UINT32_Timestamp[i]);
00246     }
00247     
00248     for(i=0; i<2; i++)
00249     {
00250         pc.putc(UINT16_PortNumber[i]);
00251     }
00252     
00253     crc_cal();
00254     
00255     for(i=0; i<4; i++)
00256     {
00257         pc.putc(crcchar[i]);
00258     }
00259     
00260     myled = 0;
00261     pin = 0;
00262 }