Selaa lähdekoodia

🧑‍💻 Adjust FastIO AVR timer enums, macros

Scott Lahteine 2 vuotta sitten
vanhempi
commit
07bffdf4bc

+ 1
- 1
Marlin/src/HAL/AVR/HAL.h Näytä tiedosto

@@ -217,7 +217,7 @@ inline void HAL_adc_init() {
217 217
  *  NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
218 218
  *  NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
219 219
  */
220
-void set_pwm_frequency(const pin_t pin, int f_desired);
220
+void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
221 221
 
222 222
 /**
223 223
  * set_pwm_duty

+ 85
- 63
Marlin/src/HAL/AVR/fast_pwm.cpp Näytä tiedosto

@@ -34,6 +34,24 @@ struct Timer {
34 34
   uint8_t q;                    // the timer output [0->2] (A->C)
35 35
 };
36 36
 
37
+// Macros for the Timer structure
38
+#define _SET_WGMnQ(TCCRnQ, V) do{ \
39
+    *(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << 0)) | (( int(V)       & 0x3) << 0); \
40
+    *(TCCRnQ)[1] = (*(TCCRnQ)[1] & ~(0x3 << 3)) | (((int(V) >> 2) & 0x3) << 3); \
41
+  }while(0)
42
+
43
+// Set TCCR CS bits
44
+#define _SET_CSn(TCCRnQ, V) (*(TCCRnQ)[1] = (*(TCCRnQ[1]) & ~(0x7 << 0)) | ((int(V) & 0x7) << 0))
45
+
46
+// Set TCCR COM bits
47
+#define _SET_COMnQ(TCCRnQ, Q, V) (*(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << (6-2*(Q)))) | (int(V) << (6-2*(Q))))
48
+
49
+// Set OCRnQ register
50
+#define _SET_OCRnQ(OCRnQ, Q, V) (*(OCRnQ)[Q] = int(V) & 0xFFFF)
51
+
52
+// Set ICRn register (one per timer)
53
+#define _SET_ICRn(ICRn, V) (*(ICRn) = int(V) & 0xFFFF)
54
+
37 55
 /**
38 56
  * get_pwm_timer
39 57
  *  Get the timer information and register of the provided pin.
@@ -42,115 +60,119 @@ struct Timer {
42 60
  */
43 61
 Timer get_pwm_timer(const pin_t pin) {
44 62
   uint8_t q = 0;
63
+
45 64
   switch (digitalPinToTimer(pin)) {
46 65
     // Protect reserved timers (TIMER0 & TIMER1)
47 66
     #ifdef TCCR0A
48
-      #if !AVR_AT90USB1286_FAMILY
49
-        case TIMER0A:
50
-      #endif
67
+      IF_DISABLED(AVR_AT90USB1286_FAMILY, case TIMER0A:)
51 68
       case TIMER0B:
52 69
     #endif
53 70
     #ifdef TCCR1A
54 71
       case TIMER1A: case TIMER1B:
55 72
     #endif
56
-                                        break;
57
-    #if HAS_TCCR2 || defined(TCCR2A)
58
-      #if HAS_TCCR2
59
-        case TIMER2: {
73
+
74
+    break;
75
+
76
+    #if HAS_TCCR2
77
+      case TIMER2: {
78
+        Timer timer = {
79
+          { &TCCR2, nullptr, nullptr },
80
+          { (uint16_t*)&OCR2, nullptr, nullptr },
81
+            nullptr,
82
+            2, 0
83
+        };
84
+        return timer;
85
+      }
86
+    #elif defined(TCCR2A)
87
+      #if ENABLED(USE_OCR2A_AS_TOP)
88
+        case TIMER2A:   break; // protect TIMER2A
89
+        case TIMER2B: {
60 90
           Timer timer = {
61
-            /*TCCRnQ*/  { &TCCR2, nullptr, nullptr },
62
-            /*OCRnQ*/   { (uint16_t*)&OCR2, nullptr, nullptr },
63
-            /*ICRn*/      nullptr,
64
-            /*n, q*/      2, 0
91
+            { &TCCR2A,  &TCCR2B,  nullptr },
92
+            { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr },
93
+              nullptr,
94
+              2, 1
65 95
           };
96
+          return timer;
97
+        }
98
+      #else
99
+        case TIMER2B: ++q;
100
+        case TIMER2A: {
101
+          Timer timer = {
102
+            { &TCCR2A,  &TCCR2B,  nullptr },
103
+            { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr },
104
+              nullptr,
105
+              2, q
106
+          };
107
+          return timer;
66 108
         }
67
-      #elif defined(TCCR2A)
68
-        #if ENABLED(USE_OCR2A_AS_TOP)
69
-          case TIMER2A:   break; // protect TIMER2A
70
-          case TIMER2B: {
71
-            Timer timer = {
72
-              /*TCCRnQ*/  { &TCCR2A,  &TCCR2B,  nullptr },
73
-              /*OCRnQ*/   { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr },
74
-              /*ICRn*/      nullptr,
75
-              /*n, q*/      2, 1
76
-            };
77
-            return timer;
78
-          }
79
-        #else
80
-          case TIMER2B:   ++q;
81
-          case TIMER2A: {
82
-            Timer timer = {
83
-              /*TCCRnQ*/  { &TCCR2A,  &TCCR2B,  nullptr },
84
-              /*OCRnQ*/   { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr },
85
-              /*ICRn*/      nullptr,
86
-                            2, q
87
-            };
88
-            return timer;
89
-          }
90
-        #endif
91 109
       #endif
92 110
     #endif
111
+
93 112
     #ifdef OCR3C
94
-      case TIMER3C:   ++q;
95
-      case TIMER3B:   ++q;
113
+      case TIMER3C: ++q;
114
+      case TIMER3B: ++q;
96 115
       case TIMER3A: {
97 116
         Timer timer = {
98
-          /*TCCRnQ*/  { &TCCR3A,  &TCCR3B,  &TCCR3C },
99
-          /*OCRnQ*/   { &OCR3A,   &OCR3B,   &OCR3C },
100
-          /*ICRn*/      &ICR3,
101
-          /*n, q*/      3, q
117
+          { &TCCR3A,  &TCCR3B,  &TCCR3C },
118
+          { &OCR3A,   &OCR3B,   &OCR3C },
119
+            &ICR3,
120
+            3, q
102 121
         };
103 122
         return timer;
104 123
       }
105 124
     #elif defined(OCR3B)
106
-      case TIMER3B:   ++q;
125
+      case TIMER3B: ++q;
107 126
       case TIMER3A: {
108 127
         Timer timer = {
109
-          /*TCCRnQ*/  { &TCCR3A,  &TCCR3B,  nullptr },
110
-          /*OCRnQ*/   { &OCR3A,   &OCR3B,  nullptr },
111
-          /*ICRn*/      &ICR3,
112
-          /*n, q*/      3, q
128
+          { &TCCR3A,  &TCCR3B,  nullptr },
129
+          { &OCR3A,   &OCR3B,  nullptr },
130
+            &ICR3,
131
+            3, q
113 132
         };
114 133
         return timer;
115 134
       }
116 135
     #endif
136
+
117 137
     #ifdef TCCR4A
118
-      case TIMER4C:   ++q;
119
-      case TIMER4B:   ++q;
138
+      case TIMER4C: ++q;
139
+      case TIMER4B: ++q;
120 140
       case TIMER4A: {
121 141
         Timer timer = {
122
-          /*TCCRnQ*/  { &TCCR4A,  &TCCR4B,  &TCCR4C },
123
-          /*OCRnQ*/   { &OCR4A,   &OCR4B,   &OCR4C },
124
-          /*ICRn*/      &ICR4,
125
-          /*n, q*/      4, q
142
+          { &TCCR4A,  &TCCR4B,  &TCCR4C },
143
+          { &OCR4A,   &OCR4B,   &OCR4C },
144
+            &ICR4,
145
+            4, q
126 146
         };
127 147
         return timer;
128 148
       }
129 149
     #endif
150
+
130 151
     #ifdef TCCR5A
131
-      case TIMER5C:   ++q;
132
-      case TIMER5B:   ++q;
152
+      case TIMER5C: ++q;
153
+      case TIMER5B: ++q;
133 154
       case TIMER5A: {
134 155
         Timer timer = {
135
-          /*TCCRnQ*/  { &TCCR5A,  &TCCR5B,  &TCCR5C },
136
-          /*OCRnQ*/   { &OCR5A,   &OCR5B,   &OCR5C },
137
-          /*ICRn*/      &ICR5,
138
-          /*n, q*/      5, q
156
+          { &TCCR5A,  &TCCR5B,  &TCCR5C },
157
+          { &OCR5A,   &OCR5B,   &OCR5C },
158
+            &ICR5,
159
+            5, q
139 160
         };
140 161
         return timer;
141 162
       }
142 163
     #endif
143 164
   }
165
+
144 166
   Timer timer = {
145
-      /*TCCRnQ*/  { nullptr, nullptr, nullptr },
146
-      /*OCRnQ*/   { nullptr, nullptr, nullptr },
147
-      /*ICRn*/      nullptr,
148
-                    0, 0
167
+    { nullptr, nullptr, nullptr },
168
+    { nullptr, nullptr, nullptr },
169
+      nullptr,
170
+      0, 0
149 171
   };
150 172
   return timer;
151 173
 }
152 174
 
153
-void set_pwm_frequency(const pin_t pin, int f_desired) {
175
+void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
154 176
   Timer timer = get_pwm_timer(pin);
155 177
   if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
156 178
   uint16_t size;

+ 13
- 30
Marlin/src/HAL/AVR/fastio.h Näytä tiedosto

@@ -118,7 +118,7 @@
118 118
  */
119 119
 
120 120
 // Waveform Generation Modes
121
-enum WaveGenMode : char {
121
+enum WaveGenMode : uint8_t {
122 122
   WGM_NORMAL,          //  0
123 123
   WGM_PWM_PC_8,        //  1
124 124
   WGM_PWM_PC_9,        //  2
@@ -138,19 +138,19 @@ enum WaveGenMode : char {
138 138
 };
139 139
 
140 140
 // Wavefore Generation Modes (Timer 2 only)
141
-enum WaveGenMode2 : char {
142
-  WGM2_NORMAL,          // 0
143
-  WGM2_PWM_PC,          // 1
144
-  WGM2_CTC_OCR2A,       // 2
145
-  WGM2_FAST_PWM,        // 3
146
-  WGM2_reserved_1,      // 4
147
-  WGM2_PWM_PC_OCR2A,    // 5
148
-  WGM2_reserved_2,      // 6
149
-  WGM2_FAST_PWM_OCR2A,  // 7
141
+enum WaveGenMode2 : uint8_t {
142
+  WGM2_NORMAL,         // 0
143
+  WGM2_PWM_PC,         // 1
144
+  WGM2_CTC_OCR2A,      // 2
145
+  WGM2_FAST_PWM,       // 3
146
+  WGM2_reserved_1,     // 4
147
+  WGM2_PWM_PC_OCR2A,   // 5
148
+  WGM2_reserved_2,     // 6
149
+  WGM2_FAST_PWM_OCR2A, // 7
150 150
 };
151 151
 
152 152
 // Compare Modes
153
-enum CompareMode : char {
153
+enum CompareMode : uint8_t {
154 154
   COM_NORMAL,          //  0
155 155
   COM_TOGGLE,          //  1  Non-PWM: OCnx ... Both PWM (WGM 9,11,14,15): OCnA only ... else NORMAL
156 156
   COM_CLEAR_SET,       //  2  Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
@@ -158,7 +158,7 @@ enum CompareMode : char {
158 158
 };
159 159
 
160 160
 // Clock Sources
161
-enum ClockSource : char {
161
+enum ClockSource : uint8_t {
162 162
   CS_NONE,             //  0
163 163
   CS_PRESCALER_1,      //  1
164 164
   CS_PRESCALER_8,      //  2
@@ -170,7 +170,7 @@ enum ClockSource : char {
170 170
 };
171 171
 
172 172
 // Clock Sources (Timer 2 only)
173
-enum ClockSource2 : char {
173
+enum ClockSource2 : uint8_t {
174 174
   CS2_NONE,            //  0
175 175
   CS2_PRESCALER_1,     //  1
176 176
   CS2_PRESCALER_8,     //  2
@@ -203,11 +203,6 @@ enum ClockSource2 : char {
203 203
     TCCR##T##B = (TCCR##T##B & ~(0x3 << WGM##T##2)) | (((int(V) >> 2) & 0x3) << WGM##T##2); \
204 204
   }while(0)
205 205
 #define SET_WGM(T,V) _SET_WGM(T,WGM_##V)
206
-// Runtime (see set_pwm_frequency):
207
-#define _SET_WGMnQ(TCCRnQ, V) do{ \
208
-    *(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << 0)) | (( int(V)       & 0x3) << 0); \
209
-    *(TCCRnQ)[1] = (*(TCCRnQ)[1] & ~(0x3 << 3)) | (((int(V) >> 2) & 0x3) << 3); \
210
-  }while(0)
211 206
 
212 207
 // Set Clock Select bits
213 208
 // Ex: SET_CS3(PRESCALER_64);
@@ -235,8 +230,6 @@ enum ClockSource2 : char {
235 230
 #define SET_CS4(V) _SET_CS4(CS_##V)
236 231
 #define SET_CS5(V) _SET_CS5(CS_##V)
237 232
 #define SET_CS(T,V) SET_CS##T(V)
238
-// Runtime (see set_pwm_frequency)
239
-#define _SET_CSn(TCCRnQ, V) (*(TCCRnQ)[1] = (*(TCCRnQ[1]) & ~(0x7 << 0)) | ((int(V) & 0x7) << 0))
240 233
 
241 234
 // Set Compare Mode bits
242 235
 // Ex: SET_COMS(4,CLEAR_SET,CLEAR_SET,CLEAR_SET);
@@ -246,16 +239,6 @@ enum ClockSource2 : char {
246 239
 #define SET_COMB(T,V) SET_COM(T,B,V)
247 240
 #define SET_COMC(T,V) SET_COM(T,C,V)
248 241
 #define SET_COMS(T,V1,V2,V3) do{ SET_COMA(T,V1); SET_COMB(T,V2); SET_COMC(T,V3); }while(0)
249
-// Runtime (see set_pwm_duty)
250
-#define _SET_COMnQ(TCCRnQ, Q, V) (*(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << (6-2*(Q)))) | (int(V) << (6-2*(Q))))
251
-
252
-// Set OCRnQ register
253
-// Runtime (see set_pwm_duty):
254
-#define _SET_OCRnQ(OCRnQ, Q, V) (*(OCRnQ)[Q] = int(V) & 0xFFFF)
255
-
256
-// Set ICRn register (one per timer)
257
-// Runtime (see set_pwm_frequency)
258
-#define _SET_ICRn(ICRn, V) (*(ICRn) = int(V) & 0xFFFF)
259 242
 
260 243
 // Set Noise Canceler bit
261 244
 // Ex: SET_ICNC(2,1)

+ 1
- 1
Marlin/src/HAL/LPC1768/HAL.h Näytä tiedosto

@@ -206,7 +206,7 @@ void flashFirmware(const int16_t);
206 206
  *  All Hardware PWM pins run at the same frequency and all
207 207
  *  Software PWM pins run at the same frequency
208 208
  */
209
-void set_pwm_frequency(const pin_t pin, int f_desired);
209
+void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
210 210
 
211 211
 /**
212 212
  * set_pwm_duty

+ 1
- 1
Marlin/src/HAL/LPC1768/fast_pwm.cpp Näytä tiedosto

@@ -32,7 +32,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
32 32
 
33 33
 #if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
34 34
 
35
-  void set_pwm_frequency(const pin_t pin, int f_desired) {
35
+  void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
36 36
     LPC176x::pwm_set_frequency(pin, f_desired);
37 37
   }
38 38
 

+ 1
- 1
Marlin/src/HAL/STM32/HAL.h Näytä tiedosto

@@ -231,7 +231,7 @@ extern volatile uint32_t systick_uptime_millis;
231 231
  *  Set the frequency of the timer corresponding to the provided pin
232 232
  *  All Timer PWM pins run at the same frequency
233 233
  */
234
-void set_pwm_frequency(const pin_t pin, int f_desired);
234
+void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
235 235
 
236 236
 /**
237 237
  * set_pwm_duty

+ 1
- 1
Marlin/src/HAL/STM32/fast_pwm.cpp Näytä tiedosto

@@ -56,7 +56,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
56 56
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
57 57
 }
58 58
 
59
-void set_pwm_frequency(const pin_t pin, int f_desired) {
59
+void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
60 60
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
61 61
   const PinName pin_name = digitalPinToPinName(pin);
62 62
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance

+ 1
- 1
Marlin/src/HAL/STM32F1/HAL.h Näytä tiedosto

@@ -274,7 +274,7 @@ void flashFirmware(const int16_t);
274 274
  *  Set the frequency of the timer corresponding to the provided pin
275 275
  *  All Timer PWM pins run at the same frequency
276 276
  */
277
-void set_pwm_frequency(const pin_t pin, int f_desired);
277
+void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
278 278
 
279 279
 /**
280 280
  * set_pwm_duty

+ 1
- 1
Marlin/src/HAL/STM32F1/fast_pwm.cpp Näytä tiedosto

@@ -51,7 +51,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
51 51
   timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
52 52
 }
53 53
 
54
-void set_pwm_frequency(const pin_t pin, int f_desired) {
54
+void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
55 55
   if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
56 56
 
57 57
   timer_dev *timer; UNUSED(timer);

Loading…
Peruuta
Tallenna