gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
sleep.c
Go to the documentation of this file.
1 /*
2  sleep.c - determines and executes sleep procedures
3  Part of Grbl
4 
5  Copyright (c) 2016 Sungeun K. Jeon
6 
7  Grbl is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Grbl is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "grbl.h"
22 
23 
24 #define SLEEP_SEC_PER_OVERFLOW (65535.0*64.0/F_CPU)
25 #define SLEEP_COUNT_MAX (SLEEP_DURATION/SLEEP_SEC_PER_OVERFLOW)
26 
27 volatile uint8_t sleep_counter;
28 
30 static void sleep_enable() {
31  sleep_counter = 0;
32  TCNT3 = 0;
33  TIMSK3 |= (1<<TOIE3);
34 }
35 
37 static void sleep_disable() { TIMSK3 &= ~(1<<TOIE3); }
38 
40 void sleep_init()
41 {
42 // Configure Timer 3: Sleep Counter Overflow Interrupt
43 //
45  TCCR3B = 0;
46  TCCR3A = 0;
47  TCCR3B = (TCCR3B & ~((1<<CS32) | (1<<CS31))) | (1<<CS30);
48 // TCCR3B |= (1<<CS32); //!< Enable timer with 1/256 prescaler. ~4.4min max with uint8 and 1.05sec/tick
49 // TCCR3B |= (1<<CS31); //!< Enable timer with 1/8 prescaler. ~8.3sec max with uint8 and 32.7msec/tick
50  TCCR3B |= (1<<CS31)|(1<<CS30);
51 // TCCR3B |= (1<<CS32)|(1<<CS30); //!< Enable timer with 1/1024 prescaler. ~17.8min max with uint8 and 4.19sec/tick
52  sleep_disable();
53 }
54 
56 ISR(TIMER3_OVF_vect) { sleep_counter++; }
57 
59 static void sleep_execute()
60 {
61 // Fetch current number of buffered characters in serial RX buffer.
62  uint8_t rx_initial = serial_get_rx_buffer_count();
63 
64 // Enable sleep counter
65  sleep_enable();
66 
67  do {
68 // Monitor for any new RX serial data or external events (queries, buttons, alarms) to exit.
69  if ( (serial_get_rx_buffer_count() > rx_initial) || sys_rt_exec_state || sys_rt_exec_alarm ) {
70 // Disable sleep timer and return to normal operation.
71  sleep_disable();
72  return;
73  }
74  } while(sleep_counter <= SLEEP_COUNT_MAX);
75 
76 // If reached, sleep counter has expired. Execute sleep procedures.
77 // Notify user that Grbl has timed out and will be parking.
78 // To exit sleep, resume or reset. Either way, the job will not be recoverable.
81 }
82 
84 // sleep mode upon elapse.
85 //
87 // Hence, make sure any valid running state that executes the sleep timer is not one that is moving.
89 {
90 // The sleep execution feature will continue only if the machine is in an IDLE or HOLD state and
91 // has any powered components enabled.
92 //
94 // to directly monitor and record running state during parking to ensure proper function.
96  if (sys.state == STATE_IDLE) {
97  sleep_execute();
98  } else if ((sys.state & STATE_HOLD) && (sys.suspend & SUSPEND_HOLD_COMPLETE)) {
99  sleep_execute();
101  sleep_execute();
102  }
103  }
104 }
ISR(TIMER3_OVF_vect)
Increment sleep counter with each timer overflow.
Definition: sleep.c:56
#define STATE_HOLD
Active feed hold.
Definition: system.h:77
#define MESSAGE_SLEEP_MODE
Definition: report.h:81
gc_modal_t modal
Definition: gcode.h:196
void system_set_exec_state_flag(uint8_t mask)
Special handlers for setting and clearing Grbl's real-time execution flags.
Definition: system.c:340
uint8_t suspend
System suspend bitflag variable that manages holds, cancels, and safety door.
Definition: system.h:114
void report_feedback_message(uint8_t message_code)
Prints feedback messages. This serves as a centralized method to provide additional.
Definition: report.c:135
volatile uint8_t sleep_counter
Definition: sleep.c:27
void sleep_check()
Checks running conditions for sleep. If satisfied, enables sleep countdown and executes.
Definition: sleep.c:88
volatile uint8_t sys_rt_exec_alarm
Global realtime executor bitflag variable for setting various alarms.
Definition: system.h:135
uint8_t state
Tracks the current system state of Grbl.
Definition: system.h:112
#define SUSPEND_RETRACT_COMPLETE
(Safety door only) Indicates retraction and de-energizing is complete.
Definition: system.h:85
#define SLEEP_COUNT_MAX
Definition: sleep.c:25
system_t sys
Declare system global variable structure.
Definition: main.c:25
uint8_t coolant
{M7,M8,M9}
Definition: gcode.h:177
uint8_t serial_get_rx_buffer_count()
Returns the number of bytes used in the RX serial buffer.
Definition: serial.c:46
#define EXEC_SLEEP
bitmask 10000000
Definition: system.h:38
volatile uint8_t sys_rt_exec_state
Global realtime executor bitflag variable for state management. See EXEC bitmasks.
Definition: system.h:134
uint8_t spindle
{M3,M4,M5}
Definition: gcode.h:178
void sleep_init()
Initialization routine for sleep timer.
Definition: sleep.c:40
#define SUSPEND_HOLD_COMPLETE
Indicates initial feed hold is complete.
Definition: system.h:83
parser_state_t gc_state
Declare gc extern struct.
Definition: gcode.c:35
#define STATE_IDLE
Define system state bit map. The state variable primarily tracks the individual functions.
Definition: system.h:72
#define STATE_SAFETY_DOOR
Safety door is ajar. Feed holds and de-energizes system.
Definition: system.h:79