找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1291|回复: 1
打印 上一主题 下一主题
收起左侧

对GRBL移植到STM32F407ZE上源码 HAL库

[复制链接]
跳转到指定楼层
楼主
ID:190107 发表于 2022-9-21 11:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "i2c.h"
  4. #include "spi.h"
  5. #include "tim.h"
  6. #include "usart.h"
  7. #include "gpio.h"

  8. /* Private includes ----------------------------------------------------------*/
  9. /* USER CODE BEGIN Includes */
  10. #include "grbl.h"
  11. /* USER CODE END Includes */

  12. /* Private typedef -----------------------------------------------------------*/
  13. /* USER CODE BEGIN PTD */

  14. /* USER CODE END PTD */

  15. /* Private define ------------------------------------------------------------*/
  16. /* USER CODE BEGIN PD */

  17. /* USER CODE END PD */

  18. /* Private macro -------------------------------------------------------------*/
  19. /* USER CODE BEGIN PM */

  20. /* USER CODE END PM */

  21. /* Private variables ---------------------------------------------------------*/

  22. /* USER CODE BEGIN PV */
  23. /* Private variables ---------------------------------------------------------*/
  24. // Declare system global variable structure
  25. system_t sys;
  26. int32_t sys_position[N_AXIS];      // Real-time machine (aka home) position vector in steps.
  27. int32_t sys_probe_position[N_AXIS]; // Last probe position in machine coordinates and steps.
  28. volatile uint8_t sys_probe_state;   // Probing state value.  Used to coordinate the probing cycle with stepper ISR.
  29. volatile uint8_t sys_rt_exec_state;   // Global realtime executor bitflag variable for state management. See EXEC bitmasks.
  30. volatile uint8_t sys_rt_exec_alarm;   // Global realtime executor bitflag variable for setting various alarms.
  31. volatile uint8_t sys_rt_exec_motion_override; // Global realtime executor bitflag variable for motion-based overrides.
  32. volatile uint8_t sys_rt_exec_accessory_override; // Global realtime executor bitflag variable for spindle/coolant overrides.
  33. #ifdef DEBUG
  34.   volatile uint8_t sys_rt_exec_debug;
  35. #endif

  36. char pHelloVE[] = "Hello G6F1VE\r\n";

  37. /* USER CODE END PV */

  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. static void MX_NVIC_Init(void);
  41. /* USER CODE BEGIN PFP */

  42. /* USER CODE END PFP */

  43. /* Private user code ---------------------------------------------------------*/
  44. /* USER CODE BEGIN 0 */

  45. /* USER CODE END 0 */

  46. /**
  47.   * @brief  The application entry point.
  48.   * @retval int
  49.   */
  50. int main(void)
  51. {
  52.   /* USER CODE BEGIN 1 */

  53.   /* USER CODE END 1 */


  54.   /* MCU Configuration--------------------------------------------------------*/

  55.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  56.   HAL_Init();

  57.   /* USER CODE BEGIN Init */

  58.   /* USER CODE END Init */

  59.   /* Configure the system clock */
  60.   SystemClock_Config();

  61.   /* USER CODE BEGIN SysInit */

  62.   /* USER CODE END SysInit */

  63.   /* Initialize all configured peripherals */
  64.   MX_GPIO_Init();
  65.   MX_TIM3_Init();
  66.   MX_TIM1_Init();
  67.   MX_TIM2_Init();
  68.   MX_USART1_UART_Init();
  69.   MX_TIM5_Init();
  70.   MX_I2C1_Init();
  71.   MX_SPI3_Init();
  72.   MX_UART5_Init();
  73.   MX_TIM7_Init();

  74.   /* Initialize interrupts */
  75.   MX_NVIC_Init();
  76.   /* USER CODE BEGIN 2 */
  77.   timing_init();
  78.   uart_init();
  79.   eeprom_init();
  80.   serial_init();   // Setup serial baud rate and interrupts
  81.   settings_init(); // Load Grbl settings from EEPROM
  82.   stepper_init();  // Configure stepper pins and interrupt timers
  83.   system_init();   // Configure pinout pins and pin-change interrupt
  84.   memset(sys_position,0,sizeof(sys_position)); // Clear machine position.

  85.   // Initialize system state.
  86.   #ifdef FORCE_INITIALIZATION_ALARM
  87.     // Force Grbl into an ALARM state upon a power-cycle or hard reset.
  88.     sys.state = STATE_ALARM;
  89.   #else
  90.     sys.state = STATE_IDLE;
  91.   #endif

  92.   // Check for power-up and set system alarm if homing is enabled to force homing cycle
  93.   // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the
  94.   // startup scripts, but allows access to settings and internal commands. Only a homing
  95.   // cycle '$H' or kill alarm locks '$X' will disable the alarm.
  96.   // NOTE: The startup script will run after successful completion of the homing cycle, but
  97.   // not after disabling the alarm locks. Prevents motion startup blocks from crashing into
  98.   // things uncontrollably. Very bad.
  99.   #ifdef HOMING_INIT_LOCK
  100.     if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
  101.   #endif



  102.   /* USER CODE END 2 */

  103.   /* Infinite loop */
  104.   /* USER CODE BEGIN WHILE */
  105.   while (1)
  106.   {
  107.         // Reset system variables.
  108.                 uint8_t prior_state = sys.state;
  109.                 memset(&sys, 0, sizeof(system_t)); // Clear system struct variable.
  110.                 sys.state = prior_state;
  111.                 sys.f_override = DEFAULT_FEED_OVERRIDE;  // Set to 100%
  112.                 sys.r_override = DEFAULT_RAPID_OVERRIDE; // Set to 100%
  113.                 sys.spindle_speed_ovr = DEFAULT_SPINDLE_SPEED_OVERRIDE; // Set to 100%
  114.                         memset(sys_probe_position,0,sizeof(sys_probe_position)); // Clear probe position.
  115.                 sys_probe_state = 0;
  116.                 sys_rt_exec_state = 0;
  117.                 sys_rt_exec_alarm = 0;
  118.                 sys_rt_exec_motion_override = 0;
  119.                 sys_rt_exec_accessory_override = 0;

  120.                 // Reset Grbl primary systems.
  121.                 serial_reset_read_buffer(); // Clear serial read buffer
  122.                 gc_init(); // Set g-code parser to default state
  123.                 spindle_init();
  124.                 coolant_init();
  125.                 spi_limits_init();                                //-- expansion io based limits, before limits_init()
  126.                 limits_init();

  127.                 probe_init();
  128.                 inoutputs_init();

  129.                 plan_reset(); // Clear block buffer and planner variables
  130.                 st_reset(); // Clear stepper subsystem variables.

  131.                 // Sync cleared gcode and planner positions to current system position.
  132.                 plan_sync_position();
  133.                 gc_sync_position();

  134.                 // Print welcome message. Indicates an initialization has occured at power-up or with a reset.
  135.                 report_init_message();

  136.                 // Start Grbl main loop. Processes program inputs and executes them.
  137.                 protocol_main_loop();
  138.     /* USER CODE END WHILE */

  139.     /* USER CODE BEGIN 3 */
  140.   }
  141.   /* USER CODE END 3 */
  142. }

  143. /**
  144.   * @brief System Clock Configuration
  145.   * @retval None
  146.   */
  147. void SystemClock_Config(void)
  148. {
  149.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  150.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  151.   /** Configure the main internal regulator output voltage
  152.   */
  153.   __HAL_RCC_PWR_CLK_ENABLE();
  154.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  155.   /** Initializes the CPU, AHB and APB busses clocks
  156.   */
  157.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  158.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  159.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  160.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  161.   RCC_OscInitStruct.PLL.PLLM = 6;
  162.   RCC_OscInitStruct.PLL.PLLN = 168;
  163.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  164.   RCC_OscInitStruct.PLL.PLLQ = 4;
  165.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  166.   {
  167.     Error_Handler();
  168.   }
  169.   /** Initializes the CPU, AHB and APB busses clocks
  170.   */
  171.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  172.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  173.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  174.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  175.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  176.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  177.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  178.   {
  179.     Error_Handler();
  180.   }
  181. }

  182. /**
  183.   * @brief NVIC Configuration.
  184.   * @retval None
  185.   */
  186. static void MX_NVIC_Init(void)
  187. {
  188.   /* USART1_IRQn interrupt configuration */
  189.   NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
  190.   NVIC_EnableIRQ(USART1_IRQn);
  191.   /* EXTI0_IRQn interrupt configuration */
  192.   HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
  193.   HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  194.   /* EXTI1_IRQn interrupt configuration */
  195.   HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
  196.   HAL_NVIC_EnableIRQ(EXTI1_IRQn);
  197.   /* EXTI2_IRQn interrupt configuration */
  198.   HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0);
  199.   HAL_NVIC_EnableIRQ(EXTI2_IRQn);
  200.   /* EXTI3_IRQn interrupt configuration */
  201.   HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
  202.   HAL_NVIC_EnableIRQ(EXTI3_IRQn);
  203. }

  204. /* USER CODE BEGIN 4 */

  205. /* USER CODE END 4 */

  206. /**
  207.   * @brief  This function is executed in case of error occurrence.
  208.   * @retval None
  209.   */
  210. void Error_Handler(void)
  211. {
  212.   /* USER CODE BEGIN Error_Handler_Debug */
  213.   /* User can add his own implementation to report the HAL error return state */

  214.   /* USER CODE END Error_Handler_Debug */
  215. }

  216. #ifdef  USE_FULL_ASSERT
  217. /**
  218.   * @brief  Reports the name of the source file and the source line number
  219.   *         where the assert_param error has occurred.
  220.   * @param  file: pointer to the source file name
  221.   * @param  line: assert_param error line source number
  222.   * @retval None
  223.   */
  224. void assert_failed(uint8_t *file, uint32_t line)
  225. {
  226.   /* USER CODE BEGIN 6 */
  227.   /* User can add his own implementation to report the file name and line number,
  228.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  229.   /* USER CODE END 6 */
  230. }
  231. #endif /* USE_FULL_ASSERT */
复制代码

Keil代码下载: 代码grbl_cubemx_G2.7z (6.77 MB, 下载次数: 27)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:279787 发表于 2023-6-25 22:58 | 只看该作者
编译好多警告。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表