CANSPIWrite always return non zero result

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
cepera
Posts: 4
Joined: 09 Apr 2012 10:01

CANSPIWrite always return non zero result

#1 Post by cepera » 18 Jun 2012 06:21

Hello,

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);
While code below works fine. Sometimes result are zero that means that all all transmit buffers are full.
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);
My CAN/CANSPI initialization code:

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);
I also tried to check buffers status manually but w/o success:

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
}
Looks like it is a problem in the CANSPI library.

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
4. For programming device PicKit3 + MPLab 8.x
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.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: CANSPIWrite always return non zero result

#2 Post by janko.kaljevic » 18 Jun 2012 14:19

Hello,

Our CAN SPI modules have MCP2515 chip implemented.
This chip has INT pin which can be connected to External Interrupt pin of the microcontroller.
For example you can set this chip to generate interrupt when message is sent or received successfully.
When appropriate interrupt is triggered you can process it in your code.

At the moment our CAN SPI library does not support these settings, but you can check them in MCP2515 documentation:
http://ww1.microchip.com/downloads/en/D ... 21801F.pdf

Also this implies for the internal CAN module of the controller.
You can set interrupt on CAN receive and process your message inISR.

Best regards.

cepera
Posts: 4
Joined: 09 Apr 2012 10:01

Re: CANSPIWrite always return non zero result

#3 Post by cepera » 19 Jun 2012 19:51

janko.kaljevic wrote:Hello,

Our CAN SPI modules have MCP2515 chip implemented.
This chip has INT pin which can be connected to External Interrupt pin of the microcontroller.
For example you can set this chip to generate interrupt when message is sent or received successfully.
When appropriate interrupt is triggered you can process it in your code.
So does it mean that CANSPIWrite don't check the real status of MCP2510/2515 transmit buffers status and always return non zero even for case if buffers are full and message will be lost?
At the moment our CAN SPI library does not support these settings, but you can check them in MCP2515 documentation:
http://ww1.microchip.com/downloads/en/D ... 21801F.pdf

Also this implies for the internal CAN module of the controller.
You can set interrupt on CAN receive and process your message inISR.
I tried to use interrupt but w/o success. It never trigger.

Is it possible to configure MCP2510/2515 using SPIx_Write function. I mean according to MCP2515 manual I should:
1. set to 0 chip select pin
2. write several bytes
3. [optional] read several bytes
4. set to 1 chip select pin

Will it work? I mean SPIx_Write set to zero chip select pin each byte write and set to one just after...

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: CANSPIWrite always return non zero result

#4 Post by janko.kaljevic » 20 Jun 2012 10:21

Hello,

CANSPIWrite function will check if there is a empty transmitter.
If it is found, function will write a message and it will be queued for sending. In this case FF will be returned.
If there is no empty transmitter buffer, function will return 0.

SPIx_Write does not toggle chip select pin. This has to be done manually.
For example:

Code: Select all

CanSpi_CS = 0; //chip select
SPI_Wr_Ptr();
...
SPI_Rd_Ptr();
...
CanSpi_CS = 1; //chip deselect
Best regards.

cepera
Posts: 4
Joined: 09 Apr 2012 10:01

Re: CANSPIWrite always return non zero result

#5 Post by cepera » 20 Jun 2012 10:30

janko.kaljevic wrote:Hello,
CANSPIWrite function will check if there is a empty transmitter.
If it is found, function will write a message and it will be queued for sending. In this case FF will be returned.
If there is no empty transmitter buffer, function will return 0.
So it mean that I have a problem in hardware, right?

What method do you use in sources for checking status of transmit buffers?
Interrupt? Reading the special status register of the MCP2510?

In case of interrupt it mean that I have problem in hardware. I mean if I always have only 0xFF result from the CANSPIWrite() then it means that I have incorrect connecting of interrupt PIN from the MCP2510 to INT0 pin of PIC18F258, right?

Thanks a lot.

Post Reply

Return to “mikroC PRO for PIC General”