Skyward boardcore
Loading...
Searching...
No Matches
CanDriver.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
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#pragma once
24
25#include <interfaces-impl/arch_registers_impl.h>
26#include <miosix.h>
28
29#include "CanDriverData.h"
30#include "Filters.h"
31
32using miosix::Thread;
33
34namespace Boardcore
35{
36
37namespace Canbus
38{
39
45{
46 // How many frames to store in the RX buffer
47 static constexpr unsigned int RX_BUF_SIZE = 10;
48 static constexpr unsigned int TX_STATUS_BUF_SIZE = 10;
49
50 // Weight coefficients for calculating the optimal bit timing
51 static constexpr float BR_ERR_WEIGHT = 10;
52 static constexpr float SP_ERR_WEIGHT = 1;
53 static constexpr float N_ERR_WEIGHT = 1 / 50;
54
55 static constexpr uint8_t NUM_FILTER_BANKS = 28;
56
57public:
62 {
63 uint8_t loopback = 0; // 1: Loopback mode enabled
64 uint8_t awum = 1; // Automatic wakeup (1: automatic wakeup upon new
65 // message received)
66 uint8_t nart =
67 0; // No auto retransmission (0: packets are retransmitted until
68 // success, 1: only one transfer attempt)
69 uint8_t abom = 1; // Automatic bus off management (1: automatically
70 // recovers from bus-off state)
71 uint8_t rflm = 1; // Receive fifo locked (0: new messages overwrite
72 // last ones, 1: new message discarded)
73 uint8_t txfp = 1; // TX Fifo priority (0: identifier, 1: chronological)
74 };
75
81 {
87 uint32_t baudRate;
88
93 };
94
98 struct BitTiming
99 {
100 uint16_t BRP;
101 uint16_t BS1;
102 uint16_t BS2;
103 uint8_t SJW;
104 };
105
115 CanbusDriver(CAN_TypeDef* can, CanbusConfig config,
116 AutoBitTiming bitTiming);
117
128 CanbusDriver(CAN_TypeDef* can, CanbusConfig config, BitTiming bitTiming);
129
134
138 void init();
139
148 bool addFilter(FilterBank filter);
149
157 uint32_t send(CanPacket packet);
158
162 IRQCircularBuffer<CanRXPacket, RX_BUF_SIZE>& getRXBuffer()
163 {
164 return bufRxPackets;
165 }
166
171 IRQCircularBuffer<CanTXResult, TX_STATUS_BUF_SIZE>& getTXResultBuffer()
172 {
173 return bufTxResult;
174 }
175
179 CAN_TypeDef* getCAN() { return can; }
180
184 uint32_t getTXMailboxSequence(uint8_t i) { return txMailboxSeq[i]; }
185
190 void wakeTXThread();
191
196 void handleRXInterrupt(int fifo);
197
198private:
205 static BitTiming calcBitTiming(AutoBitTiming config);
206
207 // Stores RX packets
208 IRQCircularBuffer<CanRXPacket, RX_BUF_SIZE> bufRxPackets;
209
210 // Store results of a TX request
211 IRQCircularBuffer<CanTXResult, TX_STATUS_BUF_SIZE> bufTxResult;
212
213 CAN_TypeDef* can;
214
215 uint32_t txSeq = 1; // TX packet sequence number
216 uint32_t txMailboxSeq[3] = {0}; // Seq. no. of packets in the TX mailbox
217
218 uint8_t isInit = false;
219 uint8_t filterIndex = 0;
220 Thread* waiting = nullptr;
221};
222
223} // namespace Canbus
224
225} // namespace Boardcore
void int fifo
Low level CanBus driver, with support for both peripherals (CAN1 and CAN2) on stm32f4 microcontroller...
Definition CanDriver.h:45
uint32_t send(CanPacket packet)
Sends a packet on the bus. This function blocks until the message has been successfully put into a TX...
CanbusDriver(CAN_TypeDef *can, CanbusConfig config, AutoBitTiming bitTiming)
Construct a new Canbus object, automatically calculating timing register values from high level requi...
Definition CanDriver.cpp:43
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
IRQCircularBuffer< CanRXPacket, RX_BUF_SIZE > & getRXBuffer()
Returns a reference to the buffer containing received packets.
Definition CanDriver.h:162
bool addFilter(FilterBank filter)
Adds a new filter to the bus, or returns false if there are no more filter banks available.
~CanbusDriver()
Disables the peripheral clock.
void init()
Exits initialization mode and starts CanBus operation.
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
This file includes all the types the logdecoder script will decode.
Struct defining high level bit timing requirements. Register values will then be calculated automatic...
Definition CanDriver.h:81
uint32_t baudRate
Canbus baud rate in bps (BITS PER SECOND). CANOpen standard values are preferred but not mandatory: 1...
Definition CanDriver.h:87
float samplePoint
Sample point in percentage of the bit length. Eg: 0.875.
Definition CanDriver.h:92
Struct specifying exact bit timing registers values.
Definition CanDriver.h:99
Configuration struct for basic CanBus operation.
Definition CanDriver.h:62
Base class for a Canbus filter bank.
Definition Filters.h:49