2012-12-04 09:48 AM
Dear all,
I am looking forward solution of my problem. I have successfully configured CAN driver in loopback mode, but when I want to connect to physical layer (MCP2551 I/C), there is no output on pins.#define CANx CAN1
#define CAN_FILTER_NMR 0#define CAN_CLK RCC_APB1Periph_CAN1#define CAN_RX_PIN GPIO_Pin_0#define CAN_TX_PIN GPIO_Pin_1#define CAN_GPIO_PORT GPIOB#define CAN_GPIO_CLK RCC_AHB1Periph_GPIOD#define CAN_AF_PORT GPIO_AF_CAN1#define CAN_RX_SOURCE GPIO_PinSource0#define CAN_TX_SOURCE GPIO_PinSource1/* Enable the CAN controller interface clock */ RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE); RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE); /* CAN pins configuration */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push - pull GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT); /* Initialisation of CAN */ CAN_DeInit(CANx); CAN_InitTypeDef CAN_InitStructure; CAN_StructInit(&CAN_InitStructure); CAN_InitStructure.CAN_TTCM = DISABLE; CAN_InitStructure.CAN_ABOM = ENABLE; // Automatic bus off managment CAN_InitStructure.CAN_AWUM = DISABLE; CAN_InitStructure.CAN_NART = DISABLE; CAN_InitStructure.CAN_RFLM = DISABLE; CAN_InitStructure.CAN_TXFP = ENABLE; CAN_InitStructure.CAN_Prescaler = 10; // 500 kbps for 42 MHz, CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_1tq;#ifdef CAN_LOOPBACK CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;#else CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;#endif uint8_t status = CAN_Init(CANx, &CAN_InitStructure);I don't know, where the problem may be. I have tested output with oscilloscope and there is no change on the wires.And the second question - can I connect RX and TX pin to simulate LoopBack mode? (LoopBack mode works fine, but I want to check output). I'm using USB CDC driver, but I have already disabled it and there is no change.This is my first experience with CAN bus, but I'm desperate from it for 2 weeks.2012-12-04 10:01 AM
So is it B or D?
#define CAN_GPIO_PORT GPIOB #define CAN_GPIO_CLK RCC_AHB1Periph_GPIOD2012-12-04 10:27 AM
This is only copy-paste error. In code I have D in both cases. I tested CAN1 and CAN2 too, but without any progress.
2012-12-04 11:34 AM
The problem is in transmitting. After 3 packets it causes an NoMailbox error.
2012-12-06 11:15 PM
So I have found solution of the problem. The problem was in timing. I have go throw these points:
1) I have checked CAN1 error code register (there was ACK problem)2) I have switch CAN to loopback mode and check output on Tx PIN and on the CAN bus (there was rectangle signal)3) I have with brute force find settings to communicate with the device#ifdef NO_USB
pres=12;
sjw=0;
bs1=7;
bs2=4;
STM_EVAL_LEDInit(LED6);
CAN_SetParameters(pres, sjw, bs1, bs2);
CAN_SetFilter();
#endif
while (1)
{
#ifdef NO_USB
Delay(100);
CanTxMsg msg;
msg.StdId = 0x080;
msg.ExtId = 0;
msg.IDE = CAN_ID_STD;
msg.RTR = CAN_RTR_DATA;
msg.DLC = 0x04;
msg.Data[0] = 0x00;
msg.Data[1] = 0x00;
msg.Data[2] = 0x07;
msg.Data[3] = 0x00;
if (CAN_Transmit(CANx, &msg) == CAN_TxStatus_NoMailBox)
{
if(++bs2>0x07)
{
bs2=1;
if(++bs1>0x0F)
{
bs1=1;
if(++sjw>0x03)
{
sjw=1;
if(++pres>60)
{
pres++;
}
}
}
}
STM_EVAL_LEDToggle(LED6);
CAN_CancelTransmit(CANx, 0);
CAN_CancelTransmit(CANx, 1);
CAN_CancelTransmit(CANx, 2);
CAN_SetParameters(pres, sjw, bs1, bs2);
CAN_SetFilter();
}
#endif
}
4) I have made table to solve counting comm parameters (the problem was in setting APB1 clk divider to 2, so the frequency of CAN wasn't 42 but 84 MHz. The table is attached to this post.2014-11-24 08:10 AM
2014-11-24 11:29 AM
I wouldn't rely on someone with 4 posts 2 years ago who had to brute force setting because the math was too hard, as an effective source for a solution where the parameters are not identical. Understand how the hardware, and its clocks are derived, and you'll be in a lot stronger position.
[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Stm32f407%20Can%20bus%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx¤tviews=48]cross-post