понедельник, 11 октября 2021 г.

Continue on your own risk (CMSIS v5 STM32F103 make example)

Of course, ignoring official vendor libraries is like shooting under your legs with bazooka. But let's think that it was a damage boost (ref.  the damage boost is a tactic in speed run game walkthrough, it helps to move as fast as never in normal game).

Firstly I download last libraries from arm. As said oficial website last CMSIS has a lot of additions and fixes. I could try it all later. Now I am interest in simple build for my gamepad project. System code is very specific and complicated, but it have template files. And all what I need to modify is described in todo comments. All that I need firstly is to adapt linker file and startup file. Fortunately assembler startup file is deprecated and I deal with simpler C file. So to adapt this library I need to add interrupt vectors and fill memory addresses variables. I need to fill the template with microcontroller related interrupts from stm32 reference guide in files CMSIS_5/Device/ARM/ARMCM3/Include/ARMCM3.h and CMSIS_5/Device/ARM/ARMCM3/Source/startup_ARMCM3.c. You can see results here and here. Vectors:

void WWDG_Handler (void)__attribute__((weak,alias("Default_Handler"))); void PVD_Handler (void)__attribute__((weak,alias("Default_Handler"))); void TAMPER_Handler (void)__attribute__((weak,alias("Default_Handler"))); void RTC_Handler (void)__attribute__((weak,alias("Default_Handler"))); ... WWDG_Handler, // Window watchdog interrrupt PVD_Handler, // PVD through EXTI Line detection interrupt TAMPER_Handler, // Tamper interrupt RTC_Handler, // RTC global interrupt ... WWDG_IRQn = 0 PVD_IRQn = 1 TAMPER_IRQn = 2 RTC_IRQn = 3 ... void WWDG_Handler (void); void PVD_Handler (void); void TAMPER_Handler (void); void RTC_Handler (void); ...

Also I need to write in flash and sram sizes for chosen MCU. These variables are taken out to file header. Path to template: CMSIS/Device/ARM/ARMCM3/Source/GCC/gcc_arm.ld,.Addresses:

/*---------------------- Flash Configuration ---------------------------------- Flash Configuration Flash Base Address <0x0-0xffffffff:8> Flash Size (in Bytes) <0x0-0xffffffff:8> -----------------------------------------------------------------------------*/ __ROM_BASE = 0x08000000; __ROM_SIZE = 0x00010000; /*--------------------- Embedded RAM Configuration ---------------------------- RAM Configuration RAM Base Address <0x0-0xffffffff:8> RAM Size (in Bytes) <0x0-0xffffffff:8> -----------------------------------------------------------------------------*/ __RAM_BASE = 0x20000000; __RAM_SIZE = 0x00005000;

After filling templates let's start to build code. First of all I was going to find makefile template. But this lib created to work in keil or eclipse IDEs, and haven't got any make templates. Also their website not the first time talks about development firmware in web services and futher uploading it to wireless device through the air. Sounds as revolutionary as expensive, it's better to leave it for the future. Analysis of the whole library says that I actually do not need almost whole library in that such simple project. Also I think that their templates for device drivers will make code more complex. Generally I need to connect files like on the picture:

Here is makefile:

#paths TARGET = result/button.elf LIB_DIR = lib REG_DIR = $(LIB_DIR)/regs ODIR = bin # code is here SOURCES = main.c SOURCES += $(LIB_DIR)/gamepad_port.c SOURCES += $(LIB_DIR)/gamepad.c SOURCES += $(LIB_DIR)/usb_core.c SOURCES += $(LIB_DIR)/usb_hid.c SOURCES += $(LIB_DIR)/usb_st_req.c SOURCES += $(LIB_DIR)/rcc.c SOURCES += $(LIB_DIR)/delay.c SOURCES += $(REG_DIR)/usb_device_regs.c INCLUDES = -I $(LIB_DIR)/ INCLUDES += -I $(REG_DIR)/ # VENDOR SPECIFIC CODE CMSIS_INC = $(LIB_DIR)/CMSIS_5/CMSIS/Core/Include VENDOR_DIR = $(LIB_DIR)/STM32F103_CMSIS SOURCES += $(VENDOR_DIR)/startup_stm32f103.c SOURCES += $(VENDOR_DIR)/system_stm32f103.c INCLUDES += -I $(VENDOR_DIR) INCLUDES += -I $(CMSIS_INC) LD_SCRIPT = $(VENDOR_DIR)/gcc_arm.ld DEFINES = ARMCM3 # Parameters CFLAGS +=-ffunction-sections -fdata-sections CFLAGS +=-Wall -Wextra -Werror -Wconversion -Wundef -Wformat=2 -Wformat-truncation CFLAGS +=-Wdouble-promotion -Wshadow -Wimplicit-function-declaration CFLAGS +=-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes CFLAGS +=-fno-common -Os -ffreestanding CFLAGS +=-mcpu=cortex-m3 -mthumb CFLAGS +=-mfloat-abi=soft -std=gnu2x CFLAGS +=-D$(DEFINES) #CFLAGS +=-ggdb3 LFLAGS = -T"$(LD_SCRIPT)" --specs=nano.specs -Wl,--gc-sections LFLAGS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group #utilities CC = arm-none-eabi-gcc LD = arm-none-eabi-gcc ### making-taking aaaa ######################################################### #object files list OBJECTS = $(SOURCES:%.c=$(ODIR)/%.o) #main rule all: $(TARGET) #compile $(ODIR)/%.o: %.c @echo "[CC] $@" @mkdir -p $(dir $@) @$(CC) $(CFLAGS) $(INCLUDES) $(DEFS) -c $< -o $@ #link $(TARGET): $(OBJECTS) @echo "[LD] $@" @$(LD) $(CFLAGS) $(LFLAGS) $(OBJECTS) -o $@ # Clean rule #.PHONY: clean: @rm -rf $(ODIR) @rm -r $(TARGET)

Except sophisticated configuration of compiler (you can see it in CFLAGS and LFLAGS variables), this makefile can be called typical. Notice, that CMSIS code is linked below "VENDOR SPECISIC CODE" comment.

In conclusion I could say, that command line building of arm cm3 MCUs code is quiet simple. Using this example you can make your own example project with standard STM32 libraries, but I in this variant built code for STM32 USB HID gamepad using my own register definitions. In the end I do not need any other libraries except libc and CMSIS.

Комментариев нет :

Отправить комментарий