Go Down

Topic: Reversing wire.begin()? (Read 1 time) previous topic - next topic

liudr

I am trying to begin I2C when certain devices are attached and end I2C so I can use A4 and A5 as analog input when other devices are attached. Since wire library has no end(), like most Arduino libraries, I figure I should look into the twi library that the wire library calls.

There are two functions that seem to be useful:

  void twi_stop(void);
  void twi_releaseBus(void);

I am thinking that I should call twi_stop() to reset so I can use A4 and A5 with analogRead. Is this correct? The releaseBus sounds like what a master/slave would do after talking.

Thanks.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Erdin

Those functions are for the bus protocol. You need to stop the TWI hardware inside the microcontroller. Check the datasheet.

Do you use an Arduino Uno, Nano, Mini Pro ? That is the ATmega328P.


CrossRoads

liudr,
'328 datasheet, 22.9.2:

22.9.2 TWCR - TWI Control Register
The TWCR is used to control the operation of the TWI. It is used to enable the TWI, to initiate a Master access by
applying a START condition to the bus, to generate a Receiver acknowledge, to generate a stop condition, and to
control halting of the bus while the data to be written to the bus are written to the TWDR. It also indicates a write
collision if data is attempted written to TWDR while the register is inaccessible.
• Bit 2 - TWEN: TWI Enable Bit
The TWEN bit enables TWI operation and activates the TWI interface. When TWEN is written to one, the TWI
takes control over the I/O pins connected to the SCL and SDA pins, enabling the slew-rate limiters and spike filters.
If this bit is written to zero, the TWI is switched off and all TWI transmissions are terminated, regardless of any
ongoing operation.

Other things might be needed also to clean up after de-activating TWI operation.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

liudr


Those functions are for the bus protocol. You need to stop the TWI hardware inside the microcontroller. Check the datasheet.

Do you use an Arduino Uno, Nano, Mini Pro ? That is the ATmega328P.




I use UNO. Yeah, calling twi_stop() actually makes my mcu (or at least my program) stuck.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

liudr

Thanks CrossRoads!

Here is the init:

Code: [Select]
void twi_init(void)
{
  // initialize state
  twi_state = TWI_READY;
 
  // activate internal pullups for twi.
  digitalWrite(SDA, 1);
  digitalWrite(SCL, 1);

  // initialize twi prescaler and bit rate
  cbi(TWSR, TWPS0);
  cbi(TWSR, TWPS1);
  TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;

  /* twi bit rate formula from atmega128 manual pg 204
  SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
  note: TWBR should be 10 or higher for master mode
  It is 72 for a 16mhz Wiring board with 100kHz TWI */

  // enable twi module, acks, and twi interrupt
  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
}



So according to the last line, init seems to do some TWEN, TWIE and TWEA bits but what is _BV? Bit value (macro)? Should I just do TWCR=0 to clear everything for the end()? Can someone confirm? Right now I am just forcing pinMode to the A4 and A5 and it works fine but I feel like I am doing the wrong thing.

I hate Arduino style of no end to begin. It's a very amateurish approach, do one thing once and be happy, don't expand into doing several things one at a time repeatedly for weeks without resetting. Like the DUE audio stuff, you can play one sample and no way to reset to do it again (as of a few months ago).  :0
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

CrossRoads

I don't know, I'll let Coding Badly, AWOL, NickGammon answer that. That's deeper down in the code than I go.
Seems to me there should be a Wire.end, Serial.end and SPI.end.
Anything that has .begin should have a .end.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

liudr


Seems to me there should be a Wire.end, Serial.end and SPI.end.
Anything that has .begin should have a .end.


100% true. To them, it's not that hard to end things that they know how to begin. They finally added a few end to some begin but that's not enough. It's their ideology that if it works one time, it's good enough for open source. When people rely on the stuff to do serious work and reset things without resetting Arduino, the satisfaction is not there. This pushed me eventually towards raspberry pi, after what I found out about the wifi shield (and dealing with it). Arduino is only good for doing more basic stuff, not for internet. If you have not checked out the pi, I strongly recommend it. I have learned a lot of linux stuff from ground up but it's worth to look.

When I was looking at a few I2C libraries someone wrote, I found zero documentation or barely any comments although the libraries seem very well written. I had to dig into the spec sheets to see what sensor range and other stuff are used in init etc. That's very different if you look for documentation about linux stuff. There's plenty out there.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

CrossRoads

Not going into Pi, too much software for me. May do some hardware for it if someone asks.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

liudr


Not going into Pi, too much software for me. May do some hardware for it if someone asks.


The model B has changed pin layout since last release (sometime 2012, sep?) so watch out for the change if some work comes your way. They have two TTL UART but only one is brought out and need cleansing to work (remove all terminal associations). I am not very interested in porting hardware to pi yet. If you damage it, poof, like any other surface mount chips, you get a coaster. My current model is to use arduino UNO or MEGA with pi to isolate and confine incidences. It is a lot of software for me too but just think about this:

You deploy a project in a remote location, plug in a wifi dongle to pi, and add a mobile hotspot so pi gets internet. It will send you data and you can SSH to it to make any changes. You almost have to go on site for any arduino update, not with pi, which is accessible over internet. I did work on my pi (at home) from my office and while on the run. Plus, you can use a full-featured IDE. I admit I have never been so spoiled by any IDE til I see eclipse on linux. It's pretty complex but pretty good. My last full IDE experience was DevC++, or rather way back to the turboC++. Things have changed so much. These are features that will add values to your work. Just my 2 cents.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

phervier

Just a comment to say that this discussion was very useful for me on a completely hidden subject.

As the SDA ans SCL pins are also used for another function, I needed to stop and restart I2C.

To stop It I save the value of TWCR , and put TWCR  to 0.
To restart I2C i put back the saved value to TWCR .

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy