Forum: Mikrocontroller und Digitale Elektronik CAN BUS Acknowledgment error


von ABCD M. (ramachandran_m)


Lesenswert?

Hallo

           I am using STM32F429 CAN bus Program with TJA1041A as CAN 
Transreciver.

The Problem is the messages are not getting acknowledged and herewith I 
am attaching the code for further reference.I am using PCAN View to see 
the messages.

I kindly request to check the code and tell me if there r any faults.

Code:-

int main(void)
{

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);

// SystemInit();

    GPIO_InitTypeDef GPIO_InitDef;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    GPIO_InitDef.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
    GPIO_InitDef.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
    GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_Init(GPIOB, &GPIO_InitDef);

/* Connect CAN pins to AF */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2); // CAN2_RX
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); // CAN2_TX

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

   GPIO_InitDef.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
    GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
    GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;

  /* Connect PD5 and PD7 pins  for CAN Transreceiver to enable and 
stanby */

 GPIO_Init(GPIOD, &GPIO_InitDef);
     GPIO_SetBits(GPIOD, GPIO_Pin_5|GPIO_Pin_7);

   RCC_ClocksTypeDef     RCC_Clocks;
  CAN_InitTypeDef       CAN_InitStructure;
  CAN_FilterInitTypeDef CAN_FilterInitStructure;

  RCC_GetClocksFreq(&RCC_Clocks);

  CAN_DeInit(CAN2);

  CAN_StructInit(&CAN_InitStructure);

  /* CAN cell init */
  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_InitStructure.CAN_Mode = CAN_Mode_Normal ;

  /* quanta 1+14+6 = 21, 21 * 4 = 84, 42000000 / 84 = 5000000 */
  /* CAN Baudrate = 500Kbps (CAN clocked at 42 MHz) Prescale = 4 */

  /* Requires a clock with integer division into APB clock */

  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 1+6+7 = 14, 1+14+6 = 21, 
1+15+5 = 21
  CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
  CAN_InitStructure.CAN_Prescaler = 4; // quanta by baudrate - 125kbps

  CAN_Init(CAN2, &CAN_InitStructure);

  /* CAN filter init */
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // 
IdMask or IdList
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit; // 16 
or 32

  CAN_FilterInitStructure.CAN_FilterIdHigh      = 0x0000; // Everything, 
otherwise 11-bit in top bits
  CAN_FilterInitStructure.CAN_FilterIdLow       = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh  = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow   = 0x0000;

  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // Rx
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

  //CAN_FilterInitStructure.CAN_FilterNumber = 0; // CAN1 [ 0..13]

//  CAN_FilterInit(&CAN_FilterInitStructure);

  CAN_FilterInitStructure.CAN_FilterNumber = 14; // CAN2 [14..27]

  CAN_FilterInit(&CAN_FilterInitStructure);
  CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);

 CanTxMsg TxMessage;

  // transmit */
  TxMessage.StdId = 0x321;
  TxMessage.ExtId = 0x00;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;

  TxMessage.Data[0] = 0x02;
  TxMessage.Data[1] = 0x11;
  TxMessage.Data[2] = 0x11;
  TxMessage.Data[3] = 0x11;

 while(1)
  {
    uint32_t i;
     int j = 0;
    uint8_t TransmitMailbox = 0;

  TxMessage.Data[4] = (j >>  0) & 0xFF; // Cycling
   TxMessage.Data[5] = (j >>  8) & 0xFF;
    TxMessage.Data[6] = (j >> 16) & 0xFF;
    TxMessage.Data[7] = (j >> 24) & 0xFF;
    j++;

    TransmitMailbox = CAN_Transmit(CAN2, &TxMessage);

i = 0;
    while((CAN_TransmitStatus(CAN2, TransmitMailbox) != CANTXOK) && (i 
!= 0xFFFF)) // Wait on Transmit
    {
      i++;
      CAN2RX();// Pump RX
    }

  }

}

von Christian G. (christiang)


Lesenswert?

Is there a second node connected to the bus and configured with the same 
bus frequency?
Only any other node can acknowledge a send message. Without another node 
you always will get this error.

von RTFM (Gast)


Lesenswert?

Is your CAN bus terminated with 120 Ohm on both ends?

von ABCD M. (ramachandran_m)


Lesenswert?

Ya

Even the other node is set at the same frequecy.

And it is terminated with 120 ohms at both the ends.

von Steffen R. (steffen_rose)


Lesenswert?

Sure, that your device transmit a message? (not loop back mode - check 
it/measure it)

In case, that your transceiver can be dactivate - is it activated?

Is you Peak device in Listen only mode? In Listen only mode the device 
do not send an acknowlede.

In case all of them should be correct - Change the bitrate of the Peak 
and transmit a message. Now the Peak should go in Error Busoff. In other 
case your device is not active on the CAN bus.

von ABCD M. (ramachandran_m)


Lesenswert?

Thanks for your reply.


The mode is in Normal Mode and regarding my CAN Tranreceiver , we have 
to enable PD5 and PD7 for activating it and it has also been done.

I havent checked in Loopback Mode.I hope in Loop back mode we can't see 
any messages.

von Steffen R. (steffen_rose)


Lesenswert?

You mean:

You see the CAN messages of your STM on the CAN bus?

Is the Peak your second node? Send with the Peak device. Do you see this 
messages, too?

> /* Connect PD5 and PD7 pins  for CAN Transreceiver to enable and
stanby */

>  GPIO_Init(GPIOD, &GPIO_InitDef);
>     GPIO_SetBits(GPIOD, GPIO_Pin_5|GPIO_Pin_7);

High active?

von ABCD M. (ramachandran_m)


Lesenswert?

My second node is TJA1041A .It is an high speed CANtransreceiver which 
is inbuilt with STM32F429.

It is soldered along with my STM microcontroller.

On PD Configuration I have set it to high in order to activate my can 
transreceiver.

Even though my can transreceiver is enabled,the messages are not 
acknowledging.

von ABCD M. (ramachandran_m)


Lesenswert?

And I am not able to see any messages in the PCAN.


I am neither able to send or receive messages

von Steffen R. (steffen_rose)


Lesenswert?

ABCD M. schrieb:
> I havent checked in Loopback Mode.I hope in Loop back mode we can't see
> any messages.

Please write more about it. What did you see and did you check the TX/RX 
Pin or CAN-H/CAN-L?

von Steffen R. (steffen_rose)


Lesenswert?

ABCD M. schrieb:
> My second node is TJA1041A .It is an high speed CANtransreceiver which
> is inbuilt with STM32F429.

A transceiver is not a CAN node. It's a "level converter".
(Very short and not completely correct! Only for better understanding.)

von Thomas F. (igel)


Lesenswert?

Steffen Rose schrieb:
> A transceiver is not a CAN node. It's a "level converter".
> (Very short and not completely correct! Only for better understanding.)

And the tranceiver does not send the ACK. This can only be done by a 
second STM32 or the PEAK hardware device of your computer, which must 
not be in listen only mode.

von Steffen R. (steffen_rose)


Lesenswert?

ABCD M. schrieb:
> And I am not able to see any messages in the PCAN.
>
> I am neither able to send or receive messages

What do you see, if you send with the Peak?
What do you see, if you send with the Peak on a different bitrate?

von ABCD M. (ramachandran_m)


Lesenswert?

On the CRO,

I am able to see the waveform on CANH and CANL but in PCAN it always 
displays "BUSHEAVY"..

After changing the bitrate also,it displays "BUSHEAVY"

von Steffen R. (steffen_rose)


Lesenswert?

You forget the main information:
What do you see, if you transmit a message with the PCAN View?

I'm meaning you started the PCAN View in Listen only mode. In this mode 
the Peak do not transmit an acknowledge.

von ABCD M. (ramachandran_m)


Lesenswert?

If I transmit a message with the PCAN View,it shows an Error Message 
with some ID and displays "BUS HEAVY."



I have checked my GPIO function also and its also perfectly right..



I am sending the following message from PCAn

ID :123
DLC:8
Data(8) = All 0x00

von ABCD M. (ramachandran_m)


Lesenswert?

And the PCAN is not in Listen Only mode...

I have checked that too..

von Steffen R. (steffen_rose)


Lesenswert?

Peak and STM are connected and online.

1) You send with the STM and see the waveform. You get an Acknowledge 
Error.
2) You send with the Peak and see the waveform. You see a Error Message 
(Acknowledge Error, too?). Only one? Bus heavy.
3) You switch the Peak to a different CAN bitrate (use a higher bitrate) 
and send a message. You see the waveform and the error like 2).

Sure, that the CAN busses are connected of both devices and both running 
at the same time?

One additional test:

Connect CAN-H and CAN-L (short circuit). If you transmit a message with 
the STM, the STM has to go in Busoff. If you transmit with the Peak, the 
Peak has to go in Busoff.

von ABCD M. (ramachandran_m)


Lesenswert?

I want to know whether CAN2 bus alone can be initalized seperately and 
transmit the messages.

Because when I studied through the reference manual,I came to know CAN2 
is a Slave and CAN1 is a Master Node.



So I kindly request you to tell me whether is ist possible to implement 
CAN2 message communication seperately by initalzing the GPIO Pins

von Steffen R. (steffen_rose)


Lesenswert?

ABCD M. schrieb:
> Because when I studied through the reference manual,I came to know CAN2
> is a Slave and CAN1 is a Master Node.

This is only for the filter mechanism. You have activated the Clock for 
CAN1. That's all what is required to use CAN2.

The CAN itself is independend.

von ABCD M. (ramachandran_m)


Lesenswert?

Thanks for your reply.

Can u explain me the below line

///CAN1: Master bxCAN for managing the communication between a Slave 
bxCAN and
the 512-byte SRAM memory..CAN2: Slave bxCAN, with no direct access to 
the SRAM memory. //

von Steffen R. (steffen_rose)


Lesenswert?

ABCD M. schrieb:
> 512-byte SRAM memory.

This is the Filter RAM. CAN1 control the filter for both CAN.

"28 filter banks shared between CAN1 and CAN2 in connectivity line 
devices"

But this is not relevant for transmitting and you acknowledge problem.

von ABCD M. (ramachandran_m)


Lesenswert?

Ok

Alles Klar

von ABCD M. (ramachandran_m)


Lesenswert?

How to check the frequency of APBCLK1 of STM32F429


I want to check whether my baud rate calculations are right

I want to check whether APB1 CLK Freq is 42Mhz.

von Steffen R. (steffen_rose)


Lesenswert?

The simplest test would be to measure the CAN waveform -> bittime.
The easierst is, to measure the Bittiming on the TX/RX pin instead on 
CAN-H/CAN-L.

But I was the meaning, if you write, you checked the waveform, that you 
did see, that the bittiming is correct.

von ABCD M. (ramachandran_m)


Lesenswert?

Sorry Ich kann nicht verstanden.


Can you explain it further more

von Steffen R. (steffen_rose)


Lesenswert?

Can you ask directly, what you don't understand?

1 bit of 500kbit/s is 2µs. This can you check with the oscilloscope.
For easier measurement the CPU pins are better to use as the Transceiver 
Pins.

To measure the CAN bitrate is easier as the check of the APB clock.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.