simple, stable version
Revision 0:d35231825f42, committed 2019-12-27
- Comitter:
- open4416
- Date:
- Fri Dec 27 14:43:04 2019 +0000
- Child:
- 1:ec8b98cf0bed
- Commit message:
- Initial release, works fine
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/A1333_PoOPoO.h Fri Dec 27 14:43:04 2019 +0000 @@ -0,0 +1,95 @@ +#ifndef A1333_PoOPoO_H +#define A1333_PoOPoO_H + +// A1333 Serial command Registers // + +#define WMASK 0x40 // write mask +#define RMASK 0x00 // read mask + +#define EWA 0x02 //r/w +#define EWDH 0x04 //r/w +#define EWDL 0x06 //r/w +#define EWCS 0x08 //ro/wo +#define ERA 0x0A //r/w +#define ERCS 0x0C //ro/wo +#define ERDH 0x0E //r/w +#define ERDL 0x10 //r/w + +#define CTRL 0x1E //rw/wo +#define ANG 0x20 //ro +#define STA 0x22 //ro +#define ERR 0x24 //ro +#define WARN 0x26 //ro +#define TSEN 0x28 //ro +#define FIELD 0x2A //ro +#define TURNS 0x2C //ro + +#define HANG 0x30 //ro +#define ANG15 0x32 //ro +#define ZANG 0x34 //ro + +#define IKEY 0x3C //ro/wo + + +// A1333 EEPROM command Registers // +#define PWE_E 0x18 +#define ABI_E 0x19 +#define MSK_E 0x1A +#define PWI_E 0x1B +#define ANG_E 0x1C +#define LPC_E 0x1D +#define COM_E 0x1E +#define CUS_E 0x1F //costume + +// A1333 EEPROM shadow Registers // +#define PWE_S 0x58 +#define ABI_S 0x59 +#define MSK_S 0x5A +#define PWI_S 0x5B +#define ANG_S 0x5C +#define LPC_S 0x5D +#define COM_S 0x5E + + +#endif //LSM9DS0_SH_H + +/* Typical readout (break out) +Welcome to A1333/A1339 configurator +Temperature is now: 28.62 +Error is: A0CB +State is: 8090 + +Read PWE_E +ERDH: D000 +ERDL: 0FDF + +Read ABI_E +ERDH: 9402 +ERDL: 10A4 + +Read MSK_E +ERDH: A490 +ERDL: 1900 + +Read PWI_E +ERDH: B0C2 +ERDL: 710A + +Read ANG_E +ERDH: 8802 +ERDL: 81D6 + +Read LPC_E +ERDH: 9480 +ERDL: B29F + +Read COM_E +ERDH: 500C +ERDL: 094D + +IKEY before unlock is: 0000 +IKEY is: 0001 +Device unlocked + +End of A1333/A1339 configurator +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Dec 27 14:43:04 2019 +0000
@@ -0,0 +1,327 @@
+#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);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Dec 27 14:43:04 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
SHENG-HEN HSIEH