this is avaiable project
Fork of LoRaMacLib by
system/utilities.cpp@0:9be122c18509, 2015-08-12 (annotated)
- Committer:
- GregCr
- Date:
- Wed Aug 12 14:08:29 2015 +0000
- Revision:
- 0:9be122c18509
First Implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregCr | 0:9be122c18509 | 1 | /* |
GregCr | 0:9be122c18509 | 2 | / _____) _ | | |
GregCr | 0:9be122c18509 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
GregCr | 0:9be122c18509 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
GregCr | 0:9be122c18509 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
GregCr | 0:9be122c18509 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
GregCr | 0:9be122c18509 | 7 | (C)2013 Semtech |
GregCr | 0:9be122c18509 | 8 | |
GregCr | 0:9be122c18509 | 9 | Description: Helper functions implementation |
GregCr | 0:9be122c18509 | 10 | |
GregCr | 0:9be122c18509 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
GregCr | 0:9be122c18509 | 12 | |
GregCr | 0:9be122c18509 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
GregCr | 0:9be122c18509 | 14 | */ |
GregCr | 0:9be122c18509 | 15 | #include <stdlib.h> |
GregCr | 0:9be122c18509 | 16 | #include <stdio.h> |
GregCr | 0:9be122c18509 | 17 | #include "board.h" |
GregCr | 0:9be122c18509 | 18 | #include "utilities.h" |
GregCr | 0:9be122c18509 | 19 | |
GregCr | 0:9be122c18509 | 20 | /*! |
GregCr | 0:9be122c18509 | 21 | * Redefinition of rand() and srand() standard C functions. |
GregCr | 0:9be122c18509 | 22 | * These functions are redefined in order to get the same behavior across |
GregCr | 0:9be122c18509 | 23 | * different compiler toolchains implementations. |
GregCr | 0:9be122c18509 | 24 | */ |
GregCr | 0:9be122c18509 | 25 | // Standard random functions redefinition start |
GregCr | 0:9be122c18509 | 26 | #define RAND_LOCAL_MAX 2147483647 |
GregCr | 0:9be122c18509 | 27 | |
GregCr | 0:9be122c18509 | 28 | static unsigned long next = 1; |
GregCr | 0:9be122c18509 | 29 | |
GregCr | 0:9be122c18509 | 30 | int rand1( void ) |
GregCr | 0:9be122c18509 | 31 | { |
GregCr | 0:9be122c18509 | 32 | return ( ( next = next * 1103515245 + 12345 ) % RAND_LOCAL_MAX ); |
GregCr | 0:9be122c18509 | 33 | } |
GregCr | 0:9be122c18509 | 34 | |
GregCr | 0:9be122c18509 | 35 | void srand1( unsigned int seed ) |
GregCr | 0:9be122c18509 | 36 | { |
GregCr | 0:9be122c18509 | 37 | next = seed; |
GregCr | 0:9be122c18509 | 38 | } |
GregCr | 0:9be122c18509 | 39 | // Standard random functions redefinition end |
GregCr | 0:9be122c18509 | 40 | |
GregCr | 0:9be122c18509 | 41 | int32_t randr( int32_t min, int32_t max ) |
GregCr | 0:9be122c18509 | 42 | { |
GregCr | 0:9be122c18509 | 43 | return ( int32_t )rand1( ) % ( max - min + 1 ) + min; |
GregCr | 0:9be122c18509 | 44 | } |
GregCr | 0:9be122c18509 | 45 | |
GregCr | 0:9be122c18509 | 46 | void memcpy1( uint8_t *dst, uint8_t *src, uint16_t size ) |
GregCr | 0:9be122c18509 | 47 | { |
GregCr | 0:9be122c18509 | 48 | while( size-- ) |
GregCr | 0:9be122c18509 | 49 | { |
GregCr | 0:9be122c18509 | 50 | *dst++ = *src++; |
GregCr | 0:9be122c18509 | 51 | } |
GregCr | 0:9be122c18509 | 52 | } |
GregCr | 0:9be122c18509 | 53 | |
GregCr | 0:9be122c18509 | 54 | void memset1( uint8_t *dst, uint8_t value, uint16_t size ) |
GregCr | 0:9be122c18509 | 55 | { |
GregCr | 0:9be122c18509 | 56 | while( size-- ) |
GregCr | 0:9be122c18509 | 57 | { |
GregCr | 0:9be122c18509 | 58 | *dst++ = value; |
GregCr | 0:9be122c18509 | 59 | } |
GregCr | 0:9be122c18509 | 60 | } |
GregCr | 0:9be122c18509 | 61 | |
GregCr | 0:9be122c18509 | 62 | int8_t Nibble2HexChar( uint8_t a ) |
GregCr | 0:9be122c18509 | 63 | { |
GregCr | 0:9be122c18509 | 64 | if( a < 10 ) |
GregCr | 0:9be122c18509 | 65 | { |
GregCr | 0:9be122c18509 | 66 | return '0' + a; |
GregCr | 0:9be122c18509 | 67 | } |
GregCr | 0:9be122c18509 | 68 | else if( a < 16 ) |
GregCr | 0:9be122c18509 | 69 | { |
GregCr | 0:9be122c18509 | 70 | return 'A' + ( a - 10 ); |
GregCr | 0:9be122c18509 | 71 | } |
GregCr | 0:9be122c18509 | 72 | else |
GregCr | 0:9be122c18509 | 73 | { |
GregCr | 0:9be122c18509 | 74 | return '?'; |
GregCr | 0:9be122c18509 | 75 | } |
GregCr | 0:9be122c18509 | 76 | } |
GregCr | 0:9be122c18509 | 77 | |
GregCr | 0:9be122c18509 | 78 | #ifdef __GNUC__ |
GregCr | 0:9be122c18509 | 79 | /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf |
GregCr | 0:9be122c18509 | 80 | set to 'Yes') calls __io_putchar() */ |
GregCr | 0:9be122c18509 | 81 | int __io_putchar( int c ) |
GregCr | 0:9be122c18509 | 82 | #else /* __GNUC__ */ |
GregCr | 0:9be122c18509 | 83 | int fputc( int c, FILE *stream ) |
GregCr | 0:9be122c18509 | 84 | #endif |
GregCr | 0:9be122c18509 | 85 | { |
GregCr | 0:9be122c18509 | 86 | return( ITM_SendChar( c ) ); |
GregCr | 0:9be122c18509 | 87 | } |