Fahad Mirza
/
Nucleo_HXC900
A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.
Utilities/utilities.c@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 | #include <stdlib.h> |
fahadmirza | 0:a0c5877bd360 | 25 | #include <stdio.h> |
fahadmirza | 0:a0c5877bd360 | 26 | #include <stdint.h> |
fahadmirza | 0:a0c5877bd360 | 27 | #include "tiny_sscanf.h" |
fahadmirza | 0:a0c5877bd360 | 28 | #include "utilities.h" |
fahadmirza | 0:a0c5877bd360 | 29 | |
fahadmirza | 0:a0c5877bd360 | 30 | /*! |
fahadmirza | 0:a0c5877bd360 | 31 | * Redefinition of rand() and srand() standard C functions. |
fahadmirza | 0:a0c5877bd360 | 32 | * These functions are redefined in order to get the same behavior across |
fahadmirza | 0:a0c5877bd360 | 33 | * different compiler toolchains implementations. |
fahadmirza | 0:a0c5877bd360 | 34 | */ |
fahadmirza | 0:a0c5877bd360 | 35 | // Standard random functions redefinition start |
fahadmirza | 0:a0c5877bd360 | 36 | #define RAND_LOCAL_MAX 2147483647L |
fahadmirza | 0:a0c5877bd360 | 37 | |
fahadmirza | 0:a0c5877bd360 | 38 | static uint32_t next = 1; |
fahadmirza | 0:a0c5877bd360 | 39 | |
fahadmirza | 0:a0c5877bd360 | 40 | int32_t rand1( void ) |
fahadmirza | 0:a0c5877bd360 | 41 | { |
fahadmirza | 0:a0c5877bd360 | 42 | return ( ( next = next * 1103515245L + 12345L ) % RAND_LOCAL_MAX ); |
fahadmirza | 0:a0c5877bd360 | 43 | } |
fahadmirza | 0:a0c5877bd360 | 44 | |
fahadmirza | 0:a0c5877bd360 | 45 | void srand1( uint32_t seed ) |
fahadmirza | 0:a0c5877bd360 | 46 | { |
fahadmirza | 0:a0c5877bd360 | 47 | next = seed; |
fahadmirza | 0:a0c5877bd360 | 48 | } |
fahadmirza | 0:a0c5877bd360 | 49 | // Standard random functions redefinition end |
fahadmirza | 0:a0c5877bd360 | 50 | |
fahadmirza | 0:a0c5877bd360 | 51 | int32_t randr( int32_t min, int32_t max ) |
fahadmirza | 0:a0c5877bd360 | 52 | { |
fahadmirza | 0:a0c5877bd360 | 53 | return ( int32_t )rand1( ) % ( max - min + 1 ) + min; |
fahadmirza | 0:a0c5877bd360 | 54 | } |
fahadmirza | 0:a0c5877bd360 | 55 | |
fahadmirza | 0:a0c5877bd360 | 56 | void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size ) |
fahadmirza | 0:a0c5877bd360 | 57 | { |
fahadmirza | 0:a0c5877bd360 | 58 | while( size-- ) |
fahadmirza | 0:a0c5877bd360 | 59 | { |
fahadmirza | 0:a0c5877bd360 | 60 | *dst++ = *src++; |
fahadmirza | 0:a0c5877bd360 | 61 | } |
fahadmirza | 0:a0c5877bd360 | 62 | } |
fahadmirza | 0:a0c5877bd360 | 63 | |
fahadmirza | 0:a0c5877bd360 | 64 | void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size ) |
fahadmirza | 0:a0c5877bd360 | 65 | { |
fahadmirza | 0:a0c5877bd360 | 66 | dst = dst + ( size - 1 ); |
fahadmirza | 0:a0c5877bd360 | 67 | while( size-- ) |
fahadmirza | 0:a0c5877bd360 | 68 | { |
fahadmirza | 0:a0c5877bd360 | 69 | *dst-- = *src++; |
fahadmirza | 0:a0c5877bd360 | 70 | } |
fahadmirza | 0:a0c5877bd360 | 71 | } |
fahadmirza | 0:a0c5877bd360 | 72 | |
fahadmirza | 0:a0c5877bd360 | 73 | void memset1( uint8_t *dst, uint8_t value, uint16_t size ) |
fahadmirza | 0:a0c5877bd360 | 74 | { |
fahadmirza | 0:a0c5877bd360 | 75 | while( size-- ) |
fahadmirza | 0:a0c5877bd360 | 76 | { |
fahadmirza | 0:a0c5877bd360 | 77 | *dst++ = value; |
fahadmirza | 0:a0c5877bd360 | 78 | } |
fahadmirza | 0:a0c5877bd360 | 79 | } |
fahadmirza | 0:a0c5877bd360 | 80 | |
fahadmirza | 0:a0c5877bd360 | 81 | int8_t Nibble2HexChar( uint8_t a ) |
fahadmirza | 0:a0c5877bd360 | 82 | { |
fahadmirza | 0:a0c5877bd360 | 83 | if( a < 10 ) |
fahadmirza | 0:a0c5877bd360 | 84 | { |
fahadmirza | 0:a0c5877bd360 | 85 | return '0' + a; |
fahadmirza | 0:a0c5877bd360 | 86 | } |
fahadmirza | 0:a0c5877bd360 | 87 | else if( a < 16 ) |
fahadmirza | 0:a0c5877bd360 | 88 | { |
fahadmirza | 0:a0c5877bd360 | 89 | return 'A' + ( a - 10 ); |
fahadmirza | 0:a0c5877bd360 | 90 | } |
fahadmirza | 0:a0c5877bd360 | 91 | else |
fahadmirza | 0:a0c5877bd360 | 92 | { |
fahadmirza | 0:a0c5877bd360 | 93 | return '?'; |
fahadmirza | 0:a0c5877bd360 | 94 | } |
fahadmirza | 0:a0c5877bd360 | 95 | } |
fahadmirza | 0:a0c5877bd360 | 96 | |
fahadmirza | 32:2d0678039a09 | 97 | uint8_t stringHexToByteArray(const char *hexString, uint8_t *buffer, uint8_t bufSize) |
fahadmirza | 0:a0c5877bd360 | 98 | { |
fahadmirza | 0:a0c5877bd360 | 99 | uint8_t count = 0; |
fahadmirza | 0:a0c5877bd360 | 100 | char hex[3]; |
fahadmirza | 0:a0c5877bd360 | 101 | hex[2] = '\0'; |
fahadmirza | 0:a0c5877bd360 | 102 | |
fahadmirza | 0:a0c5877bd360 | 103 | while( (*hexString != '\0') && (*(hexString + 1) != '\0') ) |
fahadmirza | 0:a0c5877bd360 | 104 | { |
fahadmirza | 0:a0c5877bd360 | 105 | // Separate two digits(i.e 1 HexByte) from the hex string |
fahadmirza | 0:a0c5877bd360 | 106 | hex[0] = *hexString; |
fahadmirza | 0:a0c5877bd360 | 107 | hex[1] = *(hexString + 1); |
fahadmirza | 0:a0c5877bd360 | 108 | |
fahadmirza | 0:a0c5877bd360 | 109 | if((tiny_sscanf(hex, "%hhx",&buffer[count]) != 1)) |
fahadmirza | 0:a0c5877bd360 | 110 | { |
fahadmirza | 0:a0c5877bd360 | 111 | // hex doesn't have valid numbers |
fahadmirza | 0:a0c5877bd360 | 112 | break; |
fahadmirza | 0:a0c5877bd360 | 113 | } |
fahadmirza | 0:a0c5877bd360 | 114 | count++; |
fahadmirza | 32:2d0678039a09 | 115 | if (count == (bufSize - 1)) |
fahadmirza | 0:a0c5877bd360 | 116 | { |
fahadmirza | 0:a0c5877bd360 | 117 | // We are out of space |
fahadmirza | 0:a0c5877bd360 | 118 | break; |
fahadmirza | 0:a0c5877bd360 | 119 | } |
fahadmirza | 0:a0c5877bd360 | 120 | // Advance string hex pointer by two |
fahadmirza | 0:a0c5877bd360 | 121 | hexString += 2; |
fahadmirza | 0:a0c5877bd360 | 122 | } |
fahadmirza | 6:1d09b7960d57 | 123 | |
fahadmirza | 32:2d0678039a09 | 124 | buffer[count] = '\0'; |
fahadmirza | 6:1d09b7960d57 | 125 | |
fahadmirza | 0:a0c5877bd360 | 126 | return count; |
fahadmirza | 0:a0c5877bd360 | 127 | } |
fahadmirza | 0:a0c5877bd360 | 128 |