Setup can on t20 with mcp2515 (wec7)

| Try to run can example of toradexcelibraries_2.0-20170524 on t20 with mcp2515 connected to SPI4 pins (SODIMM pins 88 86 90 92).
Chaned only interrupt pin (45 instead 73) and some little chages

As a result I get “Error in CAN initialization” - error on the first step of can init
can you help me? thanks

source code:
winmain

int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdLine, int cmdShow)
{
    HANDLE hCan = NULL;

    BOOL isSelectionDone = FALSE;
    INT selectedOption;

    BOOL returnValue = FALSE;
    DWORD bitRate;
    DWORD busStatus;

    wprintf(L"Toradex CAN demo\r\n");

    // 1. Initialize CAN without affecting hardware registers
    //hCan = Can_Init(L"CAN1"); // Internal CAN
    hCan = Can_Init(L"SPI4");
    if (hCan == NULL)
    {
        printf("Error in CAN initialization\r\n");
		Sleep (5000);// 5sec
		return 0;
    }

    // 2. Change configuration as required
    //    Please refer the documentation regarding the configurations from the .chm file present in the library package.
    //    Instead of calling this function, you can place values in the registry (not present by default). You may use
    //    StoreToRegistry instead of StoreVolatile to configure these values in registry from application for future use.
    //      [HKLM\SOFTWARE\TORADEX\CANx]
    //      Implementation = "VybridCAN"
    //      Interface      = "CAN1"
    /* {
        uIo ioRx =  COLIBRI_PIN(63);
        uIo ioTx =  COLIBRI_PIN(55);
        Can_SetConfigInt(hCan, L"ioRx", ioRx.GenericDefinition, StoreToRegistry);
        Can_SetConfigInt(hCan, L"ioTx", ioTx.GenericDefinition, StoreToRegistry);
    } */
    returnValue = Can_SetConfigString(hCan, L"Implementation", L"MCP2515",      StoreVolatile);     // use MCP2515 CAN controller over SPI interface
    //returnValue = Can_SetConfigString(hCan, L"Implementation", L"FlexCAN",        StoreVolatile); // use Vybrid internal CAN controller
    returnValue = Can_SetConfigString(hCan, L"FrameFormat",    L"standard",     StoreVolatile);     // Set FrameFormat
    returnValue = Can_SetConfigString(hCan, L"FilterRemote",   L"none",     StoreVolatile);     //
    returnValue = Can_SetConfigString(hCan, L"RtrFormat",      L"data",     StoreVolatile);     //
    returnValue = Can_SetConfigInt(hCan, L"FilterID",   0x00,       StoreVolatile);     //
    returnValue = Can_SetConfigInt(hCan, L"FilterMask", 0x00,       StoreVolatile);     //
    returnValue = Can_SetConfigInt(hCan, L"SingleThread", 0,       StoreVolatile);     // activate single thread accessing MCP2515

    {
        uIo ioIntr =  COLIBRI_PIN(45);
        returnValue = Can_SetConfigInt(hCan, L"ioInterrupt", ioIntr.GenericDefinition, StoreVolatile);
    }

	printf("returnValue:%x\r\n", returnValue);

    
    //scanf_s("%d", &bitRate);
	bitRate = 250000;
	printf("CAN bit rate in Hz: %d", bitRate);
    returnValue = Can_SetConfigInt(hCan, L"BitRateHz", bitRate, StoreVolatile);
    printf("returnValue:%x\r\n", returnValue);

    /* if (!Can_Open(hCan))
    {
        printf("CAN Tx Open failed\r\n");
        return 0;
    }
    returnValue = Can_GetConfigInt(hCan, L"BusStatus", &busStatus);
    printf("BusStatus %x",busStatus);

    /// 5. Close CAN channel
    Can_Close(hCan);  */

    do
    {
        /*printf("\nOptions:\n");
        printf("1. CAN Transmit   2. CAN Receive   3. Quit\n\n");
        printf("Choose the option and press Enter key: ");
        scanf_s("%d", &selectedOption);
        printf("\n");*/

	selectedOption = 1;
        /// Enter only if there is only one character entered from key board.
        switch (selectedOption)
        {
            case 1:
                /// CAN Transmit
                CanTxProcess(hCan);
                break;

            case 2:
                /// CAN Receive
                CanRxProcess(hCan);
                break;

            case 3:
                /// Quits the demo
                isSelectionDone = TRUE;
                break;

            default:
                printf("Invalid entry, try again!\n\n");
                isSelectionDone = FALSE;
        }
    } while (!isSelectionDone);

    /// 6. Deinit CAN channel
    Can_Deinit(hCan);

    return(TRUE);
}

tx function

DWORD CanTxProcess(HANDLE hCan)
{
    INT i;
    tCanMsg canBuf;
    BYTE data[8] = {0, 1, 2, 3, 4, 5, 6, 7};   // Tx buffer


    // 3. Apply configuration to hardware
    if (!Can_Open(hCan))
    {
        printf("CAN Tx Open failed\r\n");
        return 0;
    }

    // 4. Use CAN
    canBuf.id    = rand() & 0x7FF;         // CAN frame ID
    memcpy(canBuf.data, data, sizeof(data));
    canBuf.dataLen = 8;
while (1)
{
    if (Can_Write(hCan, &canBuf))
    {
        printf("\nCAN Transmit: Frame ID = 0x%x Remote/Data Frame = %s\nData:", canBuf.id, (canBuf.canMsgFlags & CanMsgFlags_RTR  ? "REMOTE" :  "DATA"));
        // display Transmit data
        for (i = 0; i < 8; i++)
        {
            printf(" %d", canBuf.data[i]);
        }
    }
    else
    {
        printf("CAN Transmit error\r\n");
	}
Sleep (1000);// 1sec
}

    /// 5. Close CAN channel
    Can_Close(hCan);

    return 0;
}

Please be aware, that the Iris carrier board has no MCP2515. Did you add your own MCP2515 to to board? If so, on which pins did you attach it?

If you want to use the code sample on one of our boards I suggest to use the Colibri Evaluation Board from version 3.0 on.

Thanks for your answer.
I use my own MCP2515 circuit.
It’s connected like that:
iris (X16) — mcp2515 (18PDIP)

  • 8(SSPSCLK) — 13(SCK)
  • 9(SSPFRM) — 16(CS) pulled up 10k
  • 10(SSPRXD) — 15 (SO)
  • 11(SSPTXD) — 14 (SI)
  • 20 (SODIMM45) — 12 (INT)
  • OSC1 / OSC2 use 8Mhz with couple of cap - 22pF

MCP2515_Reset puled up 10k

+VСС and GND of course connected

I have no ability to use the Colibri Evaluation Board now

Osciloscope donn’t show any action on any pin

@dima,
MCP2515 CAN library for T20 is not supported on toradexcelibraries_2.0-20170524 library version. Recently we implemented it. Please download and test this preliminary version. Let us know if you face any problem.

Tested. It’s work fine. Thanks!! Changed only 8MHz to 16 MHz. No tested on 8MHz

But now I find some issue.
We have a problem when MCP2515 NO connected to T20. Program wait about 5-6 minutes in function CanOpen.
Any ideas about this?

@dima: This could be due to some timeouts during the open phase. Do you have not MCP ship there at all or just no CAN devices on the MCP?

Using the lib without any MCP2515 is not a supported use case yet. Is this a real need for your device or is just annoying for development?