cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f405 clock synchronize

Altamash Abdul Rahim
Associate II

Hello,

I'm trying to use two identical stm32f4 boards to synchronize tick of one with respect to other.

I'm using can to using can protocol for communication in Interrupt mode.

So the Master is supposed to be always running and the slave when powers up send a sync request to master, the master receives the message in interrupt and send the current tick to the slave from the callback. The slave receives the tick which is used as an offset to the current slave tick for delay purposes .

NOW after this transaction both the MCU start to toggle the LED at the same time. (I have verified the waveform on oscilloscope).

But after some time(30 min) a lag starts to appear between them(80ms) which keeps on increasing. For both MCU i'm using 24Mhz external oscillator and the MCU are running at 80Mhz.

Does anybody has any Idea what's the cause between this increasing lag. I know these kind of things occur due to clock drift but that's why i'm using an external crystal.

Thanks,

Altamash Abdul Rahim

while(1)
{	
       uint32_t currentTick = 0;
	currentTick = HAL_GetTick();
#if SLAVE
	currentTick += tick;
#endif
	if(blinkLed) 
/* Enabled in callback of both MASTER{when sending value to SLAVE} & SLAVE{when receiving tick from MASTER}*/
	{
		if(currentTick - lastTick >= 2000) 
                /*For MASTER lastTick = HAL_GetTick() when sending tick
                   For SLAVE lastTick = tick received from master   
               */
		{
			print_string("Tick %u\n", (unsigned int)currentTick);
			HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_12);
			lastTick = currentTick;
		}
	}
}

6 REPLIES 6
S.Ma
Principal

Any interrupt potentially lasting over 1 msec in duration? (CAN transmission all within the same interrupt/callback?)

Hello,

I have a timer Interrupt triggering every 1ms executing the following code. I did watch the tick at #line:2 and #line:6 it was the same.

During sync a message is sent from callback by MASTER and SLAVE respectively.

After Sync not CAN Interrupt is invoked, Only Timer and Systick interrupts occur periodically and the while loop executes.

Thanks,

Altamash Abdul Rahim

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	TL_process(J1939_rx_pdu);
	TP_tx_Process(J1939_tx_state_machine,J1939_rx_pdu,TxMsg);
	TL_periodic(J1939_rx_state_machine, J1939_rx_pdu, J1939_rx_message);
}

Piranha
Chief II

I know these kind of things occur due to clock drift but that's why i'm using an external crystal.

Well, in my universe there are no absolutely ideal parts and everything has some precision level. Crystals have it written in datasheets as a ppm of tolerance and stability.

S.Ma
Principal

Take an STM32 board with RTC 32768Hz external crystal. This one is bult for precision which we can compare with current implementation.

USB device without drifting clock for example use the SOF USB transmitted event to resync regularly.

Hello,

I am thinking of supplying the SLAVE with reference clock from MASTER so that it could be estabilished that the issue is solely due to clock drift, Although i wont be able to this would not help in achieving my goal but then i look at other options like continuous re-sync etc.

Thanks

Altamash Abdul Rahim

Hello,

yeah i agree with you it's just that i have never experienced this issue before and hoped that using an external oscillator would have reduced the clock drift to a bigger extent if not avoided it completely.

Thanks

Altamash Abdul Rahim