|
| 1 | +Inline assembler |
| 2 | +================ |
| 3 | + |
| 4 | +Here you will learn how to write inline assembler in Micro Python. |
| 5 | + |
| 6 | +**Note**: this is an advanced tutorial, intended for those who already |
| 7 | +know a bit about microcontrollers and assembly language. |
| 8 | + |
| 9 | +Micro Python includes an inline assembler. It allows you to write |
| 10 | +assembly routines as a Python function, and you can call them as you would |
| 11 | +a normal Python function. |
| 12 | + |
| 13 | +Returning a value |
| 14 | +----------------- |
| 15 | + |
| 16 | +Inline assembler functions are denoted by a special function decorator. |
| 17 | +Let's start with the simplest example:: |
| 18 | + |
| 19 | + @micropython.asm_thumb |
| 20 | + def fun(): |
| 21 | + movw(r0, 42) |
| 22 | + |
| 23 | +You can enter this in a script or at the REPL. This function takes no |
| 24 | +arguments and returns the number 42. ``r0`` is a register, and the value |
| 25 | +in this register when the function returns is the value that is returned. |
| 26 | +Micro Python always interprets the ``r0`` as an integer, and converts it to an |
| 27 | +integer object for the caller. |
| 28 | + |
| 29 | +If you run ``print(fun())`` you will see it print out 42. |
| 30 | + |
| 31 | +Accessing peripherals |
| 32 | +--------------------- |
| 33 | + |
| 34 | +For something a bit more complicated, let's turn on an LED:: |
| 35 | + |
| 36 | + @micropython.asm_thumb |
| 37 | + def led_on(): |
| 38 | + movwt(r0, stm.GPIOA) |
| 39 | + movw(r1, 1 << 13) |
| 40 | + strh(r1, [r0, stm.GPIO_BSRRL]) |
| 41 | + |
| 42 | +This code uses a few new concepts: |
| 43 | + |
| 44 | + - ``stm`` is a module which provides a set of constants for easy |
| 45 | + access to the registers of the pyboard's microcontroller. Try |
| 46 | + running ``import stm`` and then ``help(stm)`` at the REPL. It will |
| 47 | + give you a list of all the available constants. |
| 48 | + |
| 49 | + - ``stm.GPIOA`` is the address in memory of the GPIOA peripheral. |
| 50 | + On the pyboard, the red LED is on port A, pin PA13. |
| 51 | + |
| 52 | + - ``movwt`` moves a 32-bit number into a register. It is a convenience |
| 53 | + function that turns into 2 thumb instructions: ``movw`` followed by ``movt``. |
| 54 | + The ``movt`` also shifts the immediate value right by 16 bits. |
| 55 | + |
| 56 | + - ``strh`` stores a half-word (16 bits). The instruction above stores |
| 57 | + the lower 16-bits of ``r1`` into the memory location ``r0 + stm.GPIO_BSRRL``. |
| 58 | + This has the effect of setting high all those pins on port A for which |
| 59 | + the corresponding bit in ``r0`` is set. In our example above, the 13th |
| 60 | + bit in ``r0`` is set, so PA13 is pulled high. This turns on the red LED. |
| 61 | + |
| 62 | +Accepting arguments |
| 63 | +------------------- |
| 64 | + |
| 65 | +Inline assembler functions can accept up to 3 arguments. If they are |
| 66 | +used, they must be named ``r0``, ``r1`` and ``r2`` to reflect the registers |
| 67 | +and the calling conventions. |
| 68 | + |
| 69 | +Here is a function that adds its arguments:: |
| 70 | + |
| 71 | + @micropython.asm_thumb |
| 72 | + def asm_add(r0, r1): |
| 73 | + add(r0, r0, r1) |
| 74 | + |
| 75 | +This performs the computation ``r0 = r0 + r1``. Since the result is put |
| 76 | +in ``r0``, that is what is returned. Try ``asm_add(1, 2)``, it should return |
| 77 | +3. |
| 78 | + |
| 79 | +Loops |
| 80 | +----- |
| 81 | + |
| 82 | +We can assign labels with ``label(my_label)``, and branch to them using |
| 83 | +``b(my_label)``, or a conditional branch like ``bgt(my_label)``. |
| 84 | + |
| 85 | +The following example flashes the green LED. It flashes it ``r0`` times. :: |
| 86 | + |
| 87 | + @micropython.asm_thumb |
| 88 | + def flash_led(r0): |
| 89 | + # get the GPIOA address in r1 |
| 90 | + movwt(r1, stm.GPIOA) |
| 91 | + |
| 92 | + # get the bit mask for PA14 (the pin LED #2 is on) |
| 93 | + movw(r2, 1 << 14) |
| 94 | + |
| 95 | + b(loop_entry) |
| 96 | + |
| 97 | + label(loop1) |
| 98 | + |
| 99 | + # turn LED on |
| 100 | + strh(r2, [r1, stm.GPIO_BSRRL]) |
| 101 | + |
| 102 | + # delay for a bit |
| 103 | + movwt(r4, 5599900) |
| 104 | + label(delay_on) |
| 105 | + sub(r4, r4, 1) |
| 106 | + cmp(r4, 0) |
| 107 | + bgt(delay_on) |
| 108 | + |
| 109 | + # turn LED off |
| 110 | + strh(r2, [r1, stm.GPIO_BSRRH]) |
| 111 | + |
| 112 | + # delay for a bit |
| 113 | + movwt(r4, 5599900) |
| 114 | + label(delay_off) |
| 115 | + sub(r4, r4, 1) |
| 116 | + cmp(r4, 0) |
| 117 | + bgt(delay_off) |
| 118 | + |
| 119 | + # loop r0 times |
| 120 | + sub(r0, r0, 1) |
| 121 | + label(loop_entry) |
| 122 | + cmp(r0, 0) |
| 123 | + bgt(loop1) |
0 commit comments