Browse Source

🧑‍💻 Simplify Fast PWM timer macros

Scott Lahteine 2 years ago
parent
commit
afdaa17964
1 changed files with 16 additions and 16 deletions
  1. 16
    16
      Marlin/src/HAL/AVR/fast_pwm.cpp

+ 16
- 16
Marlin/src/HAL/AVR/fast_pwm.cpp View File

34
 };
34
 };
35
 
35
 
36
 // Macros for the Timer structure
36
 // Macros for the Timer structure
37
-#define _SET_WGMnQ(TCCRnQ, V) do{ \
38
-    *(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << 0)) | (( int(V)       & 0x3) << 0); \
39
-    *(TCCRnQ)[1] = (*(TCCRnQ)[1] & ~(0x3 << 3)) | (((int(V) >> 2) & 0x3) << 3); \
37
+#define _SET_WGMnQ(T, V) do{ \
38
+    *(T.TCCRnQ)[0] = (*(T.TCCRnQ)[0] & ~(0x3 << 0)) | (( int(V)       & 0x3) << 0); \
39
+    *(T.TCCRnQ)[1] = (*(T.TCCRnQ)[1] & ~(0x3 << 3)) | (((int(V) >> 2) & 0x3) << 3); \
40
   }while(0)
40
   }while(0)
41
 
41
 
42
 // Set TCCR CS bits
42
 // Set TCCR CS bits
43
-#define _SET_CSn(TCCRnQ, V) (*(TCCRnQ)[1] = (*(TCCRnQ[1]) & ~(0x7 << 0)) | ((int(V) & 0x7) << 0))
43
+#define _SET_CSn(T, V) (*(T.TCCRnQ)[1] = (*(T.TCCRnQ[1]) & ~(0x7 << 0)) | ((int(V) & 0x7) << 0))
44
 
44
 
45
 // Set TCCR COM bits
45
 // Set TCCR COM bits
46
-#define _SET_COMnQ(TCCRnQ, Q, V) (*(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << (6-2*(Q)))) | (int(V) << (6-2*(Q))))
46
+#define _SET_COMnQ(T, Q, V) (*(T.TCCRnQ)[0] = (*(T.TCCRnQ)[0] & ~(0x3 << (6-2*(Q)))) | (int(V) << (6-2*(Q))))
47
 
47
 
48
 // Set OCRnQ register
48
 // Set OCRnQ register
49
-#define _SET_OCRnQ(OCRnQ, Q, V) (*(OCRnQ)[Q] = int(V) & 0xFFFF)
49
+#define _SET_OCRnQ(T, Q, V) (*(T.OCRnQ)[Q] = int(V) & 0xFFFF)
50
 
50
 
51
 // Set ICRn register (one per timer)
51
 // Set ICRn register (one per timer)
52
-#define _SET_ICRn(ICRn, V) (*(ICRn) = int(V) & 0xFFFF)
52
+#define _SET_ICRn(T, V) (*(T.ICRn) = int(V) & 0xFFFF)
53
 
53
 
54
 /**
54
 /**
55
  * Return a Timer struct describing a pin's timer.
55
  * Return a Timer struct describing a pin's timer.
56
  */
56
  */
57
-Timer get_pwm_timer(const pin_t pin) {
57
+const Timer get_pwm_timer(const pin_t pin) {
58
 
58
 
59
   uint8_t q = 0;
59
   uint8_t q = 0;
60
 
60
 
108
 }
108
 }
109
 
109
 
110
 void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
110
 void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
111
-  Timer timer = get_pwm_timer(pin);
111
+  const Timer timer = get_pwm_timer(pin);
112
   if (timer.isProtected || !timer.isPWM) return; // Don't proceed if protected timer or not recognized
112
   if (timer.isProtected || !timer.isPWM) return; // Don't proceed if protected timer or not recognized
113
 
113
 
114
   const bool is_timer2 = timer.n == 2;
114
   const bool is_timer2 = timer.n == 2;
166
     }
166
     }
167
   }
167
   }
168
 
168
 
169
-  _SET_WGMnQ(timer.TCCRnQ, wgm);
170
-  _SET_CSn(timer.TCCRnQ, j);
169
+  _SET_WGMnQ(timer, wgm);
170
+  _SET_CSn(timer, j);
171
 
171
 
172
   if (is_timer2) {
172
   if (is_timer2) {
173
-    TERN_(USE_OCR2A_AS_TOP, _SET_OCRnQ(timer.OCRnQ, 0, res)); // Set OCR2A value (TOP) = res
173
+    TERN_(USE_OCR2A_AS_TOP, _SET_OCRnQ(timer, 0, res)); // Set OCR2A value (TOP) = res
174
   }
174
   }
175
   else
175
   else
176
-    _SET_ICRn(timer.ICRn, res);                               // Set ICRn value (TOP) = res
176
+    _SET_ICRn(timer, res);                              // Set ICRn value (TOP) = res
177
 }
177
 }
178
 
178
 
179
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
179
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
184
   else if (v == v_size)
184
   else if (v == v_size)
185
     digitalWrite(pin, !invert);
185
     digitalWrite(pin, !invert);
186
   else {
186
   else {
187
-    Timer timer = get_pwm_timer(pin);
187
+    const Timer timer = get_pwm_timer(pin);
188
     if (timer.isPWM) {
188
     if (timer.isPWM) {
189
       if (timer.n == 0) {
189
       if (timer.n == 0) {
190
         TCCR0A |= _BV(COM0B1); // Only allow a TIMER0B select and OCR0B duty update for pin D4 outputs no frequency changes are permited.
190
         TCCR0A |= _BV(COM0B1); // Only allow a TIMER0B select and OCR0B duty update for pin D4 outputs no frequency changes are permited.
192
       }
192
       }
193
       else if (!timer.isProtected) {
193
       else if (!timer.isProtected) {
194
         const uint16_t top = timer.n == 2 ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
194
         const uint16_t top = timer.n == 2 ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
195
-        _SET_COMnQ(timer.TCCRnQ, SUM_TERN(HAS_TCCR2, timer.q, timer.q == 2), COM_CLEAR_SET + invert);   // COM20 is on bit 4 of TCCR2, so +1 for q==2
196
-        _SET_OCRnQ(timer.OCRnQ, timer.q, uint16_t(uint32_t(v) * top / v_size)); // Scale 8/16-bit v to top value
195
+        _SET_COMnQ(timer, SUM_TERN(HAS_TCCR2, timer.q, timer.q == 2), COM_CLEAR_SET + invert);   // COM20 is on bit 4 of TCCR2, so +1 for q==2
196
+        _SET_OCRnQ(timer, timer.q, uint16_t(uint32_t(v) * top / v_size)); // Scale 8/16-bit v to top value
197
       }
197
       }
198
     }
198
     }
199
     else
199
     else

Loading…
Cancel
Save