main.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include "at32f403a_407.h"
  2. #include "at32f403a_407_board.h"
  3. #include "at32f403a_407_clock.h"
  4. #include "common_config.h"
  5. #include "FreeRTOS.h"
  6. #include "task.h"
  7. #include "queue.h"
  8. #include "semphr.h"
  9. extern "C" {
  10. #include "usb_eth.h"
  11. #include "mux.h"
  12. #include "misc.h"
  13. #include "spi_common.h"
  14. #include "user_fatfs.h"
  15. #include "spi_flash.h"
  16. #include "usb_eth.h"
  17. #include "extended_sram.h"
  18. #include "modbus.h"
  19. #include "common_gpio.h"
  20. #include "io.h"
  21. #include "sys_api.h"
  22. #include "settings_api.h"
  23. #include "update.h"
  24. #include "uptime.h"
  25. #include "rtc.h"
  26. #include "mb.h"
  27. #include "io_utils.h"
  28. #include "digital_input.h"
  29. #include "digital_output.h"
  30. #include "buttons.h"
  31. #include "adc_transport.h"
  32. #include "shift_reg.h"
  33. #include "analog_input.h"
  34. #include "analog_output.h"
  35. #include "dac_transport.h"
  36. #include "log.h"
  37. #include "swap.h"
  38. #include "utility.h"
  39. #include "soft_test.h"
  40. #include "soft_wdt.h"
  41. #include "usb_clock.h"
  42. #include "filter.h"
  43. #include "modbus_ai_params.h"
  44. #include "rtc_battery.h"
  45. #include "monitoring.h"
  46. }
  47. #include "terminal_sbs.h"
  48. #include "terminal_usartbridge.h"
  49. #include <stdio.h>
  50. #include <stdbool.h>
  51. #include <string.h>
  52. #include <stdlib.h>
  53. void init_task(void *argument);
  54. //
  55. int main(void)
  56. {
  57. __disable_irq();
  58. nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x08021000);
  59. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  60. __enable_irq();
  61. crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
  62. extend_SRAM();
  63. system_clock_config();
  64. delay_init();
  65. xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  66. vTaskStartScheduler();
  67. while (1) {}
  68. }
  69. void init_task(void *argument)
  70. {
  71. uint8_t ret;
  72. // -------------------------------------------------------------------------- //
  73. // Software watchdog
  74. gpio_wdt_init();
  75. xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
  76. // -------------------------------------------------------------------------
  77. // CLI
  78. sbsTerminal.configure();
  79. terminalUsartBridge.configure();
  80. // -------------------------------------------------------------------------- //
  81. // Настройки. Загрузка и проверка.
  82. // Мьютекс для работы с настройками
  83. init_settings();
  84. // Системные настройки и копия
  85. sys_settings_load(&sys_settings);
  86. memcpy(&temp_sys_settings, &sys_settings, sizeof(sys_settings_t));
  87. // Основные настройки
  88. settings_load(&settings);
  89. // Настройки параметров порта Modbus
  90. settings_set_modbus_params(settings.com_settings.mb_port);
  91. #if 1
  92. // Только для теста
  93. settings_set_all_default();
  94. settings_save(&settings);
  95. #endif
  96. // -------------------------------------------------------------------------- //
  97. // Безопасный режим, входы, выходы
  98. #if defined (MDIO_88) || (MAO_4)
  99. //settings.save_mode = false;
  100. save_mode_init();
  101. #endif
  102. // -------------------------------------------------------------------------- //
  103. // Включить тактирование портов
  104. io_port_enable();
  105. // -------------------------------------------------------------------------- //
  106. // Цифровые входы/выходы
  107. #if defined (MDIO_88)
  108. di_init();
  109. do_init();
  110. xTaskCreate(di_task, "input_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  111. xTaskCreate(do_task, "output_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  112. #endif
  113. #if defined (MAI_12)
  114. ai_init();
  115. xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  116. xTaskCreate(adc_alarm_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  117. #endif
  118. #if defined (MAO_4)
  119. ao_init();
  120. xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  121. #endif
  122. // -------------------------------------------------------------------------- //
  123. // Кнопки [SET, RESET]
  124. button_init();
  125. xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  126. // -------------------------------------------------------------------------- //
  127. // Uptime
  128. uptime_init();
  129. // -------------------------------------------------------------------------- //
  130. // RTC, ADC для измерения напряжения батареи.
  131. ret = TM_RTC_Init();
  132. if (bpr_data_read(BACKUP_RTC_UPDATE) != 0x1234)
  133. mon_set_err(MON_RTC, 1);
  134. else
  135. mon_set_err(MON_RTC, 0);
  136. rtc_subtim_init();
  137. adc_battery_init();
  138. xTaskCreate(adc_battery_task, "adc_battery_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  139. // -------------------------------------------------------------------------- //
  140. // Мультиплексор
  141. #if 1
  142. mux_gpio_init();
  143. xTaskCreate(mux_task, "mux_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  144. #endif
  145. // -------------------------------------------------------------------------- //
  146. // Modbus
  147. mb_init();
  148. // -------------------------------------------------------------------------- //
  149. // Сброс счетчика попыток загрузок
  150. update_reset_boot_try();
  151. // -------------------------------------------------------------------------- //
  152. // Считать ревизию платы (состояние пинов)
  153. gpio_update_rev();
  154. // -------------------------------------------------------------------------- //
  155. // SPI flash, журнал событий, архив
  156. #if 0
  157. common_spi_init();
  158. spi_flash_init();
  159. log_init(false);
  160. #endif
  161. // -------------------------------------------------------------------------- //
  162. // Статистика FreeRTOS
  163. utl_stat_timer_init();
  164. // -------------------------------------------------------------------------- //
  165. // EVENT. Обновление FW.
  166. #if 0
  167. uint16_t update_status = bpr_data_read(BACKUP_UPDATE_STATUS);
  168. if (update_status == IAP_UPDATE_OK)
  169. log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_OK, 0, atof(FW_VERSION));
  170. else if (update_status == IAP_UPDATE_ERROR)
  171. log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_ERR, 0, atof(FW_VERSION));
  172. bpr_data_write(BACKUP_UPDATE_STATUS, IAP_UPDATE_NO);
  173. // -------------------------------------------------------------------------- //
  174. // EVENT. Включение питания/перезагрузка.
  175. log_add_entry(LOG_SYSTEM_BOOT, (log_event_state_t)0, 0, 0);
  176. #endif
  177. // -------------------------------------------------------------------------- //
  178. // USB (RNDIS class)
  179. #ifdef USB_RNDIS
  180. usb_clock48m_select(USB_CLK_HEXT);
  181. crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
  182. usb_init();
  183. usb_eth_init();
  184. #endif
  185. // -------------------------------------------------------------------------- //
  186. // Тесты
  187. //usb_eth_init();
  188. //sys_clear();
  189. //mux_led_test_init();
  190. //pwm_test();
  191. // -------------------------------------------------------------------------- //
  192. // Сдвиговые регистры и мультиплексоры
  193. //sh_init();
  194. // Тесты аналоговых входов
  195. //ai_connect_test();
  196. //ai_mode_test();
  197. //ai_connect_channel(V_ISO);
  198. //ai_connect_channel(AN_INP_1);
  199. //ai_connect_channel(AN_INP_7);
  200. // -------------------------------------------------------------------------- //
  201. // DAC
  202. /*
  203. dac_gpio_init();
  204. dac_test(CH_DAC_1, 30000);
  205. */
  206. /*
  207. dac_test(CH_DAC_2, 10000);
  208. dac_test(CH_DAC_3, 10000);
  209. dac_test(CH_DAC_4, 10000);
  210. */
  211. /*
  212. dac_test(CH_DAC_1, 10000);
  213. dac_test(CH_DAC_2, 10000);
  214. dac_test(CH_DAC_3, 10000);
  215. dac_test(CH_DAC_4, 10000);
  216. */
  217. #if 0
  218. // Настройки по умолчанию
  219. settings_set_all_default();
  220. settings_save(&settings);
  221. #endif
  222. // -------------------------------------------------------------------------- //
  223. // Отдельный таск для предварительных тестов
  224. // Разное
  225. //xTaskCreate(test_hw_task, "hw_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  226. // Тесты GPIO
  227. //xTaskCreate(test_gpio, "gpio_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  228. //ai_processing();
  229. //average_filter_init();
  230. // -------------------------------------------------------------------------- //
  231. // Удаляем стартовую задачу
  232. vTaskDelete(NULL);
  233. }