| 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;
}