log.c 15 KB

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