ArduinoCore-samd icon indicating copy to clipboard operation
ArduinoCore-samd copied to clipboard

ATSAMD21 is pulling ports low when calling wire.onRequest()

Open Rudgas opened this issue 2 years ago • 0 comments

The intent: Using the controller and I2C for turning a port high and low and querying for data from an agent.

The setup: Two Adafruit Itsybitsy M0 Express, but tested on the MKRWIFI1010 as well to same result so this seems to be ATSAMD21 specific. Testing with a NANO EVERY the problem does not occur. Connected via I2C, 4k7 pullups on SDA/SCL. Working example that shows the bug:

Controller:

//CONTROLLER
#include <Wire.h>

uint8_t data[6];

void setup()
{
  Wire.begin(); //start I2C
}

void loop()
{
  enablePort(0x22);
  delay(10);
  fetch(0x22);
  delay(90);
  disablePort(0x22);
  delay(100);
}

void fetch(uint8_t add)
{
  Wire.requestFrom(add,6,1); //address, quantity, bus release
  uint8_t n = 0;
  while(Wire.available())
  {
    data[n] = Wire.read();
    n++;
  }
}

void enablePort(uint8_t port)
{
  uint8_t send_status = 1;
  while(send_status != 0)
  {
    Wire.beginTransmission(port);
    Wire.write(1);
    send_status = Wire.endTransmission();
  }
}

void disablePort(uint8_t port)
{
  uint8_t send_status = 1;
  while(send_status != 0)
  {
    Wire.beginTransmission(port);
    Wire.write(0);
    send_status = Wire.endTransmission();
  }
}

Agent:

//AGENT
#include <Wire.h>

uint8_t buffer[6] = {1,2,3,4,5,6};

void setup()
{
  Wire.begin(0x22);             //join I2C Bus
  Wire.onRequest(sendData);     //what to do when being talked to
  Wire.onReceive(receiveEvent); //what to do with data received

  pinMode(13,OUTPUT);
}

void loop() {}

void sendData()
{
  Wire.write(buffer,6);
}

void receiveEvent(int bytes_incoming)
{
  byte c = Wire.read();
  if(c == 1)
  {
    digitalWrite(13,HIGH);
  }
  else
  {
    digitalWrite(13,LOW);
  }
}

What should happen: D13 (the LED) should alternately go high and low with a 100ms interval.

What actually happens: The Port is pulled high only for 10ms and when the function fetch() is called, the port is immediately pulled low.

Rudgas avatar May 11 '22 12:05 Rudgas