log.h 3.8 KB

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