utilities.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2013 Semtech
  8. Description: Helper functions implementation
  9. License: Revised BSD License, see LICENSE.TXT file include in the project
  10. Maintainer: Miguel Luis and Gregory Cristian
  11. */
  12. //#include <stdlib.h>
  13. //#include <stdio.h>
  14. #include "stdint.h"
  15. #include <stdbool.h>
  16. #include "utilities.h"
  17. /*!
  18. * Redefinition of rand() and srand() standard C functions.
  19. * These functions are redefined in order to get the same behavior across
  20. * different compiler toolchains implementations.
  21. */
  22. // Standard random functions redefinition start
  23. #define RAND_LOCAL_MAX 2147483647L
  24. static uint32_t next = 1;
  25. int32_t rand1( void )
  26. {
  27. return ( ( next = next * 1103515245L + 12345L ) % RAND_LOCAL_MAX );
  28. }
  29. void srand1( uint32_t seed )
  30. {
  31. next = seed;
  32. }
  33. // Standard random functions redefinition end
  34. int32_t randr( int32_t min, int32_t max )
  35. {
  36. return ( int32_t )rand1( ) % ( max - min + 1 ) + min;
  37. }
  38. void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size )
  39. {
  40. while( size-- )
  41. {
  42. *dst++ = *src++;
  43. }
  44. }
  45. void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size )
  46. {
  47. dst = dst + ( size - 1 );
  48. while( size-- )
  49. {
  50. *dst-- = *src++;
  51. }
  52. }
  53. void memset1( uint8_t *dst, uint8_t value, uint16_t size )
  54. {
  55. while( size )
  56. {
  57. *dst++ = value;
  58. size--;
  59. }
  60. }
  61. int8_t Nibble2HexChar( uint8_t a )
  62. {
  63. if( a < 10 )
  64. {
  65. return '0' + a;
  66. }
  67. else if( a < 16 )
  68. {
  69. return 'A' + ( a - 10 );
  70. }
  71. else
  72. {
  73. return '?';
  74. }
  75. }