NEW: sync the code of DJI Payload-SDK version 2.2.1 released on January 20 2021.
Signed-off-by: DJI-Martin <DJI-Martin@dji.com>
This commit is contained in:
179
sample/platform/rtos_freertos/stm32f4_eval/bootloader/common.c
Normal file
179
sample/platform/rtos_freertos/stm32f4_eval/bootloader/common.c
Normal file
@ -0,0 +1,179 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file IAP/IAP_Main/Src/common.c
|
||||
* @author MCD Application Team
|
||||
* @brief This file provides all the common functions.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted, provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistribution of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of other
|
||||
* contributors to this software may be used to endorse or promote products
|
||||
* derived from this software without specific written permission.
|
||||
* 4. This software, including modifications and/or derivative works of this
|
||||
* software, must execute solely and exclusively on microcontroller or
|
||||
* microprocessor devices manufactured by or for STMicroelectronics.
|
||||
* 5. Redistribution and use of this software other than as permitted under
|
||||
* this license is void and will automatically terminate your rights under
|
||||
* this license.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
|
||||
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
|
||||
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @statement DJI has modified some symbols' name.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F4xx_IAP_Main
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "common.h"
|
||||
#include "main.h"
|
||||
#include "uart.h"
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Convert an Integer to a string
|
||||
* @param p_str: The string output pointer
|
||||
* @param intnum: The integer to be converted
|
||||
* @retval None
|
||||
*/
|
||||
void Int2Str(uint8_t *p_str, uint32_t intnum)
|
||||
{
|
||||
uint32_t i, divider = 1000000000, pos = 0, status = 0;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
p_str[pos++] = (intnum / divider) + 48;
|
||||
|
||||
intnum = intnum % divider;
|
||||
divider /= 10;
|
||||
if ((p_str[pos - 1] == '0') & (status == 0)) {
|
||||
pos = 0;
|
||||
} else {
|
||||
status++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert a string to an integer
|
||||
* @param p_inputstr: The string to be converted
|
||||
* @param p_intnum: The integer value
|
||||
* @retval 1: Correct
|
||||
* 0: Error
|
||||
*/
|
||||
uint32_t Str2Int(uint8_t *p_inputstr, uint32_t *p_intnum)
|
||||
{
|
||||
uint32_t i = 0, res = 0;
|
||||
uint32_t val = 0;
|
||||
|
||||
if ((p_inputstr[0] == '0') && ((p_inputstr[1] == 'x') || (p_inputstr[1] == 'X'))) {
|
||||
i = 2;
|
||||
while ((i < 11) && (p_inputstr[i] != '\0')) {
|
||||
if (ISVALIDHEX(p_inputstr[i])) {
|
||||
val = (val << 4) + CONVERTHEX(p_inputstr[i]);
|
||||
} else {
|
||||
/* Return 0, Invalid input */
|
||||
res = 0;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
/* valid result */
|
||||
if (p_inputstr[i] == '\0') {
|
||||
*p_intnum = val;
|
||||
res = 1;
|
||||
}
|
||||
} else /* max 10-digit decimal input */
|
||||
{
|
||||
while ((i < 11) && (res != 1)) {
|
||||
if (p_inputstr[i] == '\0') {
|
||||
*p_intnum = val;
|
||||
/* return 1 */
|
||||
res = 1;
|
||||
} else if (((p_inputstr[i] == 'k') || (p_inputstr[i] == 'K')) && (i > 0)) {
|
||||
val = val << 10;
|
||||
*p_intnum = val;
|
||||
res = 1;
|
||||
} else if (((p_inputstr[i] == 'm') || (p_inputstr[i] == 'M')) && (i > 0)) {
|
||||
val = val << 20;
|
||||
*p_intnum = val;
|
||||
res = 1;
|
||||
} else if (ISVALIDDEC(p_inputstr[i]))
|
||||
{
|
||||
val = val * 10 + CONVERTDEC(p_inputstr[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* return 0, Invalid input */
|
||||
res = 0;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a string on the HyperTerminal
|
||||
* @param p_string: The string to be printed
|
||||
* @retval None
|
||||
*/
|
||||
void Serial_PutString(uint8_t *p_string)
|
||||
{
|
||||
uint16_t length = 0;
|
||||
|
||||
while (p_string[length] != '\0')
|
||||
{
|
||||
length++;
|
||||
}
|
||||
UART_Write(PSDK_CONSOLE_UART_NUM, p_string, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Transmit a byte to the HyperTerminal
|
||||
* @param param The byte to be sent
|
||||
* @retval HAL_StatusTypeDef HAL_OK if OK
|
||||
*/
|
||||
HAL_StatusTypeDef Serial_PutByte( uint8_t param )
|
||||
{
|
||||
UART_Write(PSDK_CONSOLE_UART_NUM, ¶m, 1);
|
||||
return HAL_OK;
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
@ -0,0 +1,91 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file IAP/IAP_Main/Inc/common.h
|
||||
* @author MCD Application Team
|
||||
* @brief This file provides all the headers of the common functions.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted, provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistribution of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of other
|
||||
* contributors to this software may be used to endorse or promote products
|
||||
* derived from this software without specific written permission.
|
||||
* 4. This software, including modifications and/or derivative works of this
|
||||
* software, must execute solely and exclusively on microcontroller or
|
||||
* microprocessor devices manufactured by or for STMicroelectronics.
|
||||
* 5. Redistribution and use of this software other than as permitted under
|
||||
* this license is void and will automatically terminate your rights under
|
||||
* this license.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
|
||||
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
|
||||
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @statement DJI has modified some symbols' name.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __COMMON_H
|
||||
#define __COMMON_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f4xx.h"
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
#define FILE_NAME_LENGTH ((uint32_t)64)
|
||||
/* Constants used by Serial Command Line Mode */
|
||||
#define TX_TIMEOUT ((uint32_t)100)
|
||||
#define RX_TIMEOUT ((uint32_t)0xFFFFFFFF)
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
#define IS_CAP_LETTER(c) (((c) >= 'A') && ((c) <= 'F'))
|
||||
#define IS_LC_LETTER(c) (((c) >= 'a') && ((c) <= 'f'))
|
||||
#define IS_09(c) (((c) >= '0') && ((c) <= '9'))
|
||||
#define ISVALIDHEX(c) (IS_CAP_LETTER(c) || IS_LC_LETTER(c) || IS_09(c))
|
||||
#define ISVALIDDEC(c) IS_09(c)
|
||||
#define CONVERTDEC(c) (c - '0')
|
||||
|
||||
#define CONVERTHEX_ALPHA(c) (IS_CAP_LETTER(c) ? ((c) - 'A'+10) : ((c) - 'a'+10))
|
||||
#define CONVERTHEX(c) (IS_09(c) ? ((c) - '0') : CONVERTHEX_ALPHA(c))
|
||||
#define CRC16 ((uint8_t)0x43) /* 'C' == 0x43, request 16-bit CRC */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
COM_OK = 0x00,
|
||||
COM_ERROR = 0x01,
|
||||
COM_ABORT = 0x02,
|
||||
COM_TIMEOUT = 0x03,
|
||||
COM_DATA = 0x04,
|
||||
COM_LIMIT = 0x05
|
||||
} COM_StatusTypeDef;
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void Int2Str(uint8_t *p_str, uint32_t intnum);
|
||||
uint32_t Str2Int(uint8_t *inputstr, uint32_t *intnum);
|
||||
void Serial_PutString(uint8_t *p_string);
|
||||
HAL_StatusTypeDef Serial_PutByte(uint8_t param);
|
||||
|
||||
#endif /* __COMMON_H */
|
||||
|
||||
/*******************(C)COPYRIGHT 2016 STMicroelectronics *****END OF FILE******/
|
244
sample/platform/rtos_freertos/stm32f4_eval/bootloader/main.c
Normal file
244
sample/platform/rtos_freertos/stm32f4_eval/bootloader/main.c
Normal file
@ -0,0 +1,244 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file IAP/IAP_Main/Src/main.c
|
||||
* @author MCD Application Team
|
||||
* @brief Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted, provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistribution of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of other
|
||||
* contributors to this software may be used to endorse or promote products
|
||||
* derived from this software without specific written permission.
|
||||
* 4. This software, including modifications and/or derivative works of this
|
||||
* software, must execute solely and exclusively on microcontroller or
|
||||
* microprocessor devices manufactured by or for STMicroelectronics.
|
||||
* 5. Redistribution and use of this software other than as permitted under
|
||||
* this license is void and will automatically terminate your rights under
|
||||
* this license.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
|
||||
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
|
||||
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @statement DJI has modified some symbols' name.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include <button.h>
|
||||
#include <uart.h>
|
||||
#include "main.h"
|
||||
#include "menu.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include <upgrade_platform_opt_stm32.h>
|
||||
#include "osal/osal.h"
|
||||
|
||||
/** @addtogroup STM32F4xx_IAP_Main
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Exported variables --------------------------------------------------------*/
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
#define USER_START_TASK_STACK_SIZE 512
|
||||
#define USER_START_TASK_PRIORITY 0
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
extern pFunction JumpToApplication;
|
||||
extern uint32_t JumpAddress;
|
||||
static TaskHandle_t startTask;
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
static void IAP_Init(void);
|
||||
static void SystemClock_Config(void);
|
||||
static void PsdkUser_StartTask(void const *argument);
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Main program.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* Reset of all peripherals, Initializes the Systick. */
|
||||
HAL_Init();
|
||||
|
||||
/* Configure the system clock to have a system clock = 168 Mhz */
|
||||
SystemClock_Config();
|
||||
|
||||
/* Create start task */
|
||||
xTaskCreate((TaskFunction_t) PsdkUser_StartTask, "start_task", 1024,
|
||||
NULL, USER_START_TASK_PRIORITY, startTask);
|
||||
|
||||
/* Start scheduler */
|
||||
vTaskStartScheduler();
|
||||
|
||||
/*Taken by the scheduler */
|
||||
for (;;);
|
||||
}
|
||||
|
||||
static void PsdkUser_StartTask(void const *argument)
|
||||
{
|
||||
bool isUpgradeReboot;
|
||||
T_PsdkUpgradeEndInfo upgradeEndInfo;
|
||||
T_PsdkReturnCode returnCode;
|
||||
|
||||
/* attention : Delay for power on button state check mistake */
|
||||
Osal_TaskSleepMs(50);
|
||||
|
||||
/* Initialize Key Button mounted on STM324xG-EVAL board */
|
||||
Button_Init(BUTTON_KEY1, BUTTON_MODE_GPIO);
|
||||
|
||||
/* Test if Key push-button1 is pressed */
|
||||
if (Button_GetState(BUTTON_KEY1) == GPIO_PIN_RESET) {
|
||||
/* Execute the IAP driver in order to reprogram the Flash */
|
||||
IAP_Init();
|
||||
/* Display main menu */
|
||||
Main_Menu();
|
||||
} else {
|
||||
returnCode = PsdkUpgradePlatformStm32_GetUpgradeRebootState(&isUpgradeReboot, &upgradeEndInfo);
|
||||
if (returnCode == PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS && isUpgradeReboot == true &&
|
||||
upgradeEndInfo.upgradeEndState == PSDK_UPGRADE_END_STATE_SUCCESS) {
|
||||
//replace old program
|
||||
returnCode = PsdkUpgradePlatformStm32_ReplaceOldProgram();
|
||||
if (returnCode != PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {
|
||||
upgradeEndInfo.upgradeEndState = PSDK_UPGRADE_END_STATE_UNKNOWN_ERROR;
|
||||
PsdkUpgradePlatformStm32_SetUpgradeRebootState(&upgradeEndInfo);
|
||||
}
|
||||
}
|
||||
/* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
|
||||
if (((*(__IO uint32_t *) APPLICATION_ADDRESS) & 0x2FFE0000) == 0x20000000) {
|
||||
__disable_irq();
|
||||
__disable_fiq();
|
||||
/* Jump to user application */
|
||||
JumpAddress = *(__IO uint32_t *) (APPLICATION_ADDRESS + 4);
|
||||
JumpToApplication = (pFunction) JumpAddress;
|
||||
/* Initialize user application's Stack Pointer */
|
||||
__set_MSP(*(__IO uint32_t *) APPLICATION_ADDRESS);
|
||||
JumpToApplication();
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize the IAP: Configure USART.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void IAP_Init(void)
|
||||
{
|
||||
UART_Init(PSDK_CONSOLE_UART_NUM, PSDK_CONSOLE_UART_BAUD);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* The system Clock is configured as follow :
|
||||
* System Clock source = PLL (HSE)
|
||||
* SYSCLK(Hz) = 168000000
|
||||
* HCLK(Hz) = 168000000
|
||||
* AHB Prescaler = 1
|
||||
* APB1 Prescaler = 4
|
||||
* APB2 Prescaler = 2
|
||||
* HSE Frequency(Hz) = 25000000
|
||||
* PLL_M = 25
|
||||
* PLL_N = 336
|
||||
* PLL_P = 2
|
||||
* PLL_Q = 7
|
||||
* VDD(V) = 3.3
|
||||
* Main regulator output voltage = Scale1 mode
|
||||
* Flash Latency(WS) = 5
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void SystemClock_Config(void)
|
||||
{
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct;
|
||||
|
||||
/* Enable Power Control clock */
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
|
||||
/* The voltage scaling allows optimizing the power consumption when the device is
|
||||
clocked below the maximum system frequency, to update the voltage scaling value
|
||||
regarding system frequency refer to product datasheet. */
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
||||
|
||||
/* Enable HSE Oscillator and activate PLL with HSE as source */
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
||||
RCC_OscInitStruct.PLL.PLLM = HSE_VALUE / 1000000;
|
||||
RCC_OscInitStruct.PLL.PLLN = 336;
|
||||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||||
RCC_OscInitStruct.PLL.PLLQ = 7;
|
||||
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
||||
|
||||
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
|
||||
clocks dividers */
|
||||
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 |
|
||||
RCC_CLOCKTYPE_PCLK2);
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
|
||||
|
||||
/* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
|
||||
if (HAL_GetREVID() == 0x1001) {
|
||||
/* Enable the Flash prefetch */
|
||||
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
void assert_failed(uint8_t* file, uint32_t line)
|
||||
{
|
||||
/* User can add his own implementation to report the file name and line number,
|
||||
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
||||
|
||||
/* Infinite loop */
|
||||
while (1)
|
||||
{}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
63
sample/platform/rtos_freertos/stm32f4_eval/bootloader/main.h
Normal file
63
sample/platform/rtos_freertos/stm32f4_eval/bootloader/main.h
Normal file
@ -0,0 +1,63 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file IAP/IAP_Main/Inc/main.h
|
||||
* @author MCD Application Team
|
||||
* @brief Header for main.c module
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted, provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistribution of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of other
|
||||
* contributors to this software may be used to endorse or promote products
|
||||
* derived from this software without specific written permission.
|
||||
* 4. This software, including modifications and/or derivative works of this
|
||||
* software, must execute solely and exclusively on microcontroller or
|
||||
* microprocessor devices manufactured by or for STMicroelectronics.
|
||||
* 5. Redistribution and use of this software other than as permitted under
|
||||
* this license is void and will automatically terminate your rights under
|
||||
* this license.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
|
||||
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
|
||||
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @statement DJI has modified some symbols' name.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
/* Exported variables --------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
#define PSDK_CONSOLE_UART_NUM UART_NUM_1
|
||||
#define PSDK_CONSOLE_UART_BAUD 921600
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
#endif /* __MAIN_H */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
232
sample/platform/rtos_freertos/stm32f4_eval/bootloader/menu.c
Normal file
232
sample/platform/rtos_freertos/stm32f4_eval/bootloader/menu.c
Normal file
@ -0,0 +1,232 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file IAP/IAP_Main/Src/menu.c
|
||||
* @author MCD Application Team
|
||||
|
||||
* @brief This file provides the software which contains the main menu routine.
|
||||
* The main menu gives the options of:
|
||||
* - downloading a new binary file,
|
||||
* - uploading internal flash memory,
|
||||
* - executing the binary file already loaded
|
||||
* - configuring the write protection of the Flash sectors where the
|
||||
* user loads his binary file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted, provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistribution of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of other
|
||||
* contributors to this software may be used to endorse or promote products
|
||||
* derived from this software without specific written permission.
|
||||
* 4. This software, including modifications and/or derivative works of this
|
||||
* software, must execute solely and exclusively on microcontroller or
|
||||
* microprocessor devices manufactured by or for STMicroelectronics.
|
||||
* 5. Redistribution and use of this software other than as permitted under
|
||||
* this license is void and will automatically terminate your rights under
|
||||
* this license.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
|
||||
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
|
||||
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @statement DJI has modified some symbols' name.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F4xx_IAP_Main
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "common.h"
|
||||
#include "flash_if.h"
|
||||
#include "menu.h"
|
||||
#include "uart.h"
|
||||
#include <upgrade_platform_opt_stm32.h>
|
||||
#include <osal/osal.h>
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
pFunction JumpToApplication;
|
||||
uint32_t JumpAddress;
|
||||
uint32_t FlashProtection = 0;
|
||||
uint8_t aFileName[FILE_NAME_LENGTH];
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SerialDownload(void);
|
||||
void SerialUpload(void);
|
||||
HAL_StatusTypeDef Uart_ReadWithTimeOut(E_UartNum uartNum, uint8_t *data, uint16_t len, uint32_t timeOut);
|
||||
HAL_StatusTypeDef Uart_WriteWithTimeOut(E_UartNum uartNum, uint8_t *data, uint16_t len, uint32_t timeOut);
|
||||
extern COM_StatusTypeDef Ymodem_Receive(uint32_t *p_size);
|
||||
extern COM_StatusTypeDef Ymodem_Transmit(uint8_t *p_buf, const uint8_t *p_file_name, uint32_t file_size);
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Download a file via serial port
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SerialDownload(void)
|
||||
{
|
||||
uint8_t number[11] = {0};
|
||||
uint32_t size = 0;
|
||||
COM_StatusTypeDef result;
|
||||
|
||||
Serial_PutString((uint8_t *) "Waiting for the file to be sent .f.. (press 'a' to abort)\n\r");
|
||||
result = Ymodem_Receive(&size);
|
||||
if (result == COM_OK) {
|
||||
Serial_PutString(
|
||||
(uint8_t *) "\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
|
||||
Serial_PutString(aFileName);
|
||||
Int2Str(number, size);
|
||||
Serial_PutString((uint8_t *) "\n\r Size: ");
|
||||
Serial_PutString(number);
|
||||
Serial_PutString((uint8_t *) " Bytes\r\n");
|
||||
Serial_PutString((uint8_t *) "-------------------\n");
|
||||
} else if (result == COM_LIMIT) {
|
||||
Serial_PutString((uint8_t *) "\n\n\rThe image size is higher than the allowed space memory!\n\r");
|
||||
} else if (result == COM_DATA) {
|
||||
Serial_PutString((uint8_t *) "\n\n\rVerification failed!\n\r");
|
||||
} else if (result == COM_ABORT) {
|
||||
Serial_PutString((uint8_t *) "\r\n\nAborted by user.\n\r");
|
||||
} else {
|
||||
Serial_PutString((uint8_t *) "\n\rFailed to receive the file!\n\r");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Upload a file via serial port.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SerialUpload(void)
|
||||
{
|
||||
uint8_t status = 0;
|
||||
|
||||
Serial_PutString((uint8_t *) "\n\n\rSelect Receive File\n\r");
|
||||
|
||||
Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &status, 1, RX_TIMEOUT);
|
||||
if (status == CRC16) {
|
||||
/* Transmit the flash image through ymodem protocol */
|
||||
status = Ymodem_Transmit((uint8_t *) APPLICATION_ADDRESS, (const uint8_t *) "UploadedFlashImage.bin",
|
||||
APPLICATION_FLASH_SIZE);
|
||||
|
||||
if (status != 0) {
|
||||
Serial_PutString((uint8_t *) "\n\rError Occurred while Transmitting File\n\r");
|
||||
} else {
|
||||
Serial_PutString((uint8_t *) "\n\rFile uploaded successfully \n\r");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Display the Main Menu on HyperTerminal
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void Main_Menu(void)
|
||||
{
|
||||
uint8_t key = 0;
|
||||
|
||||
Serial_PutString((uint8_t *) "\r\n======================================================================");
|
||||
Serial_PutString((uint8_t *) "\r\n= (C) COPYRIGHT 2016 STMicroelectronics =");
|
||||
Serial_PutString((uint8_t *) "\r\n= =");
|
||||
Serial_PutString((uint8_t *) "\r\n= STM32F4xx In-Application Programming Application =");
|
||||
Serial_PutString((uint8_t *) "\r\n= =");
|
||||
Serial_PutString((uint8_t *) "\r\n= By MCD Application Team =");
|
||||
Serial_PutString((uint8_t *) "\r\n======================================================================");
|
||||
Serial_PutString((uint8_t *) "\r\n\r\n");
|
||||
|
||||
while (1) {
|
||||
|
||||
/* Test if any sector of Flash memory where user application will be loaded is write protected */
|
||||
FlashProtection = FLASH_If_GetWriteProtectionStatus();
|
||||
|
||||
Serial_PutString((uint8_t *) "\r\n=================== Main Menu ============================\r\n\n");
|
||||
Serial_PutString((uint8_t *) " Download image to the internal Flash ----------------- 1\r\n\n");
|
||||
Serial_PutString((uint8_t *) " Upload image from the internal Flash ----------------- 2\r\n\n");
|
||||
Serial_PutString((uint8_t *) " Execute the loaded application ----------------------- 3\r\n\n");
|
||||
|
||||
if (FlashProtection != FLASHIF_PROTECTION_NONE) {
|
||||
Serial_PutString((uint8_t *) " Disable the write protection ------------------------- 4\r\n\n");
|
||||
} else {
|
||||
Serial_PutString((uint8_t *) " Enable the write protection -------------------------- 4\r\n\n");
|
||||
}
|
||||
Serial_PutString((uint8_t *) "==========================================================\r\n\n");
|
||||
|
||||
/* Receive key */
|
||||
Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &key, 1, RX_TIMEOUT);
|
||||
|
||||
switch (key) {
|
||||
case '1' :
|
||||
/* Download user application in the Flash */
|
||||
SerialDownload();
|
||||
break;
|
||||
case '2' :
|
||||
/* Upload user application from the Flash */
|
||||
SerialUpload();
|
||||
break;
|
||||
case '3' :
|
||||
Serial_PutString((uint8_t *) "Start program execution......\r\n\n");
|
||||
Osal_TaskSleepMs(50);
|
||||
PsdkUpgradePlatformStm32_RebootSystem();
|
||||
break;
|
||||
case '4' :
|
||||
if (FlashProtection != FLASHIF_PROTECTION_NONE) {
|
||||
/* Disable the write protection */
|
||||
if (FLASH_If_WriteProtectionConfig(OB_WRPSTATE_DISABLE) == HAL_OK) {
|
||||
Serial_PutString((uint8_t *) "Write Protection disabled...\r\n");
|
||||
Serial_PutString((uint8_t *) "System will now restart...\r\n");
|
||||
/* Launch the option byte loading */
|
||||
HAL_FLASH_OB_Launch();
|
||||
/* Ulock the flash */
|
||||
HAL_FLASH_Unlock();
|
||||
} else {
|
||||
Serial_PutString((uint8_t *) "Error: Flash write un-protection failed...\r\n");
|
||||
}
|
||||
} else {
|
||||
if (FLASH_If_WriteProtectionConfig(OB_WRPSTATE_ENABLE) == HAL_OK) {
|
||||
Serial_PutString((uint8_t *) "Write Protection enabled...\r\n");
|
||||
Serial_PutString((uint8_t *) "System will now restart...\r\n");
|
||||
/* Launch the option byte loading */
|
||||
HAL_FLASH_OB_Launch();
|
||||
} else {
|
||||
Serial_PutString((uint8_t *) "Error: Flash write protection failed...\r\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Serial_PutString((uint8_t *) "Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
69
sample/platform/rtos_freertos/stm32f4_eval/bootloader/menu.h
Normal file
69
sample/platform/rtos_freertos/stm32f4_eval/bootloader/menu.h
Normal file
@ -0,0 +1,69 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file IAP/IAP_Main/Inc/menu.h
|
||||
* @author MCD Application Team
|
||||
* @brief This file provides all the headers of the menu functions.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted, provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistribution of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of other
|
||||
* contributors to this software may be used to endorse or promote products
|
||||
* derived from this software without specific written permission.
|
||||
* 4. This software, including modifications and/or derivative works of this
|
||||
* software, must execute solely and exclusively on microcontroller or
|
||||
* microprocessor devices manufactured by or for STMicroelectronics.
|
||||
* 5. Redistribution and use of this software other than as permitted under
|
||||
* this license is void and will automatically terminate your rights under
|
||||
* this license.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
|
||||
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
|
||||
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @statement DJI has modified some symbols' name.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __MENU_H
|
||||
#define __MENU_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "flash_if.h"
|
||||
#include "common.h"
|
||||
|
||||
/* Imported variables --------------------------------------------------------*/
|
||||
extern uint8_t aFileName[FILE_NAME_LENGTH];
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
typedef void (*pFunction)(void);
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void Main_Menu(void);
|
||||
|
||||
#endif /* __MENU_H */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
673
sample/platform/rtos_freertos/stm32f4_eval/bootloader/ymodem.c
Normal file
673
sample/platform/rtos_freertos/stm32f4_eval/bootloader/ymodem.c
Normal file
@ -0,0 +1,673 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file IAP/IAP_Main/Src/ymodem.c
|
||||
* @author MCD Application Team
|
||||
* @brief This file provides all the software functions related to the ymodem
|
||||
* protocol.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted, provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistribution of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of other
|
||||
* contributors to this software may be used to endorse or promote products
|
||||
* derived from this software without specific written permission.
|
||||
* 4. This software, including modifications and/or derivative works of this
|
||||
* software, must execute solely and exclusively on microcontroller or
|
||||
* microprocessor devices manufactured by or for STMicroelectronics.
|
||||
* 5. Redistribution and use of this software other than as permitted under
|
||||
* this license is void and will automatically terminate your rights under
|
||||
* this license.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
|
||||
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
|
||||
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @statement DJI has modified some symbols' name.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/** @addtogroup STM32F4xx_IAP_Main
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "flash_if.h"
|
||||
#include "common.h"
|
||||
#include "string.h"
|
||||
#include "main.h"
|
||||
#include "menu.h"
|
||||
#include "uart.h"
|
||||
#include "osal/osal.h"
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Packet structure defines */
|
||||
#define PACKET_HEADER_SIZE ((uint32_t)3)
|
||||
#define PACKET_DATA_INDEX ((uint32_t)4)
|
||||
#define PACKET_START_INDEX ((uint32_t)1)
|
||||
#define PACKET_NUMBER_INDEX ((uint32_t)2)
|
||||
#define PACKET_CNUMBER_INDEX ((uint32_t)3)
|
||||
#define PACKET_TRAILER_SIZE ((uint32_t)2)
|
||||
#define PACKET_OVERHEAD_SIZE (PACKET_HEADER_SIZE + PACKET_TRAILER_SIZE - 1)
|
||||
#define PACKET_SIZE ((uint32_t)128)
|
||||
#define PACKET_1K_SIZE ((uint32_t)1024)
|
||||
|
||||
/* /-------- Packet in IAP memory ------------------------------------------\
|
||||
* | 0 | 1 | 2 | 3 | 4 | ... | n+4 | n+5 | n+6 |
|
||||
* |------------------------------------------------------------------------|
|
||||
* | unused | start | number | !num | data[0] | ... | data[n] | crc0 | crc1 |
|
||||
* \------------------------------------------------------------------------/
|
||||
* the first byte is left unused for memory alignment reasons */
|
||||
#define FILE_SIZE_LENGTH ((uint32_t)16)
|
||||
|
||||
#define SOH ((uint8_t)0x01) /* start of 128-byte data packet */
|
||||
#define STX ((uint8_t)0x02) /* start of 1024-byte data packet */
|
||||
#define EOT ((uint8_t)0x04) /* end of transmission */
|
||||
#define ACK ((uint8_t)0x06) /* acknowledge */
|
||||
#define NAK ((uint8_t)0x15) /* negative acknowledge */
|
||||
#define CA ((uint32_t)0x18) /* two of these in succession aborts transfer */
|
||||
#define NEGATIVE_BYTE ((uint8_t)0xFF)
|
||||
|
||||
#define ABORT1 ((uint8_t)0x41) /* 'A' == 0x41, abort by user */
|
||||
#define ABORT2 ((uint8_t)0x61) /* 'a' == 0x61, abort by user */
|
||||
|
||||
#define NAK_TIMEOUT ((uint32_t)0x100000)
|
||||
#define DOWNLOAD_TIMEOUT ((uint32_t)5000) /* Five second retry delay */
|
||||
#define MAX_ERRORS ((uint32_t)5)
|
||||
#define CRC16_F /* activate the CRC16 integrity */
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
__IO uint32_t flashdestination;
|
||||
/* @note ATTENTION - please keep this variable 32bit alligned */
|
||||
uint8_t aPacketData[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE];
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
static void PrepareIntialPacket(uint8_t *p_data, const uint8_t *p_file_name, uint32_t length);
|
||||
static void PreparePacket(uint8_t *p_source, uint8_t *p_packet, uint8_t pkt_nr, uint32_t size_blk);
|
||||
static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout);
|
||||
uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte);
|
||||
uint16_t Cal_CRC16(const uint8_t *p_data, uint32_t size);
|
||||
uint8_t CalcChecksum(const uint8_t *p_data, uint32_t size);
|
||||
|
||||
HAL_StatusTypeDef Uart_ReadWithTimeOut(E_UartNum uartNum, uint8_t *data, uint16_t len, uint32_t timeOut);
|
||||
HAL_StatusTypeDef Uart_WriteWithTimeOut(E_UartNum uartNum, uint8_t *data, uint16_t len, uint32_t timeOut);
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Receive a packet from sender
|
||||
* @param data
|
||||
* @param length
|
||||
* 0: end of transmission
|
||||
* 2: abort by sender
|
||||
* >0: packet length
|
||||
* @param timeout
|
||||
* @retval HAL_OK: normally return
|
||||
* HAL_BUSY: abort by user
|
||||
*/
|
||||
static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout)
|
||||
{
|
||||
uint32_t crc;
|
||||
uint32_t packet_size = 0;
|
||||
HAL_StatusTypeDef status;
|
||||
uint8_t char1;
|
||||
|
||||
*p_length = 0;
|
||||
status = Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &char1, 1, timeout);
|
||||
|
||||
if (status == HAL_OK) {
|
||||
switch (char1) {
|
||||
case SOH:
|
||||
packet_size = PACKET_SIZE;
|
||||
break;
|
||||
case STX:
|
||||
packet_size = PACKET_1K_SIZE;
|
||||
break;
|
||||
case EOT:
|
||||
break;
|
||||
case CA:
|
||||
if ((Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &char1, 1, timeout) == HAL_OK) && (char1 == CA)) {
|
||||
packet_size = 2;
|
||||
} else {
|
||||
status = HAL_ERROR;
|
||||
}
|
||||
break;
|
||||
case ABORT1:
|
||||
case ABORT2:
|
||||
status = HAL_BUSY;
|
||||
break;
|
||||
default:
|
||||
status = HAL_ERROR;
|
||||
break;
|
||||
}
|
||||
*p_data = char1;
|
||||
|
||||
if (packet_size >= PACKET_SIZE) {
|
||||
status = Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &p_data[PACKET_NUMBER_INDEX],
|
||||
packet_size + PACKET_OVERHEAD_SIZE,
|
||||
timeout);
|
||||
|
||||
/* Simple packet sanity check */
|
||||
if (status == HAL_OK) {
|
||||
if (p_data[PACKET_NUMBER_INDEX] != ((p_data[PACKET_CNUMBER_INDEX]) ^ NEGATIVE_BYTE)) {
|
||||
packet_size = 0;
|
||||
status = HAL_ERROR;
|
||||
} else {
|
||||
/* Check packet CRC */
|
||||
crc = p_data[packet_size + PACKET_DATA_INDEX] << 8;
|
||||
crc += p_data[packet_size + PACKET_DATA_INDEX + 1];
|
||||
if (Cal_CRC16(&p_data[PACKET_DATA_INDEX], packet_size) != crc) {
|
||||
packet_size = 0;
|
||||
status = HAL_ERROR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
packet_size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
*p_length = packet_size;
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Prepare the first block
|
||||
* @param p_data: output buffer
|
||||
* @param p_file_name: name of the file to be sent
|
||||
* @param length: length of the file to be sent in bytes
|
||||
* @retval None
|
||||
*/
|
||||
static void PrepareIntialPacket(uint8_t *p_data, const uint8_t *p_file_name, uint32_t length)
|
||||
{
|
||||
uint32_t i, j = 0;
|
||||
uint8_t astring[10];
|
||||
|
||||
/* first 3 bytes are constant */
|
||||
p_data[PACKET_START_INDEX] = SOH;
|
||||
p_data[PACKET_NUMBER_INDEX] = 0x00;
|
||||
p_data[PACKET_CNUMBER_INDEX] = 0xff;
|
||||
|
||||
/* Filename written */
|
||||
for (i = 0; (p_file_name[i] != '\0') && (i < FILE_NAME_LENGTH); i++) {
|
||||
p_data[i + PACKET_DATA_INDEX] = p_file_name[i];
|
||||
}
|
||||
|
||||
p_data[i + PACKET_DATA_INDEX] = 0x00;
|
||||
|
||||
/* file size written */
|
||||
Int2Str(astring, length);
|
||||
i = i + PACKET_DATA_INDEX + 1;
|
||||
while (astring[j] != '\0') {
|
||||
p_data[i++] = astring[j++];
|
||||
}
|
||||
|
||||
/* padding with zeros */
|
||||
for (j = i; j < PACKET_SIZE + PACKET_DATA_INDEX; j++) {
|
||||
p_data[j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Prepare the data packet
|
||||
* @param p_source: pointer to the data to be sent
|
||||
* @param p_packet: pointer to the output buffer
|
||||
* @param pkt_nr: number of the packet
|
||||
* @param size_blk: length of the block to be sent in bytes
|
||||
* @retval None
|
||||
*/
|
||||
static void PreparePacket(uint8_t *p_source, uint8_t *p_packet, uint8_t pkt_nr, uint32_t size_blk)
|
||||
{
|
||||
uint8_t *p_record;
|
||||
uint32_t i, size, packet_size;
|
||||
|
||||
/* Make first three packet */
|
||||
packet_size = size_blk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
|
||||
size = size_blk < packet_size ? size_blk : packet_size;
|
||||
if (packet_size == PACKET_1K_SIZE) {
|
||||
p_packet[PACKET_START_INDEX] = STX;
|
||||
} else {
|
||||
p_packet[PACKET_START_INDEX] = SOH;
|
||||
}
|
||||
p_packet[PACKET_NUMBER_INDEX] = pkt_nr;
|
||||
p_packet[PACKET_CNUMBER_INDEX] = (~pkt_nr);
|
||||
p_record = p_source;
|
||||
|
||||
/* Filename packet has valid data */
|
||||
for (i = PACKET_DATA_INDEX; i < size + PACKET_DATA_INDEX; i++) {
|
||||
p_packet[i] = *p_record++;
|
||||
}
|
||||
if (size <= packet_size) {
|
||||
for (i = size + PACKET_DATA_INDEX; i < packet_size + PACKET_DATA_INDEX; i++) {
|
||||
p_packet[i] = 0x1A; /* EOF (0x1A) or 0x00 */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update CRC16 for input byte
|
||||
* @param crc_in input value
|
||||
* @param input byte
|
||||
* @retval None
|
||||
*/
|
||||
uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte)
|
||||
{
|
||||
uint32_t crc = crc_in;
|
||||
uint32_t in = byte | 0x100;
|
||||
|
||||
do {
|
||||
crc <<= 1;
|
||||
in <<= 1;
|
||||
if (in & 0x100)
|
||||
++crc;
|
||||
if (crc & 0x10000)
|
||||
crc ^= 0x1021;
|
||||
} while (!(in & 0x10000));
|
||||
|
||||
return crc & 0xffffu;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Cal CRC16 for YModem Packet
|
||||
* @param data
|
||||
* @param length
|
||||
* @retval None
|
||||
*/
|
||||
uint16_t Cal_CRC16(const uint8_t *p_data, uint32_t size)
|
||||
{
|
||||
uint32_t crc = 0;
|
||||
const uint8_t *dataEnd = p_data + size;
|
||||
|
||||
while (p_data < dataEnd)
|
||||
crc = UpdateCRC16(crc, *p_data++);
|
||||
|
||||
crc = UpdateCRC16(crc, 0);
|
||||
crc = UpdateCRC16(crc, 0);
|
||||
|
||||
return crc & 0xffffu;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculate Check sum for YModem Packet
|
||||
* @param p_data Pointer to input data
|
||||
* @param size length of input data
|
||||
* @retval uint8_t checksum value
|
||||
*/
|
||||
uint8_t CalcChecksum(const uint8_t *p_data, uint32_t size)
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
const uint8_t *p_data_end = p_data + size;
|
||||
|
||||
while (p_data < p_data_end) {
|
||||
sum += *p_data++;
|
||||
}
|
||||
|
||||
return (sum & 0xffu);
|
||||
}
|
||||
|
||||
/* Public functions ---------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Receive a file using the ymodem protocol with CRC16.
|
||||
* @param p_size The size of the file.
|
||||
* @retval COM_StatusTypeDef result of reception/programming
|
||||
*/
|
||||
COM_StatusTypeDef Ymodem_Receive(uint32_t *p_size)
|
||||
{
|
||||
uint32_t i, packet_length, session_done = 0, file_done, errors = 0, session_begin = 0;
|
||||
// uint32_t flashdestination;
|
||||
uint32_t ramsource, filesize, packets_received;
|
||||
uint8_t *file_ptr;
|
||||
uint8_t file_size[FILE_SIZE_LENGTH], tmp;
|
||||
COM_StatusTypeDef result = COM_OK;
|
||||
|
||||
/* Initialize flashdestination variable */
|
||||
flashdestination = APPLICATION_ADDRESS;
|
||||
|
||||
while ((session_done == 0) && (result == COM_OK)) {
|
||||
packets_received = 0;
|
||||
file_done = 0;
|
||||
while ((file_done == 0) && (result == COM_OK)) {
|
||||
switch (ReceivePacket(aPacketData, &packet_length, DOWNLOAD_TIMEOUT)) {
|
||||
case HAL_OK:
|
||||
errors = 0;
|
||||
switch (packet_length) {
|
||||
case 2:
|
||||
/* Abort by sender */
|
||||
Serial_PutByte(ACK);
|
||||
result = COM_ABORT;
|
||||
break;
|
||||
case 0:
|
||||
/* End of transmission */
|
||||
Serial_PutByte(ACK);
|
||||
file_done = 1;
|
||||
break;
|
||||
default:
|
||||
/* Normal packet */
|
||||
if (aPacketData[PACKET_NUMBER_INDEX] != (uint8_t) packets_received) {
|
||||
Serial_PutByte(NAK);
|
||||
} else {
|
||||
if (packets_received == 0) {
|
||||
/* File name packet */
|
||||
if (aPacketData[PACKET_DATA_INDEX] != 0) {
|
||||
/* File name extraction */
|
||||
i = 0;
|
||||
file_ptr = aPacketData + PACKET_DATA_INDEX;
|
||||
while ((*file_ptr != 0) && (i < FILE_NAME_LENGTH)) {
|
||||
aFileName[i++] = *file_ptr++;
|
||||
}
|
||||
|
||||
/* File size extraction */
|
||||
aFileName[i++] = '\0';
|
||||
i = 0;
|
||||
file_ptr++;
|
||||
while ((*file_ptr != ' ') && (i < FILE_SIZE_LENGTH)) {
|
||||
file_size[i++] = *file_ptr++;
|
||||
}
|
||||
file_size[i++] = '\0';
|
||||
Str2Int(file_size, &filesize);
|
||||
|
||||
/* Test the size of the image to be sent */
|
||||
/* Image size is greater than Flash size */
|
||||
if (*p_size > (APPLICATION_FLASH_SIZE + 1)) {
|
||||
/* End session */
|
||||
tmp = CA;
|
||||
Uart_WriteWithTimeOut(PSDK_CONSOLE_UART_NUM, &tmp, 1, NAK_TIMEOUT);
|
||||
Uart_WriteWithTimeOut(PSDK_CONSOLE_UART_NUM, &tmp, 1, NAK_TIMEOUT);
|
||||
result = COM_LIMIT;
|
||||
}
|
||||
/* erase user application area */
|
||||
FLASH_If_Erase(APPLICATION_ADDRESS, APPLICATION_ADDRESS_END);
|
||||
*p_size = filesize;
|
||||
|
||||
Serial_PutByte(ACK);
|
||||
Serial_PutByte(CRC16);
|
||||
}
|
||||
/* File header packet is empty, end session */
|
||||
else {
|
||||
Serial_PutByte(ACK);
|
||||
file_done = 1;
|
||||
session_done = 1;
|
||||
break;
|
||||
}
|
||||
} else /* Data packet */
|
||||
{
|
||||
ramsource = (uint32_t) &aPacketData[PACKET_DATA_INDEX];
|
||||
/* Write received data in Flash */
|
||||
if (FLASH_If_Write(flashdestination, (uint8_t *) ramsource, packet_length) ==
|
||||
FLASHIF_OK) {
|
||||
flashdestination += packet_length;
|
||||
Serial_PutByte(ACK);
|
||||
} else /* An error occurred while writing to Flash memory */
|
||||
{
|
||||
/* End session */
|
||||
Serial_PutByte(CA);
|
||||
Serial_PutByte(CA);
|
||||
result = COM_DATA;
|
||||
}
|
||||
}
|
||||
packets_received++;
|
||||
session_begin = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HAL_BUSY: /* Abort actually */
|
||||
Serial_PutByte(CA);
|
||||
Serial_PutByte(CA);
|
||||
result = COM_ABORT;
|
||||
break;
|
||||
default:
|
||||
if (session_begin > 0) {
|
||||
errors++;
|
||||
}
|
||||
if (errors > MAX_ERRORS) {
|
||||
/* Abort communication */
|
||||
Serial_PutByte(CA);
|
||||
Serial_PutByte(CA);
|
||||
} else {
|
||||
Serial_PutByte(CRC16); /* Ask for a packet */
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Transmit a file using the ymodem protocol
|
||||
* @param p_buf: Address of the first byte
|
||||
* @param p_file_name: Name of the file sent
|
||||
* @param file_size: Size of the transmission
|
||||
* @retval COM_StatusTypeDef result of the communication
|
||||
*/
|
||||
COM_StatusTypeDef Ymodem_Transmit(uint8_t *p_buf, const uint8_t *p_file_name, uint32_t file_size)
|
||||
{
|
||||
uint32_t errors = 0, ack_recpt = 0, size = 0, pkt_size;
|
||||
uint8_t *p_buf_int;
|
||||
COM_StatusTypeDef result = COM_OK;
|
||||
uint32_t blk_number = 1;
|
||||
uint8_t a_rx_ctrl[2];
|
||||
uint8_t i;
|
||||
#ifdef CRC16_F
|
||||
uint32_t temp_crc;
|
||||
#else /* CRC16_F */
|
||||
uint8_t temp_chksum;
|
||||
#endif /* CRC16_F */
|
||||
|
||||
/* Prepare first block - header */
|
||||
PrepareIntialPacket(aPacketData, p_file_name, file_size);
|
||||
|
||||
while ((!ack_recpt) && (result == COM_OK)) {
|
||||
/* Send Packet */
|
||||
Uart_WriteWithTimeOut(PSDK_CONSOLE_UART_NUM, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE,
|
||||
NAK_TIMEOUT);
|
||||
|
||||
/* Send CRC or Check Sum based on CRC16_F */
|
||||
#ifdef CRC16_F
|
||||
temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
|
||||
Serial_PutByte(temp_crc >> 8);
|
||||
Serial_PutByte(temp_crc & 0xFF);
|
||||
#else /* CRC16_F */
|
||||
temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
|
||||
Serial_PutByte(temp_chksum);
|
||||
#endif /* CRC16_F */
|
||||
|
||||
/* Wait for Ack and 'C' */
|
||||
if (Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) {
|
||||
if (a_rx_ctrl[0] == ACK) {
|
||||
ack_recpt = 1;
|
||||
} else if (a_rx_ctrl[0] == CA) {
|
||||
if ((Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) &&
|
||||
(a_rx_ctrl[0] == CA)) {
|
||||
HAL_Delay(2);
|
||||
result = COM_ABORT;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
errors++;
|
||||
}
|
||||
if (errors >= MAX_ERRORS) {
|
||||
result = COM_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
p_buf_int = p_buf;
|
||||
size = file_size;
|
||||
|
||||
/* Here 1024 bytes length is used to send the packets */
|
||||
while ((size) && (result == COM_OK)) {
|
||||
/* Prepare next packet */
|
||||
PreparePacket(p_buf_int, aPacketData, blk_number, size);
|
||||
ack_recpt = 0;
|
||||
a_rx_ctrl[0] = 0;
|
||||
errors = 0;
|
||||
|
||||
/* Resend packet if NAK for few times else end of communication */
|
||||
while ((!ack_recpt) && (result == COM_OK)) {
|
||||
/* Send next packet */
|
||||
if (size >= PACKET_1K_SIZE) {
|
||||
pkt_size = PACKET_1K_SIZE;
|
||||
} else {
|
||||
pkt_size = PACKET_SIZE;
|
||||
}
|
||||
|
||||
Uart_WriteWithTimeOut(PSDK_CONSOLE_UART_NUM, &aPacketData[PACKET_START_INDEX],
|
||||
pkt_size + PACKET_HEADER_SIZE,
|
||||
NAK_TIMEOUT);
|
||||
|
||||
/* Send CRC or Check Sum based on CRC16_F */
|
||||
#ifdef CRC16_F
|
||||
temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], pkt_size);
|
||||
Serial_PutByte(temp_crc >> 8);
|
||||
Serial_PutByte(temp_crc & 0xFF);
|
||||
#else /* CRC16_F */
|
||||
temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], pkt_size);
|
||||
Serial_PutByte(temp_chksum);
|
||||
#endif /* CRC16_F */
|
||||
|
||||
/* Wait for Ack */
|
||||
if ((Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) &&
|
||||
(a_rx_ctrl[0] == ACK)) {
|
||||
ack_recpt = 1;
|
||||
if (size > pkt_size) {
|
||||
p_buf_int += pkt_size;
|
||||
size -= pkt_size;
|
||||
if (blk_number == (APPLICATION_FLASH_SIZE / PACKET_1K_SIZE)) {
|
||||
result = COM_LIMIT; /* boundary error */
|
||||
} else {
|
||||
blk_number++;
|
||||
}
|
||||
} else {
|
||||
p_buf_int += pkt_size;
|
||||
size = 0;
|
||||
}
|
||||
} else {
|
||||
errors++;
|
||||
}
|
||||
|
||||
/* Resend packet if NAK for a count of 10 else end of communication */
|
||||
if (errors >= MAX_ERRORS) {
|
||||
result = COM_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Sending End Of Transmission char */
|
||||
ack_recpt = 0;
|
||||
a_rx_ctrl[0] = 0x00;
|
||||
errors = 0;
|
||||
while ((!ack_recpt) && (result == COM_OK)) {
|
||||
Serial_PutByte(EOT);
|
||||
|
||||
/* Wait for Ack */
|
||||
if (Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) {
|
||||
if (a_rx_ctrl[0] == ACK) {
|
||||
ack_recpt = 1;
|
||||
} else if (a_rx_ctrl[0] == CA) {
|
||||
if ((Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) &&
|
||||
(a_rx_ctrl[0] == CA)) {
|
||||
HAL_Delay(2);
|
||||
result = COM_ABORT;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
errors++;
|
||||
}
|
||||
|
||||
if (errors >= MAX_ERRORS) {
|
||||
result = COM_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* Empty packet sent - some terminal emulators need this to close session */
|
||||
if (result == COM_OK) {
|
||||
/* Preparing an empty packet */
|
||||
aPacketData[PACKET_START_INDEX] = SOH;
|
||||
aPacketData[PACKET_NUMBER_INDEX] = 0;
|
||||
aPacketData[PACKET_CNUMBER_INDEX] = 0xFF;
|
||||
for (i = PACKET_DATA_INDEX; i < (PACKET_SIZE + PACKET_DATA_INDEX); i++) {
|
||||
aPacketData[i] = 0x00;
|
||||
}
|
||||
|
||||
/* Send Packet */
|
||||
Uart_WriteWithTimeOut(PSDK_CONSOLE_UART_NUM, &aPacketData[PACKET_START_INDEX], PACKET_SIZE + PACKET_HEADER_SIZE,
|
||||
NAK_TIMEOUT);
|
||||
|
||||
/* Send CRC or Check Sum based on CRC16_F */
|
||||
#ifdef CRC16_F
|
||||
temp_crc = Cal_CRC16(&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
|
||||
Serial_PutByte(temp_crc >> 8);
|
||||
Serial_PutByte(temp_crc & 0xFF);
|
||||
#else /* CRC16_F */
|
||||
temp_chksum = CalcChecksum (&aPacketData[PACKET_DATA_INDEX], PACKET_SIZE);
|
||||
Serial_PutByte(temp_chksum);
|
||||
#endif /* CRC16_F */
|
||||
|
||||
/* Wait for Ack and 'C' */
|
||||
if (Uart_ReadWithTimeOut(PSDK_CONSOLE_UART_NUM, &a_rx_ctrl[0], 1, NAK_TIMEOUT) == HAL_OK) {
|
||||
if (a_rx_ctrl[0] == CA) {
|
||||
HAL_Delay(2);
|
||||
result = COM_ABORT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result; /* file transmitted successfully */
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef Uart_ReadWithTimeOut(E_UartNum uartNum, uint8_t *data, uint16_t len, uint32_t timeOut)
|
||||
{
|
||||
int res;
|
||||
uint16_t alreadyReadLen = 0;
|
||||
uint32_t loop_count = 0;
|
||||
|
||||
while (loop_count <= timeOut) {
|
||||
res = UART_Read(uartNum, data + alreadyReadLen, len - alreadyReadLen);
|
||||
if (res > 0) {
|
||||
alreadyReadLen += res;
|
||||
}
|
||||
if (alreadyReadLen == len) {
|
||||
return HAL_OK;
|
||||
}
|
||||
Osal_TaskSleepMs(1);
|
||||
loop_count++;
|
||||
}
|
||||
|
||||
return HAL_TIMEOUT;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef Uart_WriteWithTimeOut(E_UartNum uartNum, uint8_t *data, uint16_t len, uint32_t timeOut)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = UART_Write(uartNum, data, len);
|
||||
if (res == len) {
|
||||
return HAL_OK;
|
||||
} else {
|
||||
return HAL_ERROR;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*******************(C)COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
|
Reference in New Issue
Block a user