Go Down

Topic: Sketch crash.... i2c write from progmem (I think) (Read 70 times) previous topic - next topic

ve03vsx

Sep 14, 2016, 10:00 pm Last Edit: Sep 14, 2016, 10:07 pm by ve03vsx
Hi I was hoping some one could help me out with a programming question. I have a sketch that I writing for a project containing an MPU 6050 (6 axis access/gyro). The problem is that the setup() crashes.

The sketch crashes at the same point every time just before a large write to the MPU6050 volatile memory. I think it is caused by the way I am reading from the Arduino's Program memory to get the data which I pass to the MPU6050.

I have tried to debug the sketch the best I can. Another infuriating point is that the crash only occurs approx 1 in 8 times.

I could paste the whole sketch here and will if needed but most of it is regarding the MPU6050 which if you don't have I don't think you would be able to get the sketch to run. So I have posted the functions I am using to write and read data.
Used to write data to the MPU Volatile Memory
Code: [Select]

////////////////WRITE DMP DATA////////////////
void mpuDMPData () {
  bool endLoop = true;
  bool loadFault = false;
  byte bankCnt = 0;
  i2cWrite(MPU_ADD, MPU_MEM_BNK, 0x00);
  i2cWrite(MPU_ADD, MPU_MEM_ADD, 0x00);

  //Load bank 0
  //This works great - Not validated load though
  for (byte byteCnt = 0; endLoop == true; byteCnt ++) {
    i2cWrite(MPU_ADD, MPU_RW_DATA, pgm_read_word(&(dmpData[bankCnt][byteCnt])));
    if (byteCnt == 255) {
      Serial.print("Writing bank :");
      Serial.println(bankCnt);
      bankCnt++;
      i2cWrite(MPU_ADD, MPU_MEM_BNK, 0x00 + bankCnt);
    }
    if (bankCnt == 7 && byteCnt == 136) {
      endLoop = false;     
    }   
  }
}


i2cWrite function
Code: [Select]

/////////////////////WRITE BYTE////////////////////
void i2cWrite (byte i2cAdd, byte i2cReg, byte i2cData) {
  /////START CONDITION////
  TWCR = B10100100; //(TWINT)(TWSTA)(TWEN) - Set START condition
  while (!(TWCR & B10000000)) { //Wait for TWI to set TWINT
  }
  /////SLA + W////
  TWDR = i2cAdd << 1; //load address to Atmega i2c buffer, set R/W bit
  TWCR = B10000100; //(TWINT)(TWEN) - SLA+W
  while (!(TWCR & B10000000)) {
  }
  /////REG////
  TWDR = i2cReg;
  TWCR = B10000100; //(TWINT)(TWEN) - Send Reg
  while (!(TWCR & B10000000)) {
  }
  /////DATA/////
  TWDR = i2cData;
  TWCR = B10000100; //(TWINT)(TWEN) - Send Data
  while (!(TWCR & B10000000)) {
  }
  /////STOP and SET//////
  TWCR = B10010100;
  TWCR = B11000101;
}


i2cRead Function
Code: [Select]

/////////////////////READ BYTE////////////////////
byte i2cRead(byte i2cAdd, byte i2cReg) {
  byte result;
  /////START CONDITION////
  TWCR = B10100100; //(TWINT)(TWSTA)(TWEN) - Set START condition
  while (!(TWCR & B10000000)) { //Wait for TWI to set TWINT
  }
  /////SLA + W////
  TWDR = (i2cAdd << 1); //load address to Atmega i2c buffer, set R/W bit
  TWCR = B10000100; //(TWINT)(TWEN) - SLA+R
  while (!(TWCR & B10000000)) {
  }
  /////REG////
  TWDR = i2cReg;
  TWCR = B10000100; //(TWINT)(TWEN) - Send Reg
  while (!(TWCR & B10000000)) {
  }

  ///////REPEATED START/////////
  TWCR = B10100100; //(TWINT)(TWSTA)(TWEN)
  while (!(TWCR & (1 << TWINT))) {
  }
  /////SLA + R////
  TWDR = (i2cAdd << 1) | (B00000001); //load address to Atmega i2c buffer, set R/W bit
  TWCR = B10000100; //(TWINT)(TWEN) - SLA+R
  while (!(TWCR & B10000000)) {
  }
  /////////REQUEST DATA///////////
  TWCR = B10000100;//(TWINT)(TWEN)
  while (!(TWCR & (1 << TWINT))) {
  }
  result = TWDR;
  /////STOP and SET//////
  TWCR = B10010100;
  TWCR = B11000101;

  return result;
}


Any help really appricated
D

vaj4088

ve03vsx wrote (in part):

Quote
I could paste the whole sketch here and will if needed but most of it is regarding the MPU6050 which if you don't have I don't think you would be able to get the sketch to run.
While this is true, it usually helps us to have complete information.  For example, we cannot see how the variable dmpData was declared.
Please post the whole sketch.

Crashing only 1 out of 8 times is rarely a programming problem unless something is not initialized.

It would also help to know what you mean by "... setup() crashes."


Go Up