cancel
Showing results for 
Search instead for 
Did you mean: 

CAN TX Problem

randyao12345
Associate II
Posted on October 01, 2014 at 19:44

Hi ST Forum,

Debugging a CAN TX message and can't really do it :(. I connected scope to PB13 and just see a high (3V) signal on the TX line. Anything wrong with my code below?

I'm wondering if it's my CAN_mode. Can anyone tell me what's the difference between normal mode, silent mode and loopback mode?

Thanks,

#include <stm32f4xx_can.h>

#include <stm32f4xx_gpio.h>

#include <stm32f4xx_rcc.h>

#include <semihosting.h>

GPIO_InitTypeDef GPIO_InitStructure;

CAN_InitTypeDef CAN_InitStructure;

CanTxMsg Message1;

CanTxMsg Message2;

void CANInit(void);

void Message(void);

int main(void)

{

CANInit();

Message();

    while(1)

    {

    CAN_Transmit(CAN2,&Message1);

    }

}

void CANInit(void){

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB,&GPIO_InitStructure);

GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_CAN2); //CAN_RX = PB12

GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_CAN2); //CAN_TX = PB13

/* CAN2 reset */

/* CAN2 configuration */

CAN_DeInit(CAN2); //De-initialize CAN 2 to start over

CAN_InitStructure.CAN_TTCM = DISABLE; // time-triggered communication mode = DISABLED

CAN_InitStructure.CAN_ABOM = DISABLE; // automatic bus-off management mode = DISABLED

CAN_InitStructure.CAN_AWUM = DISABLE; // automatic wake-up mode = DISABLED

CAN_InitStructure.CAN_NART = DISABLE; // non-automatic retransmission mode = DISABLED

CAN_InitStructure.CAN_RFLM = DISABLE; // receive FIFO locked mode = DISABLED

CAN_InitStructure.CAN_TXFP = DISABLE; // transmit FIFO priority = DISABLED

CAN_InitStructure.CAN_Mode = CAN_Mode_Silent; // normal CAN mode

CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // synchronization jump width = 1

CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq; //14

CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; //6

CAN_InitStructure.CAN_Prescaler = 2; // baudrate 125kbps

//CAN_InitStructure.CAN_Prescaler = 4; // baudrate 500 kbps

CAN_Init(CAN2, &CAN_InitStructure);

}

void Message(void){

Message1.RTR = CAN_RTR_Data; //transmit data (master to slave) remote frame is a destination node requesting data

Message1.IDE = CAN_Id_Standard; //Standard identifier length

Message1.StdId = 0x7FF; //Identifier and priority

Message1.DLC = 1; //8 bit data

Message1.Data[0] = (uint8_t)0xAA; //actual message

}
3 REPLIES 3
Posted on October 01, 2014 at 20:10

In Loopback mode you can test the CAN absent any other devices, in Normal mode when you TX there needs to be an RX device attached to the bus.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
randyao12345
Associate II
Posted on October 01, 2014 at 20:23

Oh thanks Clive. So if I switch to loopback mode I should be able to see the CAN frame on the oscilloscope?

Do you see anything else wrong about my code?

Thanks.

Posted on October 01, 2014 at 21:45

Hard to say, I've posted working CAN code, so I'm not going to try and decompose this.

CAN2 is slaved off CAN1, I don't think your baud rate will be what your comments suggest. Pay attention to the APB bus speed, the prescaler, and bit quanta.

A working CAN interface will require a receiver and transmitter, along with bus transceivers at both ends.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..