Incompatible (buggy) USART example code (reti missing)

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

Is this already known at ATMEL's datasheet editorial office?

In several (all?) datasheets the USART receive example needs in the assemly part a 'ret':

Assembly Code Example(1)
USART_Receive:
; Wait for data to be received
sbis UCSRnA, RXCn
rjmp USART_Receive
; Get status and 9th bit, then data from buffer
in r18, UCSRnA
in r17, UCSRnB
in r16, UDRn
; If error, return -1
andi r18,(1<<FEn)|(1<<DORn)|(1<<UPEn)
breq USART_ReceiveNoError
ldi r17, HIGH(-1)
ldi r16, LOW(-1)
*** here I would expect a return ***
USART_ReceiveNoError:
; Filter the 9th bit, then return
lsr r17
andi r17, 0x01
ret


C Code Example(1)
unsigned int USART_Receive( void )
{
unsigned char status, resh, resl;
/* Wait for data to be received */
while ( !(UCSRnA & (1<<RXCn)) )
;
/* Get status and 9th bit, then data */
/* from buffer */
status = UCSRnA;
resh = UCSRnB;
resl = UDRn;
/* If error, return -1 */
if ( status & (1<<FEn)|(1<<DORn)|(1<<UPEn) )
return -1;
/* Filter the 9th bit, then return */
resh = (resh >> 1) & 0x01;
return ((resh << 8) | resl);
}

Datasheet ATmega...P, page 189:
http://www.atmel.com/dyn/resourc...

In the assembly example in error case a 511 is returned.

In the beginning was the Word, and the Word was with God, and the Word was God.

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

The example code is a normal subroutine, not an interrupt routine, so the use of "ret" is correct. (note the use of "wait for data to be received"... you NEVER do that in an ISR)

But yes there should be a return after the loading of -1.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

Yes, glitch you are correct: my subject line was buggy, too. ;-)
Probably it comes, because similar code is more often used as an ISR rather than in polling mode.

But on the other hand, I must say, that the Atmel's datasheets are really good and clearly arranged.

Michael

In the beginning was the Word, and the Word was with God, and the Word was God.