-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathutilities.h
More file actions
219 lines (195 loc) · 5.41 KB
/
utilities.h
File metadata and controls
219 lines (195 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*!
* \file utilities.h
*
* \brief Helper functions implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
* ______ _
* / _____) _ | |
* ( (____ _____ ____ _| |_ _____ ____| |__
* \____ \| ___ | (_ _) ___ |/ ___) _ \
* _____) ) ____| | | || |_| ____( (___| | | |
* (______/|_____)_|_|_| \__)_____)\____)_| |_|
* (C)2013-2017 Semtech
*
* \endcode
*
* \author Miguel Luis ( Semtech )
*
* \author Gregory Cristian ( Semtech )
*/
#ifndef __UTILITIES_H__
#define __UTILITIES_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
/*!
* LMN (LoRaMac-node) status
*/
typedef enum LmnStatus_e
{
LMN_STATUS_ERROR = 0,
LMN_STATUS_OK = !LMN_STATUS_ERROR
} LmnStatus_t;
/*!
* \brief Returns the minimum value between a and b
*
* \param [IN] a 1st value
* \param [IN] b 2nd value
* \retval minValue Minimum value
*/
#ifndef MIN
#define MIN( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
#endif
/*!
* \brief Returns the maximum value between a and b
*
* \param [IN] a 1st value
* \param [IN] b 2nd value
* \retval maxValue Maximum value
*/
#ifndef MAX
#define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
#endif
/*!
* \brief Returns 2 raised to the power of n
*
* \param [IN] n power value
* \retval result of raising 2 to the power n
*/
#define POW2( n ) ( 1 << n )
/*!
* Version
*/
typedef union Version_u
{
struct Version_s
{
uint8_t Revision;
uint8_t Patch;
uint8_t Minor;
uint8_t Major;
}Fields;
uint32_t Value;
}Version_t;
/*!
* \brief Initializes the pseudo random generator initial value
*
* \param [IN] seed Pseudo random generator initial value
*/
void srand1( uint32_t seed );
/*!
* \brief Computes a random number between min and max
*
* \param [IN] min range minimum value
* \param [IN] max range maximum value
* \retval random random value in range min..max
*/
int32_t randr( int32_t min, int32_t max );
/*!
* \brief Copies size elements of src array to dst array
*
* \remark STM32 Standard memcpy function only works on pointers that are aligned
*
* \param [OUT] dst Destination array
* \param [IN] src Source array
* \param [IN] size Number of bytes to be copied
*/
void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size );
/*!
* \brief Copies size elements of src array to dst array reversing the byte order
*
* \param [OUT] dst Destination array
* \param [IN] src Source array
* \param [IN] size Number of bytes to be copied
*/
void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size );
/*!
* \brief Set size elements of dst array with value
*
* \remark STM32 Standard memset function only works on pointers that are aligned
*
* \param [OUT] dst Destination array
* \param [IN] value Default value
* \param [IN] size Number of bytes to be copied
*/
void memset1( uint8_t *dst, uint8_t value, uint16_t size );
/*!
* \brief Converts a nibble to an hexadecimal character
*
* \param [IN] a Nibble to be converted
* \retval hexChar Converted hexadecimal character
*/
int8_t Nibble2HexChar( uint8_t a );
/*!
* \brief Computes a CCITT 32 bits CRC
*
* \param [IN] buffer Data buffer used to compute the CRC
* \param [IN] length Data buffer length
*
* \retval crc The computed buffer of length CRC
*/
uint32_t Crc32( uint8_t *buffer, uint16_t length );
/*!
* \brief Computes the initial value of the CCITT 32 bits CRC. This function
* can be used with functions \ref Crc32Update and \ref Crc32Finalize.
*
* \retval crc Initial crc value.
*/
uint32_t Crc32Init( void );
/*!
* \brief Updates the value of the crc value.
*
* \param [IN] crcInit Previous or initial crc value.
* \param [IN] buffer Data pointer.
* \param [IN] length Length of the data.
*
* \retval crc Updated crc value.
*/
uint32_t Crc32Update( uint32_t crcInit, uint8_t *buffer, uint16_t length );
/*!
* \brief Finalizes the crc value after the calls to \ref Crc32Update.
*
* \param [IN] crc Recent crc value.
*
* \retval crc Updated crc value.
*/
uint32_t Crc32Finalize( uint32_t crc );
/*!
* Begins critical section
*/
#define CRITICAL_SECTION_BEGIN( ) uint32_t mask; BoardCriticalSectionBegin( &mask )
/*!
* Begin repeated critical section
*/
#define CRITICAL_SECTION_BEGIN_REPEAT( ) BoardCriticalSectionBegin( &mask )
/*!
* Ends critical section
*/
#define CRITICAL_SECTION_END( ) BoardCriticalSectionEnd( &mask )
/*
* ============================================================================
* Following functions must be implemented inside the specific platform
* board.c file.
* ============================================================================
*/
/*!
* Disable interrupts, begins critical section
*
* \param [IN] mask Pointer to a variable where to store the CPU IRQ mask
*/
void BoardCriticalSectionBegin( uint32_t *mask );
/*!
* Ends critical section
*
* \param [IN] mask Pointer to a variable where the CPU IRQ mask was stored
*/
void BoardCriticalSectionEnd( uint32_t *mask );
#ifdef __cplusplus
}
#endif
#endif // __UTILITIES_H__