Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: class TWBRemulation in WireKinetis.h when is it called?

  1. #1
    Senior Member
    Join Date
    May 2015
    Posts
    381

    class TWBRemulation in WireKinetis.h when is it called?

    I'm trying to modify a copy of the Wire library I copied into my Sketches folder but I'm confused on how / when class TWBRemulation is used. I wan't to add a 20 Khz (or close) I2C clock speed for a project I have that I have to deal with an I2C bus with 10 nF caps on the clock and data lines. That's way over what appears to be the maximum (400 pf) I found listed in a few places thus the need for a slow clock. The modules I am talking to are 20 years old and the caps are under a metal shield and can't be changed for a number of reasons.

    I'm confused on when class TWBRemulation is called. inline operator int () const __attribute__((always_inline) appears to "return" 12 the cpu clock is a standard speed 4 mhz or higher else it "returns" 72.

    TWBRemulation appears to use inline operator int () const __attribute__((always_inline) and sets the clock to 100 khz (about) if its 72 else its 400 khz (about).

    I cant find where inline operator TWBRemulation is actually called so I'm not sure if I need to modify it or not. To get the speed I want I need to set I2C0_F to 0x3A (for 72 Mhz CPU clock) in TwoWire::setClock(uint32_t frequency).

    Can I get away with just modifying WireKinetis.cpp TwoWire::setClock(uint32_t frequency) to add a 20khz option for 72 Mhz?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,551
    Quote Originally Posted by DaQue View Post
    how / when class TWBRemulation is used.
    It's only used when AVR-specific code reads or writes the TWBR register to change the clock speed. Those strange constants are the specific numbers needed for AVR with 16 MHz clock.

    Modern versions of the Wire library have setClock(), so this is rarely needed now. Still, it's there for compatibility with very old Arduino code.

  3. #3
    Senior Member
    Join Date
    May 2015
    Posts
    381
    PS My plan on dealing with the slow module is to init the Audio Adaptor at normal speed before I attach the module with its 10 nf caps using an adaptor board I am making then change the I2C to slow speed to talk to the module. As I understand it the I2C only needs to be at normal speed when the Audio adaptor starts up. Once the program runs the module is removed and returned to the end product.

  4. #4
    Senior Member
    Join Date
    May 2015
    Posts
    381
    Quote Originally Posted by PaulStoffregen View Post
    It's only used when AVR-specific code reads or writes the TWBR register to change the clock speed. Those strange constants are the specific numbers needed for AVR with 16 MHz clock.

    Modern versions of the Wire library have setClock(), so this is rarely needed now. Still, it's there for compatibility with very old Arduino code.
    Thanks! I can't believe how you can remember all of these details. #impressed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •