cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to get CAN2 working.

temp2010
Senior

I'm using the STM32F105R8T6, STM32Cube_FW_F1_V1.8.0, and Atollic TrueSTUDIO 9.3.0.

I have CAN1 working already, and am now trying to get CAN2 working *instead*. That is, I'm swapping over from CAN1-only to CAN2-only, as an interim step before trying to get both working simultaneously. For now, I'm only interested in RX, receiving CAN signals from a CAN generator I have.

However, CAN2-only isn't working. I *believe* the only difference in my code should be isolated to the following snippet in main.h. Please look at this and tell me if I'm missing anything, or if there's something elsewhere that I much change. The "#if 0" clause is the code that works for CAN1-only when includes via "#if 1" edit. The "#else" clause is the intend CAN2-only code. In the hardware, of course, I'm moving my connector from CAN1 to CAN2, and I've confirmed the RX signal with a scope. I'm using PB8/PB9 for remapped CAN1_RXD/CAN1_TXD, but I'm using PB12/PB13 for default CAN2_RXD/CAN2_TXD. Be aware that I've simply deduced what must be changed merely by looking at the code, since I can't find any detailed doc anywhere.

#if 0
	/* Definition for CANx clock resources */
	#define CANx                           CAN1
	#define CANx_CLK_ENABLE()              __HAL_RCC_CAN1_CLK_ENABLE()
	#define CANx_GPIO_CLK_ENABLE()         __HAL_RCC_GPIOB_CLK_ENABLE()
 
	#define CANx_FORCE_RESET()             __HAL_RCC_CAN1_FORCE_RESET()
	#define CANx_RELEASE_RESET()           __HAL_RCC_CAN1_RELEASE_RESET()
 
	/* Definition for CANx Pins */
	#define CANx_TX_PIN                    GPIO_PIN_9
	#define CANx_TX_GPIO_PORT              GPIOB
	#define CANx_RX_PIN                    GPIO_PIN_8
	#define CANx_RX_GPIO_PORT              GPIOB
 
	/* Definition for CANx AFIO Remap */
	#define CANx_AFIO_REMAP_CLK_ENABLE()   __HAL_RCC_AFIO_CLK_ENABLE()
	#define CANx_AFIO_REMAP_RX_TX_PIN()    __HAL_AFIO_REMAP_CAN1_2()
 
	/* Definition for CAN's NVIC */
	#define CANx_RX_IRQn                   USB_LP_CAN1_RX0_IRQn
	#define CANx_RX_IRQHandler             USB_LP_CAN1_RX0_IRQHandler
#else
	/* Definition for CANx clock resources */
	#define CANx                           CAN2
	#define CANx_CLK_ENABLE()              __HAL_RCC_CAN2_CLK_ENABLE()
	#define CANx_GPIO_CLK_ENABLE()         __HAL_RCC_GPIOB_CLK_ENABLE()
 
	#define CANx_FORCE_RESET()             __HAL_RCC_CAN2_FORCE_RESET()
	#define CANx_RELEASE_RESET()           __HAL_RCC_CAN2_RELEASE_RESET()
 
	/* Definition for CANx Pins */
	#define CANx_TX_PIN                    GPIO_PIN_13
	#define CANx_TX_GPIO_PORT              GPIOB
	#define CANx_RX_PIN                    GPIO_PIN_12
	#define CANx_RX_GPIO_PORT              GPIOB
 
	/* Definition for CANx AFIO Remap */
	#define CANx_AFIO_REMAP_CLK_ENABLE()   __HAL_RCC_AFIO_CLK_ENABLE()
	#define CANx_AFIO_REMAP_RX_TX_PIN()    __HAL_AFIO_REMAP_CAN2_DISABLE()
 
	/* Definition for CAN's NVIC */
	#define CANx_RX_IRQn                   CAN2_RX0_IRQn
	#define CANx_RX_IRQHandler             CAN2_RX0_IRQHandler
#endif

4 REPLIES 4

Not 2x Full CAN implementations, basically 1.5x hardware (shared resources), CAN1 must clock for CAN2 to slave off it

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
temp2010
Senior

Thanks.

So far:

#define CANx_CLK_ENABLE()       {__HAL_RCC_CAN1_CLK_ENABLE();__HAL_RCC_CAN2_CLK_ENABLE();}

the above wasn't a simple fix. I'll have to research more or hope for the best...

Ben K
Senior III

The CAN1 peripheral owns all CAN receive filters of the chip, so to receive anything on CAN2 you need to assign some filters to CAN2 RX, and configure them, in the CAN1 peripheral registers.

temp2010
Senior

Thanks, Ben.

I had been working on CAN2 by itself, but I wasn't getting anywhere. My final product must use BOTH the CAN1 *and* CAN2. Therefore I have changed strategies to trying to get BOTH working.

So, right now, CAN1 continues to work. I have looked at all the initialization done for CAN1, and reproduced what I believe are appropriate portions. So now I should hopefully be initializing BOTH the CAN1 *and* CAN2.

However, when I move my CAN generator connection from CAN1 to CAN2, my logging (receiving) stops. There's also an interrupt function CANx_RX_IRQHandler() that after seeing a #define for CANx_RX_IRQHandler, is actually USB_LP_CAN1_RX0_IRQHandler(). (Well, truly, there are more #defines and it ends up being some other function name.) If I set a breakpoint in this function, debugging successfully stops there. So this interrupt really is happening (while my CAN generator is attached). COMPARABLE to that, I have defined CANy_RX_IRQHandler(), which is really CAN2_RX0_IRQHandler(). When I set a breakpoint there and connect my CAN generator to CAN2, I do ***not*** stop at the breakpoint. So this interrupt is ***not*** happening. I don't know if it's the wrong interrupt for CAN2, or if I'm not successfully enabling the interrupt or something else associated with CAN2.

I will attach a bunch of code*. Since "CANx" was being defined as "CAN1" in main.h, from the cube CAN_Networking example, I searched and found that the only CANx references were in main.h, main.c, stm32f1xx_hal_msp.c, and stm32f1xx_it.c, all of which were local source to the project. Therefore I defined a new "CANy" in main.h, and made appropriate changes elsewhere to try and initialize BOTH the CANx and CANy, using CanHandle and CanHandleY structures.

Any obvious problems, perhaps? (I don't claim it to be clean, but I think it *should* have functioned...)

(((I tried to post only germane snippets, but forum said post was too long. I tried to upload individual files, but I can only seem to upload a single file. Therefore I just uploaded full zip of project. Please see the source files I mentioned earlier, and look for "CANx" or "CANy". Thanks!)))

Since it's Thanksgiving, I must stop work for this morning. But I am going to try again later this week(end).