- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "i2c.h"
- #include "spi.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "grbl.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- // Declare system global variable structure
- system_t sys;
- int32_t sys_position[N_AXIS]; // Real-time machine (aka home) position vector in steps.
- int32_t sys_probe_position[N_AXIS]; // Last probe position in machine coordinates and steps.
- volatile uint8_t sys_probe_state; // Probing state value. Used to coordinate the probing cycle with stepper ISR.
- volatile uint8_t sys_rt_exec_state; // Global realtime executor bitflag variable for state management. See EXEC bitmasks.
- volatile uint8_t sys_rt_exec_alarm; // Global realtime executor bitflag variable for setting various alarms.
- volatile uint8_t sys_rt_exec_motion_override; // Global realtime executor bitflag variable for motion-based overrides.
- volatile uint8_t sys_rt_exec_accessory_override; // Global realtime executor bitflag variable for spindle/coolant overrides.
- #ifdef DEBUG
- volatile uint8_t sys_rt_exec_debug;
- #endif
- char pHelloVE[] = "Hello G6F1VE\r\n";
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_NVIC_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_TIM3_Init();
- MX_TIM1_Init();
- MX_TIM2_Init();
- MX_USART1_UART_Init();
- MX_TIM5_Init();
- MX_I2C1_Init();
- MX_SPI3_Init();
- MX_UART5_Init();
- MX_TIM7_Init();
- /* Initialize interrupts */
- MX_NVIC_Init();
- /* USER CODE BEGIN 2 */
- timing_init();
- uart_init();
- eeprom_init();
- serial_init(); // Setup serial baud rate and interrupts
- settings_init(); // Load Grbl settings from EEPROM
- stepper_init(); // Configure stepper pins and interrupt timers
- system_init(); // Configure pinout pins and pin-change interrupt
- memset(sys_position,0,sizeof(sys_position)); // Clear machine position.
- // Initialize system state.
- #ifdef FORCE_INITIALIZATION_ALARM
- // Force Grbl into an ALARM state upon a power-cycle or hard reset.
- sys.state = STATE_ALARM;
- #else
- sys.state = STATE_IDLE;
- #endif
- // Check for power-up and set system alarm if homing is enabled to force homing cycle
- // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the
- // startup scripts, but allows access to settings and internal commands. Only a homing
- // cycle '$H' or kill alarm locks '$X' will disable the alarm.
- // NOTE: The startup script will run after successful completion of the homing cycle, but
- // not after disabling the alarm locks. Prevents motion startup blocks from crashing into
- // things uncontrollably. Very bad.
- #ifdef HOMING_INIT_LOCK
- if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
- #endif
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- // Reset system variables.
- uint8_t prior_state = sys.state;
- memset(&sys, 0, sizeof(system_t)); // Clear system struct variable.
- sys.state = prior_state;
- sys.f_override = DEFAULT_FEED_OVERRIDE; // Set to 100%
- sys.r_override = DEFAULT_RAPID_OVERRIDE; // Set to 100%
- sys.spindle_speed_ovr = DEFAULT_SPINDLE_SPEED_OVERRIDE; // Set to 100%
- memset(sys_probe_position,0,sizeof(sys_probe_position)); // Clear probe position.
- sys_probe_state = 0;
- sys_rt_exec_state = 0;
- sys_rt_exec_alarm = 0;
- sys_rt_exec_motion_override = 0;
- sys_rt_exec_accessory_override = 0;
- // Reset Grbl primary systems.
- serial_reset_read_buffer(); // Clear serial read buffer
- gc_init(); // Set g-code parser to default state
- spindle_init();
- coolant_init();
- spi_limits_init(); //-- expansion io based limits, before limits_init()
- limits_init();
- probe_init();
- inoutputs_init();
- plan_reset(); // Clear block buffer and planner variables
- st_reset(); // Clear stepper subsystem variables.
- // Sync cleared gcode and planner positions to current system position.
- plan_sync_position();
- gc_sync_position();
- // Print welcome message. Indicates an initialization has occured at power-up or with a reset.
- report_init_message();
- // Start Grbl main loop. Processes program inputs and executes them.
- protocol_main_loop();
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 6;
- RCC_OscInitStruct.PLL.PLLN = 168;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** 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_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief NVIC Configuration.
- * @retval None
- */
- static void MX_NVIC_Init(void)
- {
- /* USART1_IRQn interrupt configuration */
- NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
- NVIC_EnableIRQ(USART1_IRQn);
- /* EXTI0_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI0_IRQn);
- /* EXTI1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI1_IRQn);
- /* EXTI2_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI2_IRQn);
- /* EXTI3_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI3_IRQn);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
复制代码
Keil代码下载:
代码grbl_cubemx_G2.7z
(6.77 MB, 下载次数: 27)
|