cun
This commit is contained in:
457
apps/earphone/94_rfid_stc/function.c
Normal file
457
apps/earphone/94_rfid_stc/function.c
Normal file
@ -0,0 +1,457 @@
|
||||
/**
|
||||
************************************* Copyright ******************************
|
||||
* (C) Copyright 2019,TuYW,FMSH,China.
|
||||
* All Rights Reserved
|
||||
* By(Shanghai Fudan MicroeleCardTyperonics Group Company Limited)
|
||||
* https://www.fmsh.com
|
||||
*
|
||||
* FileName : function.c
|
||||
* Version : v1.0
|
||||
* Author : TuYW
|
||||
* Date : 2019-03-19
|
||||
* Description:
|
||||
*******************************************************************************/
|
||||
|
||||
#include "function.h"
|
||||
//#include "flash.h"
|
||||
|
||||
#include "intrins.h"
|
||||
|
||||
#include <stdio.h>
|
||||
/**
|
||||
* @brief Convert Hex 32Bits value into char
|
||||
* @param value: value to convert
|
||||
* @param pbuf: pointer to the buffer
|
||||
* @param len: buffer length
|
||||
* @retval None
|
||||
*/
|
||||
void IntToUnicode( uint32_t value, uint8_t *pbuf, uint8_t len )
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
for ( idx = 0; idx < len; idx++ )
|
||||
{
|
||||
if ( ( (value >> 28) ) < 0xA )
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + 'A' - 10;
|
||||
}
|
||||
|
||||
value = value << 4;
|
||||
|
||||
pbuf[2 * idx + 1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//תַ
|
||||
u8 uitoa( u16 n, u8* s )
|
||||
{
|
||||
u16 i = 0, j;
|
||||
u16 len = 0;
|
||||
u8 tmp[7] = { 0 };
|
||||
|
||||
do
|
||||
{
|
||||
tmp[i++] = n % 10 + '0'; //ȡһ
|
||||
}
|
||||
while ( (n /= 10) > 0 ); //ɾ
|
||||
|
||||
len = i;
|
||||
|
||||
for ( j = 0; j < len; j++, i-- ) //
|
||||
{
|
||||
s[j] = tmp[i - 1];
|
||||
}
|
||||
|
||||
return (len);
|
||||
}
|
||||
|
||||
|
||||
//ƽ
|
||||
u32 power( u8 n )
|
||||
{
|
||||
u8 i;
|
||||
u32 t;
|
||||
|
||||
t = 1;
|
||||
for ( i = 0; i < n; i++ )
|
||||
t *= 2;
|
||||
|
||||
return (t);
|
||||
}
|
||||
|
||||
|
||||
//m^n
|
||||
u32 calpow( u8 m, u8 n )
|
||||
{
|
||||
u32 result = 1;
|
||||
while ( n-- )
|
||||
result *= m;
|
||||
return (result);
|
||||
}
|
||||
|
||||
|
||||
//CRC
|
||||
u16 UpdateCrc( u8 ch, u16 *lpwCrc )
|
||||
{
|
||||
ch = (ch ^ (u8) ( (*lpwCrc) & 0x00FF) );
|
||||
ch = (ch ^ (ch << 4) );
|
||||
*lpwCrc = (*lpwCrc >> 8) ^ ( (u16) ch << 8) ^ ( (u16) ch << 3) ^ ( (u16) ch >> 4);
|
||||
return (*lpwCrc);
|
||||
}
|
||||
|
||||
|
||||
u16 ComputeCrc( const u16 InitCRC, const u8 *Data, const u32 Length )
|
||||
{
|
||||
u8 chBlock;
|
||||
u32 InLength = Length;
|
||||
u16 wCrc = InitCRC;
|
||||
do
|
||||
{
|
||||
chBlock = *Data++;
|
||||
UpdateCrc( chBlock, &wCrc );
|
||||
}
|
||||
while ( --InLength );
|
||||
return (~wCrc);;
|
||||
}
|
||||
|
||||
|
||||
//u16 CalculateIapCrc(void)
|
||||
//{
|
||||
// return ComputeCrc(0xffff,(u8 *)PROGRAM_IAP_BASE,PROGRAM_IAP_LENGTH);
|
||||
//}
|
||||
|
||||
//u16 CalculateAppCrc(void)
|
||||
//{
|
||||
// return ComputeCrc(0xffff,(u8 *)PROGRAM_APP_BASE,PROGRAM_APP_LENGTH);
|
||||
//}
|
||||
|
||||
|
||||
/*********************************************************************************************************
|
||||
** : CalcBCC
|
||||
** : bccУ
|
||||
** :
|
||||
** :
|
||||
** ֵ:
|
||||
*********************************************************************************************************/
|
||||
u8 CalcBCC( u8 *ibuf, u16 ilen )
|
||||
{
|
||||
u8 BCC = 0x00;
|
||||
u16 i = 0;
|
||||
for ( i = 0; i < ilen; i++ )
|
||||
{
|
||||
BCC = BCC ^ ibuf[i];
|
||||
}
|
||||
|
||||
return (BCC);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// C prototype : void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen)
|
||||
// parameter(s): [OUT] pbDest -
|
||||
// [IN] pbSrc - ַ
|
||||
// [IN] nLen - 16ֽ(ַij/2)
|
||||
// return value:
|
||||
// remarks : ַתΪ16
|
||||
*/
|
||||
void StrToHex( BYTE *pbDest, BYTE *pbSrc, int nLen )
|
||||
{
|
||||
char h1, h2;
|
||||
BYTE s1, s2;
|
||||
int i;
|
||||
|
||||
for ( i = 0; i < nLen; i++ )
|
||||
{
|
||||
h1 = pbSrc[2 * i];
|
||||
h2 = pbSrc[2 * i + 1];
|
||||
|
||||
s1 = toupper( h1 ) - 0x30;
|
||||
if ( s1 > 9 )
|
||||
s1 -= 7;
|
||||
|
||||
s2 = toupper( h2 ) - 0x30;
|
||||
if ( s2 > 9 )
|
||||
s2 -= 7;
|
||||
|
||||
pbDest[i] = s1 * 16 + s2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// C prototype : void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen)
|
||||
// parameter(s): [OUT] pbDest - Ŀַ
|
||||
// [IN] pbSrc - 16ʼַ
|
||||
// [IN] nLen - 16ֽ
|
||||
// return value:
|
||||
// remarks : 16תΪַ
|
||||
*/
|
||||
void HexToStr( BYTE *pbDest, BYTE *pbSrc, int nLen )
|
||||
{
|
||||
char ddl, ddh;
|
||||
int i;
|
||||
|
||||
for ( i = 0; i < nLen; i++ )
|
||||
{
|
||||
ddh = 48 + pbSrc[i] / 16;
|
||||
ddl = 48 + pbSrc[i] % 16;
|
||||
if ( ddh > 57 )
|
||||
ddh = ddh + 7;
|
||||
if ( ddl > 57 )
|
||||
ddl = ddl + 7;
|
||||
pbDest[i * 2] = ddh;
|
||||
pbDest[i * 2 + 1] = ddl;
|
||||
}
|
||||
|
||||
pbDest[nLen * 2] = '\0';
|
||||
}
|
||||
|
||||
|
||||
U32 le32p_to_cpu( U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]) );
|
||||
}
|
||||
|
||||
|
||||
U32 be32p_to_cpu( const U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]) );
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_le32p( U8*p, U32 n )
|
||||
{
|
||||
p[3] = (U8) (n >> 24);
|
||||
p[2] = (U8) (n >> 16);
|
||||
p[1] = (U8) (n >> 8);
|
||||
p[0] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_be32p( U8*p, U32 n )
|
||||
{
|
||||
p[0] = (U8) (n >> 24);
|
||||
p[1] = (U8) (n >> 16);
|
||||
p[2] = (U8) (n >> 8);
|
||||
p[3] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
U32 le16p_to_cpu( const U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[1] << 8) | p[0]) );
|
||||
}
|
||||
|
||||
|
||||
U32 be16p_to_cpu( U8*p )
|
||||
{
|
||||
return ( (U32) ( (p[0] << 8) | p[1]) );
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_le16p( U8*p, U32 n )
|
||||
{
|
||||
p[1] = (U8) (n >> 8);
|
||||
p[0] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
void cpu_to_be16p( U8*p, U32 n )
|
||||
{
|
||||
p[0] = (U8) (n >> 8);
|
||||
p[1] = (U8) (n);
|
||||
}
|
||||
|
||||
|
||||
/*ַs转Ӧ*/
|
||||
//int atoi(char s[])
|
||||
//{
|
||||
// int i;
|
||||
// int n = 0;
|
||||
// for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
|
||||
// {
|
||||
// n = 10 * n + (s[i] - '0');
|
||||
// }
|
||||
// return n;
|
||||
//}
|
||||
|
||||
int atoi_b( u8 *s, u32 sl )
|
||||
{
|
||||
int i;
|
||||
int n = 0;
|
||||
for ( i = 0; i < sl; i++ )
|
||||
{
|
||||
n = 10 * n + (s[i] - '0');
|
||||
}
|
||||
return (n);
|
||||
}
|
||||
|
||||
//xtell tolower改成my_tolower
|
||||
/*дĸ转Сдĸ*/
|
||||
int my_tolower( int c )
|
||||
{
|
||||
if ( c >= 'A' && c <= 'Z' )
|
||||
{
|
||||
return (c + 'a' - 'A');
|
||||
}
|
||||
else
|
||||
{
|
||||
return (c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//ʮƵַ转
|
||||
int htoi( char s[] )
|
||||
{
|
||||
int i;
|
||||
int n = 0;
|
||||
if ( s[0] == '0' && (s[1] == 'x' || s[1] == 'X') )
|
||||
{
|
||||
i = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
}
|
||||
for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'); ++i )
|
||||
{
|
||||
if ( my_tolower( s[i] ) > '9' )
|
||||
{
|
||||
n = 16 * n + (10 + my_tolower( s[i] ) - 'a');
|
||||
}
|
||||
else
|
||||
{
|
||||
n = 16 * n + (my_tolower( s[i] ) - '0');
|
||||
}
|
||||
}
|
||||
return (n);
|
||||
}
|
||||
|
||||
|
||||
// itoa (ʾ integer to alphanumeric)ǰ转ַһ
|
||||
char* itoa( long num, char* str, int radix )
|
||||
{
|
||||
char temp; // 移动到函数开头
|
||||
char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //
|
||||
unsigned long unum; //Ҫ转ľֵ,转Ǹ
|
||||
int i = 0, j, k; //iָʾַӦλ转֮iʵַijȣ转˳ģkָʾ˳Ŀʼλ;jָʾ˳ʱĽ
|
||||
|
||||
//ȡҪ转ľֵ
|
||||
if ( radix == 10 && num < 0 ) //Ҫ转ʮǸ
|
||||
{
|
||||
unum = (unsigned long) -num; //numľֵunum
|
||||
str[i++] = '-'; //ַǰΪ'-'ţ1
|
||||
}
|
||||
else
|
||||
unum = (unsigned long) num; //numΪֱӸֵunum
|
||||
|
||||
//转֣ע转
|
||||
do
|
||||
{
|
||||
str[i++] = index[unum % (unsigned long) radix]; //ȡunumһλΪstrӦλָʾ1
|
||||
unum /= radix; //unumȥһλ
|
||||
}
|
||||
while ( unum ); //ֱunumΪ0˳ѭ
|
||||
|
||||
str[i] = '\0'; //ַ'\0'ַcַ'\0'
|
||||
|
||||
//˳
|
||||
if ( str[0] == '-' )
|
||||
k = 1; //ǸŲõӷź濪ʼ
|
||||
else
|
||||
k = 0; //ǸȫҪ
|
||||
|
||||
for ( j = k; j <= (i - 1) / 2; j++ ) //ͷβһһԳƽiʵַijȣֵȳ1
|
||||
{
|
||||
temp = str[j]; //ͷֵʱ
|
||||
str[j] = str[i - 1 + k - j]; //βֵͷ
|
||||
str[i - 1 + k - j] = temp; //ʱֵ(ʵ֮ǰͷֵ)β
|
||||
}
|
||||
|
||||
return (str); //转ַ
|
||||
}
|
||||
|
||||
|
||||
static const char *ALPHA_BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
//base64뺯
|
||||
int base64_encode( const char *buf, const long size, char *base64Char )
|
||||
{
|
||||
int int63 = 0x3F; // 00111111 // 移动到函数开头
|
||||
int int255 = 0xFF; // 11111111 // 移动到函数开头
|
||||
int a = 0;
|
||||
int i = 0;
|
||||
int base64Len = 0;
|
||||
|
||||
while ( i < size )
|
||||
{
|
||||
char b0 = buf[i++];
|
||||
char b1 = (i < size) ? buf[i++] : 0;
|
||||
char b2 = (i < size) ? buf[i++] : 0;
|
||||
|
||||
base64Char[a++] = ALPHA_BASE[(b0 >> 2) & int63];
|
||||
base64Char[a++] = ALPHA_BASE[( (b0 << 4) | ( (b1 & int255) >> 4) ) & int63];
|
||||
base64Char[a++] = ALPHA_BASE[( (b1 << 2) | ( (b2 & int255) >> 6) ) & int63];
|
||||
base64Char[a++] = ALPHA_BASE[b2 & int63];
|
||||
}
|
||||
|
||||
base64Len = a;
|
||||
|
||||
switch ( size % 3 )
|
||||
{
|
||||
case 1:
|
||||
base64Char[--a] = '=';
|
||||
case 2:
|
||||
base64Char[--a] = '=';
|
||||
}
|
||||
|
||||
return (base64Len);
|
||||
}
|
||||
|
||||
|
||||
//base64뺯
|
||||
char *base64_decode( const char *base64Char, const long base64CharSize, char *originChar, long originCharSize )
|
||||
{
|
||||
int toInt[128] = { -1 };
|
||||
int int255 = 0xFF; // 移动到函数开头
|
||||
int index = 0; // 移动到函数开头
|
||||
int c2, c3; // 移动到函数开头
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
for ( i = 0; i < 64; i++ )
|
||||
{
|
||||
toInt[ALPHA_BASE[i]] = i;
|
||||
}
|
||||
for ( j = 0; j < base64CharSize; j += 4 )
|
||||
{
|
||||
int c0 = toInt[base64Char[j]];
|
||||
int c1 = toInt[base64Char[j + 1]];
|
||||
originChar[index++] = ( ( (c0 << 2) | (c1 >> 4) ) & int255);
|
||||
if ( index >= originCharSize )
|
||||
{
|
||||
return (originChar);
|
||||
}
|
||||
c2 = toInt[base64Char[j + 2]];
|
||||
originChar[index++] = ( ( (c1 << 4) | (c2 >> 2) ) & int255);
|
||||
if ( index >= originCharSize )
|
||||
{
|
||||
return (originChar);
|
||||
}
|
||||
c3 = toInt[base64Char[j + 3]];
|
||||
originChar[index++] = ( ( (c2 << 6) | c3) & int255);
|
||||
}
|
||||
return (originChar);
|
||||
}
|
||||
|
||||
|
||||
//建
|
||||
void ClrLongBuf( u8* buf, u32 n )
|
||||
{
|
||||
memset( buf, 0x00, n );
|
||||
}
|
||||
Reference in New Issue
Block a user