log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 <string.h>
  12. #include <stdio.h>
  13. #include <inttypes.h>
  14. #undef DBG
  15. #define DBG if(1)
  16. static bool archive_state = false;
  17. static bool log_state = true;
  18. static bool log_init_f = false;
  19. static bool archive_init_f = false;
  20. struct ringfs fs_log;
  21. //struct ringfs fs_archive;
  22. struct ringfs fs_ch_arch[ARCH_CH_NUMBER];
  23. SemaphoreHandle_t log_mutex;
  24. xQueueHandle log_queue;
  25. EventGroupHandle_t archive_event;
  26. uint16_t log_entries_capacity;
  27. uint16_t archive_entries_capacity;
  28. uint32_t archive_cnt[ARCH_CH_NUMBER] = {0};
  29. void archive_task(void *params);
  30. void log_task(void *params);
  31. //
  32. static int op_sector_erase(struct ringfs_flash_partition *flash, int address) {
  33. (void)flash;
  34. int ret;
  35. ret = spi_flash_erase_sector(address, 0);
  36. return ret;
  37. }
  38. //
  39. static ssize_t op_program(struct ringfs_flash_partition *flash, int address, const void *data, size_t size) {
  40. (void)flash;
  41. int ret;
  42. ret = spi_flash_write(address, data, size, 0);
  43. return ret;
  44. }
  45. //
  46. static ssize_t op_read(struct ringfs_flash_partition *flash, int address, void *data, size_t size) {
  47. (void)flash;
  48. int ret;
  49. ret = spi_flash_read(address, data, size, 0);
  50. return ret;
  51. }
  52. //
  53. static struct ringfs_flash_partition ringfs_flash_log =
  54. {
  55. .sector_offset = LOG_FLASH_SECTOR_OFFSET,
  56. .sector_erase = op_sector_erase,
  57. .program = op_program,
  58. .read = op_read,
  59. };
  60. //
  61. static struct ringfs_flash_partition ringfs_flash_archive =
  62. {
  63. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET,
  64. .sector_erase = op_sector_erase,
  65. .program = op_program,
  66. .read = op_read,
  67. };
  68. //
  69. static struct ringfs_flash_partition fingfs_flash_ch_arch[ARCH_CH_NUMBER] =
  70. {
  71. {
  72. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET,
  73. .sector_erase = op_sector_erase,
  74. .program = op_program,
  75. .read = op_read,
  76. },
  77. {
  78. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET + ARCHIVE_CHANNEL_OFFSET*1,
  79. .sector_erase = op_sector_erase,
  80. .program = op_program,
  81. .read = op_read,
  82. },
  83. {
  84. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET + ARCHIVE_CHANNEL_OFFSET*2,
  85. .sector_erase = op_sector_erase,
  86. .program = op_program,
  87. .read = op_read,
  88. },
  89. {
  90. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET + ARCHIVE_CHANNEL_OFFSET*3,
  91. .sector_erase = op_sector_erase,
  92. .program = op_program,
  93. .read = op_read,
  94. },
  95. {
  96. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET + ARCHIVE_CHANNEL_OFFSET*4,
  97. .sector_erase = op_sector_erase,
  98. .program = op_program,
  99. .read = op_read,
  100. },
  101. {
  102. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET + ARCHIVE_CHANNEL_OFFSET*5,
  103. .sector_erase = op_sector_erase,
  104. .program = op_program,
  105. .read = op_read,
  106. },
  107. {
  108. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET + ARCHIVE_CHANNEL_OFFSET*6,
  109. .sector_erase = op_sector_erase,
  110. .program = op_program,
  111. .read = op_read,
  112. },
  113. {
  114. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET + ARCHIVE_CHANNEL_OFFSET*7,
  115. .sector_erase = op_sector_erase,
  116. .program = op_program,
  117. .read = op_read,
  118. },
  119. };
  120. //
  121. void log_init(bool format)
  122. {
  123. DBG printf("[LOG] Init...\r\n");
  124. if (!spi_flash_desc.present)
  125. return;
  126. // ---------------------------------------------------------------------- //
  127. // Журнал
  128. ringfs_flash_log.sector_size = spi_flash_desc.sector_size;
  129. ringfs_flash_log.sector_count = LOG_FLASH_SECTOR_COUNT;
  130. ringfs_init(&fs_log, &ringfs_flash_log, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  131. if (format || ringfs_scan(&fs_log) != 0) {
  132. DBG printf("FAT1 false\r\n");
  133. ringfs_format(&fs_log);
  134. }
  135. DBG printf("FAT1 true\r\n");
  136. // ---------------------------------------------------------------------- //
  137. // Архив. 8 буфером на каждый канал.
  138. for (uint8_t i = 0; i < ARCH_CH_NUMBER; i ++)
  139. {
  140. fingfs_flash_ch_arch[i].sector_size = spi_flash_desc.sector_size,
  141. fingfs_flash_ch_arch[i].sector_count = ARCHIVE_FLASH_SECTOR_COUNT,
  142. ringfs_init(&fs_ch_arch[i], &fingfs_flash_ch_arch[i],
  143. ARCHIV_ENTRY_VERSION + i, sizeof(archive_entry_t));
  144. if (ringfs_scan(&fs_ch_arch[i]) != 0) {
  145. DBG printf("FAT for channel %u is false\r\n", i + 1);
  146. ringfs_format(&fs_ch_arch[i]);
  147. }
  148. DBG printf("FAT for channel %u is true\r\n", i + 1);
  149. }
  150. // ---------------------------------------------------------------------- //
  151. log_mutex = xSemaphoreCreateMutex();
  152. log_entries_capacity = ringfs_capacity(&fs_log);
  153. archive_entries_capacity = ringfs_capacity(&fs_ch_arch[0]);
  154. // Event
  155. archive_event = xEventGroupCreate();
  156. xTaskCreate(archive_task, "archive_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  157. log_queue = xQueueCreate(10, sizeof(log_entry_t));
  158. xTaskCreate(log_task, "log_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  159. log_init_f = true;
  160. archive_init_f = true;
  161. // Таймер для ведения архива с разным периодом по разным каналам
  162. log_init_archive_tim();
  163. }
  164. // Настройка таймера на частоту 10 Гц
  165. void log_init_archive_tim(void)
  166. {
  167. printf("Init log timer...\r\n");
  168. crm_clocks_freq_type crm_clocks_freq_struct = {0};
  169. crm_periph_clock_enable(CRM_TMR14_PERIPH_CLOCK, TRUE);
  170. crm_clocks_freq_get(&crm_clocks_freq_struct);
  171. tmr_base_init(TMR14, 999, (crm_clocks_freq_struct.ahb_freq / 10000) - 1);
  172. tmr_cnt_dir_set(TMR14, TMR_COUNT_UP);
  173. tmr_flag_clear(TMR14, TMR_OVF_FLAG);
  174. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  175. nvic_irq_enable(TMR8_TRG_HALL_TMR14_IRQn, 5, 0);
  176. tmr_counter_enable(TMR14, TRUE);
  177. tmr_interrupt_enable(TMR14, TMR_OVF_INT, TRUE);
  178. }
  179. //
  180. void TMR8_TRG_HALL_TMR14_IRQHandler(void)
  181. {
  182. if (tmr_flag_get(TMR14, TMR_OVF_FLAG) != RESET)
  183. {
  184. tmr_flag_clear(TMR14, TMR_OVF_FLAG);
  185. if (archive_state)
  186. {
  187. log_check_archive_cnt();
  188. printf("TMR_14 irq\r\n");
  189. }
  190. }
  191. }
  192. //
  193. int log_fetch(void *entry, entry_type_t entry_type, uint8_t ch, uint32_t timeout)
  194. {
  195. int ret;
  196. ret = xSemaphoreTake(log_mutex, (TickType_t)timeout);
  197. if (ret == pdFALSE)
  198. return ret;
  199. if (entry_type == LOG_ENTRY)
  200. ret = ringfs_fetch(&fs_log, entry);
  201. else if (entry_type == ARCHIVE_ENTRY)
  202. ret = ringfs_fetch(&fs_ch_arch[ch], entry);
  203. else ret = -1;
  204. xSemaphoreGive(log_mutex);
  205. return ret;
  206. }
  207. //
  208. int log_discard(void *entry, entry_type_t entry_type, uint8_t ch, uint32_t timeout)
  209. {
  210. int ret;
  211. ret = xSemaphoreTake(log_mutex, (TickType_t)timeout);
  212. if (ret == pdFALSE)
  213. return ret;
  214. if (entry_type == LOG_ENTRY)
  215. ret = ringfs_discard(&fs_log);
  216. else if (entry_type == ARCHIVE_ENTRY)
  217. ret = ringfs_discard(&fs_ch_arch[ch]);
  218. else ret = -1;
  219. xSemaphoreGive(log_mutex);
  220. return ret;
  221. }
  222. //
  223. int log_append(void *entry, entry_type_t entry_type, uint8_t ch)
  224. {
  225. int ret;
  226. TM_RTC_t time;
  227. common_entry_t *entry_ptr = entry;
  228. log_entry_t *log_etnry_ptr;
  229. archive_entry_t *archive_etnry_ptr;
  230. ret = xSemaphoreTake(log_mutex, portMAX_DELAY);
  231. if (ret == pdFALSE)
  232. return ret;
  233. if (entry_ptr->timestamp == 0)
  234. entry_ptr->timestamp = rtc_get_ms();
  235. if (entry_type == LOG_ENTRY)
  236. {
  237. log_etnry_ptr = (log_entry_t*)entry;
  238. log_etnry_ptr->crc = crc_8(entry, sizeof(log_entry_t) - 1);
  239. ret = ringfs_append(&fs_log, entry);
  240. }
  241. else if (entry_type == ARCHIVE_ENTRY)
  242. {
  243. archive_etnry_ptr = (archive_entry_t*)entry;
  244. archive_etnry_ptr->crc = crc_8(entry, sizeof(archive_entry_t) - 1);
  245. ret = ringfs_append(&fs_ch_arch[ch], entry);
  246. }
  247. else ret = -1;
  248. xSemaphoreGive(log_mutex);
  249. return ret;
  250. }
  251. //
  252. uint16_t log_capacity(void)
  253. {
  254. return ringfs_count_exact(&fs_log);
  255. }
  256. //
  257. uint16_t log_arch_capacity(uint8_t ch)
  258. {
  259. return ringfs_count_exact(&fs_ch_arch[ch]);
  260. }
  261. // -------------------------------------------------------------------------- //
  262. // misc
  263. uint8_t crc_8(uint8_t *data, int length)
  264. {
  265. uint8_t crc = 0x00;
  266. uint8_t extract;
  267. uint8_t sum;
  268. for (int i = 0; i < length; i++) {
  269. extract = *data;
  270. for (uint8_t tmp = 8; tmp; tmp--) {
  271. sum = (crc ^ extract) & 0x01;
  272. crc >>= 1;
  273. if (sum)
  274. crc ^= 0x8C;
  275. extract >>= 1;
  276. }
  277. data++;
  278. }
  279. return crc;
  280. }
  281. // -------------------------------------------------------------------------- //
  282. // Tests
  283. // val - 0 - журнал
  284. // val - 1 - архив
  285. // ch - номер канала архива
  286. void log_info(uint8_t val, uint8_t ch)
  287. {
  288. if (val > 1)
  289. return;
  290. struct ringfs *fs = val == 0 ? &fs_log : &fs_ch_arch[ch];
  291. int capacity_flash = 0;
  292. int count_flash = 0;
  293. int count_estimate = 0;
  294. capacity_flash = ringfs_capacity(fs);
  295. count_flash = ringfs_count_exact(fs);
  296. count_estimate = ringfs_count_estimate(fs);
  297. if (val == 0)
  298. {
  299. DBG printf("Log partition capacity: %u\r\n", capacity_flash);
  300. DBG printf("Count log entry: %u\r\n", count_flash);
  301. DBG printf("Estimate count: %u\r\n", count_estimate);
  302. }
  303. else
  304. {
  305. DBG printf("Archive partition capacity: %u\r\n", capacity_flash);
  306. DBG printf("Count archive entry: %u\r\n", count_flash);
  307. DBG printf("Estimate count: %u\r\n", count_estimate);
  308. }
  309. }
  310. // val - 0 - журнал
  311. // val - 1 - архив
  312. // ch - номер канала архива
  313. void log_format(uint8_t val, uint8_t ch)
  314. {
  315. if (val == 0) {
  316. DBG printf("Formating log partition...\r\n");
  317. ringfs_format(&fs_log);
  318. }
  319. else if (val == 1) {
  320. DBG printf("Formating archive partition...\r\n");
  321. ringfs_format(&fs_ch_arch[ch]);
  322. }
  323. }
  324. // Добавить n записей журнала
  325. int log_add_random_entry(uint8_t val, uint32_t cnt_entry, uint8_t ch)
  326. {
  327. int ret;
  328. log_entry_t log_entry = {0};
  329. archive_entry_t archive_entry = {0};
  330. static uint8_t log_index = 0;
  331. static uint32_t archive_index = 0;
  332. if (val == 0)
  333. {
  334. DBG printf("Appending %u archive entries\r\n", cnt_entry);
  335. for (uint32_t i = 0; i < cnt_entry; i++)
  336. {
  337. log_entry.code_type = log_index;
  338. log_entry.code_state = log_index;
  339. log_entry.channel_number = log_index;
  340. log_entry.value = (float)log_index++;
  341. ret = log_append((void*)&log_entry, LOG_ENTRY, 0);
  342. }
  343. DBG printf("Result: %u\r\n", ret);
  344. }
  345. if (val == 1)
  346. {
  347. DBG printf("Appending %u archive entries\r\n", cnt_entry);
  348. for (uint32_t i = 0; i < cnt_entry; i++)
  349. {
  350. archive_entry.input_value = archive_index++;
  351. ret = log_append((void*)&archive_entry, ARCHIVE_ENTRY, ch);
  352. }
  353. DBG printf("Result: %u\r\n", ret);
  354. }
  355. return ret;
  356. }
  357. //
  358. int log_add_entry(log_event_type_t type, log_event_state_t state,
  359. uint8_t channel_number, float value)
  360. {
  361. log_entry_t entry;
  362. if (!log_init_f)
  363. return -1;
  364. entry.timestamp = rtc_get_ms();
  365. entry.code_type = (uint8_t)type;
  366. entry.code_state = (uint8_t)state;
  367. entry.channel_number = channel_number;
  368. entry.value = value;
  369. xQueueSend(log_queue, &entry, 0);
  370. return 0;
  371. }
  372. //
  373. void test_fetch(void)
  374. {
  375. archive_entry_t entry = {0};
  376. log_fetch(&entry, ARCHIVE_ENTRY, 0, portMAX_DELAY);
  377. //printf("\r\n%" PRId64 " [ms]\r\n", rtc_get_ms());
  378. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n", entry.timestamp, entry.input_value, entry.crc);
  379. }
  380. //
  381. void log_archive_state(bool state)
  382. {
  383. archive_state = state;
  384. }
  385. //
  386. void log_log_state(bool state)
  387. {
  388. log_state = state;
  389. }
  390. void archive_task(void *params)
  391. {
  392. int ret = 0;
  393. uint32_t event = 0;
  394. archive_entry_t entry = {0};
  395. EventBits_t bits;
  396. for (;;)
  397. {
  398. bits = xEventGroupWaitBits(archive_event, ARCH_CH_1 | ARCH_CH_2 |
  399. ARCH_CH_3 | ARCH_CH_4 | ARCH_CH_5 |
  400. ARCH_CH_6 | ARCH_CH_7 | ARCH_CH_8,
  401. pdTRUE, pdFALSE, portMAX_DELAY);
  402. for (uint32_t i = 0; i < ARCH_CH_NUMBER; i++)
  403. {
  404. if (bits & (1 << i))
  405. {
  406. DBG printf("Archive event: %u\r\n", (1 << i));
  407. entry.timestamp = 0;
  408. entry.input_value = (uint8_t)input_state_bit;
  409. DBG printf("Append archive entry...");
  410. ret = log_append((void*)&entry, ARCHIVE_ENTRY, i - 1);
  411. if (ret != 0) {
  412. DBG printf("FAIL\r\n");
  413. }
  414. else {
  415. DBG printf("OK\r\n");
  416. }
  417. }
  418. }
  419. #if 0
  420. if ((!archive_state) || (!archive_init_f)) {
  421. vTaskDelay(1000);
  422. continue;
  423. }
  424. entry.timestamp = 0;
  425. entry.input_value = (uint8_t)input_state_bit;
  426. DBG printf("Append archive entry...");
  427. ret = log_append((void*)&entry, ARCHIVE_ENTRY, 0);
  428. if (ret != 0) {
  429. DBG printf("FAIL\r\n");
  430. }
  431. else {
  432. DBG printf("OK\r\n");
  433. }
  434. vTaskDelay(settings.period_archive*1000);
  435. #endif
  436. }
  437. }
  438. //
  439. void log_task(void *params)
  440. {
  441. int ret;
  442. log_entry_t entry;
  443. for (;;)
  444. {
  445. if (xQueueReceive(log_queue, &entry, portMAX_DELAY) == pdTRUE)
  446. {
  447. DBG printf("Try append LOG entry... ");
  448. ret = log_append((void*)&entry, LOG_ENTRY, 0);
  449. DBG printf("Result: %i\r\n", ret);
  450. }
  451. }
  452. }
  453. // Вызывается в прерывании таймера с частотой 10 Гц
  454. void log_check_archive_cnt(void)
  455. {
  456. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  457. for (uint8_t i = 0; i < ARCH_CH_NUMBER; i++)
  458. {
  459. if (archive_cnt[i]++ == 10*settings.period_archive[i])
  460. {
  461. archive_cnt[i] = 0;
  462. // Номер канала с 0..7
  463. printf("Send event: %u\r\n", 1 << i);
  464. xEventGroupSetBitsFromISR(archive_event, 1 << i, &xHigherPriorityTaskWoken);
  465. }
  466. }
  467. }