Simultaneous host and client mode on device with single TWI, aka DUALCTRL?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 time lurker, new to the AVR / X series chips.

 

I'm working on a modular battery monitoring project that requires a the uC to be both an I2C HOST to read from a local ADC plus several other sensors and at the same time, respond as an I2C CLIENT to a central controlling uC.

 

For cost, size and the limits of my hand assembly abilities, I'd like to use a DA28 series chip, or something smaller. Reading the datasheet, it looks like the TWI dual mode feature is just what I need, simultaneous host and client, using different pins, with a single TWI peripheral.

 

Only, I can't seem to get it going... I am using the DxCORE Arduino framework, and have my host connected on PA2 & PA3 for the client role, and hope to use PC2 & PC3 for the host role, connected to sensor clients. The chip can't see local devices, and the host can't see my chip.

 

I had the client side working great on a good old 328, but now new chip, new headache.

 

I'm taking a break from it right now, I will post the code I'm using in setup() shortly. It goes pretty much like this:

 

#define DUALCTRL // Need this or the Dual-mode function is hidden for some reason 

 

Wire.dualMode(false); // for whatever reason FM+ is also set using this call, false disables FM+ from reading the .h files

 

Wire.begin();

 

Wire.setClock(100000); // 100khz

 

Wire.begin(0x30); // client address 

Last Edited: Mon. Aug 8, 2022 - 08:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  •  
    // Setup TWI0 for dual mode ... TWI_MANDS_SINGLE
        Wire.enableDualMode(false); // enable fmp+ is false
        Wire.begin(); // setup host
        Wire.begin(I2C_CLIENT_ADDR, false); // setup client with address, ignore broadcast
        Wire.setClock(100000);
      

     

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

and have my host connected on PA2 & PA3 for the client role, and hope to use PC2 & PC3 for the host role, connected to sensor clients. 

For TWI0 in dual mode (28pin da)- PA2/3 is host pins, PC2/3 is client pins.

 

 

This can be used as alternate code to test if wanted-

https://github.com/cv007/Avr01Dx...

 

Its not setup for dual mode, but can be used with a slight change for the 28 pin da-

Dual mode has to be set somewhere-  TWI0.DUALCTRL = 1;

and if external pullups are used, nothing more needs to be done (twi has full control of the pins, but does not enable port pullup).

 

If internal pullups are wanted, then the host will use  twim_defaultPins();   and the client   twis_altPins();

but since the altPins function also sets portmux to alt2, you will need to set it back to default afterwards-

    TWI_PORTMUX_DEFAULT();

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For TWI0 in dual mode (28pin da)- PA2/3 is host pins, PC2/3 is client pins.

Hey this fixed half the problem, thank you! My chips now see their local i2c clients, but still no luck getting the upstream supervisor to see my chips as clients.

 

I took my setup code and compiled it using the Arduino IDE 1.8.19, and it works as expected. So something is not getting setup correctly in my PlatformIO environment. 

 

Working Arduino code uploaded to Github

 

I suspect something in the boards.txt or platform.txt needs to be implemented in my platformio.ini file, so the code compiles the same. 

https://github.com/SpenceKonde/D...

https://github.com/SpenceKonde/D...

 

I implemented the -DTWI_MANDS_SINGLE flag but that didn't help.