GRBL七: STM32代码移植——IO

首先看引脚映射,关于引脚的定义都在pin_map.h中

其实感觉GRBL的引脚这地做的不太人性,因为他的几个引脚都是用的同一个IO通道且是挨在一起的,程序中对引脚的操作也是按照挨在一起的算法进行的,如果用的是不一个通道IO

且重复的话就不适合了,例如如果X_STEP使用A0  Y_STEP使用B0这样就会出现错误,可以使用不同通道,但是要确保X_STEP_BIT ,Y_STEP_BIT  , Z_STEP_BIT  ,X_DIRECTION_BIT ,Y_DIRECTION_BIT ,Z_DIRECTION_BIT 使用的是不同位,这是默认的情况,也可以在参数中修改这些bit的具体位置$6的值就是这六个引脚的映射

pin_map.h

/*
  pin_map.h - Pin mapping configuration file
  Part of Grbl

  The MIT License (MIT)

  GRBL(tm) - Embedded CNC g-code interpreter and motion-controller
  Copyright (c) 2013 Sungeun K. Jeon

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

/* The pin_map.h file serves as a central pin mapping settings file for different processor
   types, i.e. AVR 328p or AVR Mega 2560. Grbl officially supports the Arduino Uno, but the 
   other supplied pin mappings are supplied by users, so your results may vary. */

#ifndef pin_map_h
#define pin_map_h
//add by zjk for stm32
#ifdef PIN_MAP_STM32 // AVR 328p, Officially supported by Grbl.

  // Serial port pins
//  #define SERIAL_RX USART_RX_vect
//  #define SERIAL_UDRE USART_UDRE_vect

  // NOTE: All step bit and direction pins must be on the same port.
//  #define STEPPING_DDR       DDRD
//  #define STEPPING_PORT      PORTD
//  #define X_STEP_BIT         2  // Uno Digital Pin 2
//  #define Y_STEP_BIT         3  // Uno Digital Pin 3
//  #define Z_STEP_BIT         4  // Uno Digital Pin 4
//  #define X_DIRECTION_BIT    5  // Uno Digital Pin 5
//  #define Y_DIRECTION_BIT    6  // Uno Digital Pin 6
//  #define Z_DIRECTION_BIT    7  // Uno Digital Pin 7
//  #define STEP_MASK ((1<ODR
  #define Y_STEP_PORT      GPIOD->ODR
  #define Z_STEP_PORT
  #define X_STEP_BIT         8  // PA8
  #define Y_STEP_BIT         15  // PD15
  #define Z_STEP_BIT
  #define X_STEP_MASK      (1<>2)<>3)<>4)<ODR
  #define Y_DIRECTION_PORT	GPIOA->ODR
  #define Z_DIRECTION_PORT
  #define X_DIRECTION_BIT    6  // PC6
  #define Y_DIRECTION_BIT    2  // PA2
  #define Z_DIRECTION_BIT
  #define X_DIRECTION_MASK   (1<>5)<>7)<

pinmap中其实定义的是三种单片机的引脚定义,具体使用哪一个在config.h中有定义,如下:

#define PIN_MAP_STM32
//#define PIN_MAP_ARDUINO_UNO

由于我修改的是STM32的,所以我额外定义了个STM32的选项,可以看到我使用的是不同通道的IO,所以定义起来就有所小不同,为了尽量少修改GRBL源程序,尽量修改定义这地方的代码,减少调用出代码的修改

stepper.c中关于引脚的初始化如下:

/ Initialize and start the stepper motor subsystem
void st_init()
{
  // Configure directions of interface pins
//  STEPPING_DDR |= STEPPING_MASK;
//  STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | settings.invert_mask;
//  STEPPERS_DISABLE_DDR |= 1<
  //接下来是定时器的初始化参考:http://blog.csdn.net/zhangjikuan/article/details/46697657


调用处如下所示:

//定时器4中断服务函数
void TIM4_IRQHandler(void) //中断服务函数名一定正确
{
	if(TIM4->SR&0X0001)//再次判断是否更新事件
	{

	  // Reset stepping pins (leave the direction pins)
	  //STEPPING_PORT = (STEPPING_PORT & ~STEP_MASK) | (settings.invert_mask & STEP_MASK); 
	  X_STEP_PORT = (X_STEP_PORT & ~X_STEP_MASK) | (settings.invert_mask & X_STEP_MASK);
	  Y_STEP_PORT = (Y_STEP_PORT & ~Y_STEP_MASK) | (settings.invert_mask & Y_STEP_MASK);
	  //TCCR2B = 0; // Disable Timer2 to prevent re-entering this interrupt when it's not needed. 
	  //bit0=1关闭定时器
	  TIM4->CR1&=~0X01;
	}
	TIM4->SR&=~(1<<0); //状态寄存器清除
}













你可能感兴趣的:(GRBL)