Skip to content

Commit

Permalink
vsv ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Karanami committed Jun 30, 2024
1 parent f00c89b commit 005aa6c
Show file tree
Hide file tree
Showing 25 changed files with 653 additions and 826 deletions.
26 changes: 16 additions & 10 deletions firmware/Core/Inc/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
* Config.hpp
*
* Created on: 3 sie 2023
* Author: piotr
* Author: Piotr Lesicki
*/

#ifndef INC_INTERFACES_CONFIG_HPP_
#define INC_INTERFACES_CONFIG_HPP_

#include <main.h>
#include <array>

#include <Interfaces/MCP356xRegs.hpp>

namespace LtcConfig
{
static constexpr size_t CHAIN_SIZE = 15;
static constexpr size_t CHAIN_SIZE = 14;
static constexpr float UNDERVOLTAGE = 2.f;
static constexpr float OVERVOLTAGE = 4.f;

static constexpr size_t CELL_COUNT = CHAIN_SIZE * 9;
static constexpr size_t TEMP_COUNT = CHAIN_SIZE * 3;
//max 12;
static constexpr size_t CELLS_PER_LTC = 10;
//needs to be the size of CELLS_PER_LTC (max 12) and be indexed from 0-11
static constexpr std::array < size_t, CELLS_PER_LTC > CELL_TO_CH_MAP { 0, 1, 2, 3, 4, 6, 7, 8, 9, 10 };
static constexpr size_t CELL_COUNT = CHAIN_SIZE * CELLS_PER_LTC;
static constexpr size_t TEMP_COUNT = CHAIN_SIZE * 5;
}

namespace ChecksConfig
Expand All @@ -37,16 +42,17 @@ namespace ChecksConfig

namespace Mcp356xConfig
{
constexpr static double ADC_REF_P = 3.0;
constexpr static double ADC_REF_M = 0.0;
constexpr static double ADC_REF = ADC_REF_P - ADC_REF_M;
constexpr static float ADC_REF_P = 3.0;
constexpr static float ADC_REF_M = 0.0;
constexpr static float ADC_REF = ADC_REF_P - ADC_REF_M;
}

namespace ExternalConfig
{
constexpr static double ADC_VOLT_R = 2'000.0;
constexpr static double ADC_VOLT_COEF = (800'000.0 + ADC_VOLT_R) / ADC_VOLT_R;
constexpr static double ADC_CURR_COEF = 200.0 / 1.25;
constexpr static float ADC_VOLT_REF = 3.0;
constexpr static float ADC_VOLT_RES = 2'000.0;
constexpr static float ADC_VOLT_COEF = (800'000.0 + ADC_VOLT_RES) / ADC_VOLT_RES;
constexpr static float ADC_CURR_COEF = 200.0 / 1.25;
}

#endif /* INC_INTERFACES_CONFIG_HPP_ */
19 changes: 17 additions & 2 deletions firmware/Core/Inc/Interfaces/LTC6811Regs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace Ltc6811
{
//uint8_t bytes[2];
uint16_t val;
} cell[3];
} channel[3];
};
// generic reg gpio[0:2] OR gpio[3:4] + ref voltage rd
struct AuxilliaryVoltage : public IReadRegisterGroup
Expand Down Expand Up @@ -276,10 +276,25 @@ namespace Ltc6811
std::memcpy(destination, &source, 6);
}

// std::array < uint8_t, 6 > serializeRegisterGroup(RegisterGroup &source)
// {
// std::array < uint8_t, 6 > destination;
// std::memcpy(destination.data(), &source, 6);
// return destination;
// }

// template < ReadRegisterGroup RegisterGroup >
// void deserializeRegisterGroup(RegisterGroup &destination, uint8_t const *source)
// {
// std::memcpy(&destination, source, 6);
// }

template < ReadRegisterGroup RegisterGroup >
void deserializeRegisterGroup(RegisterGroup &destination, uint8_t const *source)
RegisterGroup deserializeRegisterGroup(uint8_t const *source)
{
static RegisterGroup destination;
std::memcpy(&destination, source, 6);
return destination;
}
}

Expand Down
28 changes: 14 additions & 14 deletions firmware/Core/Inc/PerypherialManagers/AIRdriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@
#include "AirSM.hpp"
#include "main.h"

class AIRdriver{

inline static GpioOut air_P { SIG_AIR_P_GPIO_Port, SIG_AIR_P_Pin, false };
inline static GpioOut air_M { SIG_AIR_M_GPIO_Port, SIG_AIR_M_Pin, false };
inline static GpioOut precharge { SIG_AIR_PRE_GPIO_Port, SIG_AIR_PRE_Pin, false };
class AIRdriver
{
inline static GpioOut air_p { SIG_AIR_P_GPIO_Port, SIG_AIR_P_Pin, false };
inline static GpioOut air_m { SIG_AIR_M_GPIO_Port, SIG_AIR_M_Pin, false };
inline static GpioOut air_pre { SIG_AIR_PRE_GPIO_Port, SIG_AIR_PRE_Pin, false };

public:
inline static void SetState(AIRstateEnum state){
switch (state) {
case AIRstateEnum::Open:
air_M.deactivate();
air_P.deactivate();
precharge.deactivate();
air_m.deactivate();
air_p.deactivate();
air_pre.deactivate();
break;

case AIRstateEnum::Precharge:
air_M.activate();
air_P.deactivate();
precharge.activate();
air_m.activate();
air_p.deactivate();
air_pre.activate();
break;

case AIRstateEnum::Closed:
air_M.activate();
air_P.activate();
precharge.deactivate();
air_m.activate();
air_p.activate();
air_pre.deactivate();
break;
}
}
Expand Down
41 changes: 20 additions & 21 deletions firmware/Core/Inc/PerypherialManagers/Ltc6811Controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <atomic>
#include <cmath>
#include <algorithm>
#include <optional>
#include <variant>

#include <PerypherialManagers/GpioController.hpp>
#include <Interfaces/LTC6811Cmd.hpp>
Expand All @@ -39,6 +41,13 @@ enum struct LtcCtrlStatus
RegValMismatchError = 0x03
};

enum struct LtcError
{
Error = 0x00,
PecError = 0x01,
RegValMismatchError = 0x02
};

enum struct LtcDiagnosisStatus
{
Passed,
Expand All @@ -55,32 +64,22 @@ class Ltc6811Controller
/*
* direct read
*/
//template < Ltc6811::ReadRegisterGroup RdReg >
//LtcCtrlStatus rawRead(Ltc6811::RCmd cmd, std::array < RdReg, LtcConfig::CHAIN_SIZE > &data);

template < Ltc6811::ReadRegisterGroup RdReg >
LtcCtrlStatus rawRead(Ltc6811::RCmd cmd, std::array < RdReg, LtcConfig::CHAIN_SIZE > &data, std::array < PecStatus, LtcConfig::CHAIN_SIZE > &pec_status);

std::array < std::variant < RdReg, LtcError >, LtcConfig::CHAIN_SIZE > rawRead(Ltc6811::RCmd cmd);
/*
* direct write overrides current mem
*/
template < Ltc6811::WriteReadRegisterGroup WrRdReg >
LtcCtrlStatus rawWrite(Ltc6811::WCmd cmd, std::array< WrRdReg, LtcConfig::CHAIN_SIZE > const &data);
LtcCtrlStatus rawWrite(Ltc6811::WCmd cmd);

LtcCtrlStatus configure();
LtcCtrlStatus readVoltages(std::array< std::array< float, 12 >, LtcConfig::CHAIN_SIZE > &vol);
LtcCtrlStatus diagnose(std::array < LtcDiagnosisStatus, LtcConfig::CHAIN_SIZE > &diag);
LtcCtrlStatus readGpioAndRef2(std::array< std::array< float, 6 >, LtcConfig::CHAIN_SIZE > &aux);
LtcCtrlStatus setDischarge(std::array< std::array< bool, 12 >, LtcConfig::CHAIN_SIZE > &dis);

//atomic versions
LtcCtrlStatus readVoltages(std::array< std::atomic<float>, LtcConfig::CELL_COUNT > &vol);
//LtcCtrlStatus diagnose(std::array < LtcDiagnosisStatus, Ltc::CHAIN_SIZE > &diag);
//LtcCtrlStatus readGpioAndRef2(std::array< std::array< float, 6 >, Ltc::CHAIN_SIZE > &aux);
LtcCtrlStatus setDischarge(const std::array< std::atomic<bool>, LtcConfig::CELL_COUNT > &dis);
LtcCtrlStatus readGpioTemp(std::array< std::atomic < float >, LtcConfig::TEMP_COUNT > &temp);
LtcCtrlStatus readStackVoltage(std::atomic<float> &stack_vol);
std::optional < LtcError > rawWrite(Ltc6811::WCmd cmd, std::array< WrRdReg, LtcConfig::CHAIN_SIZE > const &data);
std::optional < LtcError > rawWrite(Ltc6811::WCmd cmd);

//variant versions
std::optional < LtcError > configure();
std::array < std::variant < LtcError, float > , LtcConfig::CHAIN_SIZE * 12 > readVoltages();
std::optional < LtcError > setDischarge(const std::array< bool, LtcConfig::CHAIN_SIZE * 12 > &dis);
std::array < std::variant < LtcError, float > , LtcConfig::CHAIN_SIZE * 5 > readGpio();
std::variant < LtcError, float > readStackVoltage();

/*
* the ltc will timeout and will go into idle / sleep mode
* use every 2 sec in the case no valid command is scheduled
Expand Down
11 changes: 6 additions & 5 deletions firmware/Core/Inc/PerypherialManagers/Mcp356xController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ enum struct Mcp356xAddress : uint8_t
class Mcp356xController
{
public:
explicit Mcp356xController(GpioOut cs, SPI_HandleTypeDef &hspi, Mcp356xVersion version);
explicit Mcp356xController(GpioOut cs, SPI_HandleTypeDef &hspi);

constexpr static Mcp356xAddress address = Mcp356xAddress::_1;

Mcp356x::StatusByte status_byte;

const GpioOut cs;
GpioOut cs;

const SPI_HandleTypeDef &hspi;

Expand All @@ -57,16 +57,17 @@ class Mcp356xController
Mcp356x::StatusByte poolSatusByte();

void configure(Mcp356x::ConfigGroup config);
void init();

bool statusByteOk();
void setChannels(Mcp356x::MuxIn ch_p, Mcp356x::MuxIn ch_m);
void restartAdc();
bool dataReady();
int32_t readData();
double readVoltage();
float readVoltage();

double gain { 1.f };
double offset { 0.f };
float gain { 1.f };
float offset { 0.f };
protected:
private:
Mcp356x::DataFormat adc_variant;
Expand Down
22 changes: 11 additions & 11 deletions firmware/Core/Inc/StackData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ struct FullStackData

class FullStackDataInstance
{
public:
inline static FullStackData instance;
inline static FullStackData &set()
{
return instance;
}

inline static const FullStackData &get()
{
return set();
}
public:
inline static FullStackData instance;
inline static FullStackData &set()
{
return instance;
}

inline static const FullStackData &get()
{
return instance;
}
};

#endif /* STACKDATA_HPP_ */
8 changes: 4 additions & 4 deletions firmware/Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ void Error_Handler(void);
#define LTC_MISO_GPIO_Port GPIOB
#define LTC_MOSI_Pin GPIO_PIN_15
#define LTC_MOSI_GPIO_Port GPIOB
#define SIG_AIR_M_Pin GPIO_PIN_7
#define SIG_AIR_M_GPIO_Port GPIOC
#define SIG_AIR_PRE_Pin GPIO_PIN_7
#define SIG_AIR_PRE_GPIO_Port GPIOC
#define SIG_AIR_P_Pin GPIO_PIN_8
#define SIG_AIR_P_GPIO_Port GPIOC
#define SIG_AIR_PRE_Pin GPIO_PIN_9
#define SIG_AIR_PRE_GPIO_Port GPIOC
#define SIG_AIR_M_Pin GPIO_PIN_9
#define SIG_AIR_M_GPIO_Port GPIOC
#define NMES_ISENS_CS_Pin GPIO_PIN_8
#define NMES_ISENS_CS_GPIO_Port GPIOA
#define NMES_EXT_V_CS_Pin GPIO_PIN_9
Expand Down
Loading

0 comments on commit 005aa6c

Please sign in to comment.