2016-02-25 02:46:45 -08:00
|
|
|
/************************************************************************************//**
|
2018-07-17 06:06:06 -07:00
|
|
|
* \file Source/TRICORE_TC1798/uart.c
|
2016-02-25 02:46:45 -08:00
|
|
|
* \brief Bootloader UART communication interface source file.
|
|
|
|
* \ingroup Target_TRICORE_TC1798
|
|
|
|
* \internal
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* C O P Y R I G H T
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) 2015 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.
|
|
|
|
*
|
2016-07-25 01:33:52 -07:00
|
|
|
* You have received a copy of the GNU General Public License along with OpenBLT. It
|
2016-04-30 15:52:15 -07:00
|
|
|
* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
|
2016-03-01 06:24:23 -08:00
|
|
|
*
|
2016-02-25 02:46:45 -08:00
|
|
|
* \endinternal
|
|
|
|
****************************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Include files
|
|
|
|
****************************************************************************************/
|
|
|
|
#include "boot.h" /* bootloader generic header */
|
2016-02-25 05:51:12 -08:00
|
|
|
#include "cpu_comp.h" /* compiler specific CPU definitions */
|
2016-02-25 02:46:45 -08:00
|
|
|
|
|
|
|
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
|
|
|
/****************************************************************************************
|
|
|
|
* Type definitions
|
|
|
|
****************************************************************************************/
|
|
|
|
typedef struct
|
|
|
|
{
|
2016-03-01 06:24:23 -08:00
|
|
|
blt_int32u reserved0[0x1]; /* 0x0 */
|
|
|
|
ASCn_PISEL_t PISEL; /* 0x4 */
|
|
|
|
ASCn_ID_t ID; /* 0x8 */
|
|
|
|
blt_int32u reserved3[0x1]; /* 0xc */
|
|
|
|
ASCn_CON_t CON; /* 0x10 */
|
|
|
|
ASCn_BG_t BG; /* 0x14 */
|
|
|
|
ASCn_FDV_t FDV; /* 0x18 */
|
|
|
|
blt_int32u reserved7[0x1]; /* 0x1c */
|
|
|
|
ASCn_TBUF_t TBUF; /* 0x20 */
|
|
|
|
ASCn_RBUF_t RBUF; /* 0x24 */
|
|
|
|
blt_int32u reserved10[0xa]; /* 0x28 */
|
|
|
|
ASCn_WHBCON_t WHBCON; /* 0x50 */
|
|
|
|
blt_int32u reserved12[0x27]; /* 0x54 */
|
|
|
|
ASCn_TSRC_t TSRC; /* 0xf0 */
|
|
|
|
ASCn_RSRC_t RSRC; /* 0xf4 */
|
|
|
|
ASCn_ESRC_t ESRC; /* 0xf8 */
|
|
|
|
ASCn_TBSRC_t TBSRC; /* 0xfc */
|
2016-02-25 02:46:45 -08:00
|
|
|
} tUartRegs;
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Macro definitions
|
|
|
|
****************************************************************************************/
|
|
|
|
/** \brief Timeout time for the reception of a CTO packet. The timer is started upon
|
|
|
|
* reception of the first packet byte.
|
|
|
|
*/
|
|
|
|
#define UART_CTO_RX_PACKET_TIMEOUT_MS (100u)
|
2018-04-25 06:49:24 -07:00
|
|
|
/** \brief Timeout for transmitting a byte in milliseconds. */
|
|
|
|
#define UART_BYTE_TX_TIMEOUT_MS (10u)
|
2016-02-25 02:46:45 -08:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Register definitions
|
|
|
|
****************************************************************************************/
|
|
|
|
#if (BOOT_COM_UART_CHANNEL_INDEX == 0)
|
|
|
|
/** \brief Set UART base address to ASC0. */
|
|
|
|
#define UARTx ((tUartRegs *) (blt_int32u)0xf0000a00)
|
|
|
|
#elif (BOOT_COM_UART_CHANNEL_INDEX == 1)
|
|
|
|
/** \brief Set UART base address to ASC1. */
|
|
|
|
#define UARTx ((tUartRegs *) (blt_int32u)0xf0000b00)
|
|
|
|
#else
|
|
|
|
/** \brief Set UART base address to ASC0 by default. */
|
|
|
|
#define UARTx ((tUartRegs *) (blt_int32u)0xf0000a00)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Function prototypes
|
|
|
|
****************************************************************************************/
|
|
|
|
static blt_bool UartReceiveByte(blt_int8u *data);
|
|
|
|
static blt_bool UartTransmitByte(blt_int8u data);
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Initializes the UART communication interface.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void UartInit(void)
|
|
|
|
{
|
|
|
|
blt_int32u frequency, reload_value, fdv, dfreq;
|
|
|
|
|
2016-03-01 06:24:23 -08:00
|
|
|
/* Compute system frequency and reload value for ASC */
|
|
|
|
frequency = BOOT_CPU_SYSTEM_SPEED_KHZ * 1000;
|
2016-02-25 02:46:45 -08:00
|
|
|
|
2016-03-01 06:24:23 -08:00
|
|
|
/* reload_value = fdv/512 * freq/16/baudrate -1 ==>
|
2016-02-25 02:46:45 -08:00
|
|
|
* reload_value = (512*freq)/(baudrate * 512*16) - 1
|
|
|
|
* fdv = (reload_value + 1) * (baudrate*512*16/freq)
|
|
|
|
* reload_value = (frequency / 32) / baudrate - 1;
|
2016-03-01 06:24:23 -08:00
|
|
|
*/
|
|
|
|
reload_value = (frequency / ((blt_int32u)BOOT_COM_UART_BAUDRATE * 16)) - 1;
|
|
|
|
dfreq = frequency / (16*512);
|
|
|
|
fdv = (reload_value + 1) * (blt_int32u)BOOT_COM_UART_BAUDRATE / dfreq;
|
2016-02-25 02:46:45 -08:00
|
|
|
|
2016-03-01 06:24:23 -08:00
|
|
|
/* enable ASC module */
|
|
|
|
CpuEnterInitMode();
|
|
|
|
ASC0_CLC.bits.RMC = 1;
|
|
|
|
ASC0_CLC.bits.DISR = 0;
|
|
|
|
CpuLeaveInitMode();
|
2016-02-25 02:46:45 -08:00
|
|
|
|
|
|
|
/* configure the ASC module for 8,n,1 */
|
|
|
|
UARTx->CON.reg = 0;
|
2016-03-01 06:24:23 -08:00
|
|
|
UARTx->BG.reg = reload_value;
|
|
|
|
UARTx->FDV.reg = fdv;
|
2016-02-25 02:46:45 -08:00
|
|
|
|
2016-03-01 06:24:23 -08:00
|
|
|
UARTx->CON.bits.M = 0x01;
|
|
|
|
UARTx->CON.bits.R = 1;
|
|
|
|
UARTx->CON.bits.REN = 1;
|
|
|
|
UARTx->CON.bits.FDE = 1;
|
2016-02-25 02:46:45 -08:00
|
|
|
} /*** end of UartInit ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Transmits a packet formatted for the communication interface.
|
|
|
|
** \param data Pointer to byte array with data that it to be transmitted.
|
|
|
|
** \param len Number of bytes that are to be transmitted.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void UartTransmitPacket(blt_int8u *data, blt_int8u len)
|
|
|
|
{
|
|
|
|
blt_int16u data_index;
|
|
|
|
blt_bool result;
|
|
|
|
|
|
|
|
/* verify validity of the len-paramenter */
|
2016-03-01 06:24:23 -08:00
|
|
|
ASSERT_RT(len <= BOOT_COM_UART_TX_MAX_DATA);
|
2016-02-25 02:46:45 -08:00
|
|
|
|
2016-03-01 06:24:23 -08:00
|
|
|
/* first transmit the length of the packet */
|
2016-02-25 02:46:45 -08:00
|
|
|
result = UartTransmitByte(len);
|
2016-03-01 06:24:23 -08:00
|
|
|
ASSERT_RT(result == BLT_TRUE);
|
|
|
|
|
2016-02-25 02:46:45 -08:00
|
|
|
/* transmit all the packet bytes one-by-one */
|
|
|
|
for (data_index = 0; data_index < len; data_index++)
|
|
|
|
{
|
|
|
|
/* keep the watchdog happy */
|
|
|
|
CopService();
|
|
|
|
/* write byte */
|
|
|
|
result = UartTransmitByte(data[data_index]);
|
2016-03-01 06:24:23 -08:00
|
|
|
ASSERT_RT(result == BLT_TRUE);
|
2016-02-25 02:46:45 -08:00
|
|
|
}
|
|
|
|
} /*** end of UartTransmitPacket ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Receives a communication interface packet if one is present.
|
|
|
|
** \param data Pointer to byte array where the data is to be stored.
|
2018-02-09 08:59:41 -08:00
|
|
|
** \param len Pointer where the length of the packet is to be stored.
|
2016-02-25 02:46:45 -08:00
|
|
|
** \return BLT_TRUE if a packet was received, BLT_FALSE otherwise.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
2018-02-09 08:59:41 -08:00
|
|
|
blt_bool UartReceivePacket(blt_int8u *data, blt_int8u *len)
|
2016-02-25 02:46:45 -08:00
|
|
|
{
|
|
|
|
static blt_int8u xcpCtoReqPacket[BOOT_COM_UART_RX_MAX_DATA+1]; /* one extra for length */
|
|
|
|
static blt_int8u xcpCtoRxLength;
|
|
|
|
static blt_bool xcpCtoRxInProgress = BLT_FALSE;
|
|
|
|
static blt_int32u xcpCtoRxStartTime = 0;
|
|
|
|
|
|
|
|
/* start of cto packet received? */
|
|
|
|
if (xcpCtoRxInProgress == BLT_FALSE)
|
|
|
|
{
|
|
|
|
/* store the message length when received */
|
|
|
|
if (UartReceiveByte(&xcpCtoReqPacket[0]) == BLT_TRUE)
|
|
|
|
{
|
2018-02-09 02:12:04 -08:00
|
|
|
if ( (xcpCtoReqPacket[0] > 0) &&
|
|
|
|
(xcpCtoReqPacket[0] <= BOOT_COM_UART_RX_MAX_DATA) )
|
2016-02-25 02:46:45 -08:00
|
|
|
{
|
|
|
|
/* store the start time */
|
|
|
|
xcpCtoRxStartTime = TimerGet();
|
|
|
|
/* reset packet data count */
|
|
|
|
xcpCtoRxLength = 0;
|
|
|
|
/* indicate that a cto packet is being received */
|
|
|
|
xcpCtoRxInProgress = BLT_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* store the next packet byte */
|
|
|
|
if (UartReceiveByte(&xcpCtoReqPacket[xcpCtoRxLength+1]) == BLT_TRUE)
|
|
|
|
{
|
|
|
|
/* increment the packet data count */
|
|
|
|
xcpCtoRxLength++;
|
|
|
|
|
|
|
|
/* check to see if the entire packet was received */
|
|
|
|
if (xcpCtoRxLength == xcpCtoReqPacket[0])
|
|
|
|
{
|
|
|
|
/* copy the packet data */
|
2016-03-01 06:24:23 -08:00
|
|
|
CpuMemCopy((blt_int32u)data, (blt_int32u)&xcpCtoReqPacket[1], xcpCtoRxLength);
|
2016-02-25 02:46:45 -08:00
|
|
|
/* done with cto packet reception */
|
|
|
|
xcpCtoRxInProgress = BLT_FALSE;
|
2018-02-09 08:59:41 -08:00
|
|
|
/* set the packet length */
|
|
|
|
*len = xcpCtoRxLength;
|
2016-02-25 02:46:45 -08:00
|
|
|
/* packet reception complete */
|
|
|
|
return BLT_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check packet reception timeout */
|
|
|
|
if (TimerGet() > (xcpCtoRxStartTime + UART_CTO_RX_PACKET_TIMEOUT_MS))
|
|
|
|
{
|
|
|
|
/* cancel cto packet reception due to timeout. note that that automaticaly
|
|
|
|
* discards the already received packet bytes, allowing the host to retry.
|
|
|
|
*/
|
|
|
|
xcpCtoRxInProgress = BLT_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* packet reception not yet complete */
|
|
|
|
return BLT_FALSE;
|
|
|
|
} /*** end of UartReceivePacket ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Receives a communication interface byte if one is present.
|
|
|
|
** \param data Pointer to byte where the data is to be stored.
|
|
|
|
** \return BLT_TRUE if a byte was received, BLT_FALSE otherwise.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
static blt_bool UartReceiveByte(blt_int8u *data)
|
|
|
|
{
|
|
|
|
blt_bool result = BLT_FALSE;
|
|
|
|
|
|
|
|
/* reception event pending? */
|
|
|
|
if (UARTx->RSRC.bits.SRR != 0)
|
|
|
|
{
|
|
|
|
/* read out the newly received byte */
|
|
|
|
*data = UARTx->RBUF.bits.RD_VALUE;
|
|
|
|
/* reset the reception event flag */
|
|
|
|
UARTx->RSRC.bits.CLRR = 1;
|
|
|
|
/* set result to indicate that a new byte was received */
|
2016-03-01 06:24:23 -08:00
|
|
|
result = BLT_TRUE;
|
2016-02-25 02:46:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* inform caller about the result */
|
|
|
|
return result;
|
|
|
|
} /*** end of UartReceiveByte ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Transmits a communication interface byte.
|
|
|
|
** \param data Value of byte that is to be transmitted.
|
|
|
|
** \return BLT_TRUE if the byte was transmitted, BLT_FALSE otherwise.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
static blt_bool UartTransmitByte(blt_int8u data)
|
|
|
|
{
|
2018-04-25 06:49:24 -07:00
|
|
|
blt_int32u timeout;
|
|
|
|
blt_bool result = BLT_TRUE;
|
|
|
|
|
2016-02-25 02:46:45 -08:00
|
|
|
/* reset transmit buffer interrupt request */
|
|
|
|
UARTx->TBSRC.bits.CLRR = 1;
|
|
|
|
/* write byte to transmit buffer register */
|
|
|
|
UARTx->TBUF.reg = data;
|
2018-04-25 06:49:24 -07:00
|
|
|
/* set timeout time to wait for transmit completion. */
|
|
|
|
timeout = TimerGet() + UART_BYTE_TX_TIMEOUT_MS;
|
2016-02-25 02:46:45 -08:00
|
|
|
/* wait for transmit buffer register to be empty */
|
2016-03-01 06:24:23 -08:00
|
|
|
while (UARTx->TBSRC.bits.SRR == 0)
|
2016-02-25 02:46:45 -08:00
|
|
|
{
|
2018-04-25 06:49:24 -07:00
|
|
|
/* keep the watchdog happy */
|
2016-02-25 02:46:45 -08:00
|
|
|
CopService();
|
2018-04-25 06:49:24 -07:00
|
|
|
/* break loop upon timeout. this would indicate a hardware failure. */
|
|
|
|
if (TimerGet() > timeout)
|
|
|
|
{
|
|
|
|
result = BLT_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-01 06:24:23 -08:00
|
|
|
}
|
2018-04-25 06:49:24 -07:00
|
|
|
/* give the result back to the caller */
|
|
|
|
return result;
|
2016-02-25 02:46:45 -08:00
|
|
|
} /*** end of UartTransmitByte ***/
|
|
|
|
#endif /* BOOT_COM_UART_ENABLE > 0 */
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************** end of uart.c *************************************/
|