SHENG-HEN HSIEH / Mbed 2 deprecated A1333_A1339_spi_master

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "A1333_PoOPoO.h"
00003 
00004 #define pi 3.141592f
00005 #define d2r 0.01745329f
00006 #define NN  1
00007 #define Rms 1000000             // TT rate
00008 
00009 //choose if write in configurations
00010 #define WRCONFIG    1           // 1 to write in configurations
00011 
00012 //choose if A1339 extended settings
00013 #define A1339EXT    0           // 1 to enable A1339 extended settings
00014 
00015 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓GPIO registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00016 //~~~structure~~~//
00017 DigitalOut  led(D13);           //detection
00018 DigitalOut  TT_ext(D12);
00019 
00020 //~~~SPI~~~//
00021 DigitalOut  spi_CS(D6,1);       //low for ACC/MAG enable
00022 SPI spi(D4, D5, D3);            //MOSI MISO SCLK
00023 
00024 //~~~Serial~~~//
00025 Serial      pc(D1, D0);         //Serial reg(TX RX)
00026 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of GPIO registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00027 
00028 
00029 
00030 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Varible registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00031 //~~~globle~~~//
00032 Ticker  TT;                         //call a timer
00033 int Count = 0;                      //one second counter for extrenal led blink
00034 
00035 //~~~A1333_data~~~//
00036 
00037 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Varible registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00038 
00039 
00040 
00041 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Function registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00042 void    init_TIMER();           //set TT_main() rate
00043 void    TT_main();              //timebase function rated by TT
00044 void    init_IO();              //initialize IO state
00045 void    A1333_init();           //initialize shit
00046 void    A1333_reset();          //restart target
00047 void    A1333_getstate();       //get current state & errors
00048 void    A1333_ulkEEPROM();      //unlock protection of EEPROM
00049 void    A1333_getEEP(int X_E);  //read EEPROM of X_E
00050 void    A1333_writeEEP(int X_E, int32_t DataH, int32_t DataL);  //write EEPROM of X_E
00051 
00052 //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Function registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑//
00053 
00054 
00055 
00056 //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓main funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓//
00057 int main()
00058 {
00059     init_IO();                  //initialized value
00060     A1333_init();
00061 
00062     pc.printf("\n\n");
00063     pc.printf("Welcome to A1333/A1339 configurator\n");
00064     pc.printf("Resetting target...\n");
00065     A1333_reset();
00066     wait_ms(200);
00067 
00068 #if A1339EXT
00069     pc.printf("A1339 extended settings are enable!\n");
00070 #endif
00071 
00072     A1333_getstate();
00073     pc.printf("\n");
00074 
00075     pc.printf("Read PWE_E\n");  //Doesnt matter for us
00076     A1333_getEEP(PWE_E);
00077     pc.printf("\n");
00078 
00079     pc.printf("Read ABI_E\n");  //Need to be set for application!!
00080     A1333_getEEP(ABI_E);
00081     pc.printf("\n");
00082 
00083     pc.printf("Read MSK_E\n");  //Doesnt matter for us
00084     A1333_getEEP(MSK_E);
00085     pc.printf("\n");
00086 
00087     pc.printf("Read PWI_E\n");  //Doesnt matter for us
00088     A1333_getEEP(PWI_E);
00089     pc.printf("\n");
00090 
00091     pc.printf("Read ANG_E\n");
00092     A1333_getEEP(ANG_E);
00093     pc.printf("\n");
00094 
00095     pc.printf("Read LPC_E\n");
00096     A1333_getEEP(LPC_E);
00097     pc.printf("\n");
00098 
00099     pc.printf("Read COM_E\n");
00100     A1333_getEEP(COM_E);
00101     pc.printf("\n");
00102 
00103     // A1333/A1339 require unlock before eeprom access
00104     A1333_ulkEEPROM();
00105     pc.printf("\n");
00106 
00107 #if WRCONFIG
00108     pc.printf("Start writting configurations!\n");
00109 
00110     /* Configuration sectionfor ABI_EEPROM settings "A1333/A1339"
00111     ABI_SLEW_TIME, INV,  AHE,  INDEX_MODE, WDH, PLH, IOE, UVW, RESOLUTION_PAIRS,
00112     [21:16],       [15], [12], [9:8],      [7], [6], [5], [4], [3:0],
00113     00000001,      0,    1,    0,          1,   1,   1,   0,   0100,
00114     250ns          no,   yes,  0+R,        yes, yes, yes, ABI, 1024pulse/mechRev,(Notice ABx4 res is *4 at last)
00115     result to 0x0001, 0x10E4
00116     Run follow to write in */
00117     A1333_writeEEP(ABI_E, 0x01, 0x10E4);
00118     pc.printf("ABI_E write done\n");
00119     pc.printf("\n");
00120 
00121     /* Configuration sectionfor PWI_EEPROM settings "A1333/A1339"
00122     PEN,  BAND,    FREQ,    PHE,  PEO,  PES,  DM,  S17, SC,
00123     [23], [22:20], [19:16], [14], [13], [12], [3], [1], [0],
00124     1,    0,       0,       0,    0,    0,    0,   0,   0,
00125     yes,  3125Hz,           no,   no,   no,   no,  no,  no,
00126     result to 0x0080, 0x0000
00127     Run follow to write in */
00128     A1333_writeEEP(PWI_E, 0x80, 0x0000);
00129     pc.printf("PWI_E write done\n");
00130     pc.printf("\n");
00131 
00132     /* Configuration sectionfor ANG_EEPROM settings "A1333/A1339"
00133     ORATE,   RD,   RO,        HYSTERESIS,  ZERO_OFFSET,
00134     [23:20], [19], [18],      [17:12],     [11:0],
00135     0000,    0,    0,         101000,      0x000-0x3FF,
00136     1us,     no,   direction, 20/16383rev, Calibrate in DSM instead,
00137     result to 0x0002, 0x8000
00138     Run follow to write in */
00139     A1333_writeEEP(ANG_E, 0x02, 0x8000);
00140     pc.printf("ANG_E write done\n");
00141     pc.printf("\n");
00142 
00143 #if A1339EXT
00144     /* Configuration sectionfor LPC_EEPROM settings "A1339 only"
00145     T45,     TPMD, LPMD, LPM_CYCLE_TIME, LPM_WAKE_THRESHOLD,
00146     [23],    [21], [20], [17:12],        [10:0],
00147     1,       1,    1,    001011,         01010011111,
00148     default, no,   no,   default,        default,
00149     result to 0x00B0, 0xB29F
00150     Run follow to write in */
00151     A1333_writeEEP(LPC_E, 0xB0, 0xB29F);    //A1339 only
00152     pc.printf("LPC_E write done\n");
00153     pc.printf("\n");
00154 #endif  //A1339EXT
00155 
00156     /* Configuration sectionfor COM_EEPROM settings "A1333/A1339"
00157     LOCK,    LBE,  CSE,  DST,  DHR,  MAG_THRES_HI, MAG_THRES_LO,
00158     [23:20], [19], [18], [13], [12], [11:6],       [5:0],
00159     0000,    1,    1,    0,    0,    100101,       001101,
00160     no,      yes,  yes,  no,   no,   default,      default,
00161     result to 0x000C, 0x094D
00162     Run follow to write in */
00163     A1333_writeEEP(COM_E, 0x0C, 0x094D);
00164     pc.printf("COM_E write done\n");
00165     pc.printf("\n");
00166 
00167 #endif  //WRCONFIG
00168 
00169     pc.printf("End of A1333/A1339 configurator\n");
00170 
00171     init_TIMER();               //start TT_main
00172 
00173     while(1) {                  //main() loop
00174         if(Count >= NN) {       //check if main working
00175             Count=0;
00176             led = !led;
00177         }
00178     }
00179 }
00180 
00181 
00182 void init_TIMER()                   //set TT_main{} rate
00183 {
00184     TT.attach_us(&TT_main, Rms);
00185 }
00186 
00187 
00188 void TT_main()                      //interrupt function by TT
00189 {
00190     TT_ext = !TT_ext;               //indicate TT_main() function working
00191     Count = Count+1;                //one second counter
00192 }
00193 
00194 
00195 void init_IO(void)                  //initialize
00196 {
00197     pc.baud(115200);                //set baud rate
00198     TT_ext = 0;
00199     led = 1;
00200 }
00201 
00202 
00203 void A1333_init(void)               //initialize
00204 {
00205     //gloable config
00206     spi_CS = 1;                     //high as init for disable SPI
00207 
00208     spi.format(16, 3);              //byte width, spi mode
00209     spi.frequency(1000000);         //8MHz
00210 }
00211 
00212 void A1333_reset(void)              //restart target
00213 {
00214     // write in special
00215     spi_CS = 0;
00216     spi.write( (((CTRL|WMASK)<<8U)&0xFF00) | 0b111 );   //CTRL.SPECIAL <= 0b111
00217     spi_CS = 1;
00218     // write in INITIATE_SPECIAL
00219     spi_CS = 0;
00220     spi.write( ((((CTRL+1U)|WMASK)<<8U)&0xFF00) | 0x5A );   //CTRL.INITIATE_SPECIAL <= 0x5A
00221     spi_CS = 1;
00222 }
00223 
00224 void A1333_writeEEP(int X_E, int32_t DataH, int32_t DataL)
00225 {
00226     uint16_t tmp;
00227 
00228 //Read EEPROM of X_E
00229     //Prepare eeprom
00230     spi_CS = 0;
00231     spi.write( ((((EWA+1U)|WMASK)<<8U)&0xFF00) | X_E );
00232     spi_CS = 1;
00233 //Push in data
00234     spi_CS = 0;
00235     spi.write( (((EWDH|WMASK)<<8U)&0xFF00) | ((DataH>>8U)&0xFF) );
00236     spi_CS = 1;
00237     spi_CS = 0;
00238     spi.write( ((((EWDH+1U)|WMASK)<<8U)&0xFF00) | (DataH&0xFF) );
00239     spi_CS = 1;
00240     spi_CS = 0;
00241     spi.write( (((EWDL|WMASK)<<8U)&0xFF00) | ((DataL>>8U)&0xFF) );
00242     spi_CS = 1;
00243     spi_CS = 0;
00244     spi.write( ((((EWDL+1U)|WMASK)<<8U)&0xFF00) | (DataL&0xFF) );
00245     spi_CS = 1;
00246 
00247 //Start EEP writing
00248     spi_CS = 0;
00249     spi.write( (((EWCS|WMASK)<<8U)&0xFF00) | 0x80 );
00250     spi_CS = 1;
00251     tmp = 0x0000;
00252     while((tmp & 0x0001) != 0x0001) {
00253         spi_CS = 0;
00254         spi.write( (EWCS << 8U) & 0xFF00 );
00255         spi_CS = 1;
00256         spi_CS = 0;
00257         tmp = spi.write(0x0000);
00258         spi_CS = 1;
00259     }
00260 }
00261 
00262 void A1333_getstate(void)               //read IMU data give raw data
00263 {
00264     uint16_t tmp;
00265 
00266     //Read temperature as state check
00267     spi_CS = 0;                         //start spi talking
00268     spi.write( (TSEN << 8U) & 0xFF00 ); //read mask: RMASK = 0, no need
00269     spi_CS = 1;
00270     spi_CS = 0;
00271     tmp = spi.write(0x0000);
00272     spi_CS = 1;                         //end spi talking
00273     if ((tmp & 0xF000) != 0xF000) {
00274         pc.printf("Temperature read id mismatch\n");
00275     } else {
00276         pc.printf("TSEN is: %04X\n", tmp);
00277         int16_t tmps = tmp<<4;
00278         float tmpf = tmps/64.0f + 25.0f;
00279         pc.printf("Temperature is: %.2f\n", tmpf);
00280     }
00281 
00282     //Read error check
00283     spi_CS = 0;                         //start spi talking Wx
00284     spi.write( (ERR << 8U) & 0xFF00 );
00285     spi_CS = 1;
00286     spi_CS = 0;
00287     tmp = spi.write(0x0000);
00288     spi_CS = 1;                         //end spi talking
00289     if ((tmp & 0xF000) != 0xA000) {
00290         pc.printf("Error read id mismatch\n");
00291     } else {
00292         pc.printf("ERR is: %04X\n", tmp);
00293     }
00294 
00295     //Read status check
00296     spi_CS = 0;                    //start spi talking Wx
00297     spi.write( (STA << 8U) & 0xFF00 );
00298     spi_CS = 1;
00299     spi_CS = 0;
00300     tmp = spi.write(0x0000);
00301     spi_CS = 1;                    //end spi talking
00302     if ((tmp & 0xF000) != 0x8000) {
00303         pc.printf("State read id mismatch\n");
00304     } else {
00305         pc.printf("STA is: %04X\n", tmp);
00306     }
00307 }
00308 
00309 void A1333_ulkEEPROM(void)
00310 {
00311     uint16_t tmp;
00312 
00313     //read key lock
00314     spi_CS = 0;                    //start spi talking Wx
00315     spi.write( (IKEY << 8U) & 0xFF00 );
00316     spi_CS = 1;
00317     spi_CS = 0;
00318     tmp = spi.write(0x0000);
00319     spi_CS = 1;
00320     pc.printf("IKEY before unlock is: %04X\n", tmp);
00321 
00322     //Now try to unlock EEPROM write
00323     //write in key
00324     spi_CS = 0;
00325     spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x00 );
00326     spi_CS = 1;
00327     spi_CS = 0;
00328     spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x27 );
00329     spi_CS = 1;
00330     spi_CS = 0;
00331     spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x81 );
00332     spi_CS = 1;
00333     spi_CS = 0;
00334     spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x1F );
00335     spi_CS = 1;
00336     spi_CS = 0;
00337     spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x77 );
00338     spi_CS = 1;
00339 
00340     //read key lock again after unlock
00341     spi_CS = 0;                    //start spi talking Wx
00342     spi.write( (IKEY << 8U) & 0xFF00 );
00343     spi_CS = 1;
00344     spi_CS = 0;
00345     tmp = spi.write(0x0000);
00346     spi_CS = 1;
00347     pc.printf("IKEY is: %04X\n", tmp);
00348     if ((tmp & 0x00FF) != 0x0001) {
00349         pc.printf("Device fail to unlock\n");
00350     } else {
00351         pc.printf("Device unlocked\n");
00352     }
00353 }
00354 
00355 void A1333_getEEP(int X_E)
00356 {
00357     uint16_t tmp;
00358 
00359     //Read eeprom
00360     spi_CS = 0;
00361     spi.write( ((((ERA+1U)|WMASK)<<8U)&0xFF00) | X_E );
00362     spi_CS = 1;
00363     spi_CS = 0;
00364     spi.write( (((ERCS|WMASK)<<8U)&0xFF00) | 0x80 );
00365     spi_CS = 1;
00366     tmp = 0x0000;
00367     while((tmp & 0x0001) != 0x0001) {
00368         spi_CS = 0;
00369         spi.write( (ERCS << 8U) & 0xFF00 );
00370         spi_CS = 1;
00371         spi_CS = 0;
00372         tmp = spi.write(0x0000);
00373         spi_CS = 1;
00374     }
00375     spi_CS = 0;
00376     spi.write( (ERDH << 8U) & 0xFF00 );
00377     spi_CS = 1;
00378     spi_CS = 0;
00379     tmp = spi.write( (ERDL << 8U) & 0xFF00 ) & 0xFF;
00380     spi_CS = 1;
00381     pc.printf("ERDH/ERDL: %02X", tmp);
00382 
00383     spi_CS = 0;
00384     tmp = spi.write(0x0000);
00385     spi_CS = 1;
00386     pc.printf("%04X\n", tmp);
00387 }