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 <chrono>
30
31#include "CanDriverData.h"
32#include "Filters.h"
33
34using miosix::Thread;
35
36namespace Boardcore
37{
38
39namespace Canbus
40{
41
47{
48 // How many frames to store in the RX buffer
49 static constexpr unsigned int RX_BUF_SIZE = 32;
50 static constexpr unsigned int TX_STATUS_BUF_SIZE = 32;
51
52 // Weight coefficients for calculating the optimal bit timing
53 static constexpr float BR_ERR_WEIGHT = 10;
54 static constexpr float SP_ERR_WEIGHT = 1;
55 static constexpr float N_ERR_WEIGHT = 1 / 50;
56
57 static constexpr uint8_t NUM_FILTER_BANKS = 28;
58
59public:
64 {
65 uint8_t loopback = 0; // 1: Loopback mode enabled
66 uint8_t awum = 1; // Automatic wakeup (1: automatic wakeup upon new
67 // message received)
68 uint8_t nart =
69 0; // No auto retransmission (0: packets are retransmitted until
70 // success, 1: only one transfer attempt)
71 uint8_t abom = 1; // Automatic bus off management (1: automatically
72 // recovers from bus-off state)
73 uint8_t rflm = 1; // Receive fifo locked (0: new messages overwrite
74 // last ones, 1: new message discarded)
75 uint8_t txfp = 1; // TX Fifo priority (0: identifier, 1: chronological)
76 };
77
83 {
89 uint32_t baudRate;
90
95 };
96
101 {
102 uint16_t BRP;
103 uint16_t BS1;
104 uint16_t BS2;
105 uint8_t SJW;
106 };
107
117 CanbusDriver(CAN_TypeDef* can, CanbusConfig config,
118 AutoBitTiming bitTiming);
119
130 CanbusDriver(CAN_TypeDef* can, CanbusConfig config, BitTiming bitTiming);
131
136
142 bool init(
143 std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
144
153 bool addFilter(FilterBank filter);
154
162 uint32_t send(CanPacket packet);
163
168 {
169 return bufRxPackets;
170 }
171
180
184 CAN_TypeDef* getCAN() { return can; }
185
189 uint32_t getTXMailboxSequence(uint8_t i) { return txMailboxSeq[i]; }
190
195 void wakeTXThread();
196
201 void handleRXInterrupt(int fifo);
202
203private:
210 static BitTiming calcBitTiming(AutoBitTiming config);
211
212 // Stores RX packets
214
215 // Store results of a TX request
217
218 CAN_TypeDef* can;
219
220 uint32_t txSeq = 1; // TX packet sequence number
221 uint32_t txMailboxSeq[3] = {0}; // Seq. no. of packets in the TX mailbox
222
223 uint8_t isInit = false;
224 uint8_t filterIndex = 0;
225 Thread* waiting = nullptr;
226};
227
228} // namespace Canbus
229
230} // namespace Boardcore
void int fifo
Low level CanBus driver, with support for both peripherals (CAN1 and CAN2) on stm32f4 microcontroller...
Definition CanDriver.h:47
uint32_t send(CanPacket packet)
Sends a packet on the bus. This function blocks until the message has been successfully put into a TX...
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:176
IRQCircularBuffer< CanRXPacket, RX_BUF_SIZE > & getRXBuffer()
Returns a reference to the buffer containing received packets.
Definition CanDriver.h:167
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.
uint32_t getTXMailboxSequence(uint8_t i)
Gets the sequence number of the message in the ith tx mailbox.
Definition CanDriver.h:189
void wakeTXThread()
Wakes the transmission thread. ONLY to be called from the Canbus TX interrupt handler routine.
bool init(std::chrono::milliseconds timeout=std::chrono::milliseconds::zero())
Exits initialization mode and starts CanBus operation.
CAN_TypeDef * getCAN()
Returns the CanBus peripheral assigned to this instance.
Definition CanDriver.h:184
Driver for the VN100S IMU.
Struct defining high level bit timing requirements. Register values will then be calculated automatic...
Definition CanDriver.h:83
uint32_t baudRate
Canbus baud rate in bps (BITS PER SECOND). CANOpen standard values are preferred but not mandatory: 1...
Definition CanDriver.h:89
float samplePoint
Sample point in percentage of the bit length. Eg: 0.875.
Definition CanDriver.h:94
Struct specifying exact bit timing registers values.
Definition CanDriver.h:101
Configuration struct for basic CanBus operation.
Definition CanDriver.h:64
Base class for a Canbus filter bank.
Definition Filters.h:49