1515#include " port.hpp"
1616
1717namespace klib ::core::lpc175x::io::detail {
18+ template <uint32_t InterruptId>
1819 class can_interrupt {
1920 public:
2021 // using for the array of callbacks
@@ -28,13 +29,40 @@ namespace klib::core::lpc175x::io::detail {
2829 static inline interrupt_callback callbacks[can_count] = {nullptr , nullptr };
2930
3031 public:
31- template <typename Can>
32+ /* *
33+ * @brief Initialize the Can interrupt. This is needed to
34+ * make sure all Can channel interrupts are called when a
35+ * interrupt is triggered
36+ *
37+ */
3238 static void init () {
3339 // register the interrupt handler
34- target::irq::register_irq<Can::interrupt_id>(irq_handler);
40+ target::irq::register_irq<InterruptId>(irq_handler);
41+
42+ // enable the interrupt
43+ target::enable_irq<InterruptId>();
44+ }
45+
46+ /* *
47+ * @brief Deinit the Can interrupt if no interrupt
48+ * callbacks are set
49+ *
50+ */
51+ static void deinit () {
52+ // check if any of the channels is still in use
53+ for (uint32_t i = 0 ; i < can_count; i++) {
54+ if (callbacks[i] != nullptr ) {
55+ // channel is in use. Do not deinit
56+ return ;
57+ }
58+ }
59+
60+ // no active channels. Unregister the Can interrupt
61+ // and disable the interrupt
62+ target::irq::unregister_irq<InterruptId>();
3563
3664 // enable the interrupt
37- target::enable_irq<Can::interrupt_id >();
65+ target::disable_irq<InterruptId >();
3866 }
3967
4068 /* *
@@ -65,12 +93,13 @@ namespace klib::core::lpc175x::io::detail {
6593 }
6694
6795 /* *
68- * @brief Interrupt handler for the can hardware. This should only be
96+ * @brief Interrupt handler for the Can hardware. This should only be
6997 * called from NVIC
7098 *
7199 */
72100 static void irq_handler () {
73- // call every callback we have
101+ // call every callback we have so they can check if they
102+ // have a interrupt
74103 for (uint32_t i = 0 ; i < can_count; i++) {
75104 // check if we have a valid callback
76105 if (callbacks[i]) {
@@ -374,10 +403,10 @@ namespace klib::core::lpc175x::io {
374403 (void )Can::port->ICR ;
375404
376405 // register the interrupt
377- detail::can_interrupt::template register_irq<Can::id>(irq_handler);
406+ detail::can_interrupt<Can::interrupt_id> ::template register_irq<Can::id>(irq_handler);
378407
379408 // init the can interrupt
380- detail::can_interrupt:: template init <Can> ();
409+ detail::can_interrupt<Can::interrupt_id>:: init ();
381410
382411 // enable the can controller
383412 Can::port->MOD = 0x00 ;
0 commit comments