log.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include "log.h"
  2. #include "rtc.h"
  3. #include "ringfs.h"
  4. #include "spi_flash.h"
  5. #include "FreeRTOS.h"
  6. #include "task.h"
  7. #include "semphr.h"
  8. #include "event_groups.h"
  9. #include "rtc.h"
  10. #include "settings_api.h"
  11. #include "digital_input.h"
  12. #include "log_ai.h"
  13. #include "log_dio.h"
  14. #include "ringfs_api.h"
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <inttypes.h>
  18. #undef DBG
  19. #define DBG if(1)
  20. static bool archive_state = false;
  21. static bool log_state = true;
  22. static bool log_init_f = false;
  23. static bool archive_init_f = false;
  24. struct ringfs fs_log;
  25. SemaphoreHandle_t log_mutex;
  26. xQueueHandle log_queue;
  27. //EventGroupHandle_t archive_event;
  28. uint16_t log_entries_capacity;
  29. uint16_t archive_entries_capacity;
  30. void archive_task(void *params);
  31. void log_task(void *params);
  32. //
  33. static struct ringfs_flash_partition ringfs_flash_log =
  34. {
  35. .sector_offset = LOG_FLASH_SECTOR_OFFSET,
  36. .sector_erase = op_sector_erase,
  37. .program = op_program,
  38. .read = op_read,
  39. };
  40. //
  41. void log_init(bool format)
  42. {
  43. DBG printf("[LOG] Init...\r\n");
  44. if (!spi_flash_desc.present)
  45. return;
  46. // ---------------------------------------------------------------------- //
  47. // Журнал
  48. ringfs_flash_log.sector_size = spi_flash_desc.sector_size;
  49. ringfs_flash_log.sector_count = LOG_FLASH_SECTOR_COUNT;
  50. ringfs_init(&fs_log, &ringfs_flash_log, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  51. if (format || ringfs_scan(&fs_log) != 0) {
  52. DBG printf("FAT1 false\r\n");
  53. ringfs_format(&fs_log);
  54. }
  55. DBG printf("FAT1 true\r\n");
  56. #if defined (MDIO_88)
  57. log_dio_archive_init();
  58. #endif
  59. #if defined (MAI_12)
  60. log_ai_archive_init();
  61. #endif
  62. // ---------------------------------------------------------------------- //
  63. log_mutex = xSemaphoreCreateMutex();
  64. log_entries_capacity = ringfs_capacity(&fs_log);
  65. archive_entries_capacity = ringfs_capacity(&fs_ch_arch[0]);
  66. // Event
  67. archive_event = xEventGroupCreate();
  68. xTaskCreate(archive_task, "archive_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  69. log_queue = xQueueCreate(10, sizeof(log_entry_t));
  70. xTaskCreate(log_task, "log_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  71. log_init_f = true;
  72. archive_init_f = true;
  73. // Таймер для ведения архива с разным периодом по разным каналам
  74. log_init_archive_tim();
  75. }
  76. // Настройка таймера на частоту 10 Гц
  77. void log_init_archive_tim(void)
  78. {
  79. printf("Init log timer...\r\n");
  80. crm_clocks_freq_type crm_clocks_freq_struct = {0};
  81. crm_periph_clock_enable(CRM_TMR14_PERIPH_CLOCK, TRUE);
  82. crm_clocks_freq_get(&crm_clocks_freq_struct);
  83. tmr_base_init(TMR14, 999, (crm_clocks_freq_struct.ahb_freq / 10000) - 1);
  84. tmr_cnt_dir_set(TMR14, TMR_COUNT_UP);
  85. tmr_flag_clear(TMR14, TMR_OVF_FLAG);
  86. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  87. nvic_irq_enable(TMR8_TRG_HALL_TMR14_IRQn, 5, 0);
  88. tmr_counter_enable(TMR14, TRUE);
  89. tmr_interrupt_enable(TMR14, TMR_OVF_INT, TRUE);
  90. }
  91. //
  92. void TMR8_TRG_HALL_TMR14_IRQHandler(void)
  93. {
  94. if (tmr_flag_get(TMR14, TMR_OVF_FLAG) != RESET)
  95. {
  96. tmr_flag_clear(TMR14, TMR_OVF_FLAG);
  97. if (archive_state)
  98. {
  99. log_check_archive_cnt();
  100. printf("TMR_14 irq\r\n");
  101. }
  102. }
  103. }
  104. //
  105. int log_fetch(void *entry, entry_type_t entry_type, uint8_t ch, uint32_t timeout)
  106. {
  107. int ret;
  108. ret = xSemaphoreTake(log_mutex, (TickType_t)timeout);
  109. if (ret == pdFALSE)
  110. return ret;
  111. if (entry_type == LOG_ENTRY)
  112. ret = ringfs_fetch(&fs_log, entry);
  113. else if (entry_type == ARCHIVE_ENTRY)
  114. ret = ringfs_fetch(&fs_ch_arch[ch], entry);
  115. else ret = -1;
  116. xSemaphoreGive(log_mutex);
  117. return ret;
  118. }
  119. //
  120. int log_discard(void *entry, entry_type_t entry_type, uint8_t ch, uint32_t timeout)
  121. {
  122. int ret;
  123. ret = xSemaphoreTake(log_mutex, (TickType_t)timeout);
  124. if (ret == pdFALSE)
  125. return ret;
  126. if (entry_type == LOG_ENTRY)
  127. ret = ringfs_discard(&fs_log);
  128. else if (entry_type == ARCHIVE_ENTRY)
  129. ret = ringfs_discard(&fs_ch_arch[ch]);
  130. else ret = -1;
  131. xSemaphoreGive(log_mutex);
  132. return ret;
  133. }
  134. //
  135. int log_append(void *entry, entry_type_t entry_type, uint8_t ch)
  136. {
  137. int ret;
  138. TM_RTC_t time;
  139. common_entry_t *entry_ptr = entry;
  140. log_entry_t *log_etnry_ptr;
  141. archive_entry_t *archive_etnry_ptr;
  142. ret = xSemaphoreTake(log_mutex, portMAX_DELAY);
  143. if (ret == pdFALSE)
  144. return ret;
  145. if (entry_ptr->timestamp == 0)
  146. entry_ptr->timestamp = rtc_get_ms();
  147. if (entry_type == LOG_ENTRY)
  148. {
  149. log_etnry_ptr = (log_entry_t*)entry;
  150. log_etnry_ptr->crc = crc_8(entry, sizeof(log_entry_t) - 1);
  151. ret = ringfs_append(&fs_log, entry);
  152. }
  153. else if (entry_type == ARCHIVE_ENTRY)
  154. {
  155. archive_etnry_ptr = (archive_entry_t*)entry;
  156. archive_etnry_ptr->crc = crc_8(entry, sizeof(archive_entry_t) - 1);
  157. ret = ringfs_append(&fs_ch_arch[ch], entry);
  158. }
  159. else ret = -1;
  160. xSemaphoreGive(log_mutex);
  161. return ret;
  162. }
  163. //
  164. uint16_t log_capacity(void)
  165. {
  166. return ringfs_count_exact(&fs_log);
  167. }
  168. //
  169. uint16_t log_arch_capacity(uint8_t ch)
  170. {
  171. return ringfs_count_exact(&fs_ch_arch[ch]);
  172. }
  173. // -------------------------------------------------------------------------- //
  174. // misc
  175. uint8_t crc_8(uint8_t *data, int length)
  176. {
  177. uint8_t crc = 0x00;
  178. uint8_t extract;
  179. uint8_t sum;
  180. for (int i = 0; i < length; i++) {
  181. extract = *data;
  182. for (uint8_t tmp = 8; tmp; tmp--) {
  183. sum = (crc ^ extract) & 0x01;
  184. crc >>= 1;
  185. if (sum)
  186. crc ^= 0x8C;
  187. extract >>= 1;
  188. }
  189. data++;
  190. }
  191. return crc;
  192. }
  193. // -------------------------------------------------------------------------- //
  194. // Tests
  195. // val - 0 - журнал
  196. // val - 1 - архив
  197. // ch - номер канала архива
  198. void log_info(uint8_t val, uint8_t ch)
  199. {
  200. if (val > 1)
  201. return;
  202. struct ringfs *fs = val == 0 ? &fs_log : &fs_ch_arch[ch];
  203. int capacity_flash = 0;
  204. int count_flash = 0;
  205. int count_estimate = 0;
  206. capacity_flash = ringfs_capacity(fs);
  207. count_flash = ringfs_count_exact(fs);
  208. count_estimate = ringfs_count_estimate(fs);
  209. if (val == 0)
  210. {
  211. DBG printf("Log partition capacity: %u\r\n", capacity_flash);
  212. DBG printf("Count log entry: %u\r\n", count_flash);
  213. DBG printf("Estimate count: %u\r\n", count_estimate);
  214. }
  215. else
  216. {
  217. DBG printf("Archive partition capacity: %u\r\n", capacity_flash);
  218. DBG printf("Count archive entry: %u\r\n", count_flash);
  219. DBG printf("Estimate count: %u\r\n", count_estimate);
  220. }
  221. }
  222. // val - 0 - журнал
  223. // val - 1 - архив
  224. // ch - номер канала архива
  225. void log_format(uint8_t val, uint8_t ch)
  226. {
  227. if (val == 0) {
  228. DBG printf("Formating log partition...\r\n");
  229. ringfs_format(&fs_log);
  230. }
  231. else if (val == 1) {
  232. DBG printf("Formating archive partition...\r\n");
  233. ringfs_format(&fs_ch_arch[ch]);
  234. }
  235. }
  236. // Добавить n записей журнала
  237. int log_add_random_entry(uint8_t val, uint32_t cnt_entry, uint8_t ch)
  238. {
  239. int ret;
  240. log_entry_t log_entry = {0};
  241. archive_entry_t archive_entry = {0};
  242. static uint8_t log_index = 0;
  243. static uint32_t archive_index = 0;
  244. if (val == 0)
  245. {
  246. DBG printf("Appending %u archive entries\r\n", cnt_entry);
  247. for (uint32_t i = 0; i < cnt_entry; i++)
  248. {
  249. log_entry.code_type = log_index;
  250. log_entry.code_state = log_index;
  251. log_entry.channel_number = log_index;
  252. log_entry.value = (float)log_index++;
  253. ret = log_append((void*)&log_entry, LOG_ENTRY, 0);
  254. }
  255. DBG printf("Result: %u\r\n", ret);
  256. }
  257. if (val == 1)
  258. {
  259. DBG printf("Appending %u archive entries\r\n", cnt_entry);
  260. for (uint32_t i = 0; i < cnt_entry; i++)
  261. {
  262. archive_entry.input_value = archive_index++;
  263. ret = log_append((void*)&archive_entry, ARCHIVE_ENTRY, ch);
  264. }
  265. DBG printf("Result: %u\r\n", ret);
  266. }
  267. return ret;
  268. }
  269. //
  270. int log_add_entry(log_event_type_t type, log_event_state_t state,
  271. uint8_t channel_number, float value)
  272. {
  273. log_entry_t entry;
  274. if (!log_init_f)
  275. return -1;
  276. entry.timestamp = rtc_get_ms();
  277. entry.code_type = (uint8_t)type;
  278. entry.code_state = (uint8_t)state;
  279. entry.channel_number = channel_number;
  280. entry.value = value;
  281. xQueueSend(log_queue, &entry, 0);
  282. return 0;
  283. }
  284. //
  285. void test_fetch(void)
  286. {
  287. archive_entry_t entry = {0};
  288. log_fetch(&entry, ARCHIVE_ENTRY, 0, portMAX_DELAY);
  289. //printf("\r\n%" PRId64 " [ms]\r\n", rtc_get_ms());
  290. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n", entry.timestamp, entry.input_value, entry.crc);
  291. }
  292. //
  293. void log_archive_state(bool state)
  294. {
  295. archive_state = state;
  296. }
  297. //
  298. void log_log_state(bool state)
  299. {
  300. log_state = state;
  301. }
  302. void archive_task(void *params)
  303. {
  304. int ret = 0;
  305. uint32_t event = 0;
  306. archive_entry_t entry = {0};
  307. EventBits_t bits;
  308. uint8_t channel_number = log_get_arch_channel_number();
  309. for (;;)
  310. {
  311. log_archive_task_device();
  312. }
  313. }
  314. //
  315. void log_task(void *params)
  316. {
  317. int ret;
  318. log_entry_t entry;
  319. for (;;)
  320. {
  321. if (xQueueReceive(log_queue, &entry, portMAX_DELAY) == pdTRUE)
  322. {
  323. DBG printf("Try append LOG entry... ");
  324. ret = log_append((void*)&entry, LOG_ENTRY, 0);
  325. DBG printf("Result: %i\r\n", ret);
  326. }
  327. }
  328. }
  329. // Вызывается в прерывании таймера с частотой 10 Гц
  330. // TODO
  331. void log_check_archive_cnt(void)
  332. {
  333. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  334. uint8_t channel_number = log_get_arch_channel_number();
  335. for (uint8_t i = 0; i < channel_number; i++)
  336. {
  337. if (archive_cnt[i]++ == 10*settings.period_archive[i])
  338. {
  339. archive_cnt[i] = 0;
  340. if (log_is_channel_on(i))
  341. {
  342. DBG printf("Send event: %u\r\n", 1 << i);
  343. xEventGroupSetBitsFromISR(archive_event, 1 << i, &xHigherPriorityTaskWoken);
  344. }
  345. }
  346. }
  347. }