not logged in | [Login]

References

uPy assembler functions

Are prefixed by the decorator @micropython.asm_thumb which causes uPy to interpret the content of the function as inline assembler, compile and wrap it accordingly so that it can be called from by Python code.

Inline assembler functions always return the content of register r0 on exit, if uPy code needs to receive a value from your assembler function - make sure it's in r0 when the function completes, the content of r0 will be returned - whether you want it or not.

Inline assembler functions can have a minimum of zero, and maximum of three arguments, which are passed into registers r0, r1 and r2 in order and must be named r0, r1 and r2 in the function defenition;

Legal;

def test()
def test(r0)
def test(r0, r1)
def test(r0, r1, r2)

Illegal;

def test(self)
def test(r1,r3)
def test(r0, r1, r2, r3)

Example:

@micropython.asm_thumb
def test(r0,r1):
  nop() #don't do anything
>>> test(1,2)
1 

test returns 1 because that's what we passed into r0 when the function was called, and the value in r0 wasn't changed.

Under certain circumstances Python, therefore uPy, automagically passes objects into functions, but this functionality is likely undesirable in assembler functions. For example a method of a class, defined as a function, is automatically passed a reference to the object it as an instance of as its first argument (Python Classes Tutorial. In Python this object instance is often referred to as self or new, depending on context ... however this is just a convention and any variable name can be used.

This behaviour is by default applied to methods defined as assembler functions ... with the result that a pointer to the class object is passed into r0, which is highly unlikely to be of any use in assembler, this has the additional side-effect of reducing the number of 'useable' assembler arguments from 3 to 2 (r1 and r2) for an assembler method of a class.

This behaviour can be excepted by using one of Python's existing decorators - @staticmethod - which has made it's way across into uPy;

Example:

class test:
  @micropython.asm_thumb
  def t1(r0):
    nop()
  @staticmethod
  @micropython.asm_thumb
  def t2(r0):
    nop()
>>> t=test()
>>> t.t1()

>>> t.t2(42)
42

Exposed Assembler Instructions

Not all of the stm32f405 chips instructions are exposed for use in uPy assembler functions - those that are may be gleaned from uPy's Inline Assembler source-code, emitinlinethumb.c. Basic notes and a few examples of the currently implemented instructions are presented hereunder;