prj_com.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef __PRJ_COM_H__
  2. #define __PRJ_COM_H__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "stdint.h"
  7. /**
  8. * Get x sign bit only for little-endian
  9. * if x >= 0 then 0(false)
  10. * if x < 0 then -1(true)
  11. */
  12. #define IsNegative(x) ((sizeof(x)==4) ? *((int32_t *)&x)>>31 : \
  13. (sizeof(x)==2) ? *((int16_t *)&x)>>15 : \
  14. *((int8_t *)&x)>>7)
  15. #define IsPositive(x) (~IsNegative(x))
  16. #define BcdToOct(bcd) (((bcd) & 0x0f) + ((bcd) >> 4) * 10)
  17. #define OctToBcd(bcd) (((bcd / 10) <<4 ) + ((bcd) % 10))
  18. #define SET_BIT_WARN(data, bit) do{ (data) |= 1<<(bit); }while(0)
  19. #define RESET_BIT_WARN(data, bit) do{ (data) &= (~(1<<(bit))); }while(0)
  20. #define BIT_VAL(data, bit) (((data) & (1 << bit)) > 0 ? 1 : 0)
  21. typedef struct {
  22. uint16_t year;
  23. uint16_t month;
  24. uint16_t day;
  25. uint32_t hour;
  26. uint32_t min;
  27. uint32_t sec;
  28. }tm_t;
  29. extern void HexToStr(uint32_t Hex, char *Str, int16_t Len);
  30. extern int str2int(char *str, uint32_t *n);
  31. extern int str2float(char *str, float *f);
  32. extern void int2str(int n, char *str);
  33. extern void HexBufToStr(char *dest, unsigned char *src, int len);
  34. extern void StrToHexBuf(char *dest, char *src, int len);
  35. extern void num2str(char *str, float value, uint8_t g, uint8_t l);
  36. extern void ToggleEndian(uint8_t *src, uint8_t *dst, uint16_t width, uint16_t num);
  37. extern uint32_t com_crc32L_next(uint32_t crc_cur, uint8_t *data, uint32_t len, uint32_t pattern);
  38. extern uint16_t com_crc16L_next(uint16_t crc_cur, uint8_t *data, uint32_t len, uint16_t pattern);
  39. extern uint32_t com_crc32H_next(uint32_t crc_cur, uint8_t *data, uint32_t len, uint32_t pattern);
  40. extern uint16_t com_crc16H_next(uint16_t crc_cur, uint8_t *data, uint32_t len, uint16_t pattern);
  41. extern uint32_t strnchar(const char *src, char ch, int index);
  42. extern uint32_t strcntchar(const char *src, char ch);
  43. extern uint8_t is_leap(uint16_t year);
  44. extern uint8_t last_day_of_month(uint8_t year, uint8_t month);
  45. extern uint32_t days_from_leap_year_begin(uint8_t month, uint8_t day);
  46. extern uint32_t next_day(uint32_t date);
  47. extern uint32_t next_month(uint32_t date);
  48. extern uint32_t prev_month(uint32_t date);
  49. extern uint32_t prev_day(uint32_t date);
  50. extern void time_add_secs(tm_t *dest, tm_t *src, uint32_t secs);
  51. extern void time_sub_secs(tm_t *dest, tm_t *src, uint32_t secs);
  52. extern uint8_t check_sum(volatile uint8_t *data, uint16_t len);
  53. extern uint16_t check_sum_2_byte(volatile uint8_t *data, uint16_t len);//add by yuewei 20260304
  54. extern uint16_t crc16_modbus(volatile uint8_t *data, uint16_t length);//add by yuewei 20260317
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #endif