I use scheme similar to CAN/CANSPI example from uC manual.
After some debugging all works fine except that I never see zero result (all transmit buffers full) for code below.
I don't see zero result even if I send messages like mad. And some messages are corrupted/lost as result of intensive sending.
Code: Select all
char result;
result = CANSPIWrite(tmpMsg.id, (char *)tmpMsg.buffer, tmpMsg.length, tmpMsg.flags);
So I just retry transmission later and don't have any lost/corrupted messages at all.
Code: Select all
result = CANWrite(tmpMsg.id, (char *)tmpMsg.buffer, tmpMsg.length, tmpMsg.flags);
Code: Select all
char canFlags = _CAN_CONFIG_PHSEG2_PRG_ON &
_CAN_CONFIG_LINE_FILTER_OFF &
_CAN_CONFIG_SAMPLE_THRICE &
_CAN_CONFIG_DBL_BUFFER_ON &
_CAN_CONFIG_STD_MSG;
CANInitialize(CANBaudrateParameters[(unsigned char)speed].SJW,
CANBaudrateParameters[(unsigned char)speed].BRP,
CANBaudrateParameters[(unsigned char)speed].PHSEG1,
CANBaudrateParameters[(unsigned char)speed].PHSEG2,
CANBaudrateParameters[(unsigned char)speed].PROPSEG,
canFlags);
...
char canSpiFlags = _CANSPI_CONFIG_PHSEG2_PRG_ON &
_CANSPI_CONFIG_LINE_FILTER_OFF &
_CANSPI_CONFIG_SAMPLE_THRICE &
_CANSPI_CONFIG_DBL_BUFFER_ON &
_CANSPI_CONFIG_STD_MSG;
// We should use SPI mode 0,0 or 1,1
SPI1_Init();
//SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_LOW_2_HIGH);//_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
Delay_ms(100);
CANSPIInitialize(CANBaudrateParameters[(unsigned char)speed].SJW,
CANBaudrateParameters[(unsigned char)speed].BRP,
CANBaudrateParameters[(unsigned char)speed].PHSEG1,
CANBaudrateParameters[(unsigned char)speed].PHSEG2,
CANBaudrateParameters[(unsigned char)speed].PROPSEG,
canSpiFlags);
Code: Select all
unsigned char canSpiHaveFreeBuffer()
{
unsigned char status, dummy;
CanSpi_CS = 0;
SPI1_Write(0xA0);
SPI1_Read(status);
SPI1_Read(dummy);
CanSpi_CS = 1;
if (status & 0x54) // I also tried (status & 0x54) == 0x54
return CAN_FAIL;
return CAN_OK; // We have free buffers and can call CANSPIWrite
}
For debugging I use:
1. Device based on PIC18F258 + MCP2510 + 2xMCP2551
2. The Oscillator clock is 8Mhz on both pic and mcp2510
3. CAN bitrate is 25Kbps (I checked it using oscillograph and USB-CAN adapter connected to PC)
I use custom bitrate parameters but I don't think that it is matter.
Code: Select all
//BRP SJW PROPSEG PHSEG1 PHSEG2 Quality
{ 10, 1, 1, 8, 6, CANQualityBest }, // 25Kbps 16Tq - best
5. MikroC version 5.61 on Win XP SP3.
PS
Where I can read about interrupt usage with CANSPI library to be notified about message transmit complete/new message received?
I would like to use interrupts for to speedup CAN messages processing.
Also I would like to use interrupts for CAN library for same reason.
Thanks a lot.