Skip to content

Instantly share code, notes, and snippets.

@timblakely

timblakely/example_usage.h

Last active Apr 11, 2020
Embed
What would you like to do?
Example of C++-based interrupt table
template <int InplaceFunctionStorageSize = 10>
class Stm32g474reInterruptTable
: public common::InterruptTable<118, InplaceFunctionStorageSize> {
public:
using Callback =
common::InterruptTable<118, InplaceFunctionStorageSize>::Callback;
void relocate_vector_table_to_sram();
void reset_with_default(Callback handler);
void set_interrupt(IRQn_Type interrupt, int P, int SP, Callback handler);
};
/*
MIT License
Copyright (c) 2020 Tim Blakely
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef COMMON_INTERRUPT_TABLE_H_
#define COMMON_INTERRUPT_TABLE_H_
// Disable exception throwing
#define SG14_INPLACE_FUNCTION_THROW(x)
#include <memory>
#include <utility>
#include "external/sg14/SG14/inplace_function.h"
namespace common {
// some metaprogramming utility code
namespace utility {
template <std::size_t... Is>
auto index_over(std::index_sequence<Is...>) {
return [](auto&& f) -> decltype(auto) {
return f(std::integral_constant<std::size_t, Is>{}...);
};
}
template <std::size_t N>
auto index_upto(std::integral_constant<std::size_t, N> = {}) {
return index_over(std::make_index_sequence<N>{});
}
} // namespace utility
template <int NumInterrupts, int InplaceFunctionStorageSize = 10>
class InterruptTable {
public:
using ISR = void (*)();
using Callback = stdext::inplace_function<void(), InplaceFunctionStorageSize>;
InterruptTable() = default;
// Creates an array of ISRs that invoke the associated handler. This is
// equivalent to copy-pasting void global_isr_N() { global_handler_N(); } N
// times.
static std::array<ISR, NumInterrupts> make_isrs() {
return utility::index_upto<NumInterrupts>()(
[](auto... Is) -> std::array<ISR, NumInterrupts> {
return {{[] { handlers[decltype(Is)::value](); }...}};
});
}
protected:
static inline std::array<ISR, NumInterrupts> isrs = make_isrs();
static inline std::array<Callback, NumInterrupts> handlers = {{}};
};
} // namespace common
#endif // COMMON_INTERRUPT_TABLE_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.