rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
can.h
Go to the documentation of this file.
1/**
2 * @file can.h
3 *
4 * @date Mar 19, 2020
5 * @author Matthew Kennedy, (c) 2020
6 */
7
8#pragma once
9
10#define HAS_CAN_FRAME (EFI_SIMULATOR || HAL_USE_CAN || EFI_UNIT_TEST)
11
12#if EFI_UNIT_TEST || !EFI_CAN_SUPPORT
13#include "can_mocks.h"
14#endif // EFI_PROD_CODE
15
16#if !EFI_UNIT_TEST
17#include "hal.h"
18#endif // EFI_UNIT_TEST
19
21
22#define CAN_TIMEOUT MS2NT(100)
23
24//can tx periodic task cycle time in frequency, 200hz -> 5ms period
25#define CAN_CYCLE_FREQ (200.0f)
26//can tx periodic task cycle time in ms
27#define CAN_CYCLE_PERIOD (CH_CFG_ST_FREQUENCY / CAN_CYCLE_FREQ)
28
29enum class CanInterval : uint16_t {
30 None = 0,
31 _5ms = 1 << 0,
32 _10ms = 1 << 1,
33 _20ms = 1 << 2,
34 _50ms = 1 << 3,
35 _100ms = 1 << 4,
36 _200ms = 1 << 5,
37 _250ms = 1 << 6,
38 _500ms = 1 << 7,
39 _1000ms = 1 << 8,
41};
42
44
45// 11 bit (CAN 2.0A)
46#define IS_EXT_RANGE_ID(id) ((id) >= 2048)
47
48class CanListener;
49class CanSensorBase;
50
51#if EFI_CAN_SUPPORT
52void processCanRxMessage(const size_t busIndex, const CANRxFrame& msg, efitick_t nowNt);
53#endif // EFI_CAN_SUPPORT
54
55void registerCanListener(CanListener& listener);
57
59// TODO: unregisterCanSensor()?
60
61class CanWrite final : public PeriodicController</*TStackSize*/512> {
62public:
63 CanWrite();
64 void PeriodicTask(efitick_t nowNt) override;
65};
66
67// allow using shorthand CI
69
70// logical and/or operators so we can use our enum like an int
71constexpr CI operator |(CI lhs, CI rhs) {
72 using T = std::underlying_type_t<CI>;
73 return static_cast<CI>(static_cast<T>(lhs) | static_cast<T>(rhs));
74}
75
76constexpr CI operator &(CI lhs, CI rhs) {
77 using T = std::underlying_type_t<CI>;
78 return static_cast<CI>(static_cast<T>(lhs) & static_cast<T>(rhs));
79}
80
81constexpr CI& operator |=(CI& lhs, CI rhs) {
82 lhs = lhs | rhs;
83 return lhs;
84}
85
86class CanCycle {
87public:
88 explicit CanCycle(uint32_t cycleCounter200hz)
89 : m_cycleFlags(computeFlags(cycleCounter200hz))
90 {
91 }
92
93 bool isInterval(CanInterval interval) {
94 return CanInterval::None != (m_cycleFlags & interval);
95 }
96
97private:
98 static CanInterval computeFlags(uint32_t cycleCount);
99
101};
102
103// We need these helpers because the frame layout is different on STM32H7
104#ifdef STM32H7XX
105#define CAN_SID(f) ((f).std.SID)
106#define CAN_EID(f) ((f).ext.EID)
107#define CAN_ISX(f) ((f).common.XTD)
108#else
109#define CAN_SID(f) ((f).SID)
110#define CAN_EID(f) ((f).EID)
111#define CAN_ISX(f) ((f).IDE)
112#endif
113
114#define CAN_ID(f) (CAN_ISX(f) ? CAN_EID(f) : CAN_SID(f))
115
void processCanRxMessage(const size_t busIndex, const CANRxFrame &msg, efitick_t nowNt)
Definition can_rx.cpp:227
void registerCanListener(CanListener &listener)
Definition can_rx.cpp:86
constexpr CI & operator|=(CI &lhs, CI rhs)
Definition can.h:81
CanInterval
Definition can.h:29
constexpr CI operator|(CI lhs, CI rhs)
Definition can.h:71
constexpr CI operator&(CI lhs, CI rhs)
Definition can.h:76
void registerCanSensor(CanSensorBase &sensor)
Definition can_rx.cpp:116
void unregisterCanListener(CanListener &listener)
Definition can_rx.cpp:96
void resetCanWriteCycle()
Definition can_tx.cpp:45
Definition can.h:86
const CanInterval m_cycleFlags
Definition can.h:100
CanCycle(uint32_t cycleCounter200hz)
Definition can.h:88
bool isInterval(CanInterval interval)
Definition can.h:93
static CanInterval computeFlags(uint32_t cycleCount)
Definition can_tx.cpp:106
Definition can.h:61
CanWrite()
Definition can_tx.cpp:24
void PeriodicTask(efitick_t nowNt) override
Called periodically. Override this method to do work for your controller.
Definition can_tx.cpp:50
Base class for a controller that needs to run periodically to perform work.
static Lps25Sensor sensor(device)