Skip to content

Commit

Permalink
Added copy_data function to fix the global variable problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
fcayci committed May 4, 2017
1 parent 8144454 commit 9e5a138
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 29 deletions.
2 changes: 1 addition & 1 deletion adc/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ int32_t main(void)
ADC1->CR2 |= (1 << 3);
while((ADC1->CR2 & (1 << 3)));
ADC1->CR2 |= (1 << 2);
while((ADC1->CR2 & (1 << 2)));
while((ADC1->CR2 & (1 << 2)));

// Start conversion with software trigger
ADC1->CR2 |= (1<<22);
Expand Down
27 changes: 18 additions & 9 deletions mathtest/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ INCLUDES = -I.

LINKER_SCRIPT = stm32.ld

CFLAGS += -mcpu=cortex-m3 -mthumb
CFLAGS += -O0
CFLAGS += -fno-common -Wall
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections

LDFLAGS += -march=armv7-m # Make sure to pull thumb libraries
CFLAGS += -mcpu=cortex-m3 -mthumb # Processor setup
CFLAGS += -O0 # Optimization is off
CFLAGS += -g2 # Generate debug info
CFLAGS += -fno-common
CFLAGS += -Wall # Turn on warnings
#CFLAGS += -pedantic # more warnings
CFLAGS += -Wsign-compare
CFLAGS += -mfloat-abi=soft -fsingle-precision-constant
CFLAGS += -Wcast-align
CFLAGS += -Wconversion # neg int const implicitly converted to uint
CFLAGS += -fomit-frame-pointer # Do not use fp if not needed
CFLAGS += -ffunction-sections -fdata-sections

LDFLAGS += -march=armv7-m
LDFLAGS += -nostartfiles
LDFLAGS += -lm # link agains math
LDFLAGS += --specs=nosys.specs
LDFLAGS += -lm # link agains math
LDFLAGS += -Wl,--gc-sections # Linker garbage collector
LDFLAGS += -T$(LINKER_SCRIPT)

CROSS_COMPILE = arm-none-eabi-
Expand All @@ -31,7 +40,7 @@ all: clean $(SRCS) build size
build: $(TARGET).elf $(TARGET).hex $(TARGET).bin $(TARGET).lst

$(TARGET).elf: $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $@
@$(CC) $(OBJS) $(LDFLAGS) -o $@

%.o: %.c
@echo "Building" $<
Expand Down Expand Up @@ -71,4 +80,4 @@ clean:
@rm -f $(TARGET).lst
@rm -f $(TARGET).o

.PHONY: all build size clean burn
.PHONY: all build size clean burn debug
40 changes: 38 additions & 2 deletions mathtest/mathtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ typedef struct
} RCC_type;

// Function declarations. Add your functions here
void copy_data(void);
int32_t main(void);

/*************************************************
Expand All @@ -103,11 +104,46 @@ __attribute__ ((section(".vectors"))) = {
0, /* 0x00C HardFault */
};

/*************************************************
* Copy the data contents from LMA to VMA
*************************************************/
void copy_data(void)
{
extern char _etext, _sdata, _edata, _sbss, _ebss;
char *src = &_etext;
char *dst = &_sdata;

/* ROM has data at end of text; copy it. */
while (dst < &_edata)
*dst++ = *src++;

/* Zero bss. */
for (dst = &_sbss; dst< &_ebss; dst++)
*dst = 0;
}

double alpha;

uint32_t glb_uint_array[80] = {
1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,57,58,59,60,
61,62,63,64,65,66,67,68,69,70,
71,72,73,74,75,76,77,78,79,80};

/*************************************************
* Main code starts from here
*************************************************/
int32_t main(void)
{
// Copy LMA to VMA for data section
copy_data();

alpha = 0.5f;

uint32_t i, j;
// Enable GPIOD clock. Bit 5 in RCC APB2ENR register
RCC->APB2ENR |= (1 << 5);
Expand All @@ -119,9 +155,9 @@ int32_t main(void)

while(1)
{
for (i=1; i<0xFFFF; i++)
for (i=1; i<80; i++)
{
GPIOD->ODR = (uint16_t)sqrt((double)(i*i));
GPIOD->ODR = (uint16_t)sqrt((double)glb_uint_array[i] * alpha);
for (j=10000; j>0; j--); // Delay
}
}
Expand Down
24 changes: 18 additions & 6 deletions mathtest/stm32.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,37 @@ MEMORY {
}

SECTIONS {
.text :
.text ORIGIN(rom) :
{
KEEP(*(.vectors)) /* Vector table */
*(.text*) /* Program code */
*(.rodata*) /* Read only data */
. = ALIGN(4);
} >rom
_etext = .;
}

.data :
_sidata = .;

.data ORIGIN(ram) : AT ( ADDR (.text) + SIZEOF (.text) )
{
_sdata = .;
*(.data*) /* Read-write initialized data */
. = ALIGN(4);
} >ram AT >rom
_edata = .;
}


.bss :
.bss ADDR (.data) + SIZEOF (.data):
{
_sbss = .;
. = ALIGN(4);
*(.bss*) /* Read-write zero initialized data */
*(COMMON)
. = ALIGN(4);
} >ram
_ebss = .;
}
}

__text_size = SIZEOF (.text);
__data_size = SIZEOF (.data);
__bss_size = SIZEOF (.bss);
25 changes: 20 additions & 5 deletions pwm/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ INCLUDES = -I.
LINKER_SCRIPT = stm32.ld

CFLAGS += -mcpu=cortex-m3 -mthumb # Processor setup
CFLAGS += -O0 # Optimization is off
#CFLAGS += -g0 # Generate debug information
CFLAGS += -fno-common -Wall
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections
CFLAGS += -O0 # Optimization is off
CFLAGS += -g2 # Generate debug info
CFLAGS += -fno-common
CFLAGS += -Wall # Turn on warnings
#CFLAGS += -pedantic # more warnings
CFLAGS += -Wsign-compare
CFLAGS += -mfloat-abi=soft -fsingle-precision-constant
CFLAGS += -Wcast-align
#CFLAGS += -Wconversion # neg int const implicitly converted to uint
CFLAGS += -fomit-frame-pointer # Do not use fp if not needed
CFLAGS += -ffunction-sections -fdata-sections

LDFLAGS += -march=armv7-m
LDFLAGS += -nostartfiles
LDFLAGS += --specs=nosys.specs
LDFLAGS += -Wl,--gc-sections # Linker garbage collector
LDFLAGS += -T$(LINKER_SCRIPT)

CROSS_COMPILE = arm-none-eabi-
Expand All @@ -23,6 +31,7 @@ LD = $(CROSS_COMPILE)ld
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size
DBG = $(CROSS_COMPILE)gdb

all: clean $(SRCS) build size
@echo "Successfully finished..."
Expand Down Expand Up @@ -55,6 +64,12 @@ size: $(TARGET).elf
burn:
@st-flash write $(TARGET).bin 0x8000000

debug:
@$(DBG) -tui --eval-command="target extended-remote :4242" \
--eval-command="layout asm" \
--eval-command="layout regs" \
$(TARGET).elf

clean:
@echo "Cleaning..."
@rm -f $(TARGET).elf
Expand All @@ -64,4 +79,4 @@ clean:
@rm -f $(TARGET).lst
@rm -f $(TARGET).o

.PHONY: all build size clean burn
.PHONY: all build size clean burn debug
22 changes: 22 additions & 0 deletions pwm/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ typedef enum IRQn


// Function declarations. Add your functions here
void copy_data(void);
void enable_interrupt(IRQn_type IRQn);
void disable_interrupt(IRQn_type IRQn);
void set_system_clock_to_25Mhz(void);
Expand Down Expand Up @@ -351,6 +352,24 @@ __attribute__ ((section(".vectors"))) = {
0, /* 74 USB OTG FS */
};

/*************************************************
* Copy the data contents from LMA to VMA
*************************************************/
void copy_data(void)
{
extern char _etext, _sdata, _edata, _sbss, _ebss;
char *src = &_etext;
char *dst = &_sdata;

/* ROM has data at end of text; copy it. */
while (dst < &_edata)
*dst++ = *src++;

/* Zero bss. */
for (dst = &_sbss; dst< &_ebss; dst++)
*dst = 0;
}

volatile uint32_t duty;
volatile uint32_t period;

Expand Down Expand Up @@ -470,6 +489,9 @@ void tim1_handler(void)
*************************************************/
int32_t main(void)
{
// Copy LMA to VMA for data section
copy_data();

duty = 1500;
period = 3000;
// Set clock to 72 MHz
Expand Down
24 changes: 18 additions & 6 deletions pwm/stm32.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,37 @@ MEMORY {
}

SECTIONS {
.text :
.text ORIGIN(rom) :
{
KEEP(*(.vectors)) /* Vector table */
*(.text*) /* Program code */
*(.rodata*) /* Read only data */
. = ALIGN(4);
} >rom
_etext = .;
}

.data :
_sidata = .;

.data ORIGIN(ram) : AT ( ADDR (.text) + SIZEOF (.text) )
{
_sdata = .;
*(.data*) /* Read-write initialized data */
. = ALIGN(4);
} >ram AT >rom
_edata = .;
}


.bss :
.bss ADDR (.data) + SIZEOF (.data):
{
_sbss = .;
. = ALIGN(4);
*(.bss*) /* Read-write zero initialized data */
*(COMMON)
. = ALIGN(4);
} >ram
_ebss = .;
}
}

__text_size = SIZEOF (.text);
__data_size = SIZEOF (.data);
__bss_size = SIZEOF (.bss);

0 comments on commit 9e5a138

Please sign in to comment.