main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. //taskENTER_CRITICAL();
  61. xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  62. xTaskCreate(input_task, "input_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  63. xTaskCreate(mux_task, "mux_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  64. #if defined (MAI_12)
  65. xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  66. #endif
  67. #if defined (MAO_8)
  68. xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  69. #endif
  70. //taskEXIT_CRITICAL();
  71. vTaskStartScheduler();
  72. while (1) {}
  73. }
  74. void init_task(void *argument)
  75. {
  76. // -------------------------------------------------------------------------- //
  77. // Software watchdog
  78. gpio_wdt_init();
  79. xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
  80. // -------------------------------------------------------------------------
  81. // CLI
  82. sbsTerminal.configure();
  83. terminalUsartBridge.configure();
  84. // -------------------------------------------------------------------------- //
  85. // Настройки. Загрузка и проверка.
  86. // Мьютекс для работы с настройками
  87. init_settings();
  88. // Системные настройки и копия
  89. sys_settings_load(&sys_settings);
  90. memcpy(&temp_sys_settings, &sys_settings, sizeof(sys_settings_t));
  91. // Основные настройки
  92. settings_load(&settings);
  93. // Настройки параметров порта Modbus
  94. settings_set_modbus_params(settings.com_settings.mb_port);
  95. // -------------------------------------------------------------------------- //
  96. // Безопасный режим, входы, выходы
  97. save_mode_init();
  98. // TODO Для теста отключаем безопасный режим работы
  99. save_mode_set(false);
  100. io_port_init();
  101. //in_exint_init();
  102. // -------------------------------------------------------------------------- //
  103. // Кнопки [SET, RESET]
  104. button_init();
  105. xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  106. // -------------------------------------------------------------------------- //
  107. // Uptime
  108. uptime_init();
  109. // -------------------------------------------------------------------------- //
  110. // RTC
  111. TM_RTC_Init();
  112. rtc_subtim_init();
  113. // -------------------------------------------------------------------------- //
  114. // Мультиплексор
  115. mux_gpio_init();
  116. // -------------------------------------------------------------------------- //
  117. // Modbus
  118. mb_init();
  119. // -------------------------------------------------------------------------- //
  120. // Базовая инициализация входов/выходов
  121. // TODO потом брать значения из настроек
  122. io_init();
  123. // -------------------------------------------------------------------------- //
  124. // Сброс счетчика попыток загрузок
  125. update_reset_boot_try();
  126. // -------------------------------------------------------------------------- //
  127. // Тесты
  128. //pwm_test(); // тесы PWM
  129. gpio_get_rev();
  130. // -------------------------------------------------------------------------- //
  131. // Сдвиговые регистры и мультиплексоры
  132. //sh_init();
  133. // Тесты аналоговых входов
  134. //ai_connect_test();
  135. //ai_mode_test();
  136. //ai_connect_channel(V_ISO);
  137. //ai_connect_channel(AN_INP_1);
  138. //ai_connect_channel(AN_INP_7);
  139. // -------------------------------------------------------------------------- //
  140. // DAC
  141. /*
  142. dac_gpio_init();
  143. dac_test(CH_DAC_1, 30000);
  144. */
  145. /*
  146. dac_test(CH_DAC_2, 10000);
  147. dac_test(CH_DAC_3, 10000);
  148. dac_test(CH_DAC_4, 10000);
  149. */
  150. /*
  151. dac_test(CH_DAC_1, 10000);
  152. dac_test(CH_DAC_2, 10000);
  153. dac_test(CH_DAC_3, 10000);
  154. dac_test(CH_DAC_4, 10000);
  155. */
  156. // -------------------------------------------------------------------------- //
  157. // USB (RNDIS class)
  158. #ifdef USB_RNDIS
  159. usb_clock48m_select(USB_CLK_HEXT);
  160. crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
  161. usb_init();
  162. usb_eth_init();
  163. #endif
  164. // -------------------------------------------------------------------------- //
  165. // SPI flash, журнал событий, архив
  166. common_spi_init();
  167. spi_flash_init();
  168. log_init(false);
  169. // -------------------------------------------------------------------------- //
  170. // Статистика FreeRTOS
  171. utl_stat_timer_init();
  172. // -------------------------------------------------------------------------- //
  173. // EVENT. Обновление FW.
  174. uint16_t update_status = bpr_data_read(BACKUP_UPDATE_STATUS);
  175. if (update_status == IAP_UPDATE_OK)
  176. log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_OK, 0, atof(FW_VERSION));
  177. else if (update_status == IAP_UPDATE_ERROR)
  178. log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_ERR, 0, atof(FW_VERSION));
  179. bpr_data_write(BACKUP_UPDATE_STATUS, IAP_UPDATE_NO);
  180. // -------------------------------------------------------------------------- //
  181. // EVENT. Включение питания/перезагрузка.
  182. log_add_entry(LOG_SYSTEM_BOOT, (log_event_state_t)0, 0, 0);
  183. // -------------------------------------------------------------------------- //
  184. // Удаляем стартовую задачу
  185. vTaskDelete(NULL);
  186. // -------------------------------------------------------------------------- //
  187. // Тесты
  188. //usb_eth_init();
  189. //sys_clear();
  190. //mux_led_test_init();
  191. #if 0
  192. // Настройки по умолчанию
  193. settings_set_all_default();
  194. settings_save(&settings);
  195. #endif
  196. // -------------------------------------------------------------------------- //
  197. // Отдельный таск для предварительных тестов
  198. // Разное
  199. //xTaskCreate(test_hw_task, "hw_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  200. // Тесты GPIO
  201. //xTaskCreate(test_gpio, "gpio_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  202. }