Page 1 of 3

Programming the NXT in C

Posted: 08 Mar 2012, 04:43
by mattallen37
I really like the EFW, but in order to take advantage of it, I need to use NBC (or NXC which gets compiled into NBC). Is there a way to program in C/C++ without needing the NBC layer? I would like to be able to compile a C like program directly into machine code (or maybe assembler), so there is no room for automatically generated temporary variables and all the rest. I know I would need to wrap up any "NXC" function I wanted to use.

Re: Programming the NXT in C

Posted: 08 Mar 2012, 06:06
by tcwan
mattallen37 wrote:I really like the EFW, but in order to take advantage of it, I need to use NBC (or NXC which gets compiled into NBC). Is there a way to program in C/C++ without needing the NBC layer? I would like to be able to compile a C like program directly into machine code (or maybe assembler), so there is no room for automatically generated temporary variables and all the rest. I know I would need to wrap up any "NXC" function I wanted to use.
I'm not sure I understand your question, are you saying that you'd like to use the features in the Enhanced Firmware but would like to compile directly to ARM machine code?
I think you'd basically be writing your code as part of the enhanced firmware, since calling the functions in the Enhanced Firmware directly requires knowing the function entry addresses, which is going to change from one firmware compile to another depending on the firmware source files (i.e. a compiled program won't work with a new version of the firmware without recompiling for that version). I don't know if there is a way to collate the function entry addresses and resolve them at runtime with your executable otherwise. It is definitely not available for the normal firmware images as far as I know, since debug information is not stored within the firmware image.

Re: Programming the NXT in C

Posted: 08 Mar 2012, 07:16
by tcwan
tcwan wrote: I think you'd basically be writing your code as part of the enhanced firmware, since calling the functions in the Enhanced Firmware directly requires know the function entry addresses, which is going to change from one firmware compile to another depending on the firmware source files (i.e. a compiled program won't work with a new version of the firmware without recompiling for that version).
On further thought, there is a way to solve this problem at the expense of memory. You can define an array of function pointers which is used to access the firmware routines that you're interested in, and all calls to firmware routines would need to go through this array. It is similar to the ioctl() function call in C where you specify the routine you want to call as a parameter (this can be resolved into a double pointer dereference instead of using ioctl() itself). By placing this array in a fixed location in memory, it is possible to call the correct function without worrying about where the actual address is located.

E.g., if pointer_array[3] has the function address for clearLCD() (hypothetical firmware function), you'd access clearLCD() using (*pointer_array[3])().

This function array would need to be compiled as part of the firmware image, and linked at a fixed memory address, for you to access it from your own program.

Re: Programming the NXT in C

Posted: 08 Mar 2012, 10:21
by spillerrec
mattallen37 wrote:... so there is no room for automatically generated temporary variables and all the rest. ...
Are you concerned about the NBC layer or are you concerned about inefficient compiling from NXC to NBC?

Re: Programming the NXT in C

Posted: 08 Mar 2012, 10:29
by pepijndevos
I heard the enhance firmware is made to recognize certain header, in which case it directly executes the machine code, instead of interpreting the bytecode.

Re: Programming the NXT in C

Posted: 08 Mar 2012, 13:29
by pbenco
hello mattallen37

You can use NXTGCC, which is a GCC compiler, with the Lego opensource (*) firmware sources. You can develop directly into the arm codebyte.
Eclipse add-on is available, to replace bricxcc as an IDE.
Hope this help
Ben

http://nxtgcc.sourceforge.net/

* previously made a mistake, pretending NXTGCC used enhanced firmware instead of Lego standard sources. Sorry

Re: Programming the NXT in C

Posted: 08 Mar 2012, 14:25
by HaWe
+1

is there also another IDE available instead of Eclipse?
Eclipse appears to me like a Dinosaur - is there something really simple like BCC or maybe like dev cpp (compile - link - make - download - run in 1 combined step) ?

Re: Programming the NXT in C

Posted: 08 Mar 2012, 14:46
by pbenco
Dear doc-helmut

Any IDE able to support cross compilation under GCC is able to support NXTGCC

here you will find an usefull powerpoint covering C programmation of NXT under opensource/commercial IDE.

Inside the Open Source Lego Mindstorms NXT

Speakers:

Rasmus Ulslev Pedersen (Department of Informatics at the Copenhagen Business School)

Abstract: The tutorial attendees will be presented with a technical overview of the Lego Mindstorms NXT. Alternative approaches to firmware and user-level programming are introduced while continuously relating to the underlying hardware of NXT. The audience will afterwards be able to choose between alternative approaches when it comes to possible future research, teaching, or community building with NXT.

http://nxtgcc.sourceforge.net/EMSOFT-20 ... torial.pdf
Best regards
Ben

Re: Programming the NXT in C

Posted: 08 Mar 2012, 15:00
by HaWe
which simple IDE would you recommend specifically, personally?
I have no experience with other IDE's at all except BCC, dev cpp, Borland C++ Builder (4/5)
(the tutorial is a little hard to understand...)

Re: Programming the NXT in C

Posted: 08 Mar 2012, 18:22
by mattallen37
First, I'd like to say that this would certainly not replace NXC for me. It would primarily be used as a learning experience.

I'm not exactly sure what I'm looking for. I want to program at a lower level, with support for pointers etc. but I want to be able to do it through USB (I don't want JTAG to be required).

I've heard about Bare Metal on the NXT. Perhaps that's what I should try.

I have also heard about NXTGCC. Can you tell me a little more about it, and how to get started with it? I would like to use the BCC IDE if possible.