CAN library buffer problems on full speed stm32f407vg

Discuss with MikroElektronika software developers about current library development.
Post Reply
Author
Message
Sjoerd
Posts: 7
Joined: 19 Aug 2013 13:10

CAN library buffer problems on full speed stm32f407vg

#1 Post by Sjoerd » 13 Sep 2013 11:02

Hello all,

I'm using the CAN library and I'm testing it on full speed. every time there's a place free in the send buffer i send the next CAN message.
If i do this the first message will be send, the second and third message are lost. after the third message all messages will be send.

I use the code below.

Code: Select all

#define CAN_CONFIG_FLAGS (_CAN_CONFIG_AUTOMATIC_RETRANSMISSION & _CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN &\
                          _CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED & _CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER & _CAN_CONFIG_WAKE_UP)
                          
unsigned long Can_Init_Flags;
unsigned char Can_Send_Flags; 									// can flags
char RxTx_Data[8];                              // can rx/tx data buffer
const long ID_1st = 1024;                       // node IDs

void CanBus_Init(void)
{
	#if CAN==1
	CAN1InitializeAdvanced(1,40,1,3,5, CAN_CONFIG_FLAGS, &_GPIO_MODULE_CAN1_PD01); // Initialize CAN module (100Kb/s uc 160Mhz)
	CAN1SetOperationMode(_CAN_OperatingMode_Initialization);                   // set CONFIGURATION mode
	CANSetFilterScale32(0, _CAN_FILTER_ENABLED & _CAN_FILTER_ID_MASK_MODE & _CAN_FILTER_STD_MSG, -1, 0);
	CAN1SetOperationMode(_CAN_OperatingMode_Normal); // set NORMAL mode
	#elif CAN==2
	CAN2InitializeAdvanced(1,40,1,3,5, CAN_CONFIG_FLAGS, &_GPIO_MODULE_CAN2_PB56); // Initialize CAN module (100Kb/s uc 160Mhz)
	CAN2SetOperationMode(_CAN_OperatingMode_Initialization);                   // set CONFIGURATION mode
	CANSetFilterScale32(1, _CAN_FILTER_ENABLED & _CAN_FILTER_ID_MASK_MODE & _CAN_FILTER_STD_MSG, -1, 0);
	CANSlaveStartBank(1);
	CAN2SetOperationMode(_CAN_OperatingMode_Normal); // set NORMAL mode
	#endif
} // CanBus_Init
  
void main()
{
  Can_Init_Flags = 0;                                       //
  Can_Send_Flags = 0;                                       // clear flags

  Can_Send_Flags = _CAN_TX_STD_FRAME &                      // with CANWrite
                   _CAN_TX_NO_RTR_FRAME;


  CanBus_Init();

  RxTx_Data[0] = 0;                                					// set initial data to be sent

  Delay_ms(500);																						// Give CANbus some time

  while(1) {  // endless loop
      #if CAN==1																						// chose between CAN1 and CAN2
      while(CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) > 2);		// Wait until a buffer place becomes free
      #elif CAN==2
      while(CAN2Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) > 2);		// Wait until a buffer place becomes free
      #endif

      if(RxTx_Data[0] >= 255)
        RxTx_Data[0] = 0;																		// Reset counter when byte max reached 
      else
        RxTx_Data[0]++;																			// Increment counter
  }
}
I analyzed the CAN transmit line with a logic analyzer
CAN missing messages.png
CAN missing messages.png (31.51 KiB) Viewed 3017 times
I also tested this problem with a delay of 1.5 ms after 256 messages have bin sent. the same problem occurs after delay when uc starts to send on full speed again.
but message 2 and 3 are send in the 1.5 ms delay after message 256 has been sent.

Code: Select all

while(1) {  // endless loop
      #if CAN==1																						// chose between CAN1 and CAN2
      while(CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) > 2);		// Wait until a buffer place becomes free
      #elif CAN==2
      while(CAN2Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) > 2);		// Wait until a buffer place becomes free
      #endif

      if(RxTx_Data[0] >= 255)
			{
        RxTx_Data[0] = 0;																		// Reset counter when byte max reached 
				Delay_us(1500);
			}
			else
        RxTx_Data[0]++;																			// Increment counter
  }
CAN missing messages with delay.png
CAN missing messages with delay.png (36.28 KiB) Viewed 3017 times
this means that when I'm sending on full speed buffer place 2 and 3 will never been sent. how can resolve this problem because its important that the messages are sent in the right order.

Best regards.

Sjoerd
Posts: 7
Joined: 19 Aug 2013 13:10

Re: CAN library buffer problems on full speed stm32f407vg

#2 Post by Sjoerd » 16 Sep 2013 08:09

Problem solved! just needed to change _CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER flag into _CAN_CONFIG_TX_FIFO_PRIORITY_BY_REQUEST_ORDER in CANxInitializeAdvanced() function.

Post Reply

Return to “Library Development Discussion”