utils.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef _UTILS_H__
  2. #define _UTILS_H__
  3. #include "os/os_type.h"
  4. #include "os/co_task.h"
  5. #define delay_us cpu_udelay
  6. static __inline__ void delay_ms(u32 ms) {
  7. cpu_udelay(ms*1000);
  8. }
  9. static __inline__ u8 decode_u8(u8 *buff) {
  10. return buff[0];
  11. }
  12. static __inline__ u16 decode_u16(u8 *buff) {
  13. return (((u16)(buff[1]) << 8) | buff[0]);
  14. }
  15. static __inline__ u32 decode_u24(u8 *buff) {
  16. return ((u32)(buff[2]) << 16 | decode_u16(buff));
  17. }
  18. static __inline__ u16 decode_u32(u8 *buff) {
  19. return ((u32)(buff[3]) << 24 | decode_u24(buff));
  20. }
  21. static __inline__ void encode_u8(u8 *buff, u8 value)
  22. {
  23. buff[0] = value;
  24. }
  25. static __inline__ void encode_u16(u8 *buff, u16 value)
  26. {
  27. buff[0] = value;
  28. buff[1] = value >> 8;
  29. }
  30. static __inline__ void encode_u24(u8 *buff, u32 value)
  31. {
  32. encode_u16(buff, value);
  33. buff[2] = value >> 16;
  34. }
  35. static __inline__ void encode_u32(u8 *buff, u32 value)
  36. {
  37. encode_u24(buff, value);
  38. buff[3] = value >> 24;
  39. }
  40. static __inline__ s8 decode_s08(const u8 *buff)
  41. {
  42. return ((s8) buff[0]);
  43. }
  44. static __inline__ s16 decode_s16(const u8 *buff)
  45. {
  46. return ((s16) buff[1]) << 8 | buff[0];
  47. }
  48. static __inline__ s32 decode_s24(const u8 *buff)
  49. {
  50. return ((s32) buff[2]) << 16 | decode_s16(buff);
  51. }
  52. static __inline__ s32 decode_s32(const u8 *buff)
  53. {
  54. return ((s32) buff[3]) << 24 | decode_s24(buff);
  55. }
  56. static __inline__ void encode_s08(u8 *buff, s8 value)
  57. {
  58. buff[0] = value;
  59. }
  60. static __inline__ void encode_s16(u8 *buff, s16 value)
  61. {
  62. encode_s08(buff,value);
  63. encode_s08(buff+1,value >> 8);
  64. }
  65. static __inline__ void encode_s24(u8 *buff, s32 value)
  66. {
  67. encode_s16(buff, value);
  68. encode_s08(buff+2,value >> 16);
  69. }
  70. static __inline__ void encode_s32(u8 *buff, s32 value)
  71. {
  72. encode_s24(buff, value);
  73. encode_s08(buff+3,value >> 24);
  74. }
  75. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
  76. #define min(a,b) (a>b?b:a)
  77. #define MAX(x, y) ((x)>(y)?(x):(y))
  78. #define F2I(f) ((int)(f))
  79. #endif /* _UTILS_H__ */