Skip to content

Commit

Permalink
build: reorganize various gen scripts, update core
Browse files Browse the repository at this point in the history
  • Loading branch information
paradajz committed Dec 27, 2022
1 parent 8454671 commit 2bf7ea9
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 315 deletions.
2 changes: 1 addition & 1 deletion modules/core
Submodule core updated from 9fe2a0 to 8eb97f
2 changes: 1 addition & 1 deletion scripts/clang_tidy_wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extra_args=()
# is then missing. To bypass this, generated MCU file is added via include
# flag, and CORE_MCU_STUB symbol is explicitly defined so that only stub
# implementations from core are available.
extra_args+=(-extra-arg="-include$script_dir/../src/board/gen/mcu/$mcu/MCU.h")
extra_args+=(-extra-arg="-include$script_dir/../src/board/generated/mcu/$mcu/CoreMCUGenerated.h")
extra_args+=(-extra-arg="-DCORE_MCU_STUB")

case $compiler in
Expand Down
238 changes: 8 additions & 230 deletions scripts/gen/gen_mcu.sh
Original file line number Diff line number Diff line change
@@ -1,247 +1,25 @@
#!/usr/bin/env bash

# core module already provides defines and Makefile for MCU via
# its own gen_mcu.sh script.
# core module already provides defines and Makefile via mcu Makefile target.
# This script is used to provide additional project-specific
# configuration on top of base configuration.
# configuration on top of base configuration if required.

script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
base_yaml_file=$1
yaml_file=$2
gen_dir=$3
extFreq=$4
mcu=$1
core_yaml_file=$(make --no-print-directory -C "$script_dir"/../../modules/core/src MCU="$mcu" print-MCU_YML_FILE)
project_yaml_file=${script_dir}/../../config/mcu/$mcu.yml
gen_dir=$2
yaml_parser="dasel -n -p yaml --plain -f"
out_header="$gen_dir"/MCU.h
out_makefile="$gen_dir"/MCU.mk

if ! "$script_dir"/../../modules/core/scripts/gen_mcu.sh "$base_yaml_file" "$gen_dir" "$extFreq"
if ! make --no-print-directory -C "$script_dir"/../../modules/core/src MCU="$mcu" MCU_GEN_DIR="$gen_dir"
then
exit 1
fi

echo "Generating MCU configuration..."

printf "%s\n" "### gen_mcu.sh ADDITIONS ###" >> "$out_makefile"

mkdir -p "$gen_dir"

declare -i app_start_page
declare -i boot_supported
declare -i boot_start_page
declare -i boot_end_page
declare -i app_boot_jump_offset
declare -i adc_prescaler
declare -i adc_samples
declare -i boot_size
declare -i factory_flash_page
declare -i factory_flash_page_offset
declare -i eeprom_flash_page_1
declare -i eeprom_flash_page_2
declare -i total_flash_pages

if [[ $($yaml_parser "$yaml_file" flash.boot-start-page) != "null" ]]
then
# Having just "if [[ $boot_start_page != "null" ]]" check later will
# fail because boot_start_page is defined as integer, so even if
# dasel returns null for boot-start-page key, the value will be 0
boot_supported=1
fi

app_start_page=$($yaml_parser "$yaml_file" flash.app-start-page)
boot_start_page=$($yaml_parser "$yaml_file" flash.boot-start-page)
boot_end_page=0
app_boot_jump_offset=$($yaml_parser "$yaml_file" flash.app-boot-jump-offset)
adc_prescaler=$($yaml_parser "$yaml_file" adc.prescaler)
adc_samples=$($yaml_parser "$yaml_file" adc.samples)
boot_size=0
factory_flash_page=0
factory_flash_page_offset=0
eeprom_flash_page_1=0
eeprom_flash_page_2=0
total_flash_pages=0

if [[ $app_start_page == "null" ]]
then
app_start_page=0
fi

if [[ $boot_supported -ne 0 ]]
then
{
printf "%s\n" "DEFINES += HW_SUPPORT_BOOTLOADER"
} >> "$out_makefile"
else
# No bootloader, start at first page
app_start_page=0
fi

{
printf "%s\n" "DEFINES += FLASH_PAGE_APP=$app_start_page"
} >> "$out_makefile"

if [[ $($yaml_parser "$base_yaml_file" flash.pages) != "null" ]]
then
app_start_address=$($yaml_parser "$base_yaml_file" flash.pages.["$app_start_page"].address)

if [[ $boot_supported -ne 0 ]]
then
boot_start_address=$($yaml_parser "$base_yaml_file" flash.pages.["$boot_start_page"].address)
fi

total_flash_pages=$($yaml_parser "$base_yaml_file" flash.pages --length)
else
# All flash pages have common size
app_start_address=$($yaml_parser "$base_yaml_file" flash.page-size)
flash_start_address=$($yaml_parser "$base_yaml_file" flash.flash-start)
((app_start_address*=app_start_page))
((app_start_address+=flash_start_address))

if [[ $boot_supported -ne 0 ]]
then
boot_start_address=$($yaml_parser "$base_yaml_file" flash.page-size)
((boot_start_address*=boot_start_page))
((boot_start_address+=flash_start_address))
fi

total_flash_pages=$($yaml_parser "$base_yaml_file" flash.size)/$($yaml_parser "$base_yaml_file" flash.page-size)
fi

{
printf "%s%x\n" "FLASH_ADDR_APP_START := 0x" "$app_start_address"
printf "%s\n" 'FLASH_ADDR_FW_METADATA := $(shell printf "0x%x" $$(( $(FLASH_ADDR_APP_START) + $(CORE_FW_METADATA_OFFSET) )) )'
} >> "$out_makefile"

if [[ $boot_supported -ne 0 ]]
then
{
printf "%s%x\n" "FLASH_ADDR_BOOT_START := 0x" "$boot_start_address"
} >> "$out_makefile"
fi

if [[ $($yaml_parser "$yaml_file" flash.emueeprom) != "null" ]]
then
factory_flash_page=$($yaml_parser "$yaml_file" flash.emueeprom.factory-page.page)
factory_flash_page_offset=$($yaml_parser "$yaml_file" flash.emueeprom.factory-page.start-offset)
eeprom_flash_page_1=$($yaml_parser "$yaml_file" flash.emueeprom.page1.page)
eeprom_flash_page_2=$($yaml_parser "$yaml_file" flash.emueeprom.page2.page)

if [[ $factory_flash_page_offset == "null" ]]
then
factory_flash_page_offset=0
fi

{
printf "%s\n" "FLASH_PAGE_FACTORY := $factory_flash_page"
printf "%s\n" "FLASH_PAGE_EEPROM_1 := $eeprom_flash_page_1"
printf "%s\n" "FLASH_PAGE_EEPROM_2 := $eeprom_flash_page_2"
printf "%s\n" 'DEFINES += FLASH_PAGE_FACTORY=$(FLASH_PAGE_FACTORY)'
printf "%s\n" 'DEFINES += FLASH_PAGE_EEPROM_1=$(FLASH_PAGE_EEPROM_1)'
printf "%s\n" 'DEFINES += FLASH_PAGE_EEPROM_2=$(FLASH_PAGE_EEPROM_2)'
} >> "$out_makefile"

if [[ $($yaml_parser "$base_yaml_file" flash.pages) == "null" ]]
then
# Common flash page size
{
printf "%s\n" 'EMU_EEPROM_PAGE_SIZE := $(shell echo $$(( ($(FLASH_PAGE_EEPROM_2) - $(FLASH_PAGE_EEPROM_1)) * $(CORE_MCU_FLASH_PAGE_SIZE_COMMON) )) )'
printf "%s\n" 'DEFINES += EMU_EEPROM_PAGE_SIZE=$(EMU_EEPROM_PAGE_SIZE)'
# Offset is never used when flash pages have the same size
printf "%s\n" 'EMU_EEPROM_FLASH_USAGE := $(shell echo $$(( $(EMU_EEPROM_PAGE_SIZE) * 3 )) )'
} >> "$out_makefile"
else
emueeprom_factory_size=$($yaml_parser "$base_yaml_file" flash.pages.["$factory_flash_page"].size)
emueeprom_page1_size=$($yaml_parser "$base_yaml_file" flash.pages.["$eeprom_flash_page_1"].size)
emueeprom_page2_size=$($yaml_parser "$base_yaml_file" flash.pages.["$eeprom_flash_page_2"].size)

emueeprom_page_size=$emueeprom_factory_size
((emueeprom_page_size-=factory_flash_page_offset))
emueeprom_flash_usage=$((emueeprom_page_size + emueeprom_page1_size + emueeprom_page2_size))

{
printf "%s\n" "EMU_EEPROM_PAGE_SIZE := $emueeprom_page_size"
printf "%s\n" 'DEFINES += EMU_EEPROM_PAGE_SIZE=$(EMU_EEPROM_PAGE_SIZE)'
printf "%s\n" "EMU_EEPROM_FLASH_USAGE := $emueeprom_flash_usage"
} >> "$out_makefile"
fi

# When emulated EEPROM is used, one of the pages is factory page with
# default settings. Database shouldn't be formatted in this case.
# The values from factory page should be used as initial ones.
printf "%s\n" "DEFINES += DATABASE_INIT_DATA=0" >> "$out_makefile"
else
{
printf "%s\n" "EMU_EEPROM_FLASH_USAGE := 0"
printf "%s\n" "DEFINES += DATABASE_INIT_DATA=1"
} >> "$out_makefile"
fi

if [[ $app_boot_jump_offset != "null" ]]
then
printf "%s\n" "DEFINES += FLASH_OFFSET_APP_JUMP_FROM_BOOTLOADER=$app_boot_jump_offset" >> "$out_makefile"
fi

# Calculate boot size
# First boot page is known, but last isn't - determine

if [[ $boot_supported -ne 0 ]]
then
boot_end_page=$total_flash_pages

for ((i=0;i<total_flash_pages;i++))
do
if [[ ($app_start_page == "$i") || ($factory_flash_page == "$i") ]]
then
if [[ $i > $boot_start_page ]]
then
boot_end_page=$i
break
fi
fi
done

total_boot_pages=$((boot_end_page-boot_start_page))

if [[ $($yaml_parser "$base_yaml_file" flash.pages) != "null" ]]
then
for ((i=0;i<total_boot_pages;i++))
do
boot_size+=$($yaml_parser "$base_yaml_file" flash.pages.["$i"].size)
done
else
boot_size=$(($($yaml_parser "$base_yaml_file" flash.page-size)*total_boot_pages))
fi

{
printf "%s%s\n" "BOOT_SIZE := " "$boot_size"
printf "%s%s\n" "APP_SIZE := " '$(shell echo $$(( $(CORE_MCU_FLASH_SIZE) - $(BOOT_SIZE) - $(EMU_EEPROM_FLASH_USAGE) )) )'
} >> "$out_makefile"
else
printf "%s%s\n" "APP_SIZE := " '$(shell echo $$(( $(CORE_MCU_FLASH_SIZE) - $(EMU_EEPROM_FLASH_USAGE) )) )' >> "$out_makefile"
fi

if [[ $($yaml_parser "$yaml_file" fuses) != "null" ]]
then
fuse_unlock=$($yaml_parser "$yaml_file" fuses.unlock)
fuse_lock=$($yaml_parser "$yaml_file" fuses.lock)
fuse_ext=$($yaml_parser "$yaml_file" fuses.ext)
fuse_high=$($yaml_parser "$yaml_file" fuses.high)
fuse_low=$($yaml_parser "$yaml_file" fuses.low)

{
printf "%s\n" "FUSE_UNLOCK := $fuse_unlock"
printf "%s\n" "FUSE_LOCK := $fuse_lock"
printf "%s\n" "FUSE_EXT := $fuse_ext"
printf "%s\n" "FUSE_HIGH := $fuse_high"
printf "%s\n" "FUSE_LOW := $fuse_low"
} >> "$out_makefile"
fi

if [[ $adc_prescaler != "null" ]]
then
printf "%s\n" "DEFINES += HW_ADC_PRESCALER=$adc_prescaler" >> "$out_makefile"
fi

if [[ $adc_samples != "null" ]]
then
printf "%s\n" "DEFINES += HW_ADC_SAMPLES=$adc_samples" >> "$out_makefile"
fi
source "$script_dir"/mcu/main.sh
40 changes: 24 additions & 16 deletions scripts/gen/gen_target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,34 @@ gen_dir=$3
yaml_parser="dasel -n -p yaml --plain -f"
out_header="$gen_dir"/Target.h
out_makefile="$gen_dir"/Makefile

echo "Generating target definitions..."
mcu=$($yaml_parser "$yaml_file" mcu)
target_name=$(basename "$yaml_file" .yml)
mcu_gen_dir=$(dirname "$gen_dir")/../mcu/$mcu
hw_test_yaml_file=$(dirname "$yaml_file")/../hw-test/$target_name.yml

mkdir -p "$gen_dir"
echo "" > "$out_header"
echo "" > "$out_makefile"

{
printf "%s\n\n" "#pragma once"
printf "%s\n" "#include \"board/Internal.h\""
printf "%s\n" "#include \"core/src/MCU.h\""
printf "%s\n" "#include \"core/src/arch/common/UART.h\""
printf "%s\n" "#include \"core/src/arch/common/I2C.h\""
} >> "$out_header"
if [[ ! -d $mcu_gen_dir ]]
then
if ! "$script_dir"/gen_mcu.sh "$mcu" "$mcu_gen_dir"
then
exit 1
fi
fi

echo "Generating target definitions..."

source "$script_dir"/target/core.sh
source "$script_dir"/target/peripherals.sh
source "$script_dir"/target/digital_inputs.sh
source "$script_dir"/target/digital_outputs.sh
source "$script_dir"/target/analog_inputs.sh
source "$script_dir"/target/unused_io.sh
source "$script_dir"/target/main.sh

printf "\n%s" "#include \"board/common/Map.h.include\"" >> "$out_header"
if [[ -f $hw_test_yaml_file ]]
then
# HW config files go into the same dir as target ones
if ! "$script_dir"/gen_hwconfig.sh "$project" "$hw_test_yaml_file" "$gen_dir"
then
exit 1
else
printf "%s\n" "DEFINES += TESTS_HW_SUPPORT" >> "$out_makefile"
fi
fi
17 changes: 17 additions & 0 deletions scripts/gen/mcu/adc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

declare -i adc_prescaler
declare -i adc_samples

adc_prescaler=$($yaml_parser "$project_yaml_file" adc.prescaler)
adc_samples=$($yaml_parser "$project_yaml_file" adc.samples)

if [[ $adc_prescaler != "null" ]]
then
printf "%s\n" "DEFINES += HW_ADC_PRESCALER=$adc_prescaler" >> "$out_makefile"
fi

if [[ $adc_samples != "null" ]]
then
printf "%s\n" "DEFINES += HW_ADC_SAMPLES=$adc_samples" >> "$out_makefile"
fi
Loading

0 comments on commit 2bf7ea9

Please sign in to comment.