mirror of
https://github.com/No-Chicken/Power-Pico.git
synced 2026-04-03 13:02:36 +08:00
更新BootLoader为USB传输,替代V1版本中的usart
This commit is contained in:
@@ -25,11 +25,20 @@
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "common.h"
|
||||
#include "usart.h"
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "usbd_core.h" // ??????????????
|
||||
#include "usb_device.h" // ?????? hUsbDeviceFS
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
// ?? usbd_cdc_if.c ????
|
||||
extern uint8_t USB_RxBuffer[];
|
||||
extern volatile uint16_t USB_RxHead;
|
||||
extern volatile uint16_t USB_RxTail;
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
@@ -151,7 +160,7 @@ uint32_t Str2Int(uint8_t *inputstr, int32_t *intnum)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get an integer from the HyperTerminal
|
||||
* @brief Get an integer from the USB VCP
|
||||
* @param num: The integer
|
||||
* @retval 1: Correct
|
||||
* 0: Error
|
||||
@@ -166,13 +175,13 @@ uint32_t GetIntegerInput(int32_t * num)
|
||||
if (inputstr[0] == '\0') continue;
|
||||
if ((inputstr[0] == 'a' || inputstr[0] == 'A') && inputstr[1] == '\0')
|
||||
{
|
||||
SerialPutString("User Cancelled \r\n");
|
||||
USB_PutString("User Cancelled \r\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Str2Int(inputstr, num) == 0)
|
||||
{
|
||||
SerialPutString("Error, Input again: \r\n");
|
||||
USB_PutString("Error, Input again: \r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -182,17 +191,19 @@ uint32_t GetIntegerInput(int32_t * num)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test to see if a key has been pressed on the HyperTerminal
|
||||
* @brief Test to see if a key has been pressed on the USB VCP
|
||||
* @param key: The key pressed
|
||||
* @retval 1: Correct
|
||||
* 0: Error
|
||||
* @retval 1: A key was received
|
||||
* 0: No key was received
|
||||
*/
|
||||
uint32_t SerialKeyPressed(uint8_t *key)
|
||||
uint32_t USB_KeyPressed(uint8_t *key)
|
||||
{
|
||||
if (__HAL_UART_GET_FLAG(&huart6, UART_FLAG_RXNE) != RESET)
|
||||
// Check if there is data in the USB receive ring buffer
|
||||
if (USB_RxHead != USB_RxTail)
|
||||
{
|
||||
*key = (uint16_t)((&huart6)->Instance->DR & (uint16_t)0x01FF);
|
||||
return 1;
|
||||
*key = USB_RxBuffer[USB_RxTail];
|
||||
USB_RxTail = (USB_RxTail + 1) % 2048; // APP_RX_DATA_SIZE
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -200,9 +211,8 @@ uint32_t SerialKeyPressed(uint8_t *key)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get a key from the HyperTerminal
|
||||
* @brief Get a key from the USB VCP
|
||||
* @param None
|
||||
* @retval The Key Pressed
|
||||
*/
|
||||
@@ -213,39 +223,66 @@ uint8_t GetKey(void)
|
||||
/* Waiting for user input */
|
||||
while (1)
|
||||
{
|
||||
if (SerialKeyPressed((uint8_t*)&key)) break;
|
||||
if (USB_KeyPressed((uint8_t*)&key)) break;
|
||||
}
|
||||
return key;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Print a character on the HyperTerminal
|
||||
* @param c: The character to be printed
|
||||
* @retval None
|
||||
*/
|
||||
void SerialPutChar(uint8_t c)
|
||||
uint8_t USB_GetChar(uint8_t *c, uint32_t timeout)
|
||||
{
|
||||
USART_SendData(USART6, c);
|
||||
while (__HAL_UART_GET_FLAG(&huart6, UART_FLAG_TXE) == RESET)
|
||||
{
|
||||
}
|
||||
uint32_t tickstart = HAL_GetTick();
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (USB_RxHead != USB_RxTail)
|
||||
{
|
||||
*c = USB_RxBuffer[USB_RxTail];
|
||||
USB_RxTail = (USB_RxTail + 1) % RING_BUFFER_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((HAL_GetTick() - tickstart) > timeout)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Print a string on the HyperTerminal
|
||||
* @param s: The string to be printed
|
||||
* @brief Send a char to the USB VCP
|
||||
* @param c: The char to be sent
|
||||
* @retval None
|
||||
*/
|
||||
void Serial_PutString(uint8_t *s)
|
||||
void USB_PutChar(uint8_t c)
|
||||
{
|
||||
while (*s != '\0')
|
||||
{
|
||||
SerialPutChar(*s);
|
||||
s++;
|
||||
}
|
||||
static uint8_t temp_buf;
|
||||
|
||||
if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED) return;
|
||||
|
||||
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
|
||||
while (hcdc->TxState != 0) {}
|
||||
|
||||
temp_buf = c;
|
||||
CDC_Transmit_FS(&temp_buf, 1);
|
||||
}
|
||||
|
||||
void USB_PutString(char *s)
|
||||
{
|
||||
if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t len = strlen(s);
|
||||
while (CDC_Transmit_FS((uint8_t*)s, len) == USBD_BUSY)
|
||||
{
|
||||
if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,40 +303,27 @@ void GetInputString (uint8_t * buffP)
|
||||
{
|
||||
if (bytes_read > 0)
|
||||
{
|
||||
SerialPutString("\b \b");
|
||||
USB_PutString("\b \b");
|
||||
bytes_read --;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (bytes_read >= CMD_STRING_SIZE )
|
||||
{
|
||||
SerialPutString("Command string size overflow\r\n");
|
||||
USB_PutString("Command string size overflow\r\n");
|
||||
bytes_read = 0;
|
||||
continue;
|
||||
}
|
||||
if (c >= 0x20 && c <= 0x7E)
|
||||
{
|
||||
buffP[bytes_read++] = c;
|
||||
SerialPutChar(c);
|
||||
USB_PutChar(c);
|
||||
}
|
||||
}
|
||||
while (1);
|
||||
SerialPutString(("\n\r"));
|
||||
USB_PutString(("\n\r"));
|
||||
buffP[bytes_read] = '\0';
|
||||
}
|
||||
|
||||
//Ìí¼Ó´úÂë
|
||||
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
|
||||
{
|
||||
/* Check the parameters */
|
||||
assert_param(IS_USART_ALL_PERIPH(USARTx));
|
||||
assert_param(IS_USART_DATA(Data));
|
||||
|
||||
/* Transmit Data */
|
||||
USARTx->DR = (Data & (uint16_t)0x01FF);
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*******************(C)COPYRIGHT 2011 STMicroelectronics *****END OF FILE******/
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file STM32F4xx_IAP/inc/common.h
|
||||
* @file STM32F4xx_IAP/inc/common.h
|
||||
* @author MCD Application Team
|
||||
* @version V1.0.0
|
||||
* @date 10-October-2011
|
||||
@@ -17,7 +17,7 @@
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
|
||||
******************************************************************************
|
||||
*/
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __COMMON_H
|
||||
@@ -44,20 +44,21 @@
|
||||
#define CONVERTHEX_alpha(c) (IS_AF(c) ? (c - 'A'+10) : (c - 'a'+10))
|
||||
#define CONVERTHEX(c) (IS_09(c) ? (c - '0') : CONVERTHEX_alpha(c))
|
||||
|
||||
#define SerialPutString(x) Serial_PutString((uint8_t*)(x))
|
||||
#define USBPutString(x) USB_PutString((uint8_t*)(x))
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void Int2Str(uint8_t* str,int32_t intnum);
|
||||
uint32_t Str2Int(uint8_t *inputstr,int32_t *intnum);
|
||||
uint32_t GetIntegerInput(int32_t * num);
|
||||
uint32_t SerialKeyPressed(uint8_t *key);
|
||||
uint32_t USB_KeyPressed(uint8_t *key);
|
||||
uint8_t GetKey(void);
|
||||
void SerialPutChar(uint8_t c);
|
||||
void Serial_PutString(uint8_t *s);
|
||||
void USB_PutChar(uint8_t c);
|
||||
void USB_PutString(char *s);
|
||||
void GetInputString(uint8_t * buffP);
|
||||
|
||||
//Ìí¼ÓÉùÃ÷
|
||||
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
|
||||
//????
|
||||
|
||||
|
||||
#endif /* __COMMON_H */
|
||||
|
||||
/*******************(C)COPYRIGHT 2011 STMicroelectronics *****END OF FILE******/
|
||||
|
||||
@@ -51,6 +51,18 @@
|
||||
Note: the 1st sector 0x08000000-0x08003FFF is reserved for the IAP code */
|
||||
#define APPLICATION_ADDRESS (uint32_t)0x0800C000
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
|
||||
#define UPGRADE_INFO_ADDRESS (USER_FLASH_END_ADDRESS - sizeof(UpgradeInfo_t) + 1)
|
||||
// 定义升级信息结构体
|
||||
typedef struct {
|
||||
uint32_t magic_word; // 魔法字,如 0xDEADBEEF,用于标识此结构体有效
|
||||
uint32_t upgrade_status; // 升级状态标志
|
||||
uint32_t new_app_size; // 新APP的固件大小
|
||||
uint32_t new_app_crc32; // 新APP的CRC32校验和
|
||||
} UpgradeInfo_t;
|
||||
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void FLASH_If_Init(void);
|
||||
|
||||
@@ -34,9 +34,17 @@
|
||||
#include "flash_if.h"
|
||||
#include "menu.h"
|
||||
#include "ymodem.h"
|
||||
#include "string.h"
|
||||
#include "usb_device.h"
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
|
||||
// 定义升级状态
|
||||
#define UPGRADE_STATUS_NONE 0xFFFFFFFF
|
||||
#define UPGRADE_STATUS_START 0xABCD1234
|
||||
#define UPGRADE_STATUS_COMPLETE 0x5A5A5A5A
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
pFunction Jump_To_Application;
|
||||
@@ -49,99 +57,127 @@ uint8_t tab_1024[1024] =
|
||||
uint8_t FileName[FILE_NAME_LENGTH];
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SerialDownload(void);
|
||||
void SerialUpload(void);
|
||||
void USB_Download(void);
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Writes the UpgradeInfo structure to the dedicated Flash sector.
|
||||
* @param info: pointer to the UpgradeInfo_t structure
|
||||
* @retval None
|
||||
*/
|
||||
static void Write_Upgrade_Info(UpgradeInfo_t* info)
|
||||
{
|
||||
// 假设校验区在最后一个扇区 (Sector 7 for 512KB device)
|
||||
// 注意:擦除会擦除整个扇区,如果扇区很大,需要先备份再写入
|
||||
// 为简化,我们直接擦除并写入
|
||||
FLASH_If_Erase_One_Sector(7);
|
||||
|
||||
uint32_t flash_address = UPGRADE_INFO_ADDRESS;
|
||||
FLASH_If_Write((__IO uint32_t*)&flash_address, (uint32_t*)info, sizeof(UpgradeInfo_t) / 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads the UpgradeInfo structure from the dedicated Flash sector.
|
||||
* @param info: pointer to the UpgradeInfo_t structure to be filled
|
||||
* @retval None
|
||||
*/
|
||||
static void Read_Upgrade_Info(UpgradeInfo_t* info)
|
||||
{
|
||||
memcpy(info, (void*)UPGRADE_INFO_ADDRESS, sizeof(UpgradeInfo_t));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the CRC32 of a given memory area.
|
||||
* @param start_address: Start address of the data
|
||||
* @param size: Size of the data in bytes
|
||||
* @retval Calculated CRC32 value
|
||||
*/
|
||||
static uint32_t Calculate_CRC32(uint32_t start_address, uint32_t size)
|
||||
{
|
||||
// 启用CRC外设时钟
|
||||
__HAL_RCC_CRC_CLK_ENABLE();
|
||||
CRC_HandleTypeDef CrcHandle;
|
||||
CrcHandle.Instance = CRC;
|
||||
HAL_CRC_Init(&CrcHandle);
|
||||
|
||||
return HAL_CRC_Calculate(&CrcHandle, (uint32_t*)start_address, size / 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Download a file via serial port
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SerialDownload(void)
|
||||
void USB_Download(void)
|
||||
{
|
||||
UpgradeInfo_t received_info;
|
||||
uint8_t Number[10] = " ";
|
||||
int32_t Size = 0;
|
||||
int32_t file_size = 0;
|
||||
|
||||
// user operation
|
||||
// clear the flag in flash in 0x08008000
|
||||
uint32_t flashdestination = ADDR_FLASH_SECTOR_2;
|
||||
// sector 2
|
||||
FLASH_If_Erase_One_Sector(2U);
|
||||
|
||||
// 1. 接收元数据
|
||||
USB_PutString("Waiting for the file to be sent ... (press 'a' to abort)\n\r");
|
||||
// 2. 擦除整个APP区域
|
||||
USB_PutString("Erasing Application Area...\r\n");
|
||||
if (FLASH_If_Erase(APPLICATION_ADDRESS) != 0)
|
||||
{
|
||||
USB_PutString("Error: Failed to erase application area!\r\n");
|
||||
return;
|
||||
}
|
||||
USB_PutString("Erase Complete.\r\n");
|
||||
/* Waiting for file sent */
|
||||
SerialPutString("Waiting for the file to be sent ... (press 'a' to abort)\n\r");
|
||||
Size = Ymodem_Receive(&tab_1024[0]);
|
||||
if (Size > 0)
|
||||
USB_PutString("Starting Ymodem Receive\n\r");
|
||||
file_size = Ymodem_Receive(&tab_1024[0]);
|
||||
if (file_size > 0)
|
||||
{
|
||||
SerialPutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
|
||||
SerialPutString(FileName);
|
||||
Int2Str(Number, Size);
|
||||
SerialPutString("\n\r Size: ");
|
||||
SerialPutString(Number);
|
||||
SerialPutString(" Bytes\r\n");
|
||||
SerialPutString("-------------------\n");
|
||||
|
||||
// user operation
|
||||
// set the flag in flash in 0x08008000
|
||||
const char *str_flag = "APP FLAG";
|
||||
uint32_t * APP_FLAG = (uint32_t *)str_flag;
|
||||
USB_PutString("\n\n\r File reception complete. Verifying...\n\r");
|
||||
// 4. 最终校验
|
||||
// 理想情况下,这里应该有一个从上位机获取的CRC32值进行对比
|
||||
// 作为演示,我们只计算并保存
|
||||
uint32_t calculated_crc = Calculate_CRC32(APPLICATION_ADDRESS, file_size);
|
||||
|
||||
/* Write received data in Flash */
|
||||
if (FLASH_If_Write(&flashdestination, (uint32_t*) APP_FLAG, 2) == 0)
|
||||
// 5. 写入校验信息到Flash末尾
|
||||
received_info.magic_word = 0xDEADBEEF;
|
||||
received_info.upgrade_status = UPGRADE_STATUS_COMPLETE; // 标记为升级完成
|
||||
received_info.new_app_size = file_size;
|
||||
received_info.new_app_crc32 = calculated_crc;
|
||||
Write_Upgrade_Info(&received_info);
|
||||
USB_PutString("Verification and Flag Set successful!\r\n");
|
||||
USB_PutString("--------------------------------\r\n Name: ");
|
||||
USB_PutString(FileName);
|
||||
Int2Str(Number, file_size);
|
||||
USB_PutString("\n\r Size: ");
|
||||
USB_PutString(Number);
|
||||
USB_PutString(" Bytes\r\n");
|
||||
USB_PutString(" CRC32: 0x");
|
||||
// 打印CRC值 (需要实现一个Int-to-Hex函数)
|
||||
USB_PutString("\r\n-------------------\n");
|
||||
USB_PutString("Please reboot the device.\r\n");
|
||||
}
|
||||
else /* An error occurred while writing to Flash memory */
|
||||
{
|
||||
// 升级失败,清除标志区,强制下次进入Bootloader
|
||||
FLASH_If_Erase_One_Sector(7); // 假设校验区在最后一个扇区
|
||||
USB_PutString("\n\rUpgrade failed!\r\n");
|
||||
if (file_size == -1)
|
||||
{
|
||||
SerialPutString("\n\n\r APP Flag Set Successfully!\n\r--------------------------------\r\n");
|
||||
USB_PutString("\n\n\rThe image size is higher than the allowed space memory!\n\r");
|
||||
}
|
||||
else /* An error occurred while writing to Flash memory */
|
||||
else if (file_size == -2)
|
||||
{
|
||||
/* End session */
|
||||
SerialPutString("\n\n\r APP Flag Set Error!\n\r--------------------------------\r\n");
|
||||
USB_PutString("\n\n\rVerification failed!\n\r");
|
||||
}
|
||||
|
||||
}
|
||||
else if (Size == -1)
|
||||
{
|
||||
SerialPutString("\n\n\rThe image size is higher than the allowed space memory!\n\r");
|
||||
}
|
||||
else if (Size == -2)
|
||||
{
|
||||
SerialPutString("\n\n\rVerification failed!\n\r");
|
||||
}
|
||||
else if (Size == -3)
|
||||
{
|
||||
SerialPutString("\r\n\nAborted by user.\n\r");
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialPutString("\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 ;
|
||||
|
||||
SerialPutString("\n\n\rSelect Receive File\n\r");
|
||||
|
||||
if (GetKey() == CRC16)
|
||||
{
|
||||
/* Transmit the flash image through ymodem protocol */
|
||||
status = Ymodem_Transmit((uint8_t*)APPLICATION_ADDRESS, (const uint8_t*)"UploadedFlashImage.bin", USER_FLASH_SIZE);
|
||||
|
||||
if (status != 0)
|
||||
else if (file_size == -3)
|
||||
{
|
||||
SerialPutString("\n\rError Occurred while Transmitting File\n\r");
|
||||
USB_PutString("\r\n\nAborted by user.\n\r");
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialPutString("\n\rFile uploaded successfully \n\r");
|
||||
USB_PutString("\n\rFailed to receive the file!\n\r");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,14 +190,14 @@ void Main_Menu(void)
|
||||
{
|
||||
uint8_t key = 0;
|
||||
|
||||
SerialPutString("\r\n======================================================================");
|
||||
SerialPutString("\r\n= (C) COPYRIGHT 2011 STMicroelectronics =");
|
||||
SerialPutString("\r\n= =");
|
||||
SerialPutString("\r\n= STM32F4xx In-Application Programming Application (Version 1.0.0) =");
|
||||
SerialPutString("\r\n= =");
|
||||
SerialPutString("\r\n= By MCD Application Team =");
|
||||
SerialPutString("\r\n======================================================================");
|
||||
SerialPutString("\r\n\r\n");
|
||||
USB_PutString("\r\n======================================================================");
|
||||
USB_PutString("\r\n= (C) COPYRIGHT 2011 STMicroelectronics =");
|
||||
USB_PutString("\r\n= =");
|
||||
USB_PutString("\r\n= STM32F4xx In-Application Programming Application (Version 1.0.0) =");
|
||||
USB_PutString("\r\n= =");
|
||||
USB_PutString("\r\n= By MCD Application Team =");
|
||||
USB_PutString("\r\n======================================================================");
|
||||
USB_PutString("\r\n\r\n");
|
||||
|
||||
/* Test if any sector of Flash memory where user application will be loaded is write protected */
|
||||
if (FLASH_If_GetWriteProtectionStatus() == 0)
|
||||
@@ -175,17 +211,17 @@ void Main_Menu(void)
|
||||
|
||||
while (1)
|
||||
{
|
||||
SerialPutString("\r\n================== Main Menu ============================\r\n\n");
|
||||
SerialPutString(" Download Image To the STM32F4xx Internal Flash ------- 1\r\n\n");
|
||||
SerialPutString(" Upload Image From the STM32F4xx Internal Flash ------- 2\r\n\n");
|
||||
SerialPutString(" Execute The New Program ------------------------------ 3\r\n\n");
|
||||
USB_PutString("\r\n================== Main Menu ============================\r\n\n");
|
||||
USB_PutString(" Download Image To the STM32F4xx Internal Flash ------- 1\r\n\n");
|
||||
USB_PutString(" Upload Image From the STM32F4xx Internal Flash ------- 2\r\n\n");
|
||||
USB_PutString(" Execute The New Program ------------------------------ 3\r\n\n");
|
||||
|
||||
if(FlashProtection != 0)
|
||||
{
|
||||
SerialPutString(" Disable the write protection ------------------------- 4\r\n\n");
|
||||
USB_PutString(" Disable the write protection ------------------------- 4\r\n\n");
|
||||
}
|
||||
|
||||
SerialPutString("==========================================================\r\n\n");
|
||||
USB_PutString("==========================================================\r\n\n");
|
||||
|
||||
/* Receive key */
|
||||
key = GetKey();
|
||||
@@ -193,16 +229,17 @@ void Main_Menu(void)
|
||||
if (key == 0x31)
|
||||
{
|
||||
/* Download user application in the Flash */
|
||||
SerialDownload();
|
||||
USB_Download();
|
||||
}
|
||||
else if (key == 0x32)
|
||||
{
|
||||
/* Upload user application from the Flash */
|
||||
// SerialUpload();
|
||||
SerialPutString("This function is disabled! Please Use 1\r");
|
||||
// USB_Upload();
|
||||
USB_PutString("This function is disabled! Please Use 1\r");
|
||||
}
|
||||
else if (key == 0x33) /* execute the new program */
|
||||
{
|
||||
USBD_DeInit(&hUsbDeviceFS);
|
||||
//user code here
|
||||
SysTick->CTRL = 0X00;//禁止SysTick
|
||||
SysTick->LOAD = 0;
|
||||
@@ -224,13 +261,13 @@ void Main_Menu(void)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
SerialPutString("Write Protection disabled...\r\n");
|
||||
USB_PutString("Write Protection disabled...\r\n");
|
||||
FlashProtection = 0;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
SerialPutString("Error: Flash write unprotection failed...\r\n");
|
||||
USB_PutString("Error: Flash write unprotection failed...\r\n");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -242,11 +279,11 @@ void Main_Menu(void)
|
||||
{
|
||||
if (FlashProtection == 0)
|
||||
{
|
||||
SerialPutString("Invalid Number ! ==> The number should be either 1, 2 or 3\r");
|
||||
USB_PutString("Invalid Number ! ==> The number should be either 1, 2 or 3\r");
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialPutString("Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r");
|
||||
USB_PutString("Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file STM32F4xx_IAP/src/ymodem.c
|
||||
* @file STM32F4xx_IAP/src/ymodem.c
|
||||
* @author MCD Application Team
|
||||
* @version V1.0.0
|
||||
* @date 10-October-2011
|
||||
* @brief This file provides all the software functions related to the ymodem
|
||||
* @brief This file provides all the software functions related to the ymodem
|
||||
* protocol.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
@@ -20,10 +20,7 @@
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32F4xx_IAP
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "flash_if.h"
|
||||
#include "common.h"
|
||||
@@ -46,11 +43,11 @@ extern uint8_t FileName[];
|
||||
* @retval 0: Byte received
|
||||
* -1: Timeout
|
||||
*/
|
||||
static int32_t Receive_Byte (uint8_t *c, uint32_t timeout)
|
||||
static int32_t Receive_Byte (uint8_t *c, uint32_t timeout)
|
||||
{
|
||||
while (timeout-- > 0)
|
||||
{
|
||||
if (SerialKeyPressed(c) == 1)
|
||||
if (USB_KeyPressed(c) == 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -65,7 +62,7 @@ static int32_t Receive_Byte (uint8_t *c, uint32_t timeout)
|
||||
*/
|
||||
static uint32_t Send_Byte (uint8_t c)
|
||||
{
|
||||
SerialPutChar(c);
|
||||
USB_PutChar(c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -289,12 +286,12 @@ void Ymodem_PrepareIntialPacket(uint8_t *data, const uint8_t* fileName, uint32_t
|
||||
{
|
||||
uint16_t i, j;
|
||||
uint8_t file_ptr[10];
|
||||
|
||||
|
||||
/* Make first three packet */
|
||||
data[0] = SOH;
|
||||
data[1] = 0x00;
|
||||
data[2] = 0xff;
|
||||
|
||||
|
||||
/* Filename packet has valid data */
|
||||
for (i = 0; (fileName[i] != '\0') && (i < FILE_NAME_LENGTH);i++)
|
||||
{
|
||||
@@ -302,13 +299,13 @@ void Ymodem_PrepareIntialPacket(uint8_t *data, const uint8_t* fileName, uint32_t
|
||||
}
|
||||
|
||||
data[i + PACKET_HEADER] = 0x00;
|
||||
|
||||
|
||||
Int2Str (file_ptr, *length);
|
||||
for (j =0, i = i + PACKET_HEADER + 1; file_ptr[j] != '\0' ; )
|
||||
{
|
||||
data[i++] = file_ptr[j++];
|
||||
}
|
||||
|
||||
|
||||
for (j = i; j < PACKET_SIZE + PACKET_HEADER; j++)
|
||||
{
|
||||
data[j] = 0;
|
||||
@@ -325,7 +322,7 @@ void Ymodem_PreparePacket(uint8_t *SourceBuf, uint8_t *data, uint8_t pktNo, uint
|
||||
{
|
||||
uint16_t i, size, packetSize;
|
||||
uint8_t* file_ptr;
|
||||
|
||||
|
||||
/* Make first three packet */
|
||||
packetSize = sizeBlk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
|
||||
size = sizeBlk < packetSize ? sizeBlk :packetSize;
|
||||
@@ -340,7 +337,7 @@ void Ymodem_PreparePacket(uint8_t *SourceBuf, uint8_t *data, uint8_t pktNo, uint
|
||||
data[1] = pktNo;
|
||||
data[2] = (~pktNo);
|
||||
file_ptr = SourceBuf;
|
||||
|
||||
|
||||
/* Filename packet has valid data */
|
||||
for (i = PACKET_HEADER; i < size + PACKET_HEADER;i++)
|
||||
{
|
||||
@@ -357,7 +354,7 @@ void Ymodem_PreparePacket(uint8_t *SourceBuf, uint8_t *data, uint8_t pktNo, uint
|
||||
|
||||
/**
|
||||
* @brief Update CRC16 for input byte
|
||||
* @param CRC input value
|
||||
* @param CRC input value
|
||||
* @param input byte
|
||||
* @retval None
|
||||
*/
|
||||
@@ -375,7 +372,7 @@ uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte)
|
||||
if(crc & 0x10000)
|
||||
crc ^= 0x1021;
|
||||
}
|
||||
|
||||
|
||||
while(!(in & 0x10000));
|
||||
|
||||
return crc & 0xffffu;
|
||||
@@ -395,7 +392,7 @@ uint16_t Cal_CRC16(const uint8_t* data, uint32_t size)
|
||||
|
||||
while(data < dataEnd)
|
||||
crc = UpdateCRC16(crc, *data++);
|
||||
|
||||
|
||||
crc = UpdateCRC16(crc, 0);
|
||||
crc = UpdateCRC16(crc, 0);
|
||||
|
||||
@@ -443,7 +440,7 @@ void Ymodem_SendPacket(uint8_t *data, uint16_t length)
|
||||
*/
|
||||
uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t sizeFile)
|
||||
{
|
||||
|
||||
|
||||
uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD];
|
||||
uint8_t filename[FILE_NAME_LENGTH];
|
||||
uint8_t *buf_ptr, tempCheckSum;
|
||||
@@ -459,11 +456,11 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
filename[i] = sendFileName[i];
|
||||
}
|
||||
CRC16_F = 1;
|
||||
|
||||
|
||||
/* Prepare first block */
|
||||
Ymodem_PrepareIntialPacket(&packet_data[0], filename, &sizeFile);
|
||||
|
||||
do
|
||||
|
||||
do
|
||||
{
|
||||
/* Send Packet */
|
||||
Ymodem_SendPacket(packet_data, PACKET_SIZE + PACKET_HEADER);
|
||||
@@ -480,12 +477,12 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
tempCheckSum = CalChecksum (&packet_data[3], PACKET_SIZE);
|
||||
Send_Byte(tempCheckSum);
|
||||
}
|
||||
|
||||
|
||||
/* Wait for Ack and 'C' */
|
||||
if (Receive_Byte(&receivedC[0], 10000) == 0)
|
||||
if (Receive_Byte(&receivedC[0], 10000) == 0)
|
||||
{
|
||||
if (receivedC[0] == ACK)
|
||||
{
|
||||
{
|
||||
/* Packet transferred correctly */
|
||||
ackReceived = 1;
|
||||
}
|
||||
@@ -495,7 +492,7 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
errors++;
|
||||
}
|
||||
}while (!ackReceived && (errors < 0x0A));
|
||||
|
||||
|
||||
if (errors >= 0x0A)
|
||||
{
|
||||
return errors;
|
||||
@@ -504,8 +501,8 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
size = sizeFile;
|
||||
blkNumber = 0x01;
|
||||
/* Here 1024 bytes package is used to send the packets */
|
||||
|
||||
|
||||
|
||||
|
||||
/* Resend packet if NAK for a count of 10 else end of communication */
|
||||
while (size)
|
||||
{
|
||||
@@ -520,7 +517,7 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
if (size >= PACKET_1K_SIZE)
|
||||
{
|
||||
pktSize = PACKET_1K_SIZE;
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -540,14 +537,14 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
tempCheckSum = CalChecksum (&packet_data[3], pktSize);
|
||||
Send_Byte(tempCheckSum);
|
||||
}
|
||||
|
||||
|
||||
/* Wait for Ack */
|
||||
if ((Receive_Byte(&receivedC[0], 100000) == 0) && (receivedC[0] == ACK))
|
||||
{
|
||||
ackReceived = 1;
|
||||
ackReceived = 1;
|
||||
if (size > pktSize)
|
||||
{
|
||||
buf_ptr += pktSize;
|
||||
buf_ptr += pktSize;
|
||||
size -= pktSize;
|
||||
if (blkNumber == (USER_FLASH_SIZE/1024))
|
||||
{
|
||||
@@ -570,36 +567,36 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
}
|
||||
}while(!ackReceived && (errors < 0x0A));
|
||||
/* Resend packet if NAK for a count of 10 else end of communication */
|
||||
|
||||
|
||||
if (errors >= 0x0A)
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
ackReceived = 0;
|
||||
receivedC[0] = 0x00;
|
||||
errors = 0;
|
||||
do
|
||||
do
|
||||
{
|
||||
Send_Byte(EOT);
|
||||
/* Send (EOT); */
|
||||
/* Wait for Ack */
|
||||
if ((Receive_Byte(&receivedC[0], 10000) == 0) && receivedC[0] == ACK)
|
||||
{
|
||||
ackReceived = 1;
|
||||
ackReceived = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors++;
|
||||
}
|
||||
}while (!ackReceived && (errors < 0x0A));
|
||||
|
||||
|
||||
if (errors >= 0x0A)
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
|
||||
/* Last packet preparation */
|
||||
ackReceived = 0;
|
||||
receivedC[0] = 0x00;
|
||||
@@ -613,8 +610,8 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
{
|
||||
packet_data [i] = 0x00;
|
||||
}
|
||||
|
||||
do
|
||||
|
||||
do
|
||||
{
|
||||
/* Send Packet */
|
||||
Ymodem_SendPacket(packet_data, PACKET_SIZE + PACKET_HEADER);
|
||||
@@ -623,12 +620,12 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
tempCRC = Cal_CRC16(&packet_data[3], PACKET_SIZE);
|
||||
Send_Byte(tempCRC >> 8);
|
||||
Send_Byte(tempCRC & 0xFF);
|
||||
|
||||
|
||||
/* Wait for Ack and 'C' */
|
||||
if (Receive_Byte(&receivedC[0], 10000) == 0)
|
||||
if (Receive_Byte(&receivedC[0], 10000) == 0)
|
||||
{
|
||||
if (receivedC[0] == ACK)
|
||||
{
|
||||
{
|
||||
/* Packet transferred correctly */
|
||||
ackReceived = 1;
|
||||
}
|
||||
@@ -643,16 +640,16 @@ uint8_t Ymodem_Transmit (uint8_t *buf, const uint8_t* sendFileName, uint32_t siz
|
||||
if (errors >= 0x0A)
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
do
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
Send_Byte(EOT);
|
||||
/* Send (EOT); */
|
||||
/* Wait for Ack */
|
||||
if ((Receive_Byte(&receivedC[0], 10000) == 0) && receivedC[0] == ACK)
|
||||
{
|
||||
ackReceived = 1;
|
||||
ackReceived = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file STM32F4xx_IAP/inc/ymodem.h
|
||||
* @file STM32F4xx_IAP/inc/ymodem.h
|
||||
* @author MCD Application Team
|
||||
* @version V1.0.0
|
||||
* @date 10-October-2011
|
||||
* @brief This file provides all the software function headers of the ymodem.c
|
||||
* @brief This file provides all the software function headers of the ymodem.c
|
||||
* file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
|
||||
Reference in New Issue
Block a user