drivers: half_bridge: add function to force low-side switch on
Add half_bridge_force_low_side() to switch the low-side switch continuously on, bypassing the compare value limits. This is required for safety measures like blowing the battery fuse in case of a shorted high-side MOSFET. Previously, this was attempted by setting a duty cycle of 0 and starting the PWM. However, the STM32 timer drivers refuse to start with a compare value of 0, so the low-side switch was never turned on. The forced state is kept until half_bridge_stop() is called. Changes of the compare value or its limits and start requests are rejected with -EBUSY meanwhile, so that a concurrently running control loop can't override it. - advtim: Compare value 0 keeps OCxREF low, so that CHxN stays on. The output mode can't be changed, as it is locked during initialization. - gptim: Compare value 0 for both channels, so that the low-side channel with inverted polarity stays on. - HRTIM: Output 1 is reset by software and set events are removed, so that the complementary output 2 stays on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f8226e0687
commit
427943994c
@@ -31,12 +31,17 @@ struct half_bridge_emul_data
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool enabled;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static int half_bridge_emul_start(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
data->enabled = true;
|
||||
|
||||
return 0;
|
||||
@@ -47,6 +52,7 @@ static int half_bridge_emul_stop(const struct device *dev)
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->enabled = false;
|
||||
data->low_side_forced = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -62,6 +68,10 @@ static int half_bridge_emul_set_compare(const struct device *dev, uint32_t compa
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
data->compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
return 0;
|
||||
@@ -85,6 +95,10 @@ static int half_bridge_emul_set_compare_limits(const struct device *dev, uint32_
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > data->period) {
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -95,6 +109,17 @@ static int half_bridge_emul_set_compare_limits(const struct device *dev, uint32_
|
||||
return half_bridge_emul_set_compare(dev, data->compare);
|
||||
}
|
||||
|
||||
static int half_bridge_emul_force_low_side(const struct device *dev)
|
||||
{
|
||||
struct half_bridge_emul_data *data = dev->data;
|
||||
|
||||
data->compare = 0;
|
||||
data->enabled = true;
|
||||
data->low_side_forced = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_emul_api) = {
|
||||
.start = half_bridge_emul_start,
|
||||
.stop = half_bridge_emul_stop,
|
||||
@@ -103,6 +128,7 @@ static DEVICE_API(half_bridge, half_bridge_emul_api) = {
|
||||
.get_compare = half_bridge_emul_get_compare,
|
||||
.get_period = half_bridge_emul_get_period,
|
||||
.set_compare_limits = half_bridge_emul_set_compare_limits,
|
||||
.force_low_side = half_bridge_emul_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_emul_init(const struct device *dev)
|
||||
|
||||
@@ -42,6 +42,7 @@ struct half_bridge_stm32_advtim_data
|
||||
{
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static const uint32_t ch2ll_n[] = {
|
||||
@@ -56,8 +57,13 @@ static const uint32_t ch2ll_n[] = {
|
||||
static int half_bridge_stm32_advtim_start(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (half_bridge_stm32_tim_get_compare(tim, config->channel) == 0U) {
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -71,8 +77,10 @@ static int half_bridge_stm32_advtim_start(const struct device *dev)
|
||||
static int half_bridge_stm32_advtim_stop(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
|
||||
LL_TIM_DisableAllOutputs(config->tim.timer);
|
||||
data->low_side_forced = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -90,6 +98,10 @@ static int half_bridge_stm32_advtim_set_compare(const struct device *dev, uint32
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
/* protection against wrong settings which could destroy the hardware */
|
||||
compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
@@ -124,6 +136,10 @@ static int half_bridge_stm32_advtim_set_compare_limits(const struct device *dev,
|
||||
{
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > half_bridge_stm32_advtim_get_period(dev)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -134,6 +150,29 @@ static int half_bridge_stm32_advtim_set_compare_limits(const struct device *dev,
|
||||
return half_bridge_stm32_advtim_set_compare(dev, half_bridge_stm32_advtim_get_compare(dev));
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_advtim_force_low_side(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_advtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_advtim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
data->low_side_forced = true;
|
||||
|
||||
/*
|
||||
* A compare value of 0 keeps OCxREF low in PWM mode 1, so CHx (high side) stays off and the
|
||||
* complementary output CHxN (low side) stays on. The output mode itself can't be changed, as
|
||||
* it is locked after initialization.
|
||||
*/
|
||||
half_bridge_stm32_tim_set_compare(tim, config->channel, 0);
|
||||
|
||||
/* load preload registers immediately */
|
||||
LL_TIM_GenerateEvent_UPDATE(tim);
|
||||
|
||||
LL_TIM_EnableAllOutputs(tim);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_stm32_advtim_api) = {
|
||||
.start = half_bridge_stm32_advtim_start,
|
||||
.stop = half_bridge_stm32_advtim_stop,
|
||||
@@ -142,6 +181,7 @@ static DEVICE_API(half_bridge, half_bridge_stm32_advtim_api) = {
|
||||
.get_compare = half_bridge_stm32_advtim_get_compare,
|
||||
.get_period = half_bridge_stm32_advtim_get_period,
|
||||
.set_compare_limits = half_bridge_stm32_advtim_set_compare_limits,
|
||||
.force_low_side = half_bridge_stm32_advtim_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_stm32_advtim_init_adc_trigger(TIM_TypeDef *tim)
|
||||
|
||||
@@ -42,6 +42,7 @@ struct half_bridge_stm32_gptim_data
|
||||
uint32_t deadtime_clocks;
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static inline uint32_t ll_channels(const struct half_bridge_stm32_gptim_config *config)
|
||||
@@ -53,8 +54,13 @@ static inline uint32_t ll_channels(const struct half_bridge_stm32_gptim_config *
|
||||
static int half_bridge_stm32_gptim_start(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (half_bridge_stm32_tim_get_compare(tim, config->high_side_channel) == 0U) {
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -67,8 +73,10 @@ static int half_bridge_stm32_gptim_start(const struct device *dev)
|
||||
static int half_bridge_stm32_gptim_stop(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
|
||||
LL_TIM_CC_DisableChannel(config->tim.timer, ll_channels(config));
|
||||
data->low_side_forced = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -87,6 +95,10 @@ static int half_bridge_stm32_gptim_set_compare(const struct device *dev, uint32_
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
/* protection against wrong settings which could destroy the hardware */
|
||||
compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
@@ -116,6 +128,10 @@ static int half_bridge_stm32_gptim_set_compare_limits(const struct device *dev,
|
||||
{
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > half_bridge_stm32_gptim_get_period(dev)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -126,6 +142,29 @@ static int half_bridge_stm32_gptim_set_compare_limits(const struct device *dev,
|
||||
return half_bridge_stm32_gptim_set_compare(dev, half_bridge_stm32_gptim_get_compare(dev));
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_gptim_force_low_side(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_gptim_config *config = dev->config;
|
||||
struct half_bridge_stm32_gptim_data *data = dev->data;
|
||||
TIM_TypeDef *tim = config->tim.timer;
|
||||
|
||||
data->low_side_forced = true;
|
||||
|
||||
/*
|
||||
* A compare value of 0 keeps OCxREF low in PWM mode 1 for both channels. The high-side output
|
||||
* (active high) stays off and the low-side output (active low) stays on.
|
||||
*/
|
||||
half_bridge_stm32_tim_set_compare(tim, config->high_side_channel, 0);
|
||||
half_bridge_stm32_tim_set_compare(tim, config->low_side_channel, 0);
|
||||
|
||||
/* load preload registers immediately */
|
||||
LL_TIM_GenerateEvent_UPDATE(tim);
|
||||
|
||||
LL_TIM_CC_EnableChannel(tim, ll_channels(config));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_stm32_gptim_api) = {
|
||||
.start = half_bridge_stm32_gptim_start,
|
||||
.stop = half_bridge_stm32_gptim_stop,
|
||||
@@ -134,6 +173,7 @@ static DEVICE_API(half_bridge, half_bridge_stm32_gptim_api) = {
|
||||
.get_compare = half_bridge_stm32_gptim_get_compare,
|
||||
.get_period = half_bridge_stm32_gptim_get_period,
|
||||
.set_compare_limits = half_bridge_stm32_gptim_set_compare_limits,
|
||||
.force_low_side = half_bridge_stm32_gptim_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_stm32_gptim_init(const struct device *dev)
|
||||
|
||||
@@ -55,6 +55,7 @@ struct half_bridge_stm32_hrtim_data
|
||||
{
|
||||
uint32_t compare_min;
|
||||
uint32_t compare_max;
|
||||
bool low_side_forced;
|
||||
};
|
||||
|
||||
static inline HRTIM_Timerx_TypeDef *timer_a(const struct device *dev)
|
||||
@@ -67,6 +68,11 @@ static inline HRTIM_Timerx_TypeDef *timer_a(const struct device *dev)
|
||||
static int half_bridge_stm32_hrtim_start(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
config->hrtim->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
|
||||
|
||||
@@ -76,9 +82,18 @@ static int half_bridge_stm32_hrtim_start(const struct device *dev)
|
||||
static int half_bridge_stm32_hrtim_stop(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
|
||||
config->hrtim->sCommonRegs.ODISR = HRTIM_ODISR_TA1ODIS | HRTIM_ODISR_TA2ODIS;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
/* restore PWM generation for output 1 */
|
||||
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
|
||||
tim->SETx1R = HRTIM_SET1R_PER;
|
||||
tim->RSTx1R = HRTIM_RST1R_CMP1;
|
||||
data->low_side_forced = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -95,6 +110,10 @@ static int half_bridge_stm32_hrtim_set_compare(const struct device *dev, uint32_
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
/* protection against wrong settings which could destroy the hardware */
|
||||
compare = CLAMP(compare, data->compare_min, data->compare_max);
|
||||
|
||||
@@ -123,6 +142,10 @@ static int half_bridge_stm32_hrtim_set_compare_limits(const struct device *dev,
|
||||
{
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
|
||||
if (data->low_side_forced) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
if (min > max || max > half_bridge_stm32_hrtim_get_period(dev)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -133,6 +156,26 @@ static int half_bridge_stm32_hrtim_set_compare_limits(const struct device *dev,
|
||||
return half_bridge_stm32_hrtim_set_compare(dev, half_bridge_stm32_hrtim_get_compare(dev));
|
||||
}
|
||||
|
||||
static int half_bridge_stm32_hrtim_force_low_side(const struct device *dev)
|
||||
{
|
||||
const struct half_bridge_stm32_hrtim_config *config = dev->config;
|
||||
struct half_bridge_stm32_hrtim_data *data = dev->data;
|
||||
HRTIM_Timerx_TypeDef *tim = timer_a(dev);
|
||||
|
||||
data->low_side_forced = true;
|
||||
|
||||
/*
|
||||
* Remove all set events and reset output 1 (high side) by software, so that it stays
|
||||
* inactive. Output 2 (low side) is generated as the complement with dead time and stays on.
|
||||
*/
|
||||
tim->SETx1R = 0;
|
||||
tim->RSTx1R = HRTIM_RST1R_SRT;
|
||||
|
||||
config->hrtim->sCommonRegs.OENR = HRTIM_OENR_TA1OEN | HRTIM_OENR_TA2OEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEVICE_API(half_bridge, half_bridge_stm32_hrtim_api) = {
|
||||
.start = half_bridge_stm32_hrtim_start,
|
||||
.stop = half_bridge_stm32_hrtim_stop,
|
||||
@@ -141,6 +184,7 @@ static DEVICE_API(half_bridge, half_bridge_stm32_hrtim_api) = {
|
||||
.get_compare = half_bridge_stm32_hrtim_get_compare,
|
||||
.get_period = half_bridge_stm32_hrtim_get_period,
|
||||
.set_compare_limits = half_bridge_stm32_hrtim_set_compare_limits,
|
||||
.force_low_side = half_bridge_stm32_hrtim_force_low_side,
|
||||
};
|
||||
|
||||
static int half_bridge_stm32_hrtim_init(const struct device *dev)
|
||||
|
||||
Reference in New Issue
Block a user