2020-09-19 16:50:55 -07:00
|
|
|
#include "can.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#include "can_helper.h"
|
2020-12-08 23:16:47 -08:00
|
|
|
#include "heater_control.h"
|
2020-12-10 18:08:06 -08:00
|
|
|
#include "lambda_conversion.h"
|
|
|
|
#include "sampling.h"
|
2020-12-15 15:01:38 -08:00
|
|
|
#include "pump_dac.h"
|
2021-02-25 22:57:44 -08:00
|
|
|
#include "port.h"
|
2020-09-19 16:50:55 -07:00
|
|
|
|
2020-12-10 18:08:06 -08:00
|
|
|
static THD_WORKING_AREA(waCanTxThread, 256);
|
|
|
|
void CanTxThread(void*)
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2021-03-14 00:22:58 -08:00
|
|
|
auto configuration = GetConfiguration();
|
|
|
|
SendEmulatedAemXseries(configuration.CanIndexOffset);
|
2020-12-10 18:08:06 -08:00
|
|
|
|
|
|
|
chThdSleepMilliseconds(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 00:22:58 -08:00
|
|
|
static void SendAck()
|
|
|
|
{
|
|
|
|
CANTxFrame frame;
|
|
|
|
|
|
|
|
frame.IDE = CAN_IDE_EXT;
|
|
|
|
frame.EID = 0x727573; // ascii "rus"
|
|
|
|
frame.RTR = CAN_RTR_DATA;
|
|
|
|
frame.DLC = 0;
|
|
|
|
|
|
|
|
canTransmitTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_INFINITE);
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:18:20 -08:00
|
|
|
static THD_WORKING_AREA(waCanRxThread, 256);
|
|
|
|
void CanRxThread(void*)
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
CANRxFrame frame;
|
|
|
|
msg_t msg = canReceiveTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_INFINITE);
|
|
|
|
|
|
|
|
// Ignore non-ok results...
|
|
|
|
if (msg != MSG_OK)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-19 14:56:22 -08:00
|
|
|
// Ignore std frames, only listen to ext
|
|
|
|
if (frame.IDE != CAN_IDE_EXT)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:18:20 -08:00
|
|
|
// If it's a bootloader entry request, reboot to the bootloader!
|
2020-12-19 14:56:22 -08:00
|
|
|
if (frame.DLC == 0 && frame.EID == 0xEF0'0000)
|
2020-12-12 20:18:20 -08:00
|
|
|
{
|
2021-03-14 00:22:58 -08:00
|
|
|
SendAck();
|
2020-12-12 20:18:20 -08:00
|
|
|
|
|
|
|
// Let the message get out before we reset the chip
|
|
|
|
chThdSleep(50);
|
|
|
|
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
2021-03-14 00:22:58 -08:00
|
|
|
// Check if it's an "index set" message
|
|
|
|
else if (frame.DLC == 1 && frame.EID == 0xEF4'0000)
|
|
|
|
{
|
|
|
|
auto newCfg = GetConfiguration();
|
|
|
|
newCfg.CanIndexOffset = frame.data8[0];
|
|
|
|
SetConfiguration(newCfg);
|
|
|
|
SendAck();
|
|
|
|
}
|
2020-12-12 20:18:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:50:55 -07:00
|
|
|
void InitCan()
|
|
|
|
{
|
2020-12-13 15:06:32 -08:00
|
|
|
canStart(&CAND1, &canConfig500);
|
2020-12-10 18:08:06 -08:00
|
|
|
chThdCreateStatic(waCanTxThread, sizeof(waCanTxThread), NORMALPRIO, CanTxThread, nullptr);
|
2020-12-12 20:18:20 -08:00
|
|
|
chThdCreateStatic(waCanRxThread, sizeof(waCanRxThread), NORMALPRIO - 4, CanRxThread, nullptr);
|
2020-09-19 16:50:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct StandardDataFrame
|
|
|
|
{
|
|
|
|
uint16_t lambda;
|
|
|
|
uint16_t measuredResistance;
|
|
|
|
uint8_t pad[4];
|
|
|
|
};
|
|
|
|
|
2020-12-08 23:16:47 -08:00
|
|
|
#define SWAP_UINT16(x) (((x) << 8) | ((x) >> 8))
|
|
|
|
|
2021-01-17 03:32:17 -08:00
|
|
|
void SendEmulatedAemXseries(uint8_t idx) {
|
2021-03-17 23:40:15 -07:00
|
|
|
CanTxMessage frame(0x180 + idx, 8, true);
|
2020-12-08 23:16:47 -08:00
|
|
|
|
|
|
|
bool isValid = IsRunningClosedLoop();
|
|
|
|
|
2021-01-17 03:32:17 -08:00
|
|
|
float lambda = GetLambda();
|
2020-12-08 23:16:47 -08:00
|
|
|
uint16_t intLambda = lambda * 10000;
|
|
|
|
|
|
|
|
// swap endian
|
|
|
|
intLambda = SWAP_UINT16(intLambda);
|
|
|
|
*reinterpret_cast<uint16_t*>(&frame[0]) = intLambda;
|
|
|
|
|
|
|
|
// bit 1 = LSU 4.9 detected
|
|
|
|
// bit 7 = reading valid
|
|
|
|
frame[6] = 0x02 | (isValid ? 0x80 : 0x00);
|
2020-12-15 15:01:38 -08:00
|
|
|
|
2020-12-15 16:41:19 -08:00
|
|
|
// Hijack a reserved bit to indicate that we're NOT an AEM controller
|
|
|
|
frame[7] = 0x80;
|
|
|
|
|
2020-12-15 15:01:38 -08:00
|
|
|
// Now we embed some extra data for debug
|
|
|
|
// bytes 2-3 are officially oxygen percent
|
|
|
|
// byte 4 is officially supply voltage
|
|
|
|
|
|
|
|
// Report pump output PWM in byte 2, 0-255 for min to max target (128 = 0 current)
|
|
|
|
frame[2] = GetPumpOutputDuty() / 4;
|
|
|
|
|
|
|
|
// Report sensor ESR in byte 3, 4 ohm steps
|
|
|
|
frame[3] = (int)GetSensorInternalResistance() / 4;
|
|
|
|
|
|
|
|
// Report current nernst voltage in byte 4, 5mv steps
|
|
|
|
frame[4] = (int)(GetNernstDc() * 200);
|
2020-12-08 23:16:47 -08:00
|
|
|
}
|
|
|
|
|
2020-09-19 16:50:55 -07:00
|
|
|
void SendCanData(float lambda, uint16_t measuredResistance)
|
|
|
|
{
|
|
|
|
CanTxTyped<StandardDataFrame> frame(0x130);
|
|
|
|
|
2020-11-28 02:56:34 -08:00
|
|
|
frame.get().lambda = lambda * 10000;
|
2020-09-19 16:50:55 -07:00
|
|
|
frame.get().measuredResistance = measuredResistance;
|
|
|
|
}
|