Skip to content

Commit

Permalink
pw_interrupt: Adds a basic interrupt module
Browse files Browse the repository at this point in the history
Adds a basic interrupt module which for now only gives you the
ability to determine if you're executing in an interrupt
context.

In addition this also adds the first two backends of this module's
context facade (pw_interrupt_cortex_m:context_{armv7m,armv8m}).

Change-Id: If0056997f814b0ac33cb0209fc9b247c2599594e
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/24421
Commit-Queue: Ewout van Bekkum <[email protected]>
Reviewed-by: Keir Mierle <[email protected]>
Reviewed-by: Armando Montanez <[email protected]>
  • Loading branch information
Ewout van Bekkum authored and CQ Bot Account committed Dec 1, 2020
1 parent 9618d8a commit bd4906c
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ group("module_docs") {
"$dir_pw_fuzzer:docs",
"$dir_pw_hdlc_lite:docs",
"$dir_pw_hex_dump:docs",
"$dir_pw_interrupt:docs",
"$dir_pw_interrupt_cortex_m:docs",
"$dir_pw_kvs:docs",
"$dir_pw_log:docs",
"$dir_pw_log_basic:docs",
Expand Down
2 changes: 2 additions & 0 deletions modules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ declare_args() {
dir_pw_hex_dump = get_path_info("pw_hex_dump", "abspath")
dir_pw_env_setup = get_path_info("pw_env_setup", "abspath")
dir_pw_hdlc_lite = get_path_info("pw_hdlc_lite", "abspath")
dir_pw_interrupt = get_path_info("pw_interrupt", "abspath")
dir_pw_interrupt_cortex_m = get_path_info("pw_interrupt_cortex_m", "abspath")
dir_pw_kvs = get_path_info("pw_kvs", "abspath")
dir_pw_log = get_path_info("pw_log", "abspath")
dir_pw_log_basic = get_path_info("pw_log_basic", "abspath")
Expand Down
54 changes: 54 additions & 0 deletions pw_interrupt/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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

# TODO(pwbug/101): Need to add support for facades/backends to Bazel.
PW_INTERRUPT_CONTEXT_BACKEND = "//pw_interrupt_context_cortex_m:context_armv7m"

pw_cc_library(
name = "context_facade",
hdrs = [
"public/pw_interrupt/context.h",
],
includes = ["public"],
srcs = [
"context.cc"
],
deps = [
PW_INTERRUPT_CONTEXT_BACKEND + "_headers",
],
)

pw_cc_library(
name = "context",
deps = [
":context_facade",
PW_INTERRUPT_CONTEXT_BACKEND + "_headers",
],
)

pw_cc_library(
name = "context_backend",
deps = [
PW_INTERRUPT_CONTEXT_BACKEND,
],
)
39 changes: 39 additions & 0 deletions pw_interrupt/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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.

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

import("$dir_pw_build/facade.gni")
import("$dir_pw_docgen/docs.gni")
import("$dir_pw_unit_test/test.gni")

declare_args() {
# Backend for the pw_interrupt module.
pw_interrupt_CONTEXT_BACKEND = ""
}

config("public_include_path") {
include_dirs = [ "public" ]
visibility = [ ":*" ]
}

pw_facade("context") {
backend = pw_interrupt_CONTEXT_BACKEND
public_configs = [ ":public_include_path" ]
public = [ "public/pw_interrupt/context.h" ]
}

pw_doc_group("docs") {
sources = [ "docs.rst" ]
}
12 changes: 12 additions & 0 deletions pw_interrupt/docs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.. _module-pw_interrupt:

------------
pw_interrupt
------------
Pigweed's interrupt module provides a consistent interface for to determine
whether one is currently executing in an interrupt context (IRQ or NMI) or not.

.. c:function:: bool InInterruptContext()
Returns true if currently executing within an interrupt service routine
handling an IRQ or NMI.:w!
24 changes: 24 additions & 0 deletions pw_interrupt/public/pw_interrupt/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.
#pragma once

#include "pw_interrupt_backend/context_backend.h"

namespace pw::interrupt {

// Returns true if currently executing within an interrupt service routine
// handling an IRQ or NMI.
bool InInterruptContext();

} // namespace pw::interrupt
66 changes: 66 additions & 0 deletions pw_interrupt_cortex_m/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 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 = "context_armv7m_headers",
hdrs = [
"public/pw_interrupt_cortex_m/context.h",
"public_overrides/pw_interrupt_backend/context_backend.h",
],
copts = [ "-DPW_INTERRUPT_CORTEX_M_ARMV7M=1" ],
includes = [
"public",
"public_overrides",
],
)

pw_cc_library(
name = "context_armv7m",
copts = [ "-DPW_INTERRUPT_CORTEX_M_ARMV7M=1" ],
deps = [
":context_armv7m_headers",
"//pw_interrupt:context_facade",
],
)

pw_cc_library(
name = "context_armv8m_headers",
hdrs = [
"public/pw_interrupt_cortex_m/context.h",
"public_overrides/pw_interrupt_backend/context_backend.h",
],
copts = [ "-DPW_INTERRUPT_CORTEX_M_ARMV8M=1" ],
includes = [
"public",
"public_overrides",
],
)

pw_cc_library(
name = "context_armv8m",
copts = [ "-DPW_INTERRUPT_CORTEX_M_ARMV8M=1" ],
deps = [
":context_armv8m_headers",
"//pw_interrupt:context_facade",
],
)
64 changes: 64 additions & 0 deletions pw_interrupt_cortex_m/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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.

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

import("$dir_pw_build/target_types.gni")
import("$dir_pw_docgen/docs.gni")

config("public_include_path") {
include_dirs = [ "public" ]
visibility = [ ":*" ]
}

config("backend_config") {
include_dirs = [ "public_overrides" ]
visibility = [ ":*" ]
}

config("armv7m") {
defines = [ "PW_INTERRUPT_CORTEX_M_ARMV7M=1" ]
}

config("armv8m") {
defines = [ "PW_INTERRUPT_CORTEX_M_ARMV8M=1" ]
}

_context_common = {
public_deps = [ "$dir_pw_interrupt:context.facade" ]
public_configs = [
":public_include_path",
":backend_config",
]
public = [
"public/pw_interrupt_cortex_m/context.h",
"public_overrides/pw_interrupt_backend/context_backend.h",
]
}

# This targets provides the ARMv7-M backend for pw_interrupt's context facade.
pw_source_set("context_armv7m") {
forward_variables_from(_context_common, "*")
public_configs += [ ":armv7m" ]
}

# This targets provides the ARMv8-M backend for pw_interrupt's context facade.
pw_source_set("context_armv8m") {
forward_variables_from(_context_common, "*")
public_configs += [ ":armv8m" ]
}

pw_doc_group("docs") {
sources = [ "docs.rst" ]
}
7 changes: 7 additions & 0 deletions pw_interrupt_cortex_m/docs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. _module-pw_interrupt_cortex_m:

---------------------
pw_interrupt_cortex_m
---------------------
Pigweed's interrupt Cortex-M module provides a set of architecture specific
backends for ``pw_interrupt``.
34 changes: 34 additions & 0 deletions pw_interrupt_cortex_m/public/pw_interrupt_cortex_m/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.
#pragma once

namespace pw::interrupt {

#include <cstdint>

#if defined(PW_INTERRUPT_CORTEX_M_ARMV7M) || \
defined(PW_INTERRUPT_CORTEX_M_ARMV8M)
inline bool InInterruptContext() {
// ARMv7M Reference manual section B1.4.2 describes how the Interrupt
// Program Status Register (IPSR) is zero if there is no exception (interrupt)
// being processed.
uint32_t ipsr;
asm volatile("MRS %0, ipsr" : "=r"(ipsr));
return ipsr != 0;
}
#else
#error "Please select an architecture specific backend."
#endif

} // namespace pw::interrupt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

// This override header includes the main tokenized logging header and defines
// the PW_LOG macro as the tokenized logging macro.
#pragma once

#include "pw_interrupt_cortex_m/context.h"

0 comments on commit bd4906c

Please sign in to comment.