2018-01-02 12:05:56 -08:00
|
|
|
/************************************************************************************//**
|
|
|
|
* \file Demo\ARMCM3_STM32F1_Nucleo_F103RB_IAR\Prog\boot.c
|
|
|
|
* \brief Demo program bootloader interface source file.
|
|
|
|
* \ingroup Prog_ARMCM3_STM32F1_Nucleo_F103RB_IAR
|
|
|
|
* \internal
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* C O P Y R I G H T
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) 2018 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 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.
|
|
|
|
*
|
|
|
|
* \endinternal
|
|
|
|
****************************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Include files
|
|
|
|
****************************************************************************************/
|
|
|
|
#include "header.h" /* generic header */
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Function prototypes
|
|
|
|
****************************************************************************************/
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
|
|
|
static void BootComUartInit(void);
|
|
|
|
static void BootComUartCheckActivationRequest(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Initializes the communication interface.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void BootComInit(void)
|
|
|
|
{
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
|
|
|
BootComUartInit();
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
|
|
|
BootComUartCheckActivationRequest();
|
|
|
|
#endif
|
|
|
|
} /*** end of BootComCheckActivationRequest ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Bootloader activation function.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void BootActivate(void)
|
|
|
|
{
|
|
|
|
/* perform software reset to activate the bootoader again */
|
|
|
|
NVIC_SystemReset();
|
|
|
|
} /*** end of BootActivate ***/
|
|
|
|
|
|
|
|
|
|
|
|
#if (BOOT_COM_UART_ENABLE > 0)
|
|
|
|
/****************************************************************************************
|
|
|
|
* 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
|
|
|
|
****************************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* 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)
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Function prototypes
|
|
|
|
****************************************************************************************/
|
|
|
|
static unsigned char UartReceiveByte(unsigned char *data);
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Initializes the UART communication interface.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
static void BootComUartInit(void)
|
|
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
USART_InitTypeDef USART_InitStruct;
|
|
|
|
|
|
|
|
/* enable UART peripheral clock */
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
|
|
/* enable GPIO peripheral clock for transmitter and receiver pins */
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
|
|
|
|
/* configure USART Tx as alternate function push-pull */
|
|
|
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
|
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
|
|
|
|
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* Configure USART Rx as alternate function input floating */
|
|
|
|
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* configure UART communcation parameters */
|
|
|
|
USART_InitStruct.USART_BaudRate = BOOT_COM_UART_BAUDRATE;
|
|
|
|
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
|
|
|
|
USART_InitStruct.USART_StopBits = USART_StopBits_1;
|
|
|
|
USART_InitStruct.USART_Parity = USART_Parity_No;
|
|
|
|
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
|
|
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
|
|
USART_Init(USART2, &USART_InitStruct);
|
|
|
|
/* enable UART */
|
|
|
|
USART_Cmd(USART2, ENABLE);
|
|
|
|
} /*** end of BootComUartInit ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Receives the CONNECT request from the host, which indicates that the
|
|
|
|
** bootloader should be activated and, if so, activates it.
|
|
|
|
** \return none.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
static void BootComUartCheckActivationRequest(void)
|
|
|
|
{
|
|
|
|
static unsigned char xcpCtoReqPacket[BOOT_COM_UART_RX_MAX_DATA+1];
|
|
|
|
static unsigned char xcpCtoRxLength;
|
|
|
|
static unsigned char xcpCtoRxInProgress = 0;
|
|
|
|
static unsigned long xcpCtoRxStartTime = 0;
|
|
|
|
|
|
|
|
/* start of cto packet received? */
|
|
|
|
if (xcpCtoRxInProgress == 0)
|
|
|
|
{
|
|
|
|
/* store the message length when received */
|
|
|
|
if (UartReceiveByte(&xcpCtoReqPacket[0]) == 1)
|
|
|
|
{
|
|
|
|
/* 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) &&
|
|
|
|
(xcpCtoReqPacket[0] <= BOOT_COM_UART_RX_MAX_DATA) )
|
2018-01-02 12:05:56 -08:00
|
|
|
{
|
|
|
|
/* store the start time */
|
|
|
|
xcpCtoRxStartTime = TimerGet();
|
|
|
|
/* indicate that a cto packet is being received */
|
|
|
|
xcpCtoRxInProgress = 1;
|
|
|
|
/* reset packet data count */
|
|
|
|
xcpCtoRxLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* store the next packet byte */
|
|
|
|
if (UartReceiveByte(&xcpCtoReqPacket[xcpCtoRxLength+1]) == 1)
|
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
if ((xcpCtoReqPacket[1] == 0xff) && (xcpCtoReqPacket[2] == 0x00))
|
|
|
|
{
|
|
|
|
/* connection request received so start the bootloader */
|
|
|
|
BootActivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check packet reception timeout */
|
|
|
|
if (TimerGet() > (xcpCtoRxStartTime + UART_CTO_RX_PACKET_TIMEOUT_MS))
|
|
|
|
{
|
|
|
|
/* cancel cto packet reception due to timeout. note that this automatically
|
|
|
|
* discards the already received packet bytes, allowing the host to retry.
|
|
|
|
*/
|
|
|
|
xcpCtoRxInProgress = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /*** end of BootComUartCheckActivationRequest ***/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************//**
|
|
|
|
** \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.
|
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
static unsigned char UartReceiveByte(unsigned char *data)
|
|
|
|
{
|
|
|
|
/* check flag to see if a byte was received */
|
|
|
|
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
|
|
|
|
{
|
|
|
|
/* retrieve and store the newly received byte */
|
|
|
|
*data = (unsigned char)USART_ReceiveData(USART2);
|
|
|
|
/* all done */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* still here to no new byte received */
|
|
|
|
return 0;
|
|
|
|
} /*** end of UartReceiveByte ***/
|
|
|
|
#endif /* BOOT_COM_UART_ENABLE > 0 */
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************** end of boot.c *************************************/
|