Skip to content

Commit

Permalink
✨ Add working demos
Browse files Browse the repository at this point in the history
- single_level.cpp successfully goes from the throw site to the catch
  block.
  • Loading branch information
kammce committed Jul 12, 2024
1 parent 99e3343 commit 618a6b1
Show file tree
Hide file tree
Showing 17 changed files with 429 additions and 229 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ libhal_make_library(
SOURCES
src/control.cpp
${SOURCE_LIST}

PACKAGES
libhal-util

LINK_LIBRARIES
libhal::util
)

target_compile_definitions(libhal-exceptions PRIVATE
OPTIMIZATION_LEVEL=${OPTIMIZATION_LEVEL})

if(NOT ${CMAKE_CROSSCOMPILING})
libhal_unit_test(
LIBRARY_NAME libhal-exceptions
Expand All @@ -44,5 +53,11 @@ libhal_unit_test(
src/control.cpp
tests/main.test.cpp
${SOURCE_LIST}

PACKAGES
libhal-util

LINK_LIBRARIES
libhal::util
)
endif()
40 changes: 30 additions & 10 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,15 @@ def _is_arm_cortex(self):

@property
def _runtime_select(self):
if self._is_arm_cortex() and self.options.runtime == "builtin":
if self._is_arm_cortex and self.options.runtime == "builtin":
return "ARM_CORTEX_GCC"
elif self._is_arm_cortex() and self.options.runtime == "estell":
elif self._is_arm_cortex and self.options.runtime == "estell":
return "ARM_CORTEX_ESTELL"

def validate(self):
if self.settings.get_safe("compiler.cppstd"):
check_min_cppstd(self, self._min_cppstd)

# Remove this when Estell impl is ready for beta testing
if self.options.runtime != "builtin":
raise ConanInvalidConfiguration(
"Only the 'builtin' exception runtime is supported currently")

def layout(self):
cmake_layout(self)

Expand All @@ -85,9 +80,25 @@ def build_requirements(self):
self.tool_requires("libhal-cmake-util/[^4.0.3]")
self.test_requires("boost-ext-ut/1.1.9")

def requirements(self):
self.requires("libhal-util/[^5.0.0]")

def build(self):
cmake = CMake(self)
cmake.configure(variables={"RUNTIME": "ARM_CORTEX_GCC"})
optimization_level = {
'Debug': 1,
'MinSizeRel': 2,
'RelWithDebInfo': 3,
'Release': 4
}

configure_variables = {"RUNTIME": self._runtime_select}
build_type = self.settings.build_type
if str(build_type) in optimization_level:
configure_variables["OPTIMIZATION_LEVEL"] = optimization_level[str(
build_type)]

cmake.configure(variables=configure_variables)
cmake.build()

def package(self):
Expand All @@ -113,14 +124,23 @@ def package_info(self):
lib_path = os.path.join(self.package_folder,
'lib', 'liblibhal-exceptions.a')

if self.options.runtime == "estell":
self.cpp_info.exelinkflags.extend([
"-Wl,--wrap=__cxa_throw",
"-Wl,--wrap=__cxa_rethrow",
"-Wl,--wrap=__cxa_end_catch",
"-Wl,--wrap=__cxa_begin_catch",
"-Wl,--wrap=__cxa_end_cleanup",
])

# Keep this for now, will update this for the runtime select
if self._is_arm_cortex:
self.cpp_info.exelinkflags = [
self.cpp_info.exelinkflags.extend([
"-Wl,--wrap=__cxa_allocate_exception",
"-Wl,--wrap=__cxa_free_exception",
"-Wl,--wrap=__cxa_call_unexpected",
# Ensure that all symbols are added to the linker's symbol table
"-Wl,--whole-archive",
lib_path,
"-Wl,--no-whole-archive",
]
])
28 changes: 28 additions & 0 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 Khalil Estell
#
# 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
#
# http://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.

cmake_minimum_required(VERSION 3.15)

project(demos LANGUAGES CXX)

libhal_build_demos(
DEMOS
single_level

PACKAGES
libhal-exceptions

LINK_LIBRARIES
libhal::exceptions
)
48 changes: 48 additions & 0 deletions demos/applications/single_level.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Khalil Estell
//
// 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
//
// http://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 "../resource_list.hpp"

int volatile value = 5;

resource_list* resources;

struct error
{
int data = 0;
};

std::uint64_t start = 0;
std::uint64_t end = 0;

void foo()
{
if (value) {
start = resources->clock->uptime();
throw error{ .data = value };
}
}

void application(resource_list& p_resources)
{
resources = &p_resources;
try {
foo();
} catch (error const& p_error) {
end = resources->clock->uptime();
while (true) {
continue;
}
}
}
25 changes: 25 additions & 0 deletions demos/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 Khalil Estell
#
# 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
#
# http://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.

from conan import ConanFile


class demos(ConanFile):
python_requires = "libhal-bootstrap/[^2.0.0]"
python_requires_extend = "libhal-bootstrap.demo"

def requirements(self):
bootstrap = self.python_requires["libhal-bootstrap"]
bootstrap.module.add_demo_requirements(self)
self.requires("libhal-exceptions/[latest || ^1.0.0]")
17 changes: 17 additions & 0 deletions demos/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <libhal-exceptions/control.hpp>

#include "resource_list.hpp"

void terminate_handler()
{
while (true) {
continue;
}
}

int main()
{
hal::set_terminate(terminate_handler);
auto resources = initialize_platform();
application(resources);
}
1 change: 1 addition & 0 deletions demos/platforms/lpc4074.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "lpc4078.cpp"
22 changes: 17 additions & 5 deletions src/internal.hpp → demos/platforms/lpc4078.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <libhal-armcortex/dwt_counter.hpp>
#include <libhal-lpc40/clock.hpp>

#include <libhal-exceptions/control.hpp>
#include "../resource_list.hpp"

namespace hal {
exception_allocator& get_exception_allocator() noexcept;
} // namespace hal
resource_list initialize_platform()
{
using namespace hal::literals;

// Set the MCU to the maximum clock speed
hal::lpc40::maximum(12.0_MHz);

static hal::cortex_m::dwt_counter dwt_steady_clock(
hal::lpc40::get_frequency(hal::lpc40::peripheral::cpu));

return {
.clock = &dwt_steady_clock,
};
}
31 changes: 31 additions & 0 deletions demos/platforms/stm32f103c8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 Khalil Estell
//
// 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
//
// http://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 <libhal-armcortex/dwt_counter.hpp>
#include <libhal-stm32f1/clock.hpp>

#include "../resource_list.hpp"

resource_list initialize_platform()
{
using namespace hal::literals;
hal::stm32f1::maximum_speed_using_internal_oscillator();

auto cpu_frequency = hal::stm32f1::frequency(hal::stm32f1::peripheral::cpu);
static hal::cortex_m::dwt_counter dwt_steady_clock(cpu_frequency);

return {
.clock = &dwt_steady_clock,
};
}
11 changes: 11 additions & 0 deletions demos/resource_list.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <libhal/steady_clock.hpp>

struct resource_list
{
hal::steady_clock* clock;
};

resource_list initialize_platform();
void application(resource_list& p_resource_list);
Loading

0 comments on commit 618a6b1

Please sign in to comment.