Skip to content

Commit 78d3cef

Browse files
committed
Added support for HW60_MK3 and disable shutdown when watchdog runs slowly
1 parent 23e6192 commit 78d3cef

11 files changed

+196
-9
lines changed

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
=== FW 3.64 ===
2+
* Added support for HW60_MK3
3+
* Disable shutdown sampling when the watchdog runs slowly.
4+
15
=== FW 3.63 ===
26
* NRF remote power meter is now unaffected by temperature decrease and speed limits.
37
* Added LZO compression support to firmware upload, making firmware updates 30% - 50% faster.

applications/app_uartcomm.c

+9
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ static void send_packet(unsigned char *data, unsigned int len) {
7171
#ifdef HW_UART_P_DEV
7272
if (from_p_uart) {
7373
if (uart_p_is_running) {
74+
#ifdef HW_UART_P_DEV_TX
75+
sdWrite(&HW_UART_P_DEV_TX, data, len);
76+
#else
7477
sdWrite(&HW_UART_P_DEV, data, len);
78+
#endif
7579
}
7680
} else {
7781
if (uart_is_running) {
@@ -117,6 +121,11 @@ void app_uartcomm_start_permanent(void) {
117121
}
118122

119123
sdStart(&HW_UART_P_DEV, &uart_p_cfg);
124+
125+
#ifdef HW_UART_P_DEV_TX
126+
sdStart(&HW_UART_P_DEV_TX, &uart_p_cfg);
127+
#endif
128+
120129
palSetPadMode(HW_UART_P_TX_PORT, HW_UART_P_TX_PIN, PAL_MODE_ALTERNATE(HW_UART_P_GPIO_AF) |
121130
PAL_STM32_OSPEED_HIGHEST |
122131
PAL_STM32_PUDR_PULLUP);

conf_general.h

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
// Benjamins first HW60 PCB with PB5 and PB6 swapped
6868
//#define HW60_VEDDER_FIRST_PCB
6969

70+
// Mark3 version of HW60 with power switch and separate NRF UART.
71+
#define HW60_IS_MK3
72+
7073
#define HW_SOURCE "hw_60.c"
7174
#define HW_HEADER "hw_60.h"
7275

hwconf/hw_60.c

+73
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@
2222
#include "stm32f4xx_conf.h"
2323
#include "utils.h"
2424
#include "drv8301.h"
25+
#include "terminal.h"
26+
#include "commands.h"
27+
#include "mc_interface.h"
2528

2629
// Variables
2730
static volatile bool i2c_running = false;
31+
#ifdef HW60_IS_MK3
32+
static mutex_t shutdown_mutex;
33+
static float bt_diff = 0.0;
34+
#endif
2835

2936
// I2C configuration
3037
static const I2CConfig i2cfg = {
@@ -33,7 +40,16 @@ static const I2CConfig i2cfg = {
3340
STD_DUTY_CYCLE
3441
};
3542

43+
#ifdef HW60_IS_MK3
44+
static void terminal_shutdown_now(int argc, const char **argv);
45+
static void terminal_button_test(int argc, const char **argv);
46+
#endif
47+
3648
void hw_init_gpio(void) {
49+
#ifdef HW60_IS_MK3
50+
chMtxObjectInit(&shutdown_mutex);
51+
#endif
52+
3753
// GPIO clock enable
3854
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
3955
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
@@ -110,9 +126,25 @@ void hw_init_gpio(void) {
110126
palSetPadMode(GPIOC, 2, PAL_MODE_INPUT_ANALOG);
111127
palSetPadMode(GPIOC, 3, PAL_MODE_INPUT_ANALOG);
112128
palSetPadMode(GPIOC, 4, PAL_MODE_INPUT_ANALOG);
129+
#ifndef HW60_IS_MK3
113130
palSetPadMode(GPIOC, 5, PAL_MODE_INPUT_ANALOG);
131+
#endif
114132

115133
drv8301_init();
134+
135+
#ifdef HW60_IS_MK3
136+
terminal_register_command_callback(
137+
"shutdown",
138+
"Shutdown VESC now.",
139+
0,
140+
terminal_shutdown_now);
141+
142+
terminal_register_command_callback(
143+
"test_button",
144+
"Try sampling the shutdown button",
145+
0,
146+
terminal_button_test);
147+
#endif
116148
}
117149

118150
void hw_setup_adc_channels(void) {
@@ -242,3 +274,44 @@ void hw_try_restore_i2c(void) {
242274
i2cReleaseBus(&HW_I2C_DEV);
243275
}
244276
}
277+
278+
#ifdef HW60_IS_MK3
279+
bool hw_sample_shutdown_button(void) {
280+
chMtxLock(&shutdown_mutex);
281+
282+
bt_diff = 0.0;
283+
284+
for (int i = 0;i < 3;i++) {
285+
palSetPadMode(HW_SHUTDOWN_GPIO, HW_SHUTDOWN_PIN, PAL_MODE_INPUT_ANALOG);
286+
chThdSleep(5);
287+
float val1 = ADC_VOLTS(ADC_IND_SHUTDOWN);
288+
chThdSleepMilliseconds(1);
289+
float val2 = ADC_VOLTS(ADC_IND_SHUTDOWN);
290+
palSetPadMode(HW_SHUTDOWN_GPIO, HW_SHUTDOWN_PIN, PAL_MODE_OUTPUT_PUSHPULL);
291+
chThdSleepMilliseconds(1);
292+
293+
bt_diff += (val1 - val2);
294+
}
295+
296+
chMtxUnlock(&shutdown_mutex);
297+
298+
return (bt_diff > 0.12);
299+
}
300+
301+
static void terminal_shutdown_now(int argc, const char **argv) {
302+
(void)argc;
303+
(void)argv;
304+
DISABLE_GATE();
305+
HW_SHUTDOWN_HOLD_OFF();
306+
}
307+
308+
static void terminal_button_test(int argc, const char **argv) {
309+
(void)argc;
310+
(void)argv;
311+
312+
for (int i = 0;i < 40;i++) {
313+
commands_printf("BT: %d %.2f", HW_SAMPLE_SHUTDOWN(), (double)bt_diff);
314+
chThdSleepMilliseconds(100);
315+
}
316+
}
317+
#endif

hwconf/hw_60.h

+66-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
#ifndef HW_60_H_
2121
#define HW_60_H_
2222

23+
#ifdef HW60_IS_MK3
24+
#define HW_NAME "60_MK3"
25+
#else
2326
#define HW_NAME "60"
27+
#endif
2428

2529
// HW properties
2630
#define HW_HAS_DRV8301
2731
#define HW_HAS_3_SHUNTS
28-
#define HW_HAS_PERMANENT_NRF
2932
#define HW_HAS_PHASE_SHUNTS
33+
#ifndef HW60_IS_MK3
34+
#define HW_HAS_PERMANENT_NRF
35+
#endif
3036

3137
// Macros
3238
#ifdef HW60_VEDDER_FIRST_PCB
@@ -48,6 +54,22 @@
4854
#define CURRENT_FILTER_ON() palSetPad(GPIOD, 2)
4955
#define CURRENT_FILTER_OFF() palClearPad(GPIOD, 2)
5056

57+
#ifdef HW60_IS_MK3
58+
// Shutdown pin
59+
#define HW_SHUTDOWN_GPIO GPIOC
60+
#define HW_SHUTDOWN_PIN 5
61+
#define HW_SHUTDOWN_HOLD_ON() palSetPad(HW_SHUTDOWN_GPIO, HW_SHUTDOWN_PIN)
62+
#define HW_SHUTDOWN_HOLD_OFF() palClearPad(HW_SHUTDOWN_GPIO, HW_SHUTDOWN_PIN)
63+
#define HW_SAMPLE_SHUTDOWN() hw_sample_shutdown_button()
64+
65+
// Hold shutdown pin early to wake up on short pulses
66+
#define HW_EARLY_INIT() palSetPadMode(HW_SHUTDOWN_GPIO, HW_SHUTDOWN_PIN, PAL_MODE_OUTPUT_PUSHPULL); \
67+
HW_SHUTDOWN_HOLD_ON(); \
68+
palSetPadMode(GPIOD, 2, \
69+
PAL_MODE_OUTPUT_PUSHPULL | \
70+
PAL_STM32_OSPEED_HIGHEST); \
71+
CURRENT_FILTER_ON()
72+
#else
5173
// Switch on current filter if a permanent
5274
// NRF24 cannot be found, as the later
5375
// HW60 has changed one of the permanent NRF
@@ -57,6 +79,7 @@
5779
PAL_MODE_OUTPUT_PUSHPULL | \
5880
PAL_STM32_OSPEED_HIGHEST); \
5981
CURRENT_FILTER_ON()
82+
#endif
6083

6184
/*
6285
* ADC Vector
@@ -71,7 +94,7 @@
7194
* 7: IN6 ADC_EXT2
7295
* 8: IN3 TEMP_PCB
7396
* 9: IN14 TEMP_MOTOR
74-
* 10: IN15 ADC_EXT3
97+
* 10: IN15 ADC_EXT3, Shutdown on MK3
7598
* 11: IN13 AN_IN
7699
* 12: Vrefint
77100
* 13: IN0 SENS1
@@ -95,6 +118,9 @@
95118
#define ADC_IND_TEMP_MOS 8
96119
#define ADC_IND_TEMP_MOTOR 9
97120
#define ADC_IND_VREFINT 12
121+
#ifdef HW60_IS_MK3
122+
#define ADC_IND_SHUTDOWN 10
123+
#endif
98124

99125
// ADC macros and settings
100126

@@ -154,6 +180,18 @@
154180
#define HW_UART_RX_PORT GPIOB
155181
#define HW_UART_RX_PIN 11
156182

183+
#ifdef HW60_IS_MK3
184+
// Permanent UART Peripheral (for NRF51)
185+
#define HW_UART_P_BAUD 115200
186+
#define HW_UART_P_DEV SD4
187+
#define HW_UART_P_DEV_TX SD5 // UART for TX, due to mistake below
188+
#define HW_UART_P_GPIO_AF GPIO_AF_UART4
189+
#define HW_UART_P_TX_PORT GPIOC
190+
#define HW_UART_P_TX_PIN 12 // This is a mistake in the HW. We have to use a hack to use UART5.
191+
#define HW_UART_P_RX_PORT GPIOC
192+
#define HW_UART_P_RX_PIN 11
193+
#endif
194+
157195
// ICU Peripheral for servo decoding
158196
#define HW_USE_SERVO_TIM4
159197
#define HW_ICU_TIMER TIM4
@@ -190,6 +228,7 @@
190228
#define HW_ENC_TIM_ISR_CH TIM3_IRQn
191229
#define HW_ENC_TIM_ISR_VEC TIM3_IRQHandler
192230

231+
#ifndef HW60_IS_MK3
193232
// NRF pins
194233
#define NRF_PORT_CSN GPIOB
195234
#define NRF_PIN_CSN 12
@@ -199,6 +238,7 @@
199238
#define NRF_PIN_MOSI 3
200239
#define NRF_PORT_MISO GPIOD
201240
#define NRF_PIN_MISO 2
241+
#endif
202242

203243
// SPI pins
204244
#define HW_SPI_DEV SPID1
@@ -213,6 +253,16 @@
213253
#define HW_SPI_PIN_MISO 6
214254

215255
// SPI for DRV8301
256+
#ifndef HW60_IS_MK3
257+
#define DRV8301_MOSI_GPIO GPIOB
258+
#define DRV8301_MOSI_PIN 4
259+
#define DRV8301_MISO_GPIO GPIOB
260+
#define DRV8301_MISO_PIN 3
261+
#define DRV8301_SCK_GPIO GPIOC
262+
#define DRV8301_SCK_PIN 10
263+
#define DRV8301_CS_GPIO GPIOC
264+
#define DRV8301_CS_PIN 9
265+
#else
216266
#define DRV8301_MOSI_GPIO GPIOC
217267
#define DRV8301_MOSI_PIN 12
218268
#define DRV8301_MISO_GPIO GPIOC
@@ -221,6 +271,7 @@
221271
#define DRV8301_SCK_PIN 10
222272
#define DRV8301_CS_GPIO GPIOC
223273
#define DRV8301_CS_PIN 9
274+
#endif
224275

225276
// MPU9250
226277
#define MPU9X50_SDA_GPIO GPIOB
@@ -229,6 +280,14 @@
229280
#define MPU9X50_SCL_PIN 15
230281
#define IMU_FLIP
231282

283+
#ifdef HW60_IS_MK3
284+
// NRF SWD
285+
#define NRF5x_SWDIO_GPIO GPIOB
286+
#define NRF5x_SWDIO_PIN 12
287+
#define NRF5x_SWCLK_GPIO GPIOA
288+
#define NRF5x_SWCLK_PIN 4
289+
#endif
290+
232291
// Measurement macros
233292
#define ADC_V_L1 ADC_Value[ADC_IND_SENS1]
234293
#define ADC_V_L2 ADC_Value[ADC_IND_SENS2]
@@ -261,4 +320,9 @@
261320
#define HW_LIM_DUTY_MAX 0.0, 0.99
262321
#define HW_LIM_TEMP_FET -40.0, 110.0
263322

323+
// Functions
324+
#ifdef HW60_IS_MK3
325+
bool hw_sample_shutdown_button(void);
326+
#endif
327+
264328
#endif /* HW_60_H_ */

hwconf/hw_hd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ bool hw_sample_shutdown_button(void) {
266266
bt_diff = 0.0;
267267

268268
for (int i = 0;i < 3;i++) {
269-
palSetPadMode(GPIOC, 5, PAL_MODE_INPUT_ANALOG);
269+
palSetPadMode(HW_SHUTDOWN_GPIO, HW_SHUTDOWN_PIN, PAL_MODE_INPUT_ANALOG);
270270
chThdSleep(5);
271271
float val1 = ADC_VOLTS(ADC_IND_SHUTDOWN);
272272
chThdSleepMilliseconds(1);

hwconf/hw_hd.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
#define HW_UART_RX_PORT GPIOB
148148
#define HW_UART_RX_PIN 11
149149

150-
// Permanent UART Peripheral (for NRF51)
150+
// Permanent UART Peripheral (for NRF52)
151151
#define HW_UART_P_BAUD 115200
152152
#define HW_UART_P_DEV SD4
153153
#define HW_UART_P_GPIO_AF GPIO_AF_UART4

mcuconf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
#define STM32_SERIAL_USE_USART2 FALSE
276276
#define STM32_SERIAL_USE_USART3 TRUE
277277
#define STM32_SERIAL_USE_UART4 TRUE
278-
#define STM32_SERIAL_USE_UART5 FALSE
278+
#define STM32_SERIAL_USE_UART5 TRUE
279279
#define STM32_SERIAL_USE_USART6 TRUE
280280
#define STM32_SERIAL_USART1_PRIORITY 3
281281
#define STM32_SERIAL_USART2_PRIORITY 3

shutdown.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@
2626
bool volatile m_button_pressed = false;
2727
static volatile float m_inactivity_time = 0.0;
2828
static THD_WORKING_AREA(shutdown_thread_wa, 128);
29+
static mutex_t m_sample_mutex;
30+
static volatile bool m_init_done = false;
31+
static volatile bool m_sampling_disabled = false;
2932

3033
// Private functions
3134
static THD_FUNCTION(shutdown_thread, arg);
3235

3336
void shutdown_init(void) {
37+
chMtxObjectInit(&m_sample_mutex);
3438
chThdCreateStatic(shutdown_thread_wa, sizeof(shutdown_thread_wa), NORMALPRIO, shutdown_thread, NULL);
39+
m_init_done = true;
3540
}
3641

3742
void shutdown_reset_timer(void) {
@@ -46,6 +51,16 @@ float shutdown_get_inactivity_time(void) {
4651
return m_inactivity_time;
4752
}
4853

54+
void shutdown_set_sampling_disabled(bool disabled) {
55+
if (!m_init_done) {
56+
return;
57+
}
58+
59+
chMtxLock(&m_sample_mutex);
60+
m_sampling_disabled = disabled;
61+
chMtxUnlock(&m_sample_mutex);
62+
}
63+
4964
static THD_FUNCTION(shutdown_thread, arg) {
5065
(void)arg;
5166

@@ -59,12 +74,21 @@ static THD_FUNCTION(shutdown_thread, arg) {
5974
float dt = (float)chVTTimeElapsedSinceX(last_iteration_time) / (float)CH_CFG_ST_FREQUENCY;
6075
last_iteration_time = chVTGetSystemTimeX();
6176

62-
const app_configuration *conf = app_get_configuration();
77+
chMtxLock(&m_sample_mutex);
78+
79+
if (m_sampling_disabled) {
80+
chMtxUnlock(&m_sample_mutex);
81+
chThdSleepMilliseconds(10);
82+
continue;
83+
}
6384

6485
bool sample = HW_SAMPLE_SHUTDOWN();
86+
chMtxUnlock(&m_sample_mutex);
6587
bool clicked = m_button_pressed && !sample;
6688
m_button_pressed = sample;
6789

90+
const app_configuration *conf = app_get_configuration();
91+
6892
// Note: When the gates are enabled, the push to start function
6993
// will prevent the regulator from shutting down. Therefore, the
7094
// gate driver has to be disabled.

0 commit comments

Comments
 (0)