# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/linker_script.gni")
import("$dir_pw_build/target_types.gni")
import("$dir_pw_malloc/backend.gni")
import("$dir_pw_system/system_target.gni")
import("$dir_pw_toolchain/generate_toolchain.gni")
import("$pw_external_smartfusion_mss/mss.gni")

config("pw_malloc_active") {
  if (pw_malloc_BACKEND != "") {
    defines = [ "PW_MALLOC_ACTIVE=1" ]
  }
}

config("emcraft_ddr_init") {
  # Emcraft's DDR config must be manually set by a custom init function. This
  # conflicts with the built-in MSS init function. I have not looked into this
  # myself to see if it's absoutely needed or not.
  defines = [ "MSS_SYS_MDDR_CONFIG_BY_CORTEX=0" ]
}

if (current_toolchain != default_toolchain) {
  pw_linker_script("mddr_debug_linker_script") {
    defines = [
      "PW_BOOT_CODE_BEGIN=0x00000200",  # After vector table.

      # TODO(skeys) Bootloader is capable of loading 16M of uncompressed code
      # from SPI flash to external RAM. For now use the allocated eNVM flash
      # (256K - Bootloader - InSystemProgrammer = 192K)
      "PW_BOOT_CODE_SIZE=0x30000",

      # TODO: b/235348465 - Currently "pw_tokenizer/detokenize_test" requires at
      # least 6K bytes in heap when using pw_malloc:bucket_block_allocator.
      # The heap size required for tests should be investigated.
      "PW_BOOT_HEAP_SIZE=4M",

      # With external RAM remapped, we use the entire internal ram for the
      # stack (64K).
      "PW_BOOT_MIN_STACK_SIZE=64K",

      # Using external DDR RAM, we just need to make sure we go past our ROM
      # sections.
      "PW_BOOT_RAM_BEGIN=0xA1000000",

      # We assume that the bootloader loaded all 16M of text.
      "PW_BOOT_RAM_SIZE=48M",
      "PW_BOOT_VECTOR_TABLE_BEGIN=0x00000000",
      "PW_BOOT_VECTOR_TABLE_SIZE=512",
    ]
    linker_script = "emcraft_sf2_som_mddr_debug.ld"
  }
  pw_linker_script("mddr_production_linker_script") {
    defines = [
      "PW_BOOT_FLASH_BEGIN=0x00000200",  # After vector table.

      # TODO(skeys) Bootloader is capable of loading 16M of uncompressed code
      # from SPI flash to external RAM. For now use the allocated eNVM flash
      # (256K - Bootloader - InSystemProgrammer = 192K)
      "PW_BOOT_FLASH_SIZE=0x30000",

      # TODO: b/235348465 - Currently "pw_tokenizer/detokenize_test" requires at
      # least 6K bytes in heap when using pw_malloc:bucket_block_allocator.
      # The heap size required for tests should be investigated.
      "PW_BOOT_HEAP_SIZE=4M",

      # With external RAM remapped, we use the entire internal ram for the
      # stack (64K).
      "PW_BOOT_MIN_STACK_SIZE=1024K",

      # Using external DDR RAM, we just need to make sure we go past our ROM
      # sections.
      "PW_BOOT_RAM_BEGIN=0xA1000000",

      # We assume that the bootloader loaded all 16M of text.
      "PW_BOOT_RAM_SIZE=48M",
      "PW_BOOT_VECTOR_TABLE_BEGIN=0x00000000",
      "PW_BOOT_VECTOR_TABLE_SIZE=512",
    ]
    linker_script = "$dir_pw_boot_cortex_m/basic_cortex_m.ld"
  }

  pw_source_set("pre_init") {
    configs = [ ":pw_malloc_active" ]
    deps = [
      "$dir_pw_boot",
      "$dir_pw_boot_cortex_m",
      "$dir_pw_malloc",
      "$dir_pw_preprocessor",
      "$dir_pw_string",
      "$dir_pw_sys_io_emcraft_sf2",
      "$dir_pw_system",
      "$dir_pw_toolchain:infinite_loop",
      "$pw_external_freertos",
      "$pw_external_freertos:support",
    ]
    sources = [
      "boot.cc",
      "vector_table.c",
    ]
  }

  config("config_includes") {
    include_dirs = [ "config" ]
    configs = [ ":emcraft_ddr_init" ]
  }

  pw_source_set("sf2_mss_hal_config") {
    public_configs = [ ":config_includes" ]
    public =
        [ "config/sf2_mss_hal_conf.h" ]  # SKEYS likely want to put the MDDR
                                         # config by cortex etc stuff here
  }

  pw_source_set("sf2_freertos_config") {
    public_configs = [ ":config_includes" ]
    public_deps = [ "$pw_external_freertos:config_assert" ]
    public = [ "config/FreeRTOSConfig.h" ]
  }
}

# Configured for use with a first stage boot loader to configure DDR and
# perform memory remapping.
pw_system_target("emcraft_sf2_som") {
  cpu = PW_SYSTEM_CPU.CORTEX_M3
  scheduler = PW_SYSTEM_SCHEDULER.FREERTOS
  link_deps = [ "$dir_pigweed/targets/emcraft_sf2_som:pre_init" ]

  build_args = {
    pw_log_BACKEND = dir_pw_log_tokenized
    pw_log_tokenized_HANDLER_BACKEND = "$dir_pw_system:log_backend.impl"
    pw_third_party_freertos_CONFIG =
        "$dir_pigweed/targets/emcraft_sf2_som:sf2_freertos_config"
    pw_third_party_freertos_PORT = "$pw_external_freertos:arm_cm3"
    pw_sys_io_BACKEND = dir_pw_sys_io_emcraft_sf2

    pw_boot_cortex_m_LINKER_SCRIPT =
        "$dir_pigweed/targets/emcraft_sf2_som:mddr_production_linker_script"
  }
}

# Debug target configured to work with MSS linker script and startup code.
# TODO(skeys) Add linker script and config for debug builds using SoftConsole.
pw_system_target("emcraft_sf2_som_debug") {
  cpu = PW_SYSTEM_CPU.CORTEX_M3
  scheduler = PW_SYSTEM_SCHEDULER.FREERTOS
  link_deps = [ "$dir_pigweed/targets/emcraft_sf2_som:pre_init" ]

  build_args = {
    pw_log_BACKEND = dir_pw_log_tokenized
    pw_log_tokenized_HANDLER_BACKEND = "$dir_pw_system:log_backend.impl"
    pw_third_party_freertos_CONFIG =
        "$dir_pigweed/targets/emcraft_sf2_som:sf2_freertos_config"
    pw_third_party_freertos_PORT = "$pw_external_freertos:arm_cm3"
    pw_sys_io_BACKEND = dir_pw_sys_io_emcraft_sf2

    # Override the default pw_boot_cortex_m linker script and set the memory
    # regions for the target.
    pw_boot_cortex_m_LINKER_SCRIPT =
        "$dir_pigweed/targets/emcraft_sf2_som:mddr_debug_linker_script"
    pw_third_party_smartfusion_mss_CONFIG = "debug"
  }
}
