Digital filter and assembler code examples

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Digital filter and assembler code examples

Post by pythoncoder » Thu Feb 12, 2015 11:35 am

I've posted some code here https://github.com/peterhinch/micropython-filters.git with implementations of moving average and FIR (finite impulse response) functions written in ARM assembler. These are intended for use in interrupt callbacks to handle real time integer data from the ADC's or transducers and are fast (typically 8-25uS - the readme provides further guidance).
There is also a file asm.py illustrating the technique of dealing with ARM assembler instructions which are not yet implemented in MicroPython.
For obbvious reasons this code is only of use on ARM hosts. The routines have been developed and tested on the MicroPython board.
Peter Hinch
Index to my micropython libraries.

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Digital filter and assembler code examples

Post by blmorris » Thu Feb 12, 2015 3:20 pm

Thanks for putting this together! I had a quick look; I can see that you really made the effort to explain your process and show how it all works. Not just useful code, but a solid reference for anyone trying to write their own assembler. Nice work!
-Bryan

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

Re: Digital filter and assembler code examples

Post by pythoncoder » Thu Feb 12, 2015 3:37 pm

Thank you! :D
Peter Hinch
Index to my micropython libraries.

ul5255
Posts: 11
Joined: Thu Oct 23, 2014 9:08 am

Re: Digital filter and assembler code examples

Post by ul5255 » Thu Feb 12, 2015 10:29 pm

Peter,
where does this data() instruction in our inline assembler come from? I haven't seen this being mentioned anywhere in the official docs or the Wiki.

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Digital filter and assembler code examples

Post by blmorris » Fri Feb 13, 2015 4:24 am

ul5255 wrote:Peter,
where does this data() instruction in our inline assembler come from? I haven't seen this being mentioned anywhere in the official docs or the Wiki.
It was a workaround to be able to use the unimplemented push and pop assembly instructions. But as of about two hours ago, push and pop are implemented by the uPy assembler: https://github.com/micropython/micropyt ... baa5e262c2 The data() instruction may still be useful for providing other unimplemented instructions.
-Bryan

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Post by Damien » Fri Feb 13, 2015 9:57 am

That's awesome stuff! It really shows how to mix Python with assembler to do some fancy processing.

Do you know about the ite instruction?

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

Re: Digital filter and assembler code examples

Post by pythoncoder » Fri Feb 13, 2015 10:35 am

Thank you :D As for ITE I hadn't spotted that one. Very handy - I'll experiment. It's great that PUSH and POP are now done - it would be good to see SDIV and UDIV implemented. In the meantime the data workround is handy and I edited the wiki yesterday to outline it.
Peter Hinch
Index to my micropython libraries.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Digital filter and assembler code examples

Post by Damien » Fri Feb 13, 2015 11:15 am

udiv, sdiv, clz and rbit are now implemented.

Let me know if there's anything else you need, or any other ideas for improving the inline assembler :)

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

Re: Digital filter and assembler code examples

Post by pythoncoder » Sat Feb 14, 2015 9:02 am

Do you know about the ite instruction?
Eek. In its general form the IT instruction is a bit of a brain-bender ;) Unless I'm misunderstanding the required syntax, there is a problem with the ite.ge() instruction implementation. The following code works if I emit the instruction using a data statement, but crashes if I replace the latter with the commented out line.

Code: Select all

@micropython.asm_thumb
def rats(r0):
    mov(r7, 0)
    mov(r1, 1)
    mov(r2, 2)
    mov(r3, 4)
    cmp(r0, r2)
#    ite.ge()
    data(2, 0xbfac) # emit ite.ge()
    orr(r7, r1) # Executed if r0 >= 2
    orr(r7, r2) # Executed if r0 < 2
    orr(r7, r3) # Always executed.
    mov(r0, r7)

def testit():
    for x in range(6):
        print(x, hex(rats(x)))
Apologies if I'm doing something silly.
Peter Hinch
Index to my micropython libraries.

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

ITE instruction

Post by pythoncoder » Sun Feb 15, 2015 10:19 am

With the latest (15th Feb) build ite.ge() throws an argument count error when the module is loaded. I had a suspicion that Python might be supplying an unwanted argument so I created a build with the ite.ge opcode changed to itege. This seems to work as expected.

Great improvements to the assembler!

Incidentally when I did the build I got the strangest warning:
make: warning: Clock skew detected. Your build may be incomplete.
Peter Hinch
Index to my micropython libraries.

Post Reply