Skip to content

Commit 2040d11

Browse files
committed
Move can senders to separate folder
1 parent ee54487 commit 2040d11

9 files changed

Lines changed: 82 additions & 59 deletions

File tree

firmware/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ CPPSRC = $(ALLCPPSRC) \
150150
shared/flash.cpp \
151151
can.cpp \
152152
can_helper.cpp \
153-
can_aemnet.cpp \
153+
can/can_aemnet.cpp \
154+
can/can_rusefi.cpp \
154155
status.cpp \
155156
lambda_conversion.cpp \
156157
pwm.cpp \

firmware/can.cpp

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
#include "status.h"
66
#include "can_helper.h"
7-
#include "can_aemnet.h"
7+
#include "can/can_rusefi.h"
8+
#include "can/can_aemnet.h"
89
#include "heater_control.h"
910
#include "lambda_conversion.h"
1011
#include "sampling.h"
@@ -22,7 +23,7 @@ static Configuration* configuration;
2223
static THD_WORKING_AREA(waCanTxThread, 512);
2324
void CanTxThread(void*)
2425
{
25-
int cycle;
26+
int cycle = 0;
2627
chRegSetThreadName("CAN Tx");
2728

2829
// Current system time.
@@ -183,52 +184,11 @@ void InitCan()
183184
chThdCreateStatic(waCanRxThread, sizeof(waCanRxThread), NORMALPRIO - 4, CanRxThread, nullptr);
184185
}
185186

186-
void SendRusefiFormat(uint8_t ch)
187-
{
188-
auto baseAddress = WB_DATA_BASE_ADDR + 2 * configuration->afr[ch].RusEfiIdx;
189-
190-
const auto& sampler = GetSampler(ch);
191-
const auto& heater = GetHeaterController(ch);
192-
193-
auto nernstDc = sampler.GetNernstDc();
194-
auto pumpDuty = GetPumpOutputDuty(ch);
195-
auto lambda = GetLambda(ch);
196-
197-
// Lambda is valid if:
198-
// 1. Nernst voltage is near target
199-
// 2. Lambda is >0.6 (sensor isn't specified below that)
200-
bool lambdaValid =
201-
nernstDc > (NERNST_TARGET - 0.1f) && nernstDc < (NERNST_TARGET + 0.1f) &&
202-
lambda > 0.6f;
203-
204-
if (configuration->afr[ch].RusEfiTx) {
205-
CanTxTyped<wbo::StandardData> frame(baseAddress + 0);
206-
207-
// The same header is imported by the ECU and checked against this data in the frame
208-
frame.get().Version = RUSEFI_WIDEBAND_VERSION;
209-
210-
uint16_t lambdaInt = lambdaValid ? (lambda * 10000) : 0;
211-
frame.get().Lambda = lambdaInt;
212-
frame.get().TemperatureC = sampler.GetSensorTemperature();
213-
bool heaterClosedLoop = heater.IsRunningClosedLoop();
214-
frame.get().Valid = (heaterClosedLoop && lambdaValid) ? 0x01 : 0x00;
215-
}
216-
217-
if (configuration->afr[ch].RusEfiTxDiag) {
218-
CanTxTyped<wbo::DiagData> frame(baseAddress + 1);;
219-
220-
frame.get().Esr = sampler.GetSensorInternalResistance();
221-
frame.get().NernstDc = nernstDc * 1000;
222-
frame.get().PumpDuty = pumpDuty * 255;
223-
frame.get().status = GetCurrentStatus(ch);
224-
frame.get().HeaterDuty = GetHeaterDuty(ch) * 255;
225-
}
226-
}
227187

228188
// Weak link so boards can override it
229189
__attribute__((weak)) void SendCanForChannel(uint8_t ch)
230190
{
231-
SendRusefiFormat(ch);
191+
SendRusefiFormat(configuration, ch);
232192
SendAemNetUEGOFormat(configuration, ch);
233193
}
234194

firmware/can.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <cstdint>
44

55
void InitCan();
6-
void SendCanData(float lambda, uint16_t measuredResistance);
7-
void SendRusefiFormat(uint8_t ch);
86

97
enum class HeaterAllow {
108
// no CAN message telling us what to do has been rx'd
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,6 @@ static_assert(sizeof(EgtData) == 8);
5656

5757
} //namespace aemnet
5858

59-
static int LambdaIsValid(int ch)
60-
{
61-
const auto& sampler = GetSampler(ch);
62-
const auto& heater = GetHeaterController(ch);
63-
64-
float nernstDc = sampler.GetNernstDc();
65-
66-
return ((heater.IsRunningClosedLoop()) &&
67-
(nernstDc > (NERNST_TARGET - 0.1f)) &&
68-
(nernstDc < (NERNST_TARGET + 0.1f)));
69-
}
70-
7159
void SendAemNetUEGOFormat(Configuration* cfg, uint8_t ch)
7260
{
7361
if (cfg->afr[ch].AemNetTx) {

firmware/can/can_rusefi.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "can.h"
2+
#include "hal.h"
3+
4+
#include "can_rusefi.h"
5+
6+
#include "port.h"
7+
#include "status.h"
8+
#include "can_helper.h"
9+
#include "sampling.h"
10+
#include "pump_dac.h"
11+
#include "heater_control.h"
12+
#include "lambda_conversion.h"
13+
#include "../for_rusefi/wideband_can.h"
14+
15+
void SendRusefiFormat(Configuration* configuration, uint8_t ch)
16+
{
17+
auto baseAddress = WB_DATA_BASE_ADDR + 2 * configuration->afr[ch].RusEfiIdx;
18+
19+
const auto& sampler = GetSampler(ch);
20+
const auto& heater = GetHeaterController(ch);
21+
22+
auto nernstDc = sampler.GetNernstDc();
23+
auto pumpDuty = GetPumpOutputDuty(ch);
24+
auto lambda = GetLambda(ch);
25+
26+
// Lambda is valid if:
27+
// 1. Nernst voltage is near target
28+
// 2. Lambda is >0.6 (sensor isn't specified below that)
29+
bool lambdaValid =
30+
LambdaIsValid(ch) &&
31+
lambda > 0.6f;
32+
33+
if (configuration->afr[ch].RusEfiTx) {
34+
CanTxTyped<wbo::StandardData> frame(baseAddress + 0);
35+
36+
// The same header is imported by the ECU and checked against this data in the frame
37+
frame.get().Version = RUSEFI_WIDEBAND_VERSION;
38+
39+
uint16_t lambdaInt = lambdaValid ? (lambda * 10000) : 0;
40+
frame.get().Lambda = lambdaInt;
41+
frame.get().TemperatureC = sampler.GetSensorTemperature();
42+
bool heaterClosedLoop = heater.IsRunningClosedLoop();
43+
frame.get().Valid = (heaterClosedLoop && lambdaValid) ? 0x01 : 0x00;
44+
}
45+
46+
if (configuration->afr[ch].RusEfiTxDiag) {
47+
CanTxTyped<wbo::DiagData> frame(baseAddress + 1);;
48+
49+
frame.get().Esr = sampler.GetSensorInternalResistance();
50+
frame.get().NernstDc = nernstDc * 1000;
51+
frame.get().PumpDuty = pumpDuty * 255;
52+
frame.get().status = GetCurrentStatus(ch);
53+
frame.get().HeaterDuty = GetHeaterDuty(ch) * 255;
54+
}
55+
}

firmware/can/can_rusefi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#include "port.h"
4+
#include <cstdint>
5+
6+
void SendRusefiFormat(Configuration* configuration, uint8_t ch);

firmware/lambda_conversion.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "lambda_conversion.h"
22
#include "sampling.h"
3+
#include "heater_control.h"
34
#include "port.h"
45

56
static float GetPhiLsu49(float pumpCurrent)
@@ -91,3 +92,15 @@ float GetLambda(int ch)
9192
// Lambda is reciprocal of phi
9293
return 1 / GetPhi(pumpCurrent);
9394
}
95+
96+
int LambdaIsValid(int ch)
97+
{
98+
const auto& sampler = GetSampler(ch);
99+
const auto& heater = GetHeaterController(ch);
100+
101+
float nernstDc = sampler.GetNernstDc();
102+
103+
return ((heater.IsRunningClosedLoop()) &&
104+
(nernstDc > (NERNST_TARGET - 0.1f)) &&
105+
(nernstDc < (NERNST_TARGET + 0.1f)));
106+
}

firmware/lambda_conversion.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#pragma once
22

33
float GetLambda(int ch);
4+
5+
int LambdaIsValid(int ch);

0 commit comments

Comments
 (0)