return 0, return 1 What they do?

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

So, i was studying I2C, which is referred as TWI by atmel, i came across these return 0 and return 1. I do not know what they actually do and my predictions might be wrong. 

 

http://homepage.hispeed.ch/peterfleury/doxygen/avr-gcc-libraries/group__pfleury__ic2master.html

 

Example from source code

 

unsigned char i2c_start(unsigned char address)
{
    uint8_t   twst;

	// send START condition
	TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);

	// wait until transmission completed
	while(!(TWCR & (1<<TWINT)));

	// check value of TWI Status Register. Mask prescaler bits.
	twst = TW_STATUS & 0xF8;
	if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;

	// send device address
	TWDR = address;
	TWCR = (1<<TWINT) | (1<<TWEN);

	// wail until transmission completed and ACK/NACK has been received
	while(!(TWCR & (1<<TWINT)));

	// check value of TWI Status Register. Mask prescaler bits.
	twst = TW_STATUS & 0xF8;
	if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;

	return 0;

 

 

life is hard

Last Edited: Sun. Jan 14, 2018 - 01:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why not read the documentation you linked to?

 

unsigned char i2c_start ( unsigned char  addr )  

Issues a start condition and sends address and transfer direction.

Parameters

addr address and transfer direction of I2C device

Return values

0 device accessible
1 failed to access device

 

Last Edited: Sun. Jan 14, 2018 - 01:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Already explained in your link

İt says if calling i2c_star() is successfull or not

İt can be used for error handling when using i2c_start()

Majid

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

yet this still does not answer to my question and it's drawbacks on main function. Does the main function stop or reset on fail?  if returns 0 does it go from where it left off ?

Because atmel datasheet handles this with a very weird unexplained ERROR(); function.

life is hard

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

yorem_kastor wrote:
Does the main function stop or reset on fail?

That is up to the writer of the main() function to decide. E.g. if i2c_start(..) returns 1 then that means (from the very documentation that you linked to) that it "failed to access device". In that case it really is meaningless to continue with e.g. calls to read or write functions. 

 

Unfortunately, Peter Fleurys example main() function does not test on the return values and handle them. (No, we can't tell you why hee chose not to.)

 

yorem_kastor wrote:
if returns 0 does it go from where it left off ?

Yes. when a function returns execution commences at the point right after the function call. This is really basic general C stuff, nothing specific to AVRs, I2C or Peter Fleurys code. You need to take one step back, get a good book (or other good learning material) on the C programming language in general, and read.

 

yorem_kastor wrote:
Because atmel datasheet handles this with a very weird unexplained ERROR(); function.

Name the data sheet and give reference to specific spot in that (i.e. page number and spot on that page).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Unfortunately, Peter Fleurys example main() function does not test on the return values and handle them. (No, we can't tell you why hee chose not to.)

That answered all the questions i had and i was wondering why would he use return values if he has no use for them in main. So i thought it was something special to Atmel studio. Then my job would be to write my own error functions if any of the functions given by fleurys returns a error which would be to stop the current transmission and start over again.

atmega32a datasheet TWI section page 230 revision 2016

Now i think what the datasheet meant was to write our own specific error function.

 

life is hard