Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.c
00001 /* 00002 main.c - An embedded CNC Controller with rs274/ngc (g-code) support 00003 Part of Grbl 00004 00005 Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC 00006 Copyright (c) 2009-2011 Simen Svale Skogsrud 00007 00008 Grbl is free software: you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation, either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 Grbl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with Grbl. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 #include "grbl.h" 00023 // Declare system global variable structure 00024 system_t sys; 00025 int32_t sys_position[N_AXIS]; // Real-time machine (aka home) position vector in steps. 00026 int32_t sys_probe_position[N_AXIS]; // Last probe position in machine coordinates and steps. 00027 volatile uint8_t sys_probe_state; // Probing state value. Used to coordinate the probing cycle with stepper ISR. 00028 volatile uint8_t sys_rt_exec_state; // Global realtime executor bitflag variable for state management. See EXEC bitmasks. 00029 volatile uint8_t sys_rt_exec_alarm; // Global realtime executor bitflag variable for setting various alarms. 00030 volatile uint8_t sys_rt_exec_motion_override; // Global realtime executor bitflag variable for motion-based overrides. 00031 volatile uint8_t sys_rt_exec_accessory_override; // Global realtime executor bitflag variable for spindle/coolant overrides. 00032 00033 #if defined (STM32F103C8) 00034 #include "usb_lib.h" 00035 #ifdef USEUSB 00036 #include "usb_desc.h" 00037 #endif 00038 #include "hw_config.h" 00039 #ifdef USEUSB 00040 #include "usb_pwr.h" 00041 #endif 00042 #include "stm32eeprom.h" 00043 #ifndef USEUSB 00044 #include "stm32f10x_usart.h" 00045 void USART1_Configuration(u32 BaudRate) 00046 { 00047 GPIO_InitTypeDef GPIO_InitStructure; 00048 USART_InitTypeDef USART_InitStructure; 00049 NVIC_InitTypeDef NVIC_InitStructure; 00050 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); 00051 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 00052 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 00053 00054 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00055 NVIC_Init(&NVIC_InitStructure); 00056 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); 00057 00058 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 00059 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 00060 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00061 GPIO_Init(GPIOA, &GPIO_InitStructure); 00062 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 00063 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 00064 GPIO_Init(GPIOA, &GPIO_InitStructure); 00065 00066 USART_InitStructure.USART_BaudRate = BaudRate; 00067 USART_InitStructure.USART_WordLength = USART_WordLength_8b; 00068 USART_InitStructure.USART_StopBits = USART_StopBits_1; 00069 USART_InitStructure.USART_Parity = USART_Parity_No; 00070 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 00071 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 00072 USART1->CR1 |= (USART_CR1_RE | USART_CR1_TE); 00073 USART_Init(USART1, &USART_InitStructure); 00074 // USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 00075 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 00076 USART_Cmd(USART1, ENABLE); 00077 } 00078 #endif 00079 00080 #endif 00081 00082 00083 #ifdef WIN32 00084 int main(int argc, char *argv[]) 00085 #else 00086 int main(void) 00087 #endif 00088 { 00089 #if defined (STM32F103C8) 00090 GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); 00091 #ifdef LEDBLINK 00092 GPIO_InitTypeDef GPIO_InitStructure; 00093 00094 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 00095 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; 00096 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 00097 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; 00098 GPIO_Init(GPIOC, &GPIO_InitStructure); 00099 #endif 00100 //Set_System(); 00101 #ifndef USEUSB 00102 USART1_Configuration(115200); 00103 #else 00104 Set_USBClock(); 00105 USB_Interrupts_Config(); 00106 USB_Init(); 00107 #endif 00108 00109 #ifndef NOEEPROMSUPPORT 00110 FLASH_Unlock(); 00111 eeprom_init(); 00112 #endif 00113 SysTick->CTRL &= 0xfffffffb; 00114 #endif 00115 // Initialize system upon power-up. 00116 serial_init(); // Setup serial baud rate and interrupts 00117 #ifdef WIN32 00118 winserial_init(argv[1]); 00119 eeprom_init(); 00120 #endif 00121 settings_init(); // Load Grbl settings from EEPROM 00122 stepper_init(); // Configure stepper pins and interrupt timers 00123 system_init(); // Configure pinout pins and pin-change interrupt 00124 00125 memset(sys_position,0,sizeof(sys_position)); // Clear machine position. 00126 #ifdef AVRTARGET 00127 sei(); // Enable interrupts 00128 #endif 00129 // Initialize system state. 00130 #ifdef FORCE_INITIALIZATION_ALARM 00131 // Force Grbl into an ALARM state upon a power-cycle or hard reset. 00132 sys.state = STATE_ALARM; 00133 #else 00134 sys.state = STATE_IDLE; 00135 #endif 00136 00137 // Check for power-up and set system alarm if homing is enabled to force homing cycle 00138 // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the 00139 // startup scripts, but allows access to settings and internal commands. Only a homing 00140 // cycle '$H' or kill alarm locks '$X' will disable the alarm. 00141 // NOTE: The startup script will run after successful completion of the homing cycle, but 00142 // not after disabling the alarm locks. Prevents motion startup blocks from crashing into 00143 // things uncontrollably. Very bad. 00144 #ifdef HOMING_INIT_LOCK 00145 if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; } 00146 #endif 00147 00148 // Grbl initialization loop upon power-up or a system abort. For the latter, all processes 00149 // will return to this loop to be cleanly re-initialized. 00150 for(;;) { 00151 00152 // Reset system variables. 00153 uint8_t prior_state = sys.state; 00154 memset(&sys, 0, sizeof(system_t)); // Clear system struct variable. 00155 sys.state = prior_state; 00156 sys.f_override = DEFAULT_FEED_OVERRIDE; // Set to 100% 00157 sys.r_override = DEFAULT_RAPID_OVERRIDE; // Set to 100% 00158 sys.spindle_speed_ovr = DEFAULT_SPINDLE_SPEED_OVERRIDE; // Set to 100% 00159 memset(sys_probe_position,0,sizeof(sys_probe_position)); // Clear probe position. 00160 sys_probe_state = 0; 00161 sys_rt_exec_state = 0; 00162 sys_rt_exec_alarm = 0; 00163 sys_rt_exec_motion_override = 0; 00164 sys_rt_exec_accessory_override = 0; 00165 00166 // Reset Grbl primary systems. 00167 serial_reset_read_buffer(); // Clear serial read buffer 00168 gc_init(); // Set g-code parser to default state 00169 spindle_init(); 00170 coolant_init(); 00171 limits_init(); 00172 probe_init(); 00173 plan_reset(); // Clear block buffer and planner variables 00174 st_reset(); // Clear stepper subsystem variables. 00175 00176 // Sync cleared gcode and planner positions to current system position. 00177 plan_sync_position(); 00178 gc_sync_position(); 00179 00180 // Print welcome message. Indicates an initialization has occured at power-up or with a reset. 00181 report_init_message(); 00182 00183 // Start Grbl main loop. Processes program inputs and executes them. 00184 protocol_main_loop(); 00185 00186 } 00187 return 0; /* Never reached */ 00188 } 00189 #if defined (STM32F103C8) 00190 void _delay_ms(uint32_t x) 00191 { 00192 u32 temp; 00193 SysTick->LOAD = (u32)72000000 / 8000; // Loading time 00194 SysTick->VAL = 0x00; // Empty the counter 00195 SysTick->CTRL = 0x01; // Start from bottom 00196 do 00197 { 00198 temp = SysTick->CTRL; 00199 } while (temp & 0x01 && !(temp&(1 << 16))); // Wait time arrive 00200 SysTick->CTRL = 0x00; // Close the counter 00201 SysTick->VAL = 0X00; // Empty the counter 00202 } 00203 void LedBlink(void) 00204 { 00205 static BitAction nOnFlag = Bit_SET; 00206 GPIO_WriteBit(GPIOC, GPIO_Pin_13, nOnFlag); 00207 nOnFlag = (nOnFlag == Bit_SET) ? Bit_RESET : Bit_SET; 00208 } 00209 #endif
Generated on Tue Jul 12 2022 20:45:31 by
1.7.2