Skip to content

Instantly share code, notes, and snippets.

@ksvbka
Last active August 29, 2015 14:21
Show Gist options
  • Save ksvbka/f054a586ed5ef04d5453 to your computer and use it in GitHub Desktop.
Save ksvbka/f054a586ed5ef04d5453 to your computer and use it in GitHub Desktop.
Lib to process data package
/********************************************************************************
Product: MSP430 service
Module: DataPackageProc
Author: 5/23/2015, by KIENLTB
Description: Process Datapackage
********************************************************************************/
#ifndef HEADER_GUARD
#define HEADER_GUARD
/*-----------------------------------------------------------------------------*/
/* Header inclusions */
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Constant definitions */
/*-----------------------------------------------------------------------------*/
#define MAX_LENGTH 255
//#define START_BYTE 0x55 //01010101
//#define STOP_BYTE 0xAA //10101010
//#define ESC_BYTE 0x7D //1111101
// Choose new indicator to test by terminal
#define START_BYTE 't'
#define STOP_BYTE 's'
#define ESC_BYTE 'e'
/*-----------------------------------------------------------------------------*/
/* Data type definitions */
/*-----------------------------------------------------------------------------*/
// Data Package data struct
typedef struct tagDATAPACKAGE
{
BYTE data[MAX_LENGTH];
BYTE length;
}DATAPACKAGE, *PDATAPACKAGE;
// State of receive data
typedef enum
{
WAIT_START_BYTE,
IN_MESG,
STOP,
}RX_STATE;
/*-----------------------------------------------------------------------------*/
/* Macro definitions */
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Global variables */
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Function prototypes */
/*-----------------------------------------------------------------------------*/
VOID InitRxDataPackage();
VOID CollectingDataPackage(PVOID buff);
BOOL IsInvalidDataPackage(PDATAPACKAGE mesg);
VOID EncodeDataPackage(PVOID buff, PDATAPACKAGE mesg);
VOID DecodeDataPackage(PDATAPACKAGE mesg, PVOID resul);
#endif // HEADER_GUARD
/********************************************************************************
Product: MSP430 Service Framework
Module: DataPackage Service
Created: 5/23/2015, by KIENLTB
Description: Service for process DataPackage
********************************************************************************/
/*-----------------------------------------------------------------------------*/
/* Header inclusions */
/*-----------------------------------------------------------------------------*/
#include "datapackageProc.h"
#include "uart.h"
/*-----------------------------------------------------------------------------*/
/* Local Constant definitions */
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Local Macro definitions */
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Local Data type definitions */
/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
/* Global variables */
/*-----------------------------------------------------------------------------*/
BYTE volatile g_flagFrameReceived = 0;
DATAPACKAGE DataPackageRX;
RX_STATE volatile state; // state of rx data.
INTERNAL volatile BYTE lastRxData;
INTERNAL volatile BYTE nIndex;
/*-----------------------------------------------------------------------------*/
/* Function prototypes */
/*-----------------------------------------------------------------------------*/
VOID InitRxDataPackage();
VOID CollectingDataPackage(PVOID buff);
BOOL IsInvalidDataPackage(PDATAPACKAGE mesg);
VOID EncodeDataPackage(PVOID buff, PDATAPACKAGE mesg);
VOID DecodeDataPackage(PDATAPACKAGE mesg, PVOID resul);
/*-----------------------------------------------------------------------------*/
/* Function implementations */
/*-----------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------
Function: InitRxDataPackage
Purpose: Init Rx Data Package
Parameters: RX_STATE state
Return: NULL
--------------------------------------------------------------------------------*/
VOID InitRxDataPackage()
{
RegisterUartCallback(UART_DATA_EVENT, CollectingDataPackage);
}
/*--------------------------------------------------------------------------------
Function: CollectingDataPackage
Purpose: Collecting Data package and store in a buffer
Parameters: PVOID buff
Return: NULL
--------------------------------------------------------------------------------*/
VOID CollectingDataPackage(PVOID rxBuff)
{
PBYTE pRxBuff = (PBYTE)rxBuff;
switch(state)
{
case WAIT_START_BYTE:
{
lastRxData = '\0';
nIndex = 0;
if(*pRxBuff == START_BYTE)
{
DataPackageRX.data[nIndex++] = START_BYTE;
lastRxData = *pRxBuff;
state = IN_MESG;
}
break;
}
case IN_MESG:
{
if(*pRxBuff == STOP_BYTE && lastRxData != ESC_BYTE)
{
state = STOP;
break;
}
if(*pRxBuff == START_BYTE && lastRxData != ESC_BYTE)
{
// Restart frame data.
nIndex = 0;
DataPackageRX.data[nIndex++] = *pRxBuff;
lastRxData = *pRxBuff;
break;
}
DataPackageRX.data[nIndex++] = *pRxBuff;
lastRxData = *pRxBuff;
break;
}
case STOP:
{
DataPackageRX.data[nIndex++] = STOP_BYTE;
DataPackageRX.data[nIndex] = '\0';
DataPackageRX.length = nIndex;
//WriteUart(DataPackageRX.data, nIndex);
// Set flag to process frame data
g_flagFrameReceived = 1;
state = WAIT_START_BYTE;
break;
}
}
}
/*--------------------------------------------------------------------------------
Function: isInvalidDataPackage
Purpose: Check the invalid of the data package
Parameters: PDATAPACKAGE
Return: BOOL
--------------------------------------------------------------------------------*/
BOOL IsInvalidDataPackage(PDATAPACKAGE mesg)
{
return((mesg->data[0] == START_BYTE) && (mesg->data[mesg->length -1] == STOP_BYTE));
}
/*--------------------------------------------------------------------------------
Function : EncodeDataPackage
Purpose : Encode data from buff to message (msg)
Parameters : PVOID buff, PVOID mesg
Return : NULL
--------------------------------------------------------------------------------*/
VOID EncodeDatPackage(PVOID Buff, PDATAPACKAGE pMesg)
{
BYTE i = 1; // Index of pMesg, start at 1, pMesg[0] = START_BYTE
PBYTE pBuff = (PBYTE)Buff;
pMesg->data[0] = START_BYTE;
while(*pBuff != '\0')
{
if(*pBuff == START_BYTE || *pBuff == STOP_BYTE || *pBuff == ESC_BYTE)
{
pMesg->data[i++] = ESC_BYTE;
}
pMesg->data[i++] = *pBuff;
pBuff++;
}
pMesg->data[i++] = STOP_BYTE;
pMesg->length = i;
pMesg->data[i] = '\0';
}
/*--------------------------------------------------------------------------------
Function : DecodeDataPackage
Purpose : Decode data from mesg to an array
Parameters : PDATAPACKAGE mesg, PVOID resul
Return : NULL
--------------------------------------------------------------------------------*/
VOID DecodeDataPackage(PDATAPACKAGE mesg, PVOID result)
{
PBYTE pData = (PBYTE)result;
BYTE nIndex = 1; // Start index is 1
for(nIndex = 1; nIndex < mesg->length -1; nIndex++)
{
if(mesg->data[nIndex] == ESC_BYTE)
{
*pData = mesg->data[++nIndex];
}
else
{
*pData = mesg->data[nIndex];
}
pData++;
}
*pData = '\0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment