How to set up CAN correctly on an STM32F4?

Jonte_Kaletka
Thu, 01 Dec 2022 12:41:01 +0000
Boards

Hello guys,

right I am (trying to) set up CAN for my first time. I am not exactly using mbed by I figures I could get help here.
I am programming an STM32F429 on an Nucleo F429ZI using the STM32CubeIDE. I want to program it using the CMSIS.
I am pretty confident that I have programmed it (mostly of course) correct.

void intCAN()
{
	RCC->APB1ENR |= RCC_APB1ENR_CAN1EN;
	(void) RCC->APB1ENR;
	RCC->CFGR |= RCC_CFGR_PPRE1_0|RCC_CFGR_PPRE1_2;		//clock prescaler
	CAN1->MCR |= CAN_MCR_RESET;
	CAN1->MCR &= ~(CAN_MCR_SLEEP);
	CAN1->MCR |= CAN_MCR_INRQ;		//Initilization Request

//	CAN1->MCR &= ~(CAN_MCR_DBF);
	CAN1->BTR = 0;
	CAN1->BTR |= CAN_BTR_LBKM;		//Loop Back Mode
	CAN1->BTR |= (0x4 << CAN_BTR_BRP_Pos);			//BRP = 4
	CAN1->BTR |= CAN_BTR_TS2_0|CAN_BTR_TS2_2;		//Seg 2 = 5 ALSO 6
	CAN1->BTR |= CAN_BTR_TS1_0|CAN_BTR_TS1_2|CAN_BTR_TS1_3;	//Seg 1 = 13 ALSO 14
//	CAN1->BTR |= CAN_BTR_TS1_1;	//Seg 1 = 16
	CAN1->BTR |= CAN_BTR_SJW_0;		//Small Jump Width = 1

	CAN1->MCR &= ~(CAN_MCR_INRQ);				//exit initilization mode

	CAN1->sTxMailBox[0].TIR &= ~CAN_TI0R_RTR;
	CAN1->sTxMailBox[0].TIR &= ~CAN_TI0R_IDE;		//Standard Identifier
	CAN1->sTxMailBox[0].TIR &= ~CAN_TI0R_STID;
	CAN1->sTxMailBox[0].TIR |= (0xFF << CAN_TI0R_STID_Pos);				//Message ID = 1

	//message
//	CAN1->sTxMailBox[0].TDTR &= ~CAN_TDT0R_TGT;
	CAN1->sTxMailBox[0].TDTR &= ~CAN_TDT0R_DLC;
	CAN1->sTxMailBox[0].TDTR |= (0x1 << CAN_TDT0R_DLC_Pos);			//Data Length = 1 Byte
}

I send the CAN message using:

	CAN1->sTxMailBox[0].TDLR = 0;
	CAN1->sTxMailBox[0].TDHR = 0;

	CAN1->sTxMailBox[0].TDLR |= 0xFFF;			//Daten DATA0 = 1000

	for (int i = 0; i < 1000000; i++)
	{
	}

	CAN1->sTxMailBox[0].TIR |= CAN_TI0R_TXRQ;	//send mailbox

When I run this, I this in an Logic Analyser:

It is something but I am pretty sure that this is not an CAN message.
The Logic analyser is connected to the TX Pin on the nucleo board.
When I change the bit timing, the output changes slightly, and when I change the content of the CAN massage, e.g. the data, nothing happens.

Does anyone have an idea would I might have messed up, or know where I could get help?

Thank you in advance
Jeyeffkay

13 posts - 2 participants