1 /*!
2 * \file utilities.h
3 *
4 * \brief Helper functions implementation
5 *
6 * \copyright Revised BSD License, see section \ref LICENSE.
7 *
8 * \code
9 * ______ _
10 * / _____) _ | |
11 * ( (____ _____ ____ _| |_ _____ ____| |__
12 * \____ \| ___ | (_ _) ___ |/ ___) _ \
13 * _____) ) ____| | | || |_| ____( (___| | | |
14 * (______/|_____)_|_|_| \__)_____)\____)_| |_|
15 * (C)2013-2017 Semtech
16 *
17 * \endcode
18 *
19 * \author Miguel Luis ( Semtech )
20 *
21 * \author Gregory Cristian ( Semtech )
22 */
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include "utilities.h"
26
27 /*!
28 * Redefinition of rand() and srand() standard C functions.
29 * These functions are redefined in order to get the same behavior across
30 * different compiler toolchains implementations.
31 */
32 // Standard random functions redefinition start
33 #define RAND_LOCAL_MAX 2147483647L
34
35 static uint32_t next = 1;
36
rand1(void)37 int32_t rand1( void )
38 {
39 return ( ( next = next * 1103515245L + 12345L ) % RAND_LOCAL_MAX );
40 }
41
srand1(uint32_t seed)42 void srand1( uint32_t seed )
43 {
44 next = seed;
45 }
46 // Standard random functions redefinition end
47
randr(int32_t min,int32_t max)48 int32_t randr( int32_t min, int32_t max )
49 {
50 return ( int32_t )rand1( ) % ( max - min + 1 ) + min;
51 }
52
memcpy1(uint8_t * dst,const uint8_t * src,uint16_t size)53 void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size )
54 {
55 while( size-- )
56 {
57 *dst++ = *src++;
58 }
59 }
60
memcpyr(uint8_t * dst,const uint8_t * src,uint16_t size)61 void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size )
62 {
63 dst = dst + ( size - 1 );
64 while( size-- )
65 {
66 *dst-- = *src++;
67 }
68 }
69
memset1(uint8_t * dst,uint8_t value,uint16_t size)70 void memset1( uint8_t *dst, uint8_t value, uint16_t size )
71 {
72 while( size-- )
73 {
74 *dst++ = value;
75 }
76 }
77
Nibble2HexChar(uint8_t a)78 int8_t Nibble2HexChar( uint8_t a )
79 {
80 if( a < 10 )
81 {
82 return '0' + a;
83 }
84 else if( a < 16 )
85 {
86 return 'A' + ( a - 10 );
87 }
88 else
89 {
90 return '?';
91 }
92 }
93
Crc32(uint8_t * buffer,uint16_t length)94 uint32_t Crc32( uint8_t *buffer, uint16_t length )
95 {
96 // The CRC calculation follows CCITT - 0x04C11DB7
97 const uint32_t reversedPolynom = 0xEDB88320;
98
99 // CRC initial value
100 uint32_t crc = 0xFFFFFFFF;
101
102 if( buffer == NULL )
103 {
104 return 0;
105 }
106
107 for( uint16_t i = 0; i < length; ++i )
108 {
109 crc ^= ( uint32_t )buffer[i];
110 for( uint16_t i = 0; i < 8; i++ )
111 {
112 crc = ( crc >> 1 ) ^ ( reversedPolynom & ~( ( crc & 0x01 ) - 1 ) );
113 }
114 }
115
116 return ~crc;
117 }
118
Crc32Init(void)119 uint32_t Crc32Init( void )
120 {
121 return 0xFFFFFFFF;
122 }
123
Crc32Update(uint32_t crcInit,uint8_t * buffer,uint16_t length)124 uint32_t Crc32Update( uint32_t crcInit, uint8_t *buffer, uint16_t length )
125 {
126 // The CRC calculation follows CCITT - 0x04C11DB7
127 const uint32_t reversedPolynom = 0xEDB88320;
128
129 // CRC initial value
130 uint32_t crc = crcInit;
131
132 if( buffer == NULL )
133 {
134 return 0;
135 }
136
137 for( uint16_t i = 0; i < length; ++i )
138 {
139 crc ^= ( uint32_t )buffer[i];
140 for( uint16_t i = 0; i < 8; i++ )
141 {
142 crc = ( crc >> 1 ) ^ ( reversedPolynom & ~( ( crc & 0x01 ) - 1 ) );
143 }
144 }
145 return crc;
146 }
147
Crc32Finalize(uint32_t crc)148 uint32_t Crc32Finalize( uint32_t crc )
149 {
150 return ~crc;
151 }
152