misc.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef MISC_H
  18. #define MISC_H
  19. #include <inttypes.h>
  20. static inline uint16_t __swab16(uint16_t x)
  21. {
  22. return (((x & 0x00ffU) << 8) |
  23. ((x & 0xff00U) >> 8));
  24. }
  25. static inline uint32_t __swab32(uint32_t x)
  26. {
  27. return (((x & 0x000000ffUL) << 24) |
  28. ((x & 0x0000ff00UL) << 8) |
  29. ((x & 0x00ff0000UL) >> 8) |
  30. ((x & 0xff000000UL) >> 24));
  31. }
  32. #define __be32_to_cpu(x) __swab32((uint32_t)(x))
  33. #define __be16_to_cpu(x) __swab16((uint16_t)(x))
  34. #define __cpu_to_be32(x) __swab32((uint32_t)(x))
  35. #define __cpu_to_be16(x) __swab16((uint16_t)(x))
  36. #define __le32_to_cpu(x) ((uint32_t)(x))
  37. #define __le16_to_cpu(x) ((uint16_t)(x))
  38. #define __cpu_to_le32(x) ((uint32_t)(x))
  39. #define __cpu_to_le16(x) ((uint16_t)(x))
  40. // See linux/include/unaligned/generic.h etc. for more elaborate versions...
  41. static inline uint16_t __get_unaligned_2(const uint8_t *p)
  42. {
  43. return p[0] | p[1] << 8;
  44. }
  45. static inline uint32_t __get_unaligned_4(const uint8_t *p)
  46. {
  47. return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
  48. }
  49. static inline void __put_unaligned_2(uint16_t val, uint8_t* p)
  50. {
  51. *p = val;
  52. p[1] = val >> 8;
  53. }
  54. static inline void __put_unaligned_4(uint32_t val, uint8_t* p)
  55. {
  56. __put_unaligned_2(val >> 16, p + 2);
  57. __put_unaligned_2(val, p);
  58. }
  59. // character input
  60. int serial_input_available(void);
  61. int serial_input_char(void);
  62. // character output
  63. int print_char(int c);
  64. void print(const char *txt);
  65. // decimal output
  66. void print_int32(int32_t value);
  67. void print_dec32(uint32_t value);
  68. // hexadecimal output
  69. void hex_dump(const void *buffer, uint32_t size);
  70. void print_byte(uint8_t val);
  71. void print_u32(uint32_t val);
  72. #endif /* MISC_H */