Quantcast
Channel: Objective Development Forums
Viewing all articles
Browse latest Browse all 4524

Re: ATMEGA328P on V-USB

$
0
0
Here you go.

P.S. I changed the boot address to $3800

main.elf:     file format elf32-avr

SYMBOL TABLE:
00003800 l    d  .text   00000000 .text
00800100 l    d  .bss   00000000 .bss
00820000 l    d  .fuse   00000000 .fuse
00830000 l    d  .lock   00000000 .lock
00840000 l    d  .signature   00000000 .signature
00000000 l    d  .debug_aranges   00000000 .debug_aranges
00000000 l    d  .debug_pubnames   00000000 .debug_pubnames
00000000 l    d  .debug_info   00000000 .debug_info
00000000 l    d  .debug_abbrev   00000000 .debug_abbrev
00000000 l    d  .debug_line   00000000 .debug_line
00000000 l    d  .debug_frame   00000000 .debug_frame
00000000 l    d  .debug_str   00000000 .debug_str
0000387e l       .text   00000000 .do_clear_bss_start
0000387c l       .text   00000000 .do_clear_bss_loop
00000000 l    df *ABS*   00000000 main.c
0000003f l       *ABS*   00000000 __SREG__
0000003e l       *ABS*   00000000 __SP_H__
0000003d l       *ABS*   00000000 __SP_L__
00000034 l       *ABS*   00000000 __CCP__
00000000 l       *ABS*   00000000 __tmp_reg__
00000001 l       *ABS*   00000000 __zero_reg__
0000395e l       .text   00000000 __stop_program
0000388c  w      .text   00000000 __vector_22
0000392c g     F .text   00000018 __vector_1
00003868 g       .text   00000000 __trampolines_start
00003960 g       .text   00000000 _etext
0000388c  w      .text   00000000 __vector_24
0000388c  w      .text   00000000 __vector_12
0000388c g       .text   00000000 __bad_interrupt
00003960 g       *ABS*   00000000 __data_load_end
0000388c  w      .text   00000000 __vector_6
00003868 g       .text   00000000 __trampolines_end
0000388c  w      .text   00000000 __vector_3
0000388c  w      .text   00000000 __vector_23
00003960 g       *ABS*   00000000 __data_load_start
00003868 g       .text   00000000 __dtors_end
00800101 g       .bss   00000000 __bss_end
0000388c  w      .text   00000000 __vector_25
00840000 g     O .signature   00000003 __signature
00800100 g     O .bss   00000001 polarity
0000388c  w      .text   00000000 __vector_11
00003868  w      .text   00000000 __init
0000388c  w      .text   00000000 __vector_13
0000388c  w      .text   00000000 __vector_17
0000388c  w      .text   00000000 __vector_19
000038f2 g     F .text   0000003a __vector_7
00003874 g       .text   00000000 __do_clear_bss
00810000 g       .bss   00000000 __eeprom_end
00003800 g       .text   00000000 __vectors
00003800  w      .text   00000000 __vector_default
0000388c  w      .text   00000000 __vector_5
00003868 g       .text   00000000 __ctors_start
00800100 g       .bss   00000000 __bss_start
00003890 g     F .text   00000062 main
0000388c  w      .text   00000000 __vector_4
00830000 g     O .lock   00000001 __lock
00000000  w      *ABS*   00000000 __heap_end
0000388c  w      .text   00000000 __vector_9
00003944 g     F .text   00000018 __vector_2
0000388c  w      .text   00000000 __vector_21
0000388c  w      .text   00000000 __vector_15
00003868 g       .text   00000000 __dtors_start
00003868 g       .text   00000000 __ctors_end
000008ff  w      *ABS*   00000000 __stack
00800100 g       .text   00000000 _edata
00800101 g       .bss   00000000 _end
0000388c  w      .text   00000000 __vector_8
00820000 g     O .fuse   00000003 __fuse
0000395c  w      .text   00000000 exit
0000395c g       .text   00000000 _exit
0000388c  w      .text   00000000 __vector_14
0000388c  w      .text   00000000 __vector_10
0000388c  w      .text   00000000 __vector_16
0000388c  w      .text   00000000 __vector_18
0000388c  w      .text   00000000 __vector_20



Disassembly of section .text:

00003800 <__vectors>:
    3800:   0c 94 34 1c    jmp   0x3868   ; 0x3868 <__ctors_end>
    3804:   0c 94 96 1c    jmp   0x392c   ; 0x392c <__vector_1>
    3808:   0c 94 a2 1c    jmp   0x3944   ; 0x3944 <__vector_2>
    380c:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3810:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3814:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3818:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    381c:   0c 94 79 1c    jmp   0x38f2   ; 0x38f2 <__vector_7>
    3820:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3824:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3828:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    382c:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3830:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3834:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3838:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    383c:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3840:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3844:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3848:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    384c:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3850:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3854:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3858:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    385c:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3860:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>
    3864:   0c 94 46 1c    jmp   0x388c   ; 0x388c <__bad_interrupt>

00003868 <__ctors_end>:
    3868:   11 24          eor   r1, r1
    386a:   1f be          out   0x3f, r1   ; 63
    386c:   cf ef          ldi   r28, 0xFF   ; 255
    386e:   d8 e0          ldi   r29, 0x08   ; 8
    3870:   de bf          out   0x3e, r29   ; 62
    3872:   cd bf          out   0x3d, r28   ; 61

00003874 <__do_clear_bss>:
    3874:   11 e0          ldi   r17, 0x01   ; 1
    3876:   a0 e0          ldi   r26, 0x00   ; 0
    3878:   b1 e0          ldi   r27, 0x01   ; 1
    387a:   01 c0          rjmp   .+2         ; 0x387e <.do_clear_bss_start>

0000387c <.do_clear_bss_loop>:
    387c:   1d 92          st   X+, r1

0000387e <.do_clear_bss_start>:
    387e:   a1 30          cpi   r26, 0x01   ; 1
    3880:   b1 07          cpc   r27, r17
    3882:   e1 f7          brne   .-8         ; 0x387c <.do_clear_bss_loop>
    3884:   0e 94 48 1c    call   0x3890   ; 0x3890 <main>
    3888:   0c 94 ae 1c    jmp   0x395c   ; 0x395c <_exit>

0000388c <__bad_interrupt>:
    388c:   0c 94 00 1c    jmp   0x3800   ; 0x3800 <__vectors>

00003890 <main>:
    Parameters :    none
    Return values :   none
**************************************************************************************************************/
__attribute__ ((OS_main)) int main(void)
{
   MCUCR = (1 << IVCE);   // enable change of interrupt vectors
    3890:   81 e0          ldi   r24, 0x01   ; 1
    3892:   85 bf          out   0x35, r24   ; 53
   MCUCR = (1 << IVSEL);   // move interrupts to bootloader flash section
    3894:   82 e0          ldi   r24, 0x02   ; 2
    3896:   85 bf          out   0x35, r24   ; 53

   //Input
   DDRD &= ~((1<<PD2)|(1<<PD3));   // PD2=INT0=Button0/PD3=INT1=Button1
    3898:   8a b1          in   r24, 0x0a   ; 10
    389a:   83 7f          andi   r24, 0xF3   ; 243
    389c:   8a b9          out   0x0a, r24   ; 10
   PORTD |= ((1<<PD2)|(1<<PD3));   // Pull-UP
    389e:   8b b1          in   r24, 0x0b   ; 11
    38a0:   8c 60          ori   r24, 0x0C   ; 12
    38a2:   8b b9          out   0x0b, r24   ; 11

   //Output
   DDRD |= ((1<<PD4)|(1<<PD5)|(1<<PD6)|(1<<PD7));      // PD4=RedLED/PD5=GreenLED/PD6=LED0/PD7=LED1
    38a4:   8a b1          in   r24, 0x0a   ; 10
    38a6:   80 6f          ori   r24, 0xF0   ; 240
    38a8:   8a b9          out   0x0a, r24   ; 10
   PORTD &= ~((1<<PD4)|(1<<PD5)|(1<<PD6)|(1<<PD7));   // Off
    38aa:   8b b1          in   r24, 0x0b   ; 11
    38ac:   8f 70          andi   r24, 0x0F   ; 15
    38ae:   8b b9          out   0x0b, r24   ; 11

   // Set timer 2 for each second
   TCCR2A |= (1<<WGM21); // CTC
    38b0:   80 91 b0 00    lds   r24, 0x00B0
    38b4:   82 60          ori   r24, 0x02   ; 2
    38b6:   80 93 b0 00    sts   0x00B0, r24
   TCCR2B |= ((1<<CS20)|(1<<CS21)|(1<<CS22)); // XTAL/1024
    38ba:   80 91 b1 00    lds   r24, 0x00B1
    38be:   87 60          ori   r24, 0x07   ; 7
    38c0:   80 93 b1 00    sts   0x00B1, r24

   OCR2A=(unsigned char)((F_XTAL)/1024); // 1Hz
    38c4:   8f e1          ldi   r24, 0x1F   ; 31
    38c6:   80 93 b3 00    sts   0x00B3, r24

   TIMSK2 |= (1<<OCIE2A);   // Ativate interrupt on timer 2A output compare
    38ca:   80 91 70 00    lds   r24, 0x0070
    38ce:   82 60          ori   r24, 0x02   ; 2
    38d0:   80 93 70 00    sts   0x0070, r24

   TCNT2=0; // Restart timer 2
    38d4:   10 92 b2 00    sts   0x00B2, r1

   // Global variable init
   polarity=0;
    38d8:   10 92 00 01    sts   0x0100, r1

   // Enable interrupt on buttons
   EIMSK |= ((1<<INT0)|(1<<INT1));
    38dc:   8d b3          in   r24, 0x1d   ; 29
    38de:   83 60          ori   r24, 0x03   ; 3
    38e0:   8d bb          out   0x1d, r24   ; 29
   EICRA |= ((1<<ISC01)|(1<<ISC11));   // Interrupt on falling edge
    38e2:   80 91 69 00    lds   r24, 0x0069
    38e6:   8a 60          ori   r24, 0x0A   ; 10
    38e8:   80 93 69 00    sts   0x0069, r24

   sei();   // Enable interrupts
    38ec:   78 94          sei

   PORTD |= (1<<PD4);   // RedLED on
    38ee:   5c 9a          sbi   0x0b, 4   ; 11
    38f0:   ff cf          rjmp   .-2         ; 0x38f0 <main+0x60>

000038f2 <__vector_7>:
/*************************************************************************************************************
    Interruption:   TIMER2_COMPA_vect
    Description:   Happen each time TCNT2A = OCR2A (1 second)
**************************************************************************************************************/
ISR(TIMER2_COMPA_vect)
{
    38f2:   1f 92          push   r1
    38f4:   0f 92          push   r0
    38f6:   0f b6          in   r0, 0x3f   ; 63
    38f8:   0f 92          push   r0
    38fa:   11 24          eor   r1, r1
    38fc:   8f 93          push   r24
    38fe:   9f 93          push   r25
   // GreenLED flash
   polarity=polarity?0:1;
    3900:   80 91 00 01    lds   r24, 0x0100
    3904:   90 e0          ldi   r25, 0x00   ; 0
    3906:   88 23          and   r24, r24
    3908:   09 f4          brne   .+2         ; 0x390c <__vector_7+0x1a>
    390a:   91 e0          ldi   r25, 0x01   ; 1
    390c:   90 93 00 01    sts   0x0100, r25

   if(polarity)
    3910:   80 91 00 01    lds   r24, 0x0100
    3914:   88 23          and   r24, r24
    3916:   11 f0          breq   .+4         ; 0x391c <__vector_7+0x2a>
      PORTD |= (1<<PD5);
    3918:   5d 9a          sbi   0x0b, 5   ; 11
    391a:   01 c0          rjmp   .+2         ; 0x391e <__vector_7+0x2c>
   else
      PORTD &= ~(1<<PD5);
    391c:   5d 98          cbi   0x0b, 5   ; 11
}
    391e:   9f 91          pop   r25
    3920:   8f 91          pop   r24
    3922:   0f 90          pop   r0
    3924:   0f be          out   0x3f, r0   ; 63
    3926:   0f 90          pop   r0
    3928:   1f 90          pop   r1
    392a:   18 95          reti

0000392c <__vector_1>:
/*************************************************************************************************************
    Interruption:   INT0_vect
    Description:   Happen each time But0 is pushed
**************************************************************************************************************/
ISR(INT0_vect)
{
    392c:   1f 92          push   r1
    392e:   0f 92          push   r0
    3930:   0f b6          in   r0, 0x3f   ; 63
    3932:   0f 92          push   r0
    3934:   11 24          eor   r1, r1
   PORTD |= ((1<<PD6));   // Led0 on
    3936:   5e 9a          sbi   0x0b, 6   ; 11
   PORTD &= ~((1<<PD7));   // Led1 off
    3938:   5f 98          cbi   0x0b, 7   ; 11
}
    393a:   0f 90          pop   r0
    393c:   0f be          out   0x3f, r0   ; 63
    393e:   0f 90          pop   r0
    3940:   1f 90          pop   r1
    3942:   18 95          reti

00003944 <__vector_2>:
/*************************************************************************************************************
    Interruption:   INT1_vect
    Description:   Happen each time But1 is pushed
**************************************************************************************************************/
ISR(INT1_vect)
{
    3944:   1f 92          push   r1
    3946:   0f 92          push   r0
    3948:   0f b6          in   r0, 0x3f   ; 63
    394a:   0f 92          push   r0
    394c:   11 24          eor   r1, r1
   PORTD &= ~((1<<PD6));   // Led0 off
    394e:   5e 98          cbi   0x0b, 6   ; 11
   PORTD |= ((1<<PD7));   // Led1 on
    3950:   5f 9a          sbi   0x0b, 7   ; 11
}
    3952:   0f 90          pop   r0
    3954:   0f be          out   0x3f, r0   ; 63
    3956:   0f 90          pop   r0
    3958:   1f 90          pop   r1
    395a:   18 95          reti

0000395c <_exit>:
    395c:   f8 94          cli

0000395e <__stop_program>:
    395e:   ff cf          rjmp   .-2         ; 0x395e <__stop_program>


main.c
/*************************************************************************************************************
*    Title         : Test bootloader atmega328p
*    File         : main.c
*    SW Version   :
*    HW Version   :
*    MCU          : ATMEGA328P
*    Start date   : 9/9/2013
*    Update       :
**************************************************************************************************************/

#include <avr/io.h>
#include <avr/interrupt.h>

#include "fuses.h"

#define F_XTAL   32000

volatile unsigned char polarity;

/*************************************************************************************************************
    Function :      main 
    Description :   Main function
    Parameters :    none
    Return values :   none
**************************************************************************************************************/
__attribute__ ((OS_main)) int main(void)
{
   MCUCR = (1 << IVCE);   // enable change of interrupt vectors
   MCUCR = (1 << IVSEL);   // move interrupts to bootloader flash section

   //Input
   DDRD &= ~((1<<PD2)|(1<<PD3));   // PD2=INT0=Button0/PD3=INT1=Button1
   PORTD |= ((1<<PD2)|(1<<PD3));   // Pull-UP

   //Output
   DDRD |= ((1<<PD4)|(1<<PD5)|(1<<PD6)|(1<<PD7));      // PD4=RedLED/PD5=GreenLED/PD6=LED0/PD7=LED1
   PORTD &= ~((1<<PD4)|(1<<PD5)|(1<<PD6)|(1<<PD7));   // Off

   // Set timer 2 for each second
   TCCR2A |= (1<<WGM21); // CTC
   TCCR2B |= ((1<<CS20)|(1<<CS21)|(1<<CS22)); // XTAL/1024

   ASSR |= (1<<AS2); // Async

   OCR2A=(unsigned char)((F_XTAL)/1024); // 1Hz

   TIMSK2 |= (1<<OCIE2A);   // Ativate interrupt on timer 2A output compare

   TCNT2=0; // Restart timer 2

   // Global variable init
   polarity=0;

   // Enable interrupt on buttons
   EIMSK |= ((1<<INT0)|(1<<INT1));
   EICRA |= ((1<<ISC01)|(1<<ISC11));   // Interrupt on falling edge

   sei();   // Enable interrupts

   PORTD |= (1<<PD4);   // RedLED on

   for(;;)   // Infinite loop
   {
   }
}

/*************************************************************************************************************
    Interruption:   TIMER2_COMPA_vect
    Description:   Happen each time TCNT2A = OCR2A (1 second)
**************************************************************************************************************/
ISR(TIMER2_COMPA_vect)
{
   // GreenLED flash
   polarity=polarity?0:1;

   if(polarity)
      PORTD |= (1<<PD5);
   else
      PORTD &= ~(1<<PD5);
}

/*************************************************************************************************************
    Interruption:   INT0_vect
    Description:   Happen each time But0 is pushed
**************************************************************************************************************/
ISR(INT0_vect)
{
   PORTD |= ((1<<PD6));   // Led0 on
   PORTD &= ~((1<<PD7));   // Led1 off
}

/*************************************************************************************************************
    Interruption:   INT1_vect
    Description:   Happen each time But1 is pushed
**************************************************************************************************************/
ISR(INT1_vect)
{
   PORTD &= ~((1<<PD6));   // Led0 off
   PORTD |= ((1<<PD7));   // Led1 on
}


fuses.h
/*************************************************************************************************************
*    Title :         Header file for fuses
*    File :          fuses.h
*************************************************************************************************************/

/*************************************************************************************************************
*   DEVICE = ATMEGA328P
*   FUSES = LOW = 0xC2, HIGH = 0xD8, EXTENDED = 0xFD
*   -----
*   BODLEVEL = 2.7V
*   RSTDISBL = NO
*   DWEN = NO
*   SPIEN = YES
*   WDTON = NO
*   EESAVE = YES
*   BOOTSZ = Boot Flash size=2048 words, Boot address 0x3800
*   BOOTRST = YES
*   CKDIV8 = NO
*   CKOUT = NO
*   SUT_CKSEL = Int.Crystal/Osc 8.0Mhz; 6CK/14CK + 0ms
*************************************************************************************************************/

/*************************************************************************************************************
*   LOCKBITS = 0xFC
*   --------
*   LB = FURTHER PROGRAMMING & VERIFICATION DISABLED
**************************************************************************************************************/

#include <avr/io.h>
#include <avr/signature.h>

#ifndef FUSES_H
#define FUSES_H

__fuse_t __fuse __attribute__((section (".fuse"))) =
{
    .low = 0xC2,
    .high = 0xD8,
   .extended = 0xFD,
};

unsigned char __lock __attribute__((section (".lock"))) = 0xFC;

#endif


Makefile
###############################################################################
# Makefile for the project main
###############################################################################

## General Flags
PROJECT = main
MCU = atmega328p
TARGET = main.elf
CC = avr-gcc

CPP = avr-g++

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

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

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

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,--section-start=.text=0x3800  -Wl,-Map=main.map


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

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


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

## Objects explicitly added by the user
LINKONLYOBJECTS =

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

## Compile
main.o: main.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 $< $@ || exit 0

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

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

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


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


Viewing all articles
Browse latest Browse all 4524

Trending Articles