IAP(二)添加xmodem协议

本文介绍了xmodem协议在IAP过程中的应用,由于Flash擦写以1K字节为单位,故数据接收时先存于tempbuff,待积累满1K字节再一次性写入Flash。详细解析了xmodem协议代码,包括数据分块处理的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xmodem协议简介

注:因为在写入flash时,是采用 1k字节整块擦除的方式,所以对原来的代码有了更改,如果是128字节发来的数据,先暂存到tempbuff中,然后等攒够1K字节在向flash中写入。

xmodem协议代码

#include "xmodem.h"
#include "dma.h"
#include "string.h"
#include "delay.h"
//#include "crc16.h"
#define SOH  0x01
#define STX  0x02
#define EOT  0x04
#define ACK  0x06
#define NAK  0x15
#define CAN  0x18
#define CTRLZ 0x1A
#define DLY_1S 1000
#define MAXRETRANS 25

static int last_error = 0;
static int readnum =0;
u8 DMA_USART_Buff[USART_BUFF_SIZE];
unsigned char tempbuff[1024]={0};
int tempnum=0;
int crc16_ccitt(const unsigned char *ptr, int count)
{
	int crc;
	char i;
 
	crc = 0;
	while (--count >= 0)
	{
		crc = crc ^ (int) *ptr++ << 8;
		i = 8;
		do
		{
			if (crc & 0x8000)
				crc = crc << 1 ^ 0x1021;
			else
				crc = crc << 1;
		} while (--i);
	}
 
	return (crc);
}
void port_outbyte(unsigned char trychar)
{
// unsigned char buf[2];
 //buf[0] = trychar;
 //lowLevel_write(buf,1);
	memcpy((u8*)DMA_USART_Buff,(u8*)&trychar,1);
	MYDMA_Enable(DMA1_Channel4,1);//start the first DMA transfer
}

unsigned char port_inbyte(unsigned int time_out)
{
 unsigned char ch=-1;
// int i;
 last_error = 0;
 //if(lowLevel_read(&ch,1) == 1)
	w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值