Page 1 of 1

configuring CAN and clock for STM32F

Posted: Thu Mar 12, 2015 10:38 am
by enrico.dallavia
Dear all,
I'm stuck with the CAN bus, I have a board STM32F405 I succesfully managed to assign the PIN remap for using portB CAN2 Tx and RX. I found and example about how to initialize the bus, but with an oscilloscope I only have on the TX a sequence of 7 one (no CAN ID, nor packet data).
I have an external quartz of 12Mhz so I changed the config like below
#define STM32F4xx_MCUCONF

/*
* HAL driver system settings.
*/
#define STM32_NO_INIT FALSE
#define STM32_HSI_ENABLED TRUE
#define STM32_LSI_ENABLED TRUE
#define STM32_HSE_ENABLED TRUE
#define STM32_LSE_ENABLED FALSE
#define STM32_CLOCK48_REQUIRED TRUE
#define STM32_SW STM32_SW_PLL
#define STM32_PLLSRC STM32_PLLSRC_HSE
#define STM32_PLLM_VALUE 12
#define STM32_PLLN_VALUE 336
#define STM32_PLLP_VALUE 4
#define STM32_PLLQ_VALUE 7
#define STM32_HPRE STM32_HPRE_DIV1
#define STM32_PPRE1 STM32_PPRE1_DIV2 //
#define STM32_PPRE2 STM32_PPRE2_DIV1 //
#define STM32_RTCSEL STM32_RTCSEL_LSI
#define STM32_RTCPRE_VALUE 8
#define STM32_MCO1SEL STM32_MCO1SEL_HSI
#define STM32_MCO1PRE STM32_MCO1PRE_DIV1
#define STM32_MCO2SEL STM32_MCO2SEL_SYSCLK
#define STM32_MCO2PRE STM32_MCO2PRE_DIV5
#define STM32_I2SSRC STM32_I2SSRC_CKIN
#define STM32_PLLI2SN_VALUE 192
#define STM32_PLLI2SR_VALUE 2 //
#define STM32_PVD_ENABLE FALSE
#define STM32_PLS STM32_PLS_LEV0
#define STM32_BKPRAM_ENABLE FALSE


#define STM32_CAN_USE_CAN1 TRUE
#define STM32_CAN_USE_CAN2 TRUE

//those settings must achieves 500000 bps with a bus of 42Mhz
static const CANConfig cancfg = {
/*.mcr = */ CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
/*.btr = */ CAN_BTR_SJW(0) | CAN_BTR_TS2(1) | CAN_BTR_LBKM |
CAN_BTR_TS1(8) | CAN_BTR_BRP(6)
};

canStart(&CAND2, &cancfg); //ruft can_lld_start(..) auf

static CANTxFrame txmsg;
while(1)
{

txmsg.IDE = CAN_IDE_STD;

txmsg.EID = 0x755;
txmsg.RTR = CAN_RTR_DATA;
txmsg.DLC = 8;

txmsg.data32[0] = 0x0;//velocità
txmsg.data32[1] = 0xFFFFFFFF;

msg_t rc = canTransmit(&CAND2, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
chprintf((BaseSequentialStream*)&SD1, "sending CAN msg returned rc: %d, can state: %d, tx_empty_before: %d\n\r",
rc, CAND2.state, tx_empty_before);
}

The result of the function gave always rc = -1, CAND2.state = 3 and[code][/code] tx_empty_before = 0

Is there any function to check the status of CAN_MSR register?

Many thanks to whatever person who will help me.

I successfully managed to read from two SPI and send the data with the UART, the only thing I'm lacking is the CAN :(

Enrico

Re: configuring CAN and clock for STM32F

Posted: Thu Mar 12, 2015 12:18 pm
by Giovanni
You are getting -1 (MSG_RESET) from the transmit functions, this means that the driver was stopped while still attempting to transmit.

Do you have a canStop() somewhere?

Giovanni

Re: configuring CAN and clock for STM32F

Posted: Fri Mar 13, 2015 12:24 pm
by enrico.dallavia
Hi, thank you very much for the quick reply.
Indeed the problem was more insidious, because to be able to use CAN2 I had to start CAN1 (even if I don't use it at all, I don't have the pin remapped either!)

All works ok afte I add the
canObjectInit(&CAND1);//
canObjectInit(&CAND2);//anche questo dovrebbe essere chiamato in automatico dentro canInit();
canStart(&CAND1, &cancfg); //OBBLIGATORIO METTERLO anche se non è usato dall'anemometro, altrimenti CAND2 non funziona!!!
canStart(&CAND2, &cancfg); //ruft can_lld_start(..) auf

Re: configuring CAN and clock for STM32F

Posted: Fri Mar 13, 2015 12:29 pm
by Giovanni
I can see why you need to start both, it is an STM32 quirk, but the object initializations should not be required, those are done internally.

Giovanni

Re: configuring CAN and clock for STM32F

Posted: Fri Mar 13, 2015 5:49 pm
by enrico.dallavia
Yes I know that there is no need to do the canObjectInit. (I began to edit the code but have pushed the post by mistake, there are even the comments in italian).
Anyway, thanks a lot for the help. I really appreciate the help chibios gave to me while working with the STM32 board.

Re: configuring CAN and clock for STM32F

Posted: Tue Mar 17, 2015 11:16 am
by enrico.dallavia
Hello,
I'm in big trouble, I can't understand why now I have not any data from CAN.
If I avoid to to the canStart(&CAND1, &cancfg); and canStart(&CAND2, &cancfg); the function msg_t rc = canTransmit(&CAND2, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
return always 0, when few days ago it return -1.

Is it possible??

Thanks

Re: configuring CAN and clock for STM32F

Posted: Tue Mar 17, 2015 11:33 am
by Giovanni
You should enable the debug options in chconf.h (checks, assertions, state checker), that would stop execution at any error. Not starting a device is an obvious error.

Giovanni