log.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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(0)
  20. static bool archive_state = true;
  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. // ---------------------------------------------------------------------- //
  45. // Журнал
  46. ringfs_flash_log.sector_size = spi_flash_desc.sector_size;
  47. ringfs_flash_log.sector_count = LOG_FLASH_SECTOR_COUNT;
  48. ringfs_init(&fs_log, &ringfs_flash_log, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  49. if (format || ringfs_scan(&fs_log) != 0) {
  50. DBG printf("FAT1 false\r\n");
  51. ringfs_format(&fs_log);
  52. }
  53. DBG printf("FAT1 true\r\n");
  54. #if defined (MDIO_88)
  55. log_dio_archive_init();
  56. #endif
  57. #if defined (MAI_12)
  58. log_ai_archive_init();
  59. #endif
  60. // ---------------------------------------------------------------------- //
  61. log_mutex = xSemaphoreCreateMutex();
  62. log_entries_capacity = ringfs_capacity(&fs_log);
  63. #if defined (MDIO_88) || (MAI_12)
  64. archive_entries_capacity = ringfs_capacity(&fs_ch_arch[0]);
  65. // Event
  66. archive_event = xEventGroupCreate();
  67. xTaskCreate(archive_task, "archive_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  68. #endif
  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. #if defined (MDIO_88) || (MAI_12)
  114. else if (entry_type == ARCHIVE_ENTRY)
  115. ret = ringfs_fetch(&fs_ch_arch[ch], entry);
  116. #endif
  117. else ret = -1;
  118. xSemaphoreGive(log_mutex);
  119. return ret;
  120. }
  121. //
  122. int log_discard(void *entry, entry_type_t entry_type, uint8_t ch, uint32_t timeout)
  123. {
  124. int ret;
  125. ret = xSemaphoreTake(log_mutex, (TickType_t)timeout);
  126. if (ret == pdFALSE)
  127. return ret;
  128. if (entry_type == LOG_ENTRY)
  129. ret = ringfs_discard(&fs_log);
  130. #if defined (MDIO_88) || (MAI_12)
  131. else if (entry_type == ARCHIVE_ENTRY)
  132. ret = ringfs_discard(&fs_ch_arch[ch]);
  133. #endif
  134. else ret = -1;
  135. xSemaphoreGive(log_mutex);
  136. return ret;
  137. }
  138. //
  139. int log_append(void *entry, entry_type_t entry_type, uint8_t ch)
  140. {
  141. int ret;
  142. TM_RTC_t time;
  143. common_entry_t *entry_ptr = entry;
  144. log_entry_t *log_etnry_ptr;
  145. archive_entry_t *archive_etnry_ptr;
  146. ret = xSemaphoreTake(log_mutex, portMAX_DELAY);
  147. if (ret == pdFALSE)
  148. return ret;
  149. if (entry_ptr->timestamp == 0)
  150. entry_ptr->timestamp = rtc_get_ms();
  151. if (entry_type == LOG_ENTRY)
  152. {
  153. log_etnry_ptr = (log_entry_t*)entry;
  154. log_etnry_ptr->crc = crc_8(entry, sizeof(log_entry_t) - 1);
  155. ret = ringfs_append(&fs_log, entry);
  156. }
  157. #if defined (MDIO_88) || (MAI_12)
  158. else if (entry_type == ARCHIVE_ENTRY)
  159. {
  160. archive_etnry_ptr = (archive_entry_t*)entry;
  161. archive_etnry_ptr->crc = crc_8(entry, sizeof(archive_entry_t) - 1);
  162. ret = ringfs_append(&fs_ch_arch[ch], entry);
  163. }
  164. #endif
  165. else ret = -1;
  166. xSemaphoreGive(log_mutex);
  167. return ret;
  168. }
  169. //
  170. uint16_t log_capacity(void)
  171. {
  172. return ringfs_count_exact(&fs_log);
  173. }
  174. //
  175. uint16_t log_arch_capacity(uint8_t ch)
  176. {
  177. #if defined (MDIO_88) || (MAI_12)
  178. return ringfs_count_exact(&fs_ch_arch[ch]);
  179. #endif
  180. }
  181. // -------------------------------------------------------------------------- //
  182. // misc
  183. uint8_t crc_8(uint8_t *data, int length)
  184. {
  185. uint8_t crc = 0x00;
  186. uint8_t extract;
  187. uint8_t sum;
  188. for (int i = 0; i < length; i++) {
  189. extract = *data;
  190. for (uint8_t tmp = 8; tmp; tmp--) {
  191. sum = (crc ^ extract) & 0x01;
  192. crc >>= 1;
  193. if (sum)
  194. crc ^= 0x8C;
  195. extract >>= 1;
  196. }
  197. data++;
  198. }
  199. return crc;
  200. }
  201. // -------------------------------------------------------------------------- //
  202. // Tests
  203. // val - 0 - журнал
  204. // val - 1 - архив
  205. // ch - номер канала архива
  206. void log_info(uint8_t val, uint8_t ch)
  207. {
  208. if (val > 1)
  209. return;
  210. #if defined (MDIO_88) || (MAI_12)
  211. struct ringfs *fs = val == 0 ? &fs_log : &fs_ch_arch[ch];
  212. #else
  213. struct ringfs *fs = &fs_log;
  214. #endif
  215. int capacity_flash = 0;
  216. int count_flash = 0;
  217. int count_estimate = 0;
  218. capacity_flash = ringfs_capacity(fs);
  219. count_flash = ringfs_count_exact(fs);
  220. count_estimate = ringfs_count_estimate(fs);
  221. if (val == 0)
  222. {
  223. DBG printf("Log partition capacity: %u\r\n", capacity_flash);
  224. DBG printf("Count log entry: %u\r\n", count_flash);
  225. DBG printf("Estimate count: %u\r\n", count_estimate);
  226. }
  227. else
  228. {
  229. DBG printf("Archive partition capacity: %u\r\n", capacity_flash);
  230. DBG printf("Count archive entry: %u\r\n", count_flash);
  231. DBG printf("Estimate count: %u\r\n", count_estimate);
  232. }
  233. }
  234. // val - 0 - журнал
  235. // val - 1 - архив
  236. // ch - номер канала архива
  237. void log_format(uint8_t val, uint8_t ch)
  238. {
  239. if (val == 0) {
  240. DBG printf("Formating log partition...\r\n");
  241. ringfs_format(&fs_log);
  242. }
  243. #if defined (MDIO_88) || (MAI_12)
  244. else if (val == 1) {
  245. DBG printf("Formating archive partition...\r\n");
  246. ringfs_format(&fs_ch_arch[ch]);
  247. }
  248. #endif
  249. }
  250. // Добавить n записей журнала
  251. int log_add_random_entry(uint8_t val, uint32_t cnt_entry, uint8_t ch)
  252. {
  253. int ret;
  254. log_entry_t log_entry = {0};
  255. archive_entry_t archive_entry = {0};
  256. static uint8_t log_index = 0;
  257. static uint32_t archive_index = 0;
  258. if (val == 0)
  259. {
  260. DBG printf("Appending %u archive entries\r\n", cnt_entry);
  261. for (uint32_t i = 0; i < cnt_entry; i++)
  262. {
  263. log_entry.code_type = log_index;
  264. log_entry.code_state = log_index;
  265. log_entry.channel_number = log_index;
  266. log_entry.value = (float)log_index++;
  267. ret = log_append((void*)&log_entry, LOG_ENTRY, 0);
  268. }
  269. DBG printf("Result: %u\r\n", ret);
  270. }
  271. if (val == 1)
  272. {
  273. DBG printf("Appending %u archive entries\r\n", cnt_entry);
  274. for (uint32_t i = 0; i < cnt_entry; i++)
  275. {
  276. archive_entry.input_value = archive_index++;
  277. ret = log_append((void*)&archive_entry, ARCHIVE_ENTRY, ch);
  278. }
  279. DBG printf("Result: %u\r\n", ret);
  280. }
  281. return ret;
  282. }
  283. //
  284. int log_add_entry(log_event_type_t type, log_event_state_t state,
  285. uint8_t channel_number, float value)
  286. {
  287. log_entry_t entry;
  288. if (!log_init_f)
  289. return -1;
  290. entry.timestamp = rtc_get_ms();
  291. entry.code_type = (uint8_t)type;
  292. entry.code_state = (uint8_t)state;
  293. entry.channel_number = channel_number;
  294. entry.value = value;
  295. xQueueSend(log_queue, &entry, 0);
  296. return 0;
  297. }
  298. //
  299. void test_fetch(void)
  300. {
  301. archive_entry_t entry = {0};
  302. log_fetch(&entry, ARCHIVE_ENTRY, 0, portMAX_DELAY);
  303. //printf("\r\n%" PRId64 " [ms]\r\n", rtc_get_ms());
  304. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n", entry.timestamp, entry.input_value, entry.crc);
  305. }
  306. //
  307. void log_archive_state(bool state)
  308. {
  309. archive_state = state;
  310. }
  311. //
  312. void log_log_state(bool state)
  313. {
  314. log_state = state;
  315. }
  316. void archive_task(void *params)
  317. {
  318. int ret = 0;
  319. uint32_t event = 0;
  320. archive_entry_t entry = {0};
  321. EventBits_t bits;
  322. uint8_t channel_number = log_get_arch_channel_number();
  323. for (;;)
  324. {
  325. log_archive_task_device();
  326. }
  327. }
  328. //
  329. void log_task(void *params)
  330. {
  331. int ret;
  332. log_entry_t entry;
  333. for (;;)
  334. {
  335. if (xQueueReceive(log_queue, &entry, portMAX_DELAY) == pdTRUE)
  336. {
  337. DBG printf("Try append LOG entry... ");
  338. ret = log_append((void*)&entry, LOG_ENTRY, 0);
  339. DBG printf("Result: %i\r\n", ret);
  340. }
  341. }
  342. }
  343. // Вызывается в прерывании таймера с частотой 10 Гц
  344. // TODO
  345. void log_check_archive_cnt(void)
  346. {
  347. #if defined (MDIO_88) || (MAI_12)
  348. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  349. uint8_t channel_number = log_get_arch_channel_number();
  350. for (uint8_t i = 0; i < channel_number; i++)
  351. {
  352. if (archive_cnt[i]++ >= 10*settings.period_archive[i])
  353. {
  354. archive_cnt[i] = 0;
  355. if (log_is_channel_on(i))
  356. {
  357. DBG printf("Send event: %u\r\n", 1 << i);
  358. xEventGroupSetBitsFromISR(archive_event, 1 << i, &xHigherPriorityTaskWoken);
  359. }
  360. }
  361. }
  362. #endif
  363. }