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
}
}
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
}
Best regards.