STM32F4-Discovery Control for GRBL

Discussion about the arduino based g-code interpreter, grbl
Post Reply
AtomSoft
Posts: 589
Joined: Mon Apr 09, 2012 9:15 pm
Location: Brooklyn NY
Contact:

STM32F4-Discovery Control for GRBL

Post by AtomSoft » Sun Jun 24, 2012 2:12 pm

I made a nice control on a breadboard and nice simple code. I just wanted to control the CNC from the real world with no PC. It works great. I have the code here but no schematics yet since i did it on a bread board. Ill update this with pictures and schematics as they are available.

The onboard button and leds control and show the speed selected. Speeds are 0.1, 1, 10, 20

Code: Select all

#include <stm32f4xx.h>
#include "stm32f4xx_it.h"
#include "system_stm32f4xx.h"
#include <stdio.h>

#define ALL_GPIO 0x1FF

#define LED_1 13
#define LED_2 14
#define LED_3 15
#define LED_4 12

#define BTNS GPIOE
#define BTNSpeed GPIOA

#define SpeedBtn 0

#define XU 14
#define XD 13

#define YU 12
#define YD 11

#define ZU 10
#define ZD 9

#define myClock 168000000
#define myBaud 9600

void ConfigPin(GPIO_TypeDef *myGPIO, uint32_t PIN, uint32_t MODE, uint32_t SPEED, uint32_t PUPD);
void ConfigUSART2(uint32_t BAUD, uint32_t fosc);
void put2char(USART_TypeDef* myUSART, char data);
void put2str(USART_TypeDef* myUSART, char *data);
char get2char(USART_TypeDef* myUSART);
void debo (void);
void LED4C (char led);

char Speeds[4][8];
char RxBuff[255];
char Speed = 0;
char TxBuff[20];

void main(void)
{
  int count = 0;
  volatile char tmp;
  char done = 0;
  RCC->AHB1ENR = ALL_GPIO;

  sprintf(Speeds[0],"0.1\0");
  sprintf(Speeds[1],"1\0");
  sprintf(Speeds[2],"10\0");
  sprintf(Speeds[3],"20\0");

  ConfigPin(GPIOD,LED_1,1,2,0);
  ConfigPin(GPIOD,LED_2,1,2,0);
  ConfigPin(GPIOD,LED_3,1,2,0);
  ConfigPin(GPIOD,LED_4,1,2,0);

  ConfigPin(BTNSpeed,SpeedBtn,0,2,0);
  
  ConfigPin(BTNS,XU,0,2,0);
  ConfigPin(BTNS,YU,0,2,0);
  ConfigPin(BTNS,ZU,0,2,0);
  ConfigPin(BTNS,XD,0,2,0);
  ConfigPin(BTNS,YD,0,2,0);
  ConfigPin(BTNS,ZD,0,2,0);

  ConfigUSART2(myBaud,myClock);
  done = 0;
  LED4C(Speed);
  while(1)
  {

    while(!done)
    {
      switch((BTNS->IDR & 0x7E00))
      {
        case (1<<XU):
          sprintf(TxBuff," G91\n G01 X%s F260 \n",Speeds[Speed]);
          done = 1;
          debo();
          break;
        case (1<<XD):
          sprintf(TxBuff," G91\n G01 X-%s F260\n",Speeds[Speed]);
          done = 1;
          debo();
          break;
        case (1<<YU):
          sprintf(TxBuff," G91\n G01 Y%s F260\n",Speeds[Speed]);
          done = 1;
          debo();
          break;
        case (1<<YD):
          sprintf(TxBuff," G91\n G01 Y-%s F260\n",Speeds[Speed]);
          done = 1;
          debo();
          break;
        case (1<<ZU):
          sprintf(TxBuff," G91\n G01 Z%s F260\n",Speeds[Speed]);
          done = 1;
          debo();
          break;
        case (1<<ZD):
          sprintf(TxBuff," G91\n G01 Z-%s F260\n",Speeds[Speed]);
          done = 1;
          debo();
          break;  
        default:
          done = 0;
      }

      if(((BTNSpeed->IDR >> SpeedBtn) & 1) == 1)
      {
        debo();
        Speed++;
      }
      
      if(Speed > 3) Speed = 0;
      
      LED4C(Speed);
      
    
      if(done)
      {
        put2str(USART2, TxBuff);
        done = 0;
      }

      
    }

    while(1)
    {
      RxBuff[count++] = get2char(USART2);

      if(RxBuff[count-1] == 0x0D)
        break;
    }
  }
}

void ConfigPin(GPIO_TypeDef *myGPIO, uint32_t PIN, uint32_t MODE, uint32_t SPEED, uint32_t PUPD)
{
    myGPIO->MODER |= (MODE << (PIN << 1));//OUTPUT
    myGPIO->OSPEEDR |= (SPEED << (PIN << 1));//50MHz
    myGPIO->PUPDR &= (PUPD << (PIN << 1));   //Set it for NO PUPD
}

void ConfigUSART2(uint32_t BAUD, uint32_t fosc)
{
  uint32_t tmpreg = 0x00, apbclock = 0x00;
  uint32_t integerdivider = 0x00;
  uint32_t fractionaldivider = 0x00;

  apbclock = fosc/8;

  RCC->APB1ENR = (1 << 17);    //Enable USART2 Clock

  GPIOD->MODER |= (2 << 10); //Setup TX pin for Alternate Function
  GPIOD->MODER |= (2 << 12);  //Setup RX pin for Alternate Function

  GPIOD->AFR[0] |= (7 << 20); //Setup TX as the Alternate Function
  GPIOD->AFR[0] |= (7 << 24); //Setup RX as the Alternate Function

  USART2->CR1 |= (1 << 13);  //Enable USART

  integerdivider = ((25 * apbclock) / (2 * (BAUD)));  
  tmpreg = (integerdivider / 100) << 4;
  fractionaldivider = integerdivider - (100 * (tmpreg >> 4));

  tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);

  USART2->BRR = (uint16_t)tmpreg;
  USART2->CR1 |= (3 << 2);  //Enable Tx & Rx
}

void put2char(USART_TypeDef* myUSART, char data)
{
    myUSART->DR = (char)data;
    while(((myUSART->SR >> 6) & 0x01) == 0);
}

void put2str(USART_TypeDef* myUSART, char *data)
{
    while(*data)
      put2char(myUSART,*data++);
}

char get2char(USART_TypeDef* myUSART)
{
    while(((myUSART->SR >> 5) & 0x01) == 0);
      return (char)myUSART->DR;
}

void debo (void)
{
  uint32_t time = (myClock / 40);

  while(time--);
}

void LED4C (char led)
{

  GPIOD->BSRRH |= (1<<LED_1);
  GPIOD->BSRRH |= (1<<LED_2);
  GPIOD->BSRRH |= (1<<LED_3);
  GPIOD->BSRRH |= (1<<LED_4);

  if(led == 0)
  {
     GPIOD->BSRRL |= (1<<LED_1);
     return;
  }
  if(led == 1)
  {
     GPIOD->BSRRL |= (1<<LED_2);
     return;
  }
  if(led == 2)
  {
     GPIOD->BSRRL |= (1<<LED_3);
     return;
  }
  if(led == 3)
  {
     GPIOD->BSRRL |= (1<<LED_4);
     return;
  }
}
Blog / Store
Live to Love, Not to Hate.
You're not a failure because you don't make it, you're a success because you tried.
Do you like the help i give? Think i deserve a tip? DONATE

AtomSoft
Posts: 589
Joined: Mon Apr 09, 2012 9:15 pm
Location: Brooklyn NY
Contact:

Re: STM32F4-Discovery Control for GRBL

Post by AtomSoft » Sun Jun 24, 2012 3:14 pm

Schematic:
Attachments
grblc.png
grblc.png (35.46 KiB) Viewed 7096 times
Blog / Store
Live to Love, Not to Hate.
You're not a failure because you don't make it, you're a success because you tried.
Do you like the help i give? Think i deserve a tip? DONATE

AtomSoft
Posts: 589
Joined: Mon Apr 09, 2012 9:15 pm
Location: Brooklyn NY
Contact:

Re: STM32F4-Discovery Control for GRBL

Post by AtomSoft » Sun Jun 24, 2012 3:19 pm

might get a board made :)
Attachments
grblcboard.jpg
grblcboard.jpg (63.58 KiB) Viewed 7096 times
Blog / Store
Live to Love, Not to Hate.
You're not a failure because you don't make it, you're a success because you tried.
Do you like the help i give? Think i deserve a tip? DONATE

AtomSoft
Posts: 589
Joined: Mon Apr 09, 2012 9:15 pm
Location: Brooklyn NY
Contact:

Re: STM32F4-Discovery Control for GRBL

Post by AtomSoft » Sun Jun 24, 2012 3:20 pm

I recommend powering it from the Arduino Power Jack... for better performance :)
Blog / Store
Live to Love, Not to Hate.
You're not a failure because you don't make it, you're a success because you tried.
Do you like the help i give? Think i deserve a tip? DONATE

Post Reply