Skip to content

Commit 88d74ae

Browse files
committed
Added mutex to DRV SPI driver, moved fault stop to thread, smooth current ramping for sensor and flux linkage detection
1 parent c0f75f1 commit 88d74ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+184
-107
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* Updated ChibiOS to version 3.0.5.
1515
* Increased USB thread priority to avoid freeze during connect/disconnect on windows.
1616
* Smooth current ramping during resistance measurement.
17+
* Moved fault stop to thread, and added SPI mutexes to DRV drivers.
18+
* Smooth current ramping in flux linkage measurement and sensor detection.
1719

1820
=== FW 4.02 ===
1921
* Position PID fix (most notable on multiturn encoders).

build_all/100_250/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

build_all/46_o_47/VESC_0005ohm.bin

0 Bytes
Binary file not shown.

build_all/46_o_47/VESC_33k.bin

0 Bytes
Binary file not shown.

build_all/46_o_47/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/48/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/60/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/60_MK3/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/75_300/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/75_300_R2/VESC_default.bin

0 Bytes
Binary file not shown.
Binary file not shown.

build_all/A200S_V21/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/A200S_V22/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/AXIOM/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/DAS_RS/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/HD/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build_all/LUNA_BBSHD/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

build_all/UAVC_OMEGA/VESC_default.bin

0 Bytes
Binary file not shown.

build_all/UNITY/VESC_default.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

conf_general.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,10 @@ bool conf_general_measure_flux_linkage_openloop(float current, float duty,
840840
float rpm_now = 0;
841841

842842
// Start by locking the motor
843-
mcpwm_foc_set_openloop(current, rpm_now);
843+
for (int i = 0;i < 200;i++) {
844+
mcpwm_foc_set_openloop((float)i * current / 200.0, rpm_now);
845+
chThdSleepMilliseconds(1);
846+
}
844847

845848
float duty_still = 0;
846849
float samples = 0;
@@ -1015,12 +1018,15 @@ int conf_general_autodetect_apply_sensors_foc(float current,
10151018
}
10161019

10171020
// AS5047 encoder
1021+
#ifndef HW_HAS_DUAL_MOTORS
10181022
if (!res) {
10191023
mcconf->m_sensor_port_mode = SENSOR_PORT_MODE_AS5047_SPI;
10201024
mc_interface_set_configuration(mcconf);
10211025

1022-
mcpwm_foc_set_openloop_phase(current, 0.0);
1023-
chThdSleepMilliseconds(1000);
1026+
for (int i = 0;i < 1000;i++) {
1027+
mcpwm_foc_set_openloop_phase((float)i * current / 1000.0, 0.0);
1028+
chThdSleepMilliseconds(1);
1029+
}
10241030

10251031
float phase_start = encoder_read_deg();
10261032
float phase_mid = 0.0;
@@ -1053,6 +1059,7 @@ int conf_general_autodetect_apply_sensors_foc(float current,
10531059
result = 2;
10541060
}
10551061
}
1062+
#endif
10561063

10571064
// Sensorless
10581065
if (!res) {

gpdrive.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void gpdrive_init(volatile mc_configuration *configuration) {
251251

252252
// Check if the system has resumed from IWDG reset
253253
if (timeout_had_IWDG_reset()) {
254-
mc_interface_fault_stop(FAULT_CODE_BOOTING_FROM_WATCHDOG_RESET, false);
254+
mc_interface_fault_stop(FAULT_CODE_BOOTING_FROM_WATCHDOG_RESET, false, false);
255255
}
256256

257257
m_init_done = true;
@@ -547,19 +547,19 @@ static void adc_int_handler(void *p, uint32_t flags) {
547547

548548
if ((wrong_voltage_iterations >= 8)) {
549549
mc_interface_fault_stop(input_voltage < m_conf->l_min_vin ?
550-
FAULT_CODE_UNDER_VOLTAGE : FAULT_CODE_OVER_VOLTAGE, false);
550+
FAULT_CODE_UNDER_VOLTAGE : FAULT_CODE_OVER_VOLTAGE, false, true);
551551
}
552552
} else {
553553
wrong_voltage_iterations = 0;
554554
}
555555

556556
if (m_conf->l_slow_abs_current) {
557557
if (fabsf(m_current_now) > m_conf->l_abs_current_max) {
558-
mc_interface_fault_stop(FAULT_CODE_ABS_OVER_CURRENT, false);
558+
mc_interface_fault_stop(FAULT_CODE_ABS_OVER_CURRENT, false, true);
559559
}
560560
} else {
561561
if (fabsf(m_current_now_filtered) > m_conf->l_abs_current_max) {
562-
mc_interface_fault_stop(FAULT_CODE_ABS_OVER_CURRENT, false);
562+
mc_interface_fault_stop(FAULT_CODE_ABS_OVER_CURRENT, false, true);
563563
}
564564
}
565565

hwconf/drv8301.c

+9
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ static void terminal_reset_faults(int argc, const char **argv);
4545

4646
// Private variables
4747
static char m_fault_print_buffer[120];
48+
static mutex_t m_spi_mutex;
4849

4950
void drv8301_init(void) {
51+
chMtxObjectInit(&m_spi_mutex);
52+
5053
// DRV8301 SPI
5154
palSetPadMode(DRV8301_MISO_GPIO, DRV8301_MISO_PIN, PAL_MODE_INPUT);
5255
palSetPadMode(DRV8301_SCK_GPIO, DRV8301_SCK_PIN, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
@@ -221,6 +224,8 @@ unsigned int drv8301_read_reg(int reg) {
221224
out |= (reg & 0x0F) << 11;
222225
out |= 0x807F;
223226

227+
chMtxLock(&m_spi_mutex);
228+
224229
if (reg != 0) {
225230
spi_begin();
226231
spi_exchange(out);
@@ -231,6 +236,8 @@ unsigned int drv8301_read_reg(int reg) {
231236
uint16_t res = spi_exchange(0xFFFF);
232237
spi_end();
233238

239+
chMtxUnlock(&m_spi_mutex);
240+
234241
return res;
235242
}
236243

@@ -239,9 +246,11 @@ void drv8301_write_reg(int reg, int data) {
239246
out |= (reg & 0x0F) << 11;
240247
out |= data & 0x7FF;
241248

249+
chMtxLock(&m_spi_mutex);
242250
spi_begin();
243251
spi_exchange(out);
244252
spi_end();
253+
chMtxUnlock(&m_spi_mutex);
245254
}
246255

247256
// Software SPI

hwconf/drv8305.c

+9
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ static void terminal_write_reg(int argc, const char **argv);
4141

4242
// Private variables
4343
static char m_fault_print_buffer[120];
44+
static mutex_t m_spi_mutex;
4445

4546
void drv8305_init(void) {
47+
chMtxObjectInit(&m_spi_mutex);
48+
4649
// DRV8305 SPI
4750
palSetPadMode(DRV8305_MISO_GPIO, DRV8305_MISO_PIN, PAL_MODE_INPUT);
4851
palSetPadMode(DRV8305_SCK_GPIO, DRV8305_SCK_PIN, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
@@ -165,6 +168,8 @@ unsigned int drv8305_read_reg(int reg) {
165168
out |= (reg & 0x0F) << 11;
166169
out |= 0x807F;
167170

171+
chMtxLock(&m_spi_mutex);
172+
168173
if (reg != 0) {
169174
spi_begin();
170175
spi_exchange(out);
@@ -175,6 +180,8 @@ unsigned int drv8305_read_reg(int reg) {
175180
uint16_t res = spi_exchange(0xFFFF);
176181
spi_end();
177182

183+
chMtxUnlock(&m_spi_mutex);
184+
178185
return res;
179186
}
180187

@@ -183,9 +190,11 @@ void drv8305_write_reg(int reg, int data) {
183190
out |= (reg & 0x0F) << 11;
184191
out |= data & 0x7FF;
185192

193+
chMtxLock(&m_spi_mutex);
186194
spi_begin();
187195
spi_exchange(out);
188196
spi_end();
197+
chMtxUnlock(&m_spi_mutex);
189198
}
190199

191200
// Software SPI

hwconf/drv8320s.c

+9
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ static void terminal_reset_faults(int argc, const char **argv);
4444

4545
// Private variables
4646
static char m_fault_print_buffer[120];
47+
static mutex_t m_spi_mutex;
4748

4849
void drv8320s_init(void) {
50+
chMtxObjectInit(&m_spi_mutex);
51+
4952
// DRV8320S SPI
5053
palSetPadMode(DRV8320S_MISO_GPIO, DRV8320S_MISO_PIN, PAL_MODE_INPUT_PULLUP);
5154
palSetPadMode(DRV8320S_SCK_GPIO, DRV8320S_SCK_PIN, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
@@ -265,6 +268,8 @@ unsigned int drv8320s_read_reg(int reg) {
265268
out |= (reg & 0x0F) << 11;
266269
out |= 0x807F;
267270

271+
chMtxLock(&m_spi_mutex);
272+
268273
if (reg != 0) {
269274
spi_begin();
270275
spi_exchange(out);
@@ -275,6 +280,8 @@ unsigned int drv8320s_read_reg(int reg) {
275280
uint16_t res = spi_exchange(out);
276281
spi_end();
277282

283+
chMtxUnlock(&m_spi_mutex);
284+
278285
return res;
279286
}
280287

@@ -283,9 +290,11 @@ void drv8320s_write_reg(int reg, int data) {
283290
out |= (reg & 0x0F) << 11;
284291
out |= data & 0x7FF;
285292

293+
chMtxLock(&m_spi_mutex);
286294
spi_begin();
287295
spi_exchange(out);
288296
spi_end();
297+
chMtxUnlock(&m_spi_mutex);
289298
}
290299

291300
// Software SPI

hwconf/drv8323s.c

+9
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ static void terminal_reset_faults(int argc, const char **argv);
4545

4646
// Private variables
4747
static char m_fault_print_buffer[120];
48+
static mutex_t m_spi_mutex;
4849

4950
void drv8323s_init(void) {
51+
chMtxObjectInit(&m_spi_mutex);
52+
5053
// DRV8323S SPI
5154
palSetPadMode(DRV8323S_MISO_GPIO, DRV8323S_MISO_PIN, PAL_MODE_INPUT_PULLUP);
5255
palSetPadMode(DRV8323S_SCK_GPIO, DRV8323S_SCK_PIN, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
@@ -318,6 +321,8 @@ unsigned int drv8323s_read_reg(int reg) {
318321
out |= (reg & 0x0F) << 11;
319322
out |= 0x807F;
320323

324+
chMtxLock(&m_spi_mutex);
325+
321326
if (reg != 0) {
322327
spi_begin();
323328
spi_exchange(out);
@@ -328,6 +333,8 @@ unsigned int drv8323s_read_reg(int reg) {
328333
uint16_t res = spi_exchange(out);
329334
spi_end();
330335

336+
chMtxUnlock(&m_spi_mutex);
337+
331338
return res;
332339
}
333340

@@ -336,9 +343,11 @@ void drv8323s_write_reg(int reg, int data) {
336343
out |= (reg & 0x0F) << 11;
337344
out |= data & 0x7FF;
338345

346+
chMtxLock(&m_spi_mutex);
339347
spi_begin();
340348
spi_exchange(out);
341349
spi_end();
350+
chMtxUnlock(&m_spi_mutex);
342351
}
343352

344353
// Software SPI

irq_handlers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ CH_IRQ_HANDLER(PVD_IRQHandler) {
6565
if (EXTI_GetITStatus(EXTI_Line16) != RESET) {
6666
// Log the fault. Supply voltage dropped below 2.9V,
6767
// could corrupt an ongoing flash programming
68-
mc_interface_fault_stop(FAULT_CODE_MCU_UNDER_VOLTAGE, false);
68+
mc_interface_fault_stop(FAULT_CODE_MCU_UNDER_VOLTAGE, false, true);
6969

7070
// Clear the PVD pending bit
7171
EXTI_ClearITPendingBit(EXTI_Line16);

0 commit comments

Comments
 (0)