log.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef __LOG_H
  2. #define __LOG_H
  3. #include "at32f403a_407.h"
  4. #include <stdbool.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #define LOG_ENTRY_VERSION 1
  9. #define ARCHIV_ENTRY_VERSION 1
  10. #define LOG_FLASH_SECTOR_OFFSET 4
  11. #define LOG_FLASH_SECTOR_COUNT 51
  12. #define ARCHIVE_FLASH_SECTOR_OFFSET 60
  13. #define ARCHIVE_FLASH_SECTOR_COUNT 38
  14. #define ARCHIVE_CHANNEL_OFFSET (ARCHIVE_FLASH_SECTOR_COUNT + 4)
  15. #define SECTOR_COUNT (spi_flash_desc.sector_count/2 - LOG_FLASH_SECTOR_OFFSET)
  16. #define MB_ARCHIVE_ENTRY 0x06
  17. #define MB_LOG_ENTRY 0x07
  18. // -------------------------------------------------------------------------- //
  19. // Настройки архивов
  20. #define ARCH_CH_NUMBER 8
  21. // События архивов
  22. typedef enum
  23. {
  24. ARCH_CH_1 = 1 << 0,
  25. ARCH_CH_2 = 1 << 1,
  26. ARCH_CH_3 = 1 << 2,
  27. ARCH_CH_4 = 1 << 3,
  28. ARCH_CH_5 = 1 << 4,
  29. ARCH_CH_6 = 1 << 5,
  30. ARCH_CH_7 = 1 << 6,
  31. ARCH_CH_8 = 1 << 7,
  32. } arch_event_t;
  33. //
  34. typedef enum
  35. {
  36. LOG_SYSTEM_BOOT = 1, // включение питания/перезагрузка
  37. LOG_CLOCK_CHANGE, // перевод времени
  38. LOG_UPDATE_FW, // обновление FW
  39. LOG_SYSTEM_ERR, // самодиагностика/системная ошибка
  40. LOG_CHANGE_CONFIG, // изменение конфигурации
  41. LOG_OUPUTS, // диагностика выходов
  42. LOG_SETPOINT, // срабатывание уставок
  43. LOG_SAVE_MODE, // переход в безопасный режим
  44. LOG_CLEAR, // очистка журнала/архива
  45. LOG_NONE,
  46. } log_event_type_t;
  47. typedef enum
  48. {
  49. LOG_EVENT_STATE_ERR = 0,
  50. LOG_EVENT_STATE_OK,
  51. } log_event_state_t;
  52. typedef enum
  53. {
  54. LOG_ENTRY = 0,
  55. ARCHIVE_ENTRY,
  56. } entry_type_t;
  57. //
  58. typedef __packed struct
  59. {
  60. uint64_t timestamp;
  61. } common_entry_t;
  62. // Структура записи журанала
  63. typedef __packed struct
  64. {
  65. uint64_t timestamp;
  66. uint8_t code_type; // код типа события
  67. uint8_t code_state; // код состояния
  68. uint8_t channel_number; // номер канала
  69. float value; // значение
  70. uint8_t crc;
  71. } log_entry_t;
  72. // Структура архивной записи
  73. typedef __packed struct
  74. {
  75. uint64_t timestamp;
  76. uint8_t input_value;
  77. uint8_t crc;
  78. } archive_entry_t;
  79. //
  80. void log_init(bool format);
  81. //
  82. void log_init_archive_tim(void);
  83. //
  84. int log_fetch(void *entry, entry_type_t entry_type, uint8_t ch, uint32_t timeout);
  85. //
  86. int log_discard(void *entry, entry_type_t entry_type, uint8_t ch, uint32_t timeout);
  87. //
  88. int log_append(void *entry, entry_type_t entry_type, uint8_t ch);
  89. //
  90. uint16_t log_capacity(void);
  91. //
  92. uint16_t log_arch_capacity(uint8_t ch);
  93. // -------------------------------------------------------------------------- //
  94. // misc
  95. uint8_t crc_8(uint8_t *data, int length);
  96. // -------------------------------------------------------------------------- //
  97. // Tests
  98. //
  99. void log_info(uint8_t val, uint8_t ch);
  100. //
  101. void log_format(uint8_t val, uint8_t ch);
  102. //
  103. int log_add_random_entry(uint8_t val, uint32_t cnt_entry, uint8_t ch);
  104. //
  105. int log_add_entry(log_event_type_t type, log_event_state_t state,
  106. uint8_t channel_number, float value);
  107. //
  108. void test_fetch(void);
  109. //
  110. void log_archive_state(bool state);
  111. //
  112. void log_log_state(bool state);
  113. //
  114. void test_archive_format(void);
  115. //
  116. void log_check_archive_cnt(void);
  117. // -------------------------------------------------------------------------- //
  118. extern uint16_t log_entries_capacity;
  119. extern uint16_t archive_entries_capacity;
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif /* __LOG_H */