arduino-CAN icon indicating copy to clipboard operation
arduino-CAN copied to clipboard

No Sleep Mode

Open aviatorhh opened this issue 4 years ago • 1 comments

The Sleep Mode (10.2 Sleep Mode) does not work. Mainly it is because of a wrong procedure. The datasheet states to write bits 5-7 (Sleep Mode 001) to the control register (REGISTER 10-1: CANCTRL: CAN CONTROL REGISTER (ADDRESS: XFh)), but the provided function just writes a 0x01 into the register!

WRONG:

int MCP2515Class::sleep()
{
  writeRegister(REG_CANCTRL, 0x01);
  if (readRegister(REG_CANCTRL) != 0x01) {
    return 0;
  }

  return 1;
}

Set bits 5-7 to 001 (Sleep Mode): RIGHT:

int MCP2515Class::sleep() {
  // We are requesting a sleep mode in the control register
  modifyRegister(REG_CANCTRL, B11100000, B00100000);
  return 1;
}

Please refer to the datasheet for the wake up procedures. The device can be brought back to life via an event on the bus or manually by the MCU.

aviatorhh avatar Sep 19 '19 07:09 aviatorhh

@aviatorhh is correct, Sleep Mode doesn't work without his fix, but additionally the read back to verify that sleep is in fact engaged is also incorrect. The sleep function should look something like this:

` #define REG_CANSTAT 0x0e

int MCP2515Class::sleep() { modifyRegister(REG_CANCTRL, B11100000, B00100000); if ((readRegister(REG_CANSTAT) & 0xe0) != 0x20) { return 0; } return 1; } ` MCP2515Class::wakeup() is equally broken as writing a 0x00 to the entire register has side-effects that you may not want and here again you should read back from REG_CANSTAT as defined above (which isn't even in MCP2515.h) to ensure that a wake as occurred.

JasonFarque avatar Jul 08 '20 20:07 JasonFarque