Pages: [1]   Go Down
Author Topic: Arduino and Assembler  (Read 2230 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
I wanted to re-use some assembler code (in a .S file). After searching this forum and not finding how to do it, I came up with the following solution.
I just wonder if there's another way to avoid writing to the libraries folder.

1. create a folder in libraries, let's say "RoutinesASM"
2. inside this folder create a file "RoutinesASM.h" containing something like:
Code:
extern "C" {
  void Increment_a_Number(void);
}
3. inside this folder create a file "RoutinesASM.S" containing the assembler routines, like:
Code:
#include <avr/io.h>
.extern a_number
.global Increment_a_Number
Increment_a_Number:
      push r16
      lds r16, a_number
      inc      r16
      sts      a_number, r16
      pop r16
      ret
4. the sketch must include the .h file, like:
Code:
/*
  Program:  Test Arduino and Assembler
  Autor:  [iard]
  Date:   2010-06-09
*/

#include <RoutinesASM.h>

volatile int a_number;
// load the variable from RAM and not from a storage register

extern void Increment_a_Number(void);

void setup() {
  Serial.begin(9600);
}

void loop() {
  a_number = 5;  // some initial value
  Serial.print("Initial number = " );
  Serial.println(a_number);
  Increment_a_Number();  // call assembler routine
  Serial.print("Incremented number = " );
  Serial.println(a_number);
  delay(1000);
}

That's it... it won't work if the .h and .S files are in the same folder as the .pde sketch.
Any suggestions?
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
    I think the problem lies in where the compiler looks for include files. I think you'd have to modify the compile scripts so they include the .pde folder as well.
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could try using "" instead of <> for the #include.

<> tells the compiler to search the libraries
"" tells the compiler it's part of the project

Edit:
Unfortunately the Arduino IDE doesn't really support .S files.
If you're running Arduino from source you could add the following changes. These changes and the change of <> to "" made your example compile here.

In app\src\processing\app\Sketch.java change
line 1345 from
Code:
if (sc.isExtension("c") || sc.isExtension("cpp") || sc.isExtension("h")) {
to
Code:
if (sc.isExtension("c") || sc.isExtension("cpp") || sc.isExtension("h") || sc.isExtension("s")) {

line 1800 from
Code:
return new String[] { "pde", "c", "cpp", "h" };
to
Code:
return new String[] { "pde", "c", "cpp", "h", "s" };
« Last Edit: June 11, 2010, 05:02:38 am by qistoph » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@mromani
Thanks, but the problem is that I can't find any relevant settings in "preferences.txt" and I don't know where else to look. I think the problem is related to the way Arduino IDE creates the files in the temp folder before invoking the AVR GCC compiler, because the .h file is there, but the .S is not smiley-sad

@qistoph
Thanks, that was my first attempt, before moving the .h and .S files to the libraries folder. Error message: undefined reference to `Increment_a_Number'
Edit: thanks again, I'll wait for the next release and until then I'll use the workaround to move the .h and .S files to libraries
« Last Edit: June 11, 2010, 05:32:05 am by iard » Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've taken the liberty of submitting this feature request to the Issue list:
http://code.google.com/p/arduino/issues/detail?id=274

Let's hope they'll put it in the next release.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And yet another "THANK YOU!"
You reported the issue in a very professional manner!
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@iard:

by "modifying the compile scripts" I meant something along the lines of what qistoph has written (I wouldn't have been able to put toghether his solution, though)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks mromani, I just hoped there was a way to modify the path in "preferences.txt" or a makefile or something. I have the Arduino IDE installed on Windows and Ubuntu (on a slow notebook), so I'm not that eager to recompile...
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

iard, if you're talking about recompiling the Arduino IDE;
that doesn't take really long and even isn't really complicated.

All you need is a JDK (Java Development Kit) and Apache Ant.
Compiling takes less than 2 minutes here (on a Dual Core @ 2.4 GHz), but still it wouldn't take long on any other computer.
« Last Edit: June 12, 2010, 07:22:43 am by qistoph » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm, on my Windows machine I could do it... and maybe I will in the weekend, but how about on Ubuntu Netbook Edition running on a Asus EeePC 701 (512 MB RAM, Celeron M at 900 MHz, HDD 4GB, always running out of free disk space)  :-?
I know I should stick to the AVR GCC, gavrasm and avrdude on the notebook, but the fact that I can take it anywhere with me and just plug the Arduino, start a nice and clean-looking IDE, make some changes and click on Upload seems so cool...
Logged

Pages: [1]   Go Up
Jump to: