4
4
#include < cstdint>
5
5
#include " SPI.h"
6
6
#include < mcp_can.h>
7
+ #include < mutex>
7
8
8
9
#ifndef HUAWEI_PIN_MISO
9
10
#define HUAWEI_PIN_MISO 12
34
35
35
36
#define MAX_CURRENT_MULTIPLIER 20
36
37
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
37
53
#define HUAWEI_OFFLINE_VOLTAGE 0x01
38
54
#define HUAWEI_ONLINE_VOLTAGE 0x00
39
55
#define HUAWEI_OFFLINE_CURRENT 0x04
40
56
#define HUAWEI_ONLINE_CURRENT 0x03
41
57
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
55
59
#define HUAWEI_MODE_OFF 0
56
60
#define HUAWEI_MODE_ON 1
57
61
#define HUAWEI_MODE_AUTO_EXT 2
58
62
#define HUAWEI_MODE_AUTO_INT 3
59
63
64
+ // Error codes
65
+ #define HUAWEI_ERROR_CODE_RX 0x01
66
+ #define HUAWEI_ERROR_CODE_TX 0x02
67
+
60
68
// Wait time/current before shuting down the PSU / charger
61
69
// This is set to allow the fan to run for some time
62
70
#define HUAWEI_AUTO_MODE_SHUTDOWN_DELAY 60000
63
71
#define HUAWEI_AUTO_MODE_SHUTDOWN_CURRENT 1.0
64
72
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 {
66
77
float input_voltage;
67
78
float input_frequency;
68
79
float input_current;
@@ -75,6 +86,33 @@ struct RectifierParameters_t {
75
86
float output_power;
76
87
float output_temp;
77
88
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;
78
116
};
79
117
80
118
class HuaweiCanClass {
@@ -89,26 +127,24 @@ class HuaweiCanClass {
89
127
bool getAutoPowerStatus ();
90
128
91
129
private:
92
- void sendRequest ();
93
- void onReceive (uint8_t * frame, uint8_t len);
130
+ void processReceivedParameters ();
94
131
95
- SPIClass *spi;
96
- MCP_CAN *CAN;
132
+ TaskHandle_t _HuaweiCanCommunicationTaskHdl = NULL ;
97
133
bool _initialized = false ;
98
- uint8_t _huawei_irq; // IRQ pin
99
- uint8_t _huawei_power; // Power pin
134
+ uint8_t _huaweiPower; // Power pin
100
135
uint8_t _mode = HUAWEI_MODE_AUTO_EXT;
101
136
102
137
RectifierParameters_t _rp;
103
138
104
139
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
108
140
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 ;
112
147
};
113
148
114
149
extern HuaweiCanClass HuaweiCan;
150
+ extern HuaweiCanCommClass HuaweiCanComm;
0 commit comments