Modifications

Sauter à la navigation Sauter à la recherche
2 329 octets ajoutés ,  19 avril 2015 à 08:16
aucun résumé de modification
Ligne 2 : Ligne 2 :     
{{traduction}}
 
{{traduction}}
 +
 +
== introduction ==
 +
{{bloc-etroit|text=Here you will learn how to write inline assembler in Micro Python.
 +
 +
Note: this is an advanced tutorial, intended for those who already know a bit about microcontrollers and assembly language.
 +
 +
Micro Python includes an inline assembler. It allows you to write assembly routines as a Python function, and you can call them as you would a normal Python function.}}
 +
 +
== Retourner une valeur ==
 +
{{bloc-etroit|text=Inline assembler functions are denoted by a special function decorator. Let’s start with the simplest example:
 +
 +
<nowiki>@micropython.asm_thumb
 +
def fun():
 +
    movw(r0, 42)</nowiki>
 +
 +
You can enter this in a script or at the REPL. This function takes no arguments and returns the number 42. {{fname|r0}} is a register, and the value in this register when the function returns is the value that is returned. Micro Python always interprets the {{integer|r0}} as an integer, and converts it to an integer object for the caller.
 +
 +
If you run {{fname|print(fun())}} you will see it print out 42.}}
 +
 +
== Accéder aux périphériques ==
 +
For something a bit more complicated, let’s turn on an LED:
 +
 +
<nowiki>@micropython.asm_thumb
 +
def led_on():
 +
    movwt(r0, stm.GPIOA)
 +
    movw(r1, 1 << 13)
 +
    strh(r1, [r0, stm.GPIO_BSRRL])</nowiki>
 +
 +
This code uses a few new concepts:
 +
 +
* {{fname|stm}} is a module which provides a set of constants for easy access to the registers of the pyboard’s microcontroller. Try running {{fname|import stm}} and then {{fname|help(stm)}} at the REPL. It will give you a list of all the available constants.
 +
* {{fname|stm.GPIOA}} is the address in memory of the GPIOA peripheral. On the pyboard, the red LED is on port A, pin PA13.
 +
* {{fname|movwt}} moves a 32-bit number into a register. It is a convenience function that turns into 2 thumb instructions: {{fname|movw}} followed by {{fname|movt}}. The {{fname|movt}} also shifts the immediate value right by 16 bits.
 +
* {{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.
 +
    
{{MicroPython-Hack-ASM-TRAILER}}
 
{{MicroPython-Hack-ASM-TRAILER}}
29 917

modifications

Menu de navigation