Skip to content

Commit

Permalink
Merge pull request #127 from dalathegreat/master
Browse files Browse the repository at this point in the history
LEAF PDM/Inverter: Unify MPRUN handling to save memory
  • Loading branch information
damienmaguire authored Dec 5, 2024
2 parents 0850627 + bb2079c commit 22aec5b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 111 deletions.
43 changes: 11 additions & 32 deletions src/NissanPDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@

static uint16_t Vbatt=0;
static uint16_t VbattSP=0;
static uint8_t counter_1db=0;
static uint8_t counter_1dc=0;
static uint8_t counter_1f2=0;
static uint8_t counter_55b=0;
static uint8_t counter_1d4=0;
static uint8_t mprun10=0;
static uint8_t mprun100=0;
static uint8_t OBCpwrSP=0;
static uint8_t OBCpwr=0;
static bool PPStat = false;
Expand Down Expand Up @@ -205,6 +202,7 @@ void NissanPDM::Task10Ms()
}

uint8_t bytes[8];
mprun10 = (mprun10 + 1) % 4; // mprun10 cycles between 0-1-2-3-0-1...

if (opmode == MOD_CHARGE) //ONLY send when charging else sent by leafinv.cpp
{
Expand Down Expand Up @@ -234,13 +232,10 @@ void NissanPDM::Task10Ms()
// 3: Precharging (0.4%)
// 5: Starting discharge (3x10ms) (2.0%)
// 7: Precharged (93%)
bytes[4] = 0x07 | (counter_1d4 << 6);
//bytes[4] = 0x02 | (counter_1d4 << 6);
bytes[4] = 0x07 | (mprun10 << 6);
//bytes[4] = 0x02 | (mprun10 << 6);
//Bit 2 is HV status. 0x00 No HV, 0x01 HV On.

counter_1d4++;
if(counter_1d4 >= 4) counter_1d4 = 0;

// MSB nibble:
// 0: 35-40ms at startup when gear is 0, then at shutdown 40ms
// after the car has been shut off (6% total)
Expand Down Expand Up @@ -292,15 +287,11 @@ void NissanPDM::Task10Ms()
bytes[3] = ((TMP_battV & 0xC0) | (0x2b)); //0x2b should give no cut req, main rly on permission,normal p limit.
bytes[4] = 0x40; //SOC for dash in Leaf. fixed val.
bytes[5] = 0x00;
bytes[6] = counter_1db;
bytes[6] = mprun10;

// Extra CRC in byte 7
nissan_crc(bytes, 0x85);


counter_1db++;
if(counter_1db >= 4) counter_1db = 0;

can->Send(0x1DB, (uint32_t*)bytes, 8);
}

Expand All @@ -315,7 +306,7 @@ void NissanPDM::Task10Ms()
// 513 000006c0000000

// Let's just send the most common one all the time
// FIXME: This is a very sloppy implementation. Thanks. I try:)
// FIXME: This is a very sloppy implementation. Thanks. I try:) Dala: This message is not 10ms, it is 100ms
bytes[0] = 0x00;
bytes[1] = 0x00;
bytes[2] = 0x06;
Expand All @@ -341,14 +332,10 @@ void NissanPDM::Task10Ms()
bytes[3]=0xD5;
bytes[4]=0x00;//may not need pairing code crap here...and we don't:)
bytes[5]=0x00;
bytes[6]=counter_1dc;
bytes[6]=mprun10;
// Extra CRC in byte 7
nissan_crc(bytes, 0x85);

counter_1dc++;
if (counter_1dc >= 4)
counter_1dc = 0;

can->Send(0x1DC, (uint32_t*)bytes, 8);
}

Expand Down Expand Up @@ -405,15 +392,9 @@ void NissanPDM::Task10Ms()
bytes[3] = 0xAC;
bytes[4] = 0x00;
bytes[5] = 0x3C;
bytes[6] = counter_1f2;
bytes[6] = mprun10;
bytes[7] = 0x8F; //may not need checksum here?

counter_1f2++;
if (counter_1f2 >= 4)
{
counter_1f2 = 0;
}

can->Send(0x1F2, (uint32_t*)bytes, 8);
}

Expand All @@ -423,6 +404,7 @@ void NissanPDM::Task100Ms()

// MSGS for charging with pdm
uint8_t bytes[8];
mprun100 = (mprun100 + 1) % 4; // mprun100 cycles between 0-1-2-3-0-1...

if(BMSspoof)
{
Expand All @@ -435,13 +417,10 @@ void NissanPDM::Task100Ms()
bytes[3] = 0x00;
bytes[4] = 0xDF;
bytes[5] = 0xC0;
bytes[6] = ((0x1 << 4) | (counter_55b));
bytes[6] = ((0x1 << 4) | (mprun100));
// Extra CRC in byte 7
nissan_crc(bytes, 0x85);

counter_55b++;
if(counter_55b >= 4) counter_55b = 0;

can->Send(0x55b, (uint32_t*)bytes, 8);

/////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
115 changes: 36 additions & 79 deletions src/leafinv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@

static uint16_t Vbatt=0;
static uint16_t VbattSP=0;
static uint8_t counter_1db=0;
static uint8_t counter_1dc=0;
static uint8_t counter_11a_d6=0;
static uint8_t counter_1d4=0;
static uint8_t counter_1f2=0;
static uint8_t counter_55b=0;
static uint8_t mprun10=0;
static uint8_t mprun100=0;
static uint8_t OBCpwrSP=0;
static uint8_t OBCpwr=0;
static bool BMSspoofI = true;
Expand Down Expand Up @@ -117,7 +113,7 @@ void LeafINV::Task10Ms()
}

uint8_t bytes[8];

mprun10 = (mprun10 + 1) % 4; // mprun10 cycles between 0-1-2-3-0-1...

/////////////////////////////////////////////////////////////////////////////////////////////////
// CAN Messaage 0x11A
Expand Down Expand Up @@ -161,29 +157,14 @@ void LeafINV::Task10Ms()
{0xaa, 0x80},
};



bytes[3] = weird_d34_values[counter_11a_d6][0];//0xAA;
bytes[4] = weird_d34_values[counter_11a_d6][1];//0xC0;

// Always 0x00 (LeafLogs, canmsgs)
bytes[5] = 0x00;

// A 2-bit counter
bytes[6] = counter_11a_d6;

counter_11a_d6++;
if(counter_11a_d6 >= 4)
{
counter_11a_d6 = 0;
}

bytes[3] = weird_d34_values[mprun10][0];//0xAA;
bytes[4] = weird_d34_values[mprun10][1];//0xC0;
bytes[5] = 0x00; // Always 0x00 (LeafLogs, canmsgs)
bytes[6] = mprun10; // A 2-bit counter

// Extra CRC
nissan_crc(bytes, 0x85);//not sure if this is really working or just making me look like a muppet.



can->Send(0x11A, (uint32_t*)bytes, 8);


Expand Down Expand Up @@ -229,13 +210,10 @@ void LeafINV::Task10Ms()
// 3: Precharging (0.4%)
// 5: Starting discharge (3x10ms) (2.0%)
// 7: Precharged (93%)
bytes[4] = 0x07 | (counter_1d4 << 6);
//bytes[4] = 0x02 | (counter_1d4 << 6);
bytes[4] = 0x07 | (mprun10 << 6);
//bytes[4] = 0x02 | (mprun10 << 6);
//Bit 2 is HV status. 0x00 No HV, 0x01 HV On.

counter_1d4++;
if(counter_1d4 >= 4) counter_1d4 = 0;

// MSB nibble:
// 0: 35-40ms at startup when gear is 0, then at shutdown 40ms
// after the car has been shut off (6% total)
Expand Down Expand Up @@ -356,42 +334,14 @@ void LeafINV::Task10Ms()
bytes[3] = ((TMP_battV & 0xC0) | (0x2b)); //0x2b should give no cut req, main rly on permission,normal p limit.
bytes[4] = 0x40; //SOC for dash in Leaf. fixed val.
bytes[5] = 0x00;
bytes[6] = counter_1db;


bytes[6] = mprun10;

// Extra CRC in byte 7
nissan_crc(bytes, 0x85);


counter_1db++;
if(counter_1db >= 4) counter_1db = 0;

can->Send(0x1DB, (uint32_t*)bytes, 8);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// CAN Message 0x50B

// Statistics from 2016 capture:
// 10 00000000000000
// 21 000002c0000000
// 122 000000c0000000
// 513 000006c0000000

// Let's just send the most common one all the time
// FIXME: This is a very sloppy implementation. Thanks. I try:)
bytes[0] = 0x00;
bytes[1] = 0x00;
bytes[2] = 0x06;
bytes[3] = 0xc0;
bytes[4] = 0x00;
bytes[5] = 0x00;
bytes[6] = 0x00;

//possible problem here as 0x50B is DLC 7....
can->Send(0x50B, (uint32_t*)bytes, 7);

if(BMSspoofI)
{
/////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -406,14 +356,10 @@ void LeafINV::Task10Ms()
bytes[3]=0xD5;
bytes[4]=0x00;//may not need pairing code crap here...and we don't:)
bytes[5]=0x00;
bytes[6]=counter_1dc;
bytes[6]=mprun10;
// Extra CRC in byte 7
nissan_crc(bytes, 0x85);

counter_1dc++;
if (counter_1dc >= 4)
counter_1dc = 0;

can->Send(0x1DC, (uint32_t*)bytes, 8);
}

Expand Down Expand Up @@ -465,15 +411,9 @@ void LeafINV::Task10Ms()
bytes[3] = 0xAC;
bytes[4] = 0x00;
bytes[5] = 0x3C;
bytes[6] = counter_1f2;
bytes[6] = mprun10;
bytes[7] = 0x8F; //may not need checksum here?

counter_1f2++;
if (counter_1f2 >= 4)
{
counter_1f2 = 0;
}

can->Send(0x1F2, (uint32_t*)bytes, 8);
}

Expand All @@ -482,30 +422,47 @@ void LeafINV::Task100Ms()

// MSGS for charging with pdm
uint8_t bytes[8];
mprun100 = (mprun100 + 1) % 4; // mprun100 cycles between 0-1-2-3-0-1...

/////////////////////////////////////////////////////////////////////////////////////////////////
// CAN Message 0x50B - Normally sent from VCM on a LEAF

// Statistics from 2016 capture:
// 10 00000000000000
// 21 000002c0000000
// 122 000000c0000000
// 513 000006c0000000

bytes[0] = 0x00;
bytes[1] = 0x00;
bytes[2] = 0x06; //HCM_WakeUpSleepCommand == 11b == WakeUp
bytes[3] = 0xc0; //CANMASK = 1
bytes[4] = 0x00;
bytes[5] = 0x00;
bytes[6] = 0x00;

//possible problem here as 0x50B is DLC 7....
can->Send(0x50B, (uint32_t*)bytes, 7);

if(BMSspoofI)
{
/////////////////////////////////////////////////////////////////////////////////////////////////
// CAN Message 0x55B:
// CAN Message 0x55B - Normally sent from BMS on a LEAF

bytes[0] = 0xA4;
bytes[1] = 0x40;
bytes[2] = 0xAA;
bytes[3] = 0x00;
bytes[4] = 0xDF;
bytes[5] = 0xC0;
bytes[6] = ((0x1 << 4) | (counter_55b));
bytes[6] = ((0x1 << 4) | (mprun100));
// Extra CRC in byte 7
nissan_crc(bytes, 0x85);

counter_55b++;
if(counter_55b >= 4) counter_55b = 0;

can->Send(0x55b, (uint32_t*)bytes, 8);

/////////////////////////////////////////////////////////////////////////////////////////////////
// CAN Message 0x59E:
// CAN Message 0x59E - Normally sent from BMS on a LEAF

bytes[0] = 0x00;//Static msg works fine here
bytes[1] = 0x00;//Batt capacity for chg and qc.
Expand All @@ -519,7 +476,7 @@ void LeafINV::Task100Ms()
can->Send(0x59e, (uint32_t*)bytes, 8);

/////////////////////////////////////////////////////////////////////////////////////////////////
// CAN Message 0x5BC:
// CAN Message 0x5BC - Normally sent from BMS on a LEAF

// muxed msg with info for gids etc. Will try static for a test.
bytes[0] = 0x3D;//Static msg works fine here
Expand Down

0 comments on commit 22aec5b

Please sign in to comment.