Programming the NXT in C

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Programming the NXT in C

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
tcwan
Posts: 186
Joined: 30 Sep 2010, 07:39

Re: Programming the NXT in C

Post 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.
Last edited by tcwan on 08 Mar 2012, 07:19, edited 1 time in total.
tcwan
Posts: 186
Joined: 30 Sep 2010, 07:39

Re: Programming the NXT in C

Post 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.
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Programming the NXT in C

Post 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?
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
pepijndevos
Posts: 175
Joined: 28 Dec 2011, 13:07
Location: Gelderland, Netherlands
Contact:

Re: Programming the NXT in C

Post 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.
-- Pepijn
http://studl.es Mindstorms Building Instructions
pbenco
Posts: 71
Joined: 29 Sep 2010, 09:43
Contact:

Re: Programming the NXT in C

Post 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
Last edited by pbenco on 09 Mar 2012, 21:23, edited 2 times in total.
pbenco.wordpress.com/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Programming the NXT in C

Post 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) ?
pbenco
Posts: 71
Joined: 29 Sep 2010, 09:43
Contact:

Re: Programming the NXT in C

Post 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
pbenco.wordpress.com/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Programming the NXT in C

Post 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...)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Programming the NXT in C

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 15 guests