main.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "input.h"
  22. #include "output.h"
  23. #include "sys_api.h"
  24. #include "settings_api.h"
  25. #include "update.h"
  26. #include "uptime.h"
  27. #include "rtc.h"
  28. #include "mb.h"
  29. #include "io_utils.h"
  30. #include "buttons.h"
  31. #include "adc_transport.h"
  32. #include "shift_reg.h"
  33. #include "analog_input.h"
  34. #include "dac_transport.h"
  35. #include "log.h"
  36. #include "swap.h"
  37. #include "utility.h"
  38. #include "soft_test.h"
  39. #include "soft_wdt.h"
  40. #include "usb_clock.h"
  41. }
  42. #include "terminal_sbs.h"
  43. #include "terminal_usartbridge.h"
  44. #include <stdio.h>
  45. #include <stdbool.h>
  46. #include <string.h>
  47. #include <stdlib.h>
  48. void init_task(void *argument);
  49. //
  50. int main(void)
  51. {
  52. __disable_irq();
  53. nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x08021000);
  54. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  55. __enable_irq();
  56. crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
  57. extend_SRAM();
  58. system_clock_config();
  59. delay_init();
  60. xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  61. #if defined (MAI_12)
  62. xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  63. #endif
  64. #if defined (MAO_8)
  65. xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  66. #endif
  67. vTaskStartScheduler();
  68. while (1) {}
  69. }
  70. void init_task(void *argument)
  71. {
  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. // -------------------------------------------------------------------------- //
  92. // Безопасный режим, входы, выходы
  93. save_mode_init();
  94. save_mode_set(true);
  95. io_port_init();
  96. //in_exint_init();
  97. // -------------------------------------------------------------------------- //
  98. // Кнопки [SET, RESET]
  99. button_init();
  100. xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  101. // -------------------------------------------------------------------------- //
  102. // Uptime
  103. uptime_init();
  104. // -------------------------------------------------------------------------- //
  105. // RTC
  106. TM_RTC_Init();
  107. rtc_subtim_init();
  108. // -------------------------------------------------------------------------- //
  109. // Мультиплексор
  110. mux_gpio_init();
  111. xTaskCreate(mux_task, "mux_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  112. // -------------------------------------------------------------------------- //
  113. // Базовая инициализация входов/выходов
  114. // TODO потом брать значения из настроек
  115. io_init();
  116. xTaskCreate(input_task, "input_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  117. // -------------------------------------------------------------------------- //
  118. // Modbus
  119. mb_init();
  120. // -------------------------------------------------------------------------- //
  121. // Сброс счетчика попыток загрузок
  122. update_reset_boot_try();
  123. // -------------------------------------------------------------------------- //
  124. // Ревизия платы
  125. gpio_get_rev();
  126. // -------------------------------------------------------------------------- //
  127. // SPI flash, журнал событий, архив
  128. common_spi_init();
  129. spi_flash_init();
  130. log_init(false);
  131. // -------------------------------------------------------------------------- //
  132. // Статистика FreeRTOS
  133. utl_stat_timer_init();
  134. // -------------------------------------------------------------------------- //
  135. // EVENT. Обновление FW.
  136. uint16_t update_status = bpr_data_read(BACKUP_UPDATE_STATUS);
  137. if (update_status == IAP_UPDATE_OK)
  138. log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_OK, 0, atof(FW_VERSION));
  139. else if (update_status == IAP_UPDATE_ERROR)
  140. log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_ERR, 0, atof(FW_VERSION));
  141. bpr_data_write(BACKUP_UPDATE_STATUS, IAP_UPDATE_NO);
  142. // -------------------------------------------------------------------------- //
  143. // EVENT. Включение питания/перезагрузка.
  144. log_add_entry(LOG_SYSTEM_BOOT, (log_event_state_t)0, 0, 0);
  145. // -------------------------------------------------------------------------- //
  146. // USB (RNDIS class)
  147. #ifdef USB_RNDIS
  148. usb_clock48m_select(USB_CLK_HEXT);
  149. crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
  150. usb_init();
  151. usb_eth_init();
  152. #endif
  153. // Удаляем стартовую задачу
  154. vTaskDelete(NULL);
  155. // -------------------------------------------------------------------------- //
  156. // Тесты
  157. //usb_eth_init();
  158. //sys_clear();
  159. //mux_led_test_init();
  160. //pwm_test();
  161. // -------------------------------------------------------------------------- //
  162. // Сдвиговые регистры и мультиплексоры
  163. //sh_init();
  164. // Тесты аналоговых входов
  165. //ai_connect_test();
  166. //ai_mode_test();
  167. //ai_connect_channel(V_ISO);
  168. //ai_connect_channel(AN_INP_1);
  169. //ai_connect_channel(AN_INP_7);
  170. // -------------------------------------------------------------------------- //
  171. // DAC
  172. /*
  173. dac_gpio_init();
  174. dac_test(CH_DAC_1, 30000);
  175. */
  176. /*
  177. dac_test(CH_DAC_2, 10000);
  178. dac_test(CH_DAC_3, 10000);
  179. dac_test(CH_DAC_4, 10000);
  180. */
  181. /*
  182. dac_test(CH_DAC_1, 10000);
  183. dac_test(CH_DAC_2, 10000);
  184. dac_test(CH_DAC_3, 10000);
  185. dac_test(CH_DAC_4, 10000);
  186. */
  187. #if 0
  188. // Настройки по умолчанию
  189. settings_set_all_default();
  190. settings_save(&settings);
  191. #endif
  192. // -------------------------------------------------------------------------- //
  193. // Отдельный таск для предварительных тестов
  194. // Разное
  195. //xTaskCreate(test_hw_task, "hw_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  196. // Тесты GPIO
  197. //xTaskCreate(test_gpio, "gpio_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  198. }