L476RG: I2C slave mode and serialUSB not working

Discussions about the STM32generic core
Post Reply
gato_
Posts: 16
Joined: Mon Apr 03, 2017 10:18 am

L476RG: I2C slave mode and serialUSB not working

Post by gato_ » Thu Dec 14, 2017 11:49 am

I implemented a custom L476RG board. Tested Blink, tested USART1- 3, I2C1-3, and everything works fine. But:
-SerialUSB is not working at all
-Slave onRequest event doesn`t seem to execute at all
checked a variant of i2cScanner:

Code: Select all

// -----------------------------------------------------
// i2c_scanner
//
// from // http://playground.arduino.cc/Main/I2cScanner
//
// This version is addapted to the STM32GENERIC library used 
// with a
// STM32F407 Discovery Board
// Some I2C devices have to be enabled first by digitalWrite
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//


#include <Wire.h>

#define SERIALINTERFACE SerialUART2

TwoWire Wire1=TwoWire(I2C3,PC1,PC0);


void setup()
{
  Wire.begin();
  Wire1.begin(3);
  SERIALINTERFACE.begin(9600);
  delay(5000);
  SERIALINTERFACE.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  SERIALINTERFACE.println("Scanning...");
  delay(2000);

  nDevices = 0;
  for (address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    Wire.write(0);
    error = Wire.endTransmission();

    if (error == 0)
    {
      SERIALINTERFACE.print("I2C device found at address 0x");
      if (address < 16)  SERIALINTERFACE.print("0");
      SERIALINTERFACE.print(address, HEX);
      SERIALINTERFACE.println("  !");

      nDevices++;

    }
    else if (error == 4)
    {
      /*SERIALINTERFACE.print("no device found at address 0x");
      if (address < 16) SERIALINTERFACE.print("0");
      SERIALINTERFACE.println(address, HEX);*/
    }
  }
  if (nDevices == 0)
  {
    SERIALINTERFACE.println("No I2C devices found\n");
    SERIALINTERFACE.println("Did you configure the chip select for your device?\n");
  }
  else
    SERIALINTERFACE.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}
showing address 3 as detected

Code: Select all

Scanning...
I2C device found at address 0x03  !
done
but executing the following code just throws FF FF FF FF FF FF, implying there is no response from the slave port (SCL and SDA from Wire and Wire1 instances having being tied)

Code: Select all

#include <Wire.h>

#define SERIALINTERFACE SerialUART2

const int N=6;
byte buffer[N];
TwoWire Wire1=TwoWire(I2C3,PC1,PC0);

void setup()
{
  for (int i=0;i<N;i++){buffer[i]=i+1;};
  Wire.begin();
  Wire1.begin(3);
  Wire1.onRequest(requestEvent); // register event
  Wire1.onReceive(receiveEvent);
  SERIALINTERFACE.begin(9600);
  delay(1000);
}

void loop()
{
   Wire.requestFrom(3, N);
  for (int i=0;i<N;i++) {
    uint8_t c = Wire.read();
    SERIALINTERFACE.print(c,HEX);SERIALINTERFACE.print(" ");
  }
  SERIALINTERFACE.println();
  delay(500);  
}

void requestEvent() {
  for (int i=0;i<N;i++){Wire1.write(0);} // respond with message of 6 bytes
  //SERIALINTERFACE.println("wrote 6 bytes");
}
void receiveEvent(int howMany){
String param="";
if (howMany>0){
  for (int i=1;i<howMany;i++){param+=char(Wire1.read());}
    } else {param="";}
}
I tried to use GDB to attach a breakpoint to TwoWire::OnrequestService, but it never seems to be executed

gato_
Posts: 16
Joined: Mon Apr 03, 2017 10:18 am

Re: L476RG: I2C slave mode and serialUSB not working

Post by gato_ » Thu Dec 14, 2017 12:32 pm

just noticed

Code: Select all

//TODO I2C slave mode for chips that use something else than I2C_IT_BUF
in Wire.cpp:1 .....
Is there no WORKING implementation of the slave mode anywhere? tried GrumpyOldPizza's core as well, with no success....

Post Reply