Différences entre versions de « MicroPython-Hack-assembleur »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 17 : Ligne 17 :
 
     movw(r0, 42)</nowiki>
 
     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.
+
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 {{fname|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.}}
 
If you run {{fname|print(fun())}} you will see it print out 42.}}

Version du 19 avril 2015 à 08:17


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

introduction

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

Inline assembler functions are denoted by a special function decorator. Let’s start with the simplest example:

@micropython.asm_thumb
def fun():
    movw(r0, 42)

You can enter this in a script or at the REPL. This function takes no arguments and returns the number 42. 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 r0 as an integer, and converts it to an integer object for the caller.

If you run 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:

@micropython.asm_thumb
def led_on():
    movwt(r0, stm.GPIOA)
    movw(r1, 1 << 13)
    strh(r1, [r0, stm.GPIO_BSRRL])

This code uses a few new 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.




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.