Fahad Mirza
/
Nucleo_HXC900
A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.
Utilities/utilities.h@39:cb0e5a76ab15, 2019-01-24 (annotated)
- Committer:
- fahadmirza
- Date:
- Thu Jan 24 23:45:12 2019 +0000
- Revision:
- 39:cb0e5a76ab15
- Parent:
- 32:2d0678039a09
Doxygen style header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fahadmirza | 0:a0c5877bd360 | 1 | /* |
fahadmirza | 0:a0c5877bd360 | 2 | / _____) _ | | |
fahadmirza | 0:a0c5877bd360 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
fahadmirza | 0:a0c5877bd360 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
fahadmirza | 0:a0c5877bd360 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
fahadmirza | 0:a0c5877bd360 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
fahadmirza | 0:a0c5877bd360 | 7 | (C)2013 Semtech |
fahadmirza | 0:a0c5877bd360 | 8 | |
fahadmirza | 0:a0c5877bd360 | 9 | _ _ _____ _______ |
fahadmirza | 0:a0c5877bd360 | 10 | | | | | |_ _| |__ __| |
fahadmirza | 0:a0c5877bd360 | 11 | | |__| | __ ___ __ | | ___ | | |
fahadmirza | 0:a0c5877bd360 | 12 | | __ |/ _` \ \/ / | | / _ \| | |
fahadmirza | 0:a0c5877bd360 | 13 | | | | | (_| |> < _| || (_) | | |
fahadmirza | 0:a0c5877bd360 | 14 | |_| |_|\__,_/_/\_\_____\___/|_| |
fahadmirza | 0:a0c5877bd360 | 15 | (C)2017 HaxIoT |
fahadmirza | 0:a0c5877bd360 | 16 | |
fahadmirza | 0:a0c5877bd360 | 17 | Description: Helper functions implementation |
fahadmirza | 0:a0c5877bd360 | 18 | |
fahadmirza | 0:a0c5877bd360 | 19 | License: Revised BSD License, see LICENSE.TXT file include in the project |
fahadmirza | 0:a0c5877bd360 | 20 | |
fahadmirza | 0:a0c5877bd360 | 21 | Maintainer: Miguel Luis and Gregory Cristian |
fahadmirza | 0:a0c5877bd360 | 22 | Fahad Mirza (Haxiot) |
fahadmirza | 0:a0c5877bd360 | 23 | */ |
fahadmirza | 0:a0c5877bd360 | 24 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 25 | * @file utilities.h |
fahadmirza | 0:a0c5877bd360 | 26 | * @author MCD Application Team |
fahadmirza | 0:a0c5877bd360 | 27 | * @version V1.1.4 |
fahadmirza | 0:a0c5877bd360 | 28 | * @date 08-January-2018 |
fahadmirza | 0:a0c5877bd360 | 29 | * @brief Header for driver utilities.c module |
fahadmirza | 0:a0c5877bd360 | 30 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 31 | * @attention |
fahadmirza | 0:a0c5877bd360 | 32 | * |
fahadmirza | 0:a0c5877bd360 | 33 | * <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V. |
fahadmirza | 0:a0c5877bd360 | 34 | * All rights reserved.</center></h2> |
fahadmirza | 0:a0c5877bd360 | 35 | * |
fahadmirza | 0:a0c5877bd360 | 36 | * Redistribution and use in source and binary forms, with or without |
fahadmirza | 0:a0c5877bd360 | 37 | * modification, are permitted, provided that the following conditions are met: |
fahadmirza | 0:a0c5877bd360 | 38 | * |
fahadmirza | 0:a0c5877bd360 | 39 | * 1. Redistribution of source code must retain the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 40 | * this list of conditions and the following disclaimer. |
fahadmirza | 0:a0c5877bd360 | 41 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 42 | * this list of conditions and the following disclaimer in the documentation |
fahadmirza | 0:a0c5877bd360 | 43 | * and/or other materials provided with the distribution. |
fahadmirza | 0:a0c5877bd360 | 44 | * 3. Neither the name of STMicroelectronics nor the names of other |
fahadmirza | 0:a0c5877bd360 | 45 | * contributors to this software may be used to endorse or promote products |
fahadmirza | 0:a0c5877bd360 | 46 | * derived from this software without specific written permission. |
fahadmirza | 0:a0c5877bd360 | 47 | * 4. This software, including modifications and/or derivative works of this |
fahadmirza | 0:a0c5877bd360 | 48 | * software, must execute solely and exclusively on microcontroller or |
fahadmirza | 0:a0c5877bd360 | 49 | * microprocessor devices manufactured by or for STMicroelectronics. |
fahadmirza | 0:a0c5877bd360 | 50 | * 5. Redistribution and use of this software other than as permitted under |
fahadmirza | 0:a0c5877bd360 | 51 | * this license is void and will automatically terminate your rights under |
fahadmirza | 0:a0c5877bd360 | 52 | * this license. |
fahadmirza | 0:a0c5877bd360 | 53 | * |
fahadmirza | 0:a0c5877bd360 | 54 | * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" |
fahadmirza | 0:a0c5877bd360 | 55 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 56 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
fahadmirza | 0:a0c5877bd360 | 57 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY |
fahadmirza | 0:a0c5877bd360 | 58 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT |
fahadmirza | 0:a0c5877bd360 | 59 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
fahadmirza | 0:a0c5877bd360 | 60 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 61 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
fahadmirza | 0:a0c5877bd360 | 62 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
fahadmirza | 0:a0c5877bd360 | 63 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
fahadmirza | 0:a0c5877bd360 | 64 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
fahadmirza | 0:a0c5877bd360 | 65 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fahadmirza | 0:a0c5877bd360 | 66 | * |
fahadmirza | 0:a0c5877bd360 | 67 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 68 | */ |
fahadmirza | 0:a0c5877bd360 | 69 | #ifndef __UTILITIES_H__ |
fahadmirza | 0:a0c5877bd360 | 70 | #define __UTILITIES_H__ |
fahadmirza | 0:a0c5877bd360 | 71 | |
fahadmirza | 0:a0c5877bd360 | 72 | #include "stm32l0xx_hal.h" |
fahadmirza | 0:a0c5877bd360 | 73 | |
fahadmirza | 0:a0c5877bd360 | 74 | |
fahadmirza | 0:a0c5877bd360 | 75 | /* BACKUP_PRIMASK MUST be implemented at the beginning of the function |
fahadmirza | 0:a0c5877bd360 | 76 | * that implement a critical section. |
fahadmirza | 0:a0c5877bd360 | 77 | * PRIMASK is saved on STACK and recovered at the end of the function. |
fahadmirza | 0:a0c5877bd360 | 78 | * That way RESTORE_PRIMASK ensures that no irq would be triggered in case of |
fahadmirza | 0:a0c5877bd360 | 79 | * unbalanced enable/disable, reentrant code etc. |
fahadmirza | 0:a0c5877bd360 | 80 | */ |
fahadmirza | 0:a0c5877bd360 | 81 | #define BACKUP_PRIMASK() uint32_t primask_bit= __get_PRIMASK() |
fahadmirza | 0:a0c5877bd360 | 82 | #define DISABLE_IRQ() __disable_irq() |
fahadmirza | 0:a0c5877bd360 | 83 | #define ENABLE_IRQ() __enable_irq() |
fahadmirza | 0:a0c5877bd360 | 84 | #define RESTORE_PRIMASK() __set_PRIMASK(primask_bit) |
fahadmirza | 0:a0c5877bd360 | 85 | |
fahadmirza | 0:a0c5877bd360 | 86 | /* prepocessor directive to align buffer*/ |
fahadmirza | 0:a0c5877bd360 | 87 | #define ALIGN(n) __attribute__((aligned(n))) |
fahadmirza | 0:a0c5877bd360 | 88 | |
fahadmirza | 0:a0c5877bd360 | 89 | /*! |
fahadmirza | 0:a0c5877bd360 | 90 | * \brief Returns the minimum value between a and b |
fahadmirza | 0:a0c5877bd360 | 91 | * |
fahadmirza | 0:a0c5877bd360 | 92 | * \param [IN] a 1st value |
fahadmirza | 0:a0c5877bd360 | 93 | * \param [IN] b 2nd value |
fahadmirza | 0:a0c5877bd360 | 94 | * \retval minValue Minimum value |
fahadmirza | 0:a0c5877bd360 | 95 | */ |
fahadmirza | 0:a0c5877bd360 | 96 | #define MIN( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) ) |
fahadmirza | 0:a0c5877bd360 | 97 | |
fahadmirza | 0:a0c5877bd360 | 98 | /*! |
fahadmirza | 0:a0c5877bd360 | 99 | * \brief Returns the maximum value between a and b |
fahadmirza | 0:a0c5877bd360 | 100 | * |
fahadmirza | 0:a0c5877bd360 | 101 | * \param [IN] a 1st value |
fahadmirza | 0:a0c5877bd360 | 102 | * \param [IN] b 2nd value |
fahadmirza | 0:a0c5877bd360 | 103 | * \retval maxValue Maximum value |
fahadmirza | 0:a0c5877bd360 | 104 | */ |
fahadmirza | 0:a0c5877bd360 | 105 | #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) ) |
fahadmirza | 0:a0c5877bd360 | 106 | |
fahadmirza | 0:a0c5877bd360 | 107 | /*! |
fahadmirza | 0:a0c5877bd360 | 108 | * \brief Returns 2 raised to the power of n |
fahadmirza | 0:a0c5877bd360 | 109 | * |
fahadmirza | 0:a0c5877bd360 | 110 | * \param [IN] n power value |
fahadmirza | 0:a0c5877bd360 | 111 | * \retval result of raising 2 to the power n |
fahadmirza | 0:a0c5877bd360 | 112 | */ |
fahadmirza | 0:a0c5877bd360 | 113 | #define POW2( n ) ( 1 << n ) |
fahadmirza | 0:a0c5877bd360 | 114 | |
fahadmirza | 0:a0c5877bd360 | 115 | /*! |
fahadmirza | 0:a0c5877bd360 | 116 | * \brief Find First Set |
fahadmirza | 32:2d0678039a09 | 117 | * This function identifies the least significant index or position of |
fahadmirza | 32:2d0678039a09 | 118 | * the bits set to one in the word |
fahadmirza | 0:a0c5877bd360 | 119 | * |
fahadmirza | 0:a0c5877bd360 | 120 | * \param [in] value Value to find least significant index |
fahadmirza | 0:a0c5877bd360 | 121 | * \retval bitIndex Index of least significat bit at one |
fahadmirza | 0:a0c5877bd360 | 122 | */ |
fahadmirza | 0:a0c5877bd360 | 123 | __STATIC_INLINE uint8_t __ffs( uint32_t value ) |
fahadmirza | 0:a0c5877bd360 | 124 | { |
fahadmirza | 0:a0c5877bd360 | 125 | return( uint32_t )( 32 - __CLZ( value & ( -value ) ) ); |
fahadmirza | 0:a0c5877bd360 | 126 | } |
fahadmirza | 0:a0c5877bd360 | 127 | |
fahadmirza | 0:a0c5877bd360 | 128 | /*! |
fahadmirza | 0:a0c5877bd360 | 129 | * \brief Initializes the pseudo random generator initial value |
fahadmirza | 0:a0c5877bd360 | 130 | * |
fahadmirza | 0:a0c5877bd360 | 131 | * \param [IN] seed Pseudo random generator initial value |
fahadmirza | 0:a0c5877bd360 | 132 | */ |
fahadmirza | 0:a0c5877bd360 | 133 | void srand1( uint32_t seed ); |
fahadmirza | 0:a0c5877bd360 | 134 | |
fahadmirza | 0:a0c5877bd360 | 135 | /*! |
fahadmirza | 0:a0c5877bd360 | 136 | * \brief Computes a random number between min and max |
fahadmirza | 0:a0c5877bd360 | 137 | * |
fahadmirza | 0:a0c5877bd360 | 138 | * \param [IN] min range minimum value |
fahadmirza | 0:a0c5877bd360 | 139 | * \param [IN] max range maximum value |
fahadmirza | 0:a0c5877bd360 | 140 | * \retval random random value in range min..max |
fahadmirza | 0:a0c5877bd360 | 141 | */ |
fahadmirza | 0:a0c5877bd360 | 142 | int32_t randr( int32_t min, int32_t max ); |
fahadmirza | 0:a0c5877bd360 | 143 | |
fahadmirza | 0:a0c5877bd360 | 144 | /*! |
fahadmirza | 0:a0c5877bd360 | 145 | * \brief Copies size elements of src array to dst array |
fahadmirza | 0:a0c5877bd360 | 146 | * |
fahadmirza | 32:2d0678039a09 | 147 | * \remark STM32 Standard memcpy function only works on pointers that are |
fahadmirza | 32:2d0678039a09 | 148 | * aligned |
fahadmirza | 0:a0c5877bd360 | 149 | * |
fahadmirza | 0:a0c5877bd360 | 150 | * \param [OUT] dst Destination array |
fahadmirza | 0:a0c5877bd360 | 151 | * \param [IN] src Source array |
fahadmirza | 0:a0c5877bd360 | 152 | * \param [IN] size Number of bytes to be copied |
fahadmirza | 0:a0c5877bd360 | 153 | */ |
fahadmirza | 0:a0c5877bd360 | 154 | void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size ); |
fahadmirza | 0:a0c5877bd360 | 155 | |
fahadmirza | 0:a0c5877bd360 | 156 | /*! |
fahadmirza | 32:2d0678039a09 | 157 | * \brief Copies size elements of src array to dst array reversing the byte |
fahadmirza | 32:2d0678039a09 | 158 | * order |
fahadmirza | 0:a0c5877bd360 | 159 | * |
fahadmirza | 0:a0c5877bd360 | 160 | * \param [OUT] dst Destination array |
fahadmirza | 0:a0c5877bd360 | 161 | * \param [IN] src Source array |
fahadmirza | 0:a0c5877bd360 | 162 | * \param [IN] size Number of bytes to be copied |
fahadmirza | 0:a0c5877bd360 | 163 | */ |
fahadmirza | 0:a0c5877bd360 | 164 | void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size ); |
fahadmirza | 0:a0c5877bd360 | 165 | |
fahadmirza | 0:a0c5877bd360 | 166 | /*! |
fahadmirza | 0:a0c5877bd360 | 167 | * \brief Set size elements of dst array with value |
fahadmirza | 0:a0c5877bd360 | 168 | * |
fahadmirza | 32:2d0678039a09 | 169 | * \remark STM32 Standard memset function only works on pointers that are |
fahadmirza | 32:2d0678039a09 | 170 | * aligned |
fahadmirza | 0:a0c5877bd360 | 171 | * |
fahadmirza | 0:a0c5877bd360 | 172 | * \param [OUT] dst Destination array |
fahadmirza | 0:a0c5877bd360 | 173 | * \param [IN] value Default value |
fahadmirza | 0:a0c5877bd360 | 174 | * \param [IN] size Number of bytes to be copied |
fahadmirza | 0:a0c5877bd360 | 175 | */ |
fahadmirza | 0:a0c5877bd360 | 176 | void memset1( uint8_t *dst, uint8_t value, uint16_t size ); |
fahadmirza | 0:a0c5877bd360 | 177 | |
fahadmirza | 0:a0c5877bd360 | 178 | /*! |
fahadmirza | 0:a0c5877bd360 | 179 | * \brief Converts a nibble to an hexadecimal character |
fahadmirza | 0:a0c5877bd360 | 180 | * |
fahadmirza | 0:a0c5877bd360 | 181 | * \param [IN] a Nibble to be converted |
fahadmirza | 0:a0c5877bd360 | 182 | * \retval hexChar Converted hexadecimal character |
fahadmirza | 0:a0c5877bd360 | 183 | */ |
fahadmirza | 0:a0c5877bd360 | 184 | int8_t Nibble2HexChar( uint8_t a ); |
fahadmirza | 0:a0c5877bd360 | 185 | |
fahadmirza | 32:2d0678039a09 | 186 | /*! |
fahadmirza | 32:2d0678039a09 | 187 | * \brief Converts an array of character hex digits |
fahadmirza | 32:2d0678039a09 | 188 | * into an array of byte-hex |
fahadmirza | 32:2d0678039a09 | 189 | * |
fahadmirza | 32:2d0678039a09 | 190 | * \param [IN] hexString Hex string to be converted |
fahadmirza | 32:2d0678039a09 | 191 | * [IN] buffer Container to save the conversion |
fahadmirza | 32:2d0678039a09 | 192 | * [IN] bufSize Size of the container |
fahadmirza | 32:2d0678039a09 | 193 | * |
fahadmirza | 32:2d0678039a09 | 194 | * \retval [OUT] uint8_t Size of the buffer after conversion |
fahadmirza | 32:2d0678039a09 | 195 | * |
fahadmirza | 32:2d0678039a09 | 196 | * \note It is assumed that the bufSize won't be larger than 256 |
fahadmirza | 32:2d0678039a09 | 197 | */ |
fahadmirza | 32:2d0678039a09 | 198 | uint8_t stringHexToByteArray(const char *hexString, uint8_t *buffer, uint8_t bufSize); |
fahadmirza | 0:a0c5877bd360 | 199 | |
fahadmirza | 0:a0c5877bd360 | 200 | #endif // __UTILITIES_H__ |
fahadmirza | 0:a0c5877bd360 | 201 |