📄 lin_api.c
字号:
/******************************************************************************/
/* Renesas Technology America, Inc. Legal Disclaimer */
/******************************************************************************/
/* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY */
/* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR */
/* PURPOSE. */
/* */
/* BY USING THE SOFTWARE HEREIN, YOU (AND YOUR COMPANY) AGREE TO BE BOUND BY */
/* THE TERMS AND CONDITIONS OF RENESAS TECHNOLOGY AMERICA, INC.'S SOURCE CODE */
/* LICENSE AGREEMENT. PLEASE READ THIS AGREEMENT CAREFULLY. IF YOU (OR YOUR */
/* COMPANY) DO NOT AGREE TO ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT, */
/* DO NOT INSTALL OR USE THIS SOFTWARE AND ASSOCIATED DOCUMENTATION. */
/* */
/* Copyright (c) 2003, 2004, 2005 and 2006 Renesas Technology America, Inc. */
/******************************************************************************/
/**********************************************************************
Title : lin_api.c
Module Description : This file contains the LIN 2.0 API function routines.
MCU Family : R8C
Author : Bob Chamberlain
Version : LIN API 2.0 Version 1.1
*********************************************************************/
/**********************************************************************
* Include header files
*********************************************************************/
#include "lin_api.h"
/*********************************************************************
* File level pragmas
*********************************************************************/
/**********************************************************************
* Constant and Macro Definitions using #define
*********************************************************************/
/**********************************************************************
* Enumerations, Structures and Typedefs
*********************************************************************/
/**********************************************************************
* Global and Const Variable Defining Definitions / Initializations
*********************************************************************/
/**********************************************************************
* Static Variables With File Level Scope
*********************************************************************/
/**********************************************************************
* ROM Const Variables With File Level Scope
*********************************************************************/
/**********************************************************************
* Function Prototypes for Private Functions with File Level Scope
*********************************************************************/
/**********************************************************************
* Function Definitions
*********************************************************************/
/**********************************************************************
* Function Name: l_sys_irq_disable()
* Description: Disables all interrupts.
* Parameters: None.
* Returns: Interrupt enable flag state prior to the call.
* Ext. References: None.
* Preemptible: Yes.
* Reentrant: Yes.
*********************************************************************/
l_irqmask l_sys_irq_disable(void)
{
unsigned int rdflg;
unsigned char flg_state;
/* Read the current state of the flag register. */
_asm("STC FLG,$$[FB]", rdflg);
_asm("FCLR I"); /* Clear the global interrupt enable flag. */
/* Determine if the I flag is set in the flag register. */
if ((rdflg & 0x0040) != 0)
{
flg_state = 0x01;
}
else
{
flg_state = 0x00;
}
return (flg_state); /* Return prior state of the interrupt mask bit. */
}
/**********************************************************************
* Function Name: l_sys_irq_restore()
* Description: Restores a value to the interrupt mask bit.
* Parameters: flag_state - flag value to restore.
* Returns: Nothing.
* Ext. References: None.
* Preemptible: Yes.
* Reentrant: Yes.
*********************************************************************/
void l_sys_irq_restore(l_irqmask flag_state)
{
if (flag_state)
{
_asm("FSET I"); /* Set the global interrupt enable flag. */
}
else
{
_asm("FCLR I"); /* Clear the global interrupt enable flag. */
}
return;
}
/**********************************************************************
* Function Name: l_sys_init()
* Description: Initializes the I/O pins used by the LIN system
* resources.
* Parameters: None.
* Returns: Always returns 0.
* Ext. References: None.
* Preemptible: Yes.
* Reentrant: Yes.
*
*********************************************************************/
l_bool l_sys_init(void)
{
/* Set up the I/O pins used by the LIN interface. */
LIN_CONTROL_REG = 0x00; /* Take the interface out of LIN mode for the moment. */
LIN_PORT_RXD_DIR; /* Make sure the UART RXD pin is an input. */
LIN_PORT_DATA_OUT = 1; /* Set the TXD port latch to 1 (inactive LIN Bus level). */
LIN_PORT_TXD_DIR; /* Configure the TXD port to output (when in I/O mode). */
LIN_UART_MODE_REG = 0x00; /* Clear UART mode to place RxD and TxD into I/O mode. */
#ifdef LIN_XCVR_ENABLE
LIN_TXCVR_ENABLE; /* Set the LIN transceiver enable port to enabled state. */
LIN_TXCVR_ENABLE_DIR; /* Set the LIN transceiver enable port to output. */
#endif
#ifdef LIN_CORE_TIME_ENABLE
LIN_CORE_TIME = 0; /* Set the time measurement pin to 0 (off). */
LIN_CORE_TIME_DIR; /* Set the time measurement pin to an output. */
#endif
return (0);
}
/**********************************************************************
* Function Name: l_ifc_init()
* Description: Initializes the LIN interface hardware resources.
* Parameters: ifc_name - index number of the LIN interface - only
* 0 is allowed.
* Returns: Nothing.
* Ext. References: LIN_TX_END_FLG, LIN_RX_END_FLG.
* Preemptible: No.
* Reentrant: No.
*
* Note: This routine assumes that each node implements a single LIN
* interface, defined as interface 0 in "lin_dev.h" (the value of "LIN"
* is set to 0 by default). The API software should be modified to
* allow multiple LIN interfaces to be implemented.
*********************************************************************/
void l_ifc_init(l_ifc_handle ifc_name)
{
/* Do nothing if the specified interface is not interface 0. */
if (ifc_name == LIN)
{
#if (LIN_MASTER_NODE == LIN_MY_NODE_NAME)
lin_init_schedule_timer(); /* Configure the LIN schedule and break timers. */
LIN_now_schedule = LIN_L_NULL_SCHEDULE; /* Set the schedule table to the "empty" table. */
#else
lin_init_timer(); /* Configure the LIN break timer. */
#endif
lin_init_serial_if(); /* Configure the LIN serial interface. */
/* Initialize the system API flags. LIN_AWAKE_FLG is cleared in "lin_change_node_status()". */
API_TX_END_FLG = 0; /* Initialize LIN_TX_END_FLG. */
API_RX_END_FLG = 0; /* Initialize LIN_RX_END_FLG. */
lin_change_node_status(DISCONNECT); /* Place the node in the "disconnect" state so it cannot be used. */
lin_change_bus_status(QUIET_STATE); /* Set the LIN bus state to inactive. */
}
}
/**********************************************************************
* Function Name: l_ifc_connect()
* Description: Places the LIN interface in the "connect" state for use.
* Parameters: ifc_name - index number of the LIN interface - only
* 0 is allowed.
* Returns: Always 0.
* Ext. References: No direct external references.
* Preemptible: No.
* Reentrant: No.
*
* Note: This routine assumes that each node implements a single LIN
* interface, defined as interface 0 in "lin_dev.h" (the value of "LIN"
* is set to 0 by default). The API software should be modified to
* allow multiple LIN interfaces to be implemented.
*********************************************************************/
l_bool l_ifc_connect(l_ifc_handle ifc_name)
{
/* Do nothing if the specified interface is not interface 0. */
if (ifc_name == LIN)
{
lin_change_node_status(CONNECT); /* Place the node in the "connect" state so it can be used. */
lin_change_bus_status(QUIET_STATE); /* Set the LIN bus state to inactive. */
}
return (0); /* Always returns 0 (success), which is OK as long as no errors can occur. */
}
/**********************************************************************
* Function Name: l_ifc_disconnect()
* Description: Returns the LIN interface to the idle state after use.
* Parameters: ifc_name - index number of the LIN interface - only
* 0 is allowed.
* Returns: Always 0.
* Ext. References: No direct external references.
* Preemptible: No.
* Reentrant: No.
*
* Note: This routine assumes that each node implements a single LIN
* interface, defined as interface 0 in "lin_dev.h" (the value of "LIN"
* is set to 0 by default). The API software should be modified to
* allow multiple LIN interfaces to be implemented.
*********************************************************************/
l_bool l_ifc_disconnect(l_ifc_handle ifc_name)
{
/* Do nothing if the specified interface is not interface 0. */
if (ifc_name == LIN)
{
/* Wait for the LIN bus to enter the quiet state (between LIN frames). */
while (LIN_bus_status != QUIET_STATE);
lin_change_node_status(DISCONNECT); /* Place the node in the "disconnect" state so it cannot be used. */
}
return (0); /* Always returns 0 (success), which is OK as long as no errors can occur. */
}
/**********************************************************************
* Function Name: l_flg_tst()
* Description: Reads a system flag or signal status byte.
* Parameters: flg - possible values:
* any valid signal name defined in "lin_dev.h"
* LIN_TX_END_FLG (253)
* LIN_RX_END_FLG (254)
* LIN_AWAKE_FLG (254)
* LIN_DIAG_RESP_FLG (255).
* Returns: Boolean 1 if the requested flag or status byte is not 0.
* Ext. References: LIN_sleep_timer, system flag referenced by "flg".
* Preemptible: Yes.
* Reentrant: No.
*********************************************************************/
l_bool l_flg_tst(l_flag_handle flg)
{
unsigned char flag_state;
unsigned char ret_val;
#if ((LIN_MASTER_NODE != LIN_MY_NODE_NAME) && (defined USE_POWER_CONTROL))
/* If this is a Slave node, put the node in the sleep state if "LIN_sleep_timer"
* is greater than 4. */
if (LIN_sleep_timer > 4)
{
LIN_sleep_timer = 0;
flag_state = l_sys_irq_disable(); /* Disable the global interrupt. */
lin_change_node_status(SLEEP_STATE);
l_sys_irq_restore(flag_state); /* Restore the global interrupt. */
}
#endif /* Slave node and USE_POWER_CONTROL is defined. */
/* If "flg" has the value 252, 253, 254 or 255, this request is for one of the
* API flags LIN TX_END_FLG, LIN_END_FLG, LIN_AWAKE_FLG or LIN_DIAG_RESP_FLG.
* Otherwise, the request is for the status of one of the user signals defined
* in "LIN_signal_table[]". */
if (flg > 251)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -