Hardware CAN initialization fails

Working libraries, libraries being ported and related hardware
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Hardware CAN initialization fails

Post by fpiSTM »

@Phono
could you share your project, please ?
Phono
Posts: 68
Joined: Thu Mar 19, 2020 9:32 am

Re: Hardware CAN initialization fails

Post by Phono »

I said earlier that the behaviour of my compiler had changed after a Windows 10 update. This happened again last night. Yesterday, I mentioned five errors. This morning, there is only one. It looks like there is some problem with my computer. Before writing this answer, I rebooted the computer, performed a cleanup with Glary Utilities, and rebooted again. The problem did not change.
I attach the zipped directory containing the project and my own libraries. The project also requires the LiquidCrystal library and the IWatchdog library.
Thank you for having a look at it.
Attachments
SketchesPhono.zip
(13.72 KiB) Downloaded 286 times
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Hardware CAN initialization fails

Post by fpiSTM »

OK I understood why you got issue.

You didn't enable the HAL CAN module.
In fact, include the stm32f1xx_hal_can.h is not enough to get HAL CAN support.
When the stm32f1xx_hal_can.c is compile if the HAL_CAN_MODULE_ENABLED is not set then they are no functions defined.

So what you have to do:
  1. Remove the

    Code: Select all

    #include "stm32F1xx_hal_can.h"
    in IndicateurLCDJauges7
    Note the the correct syntax is "stm32f1xx_hal_can.h", like windows is not case sensitive it worked but on Linux this would not work ;)
  • Add a file named "hal_conf_extra.h" with this inside:

    Code: Select all

    #define HAL_CAN_MODULE_ENABLED
Note that there is some other warnings you can fix (if you don't see them, I advise to enable all the compiler warnings in the preferences)
- Avoid {0} to init structure variable, use {} instead.
- for unused variable arguments, you can do this:

Code: Select all

UNUSED(hcan);
Phono
Posts: 68
Joined: Thu Mar 19, 2020 9:32 am

Re: Hardware CAN initialization fails

Post by Phono »

@fpiSTM
Thanks a lot! It works now. Maybe I overlooked the important information about HAL. Nevertheless, I feel that the STM32Duino documentation is too succinct to start with.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Hardware CAN initialization fails

Post by fpiSTM »

Phono wrote: Wed Apr 15, 2020 1:23 pm @fpiSTM
Thanks a lot! It works now. Maybe I overlooked the important information about HAL. Nevertheless, I feel that the STM32Duino documentation is too succinct to start with.
De rien ;)

Yes, documentation need to be extended.
About HAL usage:
https://github.com/stm32duino/wiki/wiki ... ion--150-1
Phono
Posts: 68
Joined: Thu Mar 19, 2020 9:32 am

Re: Hardware CAN initialization fails

Post by Phono »

Moving forward with my CAN driver, I completed the implementation and had it work perfectly as far as handling CAN frames back and forth.
Reminder : I use a Maple-like board, namely Olimexino STM32 which includes all the CAN circuitry to connect to a CAN bus, and a USB socket for programming.
Since on the STM32F103 the CAN and the USB cannot coexist, I compiled my project using the option "CDC disabled". If I enable it, there is a conflict at the ISR declaration since the CAN fifo receive and the USB share the same IRQ vector.
PROBLEM:
I disconnected the USB that had served to program the board through the Maple bootloader. The board was still powered through its power jack.
And then, the application crashed.
I traced the problem down to the ISR. Even with no incoming CAN traffic, at the time the USB is disconnected, the USB_LP_CAN1_RX0_iRQHandler interrupt is fired. Since the interrupt is not generated by the CAN hardware, the HAL_CAN_IRQ_Handler does not call my HAL_CAN_RxFifo0MsgPendingCallback function. The application then crashes.
Since I selected a disabled CDC, how could the USB interrupt be active?
The HAL manual does not seem to include the USB. How can I mask the USB interrupt out?
NOTE: I am wondering whether the bootloader, which was active at start time, forgot to mask the USB interrupt out when it passes control to the sketch code?
NOTE2: This does not happen with Roger's core.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Hardware CAN initialization fails

Post by fpiSTM »

When USB is disabled there is no USB code compiled.
So propbably the bootloader configure the USB and with Roger core, this is handled to disable it properly.
Phono
Posts: 68
Joined: Thu Mar 19, 2020 9:32 am

Re: Hardware CAN initialization fails

Post by Phono »

This is more or less what I suspect. Now, how can I shut off the USB or at least disable the USB interrupt ? The HAL API is somewhat complicated, and to shut it off one must have a handler to pass to the DeInit or Stop function.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Hardware CAN initialization fails

Post by fpiSTM »

This is the same IRQn so if you disable it you will also disable for CAN.

I guess you have an other issue but hard to tell which one.
Phono
Posts: 68
Joined: Thu Mar 19, 2020 9:32 am

Re: Hardware CAN initialization fails

Post by Phono »

After reading the STM32 manual, I found how to stop the USB hardware. It works now. So I conclude that the bootstrap loader, when it jumps to the application program, does not shut off the USB, and this must have been done in the core (Roger's version). It is not the case anymore. So the CAN driver must take care of it. Which i did by calling at the beginning of the CAN initialisation function the following function:

Code: Select all

void InhibitUSB ( void )
{
	*((uint32_t*)0x40005c40) = 3 ;		// USB_CNTR
	*((uint32_t*)0x40005c44) = 0 ;		// USB_ISTR
	
	__HAL_RCC_USB_CLK_DISABLE();
}
It sets the control register to the reset value, clears any interrupt request active, and stops the USB clock.
Porting an application to a new environment if full of traps!
In any case, @fpiSTM, thanks for your help and bonjour d'Ermont 95.
Post Reply

Return to “Libraries & Hardware”