/***************************************************************************** * * FILENAME: spi.c * COMPILER: MINGW * PROCESSOR: x86 * * COPYRIGHT: AA * AUTHOR, DPT: Alen Harbas * * DATE OF CREATION: 11.05.2019 * *****************************************************************************/
/* ========== Includes ===================================================== */#include "candll.h"#include "Can.h"#include "TCAN4550.h"#include "TCAN4x5x_SPI.h"#include <stdio.h>
/* ========== Local function prototypes ============================================================================= */
/* ========== Enumerations, Typedefinitions, Local Defines ================= */
#define CAN_CHANNEL_1 0x51#define CAN_CHANNEL_2 0x52
#define CAN_TX_FIFO_INDEX 0u
/* ========== Local variables ============================================== */
CANDLL_ConfigType DLLCongigType;CANDLL_ConfigType *CANConfig = &DLLCongigType;uint8 CAN_Ready = 0;CAN_RxMessageType rx_LAST_msg;uint8 TX_BUFFER_INDEX = 0;
uint16 milis = 0;/********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_Initialize( TPCANHandle handle, TPCANBaudrate baudrate, TPCANType can_type, TPCANLong io_port, uint16_t interrupt) { memset(&rx_LAST_msg,0,sizeof(CAN_RxMessageType)); TX_BUFFER_INDEX = 0;
AllocConsole();
freopen("CONIN$", "r",stdin); freopen("CONOUT$","w",stdout); freopen("CONOUT$","w",stderr);
if(CAN_Ready == 1){ return PCAN_ERROR_OK ; }
CAN_Ready = 1;
//printf("CAN_Initialize------------------------------------------------------------------------\n"); if(CAN_Init() == E_OK){ printf("CAN2USB READY\n"); }
return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_InitializeFD( TPCANHandle handle, TPCANBitrateFD bitrateFD) { return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_Uninitialize( TPCANHandle handle) { //printf("Deinit"); //FreeConsole(); return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_Reset( TPCANHandle handle) { printf("reset\n"); memset(&rx_LAST_msg,0,sizeof(CAN_RxMessageType)); TX_BUFFER_INDEX = 0; return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_GetStatus( TPCANHandle handle) { printf("status\n"); return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_Read( TPCANHandle handle, TPCANMsg * msg, TPCANTimestamp *timestamp) {
// CAN_RxMessageType rx_msg;// memset(&rx_msg,0,sizeof(CAN_RxMessageType));//// Std_ReturnType CANGetStatus = E_NOT_OK;// uint16 wait = 0;// struct _SYSTEMTIME time;////// while( (CANGetStatus == E_NOT_OK) && (wait < 400)){// CANGetStatus = CAN_Get(&rx_msg);// wait++;// usleep(1);// }//// //printf("RECV\n");// if(CANGetStatus == E_OK){//// memcpy(&rx_LAST_msg,&rx_msg,sizeof( CAN_RxMessageType ));//// }else{// printf("OLD MSG WILL BE COPIED\n");// memcpy(&rx_msg,&rx_LAST_msg,sizeof( CAN_RxMessageType ));// //return PCAN_ERROR_QRCVEMPTY;// }//// msg->ID = rx_msg.header.ID;// msg->LEN = TCAN4x5x_MCAN_DLCtoBytes(rx_msg.header.DLC);// msg->MSGTYPE = 0x00; //standard// memcpy(msg->DATA,rx_msg.msg,msg->LEN);//
TCAN4x5x_MCAN_Interrupts mcan_ir = {0}; // Setup a new MCAN IR object for easy interrupt checking
TCAN4x5x_MCAN_ReadInterrupts(&mcan_ir); // Read the interrupt register
// uint8 wait = 0;// while(mcan_ir.RF0N == 0 && wait < 10){// TCAN4x5x_MCAN_ReadInterrupts(&mcan_ir);// wait++;// usleep(50 * 1000);// }
if (mcan_ir.RF0N && msg != 0) // If a new message in RX FIFO 0 {
timestamp->millis = milis; // Base-value: milliseconds: 0.. 2^32-1 timestamp->millis_overflow = 0xFFFF-milis; // Roll-arounds of millis timestamp->micros = 0; // Microseconds: 0..999 milis++;
//printf("Recv\n"); TCAN4x5x_MCAN_RX_Header MsgHeader = {0}; // Initialize to 0 or you'll get garbage uint8 dataPayload[64] = {0};
TCAN4x5x_MCAN_ClearInterrupts(&mcan_ir); // Clear any of the interrupt bits that are set.
TCAN4x5x_MCAN_ReadNextFIFO( RXFIFO0, &MsgHeader, dataPayload); // This will read the next element in the RX FIFO 0
/*save msg*/ memcpy(&rx_LAST_msg.header,&MsgHeader,sizeof(TCAN4x5x_MCAN_RX_Header)); memcpy(&rx_LAST_msg.msg,&dataPayload,8);
memset(msg,0,sizeof(TPCANMsg));
msg->ID = MsgHeader.ID; msg->LEN = TCAN4x5x_MCAN_DLCtoBytes(MsgHeader.DLC); msg->MSGTYPE = MsgHeader.XTD; //standard memcpy(msg->DATA,dataPayload,msg->LEN);
return PCAN_ERROR_OK;
}else{// memset(msg,0,sizeof(TPCANMsg)); /*restore old*/// msg->ID = rx_LAST_msg.header.ID;// msg->LEN = TCAN4x5x_MCAN_DLCtoBytes(rx_LAST_msg.header.DLC);// msg->MSGTYPE = rx_LAST_msg.header.XTD; //standard// memcpy(msg->DATA,rx_LAST_msg.msg,msg->LEN); return PCAN_ERROR_OK; }
return PCAN_ERROR_UNKNOWN;
} /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_ReadFD( TPCANHandle handle, TPCANMsgFD *msg, TPCANTimestampFD *timestamp) {
return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_Write( TPCANHandle handle, TPCANMsg *msg) { if(TX_BUFFER_INDEX > 19){ TX_BUFFER_INDEX = 0; }
// CAN_TxMessageType tx_msg;// memset(&tx_msg,0,sizeof(CAN_TxMessageType));// TCAN4x5x_MCAN_TX_Header header = {0};////// header.DLC = msg->LEN; // Set the DLC to be equal to or less than the data payload (it is ok to pass a 64 byte data array into the WriteTXFIFO function if your DLC is 8 bytes, only the first 8 bytes will be read)// header.ID = msg->ID; // Set the ID// header.FDF = 0; // CAN FD frame enabled// header.BRS = 0; // Bit rate switch enabled// header.EFC = 0;// header.MM = 0;// header.RTR = 0;// header.XTD = 0; // We are not using an extended ID in this example// header.ESI = 0; // Error state indicator////// /*copy header*/// memcpy(&tx_msg.header,&header,sizeof(TCAN4x5x_MCAN_TX_Header));// /*copy data*/// memcpy(tx_msg.msg,msg->DATA,msg->LEN);// /*important*/// tx_msg.FIFOIndex = CAN_TX_FIFO_INDEX;//// /*blocking*/// CAN_Set(&tx_msg);
//printf("sending\n");
TCAN4x5x_MCAN_TX_Header header = {0}; // Remember to initialize to 0, or you'll get random garbage!
header.DLC = msg->LEN; // Set the DLC to be equal to or less than the data payload (it is ok to pass a 64 byte data array into the WriteTXFIFO function if your DLC is 8 bytes, only the first 8 bytes will be read) header.ID = msg->ID; // Set the ID header.FDF = 0; // CAN FD frame enabled header.BRS = 0; // Bit rate switch enabled header.EFC = 0; header.MM = 0; header.RTR = 0; header.XTD = msg->MSGTYPE; // We are not using an extended ID in this example header.ESI = 0; // Error state indicator
TCAN4x5x_MCAN_WriteTXBuffer(TX_BUFFER_INDEX, &header, msg->DATA);
if(TCAN4x5x_MCAN_TransmitBufferContents(TX_BUFFER_INDEX) == E_NOT_OK){ printf("Sending failed\n"); } //usleep(10); TX_BUFFER_INDEX++;
return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_WriteFD( TPCANHandle handle, TPCANMsgFD *msg) { return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_FilterMessages( TPCANHandle handle, TPCANLong fromID, TPCANLong toID, TPCANMode mode) { return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_GetValue( TPCANHandle handle, TPCANParameter parameter, void *buffer, TPCANLong size) { TPCANStatus ret = PCAN_ERROR_OK; unsigned char *ptr = (unsigned char *) buffer;
//printf("CAN_GetValue\n");
switch(handle){ case CAN_CHANNEL_1:
if(parameter == PCAN_CHANNEL_CONDITION){
/*channel exists*/ if(size > 0){ ptr[0] = PCAN_CHANNEL_AVAILABLE; } } break; case CAN_CHANNEL_2:
if(parameter == PCAN_CHANNEL_CONDITION){
/*channel exists*/ if(size > 0){ ptr[0] = PCAN_CHANNEL_AVAILABLE; } } break; default: break; }
return ret; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_SetValue( TPCANHandle handle, TPCANParameter parameter, void *buffer, TPCANLong size) { return PCAN_ERROR_OK ; } /********************************************************************************************************************** * FUNCTION: **********************************************************************************************************************/ TPCANStatus __stdcall CAN_GetErrorText( TPCANStatus handle, uint16_t language, char *string) { return PCAN_ERROR_OK ; }