forked from Traumflug/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mendel.c
216 lines (175 loc) · 5.34 KB
/
mendel.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/** \file
\brief Main file - this is where it all starts, and ends
*/
/** \mainpage Teacup Reprap Firmware
\section intro_sec Introduction
Teacup Reprap Firmware (originally named FiveD on Arduino) is a firmware package for numerous reprap electronics sets.
Please see README for a full introduction and long-winded waffle about this project
\section install_sec Installation
\subsection step1 Step 1: Download
\code git clone git://github.com/traumflug/Teacup_Firmware \endcode
\subsection step2 Step 2: configure
\code cp config.[yourboardhere].h config.h \endcode
Edit config.h to suit your machone
Edit Makefile to select the correct chip and programming settings
\subsection step3 Step 3: Compile
\code make \endcode
\code make program \endcode
\subsection step4 Step 4: Test!
\code ./func.sh mendel_reset
./func.sh mendel_talk
M115
ctrl+d \endcode
*/
#ifdef __AVR__
#include <avr/interrupt.h>
#endif
#include "config_wrapper.h"
#include "cpu.h"
#include "serial.h"
#include "dda_queue.h"
#include "gcode_parse.h"
#include "timer.h"
#include "temp.h"
#include "watchdog.h"
#include "debug.h"
#include "heater.h"
#include "analog.h"
#include "pinio.h"
#include "clock.h"
#include "intercom.h"
#include "spi.h"
#include "sd.h"
#include "display.h"
#ifdef SIMINFO
#include "../simulavr/src/simulavr_info.h"
SIMINFO_DEVICE(MCU_STR);
SIMINFO_CPUFREQUENCY(F_CPU);
#ifdef BAUD
SIMINFO_SERIAL_IN("D0", "-", BAUD);
SIMINFO_SERIAL_OUT("D1", "-", BAUD);
#endif
#endif
#ifdef CANNED_CYCLE
const char PROGMEM canned_gcode_P[] = CANNED_CYCLE;
#endif
/** Initialise all the subsystems.
Note that order of appearance is critical here. For example, running
spi_init() before io_init() makes SPI fail (for reasons not exactly
investigated).
*/
void init(void) {
cpu_init();
// set up watchdog
wd_init();
// set up serial
serial_init();
// set up G-code parsing
gcode_init();
// set up inputs and outputs
pinio_init();
#ifdef SPI
spi_init();
#endif
// set up timers
timer_init();
heater_init();
// set up dda
dda_init();
// start up analog read interrupt loop,
// if any of the temp sensors in your config.h use analog interface
analog_init();
// set up temperature inputs
temp_init();
#ifdef SD
sd_init();
#endif
// enable interrupts
sei();
// reset watchdog
wd_reset();
// prepare the power supply
power_init();
#ifdef DISPLAY
display_init();
display_greeting();
#endif
// say hi to host
serial_writestr_P(PSTR("start\nok\n"));
}
/// this is where it all starts, and ends
///
/// just run init(), then run an endless loop where we pass characters from the serial RX buffer to gcode_parse_char() and check the clocks
#ifdef SIMULATOR
int main (int argc, char** argv)
{
sim_start(argc, argv);
#else
int main (void)
{
#endif
uint8_t c, line_done, ack_waiting = 0;
init();
// main loop
for (;;)
{
// if queue is full, no point in reading chars- host will just have to wait
if (queue_full() == 0) {
/**
Postpone sending acknowledgement until there's a free slot in the
movement queue. This way the host waits with sending the next line
until it can be processed immediately. As a result, the serial receive
queue is always almost empty; it exists only for receiving via XON/XOFF
flow control. Another result is, the incoming line can be longer than
the receiving buffer, see Github issue #52.
At the time of the introduction of this strategy gcode_parse_char()
parsed a single character in 100 to 400 CPU clocks, processing
the line end took some 30'000 clocks. 115200 baud mean one character
incoming every about 1250 CPU clocks on AVR 16 MHz.
*/
if (ack_waiting) {
serial_writestr_P(PSTR("ok\n"));
ack_waiting = 0;
}
if (( ! gcode_active || gcode_active & GCODE_SOURCE_SERIAL) &&
serial_rxchars() != 0) {
gcode_active = GCODE_SOURCE_SERIAL;
c = serial_popchar();
line_done = gcode_parse_char(c);
if (line_done) {
gcode_active = 0;
ack_waiting = 1;
}
}
#ifdef SD
if (( ! gcode_active || gcode_active & GCODE_SOURCE_SD) &&
gcode_sources & GCODE_SOURCE_SD) {
if (sd_read_gcode_line()) {
serial_writestr_P(PSTR("\nSD file done.\n"));
gcode_sources &= ! GCODE_SOURCE_SD;
// There is no pf_close(), subsequent reads will stick at EOF
// and return zeros.
}
}
#endif
#ifdef CANNED_CYCLE
/**
WARNING!
This code works on a per-character basis.
Unlike with SD reading code above and for historical reasons (was
a quicky doing its job, before SD card was implemented), any data
received over serial WILL be randomly distributed through the canned
G-code, and you'll have a big mess!
The solution is to join the strategy above and make canned G-code
a third G-code source next to serial and SD.
*/
static uint32_t canned_gcode_pos = 0;
gcode_parse_char(pgm_read_byte(&(canned_gcode_P[canned_gcode_pos])));
canned_gcode_pos++;
if (pgm_read_byte(&(canned_gcode_P[canned_gcode_pos])) == 0)
canned_gcode_pos = 0;
#endif /* CANNED_CYCLE */
}
clock();
}
}