log.c 11 KB

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