chiark / gitweb /
magic PROGMEM defines to avoid hundreds of wrong GCC warnings
[marlin.git] / Marlin / Marlin.h
1 // Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
2 // Licence: GPL
3
4 #ifndef __MARLINH
5 #define __MARLINH
6
7 #define  HardwareSerial_h // trick to disable the standard HWserial
8
9 #define  FORCE_INLINE __attribute__((always_inline)) inline
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <inttypes.h>
16
17 #include <util/delay.h>
18 #include <avr/pgmspace.h>
19 #include <avr/eeprom.h>
20 #include  <avr/wdt.h>
21 #include  <avr/interrupt.h>
22
23 //do some magic defines in order to prevent hundreds of wrong warnings in gcc
24 //more info here: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=646359
25 typedef short prog_short PROGMEM;
26 #undef PROGMEM
27 #define PROGMEM __attribute__(( section(".progmem.data") )) 
28 #undef PSTR 
29 #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];})) 
30
31 #include "fastio.h"
32 #include "Configuration.h"
33 #include "pins.h"
34
35 #if ARDUINO >= 100 
36   #if defined(__AVR_ATmega644P__)
37     #include "WProgram.h"
38   #else
39     #include "Arduino.h"
40   #endif
41 #else
42    #include "WProgram.h"
43 #endif
44
45 #include "MarlinSerial.h"
46
47 #ifndef cbi
48 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
49 #endif
50 #ifndef sbi
51 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
52 #endif
53
54 #include "WString.h"
55
56 #if MOTHERBOARD == 8  // Teensylu
57   #define MYSERIAL Serial
58 #else
59   #define MYSERIAL MSerial
60 #endif
61
62 //this is a unfinsihed attemp to removes a lot of warning messages, see:
63 // http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=57011
64 //typedef char prog_char PROGMEM; 
65 // //#define PSTR    (s )        ((const PROGMEM char *)(s))
66 // //# define MYPGM(s) (__extension__({static prog_char __c[] = (s); &__c[0];})) 
67 // //#define MYPGM(s) ((const prog_char *g PROGMEM=s))
68 #define MYPGM(s) PSTR(s)
69 //#define MYPGM(s)  (__extension__({static char __c[] __attribute__((__progmem__)) = (s); &__c[0];}))  //This is the normal behaviour
70 //#define MYPGM(s)  (__extension__({static prog_char __c[]  = (s); &__c[0];})) //this does not work but hides the warnings
71
72
73 #define SERIAL_PROTOCOL(x) MYSERIAL.print(x);
74 #define SERIAL_PROTOCOL_F(x,y) MYSERIAL.print(x,y);
75 #define SERIAL_PROTOCOLPGM(x) serialprintPGM(MYPGM(x));
76 #define SERIAL_PROTOCOLLN(x) {MYSERIAL.print(x);MYSERIAL.write('\n');}
77 #define SERIAL_PROTOCOLLNPGM(x) {serialprintPGM(MYPGM(x));MYSERIAL.write('\n');}
78
79
80 const char errormagic[] PROGMEM ="Error:";
81 const char echomagic[] PROGMEM ="echo:";
82 #define SERIAL_ERROR_START serialprintPGM(errormagic);
83 #define SERIAL_ERROR(x) SERIAL_PROTOCOL(x)
84 #define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x)
85 #define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x)
86 #define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
87
88 #define SERIAL_ECHO_START serialprintPGM(echomagic);
89 #define SERIAL_ECHO(x) SERIAL_PROTOCOL(x)
90 #define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x)
91 #define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x)
92 #define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
93
94 #define SERIAL_ECHOPAIR(name,value) {SERIAL_ECHOPGM(name);SERIAL_ECHO(value);}
95
96
97 //things to write to serial from Programmemory. saves 400 to 2k of RAM.
98 #define SerialprintPGM(x) serialprintPGM(MYPGM(x))
99 FORCE_INLINE void serialprintPGM(const char *str)
100 {
101   char ch=pgm_read_byte(str);
102   while(ch)
103   {
104     MYSERIAL.write(ch);
105     ch=pgm_read_byte(++str);
106   }
107 }
108
109
110 void get_command();
111 void process_commands();
112
113 void manage_inactivity(byte debug);
114
115 #if X_ENABLE_PIN > -1
116   #define  enable_x() WRITE(X_ENABLE_PIN, X_ENABLE_ON)
117   #define disable_x() WRITE(X_ENABLE_PIN,!X_ENABLE_ON)
118 #else
119   #define enable_x() ;
120   #define disable_x() ;
121 #endif
122
123 #if Y_ENABLE_PIN > -1
124   #define  enable_y() WRITE(Y_ENABLE_PIN, Y_ENABLE_ON)
125   #define disable_y() WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON)
126 #else
127   #define enable_y() ;
128   #define disable_y() ;
129 #endif
130
131 #if Z_ENABLE_PIN > -1
132   #define  enable_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
133   #define disable_z() WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON)
134 #else
135   #define enable_z() ;
136   #define disable_z() ;
137 #endif
138
139 #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
140   #define enable_e0() WRITE(E0_ENABLE_PIN, E_ENABLE_ON)
141   #define disable_e0() WRITE(E0_ENABLE_PIN,!E_ENABLE_ON)
142 #else
143   #define enable_e0()  /* nothing */
144   #define disable_e0() /* nothing */
145 #endif
146
147 #if (EXTRUDERS > 1) && defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
148   #define enable_e1() WRITE(E1_ENABLE_PIN, E_ENABLE_ON)
149   #define disable_e1() WRITE(E1_ENABLE_PIN,!E_ENABLE_ON)
150 #else
151   #define enable_e1()  /* nothing */
152   #define disable_e1() /* nothing */
153 #endif
154
155 #if (EXTRUDERS > 2) && defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
156   #define enable_e2() WRITE(E2_ENABLE_PIN, E_ENABLE_ON)
157   #define disable_e2() WRITE(E2_ENABLE_PIN,!E_ENABLE_ON)
158 #else
159   #define enable_e2()  /* nothing */
160   #define disable_e2() /* nothing */
161 #endif
162
163
164 enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3};
165
166
167 void FlushSerialRequestResend();
168 void ClearToSend();
169
170 void get_coordinates();
171 void prepare_move();
172 void kill();
173 void Stop();
174
175 bool IsStopped();
176
177 void enquecommand(const char *cmd); //put an ascii command at the end of the current buffer.
178 void prepare_arc_move(char isclockwise);
179
180 #ifdef FAST_PWM_FAN
181 void setPwmFrequency(uint8_t pin, int val);
182 #endif
183
184 #ifndef CRITICAL_SECTION_START
185   #define CRITICAL_SECTION_START  unsigned char _sreg = SREG; cli();
186   #define CRITICAL_SECTION_END    SREG = _sreg;
187 #endif //CRITICAL_SECTION_START
188
189 extern float homing_feedrate[];
190 extern bool axis_relative_modes[];
191 extern float current_position[NUM_AXIS] ;
192 extern float add_homeing[3];
193 extern unsigned char FanSpeed;
194
195 // Handling multiple extruders pins
196 extern uint8_t active_extruder;
197
198 #endif