📄 can_init.c
字号:
if (can_TX == NULL)
{
#ifdef CAN_DEBUG_MSG
printf("! can_unitInit: ERROR TX buffer creation\n");
#endif
return (ERR);
}
can_WasSent_remote = 0;
can_WasSent_tx = 0;
can_err_ErrorPointer.ptr = can_err_ErrorDescript;
can_err_ErrorPointer.head = can_err_ErrorDescript;
can_err_ErrorPointer.tail = can_err_ErrorDescript;
can_err_ErrorPointer.count = 0;
can_err_ErrorPointer.end = &can_err_ErrorPointer.ptr[CAN_UNIT_ERROR_DESCRIP_BUFFER_SIZE];
for (i=0; i<CAN_UNIT_ERROR_DESCRIP_BUFFER_SIZE; i++)
{
can_err_ErrorDescript[i].canErrNumber = 0;
can_err_ErrorDescript[i].canErrTxtDescriptionPtr = 0;
can_err_ErrorDescript[i].sysErrTime = 0;
can_err_ErrorDescript[i].canFunctionId = 0;
}
/* hw initialization */
can_flexcan_init();
can_unit_PlugInState = can_None;
can_unit_IntegrationState = can_Waiting;
can_unit_Mode = can_ModeIntegration;
can_unit_CurrentOperation = can_StatusIdle;
return (OK);
}
/************************************************************************
* NAME: can_flexcan_init
*
* DESCRIPTION Initialization of CAN controller, hardware buffers, etc.
************************************************************************/
void
can_flexcan_init(void)
{
uint8 j, i;
unsigned long *mbuf0 = (unsigned long *) (0x00000000+0x0220);
unsigned long *mbuf1 = (unsigned long *) (0x00000000+0x0224);
unsigned long *mbuf2 = (unsigned long *) (0x00000000+0x0228);
unsigned long *mbuf3 = (unsigned long *) (0x00000000+0x022C);
unsigned long *mbuf4 = (unsigned long *) (0x00000000+0x0230);
unsigned long *mbuf5 = (unsigned long *) (0x00000000+0x0234);
unsigned long *mbuf6 = (unsigned long *) (0x00000000+0x0238);
unsigned long *mbuf7 = (unsigned long *) (0x00000000+0x023C);
unsigned long *mbuf8 = (unsigned long *) (0x00000000+0x0240);
unsigned long *mbuf9 = (unsigned long *) (0x00000000+0x0244);
unsigned long *mbuf10 = (unsigned long *) (0x00000000+0x0248);
unsigned long *mbuf11 = (unsigned long *) (0x00000000+0x024C);
unsigned long *mbuf12 = (unsigned long *) (0x00000000+0x0250);
unsigned long *mbuf13 = (unsigned long *) (0x00000000+0x0254);
unsigned long *mbuf14 = (unsigned long *) (0x00000000+0x0258);
unsigned long *mbuf15 = (unsigned long *) (0x00000000+0x025C);
unsigned long *fl_error = (unsigned long *) (0x00000000+0x0260);
unsigned long *fl_boff = (unsigned long *) (0x00000000+0x0264);
unsigned long *fl_wakeup = (unsigned long *) (0x00000000+0x0268);
*mbuf0 = (unsigned long) can_Rx;
*mbuf1 = (unsigned long) can_Rx;
*mbuf2 = (unsigned long) can_Rx;
*mbuf3 = (unsigned long) can_Rx;
*mbuf4 = (unsigned long) can_Rx;
*mbuf5 = (unsigned long) can_Rx;
*mbuf6 = (unsigned long) can_Rx;
*mbuf7 = (unsigned long) can_Rx;
*mbuf8 = (unsigned long) can_Rx;
*mbuf9 = (unsigned long) can_Rx;
*mbuf10 = (unsigned long) can_Rx;
*mbuf11 = (unsigned long) can_Tx;
*mbuf12 = (unsigned long) can_Remote;
*mbuf13 = (unsigned long) can_Remote;
*mbuf14 = (unsigned long) can_Rx;
*mbuf15 = (unsigned long) can_Rx;
*fl_error = (unsigned long) can_Error ;
*fl_boff = (unsigned long) can_Bus_Off ;
*fl_wakeup = (unsigned long) can_Wake_Up ;
MCF5282_FLEXCAN_CANMCR = MCF5282_FLEXCAN_CANMCR | MCF5282_FLEXCAN_CANMCR_WAKEMSK; // wake up interrupt enabled
// Initialize Receive Mask Registers
MCF5282_FLEXCAN_RXGMASK = 0x00000000;
MCF5282_FLEXCAN_RX14MASK = 0x00000000;
MCF5282_FLEXCAN_RX15MASK = 0x00000000;
for (i=0; i<CAN_UNIT_HW_BUFFER_NUMBER; i++)
{
// Initialize Message Buffers
MCF5282_FLEXCAN_MBUF_CTRL(i) = 0x00;
// Clear all Message Buffers
for (j=0; j<CAN_UNIT_MESSAGE_DATA_LENGTH; j++)
{
MCF5282_FLEXCAN_MBUF_BYTE(i, j) = 0x00;
}
// Setting buffer IDs
if (can_HWBufferParam[i].BufferType == RX)
{
MCF5282_FLEXCAN_MBUF_IDH(i) = can_HWBufferParam[i].Id_H;
MCF5282_FLEXCAN_MBUF_IDL(i) = can_HWBufferParam[i].Id_L;
}
}
MCF5282_INTC1_IMRL = 0xF80000FE;
MCF5282_INTC1_ICR26 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(7); // wake up
MCF5282_INTC1_ICR25 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(6); // bus off
MCF5282_INTC1_ICR24 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(5); // error
MCF5282_INTC1_ICR08 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF0
MCF5282_INTC1_ICR09 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF1
MCF5282_INTC1_ICR10 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF2
MCF5282_INTC1_ICR11 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF3
MCF5282_INTC1_ICR12 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF4
MCF5282_INTC1_ICR13 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF5
MCF5282_INTC1_ICR14 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF6
MCF5282_INTC1_ICR15 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF7
MCF5282_INTC1_ICR16 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF8
MCF5282_INTC1_ICR17 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF9
MCF5282_INTC1_ICR18 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF10
MCF5282_INTC1_ICR19 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(4); // MBUF11
MCF5282_INTC1_ICR20 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(3); // MBUF12
MCF5282_INTC1_ICR21 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(3); // MBUF13
MCF5282_INTC1_ICR22 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF14
MCF5282_INTC1_ICR23 = MCF5282_INTC_ICR_IL(5) | MCF5282_INTC_ICR_IP(2); // MBUF15
for (i=0; i<CAN_UNIT_HW_BUFFER_NUMBER; i++)
{
if (can_HWBufferParam[i].BufferType == RX)
MCF5282_FLEXCAN_MBUF_CTRL(i) = 0x40;
}
MCF5282_GPIO_PASPAR = 0x0FF0;
}
/************************************************************************
* NAME: can_set_GlobalParam
*
* DESCRIPTION Set one global param variable.
* Return TRUE, if parameter was
* sucessfully setted up or 0 as the error flag.
************************************************************************/
uint32
can_set_GlobalParam(uint8 canParamID, uint32 canParamValueToSet)
{
uint8 i, j;
if (canParamID == PARAM_CAN_SPEED)
{
for (i=0; i < CAN_SPEED_PARAM_NUM; i++)
if (can_SpeedParam[i].CanSpeed == canParamValueToSet)
{
j = can_flexcan_setBitRate(&can_SpeedParam[i]);
if ( j ) {
can_err_SendError(&can_err_ErrorTable[11 + j], current_time, can_set_GlobalParam_Id);
return (FALSE);
}
return (TRUE);
}
can_err_SendError(&can_err_ErrorTable[19], current_time, can_set_GlobalParam_Id);
return (FALSE);
}
return (FALSE);
}
/************************************************************************
* NAME: can_flexcan_setBitRate
*
* DESCRIPTION: Initialization of CAN controller bitrate fields
* (PRESDIV, SYNCSEG=1, PROPSEG, PSEG1, PSEG2, RJW)
*
* NOTE: if PRES_DIV=0 - it will be ignored
************************************************************************/
uint8
can_flexcan_setBitRate(can_SpeedParam_t* ptr)
{
/* just make some foolproof checks */
if ((ptr->NumberOfTQ < 8) || (ptr->NumberOfTQ > 25))
return (1);
if ((1 + ptr->PROPagationSEG + ptr->PhaseSEG1 + ptr->PhaseSEG2) != ptr->NumberOfTQ )
return (2);
if ((ptr->PROPagationSEG < 1) || (ptr->PROPagationSEG > 8))
return (3);
if ((ptr->PhaseSEG1 < 1) || (ptr->PhaseSEG1 > 8))
return (4);
if ((ptr->PhaseSEG2 < 2) || (ptr->PhaseSEG2 > 8))
return (5);
if ((ptr->ResyncJumpWidth < 1) || (ptr->ResyncJumpWidth > 4))
return (6);
// if ((ptr->PhaseSEG1 > 2) && (ptr->PhaseSEG2 < ptr->PhaseSEG1))
// return (7);
// if ((ptr->PhaseSEG1 < 4) && (ptr->ResyncJumpWidth > ptr->PhaseSEG1))
// return (8);
if ((ptr->SAMPlingMode != 0) && (ptr->SAMPlingMode != 1))
return (9);
/* settings */
if (ptr->PREScalerDIVision != 0)
MCF5282_FLEXCAN_PRESDIV = ptr->PREScalerDIVision;
MCF5282_FLEXCAN_CANCTRL1 = (MCF5282_FLEXCAN_CANCTRL1 & ~0x07)
| MCF5282_FLEXCAN_CANCTRL1_PROPSEG(ptr->PROPagationSEG-1);
MCF5282_FLEXCAN_CANCTRL1 = (MCF5282_FLEXCAN_CANCTRL1 & ~0x80)
| (ptr->SAMPlingMode << 7);
MCF5282_FLEXCAN_CANCTRL2 = (0
| MCF5282_FLEXCAN_CANCTRL2_PSEG1(ptr->PhaseSEG1-1)
| MCF5282_FLEXCAN_CANCTRL2_PSEG2(ptr->PhaseSEG2-1)
| MCF5282_FLEXCAN_CANCTRL2_RJW(ptr->ResyncJumpWidth-1) );
return (0);
}
/************************************************************************
* NAME: can_flexcan_getBitRate
*
* DESCRIPTION: Reading CAN controller bitrate settings directly from
* FlexCan HW registers
*
************************************************************************/
uint8
can_flexcan_getBitRate(can_SpeedParam_t* ptr)
{
ptr->PREScalerDIVision = MCF5282_FLEXCAN_PRESDIV;
ptr->PROPagationSEG = (MCF5282_FLEXCAN_CANCTRL1 & 0x07) + 1;
ptr->PhaseSEG1 = ((MCF5282_FLEXCAN_CANCTRL2 >> 3) & 0x07) + 1;
ptr->PhaseSEG2 = ((MCF5282_FLEXCAN_CANCTRL2 >> 0) & 0x07) + 1;
ptr->ResyncJumpWidth = ((MCF5282_FLEXCAN_CANCTRL2 >> 6) & 0x03) + 1;
ptr->SAMPlingMode = (MCF5282_FLEXCAN_CANCTRL1 >> 7) & 0x01;
ptr->NumberOfTQ = 1 + // SYNCSEG=1
ptr->PROPagationSEG +
ptr->PhaseSEG1 +
ptr->PhaseSEG2;
ptr->CanSpeed = can_calcSpeedFromParams(ptr);
return (0);
}
/************************************************************************
* NAME: can_calcSpeedFromParams
*
* DESCRIPTION: Calculation speed value (in kBit/s)
* from low level params
*
************************************************************************/
uint32
can_calcSpeedFromParams (can_SpeedParam_t* ptr)
{
uint32 S_clock;
uint32 bit_clock;
S_clock = (SYSTEM_CLOCK * 1000000)/(2*(ptr->PREScalerDIVision + 1));
bit_clock = S_clock / ptr->NumberOfTQ;
return (bit_clock);
}
/************************************************************************
* NAME: can_err_GetLastError
*
* DESCRIPTION Return address of the error
* description structure for the CAN Unit errors.
************************************************************************/
can_err_ErrorDescript_t*
can_err_GetLastError()
{
can_err_ErrorDescript_t* ptr;
ptr = can_err_ErrorPointer.tail;
if (can_err_ErrorPointer.count > 0)
{
can_err_ErrorPointer.count--;
if (can_err_ErrorPointer.tail == can_err_ErrorPointer.ptr)
can_err_ErrorPointer.tail = (can_err_ErrorPointer.end - 1);
else
can_err_ErrorPointer.tail--;
return (ptr);
}
else
return ((can_err_ErrorDescript_t*) NULL);
}
/************************************************************************
* NAME: can_err_SendError
*
* DESCRIPTION Put an error to the error
* description structure for the CAN Unit errors.
************************************************************************/
uint32
can_err_SendError( can_err_ErrorTable_t* canErrTxtDescriptionPtr,
uint32 sysErrTime,
uint16 canFunctionId )
{
uint32 i;
can_err_ErrorPointer.head->canErrNumber = canErrTxtDescriptionPtr->canErrCode;
can_err_ErrorPointer.head->canErrTxtDescriptionPtr = canErrTxtDescriptionPtr;
can_err_ErrorPointer.head->sysErrTime = sysErrTime;
can_err_ErrorPointer.head->canFunctionId = canFunctionId;
can_err_ErrorPointer.tail = can_err_ErrorPointer.head;
can_err_ErrorPointer.head++;
if (can_err_ErrorPointer.head == can_err_ErrorPointer.end)
can_err_ErrorPointer.head = can_err_ErrorPointer.ptr;
if (can_err_ErrorPointer.count < CAN_UNIT_ERROR_DESCRIP_BUFFER_SIZE)
can_err_ErrorPointer.count++;
return (TRUE);
}
/************************************************************************
* NAME: can_unit_auto_integration
*
* DESCRIPTION Automatically integrates to the CAN bus.
************************************************************************/
uint8
can_unit_Auto_Integration()
{
uint32 i, j;
uint32 time_out = TIME_OUT_TIME;
can_unit_IsSpeedAutodetected = FALSE;
can_unit_Mode = can_ModeIntegration;
MCF5282_FLEXCAN_CANCTRL1 = MCF5282_FLEXCAN_CANCTRL1 | MCF5282_FLEXCAN_CANCTRL1_LOM;
for (i=0; i<CAN_SPEED_PARAM_NUM; i++)
{
can_flexcan_setBitRate(&can_SpeedParam[i]);
can_unitInit();
printf (" trying %d kBit/s, connecting to the bus... ", can_SpeedParam[i].CanSpeed/1000);
can_unit_Run();
while (can_unit_IntegrationState == can_Waiting && time_out != 0)
time_out--;
if (can_unit_IntegrationState == can_MessageSuccesfullyReceived)
{
MCF5282_FLEXCAN_CANCTRL1 = MCF5282_FLEXCAN_CANCTRL1 & 0xF7;
can_unit_Stop();
can_unit_IsSpeedAutodetected = TRUE;
printf ("Connected!\n CAN baudrate detected: %d kBit/s\n", can_SpeedParam[i].CanSpeed/1000);
break;
}
else if (can_unit_IntegrationState == can_ErrorOccured)
{
can_unit_IntegrationState = can_Waiting;
time_out = TIME_OUT_TIME;
MCF5282_FLEXCAN_CANCTRL0 = MCF5282_FLEXCAN_CANCTRL0 | MCF5282_FLEXCAN_CANCTRL0_ERRMSK;
printf ("Failed\n");
}
else if (time_out == 0)
{
time_out = TIME_OUT_TIME;
can_unit_IntegrationState = can_Waiting;
printf ("Timed out\n");
}
can_unit_Stop();
}
return can_unit_IsSpeedAutodetected;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -