forked from stefanix/LasaurGrbl
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tasks.c
201 lines (167 loc) · 5.03 KB
/
tasks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
tasks.c - manage non-ISR tasks
Part of LasaurGrbl
Copyright (c) 2013 Richard Taylor
LasaurGrbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LasaurGrbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <stdint.h>
#include <stdbool.h>
#include <inc/hw_ints.h>
#include <inc/hw_types.h>
#include <inc/hw_memmap.h>
#include <inc/hw_timer.h>
#include <inc/hw_gpio.h>
#include <driverlib/gpio.h>
#include <driverlib/sysctl.h>
#include <driverlib/timer.h>
#include <driverlib/interrupt.h>
#include "tasks.h"
#include "gcode.h"
#include "planner.h"
#include "serial.h"
#include "stepper.h"
#include "sense_control.h"
#include "lcd.h"
#include "joystick.h"
static volatile task_t task_status = 0;
static void *task_data[TASK_END];
static uint64_t timer_load;
uint32_t system_time_ms = 0;
void gp_timer_isr(void) {
TimerLoadSet64(GP_TIMER, timer_load);
TimerIntClear(GP_TIMER, TIMER_TIMA_TIMEOUT);
system_time_ms++;
}
void tasks_init(void) {
task_status = 0;
// Configure GP timer
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER4);
TimerConfigure(GP_TIMER, TIMER_CFG_PERIODIC);
// Create a 1ms timer
timer_load = SysCtlClockGet() / 1000;
TimerLoadSet64(GP_TIMER, timer_load);
TimerIntRegister(GP_TIMER, TIMER_A, gp_timer_isr);
TimerIntEnable(GP_TIMER, TIMER_TIMA_TIMEOUT);
IntPrioritySet(INT_TIMER4A, CONFIG_GPTIMER_PRIORITY);
TimerEnable(GP_TIMER, TIMER_A);
}
void task_enable(TASK task, void* data) {
task_status |= (1 << task);
task_data[task] = data;
}
void task_disable(TASK task) {
task_status &= ~(1 << task);
task_data[task] = 0;
}
uint8_t task_running(TASK task) {
if (task_status & (1 << task))
return 1;
return 0;
}
void tasks_loop(void) {
uint8_t serial_active = 0;
#ifdef ENABLE_LCD
double last_x = 0;
double last_y = 0;
double last_z = 0;
#endif
// Main task loop, does not return
// None of the tasks should block, other than
// when performing work.
while (1) {
// Wait for the machine to be ready (available blocks)
if (task_running(TASK_READY_WAIT)) {
if (planner_blocks_available() >= PLANNER_FIFO_READY_THRESHOLD)
{
char tmp[2] = {0x12, 0};
printString(tmp);
task_disable(TASK_READY_WAIT);
}
}
// Process any serial data available
if (task_running(TASK_SERIAL_RX)) {
// Disable Joystick control whilst under Serial control
joystick_disable();
serial_active = 1;
if (gcode_process_data(task_data[TASK_SERIAL_RX]) == 0) {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
task_disable(TASK_SERIAL_RX);
serial_active = 0;
}
}
// Process manual moves
if (task_running(TASK_MANUAL_MOVE)) {
struct task_manual_move_data *move = task_data[TASK_MANUAL_MOVE];
if (planner_blocks_available() >= PLANNER_FIFO_READY_THRESHOLD) {
gcode_manual_move(move->x_offset, move->y_offset, move->rate);
task_disable(TASK_MANUAL_MOVE);
}
}
// Process offset set
if (task_running(TASK_SET_OFFSET)) {
gcode_set_offset_to_current_position();
task_disable(TASK_SET_OFFSET);
}
// Z Motor Run
if (task_running(TASK_MOTOR_DELAY)) {
if (system_time_ms > (uint32_t)task_data[TASK_MOTOR_DELAY])
{
GPIOPinWrite(STEP_DIR_PORT, GPIO_PIN_5 | GPIO_PIN_7, 0);
task_disable(TASK_MOTOR_DELAY);
}
}
#ifdef ENABLE_LCD
// LCD Update
if (task_running(TASK_UPDATE_LCD)) {
if (system_time_ms % 500 == 0)
{
double x = stepper_get_position_x();
double y = stepper_get_position_y();
double z = stepper_get_position_z();
if (x != last_x || y != last_y || z != last_z) {
uint32_t power = control_get_intensity();
block_t *block = planner_get_current_block();
uint32_t ppi = 0;
last_x = x;
last_y = y;
last_z = z;
if (block) {
power = block->laser_pwm * 100 / 255;
ppi = block->laser_ppi;
}
lcd_clear();
lcd_setCursor(0, 0);
lcd_drawstring(" LaserGRBL ");
lcd_drawstring("Power: ");
lcd_drawint(power);
lcd_drawstring("%\n");
lcd_drawstring("PPI: ");
lcd_drawint(ppi);
lcd_drawstring("\n");
lcd_drawstring("X: ");
lcd_drawfloat(x);
lcd_drawstring("\n");
lcd_drawstring("Y: ");
lcd_drawfloat(y);
lcd_drawstring("\n");
lcd_drawstring("Z: ");
lcd_drawfloat(z);
lcd_drawstring("\n");
lcd_display();
}
}
}
#endif // ENABLE_LCD
if (serial_active == 0 && !stepper_active()) {
// Allow Joystick control
joystick_enable();
}
}
}