Skip to content

Teensy: Crash when returning a array #252

@Joe7M

Description

@Joe7M

Hi Chris,

I added I2C support to SmallBACIS on Teensy. It works so far quite nice. I use the interactive mode. But I have a problem, that my Teensy 4.1 crashes after some time, when I return the bytes received from a I2C device as an array. Here the source code:

static int cmd_i2c_read(var_s *self, int argc, slib_par_t *args, var_s *retval) {
  uint8_t  address = get_param_int(argc, args, 0,  0);
  uint32_t bytes   = get_param_int(argc, args, 1,  1);
  uint8_t  stop    = get_param_int(argc, args, 2,  1);

  if (address == 0) {
    v_setstr(retval, ERR_PARAM);
    return 0;
  }

  //uint8_t *buffer = new uint8_t[bytes];
  //int interfaceNumber = self->v.m.id;
  //TwoWire *ptrWire;
  //ptrWire = getI2C(interfaceNumber);

  //ptrWire->requestFrom(address, bytes, stop);
  //int len = ptrWire->readBytes(buffer, bytes);

  if(bytes > 1) {
    v_toarray1(retval, bytes);
    for(uint32_t ii = 0; ii < bytes; ii++) {
      //v_setint(v_elem(retval, ii), buffer[ii]);
      v_setint(v_elem(retval, ii), 1);
    }
  }
  else {
    //v_setint(retval, buffer[0]);
    v_setint(retval, 1);
  }

  //delete[] buffer;
  return 1;
}

static int cmd_openi2c(int argc, slib_par_t *args, var_t *retval) {
  uint8_t  interfaceNumber = get_param_int(argc, args, 0,  0);
  if (interfaceNumber > 2) {
    v_setstr(retval, ERR_PARAM);
    return 0;
  }

  map_init(retval);
  v_create_callback(retval, "write", cmd_i2c_write);
  v_create_callback(retval, "read", cmd_i2c_read);
  v_create_callback(retval, "setClock", cmd_i2c_setClock);
  retval->v.m.id = interfaceNumber;

  getI2C(interfaceNumber)->begin();

  return 1;
}

static FuncSpec lib_func[] = {
  {0, 0, "GETTEMP", cmd_get_temperature},
  {0, 0, "GETCPUSPEED", cmd_get_cpu_speed},
  {1, 1, "OPENANALOGINPUT", cmd_openanaloginput},
  {1, 1, "OPENDIGITALINPUT", cmd_opendigitalinput},
  {1, 1, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
  {0, 1, "OPENSERIAL", cmd_openserial},
  {0, 1, "OPENI2C", cmd_openi2c}
};

when calling cmd_i2c_read several thousand times (i.e reading constantly the room temperature with an BMP180), and receiving 2 bytes ( that means, that the ìf(bytes > 1) section is executed, the Teensy crashes. The SmallBASIC program looks like this:

import teensy

const ADDRESS = 0x77

I2C = teensy.OpenI2C(1)    ' Using pins 16 and 17
I2C.setClock(100000)

while(1)
  ii++
  print ii
  a = I2C.read(ADDRESS, 2)
  delay(500)
wend

Here the crash report:

CrashReport:
  A problem occurred at (system time) 0:1:37
  Code was executing from address 0x16FAC
  CFSR: 82
        (DACCVIOL) Data Access Violation
        (MMARVALID) Accessed Address: 0x41
  Temperature inside the chip was 31.85 °C
  Startup CPU clock speed is 600MHz
  Reboot was caused by auto reboot after fault or bad interrupt detected

Do you have an idea why? Am I doing something wrong with returning an array?

Best regards, Joerg

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions