ymodem.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00032 #include "../common/common.h"
00033 #include "ymodem.h"
00034
00035
00036 uint16_t YModem::UpdateCRC16(uint16_t crcIn, uint8_t byte)
00037 {
00038 uint32_t crc = crcIn;
00039 uint32_t in = byte|0x100;
00040 do
00041 {
00042 crc <<= 1;
00043 in <<= 1;
00044 if(in&0x100)
00045 ++crc;
00046 if(crc&0x10000)
00047 crc ^= 0x1021;
00048 }
00049 while(!(in&0x10000));
00050 return crc&0xffffu;
00051 }
00052
00053
00054 uint16_t YModem::CRC16(const uint8_t* data, size_t size)
00055 {
00056 uint32_t crc = 0;
00057 const uint8_t* dataEnd = data+size;
00058 while(data<dataEnd)
00059 crc = UpdateCRC16(crc,*data++);
00060 crc = UpdateCRC16(crc,0);
00061 crc = UpdateCRC16(crc,0);
00062 return crc&0xffffu;
00063 }
00064
00065
00066 uint8_t YModem::Checksum(const uint8_t* data, size_t size)
00067 {
00068 int sum = 0;
00069 const uint8_t* dataEnd = data+size;
00070 while(data<dataEnd)
00071 sum += *data++;
00072 return sum&0xffu;
00073 }
00074
00075
00076 int YModem::InChar(unsigned timeout)
00077 {
00078 uint8_t c = 0;
00079 int result = Port.In(&c,1,timeout);
00080 if(result==1)
00081 return c;
00082 if(result==0)
00083 return ErrorTimeout;
00084 return result;
00085 }
00086
00087
00088 void YModem::Cancel()
00089 {
00090 const uint8_t CancelString[] = { CAN,CAN,CAN,CAN,CAN };
00091 Port.Out(CancelString,sizeof(CancelString),1000);
00092 }