0% found this document useful (0 votes)
6 views3 pages

FP CRC Calc

Uploaded by

Nayana PS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

FP CRC Calc

Uploaded by

Nayana PS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdint.h>
#include <string.h>

/*******************************************************************************
* Definitions
******************************************************************************/
#define MAX_PACKET_SIZE 255 // Maximum packet size in bytes, adjust as needed

// Protocol-specific constants
#define FRAME_SYNC_BYTE 0xAA

// Protocol field definitions


#define CMD_INDEX 1
#define LENGTH_INDEX 2
#define PARAM_START_INDEX 3 // Index where parameter data starts
#define MAX_FRAME_SYNC_RETRY_COUNT 10

// Define the packet structure


typedef struct {
uint8_t frameSync;
uint8_t command;
uint8_t remainingDataLength;
uint8_t parameter[MAX_PACKET_SIZE - 4]; // Adjust the size accordingly
uint8_t crc;
} ProtocolPacket;

ProtocolPacket packet;
uint16_t packetLength = 0;
uint8_t parameter[MAX_PACKET_SIZE - 4] = {0};
uint8_t paramLength = 0x01;

uint8_t sendBuff[MAX_PACKET_SIZE] = {0x00};


uint8_t sendBuffCount = 0;

uint8_t command = 0;
// CRC8 calculation function (you can use your own CRC8 function)
static uint8_t crc8_check(const uint8_t *ptr, uint16_t len)
{
uint8_t i;
uint8_t crc = 0x00; /* Initialize the CRC value to 0x00 */

if (NULL == ptr || 0 == len)


{
return 0;
}

while (len--)
{
crc ^= *ptr++; /* XOR each byte of data with the current CRC value */
for (i = 8; i > 0; --i)
{
if (crc & 0x80)
crc = (crc << 1) ^ 0x31;
else
crc = (crc << 1);
}
}
return crc; /* Return the calculated CRC value */
}

// Function to create a packet


uint16_t createPacket(uint8_t command, const uint8_t *parameter, uint8_t
paramLength, ProtocolPacket *packet)
{

if (paramLength > MAX_PACKET_SIZE - 4) {


// Parameter too long to fit in the packet
return 0;
}

// memcpy(sendBuff, 0, sizeof(sendBuff));

// Initialize the packet


packet->frameSync = FRAME_SYNC_BYTE;
sendBuff[sendBuffCount++] = packet->frameSync;
packet->command = command;
sendBuff[sendBuffCount++] = packet->command;
packet->remainingDataLength = paramLength;
sendBuff[sendBuffCount++] = packet->remainingDataLength;

// Copy the parameter data into the packet


memcpy(packet->parameter, parameter, paramLength);
for(uint8_t count = 0;count < paramLength ;count++)
{
sendBuff[sendBuffCount++] = packet->parameter[count];
}

// Calculate and set the CRC8 checksum


packet->crc = crc8_check((const uint8_t *)packet, paramLength + 3);
sendBuff[sendBuffCount++] = packet->crc;
sendBuffCount = 0;
// Return the total packet length (including frame sync, command, length,
data, and CRC)
return paramLength + 4;
}

// Function to print a packet as if it's being sent to a slave device


void printPacket(const ProtocolPacket *packet, uint16_t packetLength)
{
// Formated
printf("\nSending Packet to Slave:\n");
printf("Frame Sync: 0x%02X\n", packet->frameSync);
printf("Command: 0x%02X\n", packet->command);
printf("Remaining Data Length: 0x%02X\n", packet->remainingDataLength);

printf("Parameter Data: ");


for (int i = 0; i < packetLength - 4; i++) {
printf("0x%02X ", packet->parameter[i]);
}
printf("\n");
printf("CRC: 0x%02X\n", packet->crc);
printf("Packet Length: %u bytes\n\n", packetLength);

//Single line
printf("0x%02X ", packet->frameSync);
printf("0x%02X ", packet->command);
printf("0x%02X ", packet->remainingDataLength);
for (int i = 0; i < packetLength - 4; i++)
{
printf("0x%02X ", packet->parameter[i]);
}
printf("0x%02X \n\n", packet->crc);

int main()
{
while (1)
{
printf("CRC calculation\n");

printf("Enter command:\n");
scanf("%hhx", &command);

printf("Enter data Length:\n");


scanf("%hhx", &paramLength);

if (paramLength > MAX_PACKET_SIZE - 4)


{
printf("Data length exceeds maximum allowed.\n");
continue;
}

for (uint8_t i = 0; i < paramLength; i++)


{
printf("Enter data[%d]:\n", i);
scanf("%hhx", &parameter[i]);
}

packetLength = createPacket(command, parameter, paramLength , &packet);


printPacket(&packet,packetLength);

}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy