2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
* \file Source\com.c
|
|
|
|
* \brief Bootloader communication interface source file.
|
|
|
|
* \ingroup Core
|
|
|
|
* \internal
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* C O P Y R I G H T
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) 2011 by Feaser http://www.feaser.com All rights reserved
|
|
|
|
*
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* L I C E N S E
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with OpenBLT.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2016-03-01 06:24:23 -08:00
|
|
|
* A special exception to the GPL is included to allow you to distribute a combined work
|
|
|
|
* that includes OpenBLT without being obliged to provide the source code for any
|
2013-07-31 08:48:23 -07:00
|
|
|
* proprietary components. The exception text is included at the bottom of the license
|
|
|
|
* file <license.html>.
|
2016-03-01 06:24:23 -08:00
|
|
|
*
|
2013-07-31 08:48:23 -07:00
|
|
|
* \endinternal
|
2011-11-10 09:55:56 -08:00
|
|
|
****************************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Include files
|
|
|
|
****************************************************************************************/
|
|
|
|
#include "boot.h" /* bootloader generic header */
|
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
2016-03-01 06:24:23 -08:00
|
|
|
#include "can.h" /* can driver module */
|
2011-11-10 09:55:56 -08:00
|
|
|
#endif
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
2016-03-01 06:24:23 -08:00
|
|
|
#include "uart.h" /* uart driver module */
|
2011-11-10 09:55:56 -08:00
|
|
|
#endif
|
2011-12-15 14:53:57 -08:00
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
2016-03-01 06:24:23 -08:00
|
|
|
#include "usb.h" /* usb driver module */
|
2011-12-15 14:53:57 -08:00
|
|
|
#endif
|
2014-02-06 06:01:02 -08:00
|
|
|
#if (BOOT_COM_NET_ENABLE > 0)
|
2016-03-01 06:24:23 -08:00
|
|
|
#include "net.h" /* tcp/ip driver module */
|
2014-02-06 06:01:02 -08:00
|
|
|
#endif
|
2011-11-10 09:55:56 -08:00
|
|
|
|
|
|
|
|
2013-05-22 06:37:47 -07:00
|
|
|
#if (BOOT_COM_ENABLE > 0)
|
2011-11-10 09:55:56 -08:00
|
|
|
/****************************************************************************************
|
|
|
|
* Local data declarations
|
|
|
|
****************************************************************************************/
|
2014-05-26 16:14:30 -07:00
|
|
|
/** \brief Holds the communication interface of the currently active interface. */
|
|
|
|
static tComInterfaceId comActiveInterface = COM_IF_OTHER;
|
|
|
|
|
2011-11-10 09:55:56 -08:00
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
2016-03-01 06:24:23 -08:00
|
|
|
** \brief Initializes the communication module including the hardware needed for
|
2013-07-31 08:48:23 -07:00
|
|
|
** the communication.
|
|
|
|
** \return none
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void ComInit(void)
|
|
|
|
{
|
|
|
|
/* initialize the XCP communication protocol */
|
|
|
|
XcpInit();
|
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
|
|
|
/* initialize the CAN controller */
|
|
|
|
CanInit();
|
2014-05-26 16:14:30 -07:00
|
|
|
/* set it as active */
|
|
|
|
comActiveInterface = COM_IF_CAN;
|
2011-11-10 09:55:56 -08:00
|
|
|
#endif
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
|
|
|
/* initialize the UART interface */
|
|
|
|
UartInit();
|
2014-05-26 16:14:30 -07:00
|
|
|
/* set it as active */
|
|
|
|
comActiveInterface = COM_IF_UART;
|
2011-12-15 14:53:57 -08:00
|
|
|
#endif
|
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
|
|
/* initialize the USB interface */
|
|
|
|
UsbInit();
|
2014-05-26 16:14:30 -07:00
|
|
|
/* set it as active */
|
|
|
|
comActiveInterface = COM_IF_USB;
|
2014-02-06 06:01:02 -08:00
|
|
|
#endif
|
|
|
|
#if (BOOT_COM_NET_ENABLE > 0)
|
|
|
|
/* initialize the TCP/IP interface */
|
|
|
|
NetInit();
|
2014-05-26 16:14:30 -07:00
|
|
|
/* set it as active */
|
|
|
|
comActiveInterface = COM_IF_NET;
|
2011-11-10 09:55:56 -08:00
|
|
|
#endif
|
|
|
|
} /*** end of ComInit ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
2016-03-01 06:24:23 -08:00
|
|
|
** \brief Updates the communication module by checking if new data was received
|
2013-07-31 08:48:23 -07:00
|
|
|
** and submitting the request to process newly received data.
|
|
|
|
** \return none
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void ComTask(void)
|
|
|
|
{
|
|
|
|
/* make xcpCtoReqPacket static for runtime efficiency */
|
2015-03-30 02:20:44 -07:00
|
|
|
static blt_int8u xcpCtoReqPacket[BOOT_COM_RX_MAX_DATA];
|
2016-03-01 06:24:23 -08:00
|
|
|
|
2011-11-10 09:55:56 -08:00
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
|
|
|
if (CanReceivePacket(&xcpCtoReqPacket[0]) == BLT_TRUE)
|
|
|
|
{
|
2014-05-26 16:14:30 -07:00
|
|
|
/* make this the active interface */
|
|
|
|
comActiveInterface = COM_IF_CAN;
|
2011-11-10 09:55:56 -08:00
|
|
|
/* process packet */
|
|
|
|
XcpPacketReceived(&xcpCtoReqPacket[0]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
|
|
|
if (UartReceivePacket(&xcpCtoReqPacket[0]) == BLT_TRUE)
|
|
|
|
{
|
2014-05-26 16:14:30 -07:00
|
|
|
/* make this the active interface */
|
|
|
|
comActiveInterface = COM_IF_UART;
|
2011-11-10 09:55:56 -08:00
|
|
|
/* process packet */
|
|
|
|
XcpPacketReceived(&xcpCtoReqPacket[0]);
|
|
|
|
}
|
|
|
|
#endif
|
2011-12-15 14:53:57 -08:00
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
|
|
if (UsbReceivePacket(&xcpCtoReqPacket[0]) == BLT_TRUE)
|
|
|
|
{
|
2014-05-26 16:14:30 -07:00
|
|
|
/* make this the active interface */
|
|
|
|
comActiveInterface = COM_IF_USB;
|
2011-12-15 14:53:57 -08:00
|
|
|
/* process packet */
|
|
|
|
XcpPacketReceived(&xcpCtoReqPacket[0]);
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-06 06:01:02 -08:00
|
|
|
#if (BOOT_COM_NET_ENABLE > 0)
|
|
|
|
if (NetReceivePacket(&xcpCtoReqPacket[0]) == BLT_TRUE)
|
|
|
|
{
|
2014-05-26 16:14:30 -07:00
|
|
|
/* make this the active interface */
|
|
|
|
comActiveInterface = COM_IF_NET;
|
2014-02-06 06:01:02 -08:00
|
|
|
/* process packet */
|
|
|
|
XcpPacketReceived(&xcpCtoReqPacket[0]);
|
|
|
|
}
|
|
|
|
#endif
|
2011-11-10 09:55:56 -08:00
|
|
|
} /*** end of ComTask ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Releases the communication module.
|
|
|
|
** \return none
|
2011-12-15 14:53:57 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void ComFree(void)
|
|
|
|
{
|
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
|
|
/* disconnect the usb device from the usb host */
|
|
|
|
UsbFree();
|
|
|
|
#endif
|
|
|
|
} /*** end of ComFree ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Transmits the packet using the xcp transport layer.
|
|
|
|
** \param data Pointer to the byte buffer with packet data.
|
|
|
|
** \param len Number of data bytes that need to be transmitted.
|
|
|
|
** \return none
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void ComTransmitPacket(blt_int8u *data, blt_int16u len)
|
|
|
|
{
|
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
2013-07-31 08:48:23 -07:00
|
|
|
/* transmit the packet. note that len is limited to 8 in the plausibility check,
|
|
|
|
* so cast is okay.
|
|
|
|
*/
|
2014-05-26 16:14:30 -07:00
|
|
|
if (comActiveInterface == COM_IF_CAN)
|
|
|
|
{
|
|
|
|
CanTransmitPacket(data, (blt_int8u)len);
|
|
|
|
}
|
2011-11-10 09:55:56 -08:00
|
|
|
#endif
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
2013-07-31 08:48:23 -07:00
|
|
|
/* transmit the packet. note that len is limited to 255 in the plausibility check,
|
|
|
|
* so cast is okay.
|
|
|
|
*/
|
2014-05-26 16:14:30 -07:00
|
|
|
if (comActiveInterface == COM_IF_UART)
|
|
|
|
{
|
|
|
|
UartTransmitPacket(data, (blt_int8u)len);
|
|
|
|
}
|
2011-11-10 09:55:56 -08:00
|
|
|
#endif
|
2011-12-15 14:53:57 -08:00
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
|
|
/* transmit the packet */
|
2014-05-26 16:14:30 -07:00
|
|
|
if (comActiveInterface == COM_IF_USB)
|
|
|
|
{
|
|
|
|
UsbTransmitPacket(data, len);
|
|
|
|
}
|
2011-12-15 14:53:57 -08:00
|
|
|
#endif
|
2014-02-06 06:01:02 -08:00
|
|
|
#if (BOOT_COM_NET_ENABLE > 0)
|
2014-05-26 16:14:30 -07:00
|
|
|
if (comActiveInterface == COM_IF_NET)
|
|
|
|
{
|
|
|
|
/* transmit the packet */
|
|
|
|
NetTransmitPacket(data, len);
|
|
|
|
}
|
2014-02-06 06:01:02 -08:00
|
|
|
#endif
|
2011-11-10 09:55:56 -08:00
|
|
|
|
|
|
|
/* send signal that the packet was transmitted */
|
|
|
|
XcpPacketTransmitted();
|
|
|
|
} /*** end of ComTransmitPacket ***/
|
|
|
|
|
|
|
|
|
2014-05-26 16:14:30 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Obtains the maximum number of bytes that can be received on the specified
|
|
|
|
** communication interface.
|
|
|
|
** \return Maximum number of bytes that can be received.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
blt_int16u ComGetActiveInterfaceMaxRxLen(void)
|
|
|
|
{
|
|
|
|
blt_int16u result;
|
2016-03-01 06:24:23 -08:00
|
|
|
|
2014-05-26 16:14:30 -07:00
|
|
|
/* filter on communication interface identifier */
|
|
|
|
switch (comActiveInterface)
|
|
|
|
{
|
|
|
|
case COM_IF_UART:
|
|
|
|
result = BOOT_COM_UART_RX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COM_IF_CAN:
|
|
|
|
result = BOOT_COM_CAN_RX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COM_IF_USB:
|
|
|
|
result = BOOT_COM_USB_RX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COM_IF_NET:
|
|
|
|
result = BOOT_COM_NET_RX_MAX_DATA;
|
|
|
|
break;
|
2016-03-01 06:24:23 -08:00
|
|
|
|
2014-05-26 16:14:30 -07:00
|
|
|
default:
|
|
|
|
result = BOOT_COM_RX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-01 06:24:23 -08:00
|
|
|
|
2014-05-26 16:14:30 -07:00
|
|
|
return result;
|
|
|
|
} /*** end of ComGetActiveInterfaceMaxRxLen ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
2016-03-01 06:24:23 -08:00
|
|
|
** \brief Obtains the maximum number of bytes that can be transmitted on the
|
2014-05-26 16:14:30 -07:00
|
|
|
** specified communication interface.
|
|
|
|
** \return Maximum number of bytes that can be received.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
blt_int16u ComGetActiveInterfaceMaxTxLen(void)
|
|
|
|
{
|
|
|
|
blt_int16u result;
|
2016-03-01 06:24:23 -08:00
|
|
|
|
2014-05-26 16:14:30 -07:00
|
|
|
/* filter on communication interface identifier */
|
|
|
|
switch (comActiveInterface)
|
|
|
|
{
|
|
|
|
case COM_IF_UART:
|
|
|
|
result = BOOT_COM_UART_TX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COM_IF_CAN:
|
|
|
|
result = BOOT_COM_CAN_TX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COM_IF_USB:
|
|
|
|
result = BOOT_COM_USB_TX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COM_IF_NET:
|
|
|
|
result = BOOT_COM_NET_TX_MAX_DATA;
|
|
|
|
break;
|
2016-03-01 06:24:23 -08:00
|
|
|
|
2014-05-26 16:14:30 -07:00
|
|
|
default:
|
|
|
|
result = BOOT_COM_TX_MAX_DATA;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-01 06:24:23 -08:00
|
|
|
|
2014-05-26 16:14:30 -07:00
|
|
|
return result;
|
|
|
|
} /*** end of ComGetActiveInterfaceMaxTxLen ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief This function obtains the XCP connection state.
|
|
|
|
** \return BLT_TRUE when an XCP connection is established, BLT_FALSE otherwise.
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
blt_bool ComIsConnected(void)
|
|
|
|
{
|
|
|
|
return XcpIsConnected();
|
|
|
|
} /*** end of ComIsConnected ***/
|
|
|
|
|
2013-05-22 06:37:47 -07:00
|
|
|
#endif /* BOOT_COM_ENABLE > 0 */
|
2011-11-10 09:55:56 -08:00
|
|
|
|
|
|
|
/*********************************** end of com.c **************************************/
|