Skip to content

Commit

Permalink
Moved all MG specific stuff to GD3100 module
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphuebner committed Dec 18, 2024
1 parent f2ee9d9 commit 9a2ce33
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 164 deletions.
31 changes: 14 additions & 17 deletions include/GD31xxOI.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,22 @@
*/

#include "stddef.h"
#include "stdbool.h"
#include <stdint.h>
#include "delay.h"
#include "digio.h"

#define REQ_ADC_EXT 0x4001
#define REQ_STAT1 0x2800
#define REQ_STAT2 0x3000
#define REQ_STAT3 0x3800
class MGSPI {

/// SPI channels
enum SpiChannelMG
{
MGSPI1_H = 1,
MGSPI2_H = 2,
MGSPI3_H = 3,
MGSPI1_L = 4,
MGSPI2_L = 5,
MGSPI3_L = 6,
};
public:
static void Initialize();
static void CyclicFunction();
static uint16_t GetRawTemperature(int index) { return temps[index]; }
static uint16_t GetUdc() { return udc; }

private:
static uint16_t temps[3];
static uint16_t udc;

extern uint32_t Send16 (enum SpiChannelMG channel,uint16_t txData16);
extern uint32_t Config_MG (enum SpiChannelMG channel);
static uint32_t Send16(DigIo& cspin,uint16_t txData16);
static uint32_t ConfigureGateDriver(DigIo& cspin);
};
59 changes: 58 additions & 1 deletion include/digio_prj.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,65 @@

#include "hwdefs.h"

//Since there are various HW variants we first declare all possible pins...

#define DIG_IO_LIST \
DIG_IO_ENTRY(cruise_in, GPIOB, GPIO2, PinMode::INPUT_PD) \
DIG_IO_ENTRY(cruise_in,,,) \
DIG_IO_ENTRY(start_in,,,) \
DIG_IO_ENTRY(brake_in,,,) \
DIG_IO_ENTRY(mprot_in,,,) \
DIG_IO_ENTRY(fwd_in,,,) \
DIG_IO_ENTRY(rev_in,,,) \
DIG_IO_ENTRY(emcystop_in,,,) \
DIG_IO_ENTRY(bk_in,,,) \
DIG_IO_ENTRY(bms_in,,,) \
DIG_IO_ENTRY(ocur_in,,,) \
DIG_IO_ENTRY(desat_in,,,) \
DIG_IO_ENTRY(dcsw_out,,,) \
DIG_IO_ENTRY(fan_out,,,) \
DIG_IO_ENTRY(vtg_out,,,) \
DIG_IO_ENTRY(prec_out,,,) \
DIG_IO_ENTRY(led_out,,,) \
DIG_IO_ENTRY(err_out,,,) \
DIG_IO_ENTRY(temp0_out,,,) \
DIG_IO_ENTRY(speed_out,,,) \
DIG_IO_ENTRY(brk_out,,,) \
DIG_IO_ENTRY(cs1_hi,,,) \
DIG_IO_ENTRY(cs2_hi,,,) \
DIG_IO_ENTRY(cs3_hi,,,) \
DIG_IO_ENTRY(cs1_lo,,,) \
DIG_IO_ENTRY(cs2_lo,,,) \
DIG_IO_ENTRY(cs3_lo,,,) \
DIG_IO_ENTRY(v5_ctrl,,,) \
DIG_IO_ENTRY(intb_in,,,) \
DIG_IO_ENTRY(inta_in,,,) \

//...Then we assign the physical GPIOs per variant

#define DIG_IO_LIST_STD \
DIG_IO_ENTRY(cruise_in, GPIOB, GPIO5, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(start_in, GPIOB, GPIO6, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(brake_in, GPIOA, GPIO2, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(mprot_in, GPIOA, GPIO3, PinMode::INPUT_PU) \
DIG_IO_ENTRY(fwd_in, GPIOA, GPIO4, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(rev_in, GPIOC, GPIO6, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(emcystop_in, GPIOC, GPIO7, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(bk_in, GPIOB, GPIO12, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(bms_in, GPIOC, GPIO8, PinMode::INPUT_PD) \
DIG_IO_ENTRY(ocur_in, GPIOA, GPIO1, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(desat_in, GPIOC, GPIO9, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(dcsw_out, GPIOC, GPIO13, PinMode::OUTPUT) \
DIG_IO_ENTRY(fan_out, GPIOA, GPIO0, PinMode::OUTPUT) /* map to unused pin by default */ \
DIG_IO_ENTRY(vtg_out, GPIOC, GPIO11, PinMode::OUTPUT) \
DIG_IO_ENTRY(prec_out, GPIOB, GPIO1, PinMode::OUTPUT) \
DIG_IO_ENTRY(led_out, GPIOC, GPIO12, PinMode::OUTPUT) \
DIG_IO_ENTRY(err_out, GPIOC, GPIO10, PinMode::OUTPUT) \
DIG_IO_ENTRY(temp0_out, GPIOC, GPIO10, PinMode::OUTPUT) \
DIG_IO_ENTRY(speed_out, GPIOB, GPIO9, PinMode::OUTPUT) \
DIG_IO_ENTRY(brk_out, GPIOC, GPIO5, PinMode::OUTPUT) \

#define DIG_IO_LIST_MG \
DIG_IO_ENTRY(cruise_in, GPIOB, GPIO2, PinMode::INPUT_PD) \
DIG_IO_ENTRY(start_in, GPIOD, GPIO7, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(brake_in, GPIOE, GPIO4, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(mprot_in, GPIOE, GPIO5, PinMode::INPUT_PU) \
Expand Down
2 changes: 1 addition & 1 deletion include/hwdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

typedef enum
{
HW_REV1, HW_REV2, HW_REV3, HW_TESLA, HW_BLUEPILL, HW_PRIUS, HW_MINI, HW_LEAF2, HW_LEAF3, HW_BMWI3
HW_REV1, HW_REV2, HW_REV3, HW_TESLA, HW_BLUEPILL, HW_PRIUS, HW_MINI, HW_LEAF2, HW_LEAF3, HW_BMWI3, HW_MG
} HWREV;

extern HWREV hwRev;
Expand Down
7 changes: 0 additions & 7 deletions include/param_prj.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@
VALUE_ENTRY(opmode, OPMODES, 2000 ) \
VALUE_ENTRY(lasterr, errorListString, 2038 ) \
VALUE_ENTRY(status, STATUS, 2044 ) \
VALUE_ENTRY(status_GD3100, STATUS3100, 2063 ) \
VALUE_ENTRY(udc, "V", 2001 ) \
VALUE_ENTRY(idc, "A", 2002 ) \
VALUE_ENTRY(il1, "A", 2003 ) \
Expand Down Expand Up @@ -202,12 +201,6 @@
VALUE_ENTRY(din_bms, ONOFF, 2032 ) \
VALUE_ENTRY(uptime, "10ms", 2054 ) \
VALUE_ENTRY(cpuload, "%", 2035 ) \
VALUE_ENTRY(MG_Rx0, "dig", 2056 ) \
VALUE_ENTRY(MG_Rx1, "dig", 2057 ) \
VALUE_ENTRY(MG_Rx2, "dig", 2058 ) \
VALUE_ENTRY(MG_Rx3, "dig", 2059 ) \
VALUE_ENTRY(INT_L, ONOFF, 2064 ) \
VALUE_ENTRY(INT_H, ONOFF, 2065 ) \

#define VALUES_SINE \
VALUE_ENTRY(ilmax, "A", 2005 ) \
Expand Down
105 changes: 57 additions & 48 deletions src/GD31xxOI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
*/

#include "GD31xxOI.h"
#include "digio.h"
#include "hwinit.h"
#include <libopencm3/stm32/spi.h>

#define REQ_ADC_EXT 0x4001
#define REQ_STAT1 0x2800
#define REQ_STAT2 0x3000
#define REQ_STAT3 0x3800

// CRC table for polynomial 0x2F.
static const uint8_t MGcrc_table[256] = {
Expand Down Expand Up @@ -52,6 +56,51 @@ static const uint16_t MG_Gate_Config[24] = {
0X82B3, 0XA05A, 0XA470, 0X88DB, 0X8CD4, 0X9024, 0X964B, 0X9906, 0X9C3F, 0XAC00, 0XB400, 0X8440
};

uint16_t MGSPI::temps[3];
uint16_t MGSPI::udc;

void MGSPI::Initialize() {
DigIo::v5_ctrl.Set(); //Turn on MG gate driver logic side 5v power after 2 seconds
DigIo::cs1_hi.Set(); //Disable all SPI CS lines on MG
DigIo::cs2_hi.Set();
DigIo::cs3_hi.Set();
DigIo::cs1_lo.Set();
DigIo::cs2_lo.Set();
DigIo::cs3_lo.Set();

tim5_setup();//Enable gate drive psu on MG board
spi_setup();//MG gate drivers
uDelay(20);

ConfigureGateDriver(DigIo::cs1_hi); //configure all 6 MG gate drivers
ConfigureGateDriver(DigIo::cs1_hi); //must be sent twice to 1H as its a bit dumb
ConfigureGateDriver(DigIo::cs2_hi);
ConfigureGateDriver(DigIo::cs3_hi);
ConfigureGateDriver(DigIo::cs1_lo);
ConfigureGateDriver(DigIo::cs2_lo);
ConfigureGateDriver(DigIo::cs2_lo);
}

void MGSPI::CyclicFunction() {
static uint8_t MG_Cycler = 0;

switch (MG_Cycler){
case 0:
temps[0] = Send16(DigIo::cs1_hi, REQ_ADC_EXT);//HS Temp 1
break;
case 1:
temps[1] = Send16(DigIo::cs2_hi, REQ_ADC_EXT);//HS Temp 1
break;
case 2:
temps[2] = Send16(DigIo::cs3_hi, REQ_ADC_EXT);//HS Temp 1
break;
case 3:
udc = Send16(DigIo::cs3_lo, REQ_ADC_EXT);//HS Temp 1
break;
}

MG_Cycler = (MG_Cycler + 1) & 0x7; //count from 0-7
}

//////////////////////////////////////////////////////////////////////////////////////////
// Function name: Calculate_MGSPI_CRC
Expand Down Expand Up @@ -93,63 +142,23 @@ static uint8_t Calculate_SPI_CRC(uint16_t inData) {
//
/// @return uint32_t - Returns 16 bit answer from device
//////////////////////////////////////////////////////////////////////////////////////////
uint32_t Send16 (enum SpiChannelMG channel,uint16_t txData16) {
uint32_t MGSPI::Send16 (DigIo& cspin, uint16_t txData16) {
uint8_t i=0;
uint8_t rxData8[3];
uint16_t outData=0;
uint8_t txData8[3];
// Set chip select low
switch(channel) {
case 1:
DigIo::cs1_hi.Clear();
break;
case 2:
DigIo::cs2_hi.Clear();
break;
case 3:
DigIo::cs3_hi.Clear();
break;
case 4:
DigIo::cs1_lo.Clear();
break;
case 5:
DigIo::cs2_lo.Clear();
break;
case 6:
DigIo::cs3_lo.Clear();
break;
default:
break;
}
cspin.Clear();

txData8[0]=txData16>>8;
txData8[1]=txData16 & 0xFF;
txData8[2]=Calculate_SPI_CRC(txData16);

for(i=0; i<3; i++) {
rxData8[i]=spi_xfer(SPI1,txData8[i]);
}

switch(channel) {
case 1:
DigIo::cs1_hi.Set();
break;
case 2:
DigIo::cs2_hi.Set();
break;
case 3:
DigIo::cs3_hi.Set();
break;
case 4:
DigIo::cs1_lo.Set();
break;
case 5:
DigIo::cs2_lo.Set();
break;
case 6:
DigIo::cs3_lo.Set();
break;
default:
break;
}
cspin.Set();
outData=(rxData8[1] | rxData8[0]<<8) & 0x3FF;
return outData;
}
Expand All @@ -163,11 +172,11 @@ uint32_t Send16 (enum SpiChannelMG channel,uint16_t txData16) {
//
/// @return uint32_t - Returns 16 bit answer from device
//////////////////////////////////////////////////////////////////////////////////////////
uint32_t Config_MG (enum SpiChannelMG channel) {
uint32_t MGSPI::ConfigureGateDriver(DigIo& cspin) {
uint32_t dummyData;
for(int i=0; i<=23; i++) {
uDelay(20);// need to allow 20us per command as per datasheet
dummyData=Send16(channel,MG_Gate_Config[i]);
dummyData=Send16(cspin, MG_Gate_Config[i]);
}

return dummyData;
Expand Down
9 changes: 6 additions & 3 deletions src/hwinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ static HWREV ReadVariantResistor()
//See here for variants: https://openinverter.org/wiki/Mini_Mainboard#Hardware_detection
if ((result1 + result2) < 30) return HW_BMWI3; //connected to MISO so always low
else if (result2 > 3700) return HW_MINI; //might have to compare this against result1 later
else if (result1 > 327 && result1 < 347) return HW_MG;
else if (result1 > 510 && result1 < 630) return HW_LEAF3;
else return HW_MINI;
}

HWREV detect_hw()
{
return HW_REV3;//bodge for test
//Check if PB3 and PC10 are connected (mini mainboard)
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO10);
gpio_set(GPIOC, GPIO10);
Expand Down Expand Up @@ -190,7 +190,7 @@ HWREV io_setup()
hwRev = detect_hw();

ANA_IN_CONFIGURE(ANA_IN_LIST);
DIG_IO_CONFIGURE(DIG_IO_LIST);
DIG_IO_CONFIGURE(DIG_IO_LIST_STD);

switch (hwRev)
{
Expand All @@ -205,6 +205,9 @@ HWREV io_setup()
//Essentially disable error output by mapping it to an unused pin
DigIo::err_out.Configure(GPIOB, GPIO9, PinMode::INPUT_FLT);
break;
case HW_MG:
DIG_IO_CONFIGURE(DIG_IO_LIST_MG);
break;
case HW_BLUEPILL:
ANA_IN_CONFIGURE(ANA_IN_LIST_BLUEPILL);
DIG_IO_CONFIGURE(DIG_IO_BLUEPILL);
Expand Down Expand Up @@ -402,6 +405,6 @@ void tim5_setup()//Used on VCT6 for MG gate drive power supply
timer_set_period(TIM5, 270);//frequency 133khz
timer_set_oc_value(TIM5, TIM_OC1,125);//125=46% high
timer_set_oc_value(TIM5, TIM_OC2,150);//140=44% high
//timer_enable_counter(TIM5);
timer_enable_counter(TIM5);
}

Loading

0 comments on commit 9a2ce33

Please sign in to comment.