2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
2018-07-17 06:06:06 -07:00
|
|
|
* \file Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_GCC/Prog/boot.c
|
2013-07-31 08:48:23 -07:00
|
|
|
* \brief Demo program bootloader interface source file.
|
|
|
|
* \ingroup Prog_ARMCM3_EFM32_Olimex_EM32G880F128STK_GCC
|
|
|
|
* \internal
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* C O P Y R I G H T
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) 2012 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-04-30 15:52:15 -07:00
|
|
|
* You have received a copy of the GNU General Public License along with OpenBLT. It
|
|
|
|
* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
|
2013-07-31 08:48:23 -07:00
|
|
|
*
|
|
|
|
* \endinternal
|
2012-03-04 12:51:40 -08:00
|
|
|
****************************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Include files
|
|
|
|
****************************************************************************************/
|
|
|
|
#include "header.h" /* generic header */
|
|
|
|
|
|
|
|
|
2014-07-22 07:53:07 -07:00
|
|
|
/****************************************************************************************
|
|
|
|
* Function prototypes
|
|
|
|
****************************************************************************************/
|
2020-02-06 08:22:58 -08:00
|
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
|
|
|
static void BootComRs232Init(void);
|
|
|
|
static void BootComRs232CheckActivationRequest(void);
|
2014-07-22 07:53:07 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Initializes the communication interface.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void BootComInit(void)
|
|
|
|
{
|
2020-02-06 08:22:58 -08:00
|
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
|
|
|
BootComRs232Init();
|
2014-07-22 07:53:07 -07:00
|
|
|
#endif
|
|
|
|
} /*** end of BootComInit ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Receives the CONNECT request from the host, which indicates that the
|
|
|
|
** bootloader should be activated and, if so, activates it.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void BootComCheckActivationRequest(void)
|
|
|
|
{
|
2020-02-06 08:22:58 -08:00
|
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
|
|
|
BootComRs232CheckActivationRequest();
|
2014-07-22 07:53:07 -07:00
|
|
|
#endif
|
|
|
|
} /*** end of BootComCheckActivationRequest ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Bootloader activation function.
|
|
|
|
** \return none.
|
2012-03-04 12:51:40 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
2014-07-22 07:53:07 -07:00
|
|
|
void BootActivate(void)
|
2012-03-04 12:51:40 -08:00
|
|
|
{
|
2014-02-06 06:37:04 -08:00
|
|
|
/* perform software reset to activate the bootoader again */
|
|
|
|
NVIC_SystemReset();
|
2012-03-04 12:51:40 -08:00
|
|
|
} /*** end of BootActivate ***/
|
|
|
|
|
|
|
|
|
2020-02-06 08:22:58 -08:00
|
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
2012-03-04 12:51:40 -08:00
|
|
|
/****************************************************************************************
|
|
|
|
* U N I V E R S A L A S Y N C H R O N O U S R X T X I N T E R F A C E
|
|
|
|
****************************************************************************************/
|
|
|
|
|
2017-05-18 07:29:50 -07:00
|
|
|
/****************************************************************************************
|
|
|
|
* Macro definitions
|
|
|
|
****************************************************************************************/
|
|
|
|
/** \brief Timeout time for the reception of a CTO packet. The timer is started upon
|
|
|
|
* reception of the first packet byte.
|
|
|
|
*/
|
2020-02-06 08:22:58 -08:00
|
|
|
#define RS232_CTO_RX_PACKET_TIMEOUT_MS (100u)
|
2017-05-18 07:29:50 -07:00
|
|
|
|
|
|
|
|
2012-03-04 12:51:40 -08:00
|
|
|
/****************************************************************************************
|
|
|
|
* Function prototypes
|
|
|
|
****************************************************************************************/
|
2020-02-06 08:22:58 -08:00
|
|
|
static unsigned char Rs232ReceiveByte(unsigned char *data);
|
2012-03-04 12:51:40 -08:00
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Initializes the UART communication interface.
|
|
|
|
** \return none.
|
2012-03-04 12:51:40 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
2020-02-06 08:22:58 -08:00
|
|
|
static void BootComRs232Init(void)
|
2012-03-04 12:51:40 -08:00
|
|
|
{
|
|
|
|
LEUART_Init_TypeDef init = LEUART_INIT_DEFAULT;
|
|
|
|
|
|
|
|
/* configure GPIO pins */
|
|
|
|
CMU_ClockEnable(cmuClock_GPIO, true);
|
|
|
|
/* to avoid false start, configure output as high */
|
|
|
|
GPIO_PinModeSet(gpioPortC, 6, gpioModePushPull, 1);
|
|
|
|
GPIO_PinModeSet(gpioPortC, 7, gpioModeInput, 0);
|
|
|
|
/* enable CORE LE clock in order to access LE modules */
|
|
|
|
CMU_ClockEnable(cmuClock_CORELE, true);
|
|
|
|
/* select LFXO for LEUARTs (and wait for it to stabilize) */
|
|
|
|
CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
|
|
|
|
/* do not prescale clock */
|
|
|
|
CMU_ClockDivSet(cmuClock_LEUART1, cmuClkDiv_1);
|
|
|
|
/* enable LEUART1 clock */
|
|
|
|
CMU_ClockEnable(cmuClock_LEUART1, true);
|
|
|
|
/* configure LEUART */
|
|
|
|
init.enable = leuartDisable;
|
|
|
|
LEUART_Init(LEUART1, &init);
|
2020-02-06 08:22:58 -08:00
|
|
|
LEUART_BaudrateSet(LEUART1, 0, BOOT_COM_RS232_BAUDRATE);
|
2012-03-04 12:51:40 -08:00
|
|
|
/* enable pins at default location */
|
|
|
|
LEUART1->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN;
|
|
|
|
/* clear previous RX interrupts */
|
|
|
|
LEUART_IntClear(LEUART1, LEUART_IF_RXDATAV);
|
|
|
|
/* finally enable it */
|
|
|
|
LEUART_Enable(LEUART1, leuartEnable);
|
2020-02-06 08:22:58 -08:00
|
|
|
} /*** end of BootComRs232Init ***/
|
2012-03-04 12:51:40 -08:00
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Receives the CONNECT request from the host, which indicates that the
|
|
|
|
** bootloader should be activated and, if so, activates it.
|
|
|
|
** \return none.
|
2012-03-04 12:51:40 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
2020-02-06 08:22:58 -08:00
|
|
|
static void BootComRs232CheckActivationRequest(void)
|
2012-03-04 12:51:40 -08:00
|
|
|
{
|
2020-02-06 08:22:58 -08:00
|
|
|
static unsigned char xcpCtoReqPacket[BOOT_COM_RS232_RX_MAX_DATA+1];
|
2012-03-04 12:51:40 -08:00
|
|
|
static unsigned char xcpCtoRxLength;
|
|
|
|
static unsigned char xcpCtoRxInProgress = 0;
|
2017-05-18 07:29:50 -07:00
|
|
|
static unsigned long xcpCtoRxStartTime = 0;
|
2012-03-04 12:51:40 -08:00
|
|
|
|
|
|
|
/* start of cto packet received? */
|
|
|
|
if (xcpCtoRxInProgress == 0)
|
|
|
|
{
|
|
|
|
/* store the message length when received */
|
2020-02-06 08:22:58 -08:00
|
|
|
if (Rs232ReceiveByte(&xcpCtoReqPacket[0]) == 1)
|
2012-03-04 12:51:40 -08:00
|
|
|
{
|
2017-05-18 07:29:50 -07:00
|
|
|
/* check that the length has a valid value. it should not be 0 */
|
2018-02-09 02:12:04 -08:00
|
|
|
if ( (xcpCtoReqPacket[0] > 0) &&
|
2020-02-06 08:22:58 -08:00
|
|
|
(xcpCtoReqPacket[0] <= BOOT_COM_RS232_RX_MAX_DATA) )
|
2017-05-18 07:29:50 -07:00
|
|
|
{
|
|
|
|
/* store the start time */
|
|
|
|
xcpCtoRxStartTime = TimerGet();
|
|
|
|
/* indicate that a cto packet is being received */
|
|
|
|
xcpCtoRxInProgress = 1;
|
|
|
|
/* reset packet data count */
|
|
|
|
xcpCtoRxLength = 0;
|
|
|
|
}
|
2012-03-04 12:51:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* store the next packet byte */
|
2020-02-06 08:22:58 -08:00
|
|
|
if (Rs232ReceiveByte(&xcpCtoReqPacket[xcpCtoRxLength+1]) == 1)
|
2012-03-04 12:51:40 -08:00
|
|
|
{
|
|
|
|
/* increment the packet data count */
|
|
|
|
xcpCtoRxLength++;
|
|
|
|
|
|
|
|
/* check to see if the entire packet was received */
|
|
|
|
if (xcpCtoRxLength == xcpCtoReqPacket[0])
|
|
|
|
{
|
|
|
|
/* done with cto packet reception */
|
|
|
|
xcpCtoRxInProgress = 0;
|
|
|
|
|
|
|
|
/* check if this was an XCP CONNECT command */
|
2020-02-05 03:51:11 -08:00
|
|
|
if ((xcpCtoReqPacket[1] == 0xff) && (xcpCtoRxLength == 2))
|
2012-03-04 12:51:40 -08:00
|
|
|
{
|
|
|
|
/* connection request received so start the bootloader */
|
|
|
|
BootActivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 07:29:50 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check packet reception timeout */
|
2020-02-06 08:22:58 -08:00
|
|
|
if (TimerGet() > (xcpCtoRxStartTime + RS232_CTO_RX_PACKET_TIMEOUT_MS))
|
2017-05-18 07:29:50 -07:00
|
|
|
{
|
|
|
|
/* cancel cto packet reception due to timeout. note that this automatically
|
|
|
|
* discards the already received packet bytes, allowing the host to retry.
|
|
|
|
*/
|
|
|
|
xcpCtoRxInProgress = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-03-04 12:51:40 -08:00
|
|
|
}
|
2020-02-06 08:22:58 -08:00
|
|
|
} /*** end of BootComRs232CheckActivationRequest ***/
|
2012-03-04 12:51:40 -08:00
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Receives a communication interface byte if one is present.
|
|
|
|
** \param data Pointer to byte where the data is to be stored.
|
|
|
|
** \return 1 if a byte was received, 0 otherwise.
|
2012-03-04 12:51:40 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
2020-02-06 08:22:58 -08:00
|
|
|
static unsigned char Rs232ReceiveByte(unsigned char *data)
|
2012-03-04 12:51:40 -08:00
|
|
|
{
|
|
|
|
/* check to see if a new bytes was received */
|
|
|
|
if ((LEUART1->IF & LEUART_IF_RXDATAV) != 0)
|
|
|
|
{
|
|
|
|
/* store the received data byte and set return value to positive */
|
|
|
|
*data = LEUART_Rx(LEUART1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* still here to no new byte received */
|
|
|
|
return 0;
|
2020-02-06 08:22:58 -08:00
|
|
|
} /*** end of Rs232ReceiveByte ***/
|
|
|
|
#endif /* BOOT_COM_RS232_ENABLE > 0 */
|
2012-03-04 12:51:40 -08:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************** end of boot.c *************************************/
|