cancel
Showing results for 
Search instead for 
Did you mean: 

Troubles setting up the CAN

alexzapf9
Associate II
Posted on March 24, 2012 at 11:39

Hi,

I'm having troubles setting up the CAN bus on my STM32F103R4. I'm using the can example from the keil homepage . When it is about to leave initialization mode, hardware doesnt reset the INAK bit in MSR register and I really don't know why.

Here's the relevant slice of the code:

void

CAN_setup (

void

) {

unsigned

int

brp = stm32_GetPCLK1(); RCC->APB1ENR |= RCC_APB1ENR_CANEN;

// enable clock for CAN

// Note: MCBSTM32 uses PB8 and PB9 for CAN

RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;

// enable clock for Alternate Function

AFIO->MAPR &=

0xFFFF9FFF

;

// reset CAN remap

AFIO->MAPR |=

0x00004000

;

// set CAN remap, use PB8, PB9

RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;

// enable clock for GPIO B

GPIOB->CRH &= ~(

0x0F

<<

0

); GPIOB->CRH |= (

0x08

<<

0

);

// CAN RX pin PB.8 input push pull

GPIOB->CRH &= ~(

0x0F

<<

4

); GPIOB->CRH |= (

0x0B

<<

4

);

// CAN TX pin PB.9 alternate output push pull

NVIC->ISER[

0

] |= (

1

<< (USB_HP_CAN_TX_IRQChannel &

0x1F

));

// enable interrupt

NVIC->ISER[

0

] |= (

1

<< (USB_LP_CAN_RX0_IRQChannel &

0x1F

));

// enable interrupt

CAN->MCR = (CAN_MCR_NART | CAN_MCR_INRQ);

// init mode, disable auto. retransmission

// Note: only FIFO 0, transmit mailbox 0 used

CAN->IER = (CAN_IER_FMPIE0 | CAN_IER_TMEIE);

// FIFO 0 msg pending, Transmit mbx empty

/* Note: this calculations fit for PCLK1 = 36MHz */

brp = (brp /

18

) /

500000

;

// baudrate is set to 500k bit/s

/* set BTR register so that sample point is at about 72% bit time from bit start */

/* TSEG1 = 12, TSEG2 = 5, SJW = 4 => 1 CAN bit = 18 TQ, sample at 72% */

CAN->BTR &= ~(((

0x03

) <<

24

) | ((

0x07

) <<

20

) | ((

0x0F

) <<

16

) | (

0x1FF

)); CAN->BTR |= ((((

4

-

1

) &

0x03

) <<

24

) | (((

5

-

1

) &

0x07

) <<

20

) | (((

12

-

1

) &

0x0F

) <<

16

) | ((brp-

1

) &

0x1FF

)); }

void

CAN_testmode (

unsigned

int

testmode) { CAN->BTR &= ~(CAN_BTR_SILM | CAN_BTR_LBKM);

// set testmode

CAN->BTR |= (testmode & (CAN_BTR_SILM | CAN_BTR_LBKM)); }

void

CAN_start (

void

) { CAN->MCR &= ~CAN_MCR_INRQ;

// normal operating mode, reset INRQ

while

(CAN->MSR & CAN_MCR_INRQ); } I hope somebody knows how to fix that!

Regards.

Alex
0 REPLIES 0