注:因为在写入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)
while(USART_RX_CN