countPulseASM()

Abstract

The countPulseASM() is an assembler code to count the pulse length.

Source Code

The countPulseASM() is defined in hardware/arduino/avr/cores/arduino/wiring_pulse.S as below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * The following routine was generated by avr-gcc 4.8.3 with the following parameters
 * -gstabs -Wa,-ahlmsd=output.lst -dp -fverbose-asm -O2
 * on the original C function
 *
 * unsigned long pulseInSimpl(volatile uint8_t *port, uint8_t bit, uint8_t stateMask, unsigned long maxloops)
 * {
 *     unsigned long width = 0;
 *     // wait for any previous pulse to end
 *     while ((*port & bit) == stateMask)
 *         if (--maxloops == 0)
 *             return 0;
 *
 *     // wait for the pulse to start
 *     while ((*port & bit) != stateMask)
 *         if (--maxloops == 0)
 *             return 0;
 *
 *     // wait for the pulse to stop
 *     while ((*port & bit) == stateMask) {
 *         if (++width == maxloops)
 *             return 0;
 *     }
 *     return width;
 * }
 *
 * some compiler outputs were removed but the rest of the code is untouched
 */
 
#include <avr/io.h>
 
.section .text
 
.global countPulseASM
 
countPulseASM:
 
.LM0:
.LFBB1:
    push r12   ;   ;  130 pushqi1/1 [length = 1]
    push r13   ;   ;  131 pushqi1/1 [length = 1]
    push r14   ;   ;  132 pushqi1/1 [length = 1]
    push r15   ;   ;  133 pushqi1/1 [length = 1]
    push r16   ;   ;  134 pushqi1/1 [length = 1]
    push r17   ;   ;  135 pushqi1/1 [length = 1]
/* prologue: function */
/* frame size = 0 */
/* stack size = 6 */
.L__stack_usage = 6
    mov r30,r24  ;  port, port   ;  2 *movhi/1  [length = 2]
    mov r31,r25  ;  port, port
/*     unsigned long width = 0;
***     // wait for any previous pulse to end
***     while ((*port & bit) == stateMask)
*/
.LM1:
    rjmp .L2   ;   ;  181 jump  [length = 1]
.L4:
/*         if (--maxloops == 0) */
.LM2:
    subi r16,1   ;  maxloops,  ;  17  addsi3/2  [length = 4]
    sbc r17, r1   ;  maxloops
    sbc r18, r1   ;  maxloops
    sbc r19, r1   ;  maxloops
    breq .L13  ; ,   ;  19  branch  [length = 1]
.L2:
/*         if (--maxloops == 0) */
.LM3:
    ld r25,Z   ;  D.1554, *port_7(D)   ;  22  movqi_insn/4  [length = 1]
    and r25,r22  ;  D.1554, bit  ;  24  andqi3/1  [length = 1]
    cp r25,r20   ;  D.1554, stateMask  ;  25  *cmpqi/2  [length = 1]
    breq .L4   ; ,   ;  26  branch  [length = 1]
    rjmp .L6   ;   ;  184 jump  [length = 1]
.L7:
/*             return 0;
***
***     // wait for the pulse to start
***     while ((*port & bit) != stateMask)
***         if (--maxloops == 0)
*/
.LM4:
    subi r16,1   ;  maxloops,  ;  31  addsi3/2  [length = 4]
    sbc r17, r1   ;  maxloops
    sbc r18, r1   ;  maxloops
    sbc r19, r1   ;  maxloops
    breq .L13  ; ,   ;  33  branch  [length = 1]
.L6:
/*         if (--maxloops == 0) */
.LM5:
    ld r25,Z   ;  D.1554, *port_7(D)   ;  41  movqi_insn/4  [length = 1]
    and r25,r22  ;  D.1554, bit  ;  43  andqi3/1  [length = 1]
    cpse r25,r20   ;  D.1554, stateMask  ;  44  enable_interrupt-3  [length = 1]
    rjmp .L7   ;
    mov r12, r1   ;  width  ;  7 *movsi/2  [length = 4]
    mov r13, r1   ;  width
    mov r14, r1   ;  width
    mov r15, r1   ;  width
    rjmp .L9   ;   ;  186 jump  [length = 1]
.L10:
/*             return 0;
***
***     // wait for the pulse to stop
***     while ((*port & bit) == stateMask) {
***         if (++width == maxloops)
*/
.LM6:
    ldi r24,-1   ; ,   ;  50  addsi3/3  [length = 5]
    sub r12,r24  ;  width,
    sbc r13,r24  ;  width,
    sbc r14,r24  ;  width,
    sbc r15,r24  ;  width,
    cp r16,r12   ;  maxloops, width  ;  51  *cmpsi/2  [length = 4]
    cpc r17,r13  ;  maxloops, width
    cpc r18,r14  ;  maxloops, width
    cpc r19,r15  ;  maxloops, width
    breq .L13  ; ,   ;  52  branch  [length = 1]
.L9:
/*         if (++width == maxloops) */
.LM7:
    ld r24,Z   ;  D.1554, *port_7(D)   ;  60  movqi_insn/4  [length = 1]
    and r24,r22  ;  D.1554, bit  ;  62  andqi3/1  [length = 1]
    cp r24,r20   ;  D.1554, stateMask  ;  63  *cmpqi/2  [length = 1]
    breq .L10  ; ,   ;  64  branch  [length = 1]
/*             return 0;
***     }
***     return width;
*/
.LM8:
    mov r22,r12  ;  D.1553, width  ;  108 movqi_insn/1  [length = 1]
    mov r23,r13  ;  D.1553, width  ;  109 movqi_insn/1  [length = 1]
    mov r24,r14  ;  D.1553, width  ;  110 movqi_insn/1  [length = 1]
    mov r25,r15  ;  D.1553, width  ;  111 movqi_insn/1  [length = 1]
/* epilogue start */
.LM9:
    pop r17  ;   ;  171 popqi [length = 1]
    pop r16  ;   ;  172 popqi [length = 1]
    pop r15  ;   ;  173 popqi [length = 1]
    pop r14  ;   ;  174 popqi [length = 1]
    pop r13  ;   ;  175 popqi [length = 1]
    pop r12  ;   ;  176 popqi [length = 1]
    ret  ;  177 return_from_epilogue  [length = 1]
.L13:
.LM10:
    ldi r22,0  ;  D.1553   ;  120 movqi_insn/1  [length = 1]
    ldi r23,0  ;  D.1553   ;  121 movqi_insn/1  [length = 1]
    ldi r24,0  ;  D.1553   ;  122 movqi_insn/1  [length = 1]
    ldi r25,0  ;  D.1553   ;  123 movqi_insn/1  [length = 1]
/* epilogue start */
.LM11:
    pop r17  ;   ;  138 popqi [length = 1]
    pop r16  ;   ;  139 popqi [length = 1]
    pop r15  ;   ;  140 popqi [length = 1]
    pop r14  ;   ;  141 popqi [length = 1]
    pop r13  ;   ;  142 popqi [length = 1]
    pop r12  ;   ;  143 popqi [length = 1]
    ret  ;  144 return_from_epilogue  [length = 1]

I will analize the C code shown in the comment.

The inputs are port, bit stateMask and maxloops.

6
7
* unsigned long pulseInSimpl(volatile uint8_t *port, uint8_t bit, uint8_t stateMask, unsigned long maxloops)
* {

After initializing the width with 0, if the status of the pin specified with port and bit is same as stateMask, wait for the status of the pin to change.

 8
 9
10
11
12
*     unsigned long width = 0;
*     // wait for any previous pulse to end
*     while ((*port & bit) == stateMask)
*         if (--maxloops == 0)
*             return 0;

Wait for the pin specified with port and bit to become the stateMask.

14
15
16
17
*     // wait for the pulse to start
*     while ((*port & bit) != stateMask)
*         if (--maxloops == 0)
*             return 0;

Then continue to increment width until the pin will become the different status.

19
20
21
22
23
*     // wait for the pulse to stop
*     while ((*port & bit) == stateMask) {
*         if (++width == maxloops)
*             return 0;
*     }

Finally returns width.

24
25
*     return width;
* }

Version

Arduino AVR Boards 1.8.6

Last Update

March 21, 2023

inserted by FC2 system