log.c 15 KB

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