utils: Add normal CRC32

This commit is contained in:
CTCaer 2020-04-30 03:30:51 +03:00
parent ae1bb909b6
commit 54faa38920
4 changed files with 79 additions and 2 deletions

View file

@ -17,6 +17,7 @@
#include "util.h" #include "util.h"
#include "../gfx/di.h" #include "../gfx/di.h"
#include "../mem/heap.h"
#include "../mem/minerva.h" #include "../mem/minerva.h"
#include "../power/max77620.h" #include "../power/max77620.h"
#include "../rtc/max77620-rtc.h" #include "../rtc/max77620-rtc.h"
@ -81,6 +82,43 @@ void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops)
base[ops[i].off] = ops[i].val; base[ops[i].off] = ops[i].val;
} }
u32 crc32_calc(u32 crc, const u8 *buf, u32 len)
{
const u8 *p, *q;
static u32 *table = NULL;
// Calculate CRC table.
if (!table)
{
table = calloc(256, sizeof(u32));
for (u32 i = 0; i < 256; i++)
{
u32 rem = i;
for (u32 j = 0; j < 8; j++)
{
if (rem & 1)
{
rem >>= 1;
rem ^= 0xedb88320;
}
else
rem >>= 1;
}
table[i] = rem;
}
}
crc = ~crc;
q = buf + len;
for (p = buf; p < q; p++)
{
u8 oct = *p;
crc = (crc >> 8) ^ table[(crc & 0xff) ^ oct];
}
return ~crc;
}
void panic(u32 val) void panic(u32 val)
{ {
// Set panic code. // Set panic code.

View file

@ -71,5 +71,6 @@ void reboot_normal();
void reboot_rcm(); void reboot_rcm();
void power_off(); void power_off();
void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops); void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops);
u32 crc32_calc(u32 crc, const u8 *buf, u32 len);
#endif #endif

View file

@ -17,6 +17,7 @@
#include "util.h" #include "util.h"
#include "../gfx/di.h" #include "../gfx/di.h"
#include "../mem/heap.h"
#include "../mem/minerva.h" #include "../mem/minerva.h"
#include "../power/max77620.h" #include "../power/max77620.h"
#include "../rtc/max77620-rtc.h" #include "../rtc/max77620-rtc.h"
@ -24,13 +25,12 @@
#include "../soc/i2c.h" #include "../soc/i2c.h"
#include "../soc/pmc.h" #include "../soc/pmc.h"
#include "../soc/t210.h" #include "../soc/t210.h"
#include "../storage/nx_sd.h"
#define USE_RTC_TIMER #define USE_RTC_TIMER
extern volatile nyx_storage_t *nyx_str; extern volatile nyx_storage_t *nyx_str;
extern void sd_unmount(bool deinit);
u32 get_tmr_s() u32 get_tmr_s()
{ {
return RTC(APBDEV_RTC_SECONDS); return RTC(APBDEV_RTC_SECONDS);
@ -82,6 +82,43 @@ void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops)
base[ops[i].off] = ops[i].val; base[ops[i].off] = ops[i].val;
} }
u32 crc32_calc(u32 crc, const u8 *buf, u32 len)
{
const u8 *p, *q;
static u32 *table = NULL;
// Calculate CRC table.
if (!table)
{
table = calloc(256, sizeof(u32));
for (u32 i = 0; i < 256; i++)
{
u32 rem = i;
for (u32 j = 0; j < 8; j++)
{
if (rem & 1)
{
rem >>= 1;
rem ^= 0xedb88320;
}
else
rem >>= 1;
}
table[i] = rem;
}
}
crc = ~crc;
q = buf + len;
for (p = buf; p < q; p++)
{
u8 oct = *p;
crc = (crc >> 8) ^ table[(crc & 0xff) ^ oct];
}
return ~crc;
}
void panic(u32 val) void panic(u32 val)
{ {
// Set panic code. // Set panic code.

View file

@ -71,5 +71,6 @@ void reboot_normal();
void reboot_rcm(); void reboot_rcm();
void power_off(); void power_off();
void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops); void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops);
u32 crc32_calc(u32 crc, const u8 *buf, u32 len);
#endif #endif