SHENG-HEN HSIEH
/
A1333_A1339_spi_master
simple, stable version
Diff: main.cpp
- Revision:
- 1:ec8b98cf0bed
- Parent:
- 0:d35231825f42
--- a/main.cpp Fri Dec 27 14:43:04 2019 +0000 +++ b/main.cpp Tue May 18 15:35:19 2021 +0000 @@ -4,7 +4,13 @@ #define pi 3.141592f #define d2r 0.01745329f #define NN 1 -#define Rms 1000000 //TT rate +#define Rms 1000000 // TT rate + +//choose if write in configurations +#define WRCONFIG 1 // 1 to write in configurations + +//choose if A1339 extended settings +#define A1339EXT 0 // 1 to enable A1339 extended settings //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓GPIO registor↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓// //~~~structure~~~// @@ -27,10 +33,7 @@ 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↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑// @@ -39,7 +42,8 @@ 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_init(); //initialize shit +void A1333_reset(); //restart target 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 @@ -54,10 +58,16 @@ { init_IO(); //initialized value A1333_init(); - wait_ms(100); + + pc.printf("\n\n"); pc.printf("Welcome to A1333/A1339 configurator\n"); + pc.printf("Resetting target...\n"); + A1333_reset(); + wait_ms(200); - init_TIMER(); //start TT_main +#if A1339EXT + pc.printf("A1339 extended settings are enable!\n"); +#endif A1333_getstate(); pc.printf("\n"); @@ -94,16 +104,30 @@ A1333_ulkEEPROM(); pc.printf("\n"); +#if WRCONFIG + pc.printf("Start writting configurations!\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 + 00000001, 0, 1, 0, 1, 1, 1, 0, 0100, + 250ns no, yes, 0+R, yes, yes, yes, ABI, 1024pulse/mechRev,(Notice ABx4 res is *4 at last) + result to 0x0001, 0x10E4 Run follow to write in */ -// A1333_writeEEP(ABI_E, 0x0001, 0x10A4); -// pc.printf("ABI_E write done\n"); -// pc.printf("\n"); + A1333_writeEEP(ABI_E, 0x01, 0x10E4); + pc.printf("ABI_E write done\n"); + pc.printf("\n"); + + /* Configuration sectionfor PWI_EEPROM settings "A1333/A1339" + PEN, BAND, FREQ, PHE, PEO, PES, DM, S17, SC, + [23], [22:20], [19:16], [14], [13], [12], [3], [1], [0], + 1, 0, 0, 0, 0, 0, 0, 0, 0, + yes, 3125Hz, no, no, no, no, no, no, + result to 0x0080, 0x0000 + Run follow to write in */ + A1333_writeEEP(PWI_E, 0x80, 0x0000); + pc.printf("PWI_E write done\n"); + pc.printf("\n"); /* Configuration sectionfor ANG_EEPROM settings "A1333/A1339" ORATE, RD, RO, HYSTERESIS, ZERO_OFFSET, @@ -112,10 +136,11 @@ 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"); + A1333_writeEEP(ANG_E, 0x02, 0x8000); + pc.printf("ANG_E write done\n"); + pc.printf("\n"); +#if A1339EXT /* Configuration sectionfor LPC_EEPROM settings "A1339 only" T45, TPMD, LPMD, LPM_CYCLE_TIME, LPM_WAKE_THRESHOLD, [23], [21], [20], [17:12], [10:0], @@ -123,9 +148,10 @@ 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"); + A1333_writeEEP(LPC_E, 0xB0, 0xB29F); //A1339 only + pc.printf("LPC_E write done\n"); + pc.printf("\n"); +#endif //A1339EXT /* Configuration sectionfor COM_EEPROM settings "A1333/A1339" LOCK, LBE, CSE, DST, DHR, MAG_THRES_HI, MAG_THRES_LO, @@ -134,12 +160,16 @@ 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"); + A1333_writeEEP(COM_E, 0x0C, 0x094D); + pc.printf("COM_E write done\n"); + pc.printf("\n"); + +#endif //WRCONFIG pc.printf("End of A1333/A1339 configurator\n"); + init_TIMER(); //start TT_main + while(1) { //main() loop if(Count >= NN) { //check if main working Count=0; @@ -170,7 +200,7 @@ } -void A1333_init(void) //initialize +void A1333_init(void) //initialize { //gloable config spi_CS = 1; //high as init for disable SPI @@ -179,8 +209,22 @@ spi.frequency(1000000); //8MHz } +void A1333_reset(void) //restart target +{ + // write in special + spi_CS = 0; + spi.write( (((CTRL|WMASK)<<8U)&0xFF00) | 0b111 ); //CTRL.SPECIAL <= 0b111 + spi_CS = 1; + // write in INITIATE_SPECIAL + spi_CS = 0; + spi.write( ((((CTRL+1U)|WMASK)<<8U)&0xFF00) | 0x5A ); //CTRL.INITIATE_SPECIAL <= 0x5A + spi_CS = 1; +} + void A1333_writeEEP(int X_E, int32_t DataH, int32_t DataL) { + uint16_t tmp; + //Read EEPROM of X_E //Prepare eeprom spi_CS = 0; @@ -204,31 +248,35 @@ spi_CS = 0; spi.write( (((EWCS|WMASK)<<8U)&0xFF00) | 0x80 ); spi_CS = 1; - Buff = 0x0000; - while((Buff & 0x0001) != 0x0001) { + tmp = 0x0000; + while((tmp & 0x0001) != 0x0001) { spi_CS = 0; spi.write( (EWCS << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; - Buff = spi.write(0x0000); + tmp = spi.write(0x0000); spi_CS = 1; } } void A1333_getstate(void) //read IMU data give raw data { + uint16_t tmp; + //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); + tmp = spi.write(0x0000); spi_CS = 1; //end spi talking - if ((Buff & 0xF000) != 0xF000) { - pc.printf("Temperature read id mismatch"); + if ((tmp & 0xF000) != 0xF000) { + pc.printf("Temperature read id mismatch\n"); } else { - temp = ((float)(Buff & 0x0FFF)/8.0f) + 25.0f; - pc.printf("Temperature is now: %.2f\n", temp); + pc.printf("TSEN is: %04X\n", tmp); + int16_t tmps = tmp<<4; + float tmpf = tmps/64.0f + 25.0f; + pc.printf("Temperature is: %.2f\n", tmpf); } //Read error check @@ -236,30 +284,40 @@ spi.write( (ERR << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; - Buff = spi.write(0x0000); + tmp = spi.write(0x0000); spi_CS = 1; //end spi talking - pc.printf("Error is: %04X\n", Buff); + if ((tmp & 0xF000) != 0xA000) { + pc.printf("Error read id mismatch\n"); + } else { + pc.printf("ERR is: %04X\n", tmp); + } //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); + tmp = spi.write(0x0000); spi_CS = 1; //end spi talking - pc.printf("State is: %04X\n", Buff); + if ((tmp & 0xF000) != 0x8000) { + pc.printf("State read id mismatch\n"); + } else { + pc.printf("STA is: %04X\n", tmp); + } } void A1333_ulkEEPROM(void) { + uint16_t tmp; + //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); + tmp = spi.write(0x0000); spi_CS = 1; - pc.printf("IKEY before unlock is: %04X\n", Buff); + pc.printf("IKEY before unlock is: %04X\n", tmp); //Now try to unlock EEPROM write //write in key @@ -284,10 +342,10 @@ spi.write( (IKEY << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; - Buff = spi.write(0x0000); + tmp = spi.write(0x0000); spi_CS = 1; - pc.printf("IKEY is: %04X\n", Buff); - if ((Buff & 0x0001) != 0x0001) { + pc.printf("IKEY is: %04X\n", tmp); + if ((tmp & 0x00FF) != 0x0001) { pc.printf("Device fail to unlock\n"); } else { pc.printf("Device unlocked\n"); @@ -296,6 +354,8 @@ void A1333_getEEP(int X_E) { + uint16_t tmp; + //Read eeprom spi_CS = 0; spi.write( ((((ERA+1U)|WMASK)<<8U)&0xFF00) | X_E ); @@ -303,25 +363,25 @@ spi_CS = 0; spi.write( (((ERCS|WMASK)<<8U)&0xFF00) | 0x80 ); spi_CS = 1; - Buff = 0x0000; - while((Buff & 0x0001) != 0x0001) { + tmp = 0x0000; + while((tmp & 0x0001) != 0x0001) { spi_CS = 0; spi.write( (ERCS << 8U) & 0xFF00 ); spi_CS = 1; spi_CS = 0; - Buff = spi.write(0x0000); + tmp = 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 ); + tmp = spi.write( (ERDL << 8U) & 0xFF00 ) & 0xFF; spi_CS = 1; - pc.printf("ERDH: %04X\n", Buff); + pc.printf("ERDH/ERDL: %02X", tmp); spi_CS = 0; - Buff2 = spi.write(0x0000); + tmp = spi.write(0x0000); spi_CS = 1; - pc.printf("ERDL: %04X\n", Buff2); + pc.printf("%04X\n", tmp); } \ No newline at end of file