Browse Source

🧑‍💻 Adjust FastIO AVR timer enums, macros

Scott Lahteine 3 years ago
parent
commit
07bffdf4bc

+ 1
- 1
Marlin/src/HAL/AVR/HAL.h View File

217
  *  NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
217
  *  NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
218
  *  NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
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
  * set_pwm_duty
223
  * set_pwm_duty

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

34
   uint8_t q;                    // the timer output [0->2] (A->C)
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
  * get_pwm_timer
56
  * get_pwm_timer
39
  *  Get the timer information and register of the provided pin.
57
  *  Get the timer information and register of the provided pin.
42
  */
60
  */
43
 Timer get_pwm_timer(const pin_t pin) {
61
 Timer get_pwm_timer(const pin_t pin) {
44
   uint8_t q = 0;
62
   uint8_t q = 0;
63
+
45
   switch (digitalPinToTimer(pin)) {
64
   switch (digitalPinToTimer(pin)) {
46
     // Protect reserved timers (TIMER0 & TIMER1)
65
     // Protect reserved timers (TIMER0 & TIMER1)
47
     #ifdef TCCR0A
66
     #ifdef TCCR0A
48
-      #if !AVR_AT90USB1286_FAMILY
49
-        case TIMER0A:
50
-      #endif
67
+      IF_DISABLED(AVR_AT90USB1286_FAMILY, case TIMER0A:)
51
       case TIMER0B:
68
       case TIMER0B:
52
     #endif
69
     #endif
53
     #ifdef TCCR1A
70
     #ifdef TCCR1A
54
       case TIMER1A: case TIMER1B:
71
       case TIMER1A: case TIMER1B:
55
     #endif
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
           Timer timer = {
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
       #endif
109
       #endif
92
     #endif
110
     #endif
111
+
93
     #ifdef OCR3C
112
     #ifdef OCR3C
94
-      case TIMER3C:   ++q;
95
-      case TIMER3B:   ++q;
113
+      case TIMER3C: ++q;
114
+      case TIMER3B: ++q;
96
       case TIMER3A: {
115
       case TIMER3A: {
97
         Timer timer = {
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
         return timer;
122
         return timer;
104
       }
123
       }
105
     #elif defined(OCR3B)
124
     #elif defined(OCR3B)
106
-      case TIMER3B:   ++q;
125
+      case TIMER3B: ++q;
107
       case TIMER3A: {
126
       case TIMER3A: {
108
         Timer timer = {
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
         return timer;
133
         return timer;
115
       }
134
       }
116
     #endif
135
     #endif
136
+
117
     #ifdef TCCR4A
137
     #ifdef TCCR4A
118
-      case TIMER4C:   ++q;
119
-      case TIMER4B:   ++q;
138
+      case TIMER4C: ++q;
139
+      case TIMER4B: ++q;
120
       case TIMER4A: {
140
       case TIMER4A: {
121
         Timer timer = {
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
         return timer;
147
         return timer;
128
       }
148
       }
129
     #endif
149
     #endif
150
+
130
     #ifdef TCCR5A
151
     #ifdef TCCR5A
131
-      case TIMER5C:   ++q;
132
-      case TIMER5B:   ++q;
152
+      case TIMER5C: ++q;
153
+      case TIMER5B: ++q;
133
       case TIMER5A: {
154
       case TIMER5A: {
134
         Timer timer = {
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
         return timer;
161
         return timer;
141
       }
162
       }
142
     #endif
163
     #endif
143
   }
164
   }
165
+
144
   Timer timer = {
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
   return timer;
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
   Timer timer = get_pwm_timer(pin);
176
   Timer timer = get_pwm_timer(pin);
155
   if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
177
   if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
156
   uint16_t size;
178
   uint16_t size;

+ 13
- 30
Marlin/src/HAL/AVR/fastio.h View File

118
  */
118
  */
119
 
119
 
120
 // Waveform Generation Modes
120
 // Waveform Generation Modes
121
-enum WaveGenMode : char {
121
+enum WaveGenMode : uint8_t {
122
   WGM_NORMAL,          //  0
122
   WGM_NORMAL,          //  0
123
   WGM_PWM_PC_8,        //  1
123
   WGM_PWM_PC_8,        //  1
124
   WGM_PWM_PC_9,        //  2
124
   WGM_PWM_PC_9,        //  2
138
 };
138
 };
139
 
139
 
140
 // Wavefore Generation Modes (Timer 2 only)
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
 // Compare Modes
152
 // Compare Modes
153
-enum CompareMode : char {
153
+enum CompareMode : uint8_t {
154
   COM_NORMAL,          //  0
154
   COM_NORMAL,          //  0
155
   COM_TOGGLE,          //  1  Non-PWM: OCnx ... Both PWM (WGM 9,11,14,15): OCnA only ... else NORMAL
155
   COM_TOGGLE,          //  1  Non-PWM: OCnx ... Both PWM (WGM 9,11,14,15): OCnA only ... else NORMAL
156
   COM_CLEAR_SET,       //  2  Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
156
   COM_CLEAR_SET,       //  2  Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
158
 };
158
 };
159
 
159
 
160
 // Clock Sources
160
 // Clock Sources
161
-enum ClockSource : char {
161
+enum ClockSource : uint8_t {
162
   CS_NONE,             //  0
162
   CS_NONE,             //  0
163
   CS_PRESCALER_1,      //  1
163
   CS_PRESCALER_1,      //  1
164
   CS_PRESCALER_8,      //  2
164
   CS_PRESCALER_8,      //  2
170
 };
170
 };
171
 
171
 
172
 // Clock Sources (Timer 2 only)
172
 // Clock Sources (Timer 2 only)
173
-enum ClockSource2 : char {
173
+enum ClockSource2 : uint8_t {
174
   CS2_NONE,            //  0
174
   CS2_NONE,            //  0
175
   CS2_PRESCALER_1,     //  1
175
   CS2_PRESCALER_1,     //  1
176
   CS2_PRESCALER_8,     //  2
176
   CS2_PRESCALER_8,     //  2
203
     TCCR##T##B = (TCCR##T##B & ~(0x3 << WGM##T##2)) | (((int(V) >> 2) & 0x3) << WGM##T##2); \
203
     TCCR##T##B = (TCCR##T##B & ~(0x3 << WGM##T##2)) | (((int(V) >> 2) & 0x3) << WGM##T##2); \
204
   }while(0)
204
   }while(0)
205
 #define SET_WGM(T,V) _SET_WGM(T,WGM_##V)
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
 // Set Clock Select bits
207
 // Set Clock Select bits
213
 // Ex: SET_CS3(PRESCALER_64);
208
 // Ex: SET_CS3(PRESCALER_64);
235
 #define SET_CS4(V) _SET_CS4(CS_##V)
230
 #define SET_CS4(V) _SET_CS4(CS_##V)
236
 #define SET_CS5(V) _SET_CS5(CS_##V)
231
 #define SET_CS5(V) _SET_CS5(CS_##V)
237
 #define SET_CS(T,V) SET_CS##T(V)
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
 // Set Compare Mode bits
234
 // Set Compare Mode bits
242
 // Ex: SET_COMS(4,CLEAR_SET,CLEAR_SET,CLEAR_SET);
235
 // Ex: SET_COMS(4,CLEAR_SET,CLEAR_SET,CLEAR_SET);
246
 #define SET_COMB(T,V) SET_COM(T,B,V)
239
 #define SET_COMB(T,V) SET_COM(T,B,V)
247
 #define SET_COMC(T,V) SET_COM(T,C,V)
240
 #define SET_COMC(T,V) SET_COM(T,C,V)
248
 #define SET_COMS(T,V1,V2,V3) do{ SET_COMA(T,V1); SET_COMB(T,V2); SET_COMC(T,V3); }while(0)
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
 // Set Noise Canceler bit
243
 // Set Noise Canceler bit
261
 // Ex: SET_ICNC(2,1)
244
 // Ex: SET_ICNC(2,1)

+ 1
- 1
Marlin/src/HAL/LPC1768/HAL.h View File

206
  *  All Hardware PWM pins run at the same frequency and all
206
  *  All Hardware PWM pins run at the same frequency and all
207
  *  Software PWM pins run at the same frequency
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
  * set_pwm_duty
212
  * set_pwm_duty

+ 1
- 1
Marlin/src/HAL/LPC1768/fast_pwm.cpp View File

32
 
32
 
33
 #if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
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
     LPC176x::pwm_set_frequency(pin, f_desired);
36
     LPC176x::pwm_set_frequency(pin, f_desired);
37
   }
37
   }
38
 
38
 

+ 1
- 1
Marlin/src/HAL/STM32/HAL.h View File

231
  *  Set the frequency of the timer corresponding to the provided pin
231
  *  Set the frequency of the timer corresponding to the provided pin
232
  *  All Timer PWM pins run at the same frequency
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
  * set_pwm_duty
237
  * set_pwm_duty

+ 1
- 1
Marlin/src/HAL/STM32/fast_pwm.cpp View File

56
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
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
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
60
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
61
   const PinName pin_name = digitalPinToPinName(pin);
61
   const PinName pin_name = digitalPinToPinName(pin);
62
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
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 View File

274
  *  Set the frequency of the timer corresponding to the provided pin
274
  *  Set the frequency of the timer corresponding to the provided pin
275
  *  All Timer PWM pins run at the same frequency
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
  * set_pwm_duty
280
  * set_pwm_duty

+ 1
- 1
Marlin/src/HAL/STM32F1/fast_pwm.cpp View File

51
   timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
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
   if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
55
   if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
56
 
56
 
57
   timer_dev *timer; UNUSED(timer);
57
   timer_dev *timer; UNUSED(timer);

Loading…
Cancel
Save