// возвращает рассчитанное значение CRC static uint64 crcCalculate(byte *data, uint32 datasize) { byte FieldSize = crcGetFieldSize(datasize); switch (FieldSize) { case 1: return CalcCRC8(data, datasize); case 2: return CalcCRC16(data, datasize); case 4: return CalcCRC32(data, datasize); case 8: return CalcCRC64(data, datasize); default: printf("Invalid CRCFieldSize size: %d\n", FieldSize); break; } return 0xFFFFFFFF; }
unsigned long mmcWriteBlocks(CSDRegister csdReg, unsigned long address, unsigned long numBytes) { unsigned char start_token, data_response_token, temp, isBusy; unsigned short crc16; unsigned long numBytesWritten = 0; unsigned long numBlocks = (unsigned long)ceil((float)numBytes/csdReg.WriteBlockLength); if (1 == numBlocks) /*single block write*/ { /*set ups*/ start_token = 0xFE; /*for WRITE_BLOCK*/ crc16 = CalcCRC16(mmc_read_write_buffer, csdReg.WriteBlockLength); /*issue CMD_24 (WRITE_BLOCK)*/ mmcSendCMD(CMD_24, address); /*read the corresponding R1 response*/ mmcReadResponse(2); /*interpret the response, and if it's a right one, then, send a data block*/ if (mmcGetR1Response(2) == R1_COMPLETE) { /*send a data block to write*/ mmcWriteReadByte(start_token); mmcWriteBuffer(csdReg.WriteBlockLength); mmcWriteReadByte((unsigned char)(crc16 >> 8)); mmcWriteReadByte((unsigned char)(crc16 & 0x00FF)); /*wait until the data response token is completely transmitted*/ isBusy = 0; do { mmcReadResponse(RESPONSE_BUFFER_LENGTH); temp = mmcGetDataResponseToken(&isBusy); if (temp != 0xFF) { data_response_token = temp; } }while(isBusy == 1); if (data_response_token == 0x02) /*Data accepted*/ { numBytesWritten = csdReg.WriteBlockLength; } }
int YMODEMCLASS::CheckDataBlock (void) { BYTE hiCRC ,loCRC ; int bCRC ; BYTE checksum ; BOOL checkOK=TRUE ; int rVal = FT_SUCCESS ; if (fCRC) { hiCRC = CommGetChar (oiConn) ; loCRC = CommGetChar (oiConn) ; bCRC = CalcCRC16 (lpBuffer, maxblocksize) ; if (hiCRC!=HIBYTE(bCRC)||loCRC!=LOBYTE(bCRC)) checkOK = FALSE ; } else { checksum = CommGetChar (oiConn) ; if(checksum!=CheckSum (lpBuffer)) checkOK = FALSE ; } if (checkOK) { if (dataBlock0) rVal = CheckDataBlock0() ; else { fltr_state = WAIT_SOH ; WriteToFile () ; } } else { rvDataCount = 0 ; setmem(lpBuffer, maxblocksize, ' ') ; CommPutChar (oiConn, NAK) ; fltr_state = WAIT_SOH ; } return (rVal) ; }
// создаёт поле CRC. Возвращает его размер static byte crcMakeField(byte *data, uint32 datasize, byte *crcbuffer) { byte FieldSize = crcGetFieldSize(datasize); switch (FieldSize) { case 1: crcbuffer[0] = CalcCRC8(data, datasize); break; case 2: { word crc16 = CalcCRC16(data, datasize); if (IsLittleEndian()) memrev(&crc16, sizeof(crc16)); memcpy(crcbuffer, &crc16, sizeof(crc16)); break; } case 4: { uint32 crc32 = CalcCRC32(data, datasize); if (IsLittleEndian()) memrev(&crc32, sizeof(crc32)); memcpy(crcbuffer, &crc32, sizeof(crc32)); break; } case 8: { uint64 crc64 = CalcCRC64(data, datasize); if (IsLittleEndian()) memrev(&crc64, sizeof(crc64)); memcpy(crcbuffer, &crc64, sizeof(crc64)); break; } default: printf("Invalid CRCFieldSize size: %d\n", FieldSize); break; } return FieldSize; }
void CheckMasterCommand(void) { uint16 CRC=0; pDataIn = UART_Receive(&SizeIn); if (pDataIn != NULL) { pDataOut = UART_GetSendBuf(); pDataOut[0] = UART_SA_CHAR; if(ProcessMasterCommand(pDataIn, SizeIn, pDataOut+1, &SizeOut) ) { CRC = CalcCRC16(&pDataOut[1], SizeOut); SizeOut++; pDataOut[SizeOut++] = CRC & 0xFF; pDataOut[SizeOut++] = CRC >> 8; UART_SendAnswer(SizeOut); }
int YMODEMCLASS::WriteHeaderBlock (void) { ftime ft ; struct tm t ; char drive[10] ; char dir[50] ; char name[256] ; char ext[10] ; long fileLength ; int i ; fltr_state = WAIT_ACK ; lpBuffer[0] = SOH ; lpBuffer[1] = 0 ; lpBuffer[2] = 0xff ; for (i=3; i<133; i++) ; lpBuffer[i] = 0 ; if (fileCount>0) { fnsplit (filename, drive, dir, name, ext) ; strcat (name,ext) ; fileLength = SetFilePointer (hFile, 0, 2) ; SetFilePointer (hFile, 0, 0) ; getftime(hFile, &ft) ; t.tm_sec = ft.ft_tsec*2 ; t.tm_min = ft.ft_min ; t.tm_hour= ft.ft_hour ; t.tm_mday= ft.ft_day ; t.tm_mon = ft.ft_month - 1 ; t.tm_year= ft.ft_year +80 ; t.tm_isdst= FALSE ; putenv("TZ=GMT0GMT") ; tzset() ; sprintf (lpBuffer+3, "%s%c%ld %lo 0", filename, 0x00, fileLength, mktime(&t)) ; } else lpBuffer[3] = '\0' ; nTransSize = 132 ; if (fCRC) { nTransSize = 133 ; int vCRC = CalcCRC16(&lpBuffer[DATA_OFFSET], 128) ; lpBuffer[CHK_OFFSET] = HIBYTE (vCRC) ; lpBuffer[CHK_OFFSET+1] =LOBYTE(vCRC) ; } else lpBuffer[CHK_OFFSET] = CheckSum (&lpBuffer[DATA_OFFSET]) ; CommPutBuffer (oiConn, (BYTE*)lpBuffer, nTransSize) ; if (Status) { lstrcpy(lpStatus->filename, name) ; lpStatus->filesize = fileLength ; lpStatus->error = lErrorCount ; lpStatus->blocksize +=128 ; lpStatus->UpdateStatusWindow () ; if (lpStatus->transferStop) return (FT_LOCALCANCEL) ; } if (fileCount<=0) return (FT_FILEEND) ; else return (FT_SUCCESS) ; }
unsigned long mmcReadBlocks(CSDRegister csdReg, unsigned long address, unsigned long numBytes) { unsigned char start_token = 0xFF; unsigned short crc16 = 0xFFFF, r1_response = 0xFFFF; unsigned int i = 0; unsigned long numBytesRead = 0; unsigned long numBlocks = (unsigned long)ceil((float)numBytes/csdReg.ReadBlockLength); if (1 == numBlocks) /*single block read*/ { do { /*issue CMD_17 (READ_SINGLE_BLOCK)*/ mmcSendCMD(CMD_17, address); /*read the corresponding R1 response (note: need to ignore preceding zeros)*/ mmcReadResponse(5); r1_response = mmcGetR1Response(5); i++; }while ( (i < 5) && (r1_response != R1_COMPLETE) ); if (r1_response == R1_COMPLETE) { int start_index; /*read a block of data including CRC16*/ mmcReadBuffer(1+csdReg.ReadBlockLength+4); /*interpret the responses*/ start_token = mmcGetStartBlockToken(&start_index); if (start_index != -1) { crc16 = CalcCRC16(mmc_read_write_buffer+start_index, csdReg.ReadBlockLength+2); } if ( (start_token == TOKEN_START_OK) && /*if not, then, data error token was received*/ (crc16 == 0x0000) ) /*if not, then, crc error was detected*/ { numBytesRead = numBytes; } } } else /*multiple block read*/ { unsigned long b = 0; do { /*issue CMD_18 (READ_SINGLE_BLOCK)*/ mmcSendCMD(CMD_18, address); /*read the corresponding response (note: need to ignore preceding zeros)*/ mmcReadResponse(5); r1_response = mmcGetR1Response(5); i++; }while( (i < 5) && (r1_response != R1_COMPLETE) ); if (r1_response == R1_COMPLETE) { int start_index; for (b = 0; b < numBlocks; b++) { crc16 = 0xFFFF; /*read a block of data including CRC16*/ mmcReadBuffer(1+csdReg.ReadBlockLength+4); /*check the start block token and crc*/ start_token = mmcGetStartBlockToken(&start_index); if (start_index != -1) { crc16 = CalcCRC16(mmc_read_write_buffer+start_index, csdReg.ReadBlockLength+2); } if ( (start_token == TOKEN_START_OK) && (crc16 == 0x0000) ) { numBytesRead += csdReg.ReadBlockLength; } else { /*send the stop transmission command*/ mmcSendCMD(CMD_12, 0x00000000); /*wait for R1b and its tail*/ { unsigned short r1b_response = 0xFFFF; do { mmcReadResponse(RESPONSE_BUFFER_LENGTH); /*find an R1 response and the following busy signal*/ r1b_response = mmcGetR1bResponse(); }while(r1b_response == R1_BUSY); } /*break the loop*/ break; } } } } return numBytesRead; }
int main(void) { uint16_t CalcCRC16(uint8_t data_array[], int data_lenght); int i; /////////////////////OSCYLATOR 32MHZ/////////////////////////////////////////// init_osc32(); ////KONFIGURACJA ADC//////////////////////////////////////////////////////////////// konfiguracja_adc(); /////////UART//////////////////////////////////////////////////////////////////////// PORTC_OUTSET = PIN7_bm; //Let's make PC7 as TX PORTC_DIRSET = PIN7_bm; //TX pin as output PORTC_OUTCLR = PIN6_bm; PORTC_DIRCLR = PIN6_bm; //PC6 as RX setUpSerial(); /////////////////////////////////////////////////////////////////////////////// PORTF_DIR=PIN0_bm; //PORTF_OUTSET=PIN0_bm; //zapalenie diody PORTC.DIRSET = PIN0_bm; // pin C0 jako wyjœcie PORTC.DIRSET = PIN1_bm; // pin C0 jako wyjœcie ////////////////////////Flip//////////////////////////////////////////////// PORTE.DIRCLR = PIN5_bm; // pin E5 jako wejœcie PORTE.PIN5CTRL = PORT_OPC_PULLUP_gc; // podci¹gniêcie do zasilania ////////////////////////////Klawisze gora do³ prawo lewo////////////////////////////////////////// PORTF.DIRCLR = PIN1_bm|PIN2_bm|PIN5_bm|PIN6_bm; PORTF.PIN1CTRL = PORT_OPC_PULLUP_gc|PORT_ISC_FALLING_gc; PORTF.PIN2CTRL = PORT_OPC_PULLUP_gc; PORTF.PIN5CTRL = PORT_OPC_PULLUP_gc; PORTF.PIN6CTRL = PORT_OPC_PULLUP_gc; ///////////////////////////////////Przerwanie INT0///////////////////////////////////////////////////// init_int0(); //////////////////////////TIMER0////////////////////////////////////////////////////// timer(); //////////////////////W£¥CZENIE PRZERWAÑ////////////////////////////////////////// sei(); // globalne w³¹czenie przerwañ //////////////////////////////////////////////////////////////////////////////////////// LcdInit(); // inicjalizacja sterownika LCD LcdGoto(0,0); Lcd("*Inzynierka_DiagBoX*"); // wyœwietlenie napisu LcdGoto(20,0); Lcd("Przedstawia:"); LcdGoto(25,1); Lcd("Marek Wudarczyk"); _delay_ms(1000); LcdClear(); while (1){ LcdClear(); LcdGoto(0,0); LcdDec(ADC); LcdGoto(5,0); Lcd("CNT = "); LcdDec(TCC0.CNT); LcdGoto(0,1); LcdDec(TCC0.PER); LcdGoto(20,1); Lcd("czas:"); LcdDec(c); i=usart_getc(); if(i==2){ while(1){ uint8_t i,j=0; i=usart_getc(); uint16_t ADC = ADC_GetResults(); uint8_t buff[] = {mlody_bajt(ADC),stary_bajt(ADC)}; buff[2] = mlody_bajt(CalcCRC16(buff,2)); buff[3] = stary_bajt(CalcCRC16(buff,2)); while(j<4){ sendChar(buff[j]); j++; } LcdClear(); Lcd("CRC:"); LcdDec(buff[2]); _delay_ms(10); if(i == 2){ break; } } i=0; } if(i==1){ PORTF_OUTTGL=PIN0_bm; i=0; } if(!(PORTE.IN & PIN5_bm)) /* je¿eli przycisk FLIP jest wciœniêty*/ { while(1){ uint32_t licz; licz++; if((licz>15100)&&(PORTE.IN & PIN5_bm)){ licz=0; c+=10; break; } } } _delay_ms(100); } }