GRBL九:STM32移植——主函数调用

看到很多朋友对GRBL挺感兴趣,网上关于stm32移植GRBL的例子确实也不多,而且收到很多朋友询问工程源码,在这里统一回复下

由于GRBL是当初实习项目需要,把GRBL做成PLC指令放在PLC系统中的,所以GRBL自然就是系统中的一小部分功能,现在实习结束,就没有当初的源码工程了,而且这个也是公司不允许的,我网上贴出来的代码都是当初自己测试GRBL时候写的测试代码,全部测试通过的。

今天详细看了一下,硬件部分的代码:IO、串口、定时器控制都已经贴出

                                  上层代码像:gcode.c  motion_control.c  planner.c  protocol.c spindle_control.c 基本未做修改,因为这部分是上层代码,与底层硬件无关,直接借用就好

还有朋友问道关于限位开关的地方,这个是在支持文件中的limits.c中写的,如果需要限位功能,需要修改此文件中关于读取限位开关IO引脚的配置,我当初项目中未使用到限位,所以我在下面主函数调用中屏蔽掉了关于限位的代码limits_init();

关于protocol.c spindle_control.c主要是来做主轴控制的,像主刀或者喷胶之类的,这个我们只用GRBL做了个双轴差补,所以这些功能也都没实现,所以在下面主函数中屏蔽了这些功能的初始化://protocol_init(); // Clear incoming line data and execute startup lines
                      //spindle_init();
                      //coolant_init();

下面是主函数的调用,希望可以帮助到大家,仍有疑问请评论,看到尽量回复

main.c

#include <stm32f10x_lib.h>
#include "stdio.h"
#include "config.h"
#include "planner.h"
#include "nuts_bolts.h"
#include "stepper.h"
#include "spindle_control.h"
#include "coolant_control.h"
#include "motion_control.h"
#include "gcode.h"
#include "protocol.h"
#include "limits.h"
#include "report.h"
#include "settings.h"
#include "serial.h"
#include "print.h"
#include "sys.h"
//printf
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

// Declare system global variable structure
system_t sys; 	
void sei(void)
{
	//bit0=1¿ªÆô¶¨Ê±Æ÷£¬bit6,5=00±ßÑØ,bit4=0ÏòÉϼÆÊý 1ÏòÏ£¬bit9,8=00ÎÞ·ÖƵ 01=2*  10=4*
	TIM3->CR1|=0X01;
	//bit0=1¿ªÆô¶¨Ê±Æ÷£¬bit6,5=00±ßÑØ,bit4=0ÏòÉϼÆÊý 1ÏòÏ£¬bit9,8=00ÎÞ·ÖƵ 01=2*  10=4*
	TIM4->CR1|=0X01;
}
void cli(void)
{
	//bit0=1¹Ø±Õ¶¨Ê±Æ÷
	TIM3->CR1&=~0X01;
	//bit0=1¹Ø±Õ¶¨Ê±Æ÷
	TIM4->CR1&=~0X01;
}
int main(void)
{
  // Initialize system
  Stm32_Clock_Init(9); //ʱÖÓ72M   //APB1¶þ·ÖƵ£¬APB2²»·ÖƵ
  //serial_init(); // Setup serial baud rate and interrupts
  USART_Configuration();
  settings_init(); // Load grbl settings from EEPROM
  st_init(); // Setup stepper pins and interrupt timers
  sei(); // Enable interrupts //¿ªÈ«¾ÖÖжϣ¬cli()¹ØÈ«¾ÖÖÐ¶Ï  
  memset(&sys, 0, sizeof(sys));  // Clear all system variables
  sys.abort = true;   // Set abort to complete initialization
  sys.state = STATE_INIT;  // Set alarm state to indicate unknown initial position
  printPgmString("for(;;)\r\n");
  
  for(;;) {
  
    // Execute system reset upon a system abort, where the main program will return to this loop.
    // Once here, it is safe to re-initialize the system. At startup, the system will automatically
    // reset to finish the initialization process.
    if (sys.abort) {
      // Reset system.
      serial_reset_read_buffer(); // Clear serial read buffer
      plan_init(); // Clear block buffer and planner variables
      gc_init(); // Set g-code parser to default state
      //protocol_init(); // Clear incoming line data and execute startup lines
      //spindle_init();
      //coolant_init();
      //limits_init();
      st_reset(); // Clear stepper subsystem variables.

      // Sync cleared gcode and planner positions to current system position, which is only
      // cleared upon startup, not a reset/abort. 
      sys_sync_current_position();

      // Reset system variables.
      sys.abort = false;
      sys.execute = 0;
      if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) { sys.auto_start = true; }
      
      // 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 (sys.state == STATE_INIT && bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
      #endif
      
      // Check for and report alarm state after a reset, error, or an initial power up.
      if (sys.state == STATE_ALARM) {
        report_feedback_message(MESSAGE_ALARM_LOCK); 
      } else {
        // All systems go. Set system to ready and execute startup script.
        sys.state = STATE_IDLE;
      //  protocol_execute_startup(); 
      }
    }
    
    protocol_execute_runtime();
    protocol_process(); // ... process the serial protocol
    
  }
  return 0;   /* never reached */
}

//printf
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}
  
  return ch;
}






你可能感兴趣的:(stm32,gcode,C程序,GRBL,G代码)