Go Down

Topic: Watchdog ISR in assembly language (Read 8 times) previous topic - next topic

Graynomad

#20
Feb 15, 2011, 06:57 am Last Edit: Feb 15, 2011, 07:01 am by Graynomad Reason: 1
Fair dinkum this IDE is a crock.

Sketch>Add file... does nothing if it doesn't like the file type, at least have an error message.
It seems to add a .S file but doesn't show it in a tab

I don't normally use the IDE but I want what I'm doing to be available to the general Arduino crowd and most people do I think.

Anyway it's working in a fashion. Some mods to the py script (I've never used Python before, I gather the indenting actually defines the code blocks?)

Code: [Select]
import sys, os

def asm2include(infile, outfile):
   os.remove (outfile)

   line = ""
   with open(infile, 'r') as IN:
       for line in IN:
           line = line.strip(' ')
           if (line != ""):
               line = line.replace('  ',' ')
               line = line.replace('SREG','0x3f')
               line = line.replace('command','r18')
               line = line.replace('temp1','r16')
               line = line.replace('temp2','r17')
               line = line.replace('addlo','r19')
               line = line.replace('addhi','r20')
               line = line.replace('ZH','r31')
               line = line.replace('ZL','r30')
               line = line.replace('PORTB','5')
               line = line.replace('PINB','3')

               OUT = open(outfile, "a")
               if (line.rfind("//") == -1):
                   OUT.write("asm(\"")
                   OUT.write(line[:-1])
                   OUT.write("\");\n")
             
               OUT.close()
           print '.',

""" --------------------------------------------------------------
MAIN APPLICATION
"""  

print "ASM2include 0.1"
asm2include(sys.argv[1], sys.argv[2])
print "Done"


It deletes the output file and makes a few substitutions, no sanity test for the file delete so a bit dangerous at present.

.equ works on an "rvalue" but not an "lvalue", hense the py script replacements.

.equ   PIN_MOSI,3 // this works so don't bother with the replace
#define addhi   r20 // .equ addhi,r20 doesn't work, so leave this here just for reference


Leave all comments out, not point having them in the .S version

Then in the sketch

Code: [Select]
ISR (WDT_vect, ISR_NAKED) {
#include "d:\work\atmon\atmon\atmon_isr.s"
}


So it works but is pretty fugly.

The next step is to get both a C and ASM version working and see if there's much point in the first place  :)

______
Rob




Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

Some improvements

.equ command,16
clr   command

works, (note no "r16", just "16")

so the py script is almost back to original

Code: [Select]
import sys, os

def asm2include(infile, outfile):
   os.remove (outfile)

   line = ""
   with open(infile, 'r') as IN:
       for line in IN:
           line = line.strip(' ')
           if (line != ""):
               line = line.replace('  ',' ')
               OUT = open(outfile, "a")
               if (line.rfind("//") == -1):
                   OUT.write("asm(\"")
                   OUT.write(line[:-1])
                   OUT.write("\");\n")
             
               OUT.close()
           print '.',

""" --------------------------------------------------------------
MAIN APPLICATION
"""  

print "ASM2include 0.1"
asm2include(sys.argv[1], sys.argv[2])
print "Done"


______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

#22
Feb 15, 2011, 05:09 pm Last Edit: Feb 15, 2011, 05:15 pm by Graynomad Reason: 1
I know you guys have been losing sleep over whether or not this is all worth it when an ISR written in C can be written in about 2 minutes. So I did a bit of testing.

The ISP function is basically to shift 3 bytes in, do a tiny amount of processing, then shift 1 byte out, all using what is essentially bit banged SPI.

Program size (including Arduino overhead)

ASM 858bytes
C (using digitalWrite()s) 1210 bytes

So there's a large difference mostly because of the digitalWrite library being pulled it, however in any normal program would have that anyway so the above comparison is not really fair. So in setup I added a single digitalWrite to pull in the library

ASM + 1 digitalWrite 1038 bytes.

The ISR code size is

ASM 212bytes
C 388 bytes

So with a normal program we're 176 bytes better off with ASM.

That's not a deal breaker, but have a look at the timing. Using a logic analyser (pics attached, pulse train at the top is normal program flow) I measured the interruption to main program flow.

ASM 37uS
C 400uS

That's a significant improvement, the ASM version is 10x faster. Also the ASM version could be even faster but I've had to add delays to allow the device (another Arduino) to catch up).

There is a third option of course, C with direct port manipulation which would presumably be somewhere between the two, I may write this to test it as well.

______
Rob





Rob Gray aka the GRAYnomad www.robgray.com

robtillaart

Quote
I know you guys have been losing sleep over whether or not this is all worth it

Imho these kinds of insights are worth it. They might cost a lot of time to build up but will save time in the end.

The speedup is so significant that one could think of rewiting digitalWrite (and other core functions) in asm(" ").

Keep us informed of the progress.

BTW what logic analyzer do you use? there is still one my wishlist ;)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

westfw

Quote
rewiting digitalWrite (and other core functions) in asm(" ").

C is not the problem.  There has been extensive discussion on speeding up digitalWrite and friends; search the forum for "digitalwritefast"...


Go Up