A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.

Dependencies:   mbed

Committer:
fahadmirza
Date:
Mon Jul 16 20:12:42 2018 +0000
Revision:
5:53302861bfea
Parent:
utilities.h@0:a0c5877bd360
Child:
32:2d0678039a09
Updated directories;

Who changed what in which revision?

UserRevisionLine numberNew 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>&copy; 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 0:a0c5877bd360 117 * This function identifies the least significant index or position of the
fahadmirza 0:a0c5877bd360 118 * 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 0:a0c5877bd360 147 * \remark STM32 Standard memcpy function only works on pointers that are aligned
fahadmirza 0:a0c5877bd360 148 *
fahadmirza 0:a0c5877bd360 149 * \param [OUT] dst Destination array
fahadmirza 0:a0c5877bd360 150 * \param [IN] src Source array
fahadmirza 0:a0c5877bd360 151 * \param [IN] size Number of bytes to be copied
fahadmirza 0:a0c5877bd360 152 */
fahadmirza 0:a0c5877bd360 153 void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size );
fahadmirza 0:a0c5877bd360 154
fahadmirza 0:a0c5877bd360 155 /*!
fahadmirza 0:a0c5877bd360 156 * \brief Copies size elements of src array to dst array reversing the byte order
fahadmirza 0:a0c5877bd360 157 *
fahadmirza 0:a0c5877bd360 158 * \param [OUT] dst Destination array
fahadmirza 0:a0c5877bd360 159 * \param [IN] src Source array
fahadmirza 0:a0c5877bd360 160 * \param [IN] size Number of bytes to be copied
fahadmirza 0:a0c5877bd360 161 */
fahadmirza 0:a0c5877bd360 162 void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size );
fahadmirza 0:a0c5877bd360 163
fahadmirza 0:a0c5877bd360 164 /*!
fahadmirza 0:a0c5877bd360 165 * \brief Set size elements of dst array with value
fahadmirza 0:a0c5877bd360 166 *
fahadmirza 0:a0c5877bd360 167 * \remark STM32 Standard memset function only works on pointers that are aligned
fahadmirza 0:a0c5877bd360 168 *
fahadmirza 0:a0c5877bd360 169 * \param [OUT] dst Destination array
fahadmirza 0:a0c5877bd360 170 * \param [IN] value Default value
fahadmirza 0:a0c5877bd360 171 * \param [IN] size Number of bytes to be copied
fahadmirza 0:a0c5877bd360 172 */
fahadmirza 0:a0c5877bd360 173 void memset1( uint8_t *dst, uint8_t value, uint16_t size );
fahadmirza 0:a0c5877bd360 174
fahadmirza 0:a0c5877bd360 175 /*!
fahadmirza 0:a0c5877bd360 176 * \brief Converts a nibble to an hexadecimal character
fahadmirza 0:a0c5877bd360 177 *
fahadmirza 0:a0c5877bd360 178 * \param [IN] a Nibble to be converted
fahadmirza 0:a0c5877bd360 179 * \retval hexChar Converted hexadecimal character
fahadmirza 0:a0c5877bd360 180 */
fahadmirza 0:a0c5877bd360 181 int8_t Nibble2HexChar( uint8_t a );
fahadmirza 0:a0c5877bd360 182
fahadmirza 0:a0c5877bd360 183 uint8_t stringHexToByteArray(const char *hexString, uint8_t *buffer, int8_t bufSize);
fahadmirza 0:a0c5877bd360 184
fahadmirza 0:a0c5877bd360 185 #endif // __UTILITIES_H__
fahadmirza 0:a0c5877bd360 186