kdb_core.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-09 tyx first version.
  9. */
  10. #ifndef __KDB_CORE_H__
  11. #define __KDB_CORE_H__
  12. #include <rtthread.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define KDB_HEAD_SIZE (sizeof(struct kdb_head))
  17. #define KDB_ITEM_MAGIC (0x5A)
  18. #define KDB_FLAG_START (0x01 << 0)
  19. #ifndef KDB_MAX_DATA_LEN
  20. #define KDB_MAX_DATA_LEN (64)
  21. #endif
  22. #ifndef KDB_MEMCPY
  23. #define KDB_MEMCPY rt_memcpy
  24. #endif
  25. #define KDB_CCODE(_kdb) (_kdb)->ccode = kdb_ccode(_kdb)
  26. /*
  27. * +-----------------+------+------+-------+-----+
  28. * | data | tick | type | magic | len |
  29. * +-----------------+------+------+-------+-----+
  30. * data - save user data
  31. * tick - tick stamp, 4bytes
  32. * type - 1bytes, flag
  33. * magic - 1bytes, type id
  34. * len - 2bytes, the length of data + header.
  35. */
  36. struct kdb_head
  37. {
  38. rt_uint32_t tick;
  39. rt_uint8_t type;
  40. rt_uint8_t magic;
  41. rt_uint16_t len;
  42. };
  43. typedef struct kdb_head *kdb_head_t;
  44. struct kdb
  45. {
  46. rt_uint32_t ccode;
  47. rt_uint32_t curr_idx;
  48. rt_uint32_t flags;
  49. rt_int32_t blen;
  50. rt_uint8_t *buff;
  51. void *data_buff;
  52. };
  53. typedef struct kdb *kdb_t;
  54. void kdb_record(kdb_t kdb, rt_uint16_t type, void *buff, rt_uint16_t len);
  55. void kdb_trace(kdb_t kdb);
  56. rt_uint32_t kdb_ccode(kdb_t kdb);
  57. int kdb_isinval(kdb_t kdb);
  58. rt_inline void kdb_init(kdb_t kdb, void *buff, rt_int32_t len)
  59. {
  60. if (kdb)
  61. {
  62. rt_memset(buff, 0, len);
  63. kdb->curr_idx = 0;
  64. kdb->buff = buff;
  65. kdb->blen = len - KDB_MAX_DATA_LEN;
  66. kdb->flags = 0;
  67. kdb->data_buff = ((rt_uint8_t *)buff) + len - KDB_MAX_DATA_LEN;
  68. }
  69. }
  70. rt_inline void kdb_start(kdb_t kdb)
  71. {
  72. if (kdb)
  73. kdb->flags |= KDB_FLAG_START;
  74. }
  75. rt_inline void kdb_stop(kdb_t kdb)
  76. {
  77. if (kdb)
  78. kdb->flags &= ~KDB_FLAG_START;
  79. }
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. #endif