2020-03-19 05:43:37 -07:00
|
|
|
/**
|
|
|
|
* @file can.h
|
|
|
|
*
|
|
|
|
* @date Mar 19, 2020
|
|
|
|
* @author Matthew Kennedy, (c) 2020
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-09-13 21:12:56 -07:00
|
|
|
#define HAS_CAN_FRAME (EFI_SIMULATOR || HAL_USE_CAN || EFI_UNIT_TEST)
|
|
|
|
|
2022-07-03 05:25:24 -07:00
|
|
|
#if EFI_UNIT_TEST || !EFI_CAN_SUPPORT
|
2021-12-03 22:01:37 -08:00
|
|
|
#include "can_mocks.h"
|
2021-12-08 09:01:28 -08:00
|
|
|
#endif // EFI_PROD_CODE
|
|
|
|
|
|
|
|
#if !EFI_UNIT_TEST
|
2020-03-19 05:43:37 -07:00
|
|
|
#include "hal.h"
|
2021-12-03 22:01:37 -08:00
|
|
|
#endif // EFI_UNIT_TEST
|
2020-03-19 05:43:37 -07:00
|
|
|
|
2020-03-19 11:01:07 -07:00
|
|
|
#include "periodic_thread_controller.h"
|
|
|
|
|
2020-09-06 20:39:25 -07:00
|
|
|
#define CAN_TIMEOUT MS2NT(100)
|
|
|
|
|
2021-04-23 17:16:27 -07:00
|
|
|
//can tx periodic task cycle time in frequency, 200hz -> 5ms period
|
|
|
|
#define CAN_CYCLE_FREQ (200.0f)
|
|
|
|
//can tx periodic task cycle time in ms
|
|
|
|
#define CAN_CYCLE_PERIOD (CH_CFG_ST_FREQUENCY / CAN_CYCLE_FREQ)
|
|
|
|
|
|
|
|
enum class CanInterval : uint16_t {
|
|
|
|
None = 0,
|
|
|
|
_5ms = 1 << 0,
|
|
|
|
_10ms = 1 << 1,
|
|
|
|
_20ms = 1 << 2,
|
|
|
|
_50ms = 1 << 3,
|
|
|
|
_100ms = 1 << 4,
|
|
|
|
_200ms = 1 << 5,
|
|
|
|
_250ms = 1 << 6,
|
|
|
|
_500ms = 1 << 7,
|
|
|
|
_1000ms = 1 << 8,
|
|
|
|
_MAX_Cycle = _1000ms,
|
|
|
|
};
|
|
|
|
|
2024-07-25 15:35:36 -07:00
|
|
|
void resetCanWriteCycle();
|
|
|
|
|
2024-04-07 06:34:34 -07:00
|
|
|
// 11 bit (CAN 2.0A)
|
|
|
|
#define IS_EXT_RANGE_ID(id) ((id) >= 2048)
|
|
|
|
|
2021-03-31 19:51:49 -07:00
|
|
|
class CanListener;
|
2020-03-31 20:21:05 -07:00
|
|
|
class CanSensorBase;
|
|
|
|
|
2021-02-05 20:57:28 -08:00
|
|
|
#if EFI_CAN_SUPPORT
|
2021-12-20 21:48:13 -08:00
|
|
|
void processCanRxMessage(const size_t busIndex, const CANRxFrame& msg, efitick_t nowNt);
|
2021-02-05 20:57:28 -08:00
|
|
|
#endif // EFI_CAN_SUPPORT
|
|
|
|
|
2021-03-31 19:51:49 -07:00
|
|
|
void registerCanListener(CanListener& listener);
|
2020-03-31 20:21:05 -07:00
|
|
|
void registerCanSensor(CanSensorBase& sensor);
|
2020-03-19 11:01:07 -07:00
|
|
|
|
2020-03-19 16:29:56 -07:00
|
|
|
class CanWrite final : public PeriodicController<512> {
|
2020-03-19 11:01:07 -07:00
|
|
|
public:
|
|
|
|
CanWrite();
|
2022-09-11 13:08:11 -07:00
|
|
|
void PeriodicTask(efitick_t nowNt) override;
|
2020-03-19 11:01:07 -07:00
|
|
|
};
|
2021-03-09 15:54:01 -08:00
|
|
|
|
2021-04-23 17:16:27 -07:00
|
|
|
// allow using shorthand CI
|
|
|
|
using CI = CanInterval;
|
|
|
|
|
|
|
|
// logical and/or operators so we can use our enum like an int
|
|
|
|
constexpr CI operator |(CI lhs, CI rhs) {
|
|
|
|
using T = std::underlying_type_t<CI>;
|
|
|
|
return static_cast<CI>(static_cast<T>(lhs) | static_cast<T>(rhs));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CI operator &(CI lhs, CI rhs) {
|
|
|
|
using T = std::underlying_type_t<CI>;
|
|
|
|
return static_cast<CI>(static_cast<T>(lhs) & static_cast<T>(rhs));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CI& operator |=(CI& lhs, CI rhs) {
|
|
|
|
lhs = lhs | rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
class CanCycle {
|
|
|
|
public:
|
|
|
|
explicit CanCycle(uint32_t cycleCounter200hz)
|
|
|
|
: m_cycleFlags(computeFlags(cycleCounter200hz))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInterval(CanInterval interval) {
|
|
|
|
return CanInterval::None != (m_cycleFlags & interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static CanInterval computeFlags(uint32_t cycleCount);
|
|
|
|
|
|
|
|
const CanInterval m_cycleFlags;
|
|
|
|
};
|
|
|
|
|
2021-03-09 15:54:01 -08:00
|
|
|
// We need these helpers because the frame layout is different on STM32H7
|
|
|
|
#ifdef STM32H7XX
|
|
|
|
#define CAN_SID(f) ((f).std.SID)
|
|
|
|
#define CAN_EID(f) ((f).ext.EID)
|
2023-05-08 20:32:14 -07:00
|
|
|
#define CAN_ISX(f) ((f).common.XTD)
|
2021-03-09 15:54:01 -08:00
|
|
|
#else
|
|
|
|
#define CAN_SID(f) ((f).SID)
|
|
|
|
#define CAN_EID(f) ((f).EID)
|
2023-05-08 20:32:14 -07:00
|
|
|
#define CAN_ISX(f) ((f).IDE)
|
2021-03-09 15:54:01 -08:00
|
|
|
#endif
|
2023-05-08 20:45:48 -07:00
|
|
|
|
|
|
|
#define CAN_ID(f) (CAN_ISX(f) ? CAN_EID(f) : CAN_SID(f))
|
|
|
|
|