Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12747 Discussions

Dynamically modify NIOS code.

Altera_Forum
Honored Contributor II
1,319 Views

Hi all, 

I believe I may have an interesting topic for discussion here. I've been working heavily with NIOSII processors for over a year now. We are now using them quite a bit in our systems. The particular system in question has 5 NIOS processors running in it. One of those processors acts as a master which handles all communications with a host PC over a USB 2.0 connection. The other four processors are slaves which receive commands from the master processor. Their purpose is irrelavant to the conversation and for discussion purposes it might as well just be 1 slave processor. 

What I would like to do is modify the code of the slave processors while they are running. I have no problem accessing and overwriting their memory. My question is more software based as I have limited experience with the gnu-tools and specifically the linker.  

I have thought of a few ways to do this. The simplest is to take advantage of the new reset feature provided with NIOS II 6.0. I can simply have the master processor hold the slave processors in reset while their code is reloaded. This is not my ideal solution. It's not very clean and I don't really want the slave processors to go through their "boot up" sequence everytime. 

The second is what I'd like to do but I'm not sure how or if it's possible. Their is really only one function that I need to overwrite. I would like to dedicate a chunk of memory to each processor just to store the code of this function. What I'd like to do is use the nios2-gcc tool suite to generate just the memory contents of this piece of memory (which would be the function in question). Then I can upload the new memory contents, tell the slave processors to flush their instruction caches, execute the function, then do it all over again. 

So any experience with the NIOS IDE and the gnu tools that might prove useful here would be very much appreciated. Again, the hardware is no problem at all, just how to generate the desired memory contents. 

 

Thanks, 

Jake
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
626 Views

Hello jakobjones, 

 

if I understand you correctly, you want different code executed on your coprocessor. This code is generated off-line on the development system, not on-the-fly on your master. 

 

The gnutools offer to put your code / data / whatever of your program in different "sections". Normally these sections are .text for function code .data for initialized global variables and .bss for unitialized global variables. But you can instruct gcc to put your "special function" in a section all of its own. Maybe call it ".replace". Then modify your linker script to link this section to a known address. Then you can use the program objcopy (well, nios2-elf-objcopy) to generate a s-record file or a binary file. Then you can download this to your system. 

 

Please tell if my explanation is too short.
0 Kudos
Altera_Forum
Honored Contributor II
626 Views

wpaulus, 

Thank you for your reply. I believe you are pointing me in the right direction. I had looked at the objcopy command but was somewhat overwhelmed by all the options. In my NIOS C code I am assigning the desired function to the desired memory address using the "section" attribute. In fact I can run the code if I do an ALT_LOAD_SECTION_BY_NAME command within my code. But what I'd like to do is generate the binary file containing the memory contents for this seperate piece of memory and it sounds like objcopy is the way to do it.  

 

Do you think you could provide a little more detail on the objcopy command? I've got the .elf file. The object dump indicates that my function is at the desired memory address. I know the name of the section where I want the code to reside and I want a raw binary output. 

 

Again thank you and any additional help is welcome. 

 

Thanks, 

Jake
0 Kudos
Altera_Forum
Honored Contributor II
626 Views

hello jakobjones, 

 

you might try something like this: 

 

nios2-elf-objcopy -O binary -j <your_section> <input_elf_file> <output_file>  

-O you tell objcopy what output format you would like. 

-j process just the section you are intereset in( e.g. &#39;.text&#39;). 

 

Bye, 

Wolfgang
0 Kudos
Altera_Forum
Honored Contributor II
626 Views

Wolfgang, 

Thank you for your help. Since your last post I&#39;ve done some playing around and I believe I&#39;ve got it. It&#39;s pretty much exactly as you have indicated above. Thank you again for your help. 

 

Jake
0 Kudos
Reply