SHENG-HEN HSIEH
/
A1333_A1339_spi_master
simple, stable version
main.cpp
- Committer:
- open4416
- Date:
- 2019-12-27
- Revision:
- 0:d35231825f42
- Child:
- 1:ec8b98cf0bed
File content as of revision 0:d35231825f42:
#include "mbed.h" #include "A1333_PoOPoO.h" #define pi 3.141592f #define d2r 0.01745329f #define NN 1 #define Rms 1000000 //TT rate //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓GPIO registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// //~~~structure~~~// DigitalOut led(D13); //detection DigitalOut TT_ext(D12); //~~~SPI~~~// DigitalOut spi_CS(D6,1); //low for ACC/MAG enable SPI spi(D4, D5, D3); //MOSI MISO SCLK //~~~Serial~~~// Serial pc(D1, D0); //Serial reg(TX RX) //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of GPIO registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Varible registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// //~~~globle~~~// Ticker TT; //call a timer int Count = 0; //one second counter for extrenal led blink //~~~A1333_data~~~// uint16_t Buff = 0x00; uint16_t Buff2 = 0x00; float temp = 0.0f; //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Varible registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Function registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// void init_TIMER(); //set TT_main() rate void TT_main(); //timebase function rated by TT void init_IO(); //initialize IO state void A1333_init(); //initialize IMU void A1333_getstate(); //get current state & errors void A1333_ulkEEPROM(); //unlock protection of EEPROM void A1333_getEEP(int X_E); //read EEPROM of X_E void A1333_writeEEP(int X_E, int32_t DataH, int32_t DataL); //write EEPROM of X_E //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑end of Function registor↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓main funtion↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// int main() { init_IO(); //initialized value A1333_init(); wait_ms(100); pc.printf("Welcome to A1333/A1339 configurator\n"); init_TIMER(); //start TT_main A1333_getstate(); pc.printf("\n"); pc.printf("Read PWE_E\n"); //Doesnt matter for us A1333_getEEP(PWE_E); pc.printf("\n"); pc.printf("Read ABI_E\n"); //Need to be set for application!! A1333_getEEP(ABI_E); pc.printf("\n"); pc.printf("Read MSK_E\n"); //Doesnt matter for us A1333_getEEP(MSK_E); pc.printf("\n"); pc.printf("Read PWI_E\n"); //Doesnt matter for us A1333_getEEP(PWI_E); pc.printf("\n"); pc.printf("Read ANG_E\n"); A1333_getEEP(ANG_E); pc.printf("\n"); pc.printf("Read LPC_E\n"); A1333_getEEP(LPC_E); pc.printf("\n"); pc.printf("Read COM_E\n"); A1333_getEEP(COM_E); pc.printf("\n"); // A1333/A1339 require unlock before eeprom access A1333_ulkEEPROM(); pc.printf("\n"); /* Configuration sectionfor ABI_EEPROM settings "A1333/A1339" ABI_SLEW_TIME, INV, AHE, INDEX_MODE, WDH, PLH, IOE, UVW, RESOLUTION_PAIRS, [21:16], [15], [12], [9:8], [7], [6], [5], [4], [3:0], 00000010, 0, 1, 0, 1, 0, 1, 0, 0100, 500ns no, yes, 0+R, yes, no, yes, ABI, 1024pulse/Rev, result to 0x0001, 0x10A4 Run follow to write in */ // A1333_writeEEP(ABI_E, 0x0001, 0x10A4); // pc.printf("ABI_E write done\n"); // pc.printf("\n"); /* Configuration sectionfor ANG_EEPROM settings "A1333/A1339" ORATE, RD, RO, HYSTERESIS, ZERO_OFFSET, [23:20], [19], [18], [17:12], [11:0], 0000, 0, 0, 101000, 0x000-0x3FF, 1us, no, direction, 20/16383rev, Calibrate in DSM instead, result to 0x0002, 0x8000 Run follow to write in */ // A1333_writeEEP(ANG_E, 0x0002, 0x8000); // pc.printf("ANG_E write done\n"); // pc.printf("\n"); /* Configuration sectionfor LPC_EEPROM settings "A1339 only" T45, TPMD, LPMD, LPM_CYCLE_TIME, LPM_WAKE_THRESHOLD, [23], [21], [20], [17:12], [10:0], 1, 1, 1, 001011, 01010011111, default, no, no, default, default, result to 0x00B0, 0xB29F Run follow to write in */ // A1333_writeEEP(LPC_E, 0x00B0, 0xB29F); //A1339 only // pc.printf("LPC_E write done\n"); // pc.printf("\n"); /* Configuration sectionfor COM_EEPROM settings "A1333/A1339" LOCK, LBE, CSE, DST, DHR, MAG_THRES_HI, MAG_THRES_LO, [23:20], [19], [18], [13], [12], [11:6], [5:0], 0000, 1, 1, 0, 0, 100101, 001101, no, yes, yes, no, no, default, default, result to 0x000C, 0x094D Run follow to write in */ // A1333_writeEEP(COM_E, 0x000C, 0x094D); // pc.printf("COM_E write done\n"); // pc.printf("\n"); pc.printf("End of A1333/A1339 configurator\n"); while(1) { //main() loop if(Count >= NN) { //check if main working Count=0; led = !led; } } } void init_TIMER() //set TT_main{} rate { TT.attach_us(&TT_main, Rms); } void TT_main() //interrupt function by TT { TT_ext = !TT_ext; //indicate TT_main() function working Count = Count+1; //one second counter } void init_IO(void) //initialize { pc.baud(115200); //set baud rate TT_ext = 0; led = 1; } void A1333_init(void) //initialize { //gloable config spi_CS = 1; //high as init for disable SPI spi.format(16, 3); //byte width, spi mode spi.frequency(1000000); //8MHz } void A1333_writeEEP(int X_E, int32_t DataH, int32_t DataL) { //Read EEPROM of X_E //Prepare eeprom spi_CS = 0; spi.write( ((((EWA+1U)|WMASK)<<8U)&0xFF00) | X_E ); spi_CS = 1; //Push in data spi_CS = 0; spi.write( (((EWDH|WMASK)<<8U)&0xFF00) | ((DataH>>8U)&0xFF) ); spi_CS = 1; spi_CS = 0; spi.write( ((((EWDH+1U)|WMASK)<<8U)&0xFF00) | (DataH&0xFF) ); spi_CS = 1; spi_CS = 0; spi.write( (((EWDL|WMASK)<<8U)&0xFF00) | ((DataL>>8U)&0xFF) ); spi_CS = 1; spi_CS = 0; spi.write( ((((EWDL+1U)|WMASK)<<8U)&0xFF00) | (DataL&0xFF) ); spi_CS = 1; //Start EEP writing spi_CS = 0; spi.write( (((EWCS|WMASK)<<8U)&0xFF00) | 0x80 ); spi_CS = 1; Buff = 0x0000; while((Buff & 0x0001) != 0x0001) { spi_CS = 0; spi.write( (EWCS << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; Buff = spi.write(0x0000); spi_CS = 1; } } void A1333_getstate(void) //read IMU data give raw data { //Read temperature as state check spi_CS = 0; //start spi talking spi.write( (TSEN << 8U) & 0xFF00 ); //read mask: RMASK = 0, no need spi_CS = 1; spi_CS = 0; Buff = spi.write(0x0000); spi_CS = 1; //end spi talking if ((Buff & 0xF000) != 0xF000) { pc.printf("Temperature read id mismatch"); } else { temp = ((float)(Buff & 0x0FFF)/8.0f) + 25.0f; pc.printf("Temperature is now: %.2f\n", temp); } //Read error check spi_CS = 0; //start spi talking Wx spi.write( (ERR << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; Buff = spi.write(0x0000); spi_CS = 1; //end spi talking pc.printf("Error is: %04X\n", Buff); //Read status check spi_CS = 0; //start spi talking Wx spi.write( (STA << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; Buff = spi.write(0x0000); spi_CS = 1; //end spi talking pc.printf("State is: %04X\n", Buff); } void A1333_ulkEEPROM(void) { //read key lock spi_CS = 0; //start spi talking Wx spi.write( (IKEY << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; Buff = spi.write(0x0000); spi_CS = 1; pc.printf("IKEY before unlock is: %04X\n", Buff); //Now try to unlock EEPROM write //write in key spi_CS = 0; spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x00 ); spi_CS = 1; spi_CS = 0; spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x27 ); spi_CS = 1; spi_CS = 0; spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x81 ); spi_CS = 1; spi_CS = 0; spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x1F ); spi_CS = 1; spi_CS = 0; spi.write( (((IKEY|WMASK)<<8U)&0xFF00) | 0x77 ); spi_CS = 1; //read key lock again after unlock spi_CS = 0; //start spi talking Wx spi.write( (IKEY << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; Buff = spi.write(0x0000); spi_CS = 1; pc.printf("IKEY is: %04X\n", Buff); if ((Buff & 0x0001) != 0x0001) { pc.printf("Device fail to unlock\n"); } else { pc.printf("Device unlocked\n"); } } void A1333_getEEP(int X_E) { //Read eeprom spi_CS = 0; spi.write( ((((ERA+1U)|WMASK)<<8U)&0xFF00) | X_E ); spi_CS = 1; spi_CS = 0; spi.write( (((ERCS|WMASK)<<8U)&0xFF00) | 0x80 ); spi_CS = 1; Buff = 0x0000; while((Buff & 0x0001) != 0x0001) { spi_CS = 0; spi.write( (ERCS << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; Buff = spi.write(0x0000); spi_CS = 1; } spi_CS = 0; spi.write( (ERDH << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; Buff = spi.write( (ERDL << 8U) & 0xFF00 ); spi_CS = 1; pc.printf("ERDH: %04X\n", Buff); spi_CS = 0; Buff2 = spi.write(0x0000); spi_CS = 1; pc.printf("ERDL: %04X\n", Buff2); }