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
57
58class CanWrite final : public PeriodicController</*TStackSize*/512> {
59public:
60 CanWrite();
61 void PeriodicTask(efitick_t nowNt) override;
62};
63
64// allow using shorthand CI
66
67// logical and/or operators so we can use our enum like an int
68constexpr CI operator |(CI lhs, CI rhs) {
69 using T = std::underlying_type_t<CI>;
70 return static_cast<CI>(static_cast<T>(lhs) | static_cast<T>(rhs));
71}
72
73constexpr CI operator &(CI lhs, CI rhs) {
74 using T = std::underlying_type_t<CI>;
75 return static_cast<CI>(static_cast<T>(lhs) & static_cast<T>(rhs));
76}
77
78constexpr CI& operator |=(CI& lhs, CI rhs) {
79 lhs = lhs | rhs;
80 return lhs;
81}
82
83class CanCycle {
84public:
85 explicit CanCycle(uint32_t cycleCounter200hz)
86 : m_cycleFlags(computeFlags(cycleCounter200hz))
87 {
88 }
89
90 bool isInterval(CanInterval interval) {
91 return CanInterval::None != (m_cycleFlags & interval);
92 }
93
94private:
95 static CanInterval computeFlags(uint32_t cycleCount);
96
98};
99
100// We need these helpers because the frame layout is different on STM32H7
101#ifdef STM32H7XX
102#define CAN_SID(f) ((f).std.SID)
103#define CAN_EID(f) ((f).ext.EID)
104#define CAN_ISX(f) ((f).common.XTD)
105#else
106#define CAN_SID(f) ((f).SID)
107#define CAN_EID(f) ((f).EID)
108#define CAN_ISX(f) ((f).IDE)
109#endif
110
111#define CAN_ID(f) (CAN_ISX(f) ? CAN_EID(f) : CAN_SID(f))
112
void processCanRxMessage(const size_t busIndex, const CANRxFrame &msg, efitick_t nowNt)
Definition can_rx.cpp:203
void registerCanListener(CanListener &listener)
Definition can_rx.cpp:84
constexpr CI & operator|=(CI &lhs, CI rhs)
Definition can.h:78
CanInterval
Definition can.h:29
constexpr CI operator|(CI lhs, CI rhs)
Definition can.h:68
constexpr CI operator&(CI lhs, CI rhs)
Definition can.h:73
void registerCanSensor(CanSensorBase &sensor)
Definition can_rx.cpp:92
void resetCanWriteCycle()
Definition can_tx.cpp:45
Definition can.h:83
const CanInterval m_cycleFlags
Definition can.h:97
CanCycle(uint32_t cycleCounter200hz)
Definition can.h:85
bool isInterval(CanInterval interval)
Definition can.h:90
static CanInterval computeFlags(uint32_t cycleCount)
Definition can_tx.cpp:106
Definition can.h:58
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)
static CanTsListener listener