ArduinoShrink icon indicating copy to clipboard operation
ArduinoShrink copied to clipboard

Micros() is not compiled in timeout-like loops

Open omonar opened this issue 2 years ago • 1 comments

I use micro() function to timeout a do-while loop

// Originate timestamp
t1 = micros();

// Wait till data is there or timeout...
int cb = 0;
do {
  // Destination timestamp, well, sort of :)
  t4 = micros();
  if ((t4 - t1) > 1000 * 1000UL) {
    // timeout after 1000 ms
    return false;
  }
  cb = udp.parsePacket();
} while (cb == 0);

// We've received a packet, read the data from it
udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

which gets compiled to

    4982:	0e 94 f1 02 	call	0x5e2	;  micros()
    4986:	2b 01       	movw	r4, r22; t1
    4988:	3c 01       	movw	r6, r24
    498a:	84 e2       	ldi	r24, 0x24	; 36
    498c:	94 e0       	ldi	r25, 0x04	; 4
    498e:	0e 94 5d 18 	call	0x30ba	;  udp.parsePacket()
    4992:	89 2b       	or	r24, r25
    4994:	d1 f3       	breq	.-12     	;  0x498a
    4996:	40 e3       	ldi	r20, 0x30	; NTP_PACKET_SIZE = 48
    4998:	50 e0       	ldi	r21, 0x00	; 0
    499a:	b7 01       	movw	r22, r14
    499c:	84 e2       	ldi	r24, 0x24	; 36
    499e:	94 e0       	ldi	r25, 0x04	; 4
    49a0:	0e 94 14 18 	call	0x3028	;  udp.read(packetBuffer, NTP_PACKET_SIZE)

so it seems that the timeout check is not compiled in.

The same fragment of code without ArduinoShrink library compiles to

    4abe:	0e 94 2a 03 	call	0x654	;  micros()
    4ac2:	4b 01       	movw	r8, r22; t1
    4ac4:	5c 01       	movw	r10, r24
    4ac6:	0e 94 2a 03 	call	0x654	;  micros()
    4aca:	6b 01       	movw	r12, r22; t4
    4acc:	7c 01       	movw	r14, r24
    4ace:	2b 01       	movw	r4, r22
    4ad0:	3c 01       	movw	r6, r24
    4ad2:	48 18       	sub	r4, r8
    4ad4:	59 08       	sbc	r5, r9
    4ad6:	6a 08       	sbc	r6, r10
    4ad8:	7b 08       	sbc	r7, r11
    4ada:	f1 e4       	ldi	r31, 0x41	; 65
    4adc:	4f 16       	cp	r4, r31
    4ade:	f2 e4       	ldi	r31, 0x42	; 66
    4ae0:	5f 06       	cpc	r5, r31
    4ae2:	ff e0       	ldi	r31, 0x0F	; 15
    4ae4:	6f 06       	cpc	r6, r31
    4ae6:	71 04       	cpc	r7, r1
    4ae8:	08 f0       	brcs	.+2      	;  0x4aec
    4aea:	72 c0       	rjmp	.+228    	;  timeout has passed (exit)
    4aec:	8d e2       	ldi	r24, 0x2D	; 45
    4aee:	94 e0       	ldi	r25, 0x04	; 4
    4af0:	0e 94 c2 1a 	call	0x3584	;  udp.parsePacket()
    4af4:	89 2b       	or	r24, r25
    4af6:	39 f3       	breq	.-50     	;  0x4ac6
    4af8:	40 e3       	ldi	r20, 0x30	; NTP_PACKET_SIZE = 48
    4afa:	50 e0       	ldi	r21, 0x00	; 0
    4afc:	b8 01       	movw	r22, r16
    4afe:	8d e2       	ldi	r24, 0x2D	; 45
    4b00:	94 e0       	ldi	r25, 0x04	; 4
    4b02:	0e 94 79 1a 	call	0x34f2	;  udp.read(packetBuffer, NTP_PACKET_SIZE)

I compiled it with the most recent version (1.8.16) of Arduino IDE and standard settings.

omonar avatar Oct 12 '21 06:10 omonar

Thanks for the report. I haven't downloaded 1.8.16 yet, so I'll see if something changed from 1.8.13.

nerdralph avatar Oct 16 '21 18:10 nerdralph