cancel
Showing results for 
Search instead for 
Did you mean: 

When CANBUS interrupt is activated, then USB connection does not work - Help with priorities

DMårt
Lead

Hi!

I don't know really how to explain this. But I have enabled CANBUS and it works very good. I also have activated USB and it works too.

But since I have changed the preemption from 0 to 1 for CANBUS RX0, I have no ability to use USB connection.

I changed the preemption from 0 to 1 for CANBUS RX0 because else SysTick would stop. HAL_GetTick() gave the same number for ever as long I was inside the CANBUS RX interrupt function.

What happen when I have 1 for CANBUS RX0 in priority and plug in the USB. From my computers perspective, it says that it could not connect the device.

From my STM32 perspective, I get stuck inside this while loop for ever where wIstr = 41465

/**
  * @brief  This function handles PCD Endpoint interrupt request.
  * @param  hpcd PCD handle
  * @retval HAL status
  */
static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
{
  PCD_EPTypeDef *ep;
  uint16_t count, wIstr, wEPVal, TxByteNbre;
  uint8_t epindex;
 
  /* stay in loop while pending interrupts */
  while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
  {
    wIstr = hpcd->Instance->ISTR;
 
    /* extract highest priority endpoint number */
    epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
 
    if (epindex == 0U)

I activate the CANBUS interrupt with this code. I'm trying out the J1939 CANBUS library https://github.com/DanielMartensson/Open-SAE-J1939

/* Interrupt handler that read message */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
	Open_SAE_J1939_Listen_For_Messages(j1939_handler);
}
 
 
static void Create_CAN_Interrupt(CAN_HandleTypeDef *hcan) {
	/* Don't forget to check NVIC in CAN -> NVIC Settings -> CAN RX0 interrupt */
	if (HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
			Error_Handler();
}

Question:

What I acctully want help with is that HAL_GetTick() did not work with CANBUS RX0 priority 0, so I changed the priority to 1. So now HAL_GetTick() works inside the CANBUS RX interrupt function above.

But then USB won't work because I cannot connect it if the CANBUS RX0 priority is 1. So I need to set it to 0 again, but then I'm facing the same issue as before.

So what should I do here?

  1. Should I remove the HAL_GetTick() function from my CANBUS RX0 interrupt function and change the priority from 1 to 0 again?
  2. Should I change the priority for USB? (I have tried that, did not work)
  3. Should I change SysTick to a timer? Because HAL_GetTick() using SysTick...I assume that.
  4. Other solutions?

What do you think?

My STM32 is STM32F373VBTx and I'm using STM32CubeIDE 1.7.0.

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-Computer
1 ACCEPTED SOLUTION

Accepted Solutions

Hello @Daniel Mårtensson​ ,

In order to be able to use HAL_GetTick() proprely (HAL relies on it) within an interrupt, SysTick must have higher priority than the interrupt.

Therefore, setting the SysTick's priority to the highest level would in theory allow for it use anywhere.

Unfortunately, and according to the application, setting SysTick to the highest priority might lead to reactivity loss for other events.

As a rule of thumb, waiting for an event within an ISR or doing a time consuming task there isn't advised at all.

Going with option 1 is the most logical thing to do. Option 2 should work in theory. Option 3 would change nothing.

I hope this helps you.

BeST Regards,

Walid

View solution in original post

3 REPLIES 3

Hello @Daniel Mårtensson​ ,

In order to be able to use HAL_GetTick() proprely (HAL relies on it) within an interrupt, SysTick must have higher priority than the interrupt.

Therefore, setting the SysTick's priority to the highest level would in theory allow for it use anywhere.

Unfortunately, and according to the application, setting SysTick to the highest priority might lead to reactivity loss for other events.

As a rule of thumb, waiting for an event within an ISR or doing a time consuming task there isn't advised at all.

Going with option 1 is the most logical thing to do. Option 2 should work in theory. Option 3 would change nothing.

I hope this helps you.

BeST Regards,

Walid

Thank you. I solved this issue by not using delays in an interrupt.

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-Computer

Glad to hear that :smiling_face_with_smiling_eyes: Thanks for your feedback.