From 712cbfb0178eac0574a237f3da435c278130cdff Mon Sep 17 00:00:00 2001 From: Ewout van Bekkum Date: Mon, 24 Aug 2020 11:55:57 -0700 Subject: [PATCH] lm3s6965evb-qemu: move early_boot into targets/* Updates targets/lm3s6965evb-qemu/* and its sys_io counterpart to match targets/stm32f429i-disc1/* by moving the early boot and vector tables out of sys_io. Change-Id: If1f82d5bd242807d9c4914d207b7f332291de8cf Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/16700 Commit-Queue: Ewout van Bekkum Reviewed-by: Armando Montanez Reviewed-by: Keir Mierle --- pw_sys_io_baremetal_lm3s6965evb/BUILD | 18 ++++-- pw_sys_io_baremetal_lm3s6965evb/BUILD.gn | 11 ++-- .../pw_sys_io_baremetal_lm3s6965evb/init.h} | 7 +-- .../sys_io_baremetal.cc | 53 +---------------- .../sys_io_baremetal.cc | 1 - targets/lm3s6965evb-qemu/BUILD | 35 +++++++++++ targets/lm3s6965evb-qemu/BUILD.gn | 15 +++++ targets/lm3s6965evb-qemu/early_boot.c | 30 ++++++++++ .../lm3s6965evb_executable.gni | 2 +- targets/lm3s6965evb-qemu/vector_table.cc | 59 +++++++++++++++++++ targets/stm32f429i-disc1/BUILD | 2 +- targets/stm32f429i-disc1/BUILD.gn | 2 +- .../stm32f429i_executable.gni | 2 +- targets/stm32f429i-disc1/vector_table.cc | 2 + 14 files changed, 169 insertions(+), 70 deletions(-) rename pw_sys_io_baremetal_lm3s6965evb/{early_boot.c => public/pw_sys_io_baremetal_lm3s6965evb/init.h} (83%) create mode 100644 targets/lm3s6965evb-qemu/BUILD create mode 100644 targets/lm3s6965evb-qemu/early_boot.c create mode 100644 targets/lm3s6965evb-qemu/vector_table.cc diff --git a/pw_sys_io_baremetal_lm3s6965evb/BUILD b/pw_sys_io_baremetal_lm3s6965evb/BUILD index 78d4b762e..85fe01a55 100644 --- a/pw_sys_io_baremetal_lm3s6965evb/BUILD +++ b/pw_sys_io_baremetal_lm3s6965evb/BUILD @@ -12,14 +12,22 @@ # License for the specific language governing permissions and limitations under # the License. +load( + "//pw_build:pigweed.bzl", + "pw_cc_library", +) + package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # Apache License 2.0 -filegroup( +pw_cc_library( name = "pw_sys_io_baremetal_lm3s6965evb", - srcs = [ - "early_boot.c", - "sys_io_baremetal.cc", - ], + hdrs = ["public/pw_sys_io_baremetal_lm3s6965evb/init.h"], + srcs = ["sys_io_baremetal.cc"], + deps = [ + "//pw_preprocessor", + "//pw_sys_io", + ], ) + diff --git a/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn b/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn index b6cec70b6..c64c73fb5 100644 --- a/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn +++ b/pw_sys_io_baremetal_lm3s6965evb/BUILD.gn @@ -17,15 +17,18 @@ import("//build_overrides/pigweed.gni") import("$dir_pw_build/target_types.gni") import("$dir_pw_docgen/docs.gni") +config("default_config") { + include_dirs = [ "public" ] +} + pw_source_set("pw_sys_io_baremetal_lm3s6965evb") { + public_configs = [ ":default_config" ] + public = [ "public/pw_sys_io_baremetal_lm3s6965evb/init.h" ] public_deps = [ "$dir_pw_boot_armv7m" ] deps = [ "$dir_pw_preprocessor", "$dir_pw_sys_io:default_putget_bytes", "$dir_pw_sys_io:facade", ] - sources = [ - "early_boot.c", - "sys_io_baremetal.cc", - ] + sources = [ "sys_io_baremetal.cc" ] } diff --git a/pw_sys_io_baremetal_lm3s6965evb/early_boot.c b/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h similarity index 83% rename from pw_sys_io_baremetal_lm3s6965evb/early_boot.c rename to pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h index c64e0e452..352a73f2b 100644 --- a/pw_sys_io_baremetal_lm3s6965evb/early_boot.c +++ b/pw_sys_io_baremetal_lm3s6965evb/public/pw_sys_io_baremetal_lm3s6965evb/init.h @@ -11,8 +11,7 @@ // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations under // the License. +#pragma once -#include "pw_boot_armv7m/boot.h" - -void pw_boot_PreStaticMemoryInit() {} -void pw_boot_PreStaticConstructorInit() {} +// The actual implement of PreMainInit() in sys_io_BACKEND. +void pw_sys_io_Init(); diff --git a/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc b/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc index ec45eedcf..253b0c52e 100644 --- a/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc +++ b/pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc @@ -14,7 +14,6 @@ #include -#include "pw_boot_armv7m/boot.h" #include "pw_preprocessor/compiler.h" #include "pw_sys_io/sys_io.h" @@ -62,12 +61,6 @@ volatile UartBlock& uart0 = *reinterpret_cast(0x4000C000U); constexpr uint32_t kRcgcUart0EnableMask = 0x1; volatile uint32_t& rcgc1 = *reinterpret_cast(0x400FE104U); -constexpr uint32_t kRccDefault = 0x078E3AD1U; -volatile uint32_t& rcc = *reinterpret_cast(0x400FE070U); - -constexpr uint32_t kRcc2Default = 0x07802810U; -volatile uint32_t& rcc2 = *reinterpret_cast(0x400FE070U); - // Calculate a baud rate multiplier such that we have 16 bits of precision for // the integer portion and 6 bits for the fractional portion. void SetBaudRate(uint32_t clock, uint32_t target_baud) { @@ -77,53 +70,9 @@ void SetBaudRate(uint32_t clock, uint32_t target_baud) { uart0.fractional_baud = (((remainder << 7) / divisor + 1) >> 1) & 0x3f; } -// Default handler to insert into the ARMv7-M vector table (below). -// This function exists for convenience. If a device isn't doing what you -// expect, it might have hit a fault and ended up here. -void DefaultFaultHandler(void) { - while (true) { - // Wait for debugger to attach. - } -} - -// This is the device's interrupt vector table. It's not referenced in any code -// because the platform expects this table to be present at the beginning of -// flash. The exact address is specified in the pw_boot_armv7m configuration as -// part of the target config. -// -// For more information, see ARMv7-M Architecture Reference Manual DDI 0403E.b -// section B1.5.3. - -// This typedef is for convenience when building the vector table. With the -// exception of SP_main (0th entry in the vector table), all the entries of the -// vector table are function pointers. -typedef void (*InterruptHandler)(); - -PW_KEEP_IN_SECTION(".vector_table") -const InterruptHandler vector_table[] = { - // The starting location of the stack pointer. - // This address is NOT an interrupt handler/function pointer, it is simply - // the address that the main stack pointer should be initialized to. The - // value is reinterpret casted because it needs to be in the vector table. - [0] = reinterpret_cast(&pw_boot_stack_high_addr), - - // Reset handler, dictates how to handle reset interrupt. This is the - // address that the Program Counter (PC) is initialized to at boot. - [1] = pw_boot_Entry, - - // NMI handler. - [2] = DefaultFaultHandler, - // HardFault handler. - [3] = DefaultFaultHandler, -}; - } // namespace -extern "C" void pw_boot_PreMainInit() { - // Force RCC to be at default at boot. - rcc = kRccDefault; - rcc2 = kRcc2Default; - +extern "C" void pw_sys_io_Init() { rcgc1 |= kRcgcUart0EnableMask; for (volatile int i = 0; i < 3; ++i) { // We must wait after enabling uart. diff --git a/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc b/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc index 74531ef5f..46f5e40b9 100644 --- a/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc +++ b/pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc @@ -14,7 +14,6 @@ #include -#include "pw_boot_armv7m/boot.h" #include "pw_preprocessor/compiler.h" #include "pw_sys_io/sys_io.h" diff --git a/targets/lm3s6965evb-qemu/BUILD b/targets/lm3s6965evb-qemu/BUILD new file mode 100644 index 000000000..3d6639744 --- /dev/null +++ b/targets/lm3s6965evb-qemu/BUILD @@ -0,0 +1,35 @@ +# Copyright 2020 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. + +load( + "//pw_build:pigweed.bzl", + "pw_cc_library", +) + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) # Apache License 2.0 + +pw_cc_library( + name = "pre_init", + srcs = [ + "early_boot.c", + "vector_table.cc" + ], + deps = [ + "//pw_boot_armv7m", + "//pw_preprocessor", + "//pw_sys_io_baremetal_lm3s6965evb", + ], +) diff --git a/targets/lm3s6965evb-qemu/BUILD.gn b/targets/lm3s6965evb-qemu/BUILD.gn index a326df08a..990397278 100644 --- a/targets/lm3s6965evb-qemu/BUILD.gn +++ b/targets/lm3s6965evb-qemu/BUILD.gn @@ -15,6 +15,7 @@ # gn-format disable import("//build_overrides/pigweed.gni") +import("$dir_pw_build/target_types.gni") import("$dir_pw_docgen/docs.gni") import("$dir_pw_toolchain/generate_toolchain.gni") import("target_toolchains.gni") @@ -22,6 +23,20 @@ generate_toolchains("target_toolchains") { toolchains = pw_target_toolchain_lm3s6965evb_qemu_list } +if (current_toolchain != default_toolchain) { + pw_source_set("pre_init") { + public_deps = [ + "$dir_pw_boot_armv7m", + "$dir_pw_sys_io_baremetal_lm3s6965evb", + ] + deps = [ "$dir_pw_preprocessor" ] + sources = [ + "early_boot.c", + "vector_table.cc", + ] + } +} + pw_doc_group("target_docs") { sources = [ "target_docs.rst" ] } diff --git a/targets/lm3s6965evb-qemu/early_boot.c b/targets/lm3s6965evb-qemu/early_boot.c new file mode 100644 index 000000000..27bf869f4 --- /dev/null +++ b/targets/lm3s6965evb-qemu/early_boot.c @@ -0,0 +1,30 @@ +// Copyright 2020 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. + +#include "pw_boot_armv7m/boot.h" +#include "pw_sys_io_baremetal_lm3s6965evb/init.h" + +void pw_boot_PreStaticMemoryInit() { + // Force RCC to be at default at boot. + const uint32_t kRccDefault = 0x078E3AD1U; + volatile uint32_t* rcc = (volatile uint32_t*)0x400FE070U; + *rcc = kRccDefault; + const uint32_t kRcc2Default = 0x07802810U; + volatile uint32_t* rcc2 = (volatile uint32_t*)0x400FE070U; + *rcc2 = kRcc2Default; +} + +void pw_boot_PreStaticConstructorInit() {} + +void pw_boot_PreMainInit() { pw_sys_io_Init(); } diff --git a/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni b/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni index 7576fceb3..347aff575 100644 --- a/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni +++ b/targets/lm3s6965evb-qemu/lm3s6965evb_executable.gni @@ -22,6 +22,6 @@ template("lm3s6965evb_executable") { if (!defined(deps)) { deps = [] } - deps += [ dir_pw_sys_io_baremetal_lm3s6965evb ] + deps += [ "$dir_pigweed/targets/lm3s6965evb-qemu:pre_init" ] } } diff --git a/targets/lm3s6965evb-qemu/vector_table.cc b/targets/lm3s6965evb-qemu/vector_table.cc new file mode 100644 index 000000000..575a6735d --- /dev/null +++ b/targets/lm3s6965evb-qemu/vector_table.cc @@ -0,0 +1,59 @@ +// Copyright 2020 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. + +#include "pw_boot_armv7m/boot.h" + +namespace { + +// Default handler to insert into the ARMv7-M vector table (below). +// This function exists for convenience. If a device isn't doing what you +// expect, it might have hit a fault and ended up here. +void DefaultFaultHandler(void) { + while (true) { + // Wait for debugger to attach. + } +} + +// This is the device's interrupt vector table. It's not referenced in any +// code because the platform (STM32F4xx) expects this table to be present at the +// beginning of flash. The exact address is specified in the pw_boot_armv7m +// configuration as part of the target config. +// +// For more information, see ARMv7-M Architecture Reference Manual DDI 0403E.b +// section B1.5.3. + +// This typedef is for convenience when building the vector table. With the +// exception of SP_main (0th entry in the vector table), all the entries of the +// vector table are function pointers. +typedef void (*InterruptHandler)(); + +PW_KEEP_IN_SECTION(".vector_table") +const InterruptHandler vector_table[] = { + // The starting location of the stack pointer. + // This address is NOT an interrupt handler/function pointer, it is simply + // the address that the main stack pointer should be initialized to. The + // value is reinterpret casted because it needs to be in the vector table. + [0] = reinterpret_cast(&pw_boot_stack_high_addr), + + // Reset handler, dictates how to handle reset interrupt. This is the + // address that the Program Counter (PC) is initialized to at boot. + [1] = pw_boot_Entry, + + // NMI handler. + [2] = DefaultFaultHandler, + // HardFault handler. + [3] = DefaultFaultHandler, +}; + +} // namespace diff --git a/targets/stm32f429i-disc1/BUILD b/targets/stm32f429i-disc1/BUILD index a3b72d7fb..8be6916a6 100644 --- a/targets/stm32f429i-disc1/BUILD +++ b/targets/stm32f429i-disc1/BUILD @@ -22,7 +22,7 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # Apache License 2.0 pw_cc_library( - name = "pw_pre_init", + name = "pre_init", srcs = [ "early_boot.c", "vector_table.cc" diff --git a/targets/stm32f429i-disc1/BUILD.gn b/targets/stm32f429i-disc1/BUILD.gn index 29d81b214..03169f7d4 100644 --- a/targets/stm32f429i-disc1/BUILD.gn +++ b/targets/stm32f429i-disc1/BUILD.gn @@ -31,7 +31,7 @@ config("pw_malloc_active") { } if (current_toolchain != default_toolchain) { - pw_source_set("pw_pre_init") { + pw_source_set("pre_init") { configs = [ ":pw_malloc_active" ] public_deps = [ "$dir_pw_boot_armv7m", diff --git a/targets/stm32f429i-disc1/stm32f429i_executable.gni b/targets/stm32f429i-disc1/stm32f429i_executable.gni index 55e2a4ad8..b7b21a574 100644 --- a/targets/stm32f429i-disc1/stm32f429i_executable.gni +++ b/targets/stm32f429i-disc1/stm32f429i_executable.gni @@ -23,7 +23,7 @@ template("stm32f429i_executable") { if (!defined(deps)) { deps = [] } - deps += [ "$dir_pigweed/targets/stm32f429i-disc1:pw_pre_init" ] + deps += [ "$dir_pigweed/targets/stm32f429i-disc1:pre_init" ] if (pw_malloc_BACKEND != "") { if (!defined(configs)) { configs = [] diff --git a/targets/stm32f429i-disc1/vector_table.cc b/targets/stm32f429i-disc1/vector_table.cc index 33b66f299..575a6735d 100644 --- a/targets/stm32f429i-disc1/vector_table.cc +++ b/targets/stm32f429i-disc1/vector_table.cc @@ -15,6 +15,7 @@ #include "pw_boot_armv7m/boot.h" namespace { + // Default handler to insert into the ARMv7-M vector table (below). // This function exists for convenience. If a device isn't doing what you // expect, it might have hit a fault and ended up here. @@ -54,4 +55,5 @@ const InterruptHandler vector_table[] = { // HardFault handler. [3] = DefaultFaultHandler, }; + } // namespace