Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDI8: dev #294

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 64 additions & 52 deletions GDI-4ch/firmware/can.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "can.h"
#include "hal.h"

#include "efifeatures.h"

#include <cstdint>
#include <cstring>
#include "sent_canbus_protocol.h"
Expand All @@ -21,7 +23,7 @@
static char VERSION[] = {compilationYear() / 100, compilationYear() % 100, compilationMonth(), compilationDay()};

extern GDIConfiguration configuration;
extern Pt2001 chip;
extern Pt2001 chips[EFI_PT2001_CHIPS];
extern bool isOverallHappyStatus;

static const CANConfig canConfig500 =
Expand All @@ -44,17 +46,17 @@ static void countTxResult(msg_t msg) {
}
}

int canGetOutputCanIdBase(void)
int canGetOutputCanIdBase(size_t chip)
{
return (configuration.outputCanID + boardGetId() * GDI4_BASE_ADDRESS_OFFSET);
return (configuration.outputCanID + (boardGetId() + chip) * GDI4_BASE_ADDRESS_OFFSET);
}

int canGetInputCanIdBase()
int canGetInputCanIdBase(size_t chip)
{
return (configuration.inputCanID + boardGetId() * GDI4_BASE_ADDRESS_OFFSET);
return (configuration.inputCanID + (boardGetId() + chip) * GDI4_BASE_ADDRESS_OFFSET);
}

void SendSomething(int baseID) {
void SendSomething(size_t chip, int baseID) {
CANTxFrame m_frame;

m_frame.IDE = CAN_IDE_EXT;
Expand All @@ -67,7 +69,7 @@ void SendSomething(int baseID) {
m_frame.data8[0] = configuration.inputCanID;
m_frame.data8[1] = configuration.updateCounter;
m_frame.data8[2] = isOverallHappyStatus;
m_frame.data8[6] = (int)chip.fault;
m_frame.data8[6] = (int)chips[chip].fault;
m_frame.data8[7] = GDI4_MAGIC;

msg_t msg = canTransmitTimeout(&CAND1, CAN_ANY_MAILBOX, &m_frame, CAN_TX_TIMEOUT_100_MS);
Expand Down Expand Up @@ -163,6 +165,8 @@ static int intTxCounter = 0;
static THD_WORKING_AREA(waCanTxThread, 256);
void CanTxThread(void*)
{
chRegSetThreadName("CAN TX");

while (1) {
intTxCounter++;
chThdSleepMilliseconds(1000 / CAN_TX_PERIOD_MS);
Expand All @@ -171,19 +175,23 @@ void CanTxThread(void*)
continue; // we were told to be silent
}

// keep constant while sending whole banch of messages
int outID = canGetOutputCanIdBase();
for (size_t i = 0; i < EFI_PT2001_CHIPS; i++) {
// keep constant while sending whole banch of messages
int outID = canGetOutputCanIdBase(i);

if (intTxCounter % (1000 / CAN_TX_PERIOD_MS) == 0) {
sendOutConfiguration(outID);
}
if (intTxCounter % (1000 / CAN_TX_PERIOD_MS) == 0) {
sendOutVersion(outID);
}
if (intTxCounter % (1000 / CAN_TX_PERIOD_MS) == 0) {
sendOutConfiguration(outID);
}
if (intTxCounter % (1000 / CAN_TX_PERIOD_MS) == 0) {
sendOutVersion(outID);
}

SendSomething(outID);
SendSomething(i, outID);

sendOutSentData(outID);
if (i == 0) {
sendOutSentData(outID);
}
}
}
}

Expand All @@ -205,6 +213,8 @@ static float getFloat(CANRxFrame *frame, int offset) {
static THD_WORKING_AREA(waCanRxThread, 256);
void CanRxThread(void*)
{
chRegSetThreadName("CAN RX");

while (1) {
CANRxFrame frame;
msg_t msg = canReceiveTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_INFINITE);
Expand All @@ -213,13 +223,6 @@ void CanRxThread(void*)
if (msg != MSG_OK) {
continue;
}
size_t writeCount = 0;

// writeCount = chsnprintf(printBuffer, sizeof(printBuffer), "eid=%d data[0]=%d dlc=%d\n\n\n\n\n\n\n",
// frame.EID,
// frame.data8[0],
// frame.DLC);


// Ignore std frames, only listen to ext
if (frame.IDE != CAN_IDE_EXT) {
Expand All @@ -231,35 +234,44 @@ void CanRxThread(void*)
continue;
}

bool withNewValue = false;
int inputID = canGetInputCanIdBase();
// TODO: valudate DLC and IDE
if (frame.EID == inputID) {
ASSIGN_IF_CHANGED(configuration.BoostVoltage, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.BoostCurrent, getFloat(&frame, 3));
ASSIGN_IF_CHANGED(configuration.TBoostMin, getInt(&frame, 5));
} else if (frame.EID == inputID + 1) {
ASSIGN_IF_CHANGED(configuration.TBoostMax, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.PeakCurrent, getFloat(&frame, 3));
ASSIGN_IF_CHANGED(configuration.TpeakDuration, getInt(&frame, 5));
} else if (frame.EID == inputID + 2) {
ASSIGN_IF_CHANGED(configuration.TpeakOff, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.Tbypass, getInt(&frame, 3));
ASSIGN_IF_CHANGED(configuration.HoldCurrent, getFloat(&frame, 5));
} else if (frame.EID == inputID + 3) {
ASSIGN_IF_CHANGED(configuration.TholdOff, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.THoldDuration, getInt(&frame, 3));
ASSIGN_IF_CHANGED(configuration.PumpPeakCurrent, getFloat(&frame, 5));
} else if (frame.EID == inputID + 4) {
ASSIGN_IF_CHANGED(configuration.PumpHoldCurrent, getFloat(&frame, 1));
ASSIGN_IF_CHANGED(configuration.outputCanID, getInt(&frame, 3));
}
if (withNewValue) {
saveConfiguration();
chip.restart();
for (size_t i = 0; i < EFI_PT2001_CHIPS; i++) {
// size_t writeCount = 0;

// writeCount = chsnprintf(printBuffer, sizeof(printBuffer), "eid=%d data[0]=%d dlc=%d\n\n\n\n\n\n\n",
// frame.EID,
// frame.data8[0],
// frame.DLC);

bool withNewValue = false;
int inputID = canGetInputCanIdBase(i);
// TODO: valudate DLC and IDE
if (frame.EID == inputID) {
ASSIGN_IF_CHANGED(configuration.BoostVoltage, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.BoostCurrent, getFloat(&frame, 3));
ASSIGN_IF_CHANGED(configuration.TBoostMin, getInt(&frame, 5));
} else if (frame.EID == inputID + 1) {
ASSIGN_IF_CHANGED(configuration.TBoostMax, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.PeakCurrent, getFloat(&frame, 3));
ASSIGN_IF_CHANGED(configuration.TpeakDuration, getInt(&frame, 5));
} else if (frame.EID == inputID + 2) {
ASSIGN_IF_CHANGED(configuration.TpeakOff, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.Tbypass, getInt(&frame, 3));
ASSIGN_IF_CHANGED(configuration.HoldCurrent, getFloat(&frame, 5));
} else if (frame.EID == inputID + 3) {
ASSIGN_IF_CHANGED(configuration.TholdOff, getInt(&frame, 1));
ASSIGN_IF_CHANGED(configuration.THoldDuration, getInt(&frame, 3));
ASSIGN_IF_CHANGED(configuration.PumpPeakCurrent, getFloat(&frame, 5));
} else if (frame.EID == inputID + 4) {
ASSIGN_IF_CHANGED(configuration.PumpHoldCurrent, getFloat(&frame, 1));
ASSIGN_IF_CHANGED(configuration.outputCanID, getInt(&frame, 3));
}
if (withNewValue) {
saveConfiguration();
chips[i].restart();
}
// if (writeCount > 0)
// uartStartSend(&UARTD1, writeCount, printBuffer);
}
// if (writeCount > 0)
// uartStartSend(&UARTD1, writeCount, printBuffer);

chThdSleepMilliseconds(100);
}
Expand Down
4 changes: 4 additions & 0 deletions GDI-4ch/firmware/can.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <cstdint>
#include <cstddef>

int canGetOutputCanIdBase(size_t chip);
int canGetInputCanIdBase(size_t chip);

void InitCan();
11 changes: 11 additions & 0 deletions GDI-4ch/firmware/efifeatures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @file efifeatures.h
*
* @brief In this header we can configure which firmware modules are used.
*/

#pragma once

#ifndef EFI_PT2001_CHIPS
#define EFI_PT2001_CHIPS 1
#endif
75 changes: 56 additions & 19 deletions GDI-4ch/firmware/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "efifeatures.h"

#include "pt2001impl.h"

#include "can.h"
Expand All @@ -24,16 +26,31 @@ static void InitPins() {

bool isOverallHappyStatus = false;

static const SPIConfig spiCfg = {
.circular = false,
.end_cb = nullptr,
.ssport = GPIOB,
.sspad = 2,
.cr1 =
SPI_CR1_DFF |
SPI_CR1_MSTR |
SPI_CR1_CPHA | SPI_CR1_BR_1 | SPI_CR1_SPE,
static const SPIConfig spiCfg[EFI_PT2001_CHIPS] = {
{
.circular = false,
.end_cb = nullptr,
.ssport = GPIOB,
.sspad = 2,
.cr1 =
SPI_CR1_DFF |
SPI_CR1_MSTR |
SPI_CR1_CPHA | SPI_CR1_BR_1 | SPI_CR1_SPE,
.cr2 = SPI_CR2_SSOE
},
#if (EFI_PT2001_CHIPS > 1)
{
.circular = false,
.end_cb = nullptr,
.ssport = GPIOB,
.sspad = 3,
.cr1 =
SPI_CR1_DFF |
SPI_CR1_MSTR |
SPI_CR1_CPHA | SPI_CR1_BR_1 | SPI_CR1_SPE,
.cr2 = SPI_CR2_SSOE
},
#endif
};

void GDIConfiguration::resetToDefaults() {
Expand Down Expand Up @@ -68,8 +85,7 @@ GDIConfiguration *getConfiguration() {
return &configuration;
}


bool Pt2001::init() {
void initPt2001Interface() {
palSetPadMode(GPIOA, 5, PAL_MODE_STM32_ALTERNATE_PUSHPULL); // sck
palSetPadMode(GPIOA, 6, PAL_MODE_INPUT); // miso
palSetPadMode(GPIOA, 7, PAL_MODE_STM32_ALTERNATE_PUSHPULL); // mosi
Expand All @@ -79,6 +95,11 @@ bool Pt2001::init() {
palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL); // chip select
palSetPad(GPIOB, 2);

#if (EFI_PT2001_CHIPS > 1)
palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL); // chip select for second chip
palSetPad(GPIOB, 3);
#endif

// Set debug pins remap mode to use PB4 as normal pin
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
palSetPadMode(GPIOB, 4, PAL_MODE_OUTPUT_PUSHPULL); // DRVEN
Expand All @@ -89,17 +110,24 @@ bool Pt2001::init() {

palSetPadMode(GPIOB, 7, PAL_MODE_INPUT_PULLDOWN); // flag0

driver = &SPID1;
spiStart(driver, &spiCfg);
spiUnselect(driver);
#if (EFI_PT2001_CHIPS > 1)
// release reset (if shared between chips)
chThdSleepMilliseconds(1);
palSetPad(GPIOB, 5);
#endif
}

// Wait 1/2 second for things to wake up
chThdSleepMilliseconds(500);
bool Pt2001::init(SPIDriver *spi, const SPIConfig *cfg) {
driver = spi;
spiCfg = cfg;

spiStart(driver, spiCfg);
spiUnselect(driver);

return restart();
}

Pt2001 chip;
Pt2001 chips[EFI_PT2001_CHIPS];

mfs_error_t flashState;

Expand Down Expand Up @@ -127,10 +155,19 @@ int main() {
palSetPadMode(LED_GREEN_PORT, LED_GREEN_PIN, PAL_MODE_OUTPUT_PUSHPULL);
palClearPad(LED_GREEN_PORT, LED_GREEN_PIN);

bool isOverallHappyStatus = false;
bool isOverallHappyStatus = true;

initPt2001Interface();
// Wait 1/2 second for things to wake up
chThdSleepMilliseconds(500);
#if (EFI_PT2001_CHIPS > 1)
// DRVEN = 1 (if shared between chips)
palSetPad(GPIOB, 4);
#endif
// reminder that +12v is required for PT2001 to start
isOverallHappyStatus = chip.init();
for (size_t i = 0; i < EFI_PT2001_CHIPS; i++) {
isOverallHappyStatus &= chips[i].init(&SPID1, &spiCfg[i]);
}

while (true) {
if (isOverallHappyStatus) {
Expand Down
Loading