Skyward boardcore
Loading...
Searching...
No Matches
CanInterrupt.cpp
Go to the documentation of this file.
1/* Copyright (c) 2015-2021 Skyward Experimental Rocketry
2 * Authors: Luca Erbetta, Matteo Piazzolla, Alain Carlucci
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "CanInterrupt.h"
24
25#include <interfaces-impl/arch_registers_impl.h>
26#include <kernel/scheduler/scheduler.h>
27#include <miosix.h>
28
29#include "CanDriver.h"
30
31namespace Boardcore
32{
33
34namespace Canbus
35{
36
38
39}
40
41} // namespace Boardcore
42
43void __attribute__((naked)) CAN1_RX0_IRQHandler()
44{
45 saveContext();
46 asm volatile("mov r0, #0");
47 asm volatile("mov r1, #0");
48 asm volatile("bl _Z20CAN_RXIRQHandlerImplii");
49 restoreContext();
50}
51
52void __attribute__((naked)) CAN1_RX1_IRQHandler()
53{
54 saveContext();
55 asm volatile("mov r0, #0");
56 asm volatile("mov r1, #1");
57 asm volatile("bl _Z20CAN_RXIRQHandlerImplii");
58 restoreContext();
59}
60
61void __attribute__((naked)) CAN2_RX0_IRQHandler()
62{
63 saveContext();
64 asm volatile("mov r0, #1");
65 asm volatile("mov r1, #0");
66 asm volatile("bl _Z20CAN_RXIRQHandlerImplii");
67 restoreContext();
68}
69
70void __attribute__((naked)) CAN2_RX1_IRQHandler()
71{
72 saveContext();
73 asm volatile("mov r0, #1");
74 asm volatile("mov r1, #1");
75 asm volatile("bl _Z20CAN_RXIRQHandlerImplii");
76 restoreContext();
77}
78
79void __attribute__((naked)) CAN1_TX_IRQHandler()
80{
81 saveContext();
82 asm volatile("mov r0, #0");
83 asm volatile("bl _Z20CAN_TXIRQHandlerImpli");
84 restoreContext();
85}
86
87void __attribute__((naked)) CAN2_TX_IRQHandler()
88{
89 saveContext();
90 asm volatile("mov r0, #1");
91 asm volatile("bl _Z20CAN_TXIRQHandlerImpli");
92 restoreContext();
93}
94
95void __attribute__((used)) CAN_RXIRQHandlerImpl(int canDev, int fifo)
96{
97 using namespace Boardcore::Canbus;
98 (void)canDev;
99
100 if (canDrivers[canDev])
101 canDrivers[canDev]->handleRXInterrupt(fifo);
102}
103
104void __attribute__((used)) CAN_TXIRQHandlerImpl(int canDev)
105{
106 (void)canDev;
107 bool hppw = false;
108
109 using namespace Boardcore::Canbus;
110
111 CanbusDriver* bus = canDrivers[canDev];
112
113 if (bus)
114 {
115 CAN_TypeDef* can = bus->getCAN();
116
117 CanTXResult res;
118 res.tme = can->TSR & CAN_TSR_TME >> 26;
119 res.errCode = (can->ESR | CAN_ESR_LEC) >> 4;
120
121 if ((can->TSR & CAN_TSR_RQCP0) > 0)
122 {
123 res.mailbox = 0;
124 res.txStatus =
125 can->TSR & (CAN_TSR_TXOK0 | CAN_TSR_ALST0 | CAN_TSR_TERR0) >> 1;
126
127 can->TSR |= CAN_TSR_RQCP0;
128 }
129 if ((can->TSR & CAN_TSR_RQCP1) > 0)
130 {
131 res.mailbox = 1;
132 res.txStatus =
133 can->TSR & (CAN_TSR_TXOK1 | CAN_TSR_ALST1 | CAN_TSR_TERR1) >> 9;
134
135 can->TSR |= CAN_TSR_RQCP1;
136 }
137 if ((can->TSR & CAN_TSR_RQCP2) > 0)
138 {
139 res.mailbox = 2;
140 res.txStatus = can->TSR & (CAN_TSR_TXOK2 | 2 | CAN_TSR_TERR2) >> 17;
141
142 can->TSR |= CAN_TSR_RQCP2;
143 }
144
145 res.seq = bus->getTXMailboxSequence(res.mailbox);
146
147 bus->getTXResultBuffer().IRQput(res, hppw);
148 bus->wakeTXThread();
149 }
150
151 if (hppw)
152 miosix::Scheduler::IRQfindNextThread();
153}
void __attribute__((naked)) CAN1_RX0_IRQHandler()
void canDev
void int fifo
Low level CanBus driver, with support for both peripherals (CAN1 and CAN2) on stm32f4 microcontroller...
Definition CanDriver.h:45
void handleRXInterrupt(int fifo)
Handles an incoming RX interrupt. ONLY to be called from the Canbus RX interrupt handler routine.
IRQCircularBuffer< CanTXResult, TX_STATUS_BUF_SIZE > & getTXResultBuffer()
Returns a reference to the buffer containing transfer results and statistics.
Definition CanDriver.h:171
uint32_t getTXMailboxSequence(uint8_t i)
Gets the sequence number of the message in the ith tx mailbox.
Definition CanDriver.h:184
void wakeTXThread()
Wakes the transmission thread. ONLY to be called from the Canbus TX interrupt handler routine.
CAN_TypeDef * getCAN()
Returns the CanBus peripheral assigned to this instance.
Definition CanDriver.h:179
CanbusDriver * canDrivers[2]
This file includes all the types the logdecoder script will decode.