// Includes #include // Sensor definitions #define SENSOR_WRITE_ADDRESS 0xBE #define SENSOR_READ_ADDRESS 0xBF #define SENSOR_RESET_ADDRESS 0x00 typedef enum SENSOR_REG_ADDRESS { X_UPPER_OFFSET = 0x00, X_LOWER_OFFSET = 0x04, Y_UPPER_OFFSET = 0x01, Y_LOWER_OFFSET = 0x04, Z_UPPER_OFFSET = 0x02, Z_LOWER_OFFSET = 0x05, T_UPPER_OFFSET = 0x03, T_LOWER_OFFSET = 0x05, X_LEVEL_LOWER = 0x07, X_LEVEL_UPPER = 0x08, Y_LEVEL_LOWER = 0x09, Y_LEVEL_UPPER = 0x0A, Z_LEVEL_LOWER = 0x0B, Z_LEVEL_UPPER = 0x0C, HW_VERSION_OFFSET = 0x16 } SENSOR_REG_ADDRESS_t; // Global flags structure typedef struct globalFlags { uint8_t txComplete : 1; uint8_t rxComplete : 1; uint8_t nackReceived : 1; uint8_t arbitrationLost : 1; uint8_t errorDetected : 1; uint8_t unusedBits : 3; } globalFlags_t; volatile globalFlags_t globalFlags; // Prototypes void delay(uint32_t cnt); void clearGlobalFlags(void); void twiEndOfTransmit(void); void twiEndOfReceive(void); void twiNackReceived(void); void twiArbitrationLost(void); void twiErrorDetected(void); int main(void) { DAVE_STATUS_t status; status = DAVE_Init(); /* Initialization of DAVE Apps */ if(status == DAVE_STATUS_SUCCESS) { XMC_DEBUG("DAVE Apps initialization success\n"); } else { XMC_DEBUG(("DAVE Apps initialization failed with status %d\n", status)); while(1); } // Variables uint8_t i2c_data = 0; uint8_t vector[10]; for(i2c_data = 0; i2c_data < 10; i2c_data++) vector[i2c_data] = 0; // Variables initialization clearGlobalFlags(); // Main loop while(1) { // Sending a data to X_LEVEL_UPPER register i2c_data = X_LEVEL_UPPER; I2C_MASTER_Init(&I2C_MASTER_0); // if you don't force an init everytime before a communication the protocol doesn't work atleast one time I2C_MASTER_Transmit(&I2C_MASTER_0,true,SENSOR_WRITE_ADDRESS,&i2c_data,1,false); while(!globalFlags.txComplete & !globalFlags.rxComplete & !globalFlags.nackReceived & !globalFlags.arbitrationLost & !globalFlags.errorDetected); clearGlobalFlags(); i2c_data = 0x50; // any value, just to test if the I2C is working as expected I2C_MASTER_Transmit(&I2C_MASTER_0,false,SENSOR_WRITE_ADDRESS,&i2c_data,1,true); while(!globalFlags.txComplete & !globalFlags.rxComplete & !globalFlags.nackReceived & !globalFlags.arbitrationLost & !globalFlags.errorDetected); clearGlobalFlags(); // Reading I2C_MASTER_Init(&I2C_MASTER_0); // if you don't force an init everytime before a communication the protocol doesn't work atleast one time I2C_MASTER_Receive(&I2C_MASTER_0,true,SENSOR_READ_ADDRESS,&vector[0],6,true,true); while(!globalFlags.txComplete & !globalFlags.rxComplete & !globalFlags.nackReceived & !globalFlags.arbitrationLost & !globalFlags.errorDetected); clearGlobalFlags(); } return 1; } // Functions void delay(uint32_t cnt) { volatile uint32_t count = cnt; while(--count); } void clearGlobalFlags(void) { globalFlags.rxComplete = 0; globalFlags.txComplete = 0; globalFlags.nackReceived = 0; globalFlags.arbitrationLost = 0; globalFlags.errorDetected = 0; globalFlags.unusedBits = 0; } // I2C Interrupts void twiEndOfTransmit(void) { globalFlags.txComplete = 1; } void twiEndOfReceive(void) { globalFlags.rxComplete = 1; } void twiNackReceived(void) { globalFlags.nackReceived = 1; } void twiArbitrationLost(void) { globalFlags.arbitrationLost = 1; } void twiErrorDetected(void) { globalFlags.errorDetected = 1; }