Skip to content

Commit

Permalink
AEMNET: move to separate files
Browse files Browse the repository at this point in the history
(cherry picked from commit cb169fd)
  • Loading branch information
dron0gus authored and rusefillc committed Feb 4, 2025
1 parent bd4f9bd commit df61136
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 36 deletions.
1 change: 1 addition & 0 deletions firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ CPPSRC = $(ALLCPPSRC) \
shared/flash.cpp \
can.cpp \
can_helper.cpp \
can_aemnet.cpp \
fault.cpp \
lambda_conversion.cpp \
pwm.cpp \
Expand Down
4 changes: 4 additions & 0 deletions firmware/can.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "hal.h"

#include "can.h"
#include "can_aemnet.h"

#include "port.h"
#include "fault.h"
#include "can_helper.h"
#include "heater_control.h"
#include "lambda_conversion.h"
#include "sampling.h"
#include "pump_dac.h"
#include "port.h"
#include "max3185x.h"

// this same header is imported by rusEFI to get struct layouts and firmware version
#include "../for_rusefi/wideband_can.h"
Expand Down Expand Up @@ -194,4 +197,5 @@ void SendRusefiFormat(uint8_t ch)
__attribute__((weak)) void SendCanForChannel(uint8_t ch)
{
SendRusefiFormat(ch);
SendAemNetUEGOFormat(ch);
}
107 changes: 107 additions & 0 deletions firmware/can_aemnet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "can.h"
#include "hal.h"

#include "can_helper.h"
#include "can_aemnet.h"

#include "port.h"
#include "fault.h"
#include "heater_control.h"
#include "lambda_conversion.h"
#include "sampling.h"
#include "pump_dac.h"
#include "max3185x.h"

// AEMNet protocol

#define AEMNET_UEGO_TX_PERIOD_MS 10
#define AEMNET_UEGO_BASE_ID 0x00000180

namespace aemnet
{
// 29 bit ID, 500kbs, rate 100 hz, endian big, DLC 8
// ID: 0x180 .. 0x18f
struct UEGOData
{
// 0.0001 Lambda/bit, 0 to 6.5535 Lambda
beuint16_t Lambda;
// 0.001 %/bit, -32.768% to 32.768%
beuint16_t Oxygen;
// 0.1 V/bit, 0 to 25.5 Volts
uint8_t SystemVolts;
uint8_t reserved;
// [1] - Bosch LSU4.9 detected
// [5] - Free-Air cal in use
// [7] - Lambda data valid
uint8_t Flags;
// [6] - Sensor Fault
uint8_t Faults;
};
// TODO!!!
// __attribute__((packed));

static_assert(sizeof(UEGOData) == 8);

#define AEMNET_EGT_TX_PERIOD 50
#define AEMNET_EGT_BASE_ID 0x000A0305

// 29 bit ID, 500kbs, rate 20 hz, endian big, DLC 8
// ID: 0x000A0305
struct EgtData
{
// 1 degC/bit, 0 to 65535 degC
beuint16_t TemperatureC;
uint8_t pad[6];
};
//TODO!!!!
// __attribute__((packed));

static_assert(sizeof(EgtData) == 8);

} //namespace aemnet

static int LambdaIsValid(int ch)
{
const auto& sampler = GetSampler(ch);
const auto& heater = GetHeaterController(ch);

float nernstDc = sampler.GetNernstDc();

return ((heater.IsRunningClosedLoop()) &&
(nernstDc > (NERNST_TARGET - 0.1f)) &&
(nernstDc < (NERNST_TARGET + 0.1f)));
}

void SendAemNetUEGOFormat(uint8_t ch)
{
Configuration* configuration = GetConfiguration();
auto id = AEMNET_UEGO_BASE_ID + configuration->afr[ch].AemNetIdOffset;

const auto& sampler = GetSampler(ch);

if (configuration->afr[ch].AemNetTx) {
CanTxTyped<aemnet::UEGOData> frame(id, true);

frame.get().Lambda = GetLambda(ch) * 10000;
frame.get().Oxygen = 0; // TODO:
frame.get().SystemVolts = sampler.GetInternalHeaterVoltage() * 10;
frame.get().Flags =
((configuration->sensorType == SensorType::LSU49) ? 0x02 : 0x00) |
((LambdaIsValid(ch)) ? 0x80 : 0x00);
frame.get().Faults = 0; //TODO:
}
}

#if (EGT_CHANNELS > 0)
void SendAemNetEGTFormat(uint8_t ch)
{
Configuration* configuration = GetConfiguration();
auto id = AEMNET_EGT_BASE_ID + configuration->egt[ch].AemNetIdOffset;

if (configuration->egt[ch].AemNetTx) {
CanTxTyped<aemnet::EgtData> frame(id, true);

frame.get().TemperatureC = getEgtDrivers()[ch].temperature;
}
}
#endif /* EGT_CHANNELS > 0 */
7 changes: 7 additions & 0 deletions firmware/can_aemnet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <cstdint>
#include "byteswap.h"

void SendAemNetUEGOFormat(uint8_t ch);
void SendAemNetEGTFormat(uint8_t ch);
36 changes: 0 additions & 36 deletions for_rusefi/wideband_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,40 +83,4 @@ static inline const char* describeFault(Fault fault) {
return "Unknown";
}

// AEMNet protocol

#define AEMNET_UEGO_TX_PERIOD_MS 10
#define AEMNET_UEGO_BASE_ID 0x00000180

// 29 bit ID, 500kbs, rate 100 hz, endian big, DLC 8
// ID: 0x180 .. 0x18f
struct AemNetUEGOData
{
// 0.0001 Lambda/bit, 0 to 6.5535 Lambda
uint16_t Lambda;
// 0.001 %/bit, -32.768% to 32.768%
uint16_t Oxygen;
// 0.1 V/bit, 0 to 25.5 Volts
uint8_t SystemVolts;
uint8_t reserved;
// [1] - Bosch LSU4.9 detected
// [5] - Free-Air cal in use
// [7] - Lambda data valid
uint8_t Flags;
// [6] - Sensor Fault
uint8_t Faults;
} __attribute__((packed));

#define AEMNET_EGT_TX_PERIOD 50
#define AEMNET_EGT_BASE_ID 0x000A0305

// 29 bit ID, 500kbs, rate 20 hz, endian big, DLC 8
// ID: 0x000A0305
struct AemNetEgtData
{
// 1 degC/bit, 0 to 65535 degC
uint16_t TemperatureC;
uint8_t pad[6];
} __attribute__((packed));

} // namespace wbo

0 comments on commit df61136

Please sign in to comment.