Ligne 37 : |
Ligne 37 : |
| * {{fname|strh}} stores a half-word (16 bits). The instruction above stores the lower 16-bits of {{fname|r1}} into the memory location {{fname|r0 + stm.GPIO_BSRRL}}. This has the effect of setting high all those pins on port A for which the corresponding bit in {{fname|r0}} is set. In our example above, the 13th bit in {{fname|r0}} is set, so PA13 is pulled high. This turns on the red LED. | | * {{fname|strh}} stores a half-word (16 bits). The instruction above stores the lower 16-bits of {{fname|r1}} into the memory location {{fname|r0 + stm.GPIO_BSRRL}}. This has the effect of setting high all those pins on port A for which the corresponding bit in {{fname|r0}} is set. In our example above, the 13th bit in {{fname|r0}} is set, so PA13 is pulled high. This turns on the red LED. |
| | | |
| + | == Accepter des arguments == |
| + | |
| + | Inline assembler functions can accept up to 3 arguments. If they are used, they must be named {{fname|r0}}, {{fname|r1}} et {{fname|r2}} to reflect the registers and the calling conventions. |
| + | |
| + | Here is a function that adds its arguments: |
| + | |
| + | <nowiki>@micropython.asm_thumb |
| + | def asm_add(r0, r1): |
| + | add(r0, r0, r1)</nowiki> |
| + | |
| + | This performs the computation {{fname|r0 = r0 + r1}}. Since the result is put in {{fname|r0}}, that is what is returned. Try {{fname|asm_add(1, 2)}}, it should return 3. |
| + | |
| + | == Les boucles == |
| + | We can assign labels with {{fname|label(my_label)}}, and branch to them using {{fname|b(my_label)}}, or a conditional branch like {{fname|bgt(my_label)}}. |
| + | |
| + | The following example flashes the green LED. It flashes it {{fname|r0}} times. |
| + | |
| + | <nowiki>@micropython.asm_thumb |
| + | def flash_led(r0): |
| + | # get the GPIOA address in r1 |
| + | movwt(r1, stm.GPIOA) |
| + | |
| + | # get the bit mask for PA14 (the pin LED #2 is on) |
| + | movw(r2, 1 << 14) |
| + | |
| + | b(loop_entry) |
| + | |
| + | label(loop1) |
| + | |
| + | # turn LED on |
| + | strh(r2, [r1, stm.GPIO_BSRRL]) |
| + | |
| + | # delay for a bit |
| + | movwt(r4, 5599900) |
| + | label(delay_on) |
| + | sub(r4, r4, 1) |
| + | cmp(r4, 0) |
| + | bgt(delay_on) |
| + | |
| + | # turn LED off |
| + | strh(r2, [r1, stm.GPIO_BSRRH]) |
| + | |
| + | # delay for a bit |
| + | movwt(r4, 5599900) |
| + | label(delay_off) |
| + | sub(r4, r4, 1) |
| + | cmp(r4, 0) |
| + | bgt(delay_off) |
| + | |
| + | # loop r0 times |
| + | sub(r0, r0, 1) |
| + | label(loop_entry) |
| + | cmp(r0, 0) |
| + | bgt(loop1)</nowiki> |
| | | |
| {{MicroPython-Hack-ASM-TRAILER}} | | {{MicroPython-Hack-ASM-TRAILER}} |