Author Topic: ASM to Arduino  (Read 3933 times)

0 Members and 1 Guest are viewing this topic.

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
ASM to Arduino
« on: January 16, 2019, 05:11:11 pm »
There are some analogues to the PICDIV for Arduino, but if I recall correctly, the code is in .asm files. How do I use these?
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4036
  • Country: nz
Re: ASM to Arduino
« Reply #1 on: January 16, 2019, 05:51:34 pm »
You could try http://www.ulrich-bangert.de/PPSDividerMega8.zip

That simultaneously provides 8 outputs (a complete output port) with frequencies 100 kHz, 10 kHz, 1 kHz, 100 Hz, 10 Hz, 1 Hz, 0.1 Hz and 0.01 Hz assuming a 10 MHz input clock.
« Last Edit: January 16, 2019, 05:56:33 pm by brucehoult »
 

Offline fsr

  • Regular Contributor
  • *
  • Posts: 169
  • Country: ar
Re: ASM to Arduino
« Reply #2 on: January 16, 2019, 05:58:17 pm »
I have taken a look at the PicDIV website, and it seems to be an assembler project for very high precision frequency division with pic devices. Doesn't seems to be a way to add that to another project, much less arduino (obviously). This will convert the pic device into a precision frequency divider, and that's it. If you need to do something else, you need another microcontroller (well, maybe in assembler you could stuff some other functionality there, but that would require analyzing the code and adding code to it, trying to avoid upsetting the divider, if at all possible).

.ASM is the source code for assembler.
.HEX is the file to upload to the microcontroller with a programmer.

I would expect the same to apply to the AVR version.
« Last Edit: January 16, 2019, 06:06:17 pm by fsr »
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: ASM to Arduino
« Reply #3 on: January 16, 2019, 06:35:26 pm »
Doesn't seems to be a way to add that to another project, much less arduino (obviously).

There is an analog to picdiv, called ppsdiv and it is written for Arduino. From this site:

http://www.ulrich-bangert.de/html/downloads.html

http://www.ulrich-bangert.de/PPSDividerMega8.zip

There is a hex file in the zip package as well and maybe that can be burned to the chip. I've not done that either.
 

Offline fsr

  • Regular Contributor
  • *
  • Posts: 169
  • Country: ar
Re: ASM to Arduino
« Reply #4 on: January 16, 2019, 11:27:50 pm »
Doesn't seems to be a way to add that to another project, much less arduino (obviously).

There is an analog to picdiv, called ppsdiv and it is written for Arduino. From this site:

http://www.ulrich-bangert.de/html/downloads.html

http://www.ulrich-bangert.de/PPSDividerMega8.zip

There is a hex file in the zip package as well and maybe that can be burned to the chip. I've not done that either.
Are you looking for something that you can load in the Arduino gui and use inside an Arduino sketch, or are you looking for anything that will run inside an AVR microcontroller? Those two are different requirements.
« Last Edit: January 16, 2019, 11:29:34 pm by fsr »
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4036
  • Country: nz
Re: ASM to Arduino
« Reply #5 on: January 17, 2019, 06:37:22 am »
Not *very* different, as you can happily put assembly language source code into the Arduino IDE, call it from your setup() function, and never return.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: ASM to Arduino
« Reply #6 on: January 17, 2019, 07:45:21 am »
Not *very* different, as you can happily put assembly language source code into the Arduino IDE, call it from your setup() function, and never return.

For precise timing just remember to disable the interrupts.

Yes, I am looking at you, the timer that updates the millisecond counter...  :D
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline fsr

  • Regular Contributor
  • *
  • Posts: 169
  • Country: ar
Re: ASM to Arduino
« Reply #7 on: January 17, 2019, 03:47:37 pm »
Not *very* different, as you can happily put assembly language source code into the Arduino IDE, call it from your setup() function, and never return.
Well, if the Arduino ide happily accepts the code, and you disable all interrupts, and all the instructions take the same amount of cycles than in the mega8, and the arduino board has a real crystal oscillator, and it's frequency if the right one, i suppose it could work, but then the Arduino can't do anything else.
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: ASM to Arduino
« Reply #8 on: January 17, 2019, 03:51:04 pm »
Arduino IDE is not happy with the ASM code.

fsr, I simply want to load the PPSDiv code to arduino. I think it works "out of the box" so I do not expect to have to modify it, I just want to get the code onto a 328p chip. I think there is a way to burn the hex file which is included. I recall burning the bootloader onto bare chips so I may review how I did that, so many years ago... But to take a step back, I don't really need this code, but any code that will do the same thing. I could also just order some pic chips and try the picdiv code, but I don't have those chips and I have plenty of Arduinos.


This is the ASM code I tried to compile in Arduino IDE.

Code: [Select]
#include <m32def.inc>

void setup(){
init:
LDI R16,0xFF
OUT DDRA,R16
CLR R15
CLR R14
CLR R13
CLR R12
CLR R11
CLR R10
CLR R9
CLR R8

void loop(){
  LDI R16,0x0A
        INC R15
CP R15,R16
BRNE nd1
CLR R15
nd1: BRNE nd1w
INC R14
nd1w: CP R14,R16
BRNE nd2
CLR R14
nd2: BRNE nd2w
INC R13
nd2w: CP R13,R16
BRNE nd3
CLR R13
nd3: BRNE nd3w
INC R12
nd3w: CP R12,R16
BRNE nd4
CLR R12
nd4: BRNE nd4w
INC R11
nd4w: CP R11,R16
BRNE nd5
CLR R11
nd5: BRNE nd5w
INC R10
nd5w: CP R10,R16
BRNE nd6
CLR R10
nd6: BRNE nd6w
INC R9
nd6w: CP R9,R16
        BRNE cready
CLR R9
cready: CLT
BLD R8,0
OUT PORTA,R8
        LDI  R17,08
WLP0:  DEC  R17
        BRNE WLP0
CLR R8
        LDI R16,0x05
SET
BLD R8,0
CP R15,R16
BRCS d2
BLD R8,1
d2: CP R14,R16
BRCS d3
BLD R8,2
d3: CP R13,R16
BRCS d4
BLD R8,3
d4: CP R12,R16
BRCS d5
BLD R8,4
d5: CP R11,R16
BRCS d6
BLD R8,5
d6: CP R10,R16
BRCS d7
BLD R8,6
d7: CP R9,R16
BRCS ok
BLD R8,7
ok: OUT PORTA,R8
        LDI     R17,03
WLP1:   DEC     R17
        BRNE    WLP1
NOP
RJMP
}
 

Offline darrellg

  • Supporter
  • ****
  • Posts: 105
  • Country: us
Re: ASM to Arduino
« Reply #9 on: January 17, 2019, 04:26:07 pm »
Arduino IDE is not happy with the ASM code.

[...]

This is the ASM code I tried to compile in Arduino IDE.

That's not how you do it. A ten second search on Google will show you the right way to use asm in the Arduino IDE:
https://ucexperiment.wordpress.com/2016/03/04/arduino-inline-assembly-tutorial-1/
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: ASM to Arduino
« Reply #10 on: January 17, 2019, 06:07:26 pm »
That's not how you do it. A ten second search on Google will show you the right way to use asm in the Arduino IDE:
https://ucexperiment.wordpress.com/2016/03/04/arduino-inline-assembly-tutorial-1/

Yes, knowing that something exists and finding it takes 10 seconds.

Quote
Learning inline assembly language on the Arduino AVR 8-bit platform is a daunting task for many (at least it was for me). Besides the cryptic syntax and the high level of understanding the semi-official documentation assumes, there exists very little information about GCC inline assembler coding.

I tried working with the tutorial and here is where I ended up.

Code: [Select]
#include <m32def.inc>

void setup(){
asm (
"init: \n"
"LDI R16,0xFF \n"
"OUT DDRA,R16 \n"
"CLR R15 \n"
"CLR R14 \n"
"CLR R13 \n"
"CLR R12 \n"
"CLR R11 \n"
"CLR R10 \n"
"CLR R9 \n"
"CLR R8 \n"

"loop: \n"
  "LDI R16,0x0A \n"
        "INC R15 \n"
"CP R15,R16 \n"
"BRNE nd1 \n"
"CLR R15 \n"
"nd1: BRNE nd1w \n"
"INC R14 \n"
"nd1w: CP R14,R16 \n"
"BRNE nd2 \n"
"CLR R14 \n"
"nd2: BRNE nd2w \n"
"INC R13 \n"
"nd2w: CP R13,R16 \n"
"BRNE nd3 \n"
"CLR R13 \n"
"nd3: BRNE nd3w \n"
"INC R12 \n"
"nd3w: CP R12,R16 \n"
"BRNE nd4 \n"
"CLR R12 \n"
"nd4: BRNE nd4w \n"
"INC R11 \n"
"nd4w: CP R11,R16 \n"
"BRNE nd5 \n"
"CLR R11 \n"
"nd5: BRNE nd5w \n"
"INC R10 \n"
"nd5w: CP R10,R16 \n"
"BRNE nd6 \n"
"CLR R10 \n"
"nd6: BRNE nd6w \n"
"INC R9 \n"
"nd6w: CP R9,R16 \n"
        "BRNE cready \n"
"CLR R9 \n"
"cready: CLT \n"
"BLD R8,0 \n"
"OUT PORTA,R8 \n"
        "LDI  R17,08 \n"
"WLP0:  DEC  R17 \n"
        "BRNE WLP0 \n"
"CLR R8 \n"
        "LDI R16,0x05 \n"
"SET \n"
"BLD R8,0 \n"
"CP R15,R16 \n"
"BRCS d2 \n"
"BLD R8,1 \n"
"d2: CP R14,R16 \n"
"BRCS d3 \n"
"BLD R8,2 \n"
"d3: CP R13,R16 \n"
"BRCS d4 \n"
"BLD R8,3 \n"
"d4: CP R12,R16 \n"
"BRCS d5 \n"
"BLD R8,4 \n"
"d5: CP R11,R16 \n"
"BRCS d6 \n"
"BLD R8,5 \n"
"d6: CP R10,R16 \n"
"BRCS d7 \n"
"BLD R8,6 \n"
"d7: CP R9,R16 \n"
"BRCS ok \n"
"BLD R8,7 \n"
"ok: OUT PORTA,R8 \n"
        "LDI     R17,03 \n"
"WLP1:   DEC      \n"
        "BRNE    WLP1 \n"
"NOP \n"
"RJMP  \n"
);
}

void loop(){}



==========================

Error compiling:

C:\Users\\AppData\Local\Temp/ccYDtlBP.s: Assembler messages:
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:31: Error: constant value required
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:78: Error: constant value required
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:79: Error: garbage at end of line
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:107: Error: constant value required
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:109: Error: constant value required
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:112: Error: missing operand
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:124: Error: symbol `loop' is already defined
C:\Users\\AppData\Local\Temp/ccYDtlBP.s:133: Error: operation combines symbols in different segments

My favorite error is the "garbage at the end of line"  :-DD

My objective, however, is not to learn asm. And as a matter of fact, I have resigned myself from any sort of programming expectations. I just don't seem to be able to do it.
« Last Edit: January 17, 2019, 06:10:08 pm by metrologist »
 

Offline fsr

  • Regular Contributor
  • *
  • Posts: 169
  • Country: ar
Re: ASM to Arduino
« Reply #11 on: January 17, 2019, 07:35:02 pm »
Arduino IDE is not happy with the ASM code.

fsr, I simply want to load the PPSDiv code to arduino. I think it works "out of the box" so I do not expect to have to modify it, I just want to get the code onto a 328p chip. I think there is a way to burn the hex file which is included. I recall burning the bootloader onto bare chips so I may review how I did that, so many years ago... But to take a step back, I don't really need this code, but any code that will do the same thing. I could also just order some pic chips and try the picdiv code, but I don't have those chips and I have plenty of Arduinos.


This is the ASM code I tried to compile in Arduino IDE.

[...]


I see, so you want to get the .HEX into an atmega328p. I suppose that you don't have an AVR programmer, but you can see how to use one Arduino as a programmer for an stand-alone AVR in this link: https://learn.sparkfun.com/tutorials/installing-an-arduino-bootloader/all

Or you could buy a cheap-ass usbasp programmer to do the same. I use one of them, and the commands for avrdude that i use most often are the following:

This line sets the fuses (normally you only need to do this once). Check the attached libreoffice document with details about the fuses settings (those are for 5v operation with full-swing crystal oscillator, and some other miscellaneous options, YMMV):
avrdude -p m328p -c usbasp -U lfuse:w:0xf7:m -U hfuse:w:0xd9:m -U efuse:w:0xfc:m

And this line is to upload the hex file:
avrdude -p m328p -c usbasp -U flash:w:YOURHEXFILE.hex

Note that the AVR won't function as an Arduino anymore after doing that. If you want to make it work again, you possibly will need to reprogram the fuses, and upload another HEX file with the Arduino bootloader to the AVR.
« Last Edit: January 17, 2019, 07:44:04 pm by fsr »
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: ASM to Arduino
« Reply #12 on: January 17, 2019, 09:04:08 pm »
Oh yeah, that's right, I used an Arduino to program the bootloader. I also bought one of those AVRASP devices but could never get it to work in windows.

I also had AVR studio set up and used it a couple times, but it seemed to have so much overhead that I stopped using it.

I'll breadboard up a chip and if I get it working I'll make this a dedicated divider module.
 

Offline fsr

  • Regular Contributor
  • *
  • Posts: 169
  • Country: ar
Re: ASM to Arduino
« Reply #13 on: January 18, 2019, 01:55:55 am »
Did you use avrdude with the usbasp? I used it quite a lot of times under windows, and no problems. Should work.
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: ASM to Arduino
« Reply #14 on: January 18, 2019, 03:22:02 am »
I think it was that Windows did not see the ASP device. That might be another topic.

Because I was reading about it and that I found a vendor 10 miles away, I ordered 10 of PIC12F675. This seems a bit better for my application, and I have a pickit2 that needs to be used at least twice.

Thanks  :-+
 

Offline fsr

  • Regular Contributor
  • *
  • Posts: 169
  • Country: ar
Re: ASM to Arduino
« Reply #15 on: January 18, 2019, 09:56:41 pm »
You're welcome.
Makes sense to have the PICs, then you can just program them easily and use them with the original project.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ASM to Arduino
« Reply #16 on: January 19, 2019, 10:21:47 am »
Quote
Arduino IDE is not happy with the ASM code.
It's not "Arduino code", it's an "Atmel Studio" "Project", written for a bare AVR chip.
That means:
  • the .ASM files are written for the Atmel Assembler.
  • The won't get compiled from the Arduino IDE, which only knows about the Gnu Assembler.
  • If you somehow compiled them with the Atmel Assembler, they still wouldn't be linkable into an Arduino "sketch", because the Atmel Assembler doesn't produce linkable object files.
  • Your best bet is to build the project with Studio, and upload it to your Arduino via the bootloader.
  • (actually, the idea of converting it all to inline ASM isn't awful, since it's quite short.  You'll need to mess with the options for handling symbols in inline ASM, though.  And don't use symbols that are already defined ("loop", "init"))h
  • Except that if it's capable of dividing 10MHz, it's probably expecting the "input frequency" on the clock pin, which is not compatible with Arduino hardware (that already has a 16MHz resonator there.)
Quote
https://ucexperiment.wordpress.com/2016/03/04/arduino-inline-assembly-tutorial-1/
inline assembly isn't the only way, nor even the best way, to include ASM in an Arduino sketch.  As of recent versions (not THAT recent), it will happily accept .S files in the sketch directory.)
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ASM to Arduino
« Reply #17 on: January 19, 2019, 10:56:44 am »
Hmm.  Your attempt to produced inline ASM was pretty credible.
I already mentioned symbol conflicts...
Quote
#include <m32def.inc>
Get rid of that; it's avr assembler, and the equivalent is already included.
Quote
        "OUT        DDRA,R16 \n"
But symbols need to be referenced "specially."  See https://www.nongnu.org/avr-libc/user-manual/inline_asm.html
(The tutorial you found wasn't very good.)
Quote
"WLP1:   DEC      \n"
There was a type or two - missing argument for DEC, here.  And I think one other...
Quote
My favorite error is the "garbage at the end of line"
And THAT, of course, is the gnu assembler's way of telling you that "08" is not a valid octal number.  Obviously.It's a C think that nn is decimal, 0nn is octal, and 0xnn is hex...
Try this version:
Code: [Select]
#include <avr/io.h>

void setup(){
asm (
        "LDI    R16,0xFF \n"
        "OUT    %0,R16 \n"
        "CLR    R15 \n"
        "CLR    R14 \n"
        "CLR    R13 \n"
        "CLR    R12 \n"
        "CLR    R11 \n"
        "CLR    R10 \n"
        "CLR    R9 \n"
        "CLR    R8 \n"

"pploop: \n"
        "LDI    R16,0x0A \n"   
        "INC    R15 \n"
        "CP     R15,R16 \n"
        "BRNE   nd1 \n"
        "CLR    R15 \n"
"nd1:   BRNE    nd1w \n"
        "INC    R14 \n"
"nd1w:  CP      R14,R16 \n"
        "BRNE   nd2 \n"
        "CLR    R14 \n"
"nd2:   BRNE    nd2w \n"
        "INC    R13 \n"
"nd2w:  CP      R13,R16 \n"
        "BRNE   nd3 \n"
        "CLR    R13 \n"
"nd3:   BRNE    nd3w \n"
        "INC    R12 \n"
"nd3w:  CP      R12,R16 \n"
        "BRNE   nd4 \n"
        "CLR    R12 \n"
"nd4:   BRNE    nd4w \n"
        "INC    R11 \n"
"nd4w:  CP      R11,R16 \n"
        "BRNE   nd5 \n"
        "CLR    R11 \n"
"nd5:   BRNE    nd5w \n"
        "INC    R10 \n"
"nd5w:  CP      R10,R16 \n"
        "BRNE   nd6 \n"
        "CLR    R10 \n"
"nd6:   BRNE    nd6w \n"
        "INC    R9 \n"
"nd6w:  CP      R9,R16 \n"
        "BRNE   cready \n"
        "CLR    R9 \n"
"cready: CLT \n"
        "BLD    R8,0 \n"
        "OUT    %1,R8 \n"
        "LDI    R17,8 \n"
"WLP0:  DEC     R17 \n"
        "BRNE   WLP0 \n"
        "CLR    R8 \n"
        "LDI    R16,0x05 \n"
        "SET \n"
        "BLD    R8,0 \n"
        "CP     R15,R16 \n"
        "BRCS   d2 \n"
        "BLD    R8,1 \n"
"d2:    CP      R14,R16 \n"
        "BRCS   d3 \n"
        "BLD    R8,2 \n"
"d3:    CP      R13,R16 \n"
        "BRCS   d4 \n"
        "BLD    R8,3 \n"
"d4:    CP      R12,R16 \n"
        "BRCS   d5 \n"
        "BLD    R8,4 \n"
"d5:    CP      R11,R16 \n"
        "BRCS   d6 \n"
        "BLD    R8,5 \n"
"d6:    CP      R10,R16 \n"
        "BRCS   d7 \n"
        "BLD    R8,6 \n"
"d7:    CP      R9,R16 \n"
        "BRCS   ok \n"
        "BLD    R8,7 \n"
"ok:    OUT     %1,R8 \n"
        "LDI     R17,3 \n"
"WLP1:   DEC     R17 \n"
        "BRNE    WLP1 \n"
        "NOP \n"
        "RJMP  pploop\n"
        :: "I"(_SFR_IO_ADDR(DDRD)), "I"(_SFR_IO_ADDR(PORTD))
);
}
« Last Edit: January 19, 2019, 10:59:20 am by westfw »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: ASM to Arduino
« Reply #18 on: January 19, 2019, 11:00:26 am »
(change PORTD and DDRD back to PORTA and DDRA for your m32...)
 

Offline metrologistTopic starter

  • Super Contributor
  • ***
  • Posts: 2212
  • Country: 00
Re: ASM to Arduino
« Reply #19 on: January 19, 2019, 01:41:58 pm »
Thanks for looking at this and the code. I added void loop(){} and your code compiled. So now we just need the 10M coax to the clock (there are two clock pins?) and we can find the divided signals on other pins? I do have a handful of bootloaded bare 328p chips.
 

Offline cncjerry

  • Supporter
  • ****
  • Posts: 1283
Re: ASM to Arduino
« Reply #20 on: January 19, 2019, 06:39:38 pm »
I recently built a half dozen of the PICDIV chips.  I also duplicated the front end from the TAPR TADD-2 to drive them.  You can probably get away without the input transformer but I had them so I used them. The PICDIVs are the way to go as they only have a few ps of jitter.  The PIC, I think it is a 12F675? , can't remember, but they are cheap.  I bought a tube of them, programmed a bunch of different versions, and have a socket to emplace them when needed.  I programed them with a PICIT III, I think it is called, it is the red version, and it takes about a minute.

I'd have to look at Ulrich's code but I know he is a time nut so I'm sure the ADEV variance using the Arduino project vs the PICDIV would be low if even measureable.  But when you conside how cheap the PICs are and how well the PICDIVs work, I recommend you try some of them as well.

Jerry
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 4784
  • Country: pm
  • It's important to try new things..
Re: ASM to Arduino
« Reply #21 on: January 19, 2019, 06:54:51 pm »
Under normal conditions the only measurable jitter you may get could be caused by noisy Vcc only, imho.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf