format CAN

This commit is contained in:
Matthew Kennedy
2026-04-10 13:35:25 -07:00
parent 700649091c
commit c693095f1d
4 changed files with 34 additions and 32 deletions

View File

@@ -27,7 +27,7 @@ void CanTxThread(void*)
// Current system time.
systime_t prev = chVTGetSystemTime();
while(1)
while (1)
{
for (int ch = 0; ch < AFR_CHANNELS; ch++)
{
@@ -65,7 +65,7 @@ void CanRxThread(void*)
{
chRegSetThreadName("CAN Rx");
while(1)
while (1)
{
CANRxFrame frame;
msg_t msg = canReceiveTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_INFINITE);
@@ -118,7 +118,8 @@ void CanRxThread(void*)
}
}
if (frame.DLC >= 3) {
if (frame.DLC >= 3)
{
// data2 contains pump controller gain in percent (0-200)
float pumpGain = frame.data8[2] * 0.01f;
SetPumpGainAdjust(clampF(0, pumpGain, 1));
@@ -184,9 +185,7 @@ void SendRusefiFormat(uint8_t ch)
// Lambda is valid if:
// 1. Nernst voltage is near target
// 2. Lambda is >0.6 (sensor isn't specified below that)
bool lambdaValid =
nernstDc > (NERNST_TARGET - 0.1f) && nernstDc < (NERNST_TARGET + 0.1f) &&
lambda > 0.6f;
bool lambdaValid = nernstDc > (NERNST_TARGET - 0.1f) && nernstDc < (NERNST_TARGET + 0.1f) && lambda > 0.6f;
{
CanTxTyped<wbo::StandardData> frame(baseAddress + 0);

View File

@@ -6,7 +6,8 @@ void InitCan();
void SendCanData(float lambda, uint16_t measuredResistance);
void SendRusefiFormat(uint8_t ch);
enum class HeaterAllow {
enum class HeaterAllow
{
// no CAN message telling us what to do has been rx'd
Unknown,

View File

@@ -4,18 +4,22 @@
#include "can.h"
CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) {
CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended)
{
CAN_EXT(m_frame) = isExtended ? CAN_IDE_EXT : CAN_IDE_STD;
#ifdef STM32G4XX
#ifdef STM32G4XX
m_frame.common.RTR = 0;
#else // Not CAN FD
#else // Not CAN FD
m_frame.RTR = CAN_RTR_DATA;
#endif
#endif
if (isExtended) {
if (isExtended)
{
CAN_EID(m_frame) = eid;
} else {
}
else
{
CAN_SID(m_frame) = eid;
}
@@ -23,11 +27,13 @@ CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) {
memset(m_frame.data8, 0, sizeof(m_frame.data8));
}
CanTxMessage::~CanTxMessage() {
CanTxMessage::~CanTxMessage()
{
// 100 ms timeout
canTransmitTimeout(&CAND1, CAN_ANY_MAILBOX, &m_frame, TIME_MS2I(100));
}
uint8_t& CanTxMessage::operator[](size_t index) {
uint8_t& CanTxMessage::operator[](size_t index)
{
return m_frame.data8[index];
}

View File

@@ -6,10 +6,11 @@
/**
* Represent a message to be transmitted over CAN.
*
*
* Usage:
* * Create an instance of CanTxMessage
* * Set any data you'd like to transmit either using the subscript operator to directly access bytes, or any of the helper functions.
* * Set any data you'd like to transmit either using the subscript operator to directly access bytes, or any of the
* helper functions.
* * Upon destruction, the message is transmitted.
*/
class CanTxMessage
@@ -40,34 +41,29 @@ private:
/**
* A CAN message based on a type, removing the need for manually flipping bits/bytes.
*/
template <typename TData>
class CanTxTyped final : public CanTxMessage
template <typename TData> class CanTxTyped final : public CanTxMessage
{
static_assert(sizeof(TData) <= sizeof(CANTxFrame::data8));
public:
explicit CanTxTyped(uint32_t eid) : CanTxMessage(eid) { }
explicit CanTxTyped(uint32_t eid)
: CanTxMessage(eid)
{
}
/**
* Access members of the templated type.
*
* Access members of the templated type.
*
* So you can do:
* CanTxTyped<MyType> d;
* d->memberOfMyType = 23;
*/
TData* operator->()
{
return reinterpret_cast<TData*>(&m_frame.data8);
}
TData* operator->() { return reinterpret_cast<TData*>(&m_frame.data8); }
TData& get()
{
return *reinterpret_cast<TData*>(&m_frame.data8);
}
TData& get() { return *reinterpret_cast<TData*>(&m_frame.data8); }
};
template <typename TData>
void transmitStruct(uint32_t eid)
template <typename TData> void transmitStruct(uint32_t eid)
{
CanTxTyped<TData> frame(eid);
// Destruction of an instance of CanTxMessage will transmit the message over the wire.