-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathSTSPIN32G4.cpp
182 lines (107 loc) · 3.47 KB
/
STSPIN32G4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "./STSPIN32G4.h"
#ifdef ARDUINO_GENERIC_G431VBTX
STSPIN32G4::STSPIN32G4() : BLDCDriver6PWM(STSPIN32G4_PIN_INUH, STSPIN32G4_PIN_INUL, STSPIN32G4_PIN_INVH,
STSPIN32G4_PIN_INVL, STSPIN32G4_PIN_INWH, STSPIN32G4_PIN_INWL),
_wire(STSPIN32G4_PIN_SDA, STSPIN32G4_PIN_SCL) {
};
STSPIN32G4::~STSPIN32G4(){
_wire.end();
};
int STSPIN32G4::init(){
// init pins
pinMode(STSPIN32G4_PIN_WAKE, OUTPUT);
digitalWrite(STSPIN32G4_PIN_WAKE, LOW);
pinMode(STSPIN32G4_PIN_READY, INPUT_PULLUP);
pinMode(STSPIN32G4_PIN_FAULT, INPUT_PULLUP);
int result = this->BLDCDriver6PWM::init();
if(result == 0) return result;
setPwm(0,0,0); // set the phases to off
// init I2C
_wire.begin();
delayMicroseconds(50); // give driver a moment to wake up
clearFaults(); // clear the faults
// TODO init fault monitor
return isReady() ? 1 : 0;
};
void STSPIN32G4::wake() {
digitalWrite(STSPIN32G4_PIN_WAKE, HIGH);
delayMicroseconds(50); // 50ms high pulse to wake up
digitalWrite(STSPIN32G4_PIN_WAKE, LOW);
};
void STSPIN32G4::sleep() {
digitalWrite(STSPIN32G4_PIN_WAKE, LOW);
writeRegister(STSPIN32G4_REG_STBY, 0x01);
};
bool STSPIN32G4::isReady(){
return digitalRead(STSPIN32G4_PIN_READY)==HIGH;
};
bool STSPIN32G4::isFault(){
return digitalRead(STSPIN32G4_PIN_FAULT)==LOW;
};
STSPIN32G4Status STSPIN32G4::status(){
STSPIN32G4Status result;
result.reg = readRegister(STSPIN32G4_REG_STATUS);
return result;
};
void STSPIN32G4::lock(){
writeRegister(STSPIN32G4_REG_LOCK, 0x00);
};
void STSPIN32G4::unlock(){
writeRegister(STSPIN32G4_REG_LOCK, 0xF0);
};
STSPIN32G4NFault STSPIN32G4::getNFaultRegister(){
STSPIN32G4NFault result;
result.reg = readRegister(STSPIN32G4_REG_NFAULT);
return result;
};
STSPIN32G4Ready STSPIN32G4::getReadyRegister(){
STSPIN32G4Ready result;
result.reg = readRegister(STSPIN32G4_REG_READY);
return result;
};
STSPIN32G4Logic STSPIN32G4::getLogicRegister(){
STSPIN32G4Logic result;
result.reg = readRegister(STSPIN32G4_REG_LOGIC);
return result;
};
STSPIN32G4PowMng STSPIN32G4::getPowMngRegister(){
STSPIN32G4PowMng result;
result.reg = readRegister(STSPIN32G4_REG_POWMNG);
return result;
};
void STSPIN32G4::setNFaultRegister(STSPIN32G4NFault value){
writeRegister(STSPIN32G4_REG_NFAULT, value.reg);
};
void STSPIN32G4::setReadyRegister(STSPIN32G4Ready value){
writeRegister(STSPIN32G4_REG_READY, value.reg);
};
void STSPIN32G4::setLogicRegister(STSPIN32G4Logic value){
writeRegister(STSPIN32G4_REG_LOGIC, value.reg);
};
void STSPIN32G4::setPowMngRegister(STSPIN32G4PowMng value){
writeRegister(STSPIN32G4_REG_POWMNG, value.reg);
};
void STSPIN32G4::resetRegisters(){
// write 0xFF to reset register
writeRegister(STSPIN32G4_REG_RESET, 0xFF);
};
void STSPIN32G4::clearFaults(){
// write 0xFF to clear faults
writeRegister(STSPIN32G4_REG_CLEAR, 0xFF);
};
uint8_t STSPIN32G4::readRegister(uint8_t reg){
uint8_t result = 0;
_wire.beginTransmission(STSPIN32G4_I2C_ADDR);
_wire.write(reg);
_wire.endTransmission(false);
_wire.requestFrom(STSPIN32G4_I2C_ADDR, 1);
result = _wire.read();
return result;
};
void STSPIN32G4::writeRegister(uint8_t reg, uint8_t val){
_wire.beginTransmission(STSPIN32G4_I2C_ADDR);
_wire.write(reg);
_wire.write(val);
_wire.endTransmission();
};
#endif