~berfr/stm32f4discovery

502314b1cfe40f0cc95285b16afa9eac41119768 — berfr 1 year, 26 days ago 1a77b5d
Add source, cmake config as well as readme file
4 files changed, 113 insertions(+), 0 deletions(-)

A .gitignore
A CMakeLists.txt
A README.md
A src/main.c
A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
bin/

A CMakeLists.txt => CMakeLists.txt +49 -0
@@ 0,0 1,49 @@
cmake_minimum_required(VERSION 3.14)
set(CMAKE_C_COMPILER "arm-none-eabi-gcc")
enable_language(ASM)

file(GLOB_RECURSE LIB_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} lib/*.c lib/*.s)

file(GLOB_RECURSE LIB_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} lib/*.h)
list(TRANSFORM LIB_HEADERS REPLACE "/[^/]+\.h$" "" OUTPUT_VARIABLE LIB_HEADERS_DIRS)
list(REMOVE_DUPLICATES LIB_HEADERS_DIRS)

file(GLOB SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/*.c)

include_directories(inc ${LIB_HEADERS_DIRS})

set(LINKER_SCRIPT_INPUT lib/stm32f4xx/Project/STM32F4xx_StdPeriph_Templates/TrueSTUDIO/STM32F40_41xxx/STM32F417IG_FLASH.ld)
set(LINKER_SCRIPT_OUTPUT stm32_flash.ld)
configure_file(${LINKER_SCRIPT_INPUT} ${LINKER_SCRIPT_OUTPUT} COPYONLY)

add_definitions(
    -DSTM32F40_41xxx
    -DHSE_VALUE=\(\(uint32_t\)8000000\)
    -DHSI_VALUE=\(\(uint32_t\)16000000\)
    -DUSE_STDPERIPH_DRIVER
)

set(
    CPU_FLAGS
    -mcpu=cortex-m4 -mlittle-endian -mthumb -mthumb-interwork
    -mfloat-abi=hard -mfpu=fpv4-sp-d16
    -g -O0
)

add_compile_options(
    ${CPU_FLAGS}
    -fdata-sections -ffunction-sections
)

add_link_options(
    ${CPU_FLAGS}
    --specs=nosys.specs
    -T${LINKER_SCRIPT_OUTPUT}
    -Wl,--gc-sections
)

add_executable(main.elf ${SOURCES} ${LIB_SOURCES})

add_custom_target(main.bin ALL arm-none-eabi-objcopy -O binary main.elf main.bin DEPENDS main.elf)
add_custom_target(size ALL arm-none-eabi-size -A main.elf DEPENDS main.elf)
add_custom_target(flash ALL st-flash --reset write main.bin 0x8000000 DEPENDS main.bin)

A README.md => README.md +29 -0
@@ 0,0 1,29 @@
# stm32f4discovery

## dependencies

- CMake
- `gcc-arm-none-eabi` toolchain
- [texane/stlink](https://github.com/texane/stlink)
- [stm32f4discovery](https://www.st.com/en/evaluation-tools/stm32f4discovery.html)

## setup

```shell
# setup output directory
mkdir bin && cd bin
cmake ..

# build and run project
make main.bin
make flash
```

## other

```shell
# sync lib files
rsync -a -v --files-from=lib-files-freertos.txt ../embedded-libs/FreeRTOSv10.2.1/ lib/freertos/
rsync -a -v --files-from=lib-files-stm32f4xx.txt ../embedded-libs/STM32F4xx_DSP_StdPeriph_Lib_V1.8.0/ lib/stm32f4xx/
rsync -a -v --files-from=lib-files-stm32f4discovery.txt ../embedded-libs/STM32F4-Discovery_FW_V1.1.0/ lib/stm32f4discovery/
```

A src/main.c => src/main.c +34 -0
@@ 0,0 1,34 @@
#include "stm32f4_discovery.h"

#include "FreeRTOS.h"
#include "task.h"

typedef struct {
    Led_TypeDef led;
    int delay_ms;
} LedTaskParam;

static const LedTaskParam param1 = { .led = LED4, .delay_ms = 200 };
static const LedTaskParam param2 = { .led = LED5, .delay_ms = 800 };

static void led_task(void *args) {
    LedTaskParam param = *(LedTaskParam*) args;
    while (1) {
        STM_EVAL_LEDToggle(param.led);
        vTaskDelay(pdMS_TO_TICKS(param.delay_ms));
    };
}

int main(void) {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    STM_EVAL_LEDInit(LED4);
    STM_EVAL_LEDInit(LED5);

    xTaskCreate(led_task, "LED_blink_1", 128, (void*)&param1, configMAX_PRIORITIES-1, NULL);
    xTaskCreate(led_task, "LED_blink_2", 128, (void*)&param2, configMAX_PRIORITIES-1, NULL);

    vTaskStartScheduler();
    while (1) {}
    return 0;
}