Where can we find information about the inline assembler for Pico?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
cebersp
Posts: 21
Joined: Mon Feb 08, 2021 12:07 pm

Where can we find information about the inline assembler for Pico?

Post by cebersp » Tue Feb 23, 2021 11:21 am

Hi,
I would like to do something with ADC and the inline assembler.
I am starting with the tutorial: http://docs.micropython.org/en/v1.9.3/p ... mbler.html

Where can I find such information like the equivalent for Pico for "stm.GPIOA"?
"rp2.GPIO" does not work.
movw does not work. - That seems to be because it is M0.

How can I load a 32bit constant into a register? ==> Solved via array of constants

Thanks for some hints!
Christof
Last edited by cebersp on Wed Feb 24, 2021 10:19 am, edited 1 time in total.

User avatar
pythoncoder
Posts: 4921
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Where can we find information about the inline assembler for Pico?

Post by pythoncoder » Tue Feb 23, 2021 6:02 pm

There is also this doc. I haven't studied the Pico closely, there may well be incompatibilities besides the obvious one that the floating point instructions won't be supported.

If movw doesn't work then you could pass constants as args. In some of my code I pass an array of constants as a single arg which can then be accessed because arrays are stored in contiguous locations. Example code may be found in these repos fourier transforms and FIR filters.

A long time ago I submitted this PR to simplify access to data created with data statements, but it was not accepted. The data statement works but is hard to use in normal code.
Peter Hinch

cebersp
Posts: 21
Joined: Mon Feb 08, 2021 12:07 pm

Re: Where can we find information about the inline assembler for Pico?

Post by cebersp » Wed Feb 24, 2021 8:16 am

Thanks, Peter,
"an array of constants " - this is a good idea!
I was not able to use data().
Then I passed the address as an argument. But my even bigger problem was, that I was not able to find the information in the rp2040 datasheet, how to just set the output bit of the Pin. Funny: The instruction set of M0 seems to be not too difficult to understand. But these hardware pin control registers....
Christof

Edit: A fresh day, will try Bits OUTOVER in GPIO25_CTRL...
Last edited by cebersp on Wed Feb 24, 2021 8:36 am, edited 1 time in total.

cebersp
Posts: 21
Joined: Mon Feb 08, 2021 12:07 pm

Re: Where can we find information about the inline assembler for Pico?

Post by cebersp » Wed Feb 24, 2021 8:24 am

I bought: Amazon: "MicroPython Inline Assembler By Example (English Edition) Kindle Ausgabe"
But it it is not very helpful for Pico.

User avatar
pythoncoder
Posts: 4921
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Where can we find information about the inline assembler for Pico?

Post by pythoncoder » Wed Feb 24, 2021 9:50 am

Well the Pico chip is very new.

To make a very general point there is now a mechanism for creating loadable native C modules. You write code in C with specific interface features, then compile it to a .mpy file. It works well on the Pyboard. Given that the Pico arch is ARM I thought it might work on the Pico. But a quick test indicates it does not. I have raised an issue. Hopefully it will get well soon.

My DFT assembler code is optimised for ease of coding rather than performance but it is slower than the C code in numpy. The C compiler is evidently good at optimisation. So the justification for assembler is rather less than it was when I wrote those libraries.

None of which helps in accessing these peripherals, I'm afraid ;)
Peter Hinch

cebersp
Posts: 21
Joined: Mon Feb 08, 2021 12:07 pm

Re: Where can we find information about the inline assembler for Pico?

Post by cebersp » Wed Feb 24, 2021 10:17 am

Ha, got it working, the most complicated way of blinking a LED! :o
It works via "SIO" chapter 2.3.1.2.

Code: Select all

title= "inlineC.py" # http://docs.micropython.org/en/v1.9.3/pyboard/pyboard/tutorial/assembler.html
from rp2 import *
from machine import *
from time import *
import uarray

led= Pin(25,Pin.OUT)
led(1)
#sleep(2)
led(0)


@micropython.asm_thumb # works
def fun():
    mov(r0, 42) # movw hat nicht funktioniert
    add(r0, r0, 7) # nur kleine Werte <8

@micropython.asm_thumb # works
def asm_add(r0, r1):
    add(r0, r0, r1)

regdata = uarray.array('i',[
                0xd0000000 + 0x010, # 0 SIO OUT
                0xd0000000 + 0x018, # 4 SIO OUT_CLR
                0xd0000000 + 0x014, # 8 SIO OUT_SET
                1<<25 # 12 LED 
                ])

@micropython.asm_thumb
def led_peek(r0):
    mov(r1,r0)
    ldr(r2,[r1,0]) # register adress
    ldr(r0,[r2,0])

    
@micropython.asm_thumb
def led_on(r0):
    mov(r1,r0)
    ldr(r2,[r1,8]) # register address
    ldr(r0,[r1,12]) # mask data
    str(r0,[r2,0])

@micropython.asm_thumb
def led_off(r0):
    mov(r1,r0)
    ldr(r2,[r1,4]) # register address
    ldr(r0,[r1,12]) # mask data
    str(r0,[r2,0])


print(title, fun(), asm_add(20_000_000,30))
print(led_peek(regdata))
print(led_on(regdata))
print(led_peek(regdata))
sleep(2)
print(led_off(regdata))
print(led_peek(regdata))

Post Reply