Skip to content

Commit 260580f

Browse files
author
Richard Unger
committed
STSPIN32G4 driver
1 parent ed75944 commit 260580f

File tree

3 files changed

+366
-0
lines changed

3 files changed

+366
-0
lines changed

src/drivers/stspin32g4/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
# SimpleFOC STSPIN32G4 Driver
3+
4+
This driver initializes the PWM stage of the STSPIN32G4, and provides access to its configuration via I2C.
5+
6+
:warning: in development!
7+
8+
## Setup
9+
10+
Since there are currently no standard boards for Arduino based on the STSPIN32G4 you will need a custom board definition, associated linker script and project setup to compile for your board. These topics are out of scope for this driver, but you can find a working example for the [FunQi STSPIN32G4 board](TODO link) [here](TODO link);
11+
12+
Once you can compile for your board, and flash it with a "blinky" test sketch, then you're ready to try SimpleFOC and more complex code.
13+
14+
## Usage
15+
16+
Basic usage, as you can see it is very simple. Since the pins are all pre-defined due to internal connections, setup
17+
is easier than with the standard drivers. Here is an example for open loop mode:
18+
19+
```c++
20+
#include <Arduino.h>
21+
#include "SimpleFOC.h"
22+
#include "SimpleFOCDrivers.h"
23+
#include "drivers/stspin32g4/STSPIN32G4.h"
24+
25+
26+
STSPIN32G4 driver = STSPIN32G4();
27+
BLDCMotor motor = BLDCMotor(7);
28+
29+
void setup() {
30+
driver.voltage_power_supply = 12.0f;
31+
driver.init();
32+
motor.voltage_limit = driver.voltage_limit / 2.0f;
33+
motor.controller = MotionControlType::velocity_openloop;
34+
motor.linkDriver(&driver);
35+
motor.init();
36+
}
37+
38+
void loop(){
39+
motor.move(5.0f); // 5 rad/s open loop
40+
delayMicroseconds(100); // STM32G4 is very fast, add a delay in open loop if we do nothing else
41+
}
42+
```
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
2+
#include "./STSPIN32G4.h"
3+
4+
#ifdef _STM32_DEF_
5+
6+
7+
STSPIN32G4::STSPIN32G4() : BLDCDriver6PWM(STSPIN32G4_PIN_INUH, STSPIN32G4_PIN_INUL, STSPIN32G4_PIN_INVH,
8+
STSPIN32G4_PIN_INVL, STSPIN32G4_PIN_INWH, STSPIN32G4_PIN_INWL),
9+
_wire(STSPIN32G4_PIN_SDA, STSPIN32G4_PIN_SCL) {
10+
11+
};
12+
13+
14+
15+
STSPIN32G4::~STSPIN32G4(){
16+
_wire.end();
17+
};
18+
19+
20+
21+
int STSPIN32G4::init(){
22+
// init pins
23+
pinMode(STSPIN32G4_PIN_WAKE, OUTPUT);
24+
pinMode(STSPIN32G4_PIN_READY, INPUT_PULLUP);
25+
pinMode(STSPIN32G4_PIN_FAULT, INPUT_PULLUP);
26+
27+
// wake up
28+
digitalWrite(STSPIN32G4_PIN_WAKE, HIGH);
29+
30+
// init I2C
31+
_wire.begin();
32+
33+
// init PWM
34+
return BLDCDriver6PWM::init();
35+
36+
// TODO init fault monitor
37+
};
38+
39+
40+
41+
void STSPIN32G4::wake() {
42+
digitalWrite(STSPIN32G4_PIN_WAKE, HIGH);
43+
};
44+
45+
46+
47+
void STSPIN32G4::sleep() {
48+
digitalWrite(STSPIN32G4_PIN_WAKE, LOW);
49+
writeRegister(STSPIN32G4_REG_STBY, 0x01);
50+
};
51+
52+
53+
54+
bool STSPIN32G4::isReady(){
55+
return digitalRead(STSPIN32G4_PIN_READY)==HIGH;
56+
};
57+
58+
59+
60+
bool STSPIN32G4::isFault(){
61+
return digitalRead(STSPIN32G4_PIN_FAULT)==LOW;
62+
};
63+
64+
65+
66+
STSPIN32G4Status STSPIN32G4::status(){
67+
STSPIN32G4Status result;
68+
result.reg = readRegister(STSPIN32G4_REG_STATUS);
69+
return result;
70+
};
71+
72+
73+
74+
void STSPIN32G4::lock(){
75+
writeRegister(STSPIN32G4_REG_LOCK, 0x00);
76+
};
77+
78+
79+
80+
void STSPIN32G4::unlock(){
81+
writeRegister(STSPIN32G4_REG_LOCK, 0xF0);
82+
};
83+
84+
85+
86+
STSPIN32G4NFault STSPIN32G4::getNFaultRegister(){
87+
STSPIN32G4NFault result;
88+
result.reg = readRegister(STSPIN32G4_REG_NFAULT);
89+
return result;
90+
};
91+
92+
93+
94+
STSPIN32G4Ready STSPIN32G4::getReadyRegister(){
95+
STSPIN32G4Ready result;
96+
result.reg = readRegister(STSPIN32G4_REG_READY);
97+
return result;
98+
};
99+
100+
101+
102+
STSPIN32G4Logic STSPIN32G4::getLogicRegister(){
103+
STSPIN32G4Logic result;
104+
result.reg = readRegister(STSPIN32G4_REG_LOGIC);
105+
return result;
106+
};
107+
108+
109+
110+
STSPIN32G4PowMng STSPIN32G4::getPowMngRegister(){
111+
STSPIN32G4PowMng result;
112+
result.reg = readRegister(STSPIN32G4_REG_POWMNG);
113+
return result;
114+
};
115+
116+
117+
118+
void STSPIN32G4::setNFaultRegister(STSPIN32G4NFault value){
119+
writeRegister(STSPIN32G4_REG_NFAULT, value.reg);
120+
};
121+
122+
123+
124+
void STSPIN32G4::setReadyRegister(STSPIN32G4Ready value){
125+
writeRegister(STSPIN32G4_REG_READY, value.reg);
126+
};
127+
128+
129+
130+
void STSPIN32G4::setLogicRegister(STSPIN32G4Logic value){
131+
writeRegister(STSPIN32G4_REG_LOGIC, value.reg);
132+
};
133+
134+
135+
136+
void STSPIN32G4::setPowMngRegister(STSPIN32G4PowMng value){
137+
writeRegister(STSPIN32G4_REG_POWMNG, value.reg);
138+
};
139+
140+
141+
142+
void STSPIN32G4::resetRegisters(){
143+
// write 0xFF to reset register
144+
writeRegister(STSPIN32G4_REG_RESET, 0xFF);
145+
};
146+
147+
148+
149+
void STSPIN32G4::clearFaults(){
150+
// write 0xFF to clear faults
151+
writeRegister(STSPIN32G4_REG_CLEAR, 0xFF);
152+
};
153+
154+
155+
156+
uint8_t STSPIN32G4::readRegister(uint8_t reg){
157+
uint8_t result = 0;
158+
_wire.beginTransmission(STSPIN32G4_I2C_ADDR);
159+
_wire.write(reg);
160+
_wire.endTransmission(false);
161+
_wire.requestFrom(STSPIN32G4_I2C_ADDR, 1);
162+
result = _wire.read();
163+
return result;
164+
};
165+
166+
167+
168+
void STSPIN32G4::writeRegister(uint8_t reg, uint8_t val){
169+
_wire.beginTransmission(STSPIN32G4_I2C_ADDR);
170+
_wire.write(reg);
171+
_wire.write(val);
172+
_wire.endTransmission();
173+
};
174+
175+
176+
#endif
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
#pragma once
3+
4+
#include "Arduino.h"
5+
6+
#ifdef _STM32_DEF_
7+
8+
#include "Wire.h"
9+
#include "drivers/BLDCDriver6PWM.h"
10+
11+
12+
#define STSPIN32G4_PIN_INUL PE8
13+
#define STSPIN32G4_PIN_INUH PE9
14+
#define STSPIN32G4_PIN_INVL PE10
15+
#define STSPIN32G4_PIN_INVH PE11
16+
#define STSPIN32G4_PIN_INWL PE12
17+
#define STSPIN32G4_PIN_INWH PE13
18+
19+
#define STSPIN32G4_PIN_WAKE PE7
20+
#define STSPIN32G4_PIN_READY PE14
21+
#define STSPIN32G4_PIN_FAULT PE15
22+
23+
#define STSPIN32G4_PIN_SDA PC9
24+
#define STSPIN32G4_PIN_SCL PC8
25+
26+
#define STSPIN32G4_I2C_ADDR 0b1000111
27+
28+
#define STSPIN32G4_REG_POWMNG 0x01
29+
#define STSPIN32G4_REG_LOGIC 0x02
30+
#define STSPIN32G4_REG_READY 0x07
31+
#define STSPIN32G4_REG_NFAULT 0x08
32+
#define STSPIN32G4_REG_CLEAR 0x19
33+
#define STSPIN32G4_REG_STBY 0x0A
34+
#define STSPIN32G4_REG_LOCK 0x0B
35+
#define STSPIN32G4_REG_RESET 0x0C
36+
#define STSPIN32G4_REG_STATUS 0x80
37+
38+
39+
40+
41+
42+
union STSPIN32G4Status {
43+
struct {
44+
uint8_t vcc_uvlo:1;
45+
uint8_t thsd:1;
46+
uint8_t vds_p:1;
47+
uint8_t reset:1;
48+
uint8_t unused:3;
49+
uint8_t lock:1;
50+
};
51+
uint8_t reg;
52+
};
53+
54+
55+
56+
union STSPIN32G4NFault {
57+
struct {
58+
uint8_t vcc_uvlo_flt:1;
59+
uint8_t thsd_flt:1;
60+
uint8_t vds_p_flt:1;
61+
uint8_t unused:5;
62+
};
63+
uint8_t reg;
64+
};
65+
66+
67+
union STSPIN32G4Ready {
68+
struct {
69+
uint8_t vcc_uvlo_rdy:1;
70+
uint8_t thsd_rdy:1;
71+
uint8_t unused:1;
72+
uint8_t stby_rdy:1;
73+
uint8_t unused2:4;
74+
};
75+
uint8_t reg;
76+
};
77+
78+
79+
80+
union STSPIN32G4Logic {
81+
struct {
82+
uint8_t ilock:1;
83+
uint8_t dtmin:1;
84+
uint8_t vds_p_deg:2;
85+
uint8_t unused:4;
86+
};
87+
uint8_t reg;
88+
};
89+
90+
91+
92+
union STSPIN32G4PowMng {
93+
struct {
94+
uint8_t vcc_val:2;
95+
uint8_t unused:2;
96+
uint8_t stby_reg_en:1;
97+
uint8_t vcc_dis:1;
98+
uint8_t reg3v3_dis:1;
99+
uint8_t unused2:1;
100+
};
101+
uint8_t reg;
102+
};
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
class STSPIN32G4 : public BLDCDriver6PWM {
115+
public:
116+
STSPIN32G4();
117+
~STSPIN32G4();
118+
119+
int init() override;
120+
121+
void wake();
122+
void sleep();
123+
bool isReady();
124+
bool isFault();
125+
126+
STSPIN32G4Status status();
127+
void lock();
128+
void unlock();
129+
STSPIN32G4NFault getNFaultRegister();
130+
STSPIN32G4Ready getReadyRegister();
131+
STSPIN32G4Logic getLogicRegister();
132+
STSPIN32G4PowMng getPowMngRegister();
133+
void setNFaultRegister(STSPIN32G4NFault value);
134+
void setReadyRegister(STSPIN32G4Ready value);
135+
void setLogicRegister(STSPIN32G4Logic value);
136+
void setPowMngRegister(STSPIN32G4PowMng value);
137+
void resetRegisters();
138+
void clearFaults();
139+
140+
protected:
141+
uint8_t readRegister(uint8_t reg);
142+
void writeRegister(uint8_t reg, uint8_t val);
143+
144+
TwoWire _wire;
145+
};
146+
147+
148+
#endif

0 commit comments

Comments
 (0)