Configuring TWI Communication

  • I'm trying to set up TWI communication. I assume that is what Wire.cpp and Wire.h are for. Does anyone have an example they can show where they configured and used it? I'm not sure what values to put in TWI_ConfigureMaster( , ) or if I need to execute other functions prior to using it.

    Edit: I've used Wire with Arduino successfully before, but it took me a while to get it right. And this Wire library is different, so I'd appreciate any help setting it up.

  • administrators

    There is already code that drives the TWI interface in the source code. it's in the files that interface to DueX expansion boards and external SX1509B devices, in the /src/DuetNG subfolder.

  • Thanks. Two questions:

    1. I notice that there are "slashes" through the DuetNG folder and the files inside it. The Duet folder is not slashed. Is that a problem?

    2. To use it in Gcodes2.cpp, I do the following:
    #include Wire.h"

    then within a custom g-code, I write:

    uint16_t val = ( (int) (5.3 + 0.5)) ; // rounding of a input value (normally a float, but set as 5.3 for testing)
    Wire.beginTransmission(0x80); // address of TWI device
    Wire.write(0x30); // tells device what to do with the next incoming byte
    Wire.write(val/16); // upper data bits
    Wire.write((val%16) << 4); // lower data bits

    I get no error when compiling. But I do not get any response from the TWI device as expected. I had identical code in Arduino when I had it working there. Is there a way to scan the I2C devices? (btw, I have it hooked up according to the V0.8 Duex5 pin diagram and not based on the incorrectly labelled pcb silkscreen)

  • Based on oscilloscope readings, it looks like Wire.beginTransmission does send the address to the SDA pin.

    If I only use Wire.write once after Wire.beginTransmission,, it does not result in any more data being sent.

    If I use Wire.write twice, then another chunk of data is sent, but the data is equal to the address.

    If I use Wire.write 3 times after beginTransmission, then 3 chunks of data are sent, but the data is equal to the beginTransmission address, no matter what I put in Wire.write().

    Any idea what is going on?

  • Learning this as I go.

    For each sent byte, I am seeing a 1 in the ACK position, so I don't think it has anything to do with the Duet… must be the slave.

  • Problem was a solder joint. Fixed!

  • administrators


    Thanks. Two questions:

    1. I notice that there are "slashes" through the DuetNG folder and the files inside it. The Duet folder is not slashed. Is that a problem?

    That means the current configuration you have selected in Eclipse is not Duet WiFi or Duet Ethernet. The other boards supported by RRF do not use that folder. If you are using one of the older Duets, look in file src/Duet/MCP4461.cpp instead. In that file you will see that the available TWI interface on the older boards is Wire1 not Wire.

    EDIT: our posts crossed.

  • Thanks dc42. If you could help me figure out the "slashes," that would be great. Am I able to build correctly even though the slashes are there?

    When I build CoreNG, I click the hammer, but only SAM4E8E is check-marked:

    When I build RepRapFirmware, only DuetWiFi is check-marked.

    The hardware I am using Duet Wifi V1.02, Duex5 V0.8. And PanelDue. According to what it shows in DWC, the RRF firmware is 1.20beta6+1 (2017-11-09),

    In Eclipse in the RepRapFirmware folder, the only ones crossed off are within the src folder: Alligator, DuetNG, and RADDS.

    In the CoreNG folder, many are slashed, so I just took screenshots and you can see them in the links:

    How to fix this? Does it even need fixing? I can print, and the TWI device works fine now. I do plan to make many more changes to the firmware though.

  • administrators

    The slashes indicate that the folders concerned are excluded from the build in the active configuration. To change the active configuration, right click on the project, select Build Configurations, then Set Active.

  • I right click on the CoreNG folder in the Navigator, and I go to Build Configurations > Set Active … and I can see that SAM4E8E is selected.

    I do the same for the RepRapFirmware folder, and I see that it is set to DuetWiFi.

    Those are correct, aren't they?

  • administrators

    Yes they are. If you have SAM4E8E as the active configuration in CoreNG, then with the variants folder, duetNG should be enabled and the other variants should have a slash through them.

    There is a bug in Eclipse whereby occasionally it doesn't display an enabled folder but instead shows an extra copy of one of the disabled folders. It doesn't seem to affect building.

  • Hmmm, I have SAM4E8E active, but duetNG in the variants folder is slashed. Only duet is non-slashed. Maybe its another bug, hopefully in just the display of the folders.

Log in to reply