How to process .S file with GCC project for TWI (I2C)

Go To Last Post
24 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm programming an ATtiny4313 to read a HMC6352 compass module. I'm using Studio 6 and GCC, WinAVR 20100010.

Searching for some source code to help me with the TWI interface, I've arrived at files written by Peter Fluery. The version that does not use the hardware module TWI as found in the "mega" AVRs has an assembly language file i2cmaster.S which creates functions that can be called from C.

My question is, what are the mechanics of incorporating this file into my project? I have vague notions of assembling the file into object code and then somehow linking that to the overall project, but don't have an idea of the specific steps.

Some specific guidance or pointers to documents I should review would be appreciated.

Thanks,

Nick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I'm using Studio 6 and GCC, WinAVR 20100010.
Are you sure? I thought AS6 only works with the Atmel toolchain.

With AS4 you simply add the .S file to the project just like any other C file, should be the same with AS6.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I thought AS6 only works with the Atmel toolchain.

Wrong. It works just fine, at least as far as I can tell.

I have not tested including assembler files, though.

Please be aware that for the GNU too-chain there is a difference between a .s file and a .S file. Too early in the morning to remember which is which, but one is an intermediate file from the C/C++ compiler, the other is the one to use if you write your own assembler source. Use the wrong one for your source and it might not work as you wished.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Depends how you are compiling. If you are using an Atmel Studio or AVR Studio IDE, just add the .S file to your project like any other source file, and it will work just fine. If you are using the old WinAVR makefile templates, you need to add the .S file to the "ASM=" line of your makefile.

If you are calling GCC directly with your own makefile, you call GCC exactly as you would for C files, but add an extra "-x assembler-with-cpp" switch.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

If you are using an Atmel Studio or AVR Studio IDE, just add the .S file to your project like any other source file, and it will work just fine.

Dean! Can you confirm that this will work even if you use Atmel Studio with a WinAVR tool chain flavour? (I did not have time to test this myself this morning..)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a project with AS4 which has a .S LED display driver file attached and it works.

I looked at AS6 to see if I could switch from the Atmel toolchain to winAvr20100110 but could not see an easy or simple way to do it. I will import the AS4 project and try it out.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I looked at AS6 to see if I could switch from the Atmel toolchain to winAvr20100110 but could not see an easy or simple way to do it.

You need to configure a "Toolchain Flavor", which will then allow you to pick different toolchains from a common pool of configurations for each project (as opposed to AS4 where you needed to configure this for every project individually). See:

http://www.atmel.no/webdoc/atmel...

Quote:

Dean! Can you confirm that this will work even if you use Atmel Studio with a WinAVR tool chain flavour? (I did not have time to test this myself this morning..)

It will work with any GCC toolchain, since it's the IDE that drives it. The only caveat is that if you use a custom makefile rather than let AS6 call GCC for you, you will need to add the appropriate rules yourself.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

John,

Well I was going to talk you through it by doing it on my machine but AS6 just reminded me that it won't list network drives (actually a Vbox share but they act like net shares) and I actually have WinAVR on my E: which is such a share.

So I cannot do it on my machine. Maybe one day Atmel can explain to me their reluctance to show net mapped drives in their file selector?

Cliff

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well OK, I suppose I can manually type the path into the boxes. So the way I did it was to use Tools-Options...-Toolchain-Flavour Configuration then with the Toolchains drop down set to "Atmel AVR 8-bit" I used [Add flavour] and then set the Toolchain path to be \WinAVR20100110\bin and the make path to be \WinAVR20100110\utils\bin and saved all that.

Then I picked a GCC project and went to the Properties. On the Advanced tab I set Toolchain Flavour to be the new "WinAVR" I just added. Then I rebuilt the solution and saw:

	Task "RunCompilerTask"
		E:\WinAVR-20100110\utils\bin\make.exe all 
		Building file: .././GccApplication1.c
		Invoking: AVR/GNU C Compiler : (WinAVR 20100110) 4.3.3
		"E:\WinAVR-20100110\bin\avr-gcc.exe" -funsigned-char -funsigned-bitfields -Os -fpack-struct -fshort-enums -g2 -Wall -Wundef -c -std=gnu99 --save-temps -fverbose-asm  -MD -MP -MF "GccApplication1.d" -MT"GccApplication1.d" -MT"GccApplication1.o"  -mmcu=atxmega128a1   -o"GccApplication1.o" ".././GccApplication1.c" 
		Finished building: .././GccApplication1.c

thus proving it had built with WinAVR20100110 rather than the Atmel toolchain. Here are the two relevant dialogs...

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the replies.

I'd just recently installed Studio 6 and still have Studio 4 on the PC. I tried the project in Studio 4 and get the same result: About 175 error messages that appear to indicate that the .S file text is being treated as C text. Or in some cases, assembler is recognized and the error message says to use the in-line assembly keyword.

This file, as-found, has the uppercase .S extension. Interesting that file associations were set up for it to be recognized by my PC as a Studio 6 document.

I'm going back to kindergarten here, but I'm no longer sure how to "add the file to the project". I thought there used to be a project window with a directory tree where I could add files. In fact, the Studio 4 Help file shows this. But I can't find it. Looked under Project, View, and Window, plus configuration options but no luck. Even deleted my project and re-created it, but still no opportunity during the creation process (Project Wizard) or afterwards, to specify files. Are all source files in my project directory automatically part of the project?

Since I can't "add files to the project", I've been using "include" statements instead. That works fine with .C and .h files. So I'm not sure if this is related to my problems with the .S file or not but I thought I'd mention it.

I've been away from AVR/GCC programming for about a year and so am having to re-train myself. But of course, the use of the .S file is completely new to me.

Thanks,

Nick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I'm going back to kindergarten here, but I'm no longer sure how to "add the file to the project".

Are you talking about AS4 or AS6? (though I guess they are very similar in fact).

In AS6 look at the "Solution Explorer". Within it there will be the name of the project you created (it has habit of picking stupid names like GccAplication1 unless you tell it otherwise). Right click the name of the project and the context menu has Add> on it. The sub-menu from that has Existing Item... so just use that and in the file selector pick any .c or .S files you want to add to the project. The plan is then that when you build the .c files are compiled by avr-gcc with -c and the .S file assembled with and added -x assembler-with-cpp

That latter option says "pass the file through the C preprocessor (so things like #include and #define are processed) and then assembler it with avr-as".

In AS4 it's very similar. There's no "Solution Explorer" but the Project window at the side has a tree diagram very similar to solution explorer and within that is "Source Files". Right click on this and once again use the "Add Existing Source File(s)..." to make it aware of source files that should be compiled/assembled. This too passes "-x assembler-with-cpp" when invoking avr-gcc for the .S files.

Quote:

I've been using "include" statements instead.

Not a great idea. And it most definitely will not work if you #include a .S file within a .c file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:

I'm going back to kindergarten here, but I'm no longer sure how to "add the file to the project".

Are you talking about AS4 or AS6? (though I guess they are very similar in fact).

Quote:

I've been using "include" statements instead.

Not a great idea. And it most definitely will not work if you #include a .S file within a .c file.

OK, I've had some success, I think. I added my file to the "Solution Explorer" tree of Studio 6. After getting rid of the "#include" statements, which caused multiple definitions errors, I got a good build.

So I'm very pleased with that. Now I have to massage the .S code to work with my specific AVR and see if I can get these chips to talking.

I *still* am unable to find the Project window in Studio 4. Very strange - I've looked everywhere. So I'm still open to suggestions on that.

Thanks for all the help.

Nick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does your AS4 not look something like this then? ...

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Does your AS4 not look something like this then? ...

No, but I swear it used to. Let me ask one back ... if you click the x and turn off the project window, what do you push, click, or select to turn it back on?

Nick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

View-Toolbars-AVRGCC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Now I have to massage the .S code to work with my specific AVR
Remember that the ASM layout is that of GAS and NOT the Atmel assembler. But I guess you know that already. :-)

You will find a GAS ASM demo project with winAvr in case you haven't seen it already.
C:\WinAVR-20100110\doc\avr-libc\examples\asmdemo

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

View-Toolbars-AVRGCC

OK, I've got it back now. I turned that option off and on with no result but eventually poking around I found it. I think I'd hidden or minimized it so completely that I just about couldn't find it. Embarrassing ... I did look into that possibility a time or two earlier including completely clearing the workspace, but for some reason didn't find it. There was nothing left but the title bar and I didn't recognize it.

Tried to build the project after adding the .S file and it failed, but I'm not overly concerned. I think maybe it's the result of having both Studio 6 and Studio 4 working on the same project in the same directory and "4" is reading an intermediate file produced by "6" ... or something. At least now Studio 4 does attempt to assemble the .S file.

Thanks,

Nick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hmm my post disappeared. :roll:

Here is a screenshot of the above project. I managed to switch between winAvr (build at 476 byte) and the toolchain (build at 504 byte), the shortcut seems to be to go to the project properties and change it from there.

Attachment(s): 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

abcminiuser wrote:
If you are calling GCC directly with your own makefile, you call GCC exactly as you would for C files, but add an extra "-x assembler-with-cpp" switch.
That'n not needed with .S files because .S files are wired to "-x assembler-with-cpp" just the same way .c is wired to "-x c".

You need the "-x assembler-with-cpp" if you prefer an other extension like .S or .ss, for example if you prefer .asm.

Using .S can be a PITA on Windows because that OS does not properly distinguish between .S and .s so that if you have a clean rule for .s you may also delete .S.

Autsch!

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My project import issue posts moved here https://www.avrfreaks.net/index.p...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
Quote:
Remember that the ASM layout is that of GAS and NOT the Atmel assembler. But I guess you know that already. :-)

Well, don't give me too much credit. I've done most of my programming in assembler, but there's a lot that I'm not familiar with in this .S file. I wonder if there's a document that could give me some education there?

For example:

	.stabs	"",100,0,0,i2c_delay_T2
	.stabs	"i2cmaster4313.S",100,0,0,i2c_delay_T2
	.func i2c_delay_T2	; delay 5.0 microsec with 8 Mhz oscillator
i2c_delay_T2:    ; 4 cycles
	rjmp 1f      ; 2   "
1:	rjmp 2f      ; 2   "
2:	rjmp 3f      ; 2   "

I *think* .stabs has something to do with debugging info. And .func and .endfunc are self-explanatory, I suppose.

I assume the line numbers are local labels and 1f, 2f, etc. say to go *forward* to that label(?)

Also, comments in the code say that the function return value is contained in registers r25:r24. I don't find any corresponding statements in the code or header file to make that association. (I could have missed it.) Or possibly that's standard for this MCU and contained in the gcc include file of definitions for the specific MCU(?)

I'm pleased that my project does build properly, but haven't yet gotten a successful TWI transaction. Next I'm going to mangle the code slightly to produce continuous pulse trains on SDA and SCL and verify them at ~100kHz with an oscilloscope.

Nick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Well, don't give me too much credit. I've done most of my programming in assembler, but there's a lot that I'm not familiar with in this .S file. I wonder if there's a document that could give me some education there?

That looks like a ".s" file and not a ".S" file - the difference is that the lowercase version is automatically generated from the compiler, while the uppercase version is hand-written. The automatic version contains a lot more debug information and other directives that you don't normally need to worry about.

The Atmel Assembler (.asm) files is one assembly dialect, and is incompatible with the GNU toolchain, so programs you write in that can't be linked against C code. It's got a fairly limited feature set, but works well.

The GNU Assembler (GAS) on the other hand is very complex and feature rich, and allows you to link your assembly code against C, C++ and whatever else the GNU toolchain is able to compile.

Quote:

I *think* .stabs has something to do with debugging info. And .func and .endfunc are self-explanatory, I suppose.

Those are directives generated automatically by the compiler - ignore them. The labels are also automatically generated, thus the names are short and non-descriptive. You should find hand-written assembly files much easier to read.

Quote:

Also, comments in the code say that the function return value is contained in registers r25:r24. I don't find any corresponding statements in the code or header file to make that association. (I could have missed it.) Or possibly that's standard for this MCU and contained in the gcc include file of definitions for the specific MCU(?)

This assumes knowledge of something called the Application Binary Interface (ABI), which is the contract the compiler agrees to follow in regards to what registers are used for what purposes and how functions communicate with each other. You can find information on the AVR-GCC ABI here:

http://www.nongnu.org/avr-libc/u...

If your program does not respect the AVR-GCC ABI, you'll have terrible issues when you try to link your assembly code against compiler generated code.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, Dean - that's great info. I was a bit concerned that the registers used might be processor specific, but your link took care of that issue.

Nick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

They are architecture specific, but not model specific.

There's one exception here for the AVR8s however: the really, really tiny ATTINY9/10/etc. chips with the AVRL core has a slightly different ABI, as they do not have CPU registers r0-r15.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!