Переглянути джерело

Update fastio.h with special handling for Timer 2

Scott Lahteine 7 роки тому
джерело
коміт
2823bf0874
3 змінених файлів з 97 додано та 38 видалено
  1. 7
    8
      Marlin/Marlin_main.cpp
  2. 87
    27
      Marlin/fastio.h
  3. 3
    3
      Marlin/stepper.cpp

+ 7
- 8
Marlin/Marlin_main.cpp Переглянути файл

@@ -11569,47 +11569,46 @@ void prepare_move_to_destination() {
11569 11569
       #ifdef TCCR0A
11570 11570
         case TIMER0A:
11571 11571
         case TIMER0B:
11572
-          //SET_CS(0, val);
11572
+          //_SET_CS(0, val);
11573 11573
           break;
11574 11574
       #endif
11575 11575
       #ifdef TCCR1A
11576 11576
         case TIMER1A:
11577 11577
         case TIMER1B:
11578
-          //SET_CS(1, val);
11578
+          //_SET_CS(1, val);
11579 11579
           break;
11580 11580
       #endif
11581 11581
       #ifdef TCCR2
11582 11582
         case TIMER2:
11583 11583
         case TIMER2:
11584
-          TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
11585
-          TCCR2 |= val;
11584
+          _SET_CS(2, val);
11586 11585
           break;
11587 11586
       #endif
11588 11587
       #ifdef TCCR2A
11589 11588
         case TIMER2A:
11590 11589
         case TIMER2B:
11591
-          SET_CS(2, val);
11590
+          _SET_CS(2, val);
11592 11591
           break;
11593 11592
       #endif
11594 11593
       #ifdef TCCR3A
11595 11594
         case TIMER3A:
11596 11595
         case TIMER3B:
11597 11596
         case TIMER3C:
11598
-          SET_CS(3, val);
11597
+          _SET_CS(3, val);
11599 11598
           break;
11600 11599
       #endif
11601 11600
       #ifdef TCCR4A
11602 11601
         case TIMER4A:
11603 11602
         case TIMER4B:
11604 11603
         case TIMER4C:
11605
-          SET_CS(4, val);
11604
+          _SET_CS(4, val);
11606 11605
           break;
11607 11606
       #endif
11608 11607
       #ifdef TCCR5A
11609 11608
         case TIMER5A:
11610 11609
         case TIMER5B:
11611 11610
         case TIMER5C:
11612
-          SET_CS(5, val);
11611
+          _SET_CS(5, val);
11613 11612
           break;
11614 11613
       #endif
11615 11614
     }

+ 87
- 27
Marlin/fastio.h Переглянути файл

@@ -85,7 +85,7 @@
85 85
 #define OUT_WRITE(IO, v) do{ SET_OUTPUT(IO); WRITE(IO, v); }while(0)
86 86
 
87 87
 /**
88
- * Interrupt Control
88
+ * Timer and Interrupt Control
89 89
  */
90 90
 
91 91
 // Waveform Generation Modes
@@ -128,24 +128,86 @@ typedef enum {
128 128
   CS_EXT_RISING        //  7
129 129
 } ClockSource;
130 130
 
131
-#define SET_WGM(T,V) do{ \
131
+// Clock Sources (Timer 2 only)
132
+typedef enum {
133
+  CS2_NONE,            //  0
134
+  CS2_PRESCALER_1,     //  1
135
+  CS2_PRESCALER_8,     //  2
136
+  CS2_PRESCALER_32,    //  3
137
+  CS2_PRESCALER_64,    //  4
138
+  CS2_PRESCALER_128,   //  5
139
+  CS2_PRESCALER_256,   //  6
140
+  CS2_PRESCALER_1024   //  7
141
+} ClockSource2;
142
+
143
+// Get interrupt bits in an orderly way
144
+#define GET_WGM(T)   (((TCCR##T##A >> WGM##T##0) & 0x3) | ((TCCR##T##B >> WGM##T##2 << 2) & 0xC))
145
+#define GET_CS(T)    ((TCCR##T##B >> CS##T##0) & 0x7)
146
+#define GET_COM(T,Q) ((TCCR##T##Q >> COM##T##Q##0) & 0x3)
147
+#define GET_COMA(T)  GET_COM(T,A)
148
+#define GET_COMB(T)  GET_COM(T,B)
149
+#define GET_COMC(T)  GET_COM(T,C)
150
+#define GET_ICNC(T)  (!!(TCCR##T##B & _BV(ICNC##T)))
151
+#define GET_ICES(T)  (!!(TCCR##T##B & _BV(ICES##T)))
152
+#define GET_FOC(T,Q) (!!(TCCR##T##C & _BV(FOC##T##Q)))
153
+#define GET_FOCA(T)  GET_FOC(T,A)
154
+#define GET_FOCB(T)  GET_FOC(T,B)
155
+#define GET_FOCC(T)  GET_FOC(T,C)
156
+
157
+// Set Wave Generation Mode bits
158
+#define _SET_WGM(T,V) do{ \
132 159
     TCCR##T##A = (TCCR##T##A & ~(0x3 << WGM##T##0)) | (( int(V)       & 0x3) << WGM##T##0); \
133 160
     TCCR##T##B = (TCCR##T##B & ~(0x3 << WGM##T##2)) | (((int(V) >> 2) & 0x3) << WGM##T##2); \
134 161
   }while(0)
135
-
136
-#define SET_CS(T,V) do{ \
137
-    TCCR##T##B = (TCCR##T##B & ~(0x7 << CS10)) | ((int(V) & 0x7) << CS10); \
138
-  }while(0)
139
-
140
-#define SET_COM(T,Q,V) do{ \
141
-    TCCR##T##Q = (TCCR##T##Q & ~(0x3 << COM1##Q##0)) | ((int(V) & 0x3) << COM1##Q##0); \
142
-  }while(0)
162
+#define SET_WGM(T,V) _SET_WGM(T,WGM_##V)
163
+
164
+// Set Clock Select bits
165
+#define _SET_CS(T,V) (TCCR##T##B = (TCCR##T##B & ~(0x7 << CS##T##0)) | ((int(V) & 0x7) << CS##T##0))
166
+#define _SET_CS0(V) _SET_CS(0,V)
167
+#define _SET_CS1(V) _SET_CS(1,V)
168
+#ifdef TCCR2
169
+  #define _SET_CS2(V) (TCCR2 = (TCCR2 & ~(0x7 << CS20)) | (int(V) << CS20))
170
+#else
171
+  #define _SET_CS2(V) _SET_CS(2,V)
172
+#endif
173
+#define _SET_CS3(V) _SET_CS(3,V)
174
+#define _SET_CS4(V) _SET_CS(4,V)
175
+#define _SET_CS5(V) _SET_CS(5,V)
176
+#define SET_CS0(V) _SET_CS0(CS_##V)
177
+#define SET_CS1(V) _SET_CS1(CS_##V)
178
+#ifdef TCCR2
179
+  #define SET_CS2(V) _SET_CS2(CS2_##V)
180
+#else
181
+  #define SET_CS2(V) _SET_CS2(CS_##V)
182
+#endif
183
+#define SET_CS3(V) _SET_CS3(CS_##V)
184
+#define SET_CS4(V) _SET_CS4(CS_##V)
185
+#define SET_CS5(V) _SET_CS5(CS_##V)
186
+#define SET_CS(T,V) SET_CS##T(V)
187
+
188
+// Set Compare Mode bits
189
+#define _SET_COM(T,Q,V) (TCCR##T##Q = (TCCR##T##Q & ~(0x3 << COM##T##Q##0)) | (int(V) << COM##T##Q##0))
190
+#define _SET_COMA(T,V) _SET_COM(T,A,V)
191
+#define _SET_COMB(T,V) _SET_COM(T,B,V)
192
+#define _SET_COMC(T,V) _SET_COM(T,C,V)
193
+#define _SET_COMS(T,V1,V2,V3) do{ _SET_COMA(T,V1); _SET_COMB(T,V2); _SET_COMC(T,V3); }while(0)
194
+#define SET_COM(T,Q,V) _SET_COM(T,Q,COM_##V)
143 195
 #define SET_COMA(T,V) SET_COM(T,A,V)
144 196
 #define SET_COMB(T,V) SET_COM(T,B,V)
145
-#define SET_COMS(T,V1,V2) do{ SET_COMA(T,V1); SET_COMB(T,V2); }while(0)
197
+#define SET_COMC(T,V) SET_COM(T,C,V)
198
+#define SET_COMS(T,V1,V2,V3) do{ SET_COMA(T,V1); SET_COMB(T,V2); SET_COMC(T,V3); }while(0)
199
+
200
+// Set Noise Canceler bit
201
+#define SET_ICNC(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICNC##T) : TCCR##T##B & ~_BV(ICNC##T))
202
+
203
+// Set Input Capture Edge Select bit
204
+#define SET_ICES(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICES##T) : TCCR##T##B & ~_BV(ICES##T))
146 205
 
147
-#define SET_ICNC(T,V) (TCCR##T##B = (TCCR##T##B & ~_BV(7) | ((V) & 1) << 7))
148
-#define SET_ICES(T,V) (TCCR##T##B = (TCCR##T##B & ~_BV(6) | ((V) & 1) << 6))
206
+// Set Force Output Compare bit
207
+#define SET_FOC(T,Q,V) (TCCR##T##C = (V) ? TCCR##T##C | _BV(FOC##T##Q) : TCCR##T##C & ~_BV(FOC##T##Q))
208
+#define SET_FOCA(T,V) SET_FOC(T,A,V)
209
+#define SET_FOCB(T,V) SET_FOC(T,B,V)
210
+#define SET_FOCC(T,V) SET_FOC(T,C,V)
149 211
 
150 212
 /**
151 213
  * Ports and Functions
@@ -177,8 +239,8 @@ typedef enum {
177 239
   #define DEBUG_LED   AIO5
178 240
 
179 241
   /**
180
-  pins
181
-  */
242
+   * Pins Info
243
+   */
182 244
 
183 245
   #define DIO0_PIN    PIND0
184 246
   #define DIO0_RPORT  PIND
@@ -513,9 +575,10 @@ typedef enum {
513 575
   #define OC2B        DIO14
514 576
 
515 577
   #define DEBUG_LED   DIO0
578
+
516 579
   /**
517
-  pins
518
-  */
580
+   * Pins Info
581
+   */
519 582
 
520 583
   #define DIO0_PIN    PINB0
521 584
   #define DIO0_RPORT  PINB
@@ -757,8 +820,6 @@ typedef enum {
757 820
   #define AIO7_DDR    DDRA
758 821
   #define AIO7_PWM    NULL
759 822
 
760
-
761
-
762 823
   #undef PA0
763 824
   #define PA0_PIN     PINA0
764 825
   #define PA0_RPORT   PINA
@@ -1023,8 +1084,9 @@ typedef enum {
1023 1084
   #define DEBUG_LED   DIO21
1024 1085
 
1025 1086
   /**
1026
-  pins
1027
-  */
1087
+   * Pins Info
1088
+   */
1089
+
1028 1090
   #define DIO0_PIN    PINE0
1029 1091
   #define DIO0_RPORT  PINE
1030 1092
   #define DIO0_WPORT  PORTE
@@ -2075,7 +2137,7 @@ typedef enum {
2075 2137
   #define DEBUG_LED   DIO31 /* led D5 red */
2076 2138
 
2077 2139
   /**
2078
-   * pins
2140
+   * Pins Info
2079 2141
    */
2080 2142
 
2081 2143
   //#define AT90USBxx_TEENSYPP_ASSIGNMENTS // Use Teensy++ 2.0 assignments
@@ -3402,8 +3464,9 @@ typedef enum {
3402 3464
   #define DEBUG_LED   DIO46
3403 3465
 
3404 3466
   /**
3405
-  pins
3406
-  */
3467
+   * Pins Info
3468
+   */
3469
+
3407 3470
   #define DIO0_PIN    PINE0
3408 3471
   #define DIO0_RPORT  PINE
3409 3472
   #define DIO0_WPORT  PORTE
@@ -3728,9 +3791,6 @@ typedef enum {
3728 3791
   #define DIO53_DDR   DDRF
3729 3792
   #define DIO53_PWM   NULL
3730 3793
 
3731
-
3732
-
3733
-
3734 3794
   #undef PA0
3735 3795
   #define PA0_PIN     PINA0
3736 3796
   #define PA0_RPORT   PINA

+ 3
- 3
Marlin/stepper.cpp Переглянути файл

@@ -1081,17 +1081,17 @@ void Stepper::init() {
1081 1081
   #endif
1082 1082
 
1083 1083
   // waveform generation = 0100 = CTC
1084
-  SET_WGM(1, WGM_CTC_OCRnA);
1084
+  SET_WGM(1, CTC_OCRnA);
1085 1085
 
1086 1086
   // output mode = 00 (disconnected)
1087
-  SET_COMS(1, COM_NORMAL, COM_NORMAL);
1087
+  SET_COMS(1, NORMAL, NORMAL, NORMAL);
1088 1088
 
1089 1089
   // Set the timer pre-scaler
1090 1090
   // Generally we use a divider of 8, resulting in a 2MHz timer
1091 1091
   // frequency on a 16MHz MCU. If you are going to change this, be
1092 1092
   // sure to regenerate speed_lookuptable.h with
1093 1093
   // create_speed_lookuptable.py
1094
-  SET_CS(1, CS_PRESCALER_8);  //  CS 2 = 1/8 prescaler
1094
+  SET_CS(1, PRESCALER_8);  //  CS 2 = 1/8 prescaler
1095 1095
 
1096 1096
   // Init Stepper ISR to 122 Hz for quick starting
1097 1097
   OCR1A = 0x4000;

Завантаження…
Відмінити
Зберегти