Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
baer.ac
PostPosted: Aug 09, 2005 - 10:39 AM
Hangaround


Joined: Jan 17, 2004
Posts: 174
Location: Olpe / Germany

Hi,
I'm writing a bootloader (not interrupt driven) and need more program space.
I set the flag in the makefile not to generate Interrupts.. It does it, but don't removed the table.
Anyone an idea?


Andreas from Germany
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
jfiresto
PostPosted: Aug 09, 2005 - 11:27 AM
Hangaround


Joined: Oct 07, 2002
Posts: 423
Location: Germany

Moin Andreas,

Try the -nostartfiles flag, as in:
Code:
CFLAGS = -g -O1 -mmcu=$(TARGET) -nostartfiles ...

Please note that you will have to manually clear and initialize your static variables after you add it, meaning more chances to save flash!

-John


Last edited by jfiresto on Aug 09, 2005 - 11:31 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
baer.ac
PostPosted: Aug 09, 2005 - 12:25 PM
Hangaround


Joined: Jan 17, 2004
Posts: 174
Location: Olpe / Germany

Hmm... The Problem is, I've no stack now.... It's a bit difficult to programm without stack...
Andreas
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
jfiresto
PostPosted: Aug 09, 2005 - 03:14 PM
Hangaround


Joined: Oct 07, 2002
Posts: 423
Location: Germany

Someone please correct me if I am wrong. The stack pointer is just another register or register pair: just set it or them to the last RAM address, RAMEND, before you use the stack.


Last edited by jfiresto on Aug 09, 2005 - 06:00 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
lfmorrison
PostPosted: Aug 09, 2005 - 03:31 PM
Raving lunatic


Joined: Dec 08, 2004
Posts: 4650
Location: Nova Scotia, Canada

I also believe that you won't have access to the 'zero' register.

R1 is preset to have a value of zero as part of the startup routine. Gcc then assumes that R1 will always be zero from then on. If you skip the startup sections, then you might end up with a couple of surprises if you try to make use of the numeric constant 'zero' in your code. So, you might also have to include special provisions to ensure that R1 is zero before you do anything else in your main() function.
 
 View user's profile Send private message  
Reply with quote Back to top
jfiresto
PostPosted: Aug 09, 2005 - 05:43 PM
Hangaround


Joined: Oct 07, 2002
Posts: 423
Location: Germany

I forgot about the zero register -- thank you for pointing that out! You can clear R1 in your start up code as you prepare to zero all the static variables (the bss segment). The cautiously paranoid might also have main() periodically clear R1 to address the nagging worry: What if zero accidently becomes non-zero?
 
 View user's profile Send private message  
Reply with quote Back to top
boseji
PostPosted: Aug 10, 2005 - 05:13 AM
Hangaround


Joined: Sep 27, 2004
Posts: 264
Location: Bangalore ,Karnataka, INDIA Permanant-Howrah,West Bengal,INDIA

I have found a HACK to prevent the stack problem also just use:
Code:
COMMON_FLAGS  = -mmcu=$(CPU) -nostartfiles -minit-stack=0x4FF

Where 0x4FF is the end of the SRAM address to init the Stack pointer.
Also if you want to intialize some variables before main just do by:
Code:
void initvars(void) __attribute__ ((section(".init0")));
void initvars(void)
{
   //Your custome initialization before Main
}


But still the IVT remains. I am really not sure How to remove that.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
simonicemans
PostPosted: Aug 10, 2005 - 08:33 PM
Newbie


Joined: Aug 01, 2003
Posts: 3


Hi,
One way to do this is to use your own linker script.

Copy the appropriate script from

../avr/lib/ldscripts/...

into your source directory.

Look for the .text section for example

.text :
{

and just before the .text line, place the following :

/DISCARD/ : { *(.vectors); } /* Interrupt vectors */


There may be a 'cleaner' way to do this than to take a copy of the
system linker script and editting it, if so I'd be interested to hear Smile

-- Simon.
 
 View user's profile Send private message  
Reply with quote Back to top
simonicemans
PostPosted: Aug 10, 2005 - 08:39 PM
Newbie


Joined: Aug 01, 2003
Posts: 3


Oops I should have said that you need to tell the linker to actually use this
file when linking your application!!

Use the -T switch to gcc.

-- Simon.
 
 View user's profile Send private message  
Reply with quote Back to top
dl8dtl
PostPosted: Aug 10, 2005 - 09:12 PM
Raving lunatic


Joined: Dec 20, 2002
Posts: 7101
Location: Dresden, Germany

The idea with adding .vectors to DISCARD is OK, but keep in mind that
the very first entry of .vectors is actually the jump to the program
entry. You might want to replace that by your own jump. As you're
going to use a custom linker script anyway, it should be easy enough
by inventing your own initialization section, and use that in place of
the original .vectors.

.section .myvectors,"ax",@progbits
RJMP __init

__init is the name of the entry point of the section .init0
initialization code. Of course, if you don't need/want the standard
initialization either, feel free to replace whatever parts seem
appropriate from the original crt code.

_________________
Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.
Please read the `General information...' article before.
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
pfleury
PostPosted: Aug 10, 2005 - 09:29 PM
Rookie


Joined: Oct 01, 2001
Posts: 38


If you don't wont to edit the linker script add
Code:

-nostartfiles -nodefaultlibs to LDFLAGS

and the following code to your bootloader module
(assuming you are not using static variables)
Code:

void __jumpMain     (void) __attribute__ ((naked)) __attribute__ ((section (".init9")));

void __jumpMain(void)
{   
    asm volatile ( ".set __stack, %0" :: "i" (RAMEND) );
    asm volatile ( "clr __zero_reg__" );        // r1 set to 0
    asm volatile ( "rjmp main");                   // jump to main()
}
 
 View user's profile Send private message  
Reply with quote Back to top
boseji
PostPosted: Aug 11, 2005 - 04:05 AM
Hangaround


Joined: Sep 27, 2004
Posts: 264
Location: Bangalore ,Karnataka, INDIA Permanant-Howrah,West Bengal,INDIA

There was a little bit of glitch in this:
Code:
-nostartfiles -nodefaultlibs to LDFLAGS

I made it to:
Code:
-nostartfiles -nodefaultlibs

Worked Fine and now from 300 bytes my codesize is 212 bytes only.
Thanks for the help.
Also I just wanted to ask where we can get the info for the avr-gcc flags and the info regarding the Linker Scripts.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
dl8dtl
PostPosted: Aug 11, 2005 - 09:34 AM
Raving lunatic


Joined: Dec 20, 2002
Posts: 7101
Location: Dresden, Germany

> Also I just wanted to ask where we can get the info for the avr-gcc
> flags

In the GCC manual and/or info pages. Some important options are also
explained in the avr-libc documentation.

> and the info regarding the Linker Scripts.

In the linker manual (info pages -- for WinAVR, see the TkInfo
browser).

_________________
Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.
Please read the `General information...' article before.
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
alejmrm
PostPosted: Mar 13, 2006 - 01:29 AM
Hangaround


Joined: Jun 28, 2002
Posts: 164
Location: Boulder, Colorado,USA

I am also designing a bootloader... in this case an I2C bootloader.

So, I also want to discard the IVT generation... I am trying to follow these indications to create a new script file and discard the standard code with something custom... what I am doing is using the /DISCARD/ command to remove the (.vectors) section, but I am returning an error:

Quote:
C:\WinAVR\bin\..\lib\gcc\avr\3.4.5\..\..\..\..\avr\bin\ld.exe: `__vector_default' referenced in section `.text' of C:/WinAVR/bin/../lib/gcc/avr/3.4.5/../../../../avr/lib/avr5/crtm168.o: defined in discarded section `.vectors' of C:/WinAVR/bin/../lib/gcc/av
r/3.4.5/../../../../avr/lib/avr5/crtm168.o


The make files looks like this:

Code:
 ###############################################################################
# Makefile for the project i2cboot
###############################################################################

## General Flags
PROJECT = i2cboot
MCU = atmega168
TARGET = i2cboot.elf
CC = avr-gcc.exe

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2       -DF_CPU=8000000UL  -O0 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS += --verbose -Tavr5.x  -Wl,-Map=i2cboot.map


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0


## Objects that must be built in order to link
OBJECTS = main.o init.o

## Objects explicitly added by the user
LINKONLYOBJECTS =

## Build
all: $(TARGET) i2cboot.hex i2cboot.eep i2cboot.lss size

## Compile
main.o: ../main.c
   $(CC) $(INCLUDES) $(CFLAGS) -c  $<

init.o: ../init.c
   $(CC) $(INCLUDES) $(CFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
   avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
   avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@

%.lss: $(TARGET)
   avr-objdump -h -S $< > $@

size: ${TARGET}
   @echo
   @avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
   -rm -rf $(OBJECTS) i2cboot.elf dep/* i2cboot.hex i2cboot.eep i2cboot.lss i2cboot.map


## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)



What am I doing wrong?

_________________
---
ARod
 
 View user's profile Send private message  
Reply with quote Back to top
boseji
PostPosted: Mar 14, 2006 - 02:53 PM
Hangaround


Joined: Sep 27, 2004
Posts: 264
Location: Bangalore ,Karnataka, INDIA Permanant-Howrah,West Bengal,INDIA

Hi,

I am enclosing my own boot loader make file. This file was auto generated
by the KamAVR and later I modified it to my needs.
Code:

################################################################################
################################################################################
##                                                                            ##
## NOTE:  This makefile uses GNU make syntax.                                 ##
## =====  It may not work when called by other make utilities                 ##
##                                                                            ##
################################################################################
################################################################################

################################################################################
###              ###############################################################
### PROJECT NAME ###############################################################
###              ###############################################################
################################################################################

PROJECT = dk1-boot

################################################################################
##     #########################################################################
## CPU #########################################################################
##     #########################################################################
################################################################################

CPU = atmega8

################################################################################
###         ####################################################################
### SOURCES ####################################################################
###         ####################################################################
################################################################################

OBJ  = bootprog.o

################################################################################
##       #######################################################################
## TOOLS #######################################################################
##       #######################################################################
################################################################################

COMPILER  = avr-gcc
ASSEMBLER = avr-gcc
LINKER    = avr-gcc
ROMIZER   = avr-objcopy
DUMPER    = avr-objdump

################################################################################
##       #######################################################################
## FLAGS #######################################################################
##       #######################################################################
################################################################################

COMMON_FLAGS  = -mmcu=$(CPU) -nostartfiles -nodefaultlibs
COMMON_FLAGS += -Os
COMMON_FLAGS += -mno-interrupts
COMMON_FLAGS += -funsigned-char
COMMON_FLAGS += -funsigned-bitfields
COMMON_FLAGS += -Wall
COMMON_FLAGS += -Wstrict-prototypes

LINK_FLAGS  = $(COMMON_FLAGS) -ggdb

COMPILE_FLAGS  = $(LINK_FLAGS) -c -DF_CPU=4000000UL
COMPILE_FLAGS += -Wa,-acdhlmns=$(<:.c=.lst)

ASSEMBLE_FLAGS  = $(COMMON_FLAGS) -c -I. -x assembler-with-cpp
ASSEMBLE_FLAGS += -Wa,-gstabs,-acdhlmns=$(<:.s=.lst)

EEPROM_FLAGS  = -j .eeprom
EEPROM_FLAGS += --change-section-lma .eeprom=0

FLASH_FLAGS  = -j .text
FLASH_FLAGS += -j .data
FLASH_FLAGS += -j .Topseg

ELF_FLAGS  = -Wl,-Map=$(PROJECT).map
ELF_FLAGS += -Wl,-section-start=.text=0x1800
ELF_FLAGS += -Wl,-section-start=.Topseg=0x0
ELF_FLAGS += --cref
ELF_FLAGS += -lm

################################################################################
##         #####################################################################
## ACTIONS #####################################################################
##         #####################################################################
################################################################################

COMPILE      = $(COMPILER) $(COMPILE_FLAGS)
ASSEMBLE     = $(ASSEMBLER) $(ASSEMBLE_FLAGS)
LINK         = $(LINKER) $(LINK_FLAGS)
REMOVE       = rm -f
SIZE1        = avr-size $(PROJECT).elf
SIZE2        = sh avr-mem.sh $(PROJECT).elf $(CPU)

################################################################################
##       #######################################################################
## BUILD #######################################################################
##       #######################################################################
################################################################################

all: elf flash eep list
   $(SIZE1)
   $(SIZE2)

rebuild: clean all

clean:
   $(REMOVE) $(PROJECT).hex
   $(REMOVE) $(PROJECT)_eeprom.hex
   $(REMOVE) $(PROJECT).elf
   $(REMOVE) $(PROJECT).map
   $(REMOVE) $(PROJECT).cof
   $(REMOVE) $(PROJECT).lst
   $(REMOVE) $(OBJ:.o=.lst)
   $(REMOVE) $(OBJ)

eep:     $(PROJECT)_eeprom.hex
flash:   $(PROJECT).hex
elf:     $(PROJECT).elf
list:    $(PROJECT).lst

################################################################################
##      ########################################################################
## LINK ########################################################################
##      ########################################################################
################################################################################

$(PROJECT)_eeprom.hex: $(PROJECT).elf
   $(ROMIZER) $(EEPROM_FLAGS) -O ihex $(PROJECT).elf $(PROJECT)_eeprom.hex

$(PROJECT).hex: $(PROJECT).elf
   $(ROMIZER) $(FLASH_FLAGS) -O ihex $(PROJECT).elf $(PROJECT).hex

$(PROJECT).lst: $(PROJECT).elf
   $(DUMPER) -d -S $(PROJECT).elf > $(PROJECT).lst

$(PROJECT).elf: $(OBJ)
   $(LINK) $(OBJ) -o $(PROJECT).elf $(ELF_FLAGS)

################################################################################
##         #####################################################################
## COMPILE #####################################################################
##         #####################################################################
################################################################################

%.o : %.c
   $(COMPILE) $< -o $@

%.o : %.s
   $(ASSEMBLE) $< -o $@



Hope that it is helpful to you.

Regards,

Boseji
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
alejmrm
PostPosted: Mar 14, 2006 - 04:49 PM
Hangaround


Joined: Jun 28, 2002
Posts: 164
Location: Boulder, Colorado,USA

Hey thanks for sharing!!! Smile

Also I just want to point out that I am using avrStudio4.12 SP2 and the gcc plug-in and all my development is done on it. Also I found that if I hit twice the Rebuild button, the second time the error disappear...

Boseji,
I will look at your Makefile to play with it... what it is interesting is that you don't need to use a modified ldscript version. ( avr5.x)... isn't it? meaning that I won't need to mess up with link scripts.. just the Makefile... awesome!

_________________
---
ARod
 
 View user's profile Send private message  
Reply with quote Back to top
oloftangrot
PostPosted: Mar 15, 2006 - 10:20 PM
Wannabe


Joined: Jul 30, 2002
Posts: 57
Location: Umeå, Sweden

If I compile the code given by Peter Fleury the generated code locates statck initialisation to main() and not in __jumpMain(). So if I for example end __jumpMain with rjmp WinMain and rename main to WinMain there will be no stack initialisation.

Why isn't stack initialisation done in the C-startup code before main?

Olof
 
 View user's profile Send private message  
Reply with quote Back to top
dl8dtl
PostPosted: Mar 16, 2006 - 12:32 PM
Raving lunatic


Joined: Dec 20, 2002
Posts: 7101
Location: Dresden, Germany

> Why isn't stack initialisation done in the
> C-startup code before main?

It is, look closely.

_________________
Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.
Please read the `General information...' article before.
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
oloftangrot
PostPosted: Mar 16, 2006 - 07:07 PM
Wannabe


Joined: Jul 30, 2002
Posts: 57
Location: Umeå, Sweden

This is the result::

Code:

main.elf:     file format elf32-avr

Disassembly of section .text:

00000000 <__jumpMain>:

void __jumpMain(void)
{
        asm volatile ( ".set __stack, %0" :: "i" (0x55aa) );
        asm volatile ( "clr __zero_reg__" );            // r1 set to 0
   0:   11 24           eor     r1, r1
        asm volatile ( "rjmp main");                            // jump to main(
)
   2:   00 c0           rjmp    .+0             ; 0x4

00000004 <main>:
}

int main( void )
{
   4:   ca ea           ldi     r28, 0xAA       ; 170
   6:   d5 e5           ldi     r29, 0x55       ; 85
   8:   de bf           out     0x3e, r29       ; 62
   a:   cd bf           out     0x3d, r28       ; 61
        return 0;
}
   c:   80 e0           ldi     r24, 0x00       ; 0
   e:   90 e0           ldi     r25, 0x00       ; 0
  10:   00 c0           rjmp    .+0             ; 0x12

00000012 <exit>:

void exit( int a )
{
        for(;;); // Lock forever in a loop.
  12:   ff cf           rjmp    .-2             ; 0x12



To me it is pretty clear that main starts at 4h. And that the stack pointer is initialised after 4h.

I appeded the example files used to produce the code.

Best regards

Olof
 
 View user's profile Send private message  
Reply with quote Back to top
alejmrm
PostPosted: Mar 16, 2006 - 09:57 PM
Hangaround


Joined: Jun 28, 2002
Posts: 164
Location: Boulder, Colorado,USA

Olof,

I just download your zip file, and I found the __stack declaration, also I compile it everything went ok... see the first code line after void __jumpMain(void) declaration.

_________________
---
ARod
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits