原文 http://www.51hei.com/bbs/dpj-32461-1.html
最常用的几种通信传输协议有:XModem、YModem、ZModem等。
XModem是最早的协议之一,几乎所有的通讯程序支持的文件传输协议,它传输128字节信息块。一般都会选择YModem-1K传输,平时所说的YModem也是指的是YModem-1K。下面就讲讲它的传输协议。
st官方有demo可以参考
STM32F407的源代码 http://download.csdn.net/detail/liang890319/9901107
移植到其他板子https://www.dhlx.wang/STM32F411VET6/Simple_transplant_IAP_STSW-STM32067_to_STM32F411VET6.html
STM32F1XX相关代码资源
#define MODEM_SOH 0x01 //133字节数据包类型,接收正常回应0x06(含文件信息的第一个包接收正常需回应0x06、0x43)
#define MODEM_STX 0x02 //1029字节数据包类型,接收正常回应0x06
#define MODEM_EOT 0x04 //发送文件传输结束命令,接收正常回应0x06、0x43(启动空包发送)
#define MODEM_ACK 0x06 //发送确认应答,接收方crc校验成功或收到已定义的命令
#define MODEM_NAK 0x15 //发送重传当前数据包请求,接收方crc校验出错
#define MODEM_CAN 0x18 //发送取消传输命令,连续发送5个字符
#define MODEM_C 0x43 //发送大写字母C(三种情况下发送该字符: 1.启动通信握手.2.启动数据包发送.3.启动空包发送)
*/
uint16_t Y_Modem_CRC(uint8_t * buf, uint16_t len)
{
uint16_t chsum;
uint16_t stat;
uint16_t i;
uint8_t * in_ptr;
//指向要计算CRC的缓冲区开头
in_ptr = buf;
chsum = 0;
for (stat = len ; stat > 0; stat--) //len是所要计算的长度
{
chsum = chsum^(uint16_t)(*in_ptr++) << 8;
for (i=8; i!=0; i--) {
if (chsum & 0x8000){
chsum = chsum << 1 ^ 0x1021;
} else {
chsum = chsum << 1;
}
}
}
return chsum;
}
/**
******************************************************************************
* @file ymodem_daxia.c
* @author [email protected]
* @version V2.0.2
* @date 2014.12.06
* @brief Y-Modem 协议文件接收 (适用于ARM及X86平台)
******************************************************************************
* @copy
*
* Copyright (c) 2013-2014
*
*/
extern "C" {
#include "crc.h"
#include "crc16.h"
}
#ifndef STM32Fxxx
#include
#include
#endif
#ifdef STM32Fxxx
// 发送字符串到USB端点(USB虚拟串口)
void VCP_MI_02_IN(uint8_t *buf, uint8_t count);
void VCP_MI_00_IN(uint8_t *buf, uint8_t count);
#else
// 发送字符串到串口1(发送完毕关闭串口1)
void SendStringWithClose(unsigned char * buf, unsigned short len);
#endif
uint16_t Y_Modem_CRC(uint8_t * buf, uint16_t len);
#define __IO volatile
/* Includes ------------------------------------------------------------------*/
//#include "ymodem_daxia.h"
#ifdef STM32Fxxx
#include "stm32_flash_read_write.h"
#include "usb_lib.h"
#endif
/* Extern function -----------------------------------------------------------*/
#ifdef STM32Fxxx
extern void NVIC_Configuration(void);
extern void NVIC_DISABLE(void);
extern void ChipHalInit(void);
#endif
/* Extern variables ----------------------------------------------------------*/
extern __IO uint8_t USART1_Tx_Buffer[];
extern __IO uint16_t USART1_TxLength;
extern __IO uint16_t USART1_TxCount;
extern __IO uint8_t USART1_Rx_Buffer [];
extern __IO uint8_t USART1_Rx_Buffer2[];
extern __IO uint32_t USART1_RxCount;
extern __IO uint16_t USART1_Rx_ptr_in;
extern __IO uint16_t Y_Modem_TMP1;
extern __IO uint16_t Over_Time_Cnt;
extern __IO uint32_t into_app_cnt;
extern uint16_t CRC16;
extern uint16_t CRC16_Tmp;
extern uint8_t FileName[];
extern uint32_t FileLength;
extern uint32_t Write_Counter;
extern __IO bool Flag_App_Rece_OK;
extern __IO bool Flag1_1Ms; // 系统定时器1MS标志1
extern __IO bool Flag2_1Ms; // 系统定时器1MS标志2
/* Private define ------------------------------------------------------------*/
#define MODEM_SOH 0x01 //133字节数据包类型,接收正常回应0x06(含文件信息的第一个包接收正常需回应0x06、0x43)
#define MODEM_STX 0x02 //1029字节数据包类型,接收正常回应0x06
#define MODEM_EOT 0x04 //发送文件传输结束命令,接收正常回应0x06、0x43(启动空包发送)
#define MODEM_ACK 0x06 //发送确认应答,接收方crc校验成功或收到已定义的命令
#define MODEM_NAK 0x15 //发送重传当前数据包请求,接收方crc校验出错
#define MODEM_CAN 0x18 //发送取消传输命令,连续发送5个字符
#define MODEM_C 0x43 //发送大写字母C(三种情况下发送该字符: 1.启动通信握手.2.启动数据包发送.3.启动空包发送)
#define TIME_OVER_SETUP1 10000
#define TIME_OVER_SETUP2 30000
#ifdef STM32Fxxx
/*
函数功能: 大于或等于2K的程序数据,写入Flash
小于2048不写入,直接返回
*/
void Flash_Write_2K(void)
{
uint32_t tmp;
if(USART1_RxCount > Write_Counter){
tmp = USART1_RxCount - Write_Counter;
if(tmp >= 2048){
iap_write_appbin(FLASH_APP1_ADDR + Write_Counter, (uint8_t *)USART1_Rx_Buffer2, tmp);
Write_Counter += tmp;
}
}
}
/*
函数功能: 写入小于或等于128字节的程序数据
若最后一个包小于1024字节, 则发送128字节的数据包,以(SOH)开始
参数列表:
*/
void Flash_Write_128(void)
{
uint32_t tmp;
if(USART1_RxCount > Write_Counter){
tmp = USART1_RxCount - Write_Counter;
if(tmp <= 128){
iap_write_appbin(FLASH_APP1_ADDR + Write_Counter, (uint8_t *)USART1_Rx_Buffer2, tmp);
Write_Counter += tmp;
}
}
}
/* 虚拟串口MI_02接收监控 */
void VCP_MI_02_IN (uint8_t *buf, uint8_t count){
USB_SIL_Write(EP4_IN, &buf[0], count);
SetEPTxValid(ENDP4);
}
/* 虚拟串口MI_00接收监控 */
void VCP_MI_00_IN (uint8_t *buf, uint8_t count){
USB_SIL_Write(EP1_IN, &buf[0], count);
SetEPTxValid(ENDP1);
}
#endif
// 取消传输
void Cancel_Transmission(void)
{
uint8_t USB_IN_buf[5];
// 取消传输
USB_IN_buf[0] = MODEM_CAN;
USB_IN_buf[1] = MODEM_CAN;
USB_IN_buf[2] = MODEM_CAN;
USB_IN_buf[3] = MODEM_CAN;
USB_IN_buf[4] = MODEM_CAN;
#ifdef STM32Fxxx
USB_SIL_Write(EP4_IN, &USB_IN_buf[0], 5);
SetEPTxValid(ENDP4);
#else
#endif
}
// 回复确认
void Send_ACK(void)
{
uint8_t USB_IN_buf[2];
// 回复确认
USB_IN_buf[0] = MODEM_ACK;
#ifdef STM32Fxxx
USB_SIL_Write(EP4_IN, &USB_IN_buf[0], 1);
SetEPTxValid(ENDP4);
#endif
}
// 回复继续
void Send_MODEM_C(void)
{
uint8_t USB_IN_buf[1];
USB_IN_buf[0] = MODEM_C;
#ifdef STM32Fxxx
USB_SIL_Write(EP4_IN, &USB_IN_buf[0], 1);
SetEPTxValid(ENDP4);
#endif
}
// 回复确认、继续
void Send_ACK_Continue(void)
{
uint8_t USB_IN_buf[2];
// 回复确认、继续
USB_IN_buf[0] = MODEM_ACK;
USB_IN_buf[1] = MODEM_C;
#ifdef STM32Fxxx
USB_SIL_Write(EP4_IN, &USB_IN_buf[0], 2);
SetEPTxValid(ENDP4);
#endif
}
/*
函数功能: 为实时响应, 需连续调用,以尽快处理Y_Modem协议接收的数据
参数列表:
Flag_Timer_1MS 系统1ms标志传入函数, 但函数不对该标志做改动
Y_Modem_TMP1 全局变量
USART1_Rx_Buffer 4K byte 4K文件内容接收缓冲
USART1_Rx_ptr_in USART1_Rx_Buffer 数据指针当前位置
USART1_Rx_Buffer2 4K byte 4K文件内容写入Flash缓冲
USART1_RxCount USART1_Rx_Buffer2 数据指针当前位置
*/
extern uint8_t Flag_App_Update;
static uint16_t temp_cnt;
static uint16_t temp_cnt2 = 0;
void Y_Modem_Receive(void)
{
uint32_t Y_Modem_TMP2;
uint32_t Y_Modem_TMP3;
uint32_t Y_Modem_TMP4;
uint32_t Y_Modem_TMP5;
uint8_t USB_IN_buf[5];
#ifndef STM32Fxxx
Flag2_1Ms = true;
#endif
switch(Y_Modem_TMP1){
case 0:
break;
case 1:
if(Flag2_1Ms == true){
Flag2_1Ms = false;
if(Flag_App_Update == 1){
// 更新标志上电随机设置为1或从用户程序跳转过来
// 上电随机设置为1的情况不能启动,所以存在bug
}else{
// 更新标志上电随机设置为非零值
if (++temp_cnt2 > 200){
temp_cnt2 = 0;
Flag_App_Update = 0;
}
}
if (++temp_cnt > 2000){
temp_cnt = 0;
Send_MODEM_C();
#ifdef STM32Fxxx
USB_IN_buf[0] = *((uint8_t *)0x20004000);
VCP_MI_00_IN(&USB_IN_buf[0],1);
#endif
// 4K 缓冲清零
for(Y_Modem_TMP2 = 0; Y_Modem_TMP2 < 0x1000; ++Y_Modem_TMP2) {
USART1_Rx_Buffer[Y_Modem_TMP2] = 0;
USART1_Rx_Buffer2[Y_Modem_TMP2] = 0;
}
USART1_Rx_ptr_in = 0;
USART1_RxCount = 0;
Write_Counter = 0;
}
}
if(USART1_Rx_ptr_in > 0){
if(USART1_Rx_Buffer[0] == MODEM_SOH){
if(USART1_Rx_ptr_in >= 133){
Y_Modem_TMP1 = 2;
}
}
}
break;
case 2:
// 第2步,处理128字节数据块(传输文件名,文件大小信息)
if(Flag2_1Ms == true){
Flag2_1Ms = false;
if (++Over_Time_Cnt > TIME_OVER_SETUP1){
Cancel_Transmission(); // 取消传输
Y_Modem_TMP1 = 0;
Over_Time_Cnt = 0;
#ifdef STM32Fxxx
USB_IN_buf[0] = 0x01;
VCP_MI_00_IN(&USB_IN_buf[0],1);
#endif
}
}
if(USART1_Rx_ptr_in > 0){
// 第2步,如果有数据传输,则进行处理
// MODEM_SOH 128字节数据块
if(USART1_Rx_Buffer[0] == MODEM_SOH){
if(USART1_Rx_ptr_in >= 133){
CRC16 = Y_Modem_CRC((uint8_t *)&USART1_Rx_Buffer[3], 128);
CRC16_Tmp = USART1_Rx_Buffer[131];
CRC16_Tmp = CRC16_Tmp << 8;
CRC16_Tmp |= USART1_Rx_Buffer[132];
if(CRC16_Tmp == CRC16){
// 获取文件名
for(Y_Modem_TMP2 = 0; Y_Modem_TMP2 < 128; ++Y_Modem_TMP2) {
FileName[Y_Modem_TMP2] = 0;
}
Y_Modem_TMP3 = 0;
for(Y_Modem_TMP2 = 3; Y_Modem_TMP2 < 128; ) {
if(USART1_Rx_Buffer[Y_Modem_TMP2]!= 0){
FileName[Y_Modem_TMP3] = USART1_Rx_Buffer[Y_Modem_TMP2];
} else {
++Y_Modem_TMP2;
break;
}
++Y_Modem_TMP3;
++Y_Modem_TMP2;
}
// 获取文件长度
for(FileLength = 0; Y_Modem_TMP2 <= 133; ++Y_Modem_TMP2) {
if(USART1_Rx_Buffer[Y_Modem_TMP2]!= 0){
if((USART1_Rx_Buffer[Y_Modem_TMP2] >= 0x30)
&& (USART1_Rx_Buffer[Y_Modem_TMP2] <= 0x39)){
FileLength = FileLength * 10 + USART1_Rx_Buffer[Y_Modem_TMP2] - 0x30;
} else {
break;
}
} else {
break;
}
}
// STM32F103C8 只有64K 大于64K 则取消传输
if(FileLength > 1024 * 64){
Cancel_Transmission(); // 取消传输
Y_Modem_TMP1 = 0;
} else {
USART1_Rx_ptr_in = 0;
Send_ACK_Continue();
Over_Time_Cnt = 0;
Y_Modem_TMP1 = 3;
}
into_app_cnt = 0; // 延迟计数清0
}
}
}
}
break;
case 3:
// 第3步,接收并写入程序数据,以2K的字节数据块写入
if(Flag2_1Ms == true){
Flag2_1Ms = false;
if (++Over_Time_Cnt > TIME_OVER_SETUP2){
Cancel_Transmission(); // 取消传输
Y_Modem_TMP1 = 0;
into_app_cnt = 0; // 延迟计数清0
}
}
if(USART1_Rx_ptr_in > 0){
// MODEM_STX 1029字节数据块
if(USART1_Rx_Buffer[0] == MODEM_STX){
// 最后一包数据包大于128且小于1024字节时,补充以0x1A
if(USART1_Rx_ptr_in >= 1029){
CRC16 = Y_Modem_CRC((uint8_t *)&USART1_Rx_Buffer[3], 1024);
CRC16_Tmp = USART1_Rx_Buffer[1027];
CRC16_Tmp = CRC16_Tmp << 8;
CRC16_Tmp |= USART1_Rx_Buffer[1028];
if(CRC16_Tmp == CRC16){
Y_Modem_TMP5 = USART1_RxCount - Write_Counter;
for(Y_Modem_TMP2 = 0; Y_Modem_TMP2 < 1024; ++Y_Modem_TMP2) {
Y_Modem_TMP4 = USART1_RxCount;
Y_Modem_TMP4 = Y_Modem_TMP4 + Y_Modem_TMP2;
if(Y_Modem_TMP4 < FileLength){
USART1_Rx_Buffer2[Y_Modem_TMP5++] = USART1_Rx_Buffer[Y_Modem_TMP2+3];
}else{
// 最后一个数据包大于128且小于1024字节时,补充的0x1A不复制,退出循环
break;
}
USART1_Rx_Buffer[Y_Modem_TMP2] = 0;
}
USART1_RxCount += Y_Modem_TMP2;
#ifdef STM32Fxxx
Flash_Write_2K(); // USART1_RxCount - Write_Counter < 2048, 调用该函数无效
#endif
for(; Y_Modem_TMP2 <= 1029; ++Y_Modem_TMP2) {
USART1_Rx_Buffer[Y_Modem_TMP2] = 0;
}
USART1_Rx_ptr_in = 0;
Send_ACK(); // 回复确认
into_app_cnt = 0; // 延迟计数清0
}else{
// 出现CRC错误
}
}
}
// MODEM_SOH 133字节数据块
if(USART1_Rx_Buffer[0] == MODEM_SOH){
// 最后一包数据不足128字节时,补充以0x1A,总计补满133字节
if(USART1_Rx_ptr_in >= 133){
CRC16 = Y_Modem_CRC((uint8_t *)&USART1_Rx_Buffer[3], 128);
CRC16_Tmp = USART1_Rx_Buffer[131];
CRC16_Tmp = CRC16_Tmp << 8;
CRC16_Tmp |= USART1_Rx_Buffer[132];
if(CRC16_Tmp == CRC16){
Y_Modem_TMP5 = USART1_RxCount - Write_Counter;
for(Y_Modem_TMP2 = 0; Y_Modem_TMP2 < 128; ++Y_Modem_TMP2) {
Y_Modem_TMP4 = USART1_RxCount;
Y_Modem_TMP4 += Y_Modem_TMP2;
if(Y_Modem_TMP4 < FileLength){
USART1_Rx_Buffer2[Y_Modem_TMP5++] = USART1_Rx_Buffer[Y_Modem_TMP2+3];
}else{
break;
}
USART1_Rx_Buffer[Y_Modem_TMP2] = 0;
}
USART1_RxCount += Y_Modem_TMP2;
#ifdef STM32Fxxx
Flash_Write_128();
#endif
for(; Y_Modem_TMP2 <= 133; ++Y_Modem_TMP2) {
USART1_Rx_Buffer[Y_Modem_TMP2] = 0;
}
USART1_Rx_ptr_in = 0;
Send_ACK(); // 回复确认
into_app_cnt = 0; // 延迟计数清0
}
}
}
// 文件传输结束命令
if(USART1_Rx_Buffer[0] == MODEM_EOT){
// 文件传输结束
if(FileLength == USART1_RxCount){
Flag_App_Rece_OK = true;
}
USART1_Rx_ptr_in = 0;
Send_ACK_Continue(); // 回复确认、继续
Y_Modem_TMP1 = 4;
into_app_cnt = 0; // 延迟计数清0
}
// 取消传输命令
if(USART1_Rx_Buffer[0] == MODEM_CAN){
// 取消传输
for(Y_Modem_TMP2 = 0; Y_Modem_TMP2 <= 1029; ++Y_Modem_TMP2) {
USART1_Rx_Buffer[Y_Modem_TMP2] = 0;
}
USART1_Rx_ptr_in = 0;
Y_Modem_TMP1 = 4;
into_app_cnt = 0; // 延迟计数清0
}
}
break;
case 4:
if(Flag2_1Ms == true){
Flag2_1Ms = false;
if (++Over_Time_Cnt > TIME_OVER_SETUP1){
Cancel_Transmission(); // 取消传输
Y_Modem_TMP1 = 0;
into_app_cnt = 0; // 延迟计数清0
}
}
// MODEM_SOH 133字节数据块
if(USART1_Rx_Buffer[0] == MODEM_SOH){
// 最后一包数据不足128字节时,补充以0x1A,总计补满133字节
if(USART1_Rx_ptr_in >= 133){
// 最后一个空数据包,133字节,前3个字节是01 00 FF,其余是0
CRC16_Tmp = 0;
for(Y_Modem_TMP2 = 3; Y_Modem_TMP2 <= 133; ++Y_Modem_TMP2) {
CRC16_Tmp += USART1_Rx_Buffer[Y_Modem_TMP2];
}
if(CRC16_Tmp==0){
if(Flag_App_Rece_OK){
// 未写完的程序数据写入,[1-128) OR [1024,2048)
if(FileLength > Write_Counter){
Y_Modem_TMP4 = FileLength - Write_Counter;
if(Y_Modem_TMP4){
#ifdef STM32Fxxx
iap_write_appbin(FLASH_APP1_ADDR + Write_Counter, (uint8_t *)USART1_Rx_Buffer2, Y_Modem_TMP4);
#endif
Write_Counter += Y_Modem_TMP4;
}
}
}
Send_ACK(); // 回复确认,结束文件传输
Y_Modem_TMP1 = 5; // 文件传输结束
}
into_app_cnt = 0; // 延迟计数清0
}
}
break;
case 5:
if(Flag2_1Ms == true){
Flag2_1Ms = false;
#ifndef STM32Fxxx
if (++Over_Time_Cnt > 500){
#else
if (++Over_Time_Cnt > 5000){
#endif
Over_Time_Cnt = 0;
Y_Modem_TMP1 = 0;
into_app_cnt = 0; // 延迟5秒
}
}
break;
default:
#ifdef STM32Fxxx
__NOP();
__NOP();
__NOP();
#endif
break;
}
}