lin_signal.c
来自「芯科原厂所有c8051fxx程序的例子。」· C语言 代码 · 共 1,024 行 · 第 1/3 页
C
1,024 行
else
{
// If the current value of led_master has already been transmitted,
// return 0 so no data will be sent in this frame slot.
retval = 0;
}
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_1)
if (!sw_slave_id_1_flag_tx)
{
// Load the PID of the associated unconditional frame into the
// first data byte to send.
// NOTE: The hardware will not calculate the parity bits for this
// frame ID because it is being loaded into LINDT0 instead of
// LINID. Instead, the software gets the PID by calling
// l_get_frame_pid().
frame_data[0] = l_get_frame_pid (SW_SLAVE_ID_1_UNCONDITIONAL_ID);
// Load sw_slave_id_1 into bit 1 of frame_data[1].
frame_data[1] = (l_bool) sw_slave_id_1;
}
else
{
// If the current value of led_master has already been transmitted,
// return 0 so no data will be sent in this frame slot.
retval = 0;
}
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_2)
if (!sw_slave_id_2_flag_tx)
{
// Load the PID of the associated unconditional frame into the
// first data byte to send.
// NOTE: The hardware will not calculate the parity bits for this
// frame ID because it is being loaded into LINDT0 instead of
// LINID. Instead, the software gets the PID by calling
// l_get_frame_pid().
frame_data[0] = l_get_frame_pid (SW_SLAVE_ID_2_UNCONDITIONAL_ID);
// Load sw_slave_id_2 into bit 1 of frame_data[1].
frame_data[1] = (l_bool) sw_slave_id_2;
}
else
{
// If the current value of led_master has already been transmitted,
// return 0 so no data will be sent in this frame slot.
retval = 0;
}
#endif
break;
#if (LIN_SLAVE_ID == LIN_SLAVE_ID_0)
case (SW_SLAVE_ID_0_UNCONDITIONAL_ID):
frame_data[0] = (l_bool) sw_slave_id_0;
break;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_1)
case (SW_SLAVE_ID_1_UNCONDITIONAL_ID):
frame_data[0] = (l_bool) sw_slave_id_1;
break;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_2)
case (SW_SLAVE_ID_2_UNCONDITIONAL_ID):
frame_data[0] = (l_bool) sw_slave_id_2;
break;
#endif
#endif
default:
retval = 0;
break;
}
return (retval);
}
#if (LIN_MODE == LIN_MASTER)
//----------------------------------------------------------------------------
// l_get_sporadic_frame_id ()
//-----------------------------------------------------------------------------
//
// This function is called by l_sch_tick_std_ifc () when the next frame in the
// schedule table is a sporadic frame.
//
// Given the ID of the sporadic frame, this function returns the ID
// of the unconditional frame that should be sent in the frame slot. If no
// frame data will be sent with the frame, the fuction returns
// SILENT_FRAME_SLOT.
//
// Sets or clears any flags as necessary
//
l_u8 l_get_sporadic_frame_id (l_u8 frame_id)
{
l_u8 retval;
// Initialize retval to EMPTY_FRAME_SLOT.
retval = SILENT_FRAME_SLOT;
switch (frame_id)
{
case (LED_SLAVE_SPORADIC_ID):
// Send LED_SLAVE_UNCONDITIONAL_ID if any of the its signals
// have been updated since the last time it was sent.
if (!led_slave_id_0_flag_tx || !led_slave_id_1_flag_tx || !led_slave_id_2_flag_tx)
retval = LED_SLAVE_UNCONDITIONAL_ID;
break;
}
return (retval);
}
#endif // LIN_MODE == LIN_MASTER
//----------------------------------------------------------------------------
// l_tx_complete ()
//-----------------------------------------------------------------------------
//
// This function is called when a frame has been successfully transmitted.
//
// Sets or clears any flags as necessary
//
// Parameters:
//
// 1. frame_id - The frame identifier of the frame that was sent.
//
// 2. frame_data - The data that was sent in the frame.
//
void l_tx_complete (l_u8 frame_id, l_u8 * frame_data)
{
switch (frame_id)
{
#if (LIN_MODE == LIN_MASTER)
case (LED_SLAVE_UNCONDITIONAL_ID):
// If the current values of the led_slave signals match the
// values that were sent, set the corresponding flag_tx flags to
// indicate that the current values of the signals have been
// successfully transmitted.
if ((l_bool)(frame_data[0] & 0x01) == led_slave_id_0)
led_slave_id_0_flag_tx = 1;
if ((l_bool)(frame_data[0] & 0x02) == led_slave_id_1)
led_slave_id_1_flag_tx = 1;
if ((l_bool)(frame_data[0] & 0x04) == led_slave_id_2)
led_slave_id_2_flag_tx = 1;
break;
#elif (LIN_MODE == LIN_SLAVE)
case (SW_SLAVE_EVENT_TRIGGERED_ID):
// If the current value of sw_slave_id_x matches the value that was
// sent, set sw_slave_id_x_flag_tx to indicated that the current value
// of sw_slave_id_x has been successfully transmitted.
#if (LIN_SLAVE_ID == LIN_SLAVE_ID_0)
if ((l_bool)(frame_data[1] & 0x01) == sw_slave_id_0)
sw_slave_id_0_flag_tx = 1;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_1)
if ((l_bool)(frame_data[1] & 0x01) == sw_slave_id_1)
sw_slave_id_1_flag_tx = 1;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_2)
if ((l_bool)(frame_data[1] & 0x01) == sw_slave_id_2)
sw_slave_id_2_flag_tx = 1;
#endif
break;
#if (LIN_SLAVE_ID == LIN_SLAVE_ID_0)
case (SW_SLAVE_ID_0_UNCONDITIONAL_ID):
if ((l_bool)(frame_data[0] & 0x01) == sw_slave_id_0)
sw_slave_id_0_flag_tx = 1;
break;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_1)
case (SW_SLAVE_ID_1_UNCONDITIONAL_ID):
if ((l_bool)(frame_data[0] & 0x01) == sw_slave_id_1)
sw_slave_id_1_flag_tx = 1;
break;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_2)
case (SW_SLAVE_ID_2_UNCONDITIONAL_ID):
if ((l_bool)(frame_data[0] & 0x01) == sw_slave_id_2)
sw_slave_id_2_flag_tx = 1;
break;
#endif
#endif
default:
break;
}
}
//----------------------------------------------------------------------------
// l_process_rx_frame_data ()
//-----------------------------------------------------------------------------
//
// Loads received data into corresponding signals and sets all relevant flags.
//
// Parameters:
//
// 1. frame_id - The frame identifier of the received frame.
//
// 2. frame_data - The data received in the frame.
//
// Returns TRUE if the received frame data is valid and FALSE if it is invalid.
// (Invalid data is detected if the master node receives an event-triggered
// frame, and the parity of the PID in data byte 1 does not match the expected
// parity).
//
l_bool l_process_rx_frame_data(l_u8 frame_id, l_u8 * frame_data)
{
l_bool retval = TRUE;
switch (frame_id)
{
#if (LIN_MODE == LIN_MASTER)
// Event-triggered frame containing sw_slave_id_x.
case (SW_SLAVE_EVENT_TRIGGERED_ID):
// Check the parity of the PID in the first data byte.
if (!l_test_parity (frame_data[0]))
{
retval = FALSE;
break;
}
if ((frame_data[0] & FRAME_ID) == SW_SLAVE_ID_0_UNCONDITIONAL_ID)
{
sw_slave_id_0 = (l_bool)(frame_data[1] & 0x01);
sw_slave_id_0_flag_rx = 1;
}
else if ((frame_data[0] & FRAME_ID) == SW_SLAVE_ID_1_UNCONDITIONAL_ID)
{
sw_slave_id_1 = (l_bool)(frame_data[1] & 0x01);
sw_slave_id_1_flag_rx = 1;
}
else if ((frame_data[0] & FRAME_ID) == SW_SLAVE_ID_2_UNCONDITIONAL_ID)
{
sw_slave_id_2 = (l_bool)(frame_data[1] & 0x01);
sw_slave_id_2_flag_rx = 1;
}
break;
// Unconditional frame containing sw_slave_id_0
case (SW_SLAVE_ID_0_UNCONDITIONAL_ID):
sw_slave_id_0 = (l_bool)(frame_data[0] & 0x01);
sw_slave_id_0_flag_rx = 1;
break;
// Unconditional frame containing sw_slave_id_1
case (SW_SLAVE_ID_1_UNCONDITIONAL_ID):
sw_slave_id_1 = (l_bool)(frame_data[0] & 0x01);
sw_slave_id_1_flag_rx = 1;
break;
// Unconditional frame containing sw_slave_id_2
case (SW_SLAVE_ID_2_UNCONDITIONAL_ID):
sw_slave_id_2 = (l_bool)(frame_data[0] & 0x01);
sw_slave_id_2_flag_rx = 1;
break;
#endif
#if (LIN_MODE == LIN_SLAVE)
// Unconditional frame containing led_slave.
case (LED_SLAVE_UNCONDITIONAL_ID):
#if (LIN_SLAVE_ID == LIN_SLAVE_ID_0)
// led_slave_id_0 is bit 0 of byte 0 of LED_SLAVE_UNCONDITIONAL_ID.
led_slave_id_0 = (l_bool) (frame_data[0] & 0x01);
led_slave_id_0_flag_rx = 1;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_1)
// led_slave_id_1 is bit 1 of byte 0 of LED_SLAVE_UNCONDITIONAL_ID.
led_slave_id_1 = (l_bool) (frame_data[0] & 0x02);
led_slave_id_1_flag_rx = 1;
#elif (LIN_SLAVE_ID == LIN_SLAVE_ID_2)
// led_slave_id_2 is bit 2 of byte 0 of LED_SLAVE_UNCONDITIONAL_ID.
led_slave_id_2 = (l_bool) (frame_data[0] & 0x04);
led_slave_id_2_flag_rx = 1;
#endif
break;
#endif
default:
break;
}
return (retval);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?