-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCanBus.hpp
73 lines (61 loc) · 1.56 KB
/
CanBus.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include <Arduino.h>
#define STM32_CAN_TIR_TXRQ (1U << 0U) // Bit 0: Transmit Mailbox Request
#define STM32_CAN_RIR_RTR (1U << 1U) // Bit 1: Remote Transmission Request
#define STM32_CAN_RIR_IDE (1U << 2U) // Bit 2: Identifier Extension
#define STM32_CAN_TIR_RTR (1U << 1U) // Bit 1: Remote Transmission Request
#define STM32_CAN_TIR_IDE (1U << 2U) // Bit 2: Identifier Extension
#define CAN_EXT_ID_MASK 0x1FFFFFFFU
#define CAN_STD_ID_MASK 0x000007FFU
namespace Can {
enum Pinout {
RX_PA11_TX_PA12, // 36/48/64/100/144 pin packages
RX_PB8_TX_PB9, // 48/64/100/144 pin packages
RX_PD0_TX_PD1, // 100/144 pin packages
};
enum Bitrate {
CAN_33K3BPS,
CAN_50KBPS,
CAN_100KBPS,
CAN_125KBPS,
CAN_250KBPS,
CAN_500KBPS,
CAN_1000KBPS,
};
class BitrateConfig {
public:
uint8_t TS1;
uint8_t TS2;
uint8_t BRP;
};
extern Can::BitrateConfig canBitrateConfig[];
enum FrameFormat {
Standard,
Extended,
};
enum FrameType {
Data,
Remote,
};
class Frame {
public:
uint32_t id;
uint8_t length;
uint8_t data[8];
FrameFormat format;
FrameType type;
bool isStandardFrame();
bool isExtendedFrame();
bool isDataFrame();
bool isRemoteFrame();
void print();
};
class CanBus {
public:
bool begin(Bitrate bitrate, Pinout pinout);
void setFilter(uint8_t index, uint8_t scale, uint8_t mode, uint8_t fifo, uint32_t bank1, uint32_t bank2);
bool checkReceive();
void receive(Frame* frame);
bool send(Frame* frame);
};
}