formatted code.
This commit is contained in:
+102
-80
@@ -1,19 +1,19 @@
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* Description: CRC implementation file
|
||||
*
|
||||
*
|
||||
* Author: Hao Fan
|
||||
* Date: 2021-11-16 00:50
|
||||
* LastEditors: Hao Fan
|
||||
* LastEditTime: 2022-07-17 20:53
|
||||
* Revisions: Rev 0.0
|
||||
* History:
|
||||
* History:
|
||||
* [2022-07-17]: Add CRC16_0x8005 CRC8_0x31.
|
||||
* [2022-01-06]: Initial verion (Basic CRC-16).
|
||||
*
|
||||
*
|
||||
* Copyright(c) 2021 Beijing Institute of Technology.
|
||||
* Lab of Communication and Networking
|
||||
*
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "util.h"
|
||||
@@ -22,7 +22,8 @@
|
||||
#ifdef __CRC_LSB
|
||||
// CRC-16-CCITT G(X)=X16+X12+X5+1
|
||||
// Reversed
|
||||
static const uint16_t crc16_1021_lsb_lut[256] = {
|
||||
static const uint16_t crc16_1021_lsb_lut[256] =
|
||||
{
|
||||
0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
|
||||
0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
|
||||
0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
|
||||
@@ -58,7 +59,8 @@ static const uint16_t crc16_1021_lsb_lut[256] = {
|
||||
};
|
||||
|
||||
// CRC-16-MAXIM G(X)=X16+X15+X2+1
|
||||
const uint16_t crc16_8005_lsb_lut[256] = {
|
||||
const uint16_t crc16_8005_lsb_lut[256] =
|
||||
{
|
||||
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
|
||||
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
|
||||
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
|
||||
@@ -94,7 +96,8 @@ const uint16_t crc16_8005_lsb_lut[256] = {
|
||||
};
|
||||
|
||||
// CRC-8-MAXIM G(X)=X8+X5+X4+1
|
||||
const uint8_t crc8_31_lsb_lut[256] = {
|
||||
const uint8_t crc8_31_lsb_lut[256] =
|
||||
{
|
||||
0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,
|
||||
0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
|
||||
0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E,
|
||||
@@ -130,7 +133,8 @@ const uint8_t crc8_31_lsb_lut[256] = {
|
||||
};
|
||||
#else
|
||||
// CRC-16-CCITT G(X)=X16+X12+X5+1
|
||||
static const uint16_t crc16_1021_msb_lut[256] = {
|
||||
static const uint16_t crc16_1021_msb_lut[256] =
|
||||
{
|
||||
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
|
||||
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
|
||||
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
|
||||
@@ -166,7 +170,8 @@ static const uint16_t crc16_1021_msb_lut[256] = {
|
||||
};
|
||||
|
||||
// CRC-16-MAXIM G(X)=X16+X15+X2+1
|
||||
const uint16_t crc16_8005_msb_lut[256] = {
|
||||
const uint16_t crc16_8005_msb_lut[256] =
|
||||
{
|
||||
0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
|
||||
0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
|
||||
0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
|
||||
@@ -202,7 +207,8 @@ const uint16_t crc16_8005_msb_lut[256] = {
|
||||
};
|
||||
|
||||
// CRC-8-MAXIM G(X)=X8+X5+X4+1
|
||||
const uint8_t crc8_31_msb_lut[256] = {
|
||||
const uint8_t crc8_31_msb_lut[256] =
|
||||
{
|
||||
0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97,
|
||||
0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
|
||||
0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4,
|
||||
@@ -258,86 +264,91 @@ for(int i = 0; i < sizeof(dataBuff); i++)
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* @brief CRC8 generator
|
||||
*
|
||||
*
|
||||
* @param buffPtr data buffer pointer.
|
||||
* @param dataLen data length
|
||||
* @param poly polynomials of crc
|
||||
* @param crcVal a pointer to store generated crc value
|
||||
*
|
||||
*
|
||||
* @return SUCCESS
|
||||
* FAILURE
|
||||
*
|
||||
*
|
||||
******************************************************************************/
|
||||
int crc8_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint8_t* crcVal) {
|
||||
int crc8_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint8_t *crcVal)
|
||||
{
|
||||
uint8_t crc = CRC8_INIT;
|
||||
const uint8_t* crc_lut = NULL;
|
||||
const uint8_t *crc_lut = NULL;
|
||||
|
||||
switch(poly){
|
||||
switch (poly)
|
||||
{
|
||||
#ifdef __CRC_LSB
|
||||
case 0x31:
|
||||
crc_lut = crc8_31_lsb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
case 0x31:
|
||||
crc_lut = crc8_31_lsb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
#else
|
||||
case 0x31:
|
||||
crc_lut = crc8_31_msb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
case 0x31:
|
||||
crc_lut = crc8_31_msb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
#endif
|
||||
}
|
||||
|
||||
for(int i = 0; i < dataLen; i++){
|
||||
crc = crc_lut[(uint8_t)(crc^buffPtr[i])];
|
||||
|
||||
for (int i = 0; i < dataLen; i++)
|
||||
{
|
||||
crc = crc_lut[(uint8_t)(crc ^ buffPtr[i])];
|
||||
}
|
||||
*crcVal = crc;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* @brief CRC16 generator
|
||||
*
|
||||
*
|
||||
* @param buffPtr data buffer pointer.
|
||||
* @param dataLen data length
|
||||
* @param poly polynomials of crc
|
||||
* @param crcVal a pointer to store generated crc value
|
||||
*
|
||||
*
|
||||
* @return SUCCESS
|
||||
* FAILURE
|
||||
*
|
||||
*
|
||||
******************************************************************************/
|
||||
int crc16_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint16_t* crcVal) {
|
||||
int crc16_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint16_t *crcVal)
|
||||
{
|
||||
uint16_t crc = CRC16_INIT;
|
||||
const uint16_t* crc_lut = NULL;
|
||||
switch(poly){
|
||||
const uint16_t *crc_lut = NULL;
|
||||
switch (poly)
|
||||
{
|
||||
#ifdef __CRC_LSB
|
||||
case 0x8005:
|
||||
crc_lut = crc16_8005_lsb_lut;
|
||||
break;
|
||||
case 0x1021:
|
||||
crc_lut = crc16_1021_lsb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
case 0x8005:
|
||||
crc_lut = crc16_8005_lsb_lut;
|
||||
break;
|
||||
case 0x1021:
|
||||
crc_lut = crc16_1021_lsb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
#else
|
||||
case 0x8005:
|
||||
crc_lut = crc16_8005_msb_lut;
|
||||
break;
|
||||
case 0x1021:
|
||||
crc_lut = crc16_1021_msb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
case 0x8005:
|
||||
crc_lut = crc16_8005_msb_lut;
|
||||
break;
|
||||
case 0x1021:
|
||||
crc_lut = crc16_1021_msb_lut;
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
#endif
|
||||
}
|
||||
for(int i = 0; i < dataLen; i++){
|
||||
for (int i = 0; i < dataLen; i++)
|
||||
{
|
||||
#ifdef __CRC_LSB
|
||||
crc = (crc>>8) ^ crc_lut[(uint8_t)(crc^buffPtr[i])];
|
||||
crc = (crc >> 8) ^ crc_lut[(uint8_t)(crc ^ buffPtr[i])];
|
||||
#else
|
||||
crc = (crc<<8) ^ crc_lut[(uint8_t)((crc >> 8)^buffPtr[i])];
|
||||
crc = (crc << 8) ^ crc_lut[(uint8_t)((crc >> 8) ^ buffPtr[i])];
|
||||
#endif
|
||||
}
|
||||
*crcVal = crc;
|
||||
@@ -347,71 +358,82 @@ int crc16_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint16_t* crcVa
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* @brief CRC8 compare cheker
|
||||
*
|
||||
*
|
||||
* @param buffPtr data buffer pointer.
|
||||
* @param dataLen data length
|
||||
* @param crcOffset byte offset of crc stored position.
|
||||
* @param poly polynomials of crc
|
||||
*
|
||||
*
|
||||
* @return true
|
||||
* false
|
||||
*
|
||||
******************************************************************************/
|
||||
bool crc8_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly) {
|
||||
*
|
||||
******************************************************************************/
|
||||
bool crc8_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly)
|
||||
{
|
||||
int Status = SUCCESS;
|
||||
uint8_t crc_comp;
|
||||
uint8_t crc;
|
||||
if (dataLen == 0) {
|
||||
return false;
|
||||
}
|
||||
if (dataLen == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
crc_comp = (uint8_t)(*(buffPtr + crcOffset));
|
||||
Status = crc8_gen(buffPtr, dataLen, poly, &crc);
|
||||
if(Status == SUCCESS){
|
||||
if (crc == crc_comp) {
|
||||
if (Status == SUCCESS)
|
||||
{
|
||||
if (crc == crc_comp)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* @brief CRC16 compare cheker
|
||||
*
|
||||
*
|
||||
* @param buffPtr data buffer pointer.
|
||||
* @param dataLen data length
|
||||
* @param crcOffset byte offset of crc stored position.
|
||||
* @param poly polynomials of crc
|
||||
*
|
||||
*
|
||||
* @return true
|
||||
* fasle
|
||||
*
|
||||
******************************************************************************/
|
||||
bool crc16_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly) {
|
||||
*
|
||||
******************************************************************************/
|
||||
bool crc16_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly)
|
||||
{
|
||||
int Status = SUCCESS;
|
||||
uint16_t crc_comp;
|
||||
uint16_t crc;
|
||||
if (dataLen == 0) {
|
||||
return false;
|
||||
}
|
||||
if (dataLen == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
crc_comp = (uint16_t)(*(buffPtr + crcOffset));
|
||||
crc_comp |= (uint16_t)((*(buffPtr + crcOffset + 1)) << 8);
|
||||
Status = crc16_gen(buffPtr, dataLen, poly, &crc);
|
||||
if(Status == SUCCESS){
|
||||
if (crc == crc_comp) {
|
||||
if (Status == SUCCESS)
|
||||
{
|
||||
if (crc == crc_comp)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_node_new(void *val) {
|
||||
list_node_new(void *val)
|
||||
{
|
||||
list_node_t *self;
|
||||
if (!(self = LIST_MALLOC(sizeof(list_node_t))))
|
||||
return NULL;
|
||||
@@ -35,7 +36,8 @@ list_node_new(void *val) {
|
||||
*/
|
||||
|
||||
list_t *
|
||||
list_new(void) {
|
||||
list_new(void)
|
||||
{
|
||||
list_t *self;
|
||||
if (!(self = LIST_MALLOC(sizeof(list_t))))
|
||||
return NULL;
|
||||
@@ -49,18 +51,20 @@ list_new(void) {
|
||||
|
||||
/*
|
||||
* Remove all the node in list.
|
||||
* @self: Pointer to the list
|
||||
* @self: Pointer to the list
|
||||
*/
|
||||
|
||||
void
|
||||
list_clear(list_t *self) {
|
||||
void list_clear(list_t *self)
|
||||
{
|
||||
uint32_t len = self->len;
|
||||
list_node_t *next;
|
||||
list_node_t *curr = self->head;
|
||||
|
||||
while (len) {
|
||||
while (len)
|
||||
{
|
||||
next = curr->next;
|
||||
if (self->free) self->free(curr->val);
|
||||
if (self->free)
|
||||
self->free(curr->val);
|
||||
LIST_FREE(curr);
|
||||
curr = next;
|
||||
len--;
|
||||
@@ -69,11 +73,11 @@ list_clear(list_t *self) {
|
||||
|
||||
/*
|
||||
* Free the list.
|
||||
* @self: Pointer to the list
|
||||
* @self: Pointer to the list
|
||||
*/
|
||||
|
||||
void
|
||||
list_destroy(list_t *self) {
|
||||
void list_destroy(list_t *self)
|
||||
{
|
||||
list_clear(self);
|
||||
LIST_FREE(self);
|
||||
}
|
||||
@@ -86,15 +90,20 @@ list_destroy(list_t *self) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_rpush(list_t *self, list_node_t *node) {
|
||||
if (!node) return NULL;
|
||||
list_rpush(list_t *self, list_node_t *node)
|
||||
{
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
if (self->len) {
|
||||
if (self->len)
|
||||
{
|
||||
node->prev = self->tail;
|
||||
node->next = NULL;
|
||||
self->tail->next = node;
|
||||
self->tail = node;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->head = self->tail = node;
|
||||
node->prev = node->next = NULL;
|
||||
}
|
||||
@@ -109,14 +118,19 @@ list_rpush(list_t *self, list_node_t *node) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_rpop(list_t *self) {
|
||||
if (!self->len) return NULL;
|
||||
list_rpop(list_t *self)
|
||||
{
|
||||
if (!self->len)
|
||||
return NULL;
|
||||
|
||||
list_node_t *node = self->tail;
|
||||
|
||||
if (--self->len) {
|
||||
if (--self->len)
|
||||
{
|
||||
(self->tail = node->prev)->next = NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->tail = self->head = NULL;
|
||||
}
|
||||
|
||||
@@ -130,14 +144,19 @@ list_rpop(list_t *self) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_lpop(list_t *self) {
|
||||
if (!self->len) return NULL;
|
||||
list_lpop(list_t *self)
|
||||
{
|
||||
if (!self->len)
|
||||
return NULL;
|
||||
|
||||
list_node_t *node = self->head;
|
||||
|
||||
if (--self->len) {
|
||||
if (--self->len)
|
||||
{
|
||||
(self->head = node->next)->prev = NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->head = self->tail = NULL;
|
||||
}
|
||||
|
||||
@@ -153,15 +172,20 @@ list_lpop(list_t *self) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_lpush(list_t *self, list_node_t *node) {
|
||||
if (!node) return NULL;
|
||||
list_lpush(list_t *self, list_node_t *node)
|
||||
{
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
if (self->len) {
|
||||
if (self->len)
|
||||
{
|
||||
node->next = self->head;
|
||||
node->prev = NULL;
|
||||
self->head->prev = node;
|
||||
self->head = node;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->head = self->tail = node;
|
||||
node->prev = node->next = NULL;
|
||||
}
|
||||
@@ -173,22 +197,29 @@ list_lpush(list_t *self, list_node_t *node) {
|
||||
/*
|
||||
* Return the node associated to val or NULL.
|
||||
* @self: Pointer to the list for finding given value
|
||||
* @val: Value to find
|
||||
* @val: Value to find
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_find(list_t *self, void *val) {
|
||||
list_find(list_t *self, void *val)
|
||||
{
|
||||
list_iterator_t *it = list_iterator_new(self, LIST_HEAD);
|
||||
list_node_t *node;
|
||||
|
||||
while ((node = list_iterator_next(it))) {
|
||||
if (self->compare) {
|
||||
if (self->compare(val, node->val) == 0) {
|
||||
while ((node = list_iterator_next(it)))
|
||||
{
|
||||
if (self->compare)
|
||||
{
|
||||
if (self->compare(val, node->val) == 0)
|
||||
{
|
||||
list_iterator_destroy(it);
|
||||
return node;
|
||||
}
|
||||
} else {
|
||||
if (val == node->val) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (val == node->val)
|
||||
{
|
||||
list_iterator_destroy(it);
|
||||
return node;
|
||||
}
|
||||
@@ -201,23 +232,27 @@ list_find(list_t *self, void *val) {
|
||||
|
||||
/*
|
||||
* Return the node at the given index or NULL.
|
||||
* @self: Pointer to the list for finding given index
|
||||
* @self: Pointer to the list for finding given index
|
||||
* @index: the index of node in the list
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_at(list_t *self, int index) {
|
||||
list_at(list_t *self, int index)
|
||||
{
|
||||
list_direction_t direction = LIST_HEAD;
|
||||
|
||||
if (index < 0) {
|
||||
if (index < 0)
|
||||
{
|
||||
direction = LIST_TAIL;
|
||||
index = ~index;
|
||||
}
|
||||
|
||||
if ((unsigned)index < self->len) {
|
||||
if ((unsigned)index < self->len)
|
||||
{
|
||||
list_iterator_t *it = list_iterator_new(self, direction);
|
||||
list_node_t *node = list_iterator_next(it);
|
||||
while (index--) node = list_iterator_next(it);
|
||||
while (index--)
|
||||
node = list_iterator_next(it);
|
||||
list_iterator_destroy(it);
|
||||
return node;
|
||||
}
|
||||
@@ -227,21 +262,22 @@ list_at(list_t *self, int index) {
|
||||
|
||||
/*
|
||||
* Remove the given node from the list, freeing it and it's value.
|
||||
* @self: Pointer to the list to delete a node
|
||||
* @self: Pointer to the list to delete a node
|
||||
* @node: Pointer the node to be deleted
|
||||
*/
|
||||
|
||||
void
|
||||
list_remove(list_t *self, list_node_t *node) {
|
||||
void list_remove(list_t *self, list_node_t *node)
|
||||
{
|
||||
node->prev
|
||||
? (node->prev->next = node->next)
|
||||
: (self->head = node->next);
|
||||
? (node->prev->next = node->next)
|
||||
: (self->head = node->next);
|
||||
|
||||
node->next
|
||||
? (node->next->prev = node->prev)
|
||||
: (self->tail = node->prev);
|
||||
? (node->next->prev = node->prev)
|
||||
: (self->tail = node->prev);
|
||||
|
||||
if (self->free) self->free(node->val);
|
||||
if (self->free)
|
||||
self->free(node->val);
|
||||
|
||||
LIST_FREE(node);
|
||||
--self->len;
|
||||
@@ -253,10 +289,11 @@ list_remove(list_t *self, list_node_t *node) {
|
||||
*/
|
||||
|
||||
list_iterator_t *
|
||||
list_iterator_new(list_t *list, list_direction_t direction) {
|
||||
list_iterator_new(list_t *list, list_direction_t direction)
|
||||
{
|
||||
list_node_t *node = direction == LIST_HEAD
|
||||
? list->head
|
||||
: list->tail;
|
||||
? list->head
|
||||
: list->tail;
|
||||
return list_iterator_new_from_node(node, direction);
|
||||
}
|
||||
|
||||
@@ -266,7 +303,8 @@ list_iterator_new(list_t *list, list_direction_t direction) {
|
||||
*/
|
||||
|
||||
list_iterator_t *
|
||||
list_iterator_new_from_node(list_node_t *node, list_direction_t direction) {
|
||||
list_iterator_new_from_node(list_node_t *node, list_direction_t direction)
|
||||
{
|
||||
list_iterator_t *self;
|
||||
if (!(self = LIST_MALLOC(sizeof(list_iterator_t))))
|
||||
return NULL;
|
||||
@@ -281,12 +319,14 @@ list_iterator_new_from_node(list_node_t *node, list_direction_t direction) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_iterator_next(list_iterator_t *self) {
|
||||
list_iterator_next(list_iterator_t *self)
|
||||
{
|
||||
list_node_t *curr = self->next;
|
||||
if (curr) {
|
||||
if (curr)
|
||||
{
|
||||
self->next = self->direction == LIST_HEAD
|
||||
? curr->next
|
||||
: curr->prev;
|
||||
? curr->next
|
||||
: curr->prev;
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
@@ -295,8 +335,8 @@ list_iterator_next(list_iterator_t *self) {
|
||||
* Free the list iterator.
|
||||
*/
|
||||
|
||||
void
|
||||
list_iterator_destroy(list_iterator_t *self) {
|
||||
void list_iterator_destroy(list_iterator_t *self)
|
||||
{
|
||||
LIST_FREE(self);
|
||||
self = NULL;
|
||||
}
|
||||
|
||||
@@ -9,102 +9,107 @@
|
||||
#define __LIST_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* list_t iterator direction.
|
||||
*/
|
||||
/*
|
||||
* list_t iterator direction.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
LIST_HEAD,
|
||||
LIST_TAIL
|
||||
} list_direction_t;
|
||||
typedef enum
|
||||
{
|
||||
LIST_HEAD,
|
||||
LIST_TAIL
|
||||
} list_direction_t;
|
||||
|
||||
/*
|
||||
* list_t node struct.
|
||||
*/
|
||||
/*
|
||||
* list_t node struct.
|
||||
*/
|
||||
|
||||
typedef struct list_node {
|
||||
struct list_node *prev;
|
||||
struct list_node *next;
|
||||
void *val;
|
||||
} list_node_t;
|
||||
typedef struct list_node
|
||||
{
|
||||
struct list_node *prev;
|
||||
struct list_node *next;
|
||||
void *val;
|
||||
} list_node_t;
|
||||
|
||||
/*
|
||||
* list_t struct.
|
||||
*/
|
||||
/*
|
||||
* list_t struct.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
list_node_t *head;
|
||||
list_node_t *tail;
|
||||
uint32_t len;
|
||||
void (*free)(void *val);
|
||||
int8_t (*compare)(void *a, void *b);
|
||||
} list_t;
|
||||
typedef struct
|
||||
{
|
||||
list_node_t *head;
|
||||
list_node_t *tail;
|
||||
uint32_t len;
|
||||
void (*free)(void *val);
|
||||
int8_t (*compare)(void *a, void *b);
|
||||
} list_t;
|
||||
|
||||
/*
|
||||
* list_t iterator struct.
|
||||
*/
|
||||
/*
|
||||
* list_t iterator struct.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
list_node_t *next;
|
||||
list_direction_t direction;
|
||||
} list_iterator_t;
|
||||
typedef struct
|
||||
{
|
||||
list_node_t *next;
|
||||
list_direction_t direction;
|
||||
} list_iterator_t;
|
||||
|
||||
// Node prototypes.
|
||||
// Node prototypes.
|
||||
|
||||
list_node_t *
|
||||
list_node_new(void *val);
|
||||
list_node_t *
|
||||
list_node_new(void *val);
|
||||
|
||||
// list_t prototypes.
|
||||
// list_t prototypes.
|
||||
|
||||
list_t *
|
||||
list_new(void);
|
||||
list_t *
|
||||
list_new(void);
|
||||
|
||||
list_node_t *
|
||||
list_rpush(list_t *self, list_node_t *node);
|
||||
list_node_t *
|
||||
list_rpush(list_t *self, list_node_t *node);
|
||||
|
||||
list_node_t *
|
||||
list_lpush(list_t *self, list_node_t *node);
|
||||
list_node_t *
|
||||
list_lpush(list_t *self, list_node_t *node);
|
||||
|
||||
list_node_t *
|
||||
list_find(list_t *self, void *val);
|
||||
list_node_t *
|
||||
list_find(list_t *self, void *val);
|
||||
|
||||
list_node_t *
|
||||
list_at(list_t *self, int index);
|
||||
list_node_t *
|
||||
list_at(list_t *self, int index);
|
||||
|
||||
list_node_t *
|
||||
list_rpop(list_t *self);
|
||||
list_node_t *
|
||||
list_rpop(list_t *self);
|
||||
|
||||
list_node_t *
|
||||
list_lpop(list_t *self);
|
||||
list_node_t *
|
||||
list_lpop(list_t *self);
|
||||
|
||||
void
|
||||
list_remove(list_t *self, list_node_t *node);
|
||||
void
|
||||
list_remove(list_t *self, list_node_t *node);
|
||||
|
||||
void
|
||||
list_clear(list_t *self);
|
||||
void
|
||||
list_clear(list_t *self);
|
||||
|
||||
void
|
||||
list_destroy(list_t *self);
|
||||
void
|
||||
list_destroy(list_t *self);
|
||||
|
||||
// list_t iterator prototypes.
|
||||
// list_t iterator prototypes.
|
||||
|
||||
list_iterator_t *
|
||||
list_iterator_new(list_t *list, list_direction_t direction);
|
||||
list_iterator_t *
|
||||
list_iterator_new(list_t *list, list_direction_t direction);
|
||||
|
||||
list_iterator_t *
|
||||
list_iterator_new_from_node(list_node_t *node, list_direction_t direction);
|
||||
list_iterator_t *
|
||||
list_iterator_new_from_node(list_node_t *node, list_direction_t direction);
|
||||
|
||||
list_node_t *
|
||||
list_iterator_next(list_iterator_t *self);
|
||||
list_node_t *
|
||||
list_iterator_next(list_iterator_t *self);
|
||||
|
||||
void
|
||||
list_iterator_destroy(list_iterator_t *self);
|
||||
void
|
||||
list_iterator_destroy(list_iterator_t *self);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
/************************** Functions Implementation **************************/
|
||||
/******************************************************************************/
|
||||
#ifdef __BITSWAP_TABLE
|
||||
static uint8_t byte_flipLR[256] = {
|
||||
static uint8_t byte_flipLR[256] =
|
||||
{
|
||||
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
|
||||
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
|
||||
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
|
||||
@@ -47,16 +48,19 @@ static uint8_t byte_flipLR[256] = {
|
||||
};
|
||||
#endif
|
||||
|
||||
inline void IO_Out32(uintptr_t addr, uint32_t value){
|
||||
inline void IO_Out32(uintptr_t addr, uint32_t value)
|
||||
{
|
||||
volatile uint32_t *localAddr = (volatile uint32_t *)addr;
|
||||
*localAddr = value;
|
||||
}
|
||||
|
||||
inline uint32_t IO_In32(uintptr_t addr){
|
||||
return *(volatile uint32_t*) addr;
|
||||
inline uint32_t IO_In32(uintptr_t addr)
|
||||
{
|
||||
return *(volatile uint32_t *)addr;
|
||||
}
|
||||
|
||||
uint8_t byte_bit_swap(uint8_t byte){
|
||||
uint8_t byte_bit_swap(uint8_t byte)
|
||||
{
|
||||
#ifdef __BITSWAP_TABLE
|
||||
return byte_flipLR[byte];
|
||||
#else
|
||||
@@ -64,38 +68,42 @@ uint8_t byte_bit_swap(uint8_t byte){
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t word_bit_swap(uint32_t word){
|
||||
uint32_t word_bit_swap(uint32_t word)
|
||||
{
|
||||
#ifdef __BITSWAP_TABLE
|
||||
uint32_t rv_word = (byte_flipLR[word & 0xff] << 24) |
|
||||
(byte_flipLR[(word >> 8) & 0xff] << 16) |
|
||||
(byte_flipLR[(word >> 16) & 0xff] << 8) |
|
||||
(byte_flipLR[(word >> 24) & 0xff]);
|
||||
(byte_flipLR[(word >> 8) & 0xff] << 16) |
|
||||
(byte_flipLR[(word >> 16) & 0xff] << 8) |
|
||||
(byte_flipLR[(word >> 24) & 0xff]);
|
||||
return rv_word;
|
||||
#else
|
||||
word = (((word & 0xaaaaaaaa) >> 1) | ((word & 0x55555555) << 1));
|
||||
word = (((word & 0xcccccccc) >> 2) | ((word & 0x33333333) << 2));
|
||||
word = (((word & 0xf0f0f0f0) >> 4) | ((word & 0x0f0f0f0f) << 4));
|
||||
word = (((word & 0xff00ff00) >> 8) | ((word & 0x00ff00ff) << 8));
|
||||
return((word >> 16) | (word << 16));
|
||||
return ((word >> 16) | (word << 16));
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t count_ones(uint32_t word){
|
||||
uint32_t count_ones(uint32_t word)
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
return __builtin_popcount(word);
|
||||
#else
|
||||
uint32_t one_count = 0;
|
||||
while (word) {
|
||||
while (word)
|
||||
{
|
||||
if (word & 0x00000001)
|
||||
one_count ++;
|
||||
one_count++;
|
||||
word >>= 1;
|
||||
}
|
||||
return one_count;
|
||||
#endif
|
||||
}
|
||||
|
||||
double fix2double(int32_t val, uint8_t frac_bits){
|
||||
return (((double)val)/(BIT(frac_bits)));
|
||||
double fix2double(int32_t val, uint8_t frac_bits)
|
||||
{
|
||||
return (((double)val) / (BIT(frac_bits)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,20 +112,23 @@ double fix2double(int32_t val, uint8_t frac_bits){
|
||||
uint32_t find_first_set_bit(uint32_t word)
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
if(word != 0){
|
||||
if (word != 0)
|
||||
{
|
||||
return __builtin_ctz(word);
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
#else
|
||||
uint32_t first_set_bit = 0;
|
||||
|
||||
while (word) {
|
||||
while (word)
|
||||
{
|
||||
if (word & 0x1)
|
||||
return first_set_bit;
|
||||
word >>= 1;
|
||||
first_set_bit ++;
|
||||
first_set_bit++;
|
||||
}
|
||||
|
||||
return 32;
|
||||
@@ -130,21 +141,24 @@ uint32_t find_first_set_bit(uint32_t word)
|
||||
uint32_t find_last_set_bit(uint32_t word)
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
if(word != 0){
|
||||
return (31-__builtin_clz(word));
|
||||
if (word != 0)
|
||||
{
|
||||
return (31 - __builtin_clz(word));
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
#else
|
||||
uint32_t bit = 0;
|
||||
uint32_t last_set_bit = 32;
|
||||
|
||||
while (word) {
|
||||
while (word)
|
||||
{
|
||||
if (word & 0x1)
|
||||
last_set_bit = bit;
|
||||
word >>= 1;
|
||||
bit ++;
|
||||
bit++;
|
||||
}
|
||||
|
||||
return last_set_bit;
|
||||
@@ -155,15 +169,17 @@ uint32_t find_last_set_bit(uint32_t word)
|
||||
* Locate the closest element in an array.
|
||||
*/
|
||||
uint32_t find_closest(int32_t val,
|
||||
const int32_t *array,
|
||||
uint32_t size)
|
||||
const int32_t *array,
|
||||
uint32_t size)
|
||||
{
|
||||
int32_t diff = abs(array[0] - val);
|
||||
uint32_t ret = 0;
|
||||
uint32_t i;
|
||||
|
||||
for (i = 1; i < size; i++) {
|
||||
if (abs(array[i] - val) < diff) {
|
||||
for (i = 1; i < size; i++)
|
||||
{
|
||||
if (abs(array[i] - val) < diff)
|
||||
{
|
||||
diff = abs(array[i] - val);
|
||||
ret = i;
|
||||
}
|
||||
@@ -200,7 +216,7 @@ int32_t log_base_2(uint32_t x)
|
||||
* Find greatest common divisor of the given two numbers.
|
||||
*/
|
||||
uint32_t greatest_common_divisor(uint32_t a,
|
||||
uint32_t b)
|
||||
uint32_t b)
|
||||
{
|
||||
uint32_t div;
|
||||
uint32_t common_div = 1;
|
||||
@@ -219,11 +235,11 @@ uint32_t greatest_common_divisor(uint32_t a,
|
||||
* Calculate best rational approximation for a given fraction.
|
||||
*/
|
||||
void rational_best_approximation(uint32_t given_numerator,
|
||||
uint32_t given_denominator,
|
||||
uint32_t max_numerator,
|
||||
uint32_t max_denominator,
|
||||
uint32_t *best_numerator,
|
||||
uint32_t *best_denominator)
|
||||
uint32_t given_denominator,
|
||||
uint32_t max_numerator,
|
||||
uint32_t max_denominator,
|
||||
uint32_t *best_numerator,
|
||||
uint32_t *best_denominator)
|
||||
{
|
||||
uint32_t gcd;
|
||||
|
||||
@@ -233,7 +249,8 @@ void rational_best_approximation(uint32_t given_numerator,
|
||||
*best_denominator = given_denominator / gcd;
|
||||
|
||||
if ((*best_numerator > max_numerator) ||
|
||||
(*best_denominator > max_denominator)) {
|
||||
(*best_denominator > max_denominator))
|
||||
{
|
||||
*best_numerator = 0;
|
||||
*best_denominator = 0;
|
||||
}
|
||||
@@ -246,7 +263,8 @@ uint32_t hweight8(uint32_t word)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
|
||||
while (word) {
|
||||
while (word)
|
||||
{
|
||||
if (word & 0x1)
|
||||
count++;
|
||||
word >>= 1;
|
||||
@@ -258,8 +276,8 @@ uint32_t hweight8(uint32_t word)
|
||||
/**
|
||||
* Calculate the quotient and the remainder of an integer division.
|
||||
*/
|
||||
uint64_t do_div(uint64_t* n,
|
||||
uint64_t base)
|
||||
uint64_t do_div(uint64_t *n,
|
||||
uint64_t base)
|
||||
{
|
||||
uint64_t mod = 0;
|
||||
|
||||
@@ -348,4 +366,3 @@ uint32_t srt_to_uint32(const char *str)
|
||||
else
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/***************************************************************************//**
|
||||
/***************************************************************************
|
||||
//**
|
||||
* @file util.h
|
||||
* @brief Implementation of utility functions.
|
||||
* @author DBogdan ([email protected])
|
||||
@@ -48,19 +49,19 @@
|
||||
/******************************************************************************/
|
||||
/********************** Macros and Constants Definitions **********************/
|
||||
/******************************************************************************/
|
||||
//#define __CRC_LSB
|
||||
// #define __CRC_LSB
|
||||
#define __BITSWAP_TABLE
|
||||
#define __CRC_LSB
|
||||
#define CRC16_INIT 0x0000
|
||||
#define CRC8_INIT 0x00
|
||||
|
||||
#define BIT(x) (1 << (x))
|
||||
#define BIT(x) (1 << (x))
|
||||
|
||||
#define ARRAY_SIZE(x) \
|
||||
(sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
#define DIV_ROUND_UP(x,y) \
|
||||
(((x) + (y) - 1) / (y))
|
||||
#define DIV_ROUND_UP(x, y) \
|
||||
(((x) + (y)-1) / (y))
|
||||
#define DIV_ROUND_CLOSEST(x, y) \
|
||||
(((x) + (y) / 2) / (y))
|
||||
#define DIV_ROUND_CLOSEST_ULL(x, y) \
|
||||
@@ -69,52 +70,56 @@
|
||||
#define min(x, y) \
|
||||
(((x) < (y)) ? (x) : (y))
|
||||
#define min_t(type, x, y) \
|
||||
(type)min((type)(x), (type)(y))
|
||||
(type) min((type)(x), (type)(y))
|
||||
|
||||
#define max(x, y) \
|
||||
(((x) > (y)) ? (x) : (y))
|
||||
#define max_t(type, x, y) \
|
||||
(type)max((type)(x), (type)(y))
|
||||
(type) max((type)(x), (type)(y))
|
||||
|
||||
#define clamp(val, min_val, max_val) \
|
||||
(max(min((val), (max_val)), (min_val)))
|
||||
#define clamp_t(type, val, min_val, max_val) \
|
||||
(type)clamp((type)(val), (type)(min_val), (type)(max_val))
|
||||
(type) clamp((type)(val), (type)(min_val), (type)(max_val))
|
||||
|
||||
#define swap(x, y) \
|
||||
{typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;}
|
||||
#define swap(x, y) \
|
||||
{ \
|
||||
typeof(x) _tmp_ = (x); \
|
||||
(x) = (y); \
|
||||
(y) = _tmp_; \
|
||||
}
|
||||
|
||||
#define round_up(x,y) \
|
||||
(((x)+(y)-1)/(y))
|
||||
#define round_up(x, y) \
|
||||
(((x) + (y)-1) / (y))
|
||||
|
||||
#define BITS_PER_LONG 32
|
||||
|
||||
#define GENMASK(h, l) ({ \
|
||||
uint32_t t = (uint32_t)(~0UL); \
|
||||
t = t << (BITS_PER_LONG - (h - l + 1)); \
|
||||
t = t >> (BITS_PER_LONG - (h + 1)); \
|
||||
t; \
|
||||
#define GENMASK(h, l) ({ \
|
||||
uint32_t t = (uint32_t)(~0UL); \
|
||||
t = t << (BITS_PER_LONG - (h - l + 1)); \
|
||||
t = t >> (BITS_PER_LONG - (h + 1)); \
|
||||
t; \
|
||||
})
|
||||
|
||||
#define bswap_constant_32(x) \
|
||||
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
|
||||
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
|
||||
#define bswap_constant_32(x) \
|
||||
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
|
||||
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
|
||||
|
||||
#define bswap_constant_16(x) ((((x) & (uint16_t)0xff00) >> 8) | \
|
||||
(((x) & (uint16_t)0x00ff) << 8))
|
||||
(((x) & (uint16_t)0x00ff) << 8))
|
||||
|
||||
#define bit_swap_constant_8(x) \
|
||||
((((x) & 0x80) >> 7) | \
|
||||
(((x) & 0x40) >> 5) | \
|
||||
(((x) & 0x20) >> 3) | \
|
||||
(((x) & 0x10) >> 1) | \
|
||||
(((x) & 0x08) << 1) | \
|
||||
(((x) & 0x04) << 3) | \
|
||||
(((x) & 0x02) << 5) | \
|
||||
((((x) & 0x80) >> 7) | \
|
||||
(((x) & 0x40) >> 5) | \
|
||||
(((x) & 0x20) >> 3) | \
|
||||
(((x) & 0x10) >> 1) | \
|
||||
(((x) & 0x08) << 1) | \
|
||||
(((x) & 0x04) << 3) | \
|
||||
(((x) & 0x02) << 5) | \
|
||||
(((x) & 0x01) << 7))
|
||||
|
||||
#define U16_MAX ((uint16_t)~0U)
|
||||
#define S16_MAX ((int16_t)(U16_MAX>>1))
|
||||
#define U16_MAX ((uint16_t)~0U)
|
||||
#define S16_MAX ((int16_t)(U16_MAX >> 1))
|
||||
|
||||
#define DIV_U64(x, y) (x / y)
|
||||
|
||||
@@ -147,8 +152,8 @@ uint32_t find_first_set_bit(uint32_t word);
|
||||
uint32_t find_last_set_bit(uint32_t word);
|
||||
/* Locate the closest element in an array. */
|
||||
uint32_t find_closest(int32_t val,
|
||||
const int32_t *array,
|
||||
uint32_t size);
|
||||
const int32_t *array,
|
||||
uint32_t size);
|
||||
/* Shift the value and apply the specified mask. */
|
||||
uint32_t field_prep(uint32_t mask, uint32_t val);
|
||||
/* Get a field specified by a mask from a word. */
|
||||
@@ -157,22 +162,22 @@ uint32_t field_get(uint32_t mask, uint32_t word);
|
||||
int32_t log_base_2(uint32_t x);
|
||||
/* Find greatest common divisor of the given two numbers. */
|
||||
uint32_t greatest_common_divisor(uint32_t a,
|
||||
uint32_t b);
|
||||
uint32_t b);
|
||||
/* Calculate best rational approximation for a given fraction. */
|
||||
void rational_best_approximation(uint32_t given_numerator,
|
||||
uint32_t given_denominator,
|
||||
uint32_t max_numerator,
|
||||
uint32_t max_denominator,
|
||||
uint32_t *best_numerator,
|
||||
uint32_t *best_denominator);
|
||||
uint32_t given_denominator,
|
||||
uint32_t max_numerator,
|
||||
uint32_t max_denominator,
|
||||
uint32_t *best_numerator,
|
||||
uint32_t *best_denominator);
|
||||
/* Calculate the number of set bits. */
|
||||
uint32_t hweight8(uint32_t word);
|
||||
/* Calculate the quotient and the remainder of an integer division. */
|
||||
uint64_t do_div(uint64_t* n,
|
||||
uint64_t base);
|
||||
uint64_t do_div(uint64_t *n,
|
||||
uint64_t base);
|
||||
/* Unsigned 64bit divide with 64bit divisor and remainder */
|
||||
uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor,
|
||||
uint64_t *remainder);
|
||||
uint64_t *remainder);
|
||||
/* Unsigned 64bit divide with 32bit divisor with remainder */
|
||||
uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder);
|
||||
int64_t div_s64_rem(int64_t dividend, int32_t divisor, int32_t *remainder);
|
||||
@@ -184,9 +189,8 @@ int32_t str_to_int32(const char *str);
|
||||
/* Converts from string to uint32_t */
|
||||
uint32_t srt_to_uint32(const char *str);
|
||||
|
||||
int crc16_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint16_t* crcVal);
|
||||
bool crc16_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly);
|
||||
int crc8_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint8_t* crcVal);
|
||||
bool crc8_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly);
|
||||
int crc16_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint16_t *crcVal);
|
||||
bool crc16_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly);
|
||||
int crc8_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint8_t *crcVal);
|
||||
bool crc8_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly);
|
||||
#endif // __UTIL_H_
|
||||
|
||||
|
||||
Reference in New Issue
Block a user