Différences entre versions de « MicroPython-Hack-assembleur »
Ligne 4 : | Ligne 4 : | ||
== introduction == | == introduction == | ||
− | {{bloc-etroit|text= | + | {{bloc-etroit|text=Dans ce tutoriel, vous allez apprendre comment écrire de l'assembleur (''inline assembler'') en Micro Python. |
− | + | {{ambox|text=Il s'agit d'un tutoriel avancé destiné à ceux qui connaissent déjà un peu les microcontroleur et le langage assembleur.}} | |
− | Micro Python | + | Micro Python inclus un assembleur inline. Il vous permettra d'écrire des routines assembleur comme si c'était des fonctions micro python, et vous pouvez l'appeler comme si cela était des fonction Python normale.}} |
== Retourner une valeur == | == Retourner une valeur == | ||
− | {{bloc-etroit|text=Inline | + | {{bloc-etroit|text=Les fonctions assembleur Inline sont annotées avec une fonction de décoration spéciale. Commençons par l'exemple le plus simple: |
<nowiki>@micropython.asm_thumb | <nowiki>@micropython.asm_thumb | ||
Ligne 17 : | Ligne 17 : | ||
movw(r0, 42)</nowiki> | movw(r0, 42)</nowiki> | ||
− | + | Vous pouvez placer de code dans un script ou le taper sur une invite REPL. Cette fonction ne prend aucun argument et retourne le nombre 42. {{fname|r0}} est un registre, et la valeur dans ce registre lorsque la fonction se termine, est la valeur qui est retournée par la fonction. | |
− | + | Micro Python interprète toujours {{fname|r0}} comme un entier (type {{int}}) et converti cette valeur en objet {{fname|int}} (integer) pour la fonction appelante. | |
+ | |||
+ | Si vous exécutez {{fname|print(fun())}} vous verrez apparaître la valeur 42.}} | ||
== Accéder aux périphériques == | == Accéder aux périphériques == | ||
− | + | Pour un exemple un peu plus compliqué, essayons maintenant d'allumer une LED: | |
<nowiki>@micropython.asm_thumb | <nowiki>@micropython.asm_thumb | ||
Ligne 30 : | Ligne 32 : | ||
strh(r1, [r0, stm.GPIO_BSRRL])</nowiki> | strh(r1, [r0, stm.GPIO_BSRRL])</nowiki> | ||
− | + | Ce code utilise quelques nouveaux 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}} 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. |
Version du 19 avril 2015 à 09:09
En cours de traduction/élaboration. |
introduction
Dans ce tutoriel, vous allez apprendre comment écrire de l'assembleur (inline assembler) en Micro Python.
Il s'agit d'un tutoriel avancé destiné à ceux qui connaissent déjà un peu les microcontroleur et le langage assembleur. |
Micro Python inclus un assembleur inline. Il vous permettra d'écrire des routines assembleur comme si c'était des fonctions micro python, et vous pouvez l'appeler comme si cela était des fonction Python normale.
Retourner une valeur
Les fonctions assembleur Inline sont annotées avec une fonction de décoration spéciale. Commençons par l'exemple le plus simple:
@micropython.asm_thumb def fun(): movw(r0, 42)
Vous pouvez placer de code dans un script ou le taper sur une invite REPL. Cette fonction ne prend aucun argument et retourne le nombre 42. r0 est un registre, et la valeur dans ce registre lorsque la fonction se termine, est la valeur qui est retournée par la fonction.
Micro Python interprète toujours r0 comme un entier (type Modèle:Int) et converti cette valeur en objet int (integer) pour la fonction appelante.
Si vous exécutez print(fun()) vous verrez apparaître la valeur 42.
Accéder aux périphériques
Pour un exemple un peu plus compliqué, essayons maintenant d'allumer une LED:
@micropython.asm_thumb def led_on(): movwt(r0, stm.GPIOA) movw(r1, 1 << 13) strh(r1, [r0, stm.GPIO_BSRRL])
Ce code utilise quelques nouveaux concepts:
- stm is a module which provides a set of constants for easy access to the registers of the pyboard’s microcontroller. Try running import stm and then help(stm) at the REPL. It will give you a list of all the available constants.
- stm.GPIOA is the address in memory of the GPIOA peripheral. On the pyboard, the red LED is on port A, pin PA13.
- movwt moves a 32-bit number into a register. It is a convenience function that turns into 2 thumb instructions: movw followed by movt. The movt also shifts the immediate value right by 16 bits.
- strh stores a half-word (16 bits). The instruction above stores the lower 16-bits of r1 into the memory location r0 + stm.GPIO_BSRRL. This has the effect of setting high all those pins on port A for which the corresponding bit in r0 is set. In our example above, the 13th bit in 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 r0, r1 et r2 to reflect the registers and the calling conventions.
Here is a function that adds its arguments:
@micropython.asm_thumb def asm_add(r0, r1): add(r0, r0, r1)
This performs the computation r0 = r0 + r1. Since the result is put in r0, that is what is returned. Try asm_add(1, 2), it should return 3.
Les boucles
We can assign labels with label(my_label), and branch to them using b(my_label), or a conditional branch like bgt(my_label).
The following example flashes the green LED. It flashes it r0 times.
@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)
Source: Inline assembler écrit par/written by Damien P.George
Traduit par Meurisse D. pour MCHobby.be - Translated by Meurisse D. for MCHobby.be
Traduit avec l'autorisation de micropython.org - Translated with the authorisation of micropython.org
Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : « Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.
L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.