Skip to content

Commit 4324ae3

Browse files
committed
Merge branch 'development'
2 parents f0a55ea + f7abbdb commit 4324ae3

File tree

4 files changed

+355
-202
lines changed

4 files changed

+355
-202
lines changed

include/Huawei_can.h

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstdint>
55
#include "SPI.h"
66
#include <mcp_can.h>
7+
#include <mutex>
78

89
#ifndef HUAWEI_PIN_MISO
910
#define HUAWEI_PIN_MISO 12
@@ -34,35 +35,45 @@
3435

3536
#define MAX_CURRENT_MULTIPLIER 20
3637

38+
// Index values for rec_values array
39+
#define HUAWEI_INPUT_POWER_IDX 0
40+
#define HUAWEI_INPUT_FREQ_IDX 1
41+
#define HUAWEI_INPUT_CURRENT_IDX 2
42+
#define HUAWEI_OUTPUT_POWER_IDX 3
43+
#define HUAWEI_EFFICIENCY_IDX 4
44+
#define HUAWEI_OUTPUT_VOLTAGE_IDX 5
45+
#define HUAWEI_OUTPUT_CURRENT_MAX_IDX 6
46+
#define HUAWEI_INPUT_VOLTAGE_IDX 7
47+
#define HUAWEI_OUTPUT_TEMPERATURE_IDX 8
48+
#define HUAWEI_INPUT_TEMPERATURE_IDX 9
49+
#define HUAWEI_OUTPUT_CURRENT_IDX 10
50+
#define HUAWEI_OUTPUT_CURRENT1_IDX 11
51+
52+
// Defines and index values for tx_values array
3753
#define HUAWEI_OFFLINE_VOLTAGE 0x01
3854
#define HUAWEI_ONLINE_VOLTAGE 0x00
3955
#define HUAWEI_OFFLINE_CURRENT 0x04
4056
#define HUAWEI_ONLINE_CURRENT 0x03
4157

42-
#define R48xx_DATA_INPUT_POWER 0x70
43-
#define R48xx_DATA_INPUT_FREQ 0x71
44-
#define R48xx_DATA_INPUT_CURRENT 0x72
45-
#define R48xx_DATA_OUTPUT_POWER 0x73
46-
#define R48xx_DATA_EFFICIENCY 0x74
47-
#define R48xx_DATA_OUTPUT_VOLTAGE 0x75
48-
#define R48xx_DATA_OUTPUT_CURRENT_MAX 0x76
49-
#define R48xx_DATA_INPUT_VOLTAGE 0x78
50-
#define R48xx_DATA_OUTPUT_TEMPERATURE 0x7F
51-
#define R48xx_DATA_INPUT_TEMPERATURE 0x80
52-
#define R48xx_DATA_OUTPUT_CURRENT 0x81
53-
#define R48xx_DATA_OUTPUT_CURRENT1 0x82
54-
58+
// Modes of operation
5559
#define HUAWEI_MODE_OFF 0
5660
#define HUAWEI_MODE_ON 1
5761
#define HUAWEI_MODE_AUTO_EXT 2
5862
#define HUAWEI_MODE_AUTO_INT 3
5963

64+
// Error codes
65+
#define HUAWEI_ERROR_CODE_RX 0x01
66+
#define HUAWEI_ERROR_CODE_TX 0x02
67+
6068
// Wait time/current before shuting down the PSU / charger
6169
// This is set to allow the fan to run for some time
6270
#define HUAWEI_AUTO_MODE_SHUTDOWN_DELAY 60000
6371
#define HUAWEI_AUTO_MODE_SHUTDOWN_CURRENT 1.0
6472

65-
struct RectifierParameters_t {
73+
// Updateinterval used to request new values from the PSU
74+
#define HUAWEI_DATA_REQUEST_INTERVAL_MS 2500
75+
76+
typedef struct RectifierParameters {
6677
float input_voltage;
6778
float input_frequency;
6879
float input_current;
@@ -75,6 +86,33 @@ struct RectifierParameters_t {
7586
float output_power;
7687
float output_temp;
7788
float amp_hour;
89+
} RectifierParameters_t;
90+
91+
class HuaweiCanCommClass {
92+
public:
93+
bool init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t huawei_clk, uint8_t huawei_irq, uint8_t huawei_cs);
94+
void loop();
95+
bool gotNewRxDataFrame(bool clear);
96+
uint8_t getErrorCode(bool clear);
97+
uint32_t getParameterValue(uint8_t parameter);
98+
void setParameterValue(uint16_t in, uint8_t parameterType);
99+
100+
private:
101+
void sendRequest();
102+
103+
SPIClass *SPI;
104+
MCP_CAN *_CAN;
105+
uint8_t _huaweiIrq; // IRQ pin
106+
uint32_t _nextRequestMillis = 0; // When to send next data request to PSU
107+
108+
std::mutex _mutex;
109+
110+
uint32_t _recValues[12];
111+
uint16_t _txValues[5];
112+
bool _hasNewTxValue[5];
113+
114+
uint8_t _errorCode;
115+
bool _completeUpdateReceived;
78116
};
79117

80118
class HuaweiCanClass {
@@ -89,26 +127,24 @@ class HuaweiCanClass {
89127
bool getAutoPowerStatus();
90128

91129
private:
92-
void sendRequest();
93-
void onReceive(uint8_t* frame, uint8_t len);
130+
void processReceivedParameters();
94131

95-
SPIClass *spi;
96-
MCP_CAN *CAN;
132+
TaskHandle_t _HuaweiCanCommunicationTaskHdl = NULL;
97133
bool _initialized = false;
98-
uint8_t _huawei_irq; // IRQ pin
99-
uint8_t _huawei_power; // Power pin
134+
uint8_t _huaweiPower; // Power pin
100135
uint8_t _mode = HUAWEI_MODE_AUTO_EXT;
101136

102137
RectifierParameters_t _rp;
103138

104139
uint32_t _lastUpdateReceivedMillis; // Timestamp for last data seen from the PSU
105-
uint32_t _nextRequestMillis = 0; // When to send next data request to PSU
106-
uint32_t _nextAutoModePeriodicIntMillis; // When to send the next output volume request in Automatic mode
107-
uint32_t _lastPowerMeterUpdateReceivedMillis; // Timestamp of last power meter value
108140
uint32_t _outputCurrentOnSinceMillis; // Timestamp since when the PSU was idle at zero amps
109-
bool _newOutputPowerReceived = false;
110-
uint8_t _autoPowerEnabled = false;
111-
bool _autoPowerActive = false;
141+
uint32_t _nextAutoModePeriodicIntMillis; // When to set the next output voltage in automatic mode
142+
uint32_t _lastPowerMeterUpdateReceivedMillis; // Timestamp of last seen power meter value
143+
uint32_t _autoModeBlockedTillMillis = 0; // Timestamp to block running auto mode for some time
144+
145+
uint8_t _autoPowerEnabledCounter = 0;
146+
bool _autoPowerEnabled = false;
112147
};
113148

114149
extern HuaweiCanClass HuaweiCan;
150+
extern HuaweiCanCommClass HuaweiCanComm;

0 commit comments

Comments
 (0)