2022-02-03 01:34 AM
I am switching 5V relay with BJT using MCU(STM32G030) pin. When clock speed is 64MHz(max speed) and 1ms timer update interrupt is enabled, not every
time I switched relay but sometimes MCU goes into hardFault handler. But if I disable timer update interrupt or reduce the clock speed to 16MHz or
dismount the BJT that I use to switch relay, hardFault doesn't occur.
Code below executed in 1ms timer update interrupt;
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
uint8_t var1 = 0;
TIM1->SR &= 0xFFFE; //clear pending bit
for(var1; var1 < 150; var1 ++) //pretend doing something
{
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
}
I have another interrupt from sysTick timer and it has same priority with timer update interrupt(from timer1 of MCU). They both generates 1ms interrupts.
Relay switches mains but there is no load connected and I have freewheeling diode on relay.
STM32G030 doesn't have HardFault Status Register so I can't look for hardFault source. I couldn't figure out the relation between switching relay, clock speed and interrupt that causing hardFault together.
By the way in hardFault function I am checking ECC flags to see if flash corruption occured and rarely ECCC or ECCD flag set.
In main code I read one hardware pin coming from ESP8266 that telling make relay on or make relay off. According to this pin's status I switch relay. Code is below:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(HAL_GPIO_ReadPin(relayOnOff_GPIO_Port, relayOnOff_Pin))
{
HAL_GPIO_WritePin(RelayPin_GPIO_Port, RelayPin_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(RelayPin_GPIO_Port, RelayPin_Pin, GPIO_PIN_RESET);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
2022-02-03 03:26 AM
When switching the relay, check the power supply voltage in general, and close to the STM32.
2022-02-03 03:48 AM
Thanks for answer. I have connected external power supply to be sure not brownout occuring but result was same.
2022-02-03 04:00 AM
Is power suply distribution blocked by capacitors? How far is relay form MCU? You can also insert some choke into relay power suply. Inductive load, as relay coil, can cause high glitches and affect,for example, the oscilator,.
2022-02-03 04:35 AM
Using an external supply does not mean that it's well layout and buffered on the PCB, see what ONadr.1 wrote.
So better grab a scope and check the power supply on the board, at several points, especially close to the MCU.
2022-02-03 04:54 AM
The problem is 100% in hardware (scheme, PCB) design. So read this first for example: https://resources.altium.com/p/using-flyback-diodes-relays-prevents-electrical-noise-your-http://www.fordemc.com/docs/download/EMC%20Design%20Guide%20for%20PCB.pdf
and ... a lot of other about EMC and EMI.
2022-02-03 10:34 AM
Suggest you post schematic of circuits for Relay, and STM32 Power and control of relay.
.
Include the schematic.
Paul