cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 CAN Transmit doesn't work

vasyl
Associate
Posted on December 15, 2014 at 17:13

Hello! I use STM32F407 Discovery board and want to use CAN1. My IDE is CoocoxIDE.

I've putoscilloscopeon line PD1 (CAN TX) and haven't seen any activity...Discovery CAN works without any CAN IC (CAN Driver). Here is my code:


const
uint8_t TxBuffer[8] = {1,2,3,4,5,6,7,8};


int
main(
void
)

{

CAN1_Initialization();

while
(1)

{


CAN1_DataSend(TxBuffer);

_delay_ms(50);

}

}

and CAN code:


CanTxMsg TxMessage;


void
CAN1_Initialization(
void
)

{

GPIO_InitTypeDef GPIO_InitStructure;

CAN_InitTypeDef CAN_InitStructure;

CAN_FilterInitTypeDef CAN_FilterInitStructure;

//NVIC_InitTypeDef NVIC_InitStructure;


/* Enable GPIO clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


/* Configure CAN 1 RX and TX pins */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOD, &GPIO_InitStructure);


/* Connect CAN 1 pins to AF9 */

GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);

GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);


//CAN_DeInit(CAN1);


/* CAN 1 cell init */

CAN_InitStructure.CAN_Prescaler = 12;

CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;

CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;

CAN_InitStructure.CAN_TTCM = DISABLE;

CAN_InitStructure.CAN_ABOM = DISABLE;

CAN_InitStructure.CAN_AWUM = DISABLE;

CAN_InitStructure.CAN_NART = DISABLE;

CAN_InitStructure.CAN_RFLM = DISABLE;

CAN_InitStructure.CAN_TXFP = DISABLE;

CAN_Init( CAN1, &CAN_InitStructure );


/* CAN1 filter init, accept every message */

CAN_FilterInitStructure.CAN_FilterNumber = 0; 
// 0..13 for CAN1, .27 for CAN2

CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;

CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;

CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;

CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;

CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000 ;

CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;

CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;

CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

CAN_FilterInit(&CAN_FilterInitStructure);


/* Message Init */

TxMessage.StdId = 0x123;

TxMessage.ExtId = 0;

TxMessage.RTR = CAN_RTR_DATA;

TxMessage.IDE = CAN_ID_STD;

TxMessage.DLC = 1;


TxMessage.Data[0] = 5;

}


void
CAN1_DataSend(
const
uint8_t* txBuff )

{

TxMessage.StdId = 0x123;

TxMessage.ExtId = 0;

//TxMessage.RTR = CAN_RTR_DATA;

//TxMessage.IDE = CAN_ID_STD;

//TxMessage.DLC = 1;


TxMessage.Data[0] = 0;

TxMessage.Data[1] = 1;

TxMessage.Data[2] = 2;

TxMessage.Data[3] = 3;

TxMessage.Data[4] = 4;

TxMessage.Data[5] = 5;

TxMessage.Data[6] = 6;

TxMessage.Data[7] = 7;


/*

for (uint8_t i = 0; i < 8; i++)

{

TxMessage.Data[i] = txBuff[i];

}

*/

CAN_Transmit(CAN1, &TxMessage);

}

How come I can send datathrough CAN1?
1 REPLY 1
Posted on December 15, 2014 at 18:50

You'll need to wire up a proper CAN bus (transceiver, termination or diode arrangement), with a receiving node to accept your transmission.

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