#include "stm32f1xx_hal.h" #define ALLOC_GLOBALS #include "stm32fxx_STUlib.h" #include "motor_control.h" #include "power12V_driver.h" #include "tasks.h" #include "can.h" #include "gpio.h" #include "iwdg.h" #include "app_loader.h" #include "tim.h" #include "pwm_driver.h" #include "uart_process.h" #include "can_process.h" uint8_t FIT_testflag=1; //static uint32_t sRomTestData=0x55555555; //0x55555554 CRC_HandleTypeDef CrcHandle = {0}; void StartUpClock_Config_Reset(void); void STU_delay_ms(uint16_t time); #ifdef __CC_ARM /* KEIL Compiler */ void $Sub$$main(void) { __disable_irq(); if ( CRC_FLAG != 0xAAu ) { STU_StartUp(); /* trick to call StartUp before main entry */ } CRC_FLAG = 0u; __enable_irq(); $Super$$main(); } #endif /* __CC_ARM */ void STU_StartUp(void) { uint32_t crc_result; uint32_t index; FIT_testflag = 10; /* Reset of all peripherals, Initializes the Flash interface and the Systick */ HAL_Init(); /*时钟倍频到72Mhz*/ StartUpClock_Config(); // /*FIT*/ // FIT_rom_test(); /* Initialization of counters for control flow monitoring */ init_control_flow(); /*--------------------------------------------------------------------------*/ /*------------------- CPU registers test --------------------*/ /*--------------------------------------------------------------------------*/ control_flow_call(CPU_TEST_CALLER); STU_CpuTestStartup(); control_flow_resume(CPU_TEST_CALLER); STU_CPURegsAddressing(); FIT_testflag = 12; /*--------------------------------------------------------------------------*/ /*--------------------- ROM CRC check ------------------------*/ /*--------------------------------------------------------------------------*/ control_flow_call(CRC32_TEST_CALLER); CrcHandle.Instance = CRC; __HAL_CRC_DR_RESET(&CrcHandle); HAL_CRC_Init(&CrcHandle); for(index = 0; index < (uint32_t)ROM_SIZEinWORDS; index++) { CRC->DR = (*((uint32_t *)ROM_START + index)); } crc_result = CRC->DR; if(crc_result != *(uint32_t *)(&REF_CRC32)) { /*ROM err*/ FailSafePOR(); } control_flow_resume(CRC32_TEST_CALLER); FIT_testflag = 14; /*--------------------------------------------------------------------------*/ /* --------------------- Instruct and decode test -------------------*/ /*--------------------------------------------------------------------------*/ control_flow_call(INSTRUCT_AND_DECODE_TEST_CALLER); if(STU_InstructAndDecode() == ERROR) { FailSafePOR(); } control_flow_resume(INSTRUCT_AND_DECODE_TEST_CALLER); FIT_testflag = 16; /*--------------------------------------------------------------------------*/ /* --------------------- Addressing test -------------------*/ /*--------------------------------------------------------------------------*/ control_flow_call(ADDRESSING_TEST_CALLER); if(STU_AddressingTest() == ERROR) { FailSafePOR(); } control_flow_resume(ADDRESSING_TEST_CALLER); FIT_testflag = 18; /*--------------------------------------------------------------------------*/ /* --------------------- Peripheral Registers test -------------------*/ /*--------------------------------------------------------------------------*/ control_flow_call(PERIPHERAL_REGS_TEST_CALLER); if(STU_PeripheralRegistersTest() == ERROR) { FailSafePOR(); } FIT_testflag = 20; /*做两次测试,确保0->1,1->0均能有效翻转*/ if(STU_PeripheralRegistersTest() == ERROR) { FailSafePOR(); } control_flow_resume(PERIPHERAL_REGS_TEST_CALLER); FIT_testflag = 22; /*--------------------------------------------------------------------------*/ /* Verify Control flow before RAM init (which clears Ctrl flow counters) */ /*--------------------------------------------------------------------------*/ if (control_flow_check_point(STARTUP_CHECK) == ERROR) { FailSafePOR(); } FIT_testflag = 24; /*--------------------------------------------------------------------------*/ /* --------------------- RAM functional test -------------------*/ /*--------------------------------------------------------------------------*/ //__disable_irq(); if(STU_FullRamWalkpat((uint32_t *)RAM_START ,RAM_SIZE) == ERROR) { FailSafePOR(); } //__enable_irq(); /*复位时钟到默认状态*/ StartUpClock_Config_Reset(); // FIT_testflag = 5; GotoCompilerStartUp(); } void StartUpClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInit; /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.LSIState = RCC_LSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure the Systick interrupt time */ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); /**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); } /** * @brief System Clock Configuration * @retval None */ void StartUpClock_Config_Reset(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure the Systick interrupt time */ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); /**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); } /** * @brief Verifies the consistency and value of control flow counters * @param : check value of the positive counter * @retval : ErrorStatus (SUCCESS, ERROR) */ ErrorStatus control_flow_check_point(uint32_t chck) { ErrorStatus Result= SUCCESS; if ((CtrlFlowCnt != (chck)) || ((CtrlFlowCnt ^ CtrlFlowCntInv) != 0xFFFFFFFFuL)) { Result= ERROR; } return(Result); } void FailSafePOR(void) { /*上电自检错误时需要初始化如下内容*/ IAP_Init(); MX_GPIO_Init(); MX_TIM1_Init(); /*开启总中断,系统滴答时钟才能计时*/ __enable_irq(); /*RAM全检查失败后,此变量需要重置,否则影响CAN初始化*/ hcan.State = HAL_CAN_STATE_RESET; MX_CAN_Init(); __disable_irq(); #if DEBUG MX_IWDG_Init(); #endif /*停止电机,关PWM定时器和IO口*/ Pwm_Timer_Stop(); Disable_PwmGpio_Out(); //MCU故障 MC_ErrorCode.Code = 0; MC_ErrorCode.ERROR_Bit.Fault_MCU = 1; while(1) { SendData(ID_MC_BC, MODE_REPORT, 0x1104, (uint8_t*)&MC_ErrorCode.Code); STU_delay_ms(200); //看门狗清零 #if DEBUG HAL_IWDG_Refresh(&hiwdg); #endif } } void STU_delay_ms(uint16_t time) { uint16_t i=0; while(time--) { i=7500; //72MHZ主频,1ms while(i--) ; } }