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++
12599 Discussions

Upgrading Nios Software - external flash

Altera_Forum
Honored Contributor II
2,419 Views

Dear all, 

 

I would like to upgrade my NIOS code by RS232. The NIOS runs from external memory.  

 

This is my procedure: 

 

- I transfer the .elf file to the .flash file by the instruction elf2bin. (bin file for dowloading to the flash) 

 

- I have a RS232 GUI from which I can send the bin file to the flash. 

 

I have written a procedure which is stored in FPGA internal memory. This procedure receives the bin file from the UART and writes the data to flash. 

(byte by byte) 

 

Now, what is the problem: my problem is that my NIOS stops working from the moment I erase the flash sector in which the original NIOS code is stored. Nevertheless, the procedure that erases this sector is placed internal. And I keep running my code from internal memory untill the new nios bin file is stored in the flash. 

 

The nios core is placed from 0x00000.  

 

When I declare an instruction to be placed in internal memory. 

 

ex following function is placed in internal memory 

 

void test (void) 

 

 

 

Will the instruction always be in internal memory or will it be placed in internal memory from the moment this instruction is called? 

 

Has someone an idea what the problem is? Has someone experience with how to upgrade a NIOS that is running from external memory. 

 

Regards
0 Kudos
28 Replies
Altera_Forum
Honored Contributor II
610 Views

probably problem is that you erasing the flash also erasing your self. 

to make firmware update you need to do one of following things. 

 

1. make bootloader. 

this program is placed in internal rom or external flash in area that will never erased or reprogrammed on normal condition. set reset pointer to that area. when bootloader starts it will decide what to do: update firmware or execute firmware. 

advantages: easier to debug, your system will stable even on errors during updating. 

disadvantages: you need to make two modes - firmware update and normal mode and switch between them. 

 

2. include updating subprogram in your main program, on update, place that portion of code in ram and switch context to there. 

advantages: very flexible, harder to debug 

disadvantages: your system will not functional if some errors occurred while updating.
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Yes, I also think that I erase myself :-) 

 

But, I should be running in internal memory. So, that is something strange. 

 

Regards 

 

Karel
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Hi Karel, 

 

can you specify what the procedure that runs out of internal memory is doing exactly. Are you using the hal api to access flash or are you using your own implementation. 

You are writing that your program runs from external memory. Is this flash or ram?  

 

By the way, are you copying the procedure to internal memory by yourself or is alteras bootloader doing this job for you?  

 

--wolfgang
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Hi pfab, 

 

My NIOS code is running from external flash memory. 

 

What I'm doing is the follow. 

 

I first of all receive the whole bin file from RS232. I save this file into an 

free flash sector. This works fine. 

 

So, the following thing I have to do is copy this flash sector to the flash sector my nios core is running from. 

 

Therefore I have written a routine that is placed in internal memory by the section 

habdler: 

 

extern void UpdataSoftware(void)__attribute__((section(".OnChipMem"))); 

 

void UpdateSoftware(void) 

alt_irq_context count3; 

 

count3 = alt_irq_disable_all();  

 

for(FlashAd = 0x0000000; FlashAd < 0x0140000; FlashAd += 0x0020000)  

EraseFlashSector(FlashAd); 

 

/* HERE MY PROGRAM FAILS. I erase the flash sector 0 (sector from which the nios is running from). The instruction "EraseFlashSector" is also placed in internal memory*/ 

 

unsigned int FlAdW; 

unsigned int FlAdR; 

unsigned char Byte; 

 

for(FlAdW = 0x0000000,FlAdR = 0x0640000;FlAdW < 0x0140000; FlAdR++,FlAdW++) 

unsigned char * ReadFlashData = (unsigned char*)(FlAdR); 

Byte = *ReadFlashData; 

WriteFlash(Byte,FlAdW); 

}  

UART1_T(&#39;O&#39;); UART1_T(&#39;K&#39;); 

 

 

Regards 

 

Karel
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

I think that my function "eraseflashsector" is loaded from external flash 

memory and placed into internal memory.  

 

This instruction erases the flash sector the NIOS is running from. From the moment the next instruction has to be executed, for example a print instruction (also declared by the section handler in internal memory), the NIOS tries to copy this instruction into internal memory. That is impossible, because the sector in which the instruction is stored is erased by the "eraseflashsector" instrcution. 

 

Is my idea correct? Are the instructions loaded from external flash memory into internal memory from the moment they are called by the main program?  

 

If yes, is it possible to save these instrcutions "always" in internal memory 

 

Regards 

 

Karel
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Hi Karel, 

 

your function UpdateSoftware should be written to the onchip memory by the altera bootloader that gets linked into the .flash file for your project. 

This is quite important, because the .flash file is the one you should use to upgrade your software! You can convert it to a bin file using a tool like srec2bin. 

You should also check the objdump file for your project if everything is placed in the correct memory. 

 

During the update process avoid function calls and make sure to disable all interrupts if you are using any (e.g. system_timer). 

 

hope this helps, 

--wolfgang
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

p.s.: at the end of the update process, your function can not return! you must jump to the reset address.

0 Kudos
Altera_Forum
Honored Contributor II
610 Views

 

--- Quote Start ---  

originally posted by pfab@Mar 1 2006, 06:26 AM 

hi karel, 

 

your function updatesoftware should be written to the onchip memory by the altera bootloader that gets linked into the .flash file for your project. 

 

can you explain me how you can do that? do you have an example?  

 

this is quite important, because the .flash file is the one you should use to upgrade your software! you can convert it to a bin file using a tool like srec2bin. 

you should also check the objdump file for your project if everything is placed in the correct memory. 

 

during the update process avoid function calls and make sure to disable all interrupts if you are using any (e.g. system_timer). 

 

hope this helps, 

--wolfgang 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=13024) 

--- quote end ---  

 

--- Quote End ---  

0 Kudos
Altera_Forum
Honored Contributor II
610 Views

I was not right on the bootloader. That just gets linked in by the IDE if the .text section is not located in flash memory. 

You will find more information about this in the "Nios II Software Developer&#39;s Handbook" under "Memory Usage". 

The HAL API provides a function you must call in order to write a section to the onchip memory.
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Yes indeed, 

 

I exexute the load function: 

 

ALT_LOAD_SECTION_BY_NAME(OnChipMem); 

 

after a reset (at start up) 

 

The function "UpdateSoftware" must be placed internal. So, I still do&#39;nt know why 

my program stops after erasing the flash.
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Hi Karel, 

 

> So, I still do&#39;nt know why my program stops after erasing the flash. 

 

Where is your exception address? In flash? 

 

Regards, 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Hi Scott 

 

My exception stack is also placed in the onchipmemory and has a size of 0x200. 

 

Karel
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

> My exception stack is also placed in the onchipmemory and has a size of 0x200. 

 

I&#39;m referring to your exception _address_, the address where your core vectors 

when an exception occurs ... not the stack that&#39;s used during exception processing. 

 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

3 .exceptions 00000328 01000020 00009c00 00009cd4 2**2 

CONTENTS, ALLOC, LOAD, READONLY, CODE
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Info about the sections 

 

 

Sections: 

Idx Name Size VMA LMA File off Algn 

0 .entry 00000020 00000000 00000000 000000d4 2**5 

CONTENTS, ALLOC, LOAD, READONLY, CODE 

1 .text 0000961c 00000020 00000020 000000f4 2**2 

CONTENTS, ALLOC, LOAD, READONLY, CODE 

2 .rodata 000005c4 0000963c 0000963c 00009710 2**2 

CONTENTS, ALLOC, LOAD, READONLY, DATA 

3 .exceptions 00000328 01000020 00009c00 00009cd4 2**2 

CONTENTS, ALLOC, LOAD, READONLY, CODE 

4 .rwdata 00001cdc 01000348 00009f28 00009ffc 2**2 

CONTENTS, ALLOC, LOAD, DATA, SMALL_DATA 

5 .bss 0000216c 01002024 01002024 0000c01c 2**2 

ALLOC, SMALL_DATA 

6 .FLASHCtrl 00000000 0000bc04 0000bc04 0000c01c 2**0 

CONTENTS 

7 .OnChipMem 00000344 01004190 0000bc04 0000bcd8 2**2 

CONTENTS, ALLOC, LOAD, READONLY, CODE 

8 .comment 000007ba 00000000 00000000 0000c01c 2**0 

CONTENTS, READONLY 

9 .debug_aranges 00000618 00000000 00000000 0000c7d8 2**3 

CONTENTS, READONLY, DEBUGGING 

10 .debug_pubnames 00000df8 00000000 00000000 0000cdf0 2**0 

CONTENTS, READONLY, DEBUGGING 

11 .debug_info 0000ef78 00000000 00000000 0000dbe8 2**0 

CONTENTS, READONLY, DEBUGGING 

12 .debug_abbrev 00002e88 00000000 00000000 0001cb60 2**0 

CONTENTS, READONLY, DEBUGGING 

13 .debug_line 0000ba73 00000000 00000000 0001f9e8 2**0 

CONTENTS, READONLY, DEBUGGING 

14 .debug_frame 00000e74 00000000 00000000 0002b45c 2**2 

CONTENTS, READONLY, DEBUGGING 

15 .debug_str 00000d7c 00000000 00000000 0002c2d0 2**0 

CONTENTS, READONLY, DEBUGGING 

16 .debug_alt_sim_info 00000010 00000000 00000000 0002d04c 2**2 

CONTENTS, READONLY, DEBUGGING 

17 .debug_ranges 00000030 00000010 00000010 0002d05c 2**0 

CONTENTS, READONLY, DEBUGGING
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

> 3 .exceptions 00000328 01000020 00009c00 00009cd4 2**2 

> CONTENTS, ALLOC, LOAD, READONLY, CODE 

 

Ok, so I&#39;ll assume this is in flash memory. 

 

Next question: Do you have a system timer and do you disable interrupts when 

you are programming the flash? 

 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

I have a system timer 

 

Indeed, I disable interrupts when writting to the flash
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

Hi Karol, 

 

where do you declare the variable FlashAd? 

 

 

void UpdateSoftware(void) 

alt_irq_context count3; 

 

count3 = alt_irq_disable_all();  

 

for(FlashAd = 0x0000000; FlashAd < 0x0140000; FlashAd += 0x0020000)  

EraseFlashSector(FlashAd); 

...
0 Kudos
Altera_Forum
Honored Contributor II
610 Views

FlashAd is declared external and in on chip memory

0 Kudos
Altera_Forum
Honored Contributor II
558 Views

Hi Karel, 

 

Does the nios hang _after_ EraseFlashSector returns, or within EraseFlashSector? 

Could you post the the code for EraseFlashSector? 

 

--Scott
0 Kudos
Reply