How to control LEDs when CAN stops incoming

Universal CAN Converter
Locked
rixccaptv
Posts: 11
Joined: Wed 14. Apr 2021, 03:23

How to control LEDs when CAN stops incoming

Post by rixccaptv » Thu 9. Dec 2021, 09:40

The intended LED behavior is:

1) [CAN1-LED red , CAN2-LED off] when power ON but not yet receiving anything.
2) [CAN1-LED green, CAN2-LED blinking] CAN1 is receiving msgs (and CAN2 is outputting)
3) [CAN1-LED red , CAN2-LED off] when CAN1 stops receiving msgs (but power remains ON)

Steps 1 and 2 are working, but step 3 is not happening.
What is happening instead is that when I stop transmission to CAN1 the LEDs just freeze and keep their last state (whatever that was).

I don't understand why, since I expect the program to keep looping so timers keep increasing, and the LED check is still done every 500ms.

Here are the relevant code snippets:

Code: Select all

int  main ( void)
{
	// init hardware
	HW_Init();
	
	// init CAN
	CAN_UserInit();
	
	// init timer
	SYSTIME_Init();
	
	// Set red LEDs for CAN1 and CAN2
	HW_SetLED ( HW_LED_CAN1, HW_LED_RED);
	HW_SetLED ( HW_LED_CAN2, HW_LED_OFF);
	
	LED_awake = 0;
	LastCycleMsg = 0;
	timenow = 0;
	interval_count_500ms = 0;
	
	// main loop
	while (1)
	{
		CANMsg_t  RxMsg;
		
		// process messages from CAN1: VCAN input
		if (CAN_UserRead (CAN_BUS1, &RxMsg) != 0)
		{
			// wake up LEDs
			LED_awake = 1;
		}

		// process messages from CAN2: private VCAN
		if (CAN_UserRead (CAN_BUS2, &RxMsg) != 0)
		{
			// do nothing
		}
		
		// timer increments every 10ms
		timenow = SYSTIME_NOW;
		if (SYSTIME_DIFF (LastCycleMsg, timenow) >= 10000)
		{
			LastCycleMsg += 10000;
			interval_count_500ms += 1;
		}

		// periodic actions
		if (interval_count_500ms >= 50)
		{
			// toggle LEDs if message received on CAN1
			if (LED_awake)
			{
				LED_toggleCAN ^= 1;
				if (LED_toggleCAN)
				{
					HW_SetLED (HW_LED_CAN1, HW_LED_GREEN);
					HW_SetLED (HW_LED_CAN2, HW_LED_OFF);
				}
				else
				{
					HW_SetLED (HW_LED_CAN1, HW_LED_OFF);
					if (outputHonda_enabled) // CAN2 LED color indicates which output is being sent.
					{
						HW_SetLED (HW_LED_CAN2, HW_LED_ORANGE);
					}
					else
					{
						HW_SetLED (HW_LED_CAN2, HW_LED_RED);
					}
				}
			}
			else
			{
				HW_SetLED (HW_LED_CAN2, HW_LED_RED); // not working...?
				HW_SetLED (HW_LED_CAN2, HW_LED_OFF);
			}
			LED_awake = 0;

			// reset counter
			interval_count_500ms = 0;
		}
	}
}
Is the execution actually stopping (not looping anymore) when I stop CAN transmission, or is there something else I might have wrong?

rixccaptv
Posts: 11
Joined: Wed 14. Apr 2021, 03:23

Re: How to control LEDs when CAN stops incoming

Post by rixccaptv » Thu 9. Dec 2021, 09:48

Sorry, false alarm again...

Please close this thread.

When reading my post I discovered the issue (it's really silly).
At the end I just had HW_LED_CAN2 twice instead of 1 and 2.

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: How to control LEDs when CAN stops incoming

Post by M.Heidemann » Thu 9. Dec 2021, 10:41

Hello,

Thanks for the feedback.

- Closed -
---
Marvin Heidemann
PEAK-Support Team

Locked