Skip to content

Instantly share code, notes, and snippets.

@fcayci
Created May 3, 2017 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcayci/8dfea909d8142ce7618a867a499b6765 to your computer and use it in GitHub Desktop.
Save fcayci/8dfea909d8142ce7618a867a499b6765 to your computer and use it in GitHub Desktop.
#define int32_t int
#define int16_t short
#define int8_t char
#define uint32_t unsigned int
#define uint16_t unsigned short
#define uint8_t unsigned char
#define PERIPH_BASE ((uint32_t) 0x40000000)
#define GPIOD_BASE (PERIPH_BASE + 0x11400) // GPIOD base address is 0x40011400
#define RCC_BASE (PERIPH_BASE + 0x21000) // RCC base address is 0x40021000
#define STACKINIT 0x20008000
#define LEDDELAY 200000
#define GPIOD ((GPIO_type *) GPIOD_BASE)
#define RCC ((RCC_type *) RCC_BASE)
typedef struct {
uint32_t CRL;
uint32_t CRH;
uint32_t IDR;
uint32_t ODR;
uint32_t BSRR;
uint32_t BRR;
uint32_t LCKR;
} GPIO_type;
typedef struct {
uint32_t CR;
uint32_t CFGR;
uint32_t CIR;
uint32_t APB2RSTR;
uint32_t APB1RSTR;
uint32_t AHBENR;
uint32_t APB2ENR;
uint32_t APB1ENR;
uint32_t BDCR;
uint32_t CSR;
uint32_t AHBRSTR;
uint32_t CFGR2;
} RCC_type;
int main(void);
void delay(volatile uint32_t s);
// Vector Table
uint32_t (* const vector_table[])
__attribute__ ((section(".vectors"))) = {
((uint32_t *) STACKINIT),
((uint32_t *) main),
0, 0
};
double alpha;
uint32_t uint_array_init_glb[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};
int main(void)
{
uint32_t i;
alpha = 0;
// Make GPIOD output to display array values
RCC->APB2ENR |= (1 << 5);
GPIOD->CRH = 0x22222222;
GPIOD->CRL = 0x22222222;
while(1){
for (i = 0; i < 80; i++){
GPIOD->ODR = uint_array_init_glb[i];
delay(LEDDELAY);
}
}
return 0;
}
void delay(volatile uint32_t s) {
for(s; s>0; s--);
}
TARGET = array
SRCS = array.c
OBJS = $(addsuffix .o, $(basename $(SRCS)))
INCLUDES = -I.
LINKER_SCRIPT = stm32.ld
CFLAGS += -mcpu=cortex-m3 -mthumb
CFLAGS += -O0
CFLAGS += -g2
CFLAGS += -fno-common
CFLAGS += -Wall
#CFLAGS += -pedantic
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
CFLAGS += -Wl,--gc-sections # Linker garbage collector
LDFLAGS += -march=armv7-m
LDFLAGS += -nostartfiles
LDFLAGS += --specs=nosys.specs
LDFLAGS += -T$(LINKER_SCRIPT)
CROSS_COMPILE = arm-none-eabi-
CC = $(CROSS_COMPILE)gcc
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..."
build: $(TARGET).elf $(TARGET).hex $(TARGET).bin $(TARGET).lst
$(TARGET).elf: $(OBJS)
@$(CC) $(OBJS) $(LDFLAGS) -o $@
%.o: %.c
@echo "Building" $<
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
%.o: %.s
@echo "Building" $<
@$(CC) $(CFLAGS) -c $< -o $@
%.hex: %.elf
@$(OBJCOPY) -O ihex $< $@
%.bin: %.elf
@$(OBJCOPY) -O binary $< $@
%.lst: %.elf
@$(OBJDUMP) -x -S $(TARGET).elf > $@
size: $(TARGET).elf
@$(SIZE) $(TARGET).elf
burn:
@st-flash write $(TARGET).bin 0x08000000
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
@rm -f $(TARGET).bin
@rm -f $(TARGET).map
@rm -f $(TARGET).hex
@rm -f $(TARGET).lst
@rm -f $(TARGET).o
.PHONY: all build size clean burn
ENTRY(main)
MEMORY {
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K
rom (rx) : ORIGIN = 0x08000000, LENGTH = 256K
}
SECTIONS {
.text :
{
KEEP(*(.vectors)) /* Vector table */
*(.text*) /* Program code */
*(.rodata*) /* Read only data */
. = ALIGN(4);
_etext = .;
} > rom
.data :
{
_sdata = .;
*(.data*) /* Read-write initialized data */
. = ALIGN(4);
_edata = .;
} >ram AT > rom
.bss :
{
_sbss = .;
. = ALIGN(4);
*(.bss*) /* Read-write zero initialized data */
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >ram
}
__data_size = _edata - _sdata;
__text_size = _etext - ORIGIN(rom);
__bss_size = _ebss - _sbss;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment