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