Browse Source

Fix LPC176x timer functions

Co-Authored-By: ejtagle <ejtagle@hotmail.com>
Scott Lahteine 7 years ago
parent
commit
206014a957

+ 11
- 6
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp View File

126
 }
126
 }
127
 
127
 
128
 void HAL_timer_enable_interrupt(const uint8_t timer_num) {
128
 void HAL_timer_enable_interrupt(const uint8_t timer_num) {
129
-  const tTimerConfig * const pConfig = &TimerConfig[timer_num];
130
-  pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_IER = TC_IER_CPCS;
129
+  IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
130
+  NVIC_EnableIRQ(irq);
131
 }
131
 }
132
 
132
 
133
 void HAL_timer_disable_interrupt(const uint8_t timer_num) {
133
 void HAL_timer_disable_interrupt(const uint8_t timer_num) {
134
-  const tTimerConfig * const pConfig = &TimerConfig[timer_num];
135
-  pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_IDR = TC_IDR_CPCS;
134
+  IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
135
+  NVIC_DisableIRQ(irq);
136
+}
137
+
138
+// missing from CMSIS: Check if interrupt is enabled or not
139
+static bool NVIC_GetEnabledIRQ(IRQn_Type IRQn) {
140
+  return (NVIC->ISER[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F))) != 0;
136
 }
141
 }
137
 
142
 
138
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
143
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
139
-  const tTimerConfig * const pConfig = &TimerConfig[timer_num];
140
-  return (pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_IMR & TC_IMR_CPCS) != 0;
144
+  IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
145
+  return NVIC_GetEnabledIRQ(irq);
141
 }
146
 }
142
 
147
 
143
 #endif // ARDUINO_ARCH_SAM
148
 #endif // ARDUINO_ARCH_SAM

+ 0
- 2
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h View File

118
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
118
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
119
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
119
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
120
 
120
 
121
-//void HAL_timer_isr_prologue(const uint8_t timer_num);
122
-
123
 FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
121
 FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
124
   const tTimerConfig * const pConfig = &TimerConfig[timer_num];
122
   const tTimerConfig * const pConfig = &TimerConfig[timer_num];
125
   // Reading the status register clears the interrupt flag
123
   // Reading the status register clears the interrupt flag

+ 13
- 40
Marlin/src/HAL/HAL_LPC1768/HAL_timers.cpp View File

23
 /**
23
 /**
24
  * Description:
24
  * Description:
25
  *
25
  *
26
- * For TARGET_LPC1768
26
+ * Timers for LPC1768
27
  */
27
  */
28
 
28
 
29
 #ifdef TARGET_LPC1768
29
 #ifdef TARGET_LPC1768
32
 #include "HAL_timers.h"
32
 #include "HAL_timers.h"
33
 
33
 
34
 void HAL_timer_init(void) {
34
 void HAL_timer_init(void) {
35
-  SBI(LPC_SC->PCONP, 1);  // power on timer0
35
+  SBI(LPC_SC->PCONP, SBIT_TIMER0);  // Power ON Timer 0
36
   LPC_TIM0->PR = (HAL_TIMER_RATE) / (HAL_STEPPER_TIMER_RATE) - 1; // Use prescaler to set frequency if needed
36
   LPC_TIM0->PR = (HAL_TIMER_RATE) / (HAL_STEPPER_TIMER_RATE) - 1; // Use prescaler to set frequency if needed
37
 
37
 
38
-  SBI(LPC_SC->PCONP, 2);  // power on timer1
38
+  SBI(LPC_SC->PCONP, SBIT_TIMER1);  // Power ON Timer 1
39
   LPC_TIM1->PR = (HAL_TIMER_RATE) / 1000000 - 1;
39
   LPC_TIM1->PR = (HAL_TIMER_RATE) / 1000000 - 1;
40
 }
40
 }
41
 
41
 
42
 void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
42
 void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
43
   switch (timer_num) {
43
   switch (timer_num) {
44
     case 0:
44
     case 0:
45
-      LPC_TIM0->MCR = 3;              // Match on MR0, reset on MR0
45
+      LPC_TIM0->MCR = _BV(SBIT_MR0I) | _BV(SBIT_MR0R); // Match on MR0, reset on MR0, interrupts when NVIC enables them
46
       LPC_TIM0->MR0 = uint32_t(HAL_STEPPER_TIMER_RATE) / frequency; // Match value (period) to set frequency
46
       LPC_TIM0->MR0 = uint32_t(HAL_STEPPER_TIMER_RATE) / frequency; // Match value (period) to set frequency
47
-      LPC_TIM0->TCR = _BV(0);       // enable
48
-      break;
49
-    case 1:
50
-      LPC_TIM1->MCR = 3;
51
-      LPC_TIM1->MR0 = uint32_t(HAL_TEMP_TIMER_RATE) / frequency;
52
-      LPC_TIM1->TCR = _BV(0);
53
-      break;
54
-    default: break;
55
-  }
56
-}
47
+      LPC_TIM0->TCR = _BV(SBIT_CNTEN); // Counter Enable
57
 
48
 
58
-void HAL_timer_enable_interrupt(const uint8_t timer_num) {
59
-  switch (timer_num) {
60
-    case 0:
61
-      NVIC_EnableIRQ(TIMER0_IRQn); // Enable interrupt handler
62
       NVIC_SetPriority(TIMER0_IRQn, NVIC_EncodePriority(0, 1, 0));
49
       NVIC_SetPriority(TIMER0_IRQn, NVIC_EncodePriority(0, 1, 0));
50
+      NVIC_EnableIRQ(TIMER0_IRQn);
63
       break;
51
       break;
52
+
64
     case 1:
53
     case 1:
65
-      NVIC_EnableIRQ(TIMER1_IRQn);
54
+      LPC_TIM1->MCR = _BV(SBIT_MR0I) | _BV(SBIT_MR0R); // Match on MR0, reset on MR0, interrupts when NVIC enables them
55
+      LPC_TIM1->MR0 = uint32_t(HAL_TEMP_TIMER_RATE) / frequency;
56
+      LPC_TIM1->TCR = _BV(SBIT_CNTEN); // Counter Enable
57
+
66
       NVIC_SetPriority(TIMER1_IRQn, NVIC_EncodePriority(0, 2, 0));
58
       NVIC_SetPriority(TIMER1_IRQn, NVIC_EncodePriority(0, 2, 0));
59
+      NVIC_EnableIRQ(TIMER1_IRQn);
67
       break;
60
       break;
68
-  }
69
-}
70
-
71
-void HAL_timer_disable_interrupt(const uint8_t timer_num) {
72
-  switch (timer_num) {
73
-    case 0: NVIC_DisableIRQ(TIMER0_IRQn); break; // disable interrupt handler
74
-    case 1: NVIC_DisableIRQ(TIMER1_IRQn); break;
75
-  }
76
-}
77
 
61
 
78
-bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
79
-  switch (timer_num) {
80
-    case 0: return NVIC_GetActive(TIMER0_IRQn);
81
-    case 1: return NVIC_GetActive(TIMER1_IRQn);
82
-  }
83
-  return false;
84
-}
85
-
86
-void HAL_timer_isr_prologue(const uint8_t timer_num) {
87
-  switch (timer_num) {
88
-    case 0: SBI(LPC_TIM0->IR, 0); break; // Clear the Interrupt
89
-    case 1: SBI(LPC_TIM1->IR, 0); break;
62
+    default: break;
90
   }
63
   }
91
 }
64
 }
92
 
65
 

+ 73
- 36
Marlin/src/HAL/HAL_LPC1768/HAL_timers.h View File

34
 
34
 
35
 #include <stdint.h>
35
 #include <stdint.h>
36
 
36
 
37
+#include "../../core/macros.h"
38
+
39
+#define SBIT_TIMER0 1
40
+#define SBIT_TIMER1 2
41
+
42
+#define SBIT_CNTEN 0
43
+
44
+#define SBIT_MR0I  0 // Timer 0 Interrupt when TC matches MR0
45
+#define SBIT_MR0R  1 // Timer 0 Reset TC on Match
46
+#define SBIT_MR0S  2 // Timer 0 Stop TC and PC on Match
47
+#define SBIT_MR1I  3
48
+#define SBIT_MR1R  4
49
+#define SBIT_MR1S  5
50
+#define SBIT_MR2I  6
51
+#define SBIT_MR2R  7
52
+#define SBIT_MR2S  8
53
+#define SBIT_MR3I  9
54
+#define SBIT_MR3R 10
55
+#define SBIT_MR3S 11
56
+
37
 // --------------------------------------------------------------------------
57
 // --------------------------------------------------------------------------
38
 // Defines
58
 // Defines
39
 // --------------------------------------------------------------------------
59
 // --------------------------------------------------------------------------
40
 
60
 
41
-#define FORCE_INLINE __attribute__((always_inline)) inline
61
+#define _HAL_TIMER(T) _CAT(LPC_TIM, T)
62
+#define _HAL_TIMER_IRQ(T) TIMER##T##_IRQn
63
+#define __HAL_TIMER_ISR(T) extern "C" void TIMER##T##_IRQHandler(void)
64
+#define _HAL_TIMER_ISR(T)  __HAL_TIMER_ISR(T)
42
 
65
 
43
 typedef uint32_t hal_timer_t;
66
 typedef uint32_t hal_timer_t;
44
 #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
67
 #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
45
 
68
 
46
-#define STEP_TIMER_NUM 0  // index of timer to use for stepper
47
-#define TEMP_TIMER_NUM 1  // index of timer to use for temperature
69
+#define STEP_TIMER_NUM 0  // Timer Index for Stepper
70
+#define TEMP_TIMER_NUM 1  // Timer Index for Temperature
48
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
71
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
72
+#define PWM_TIMER_NUM 3   // Timer Index for PWM
49
 
73
 
50
 #define HAL_TIMER_RATE         ((SystemCoreClock) / 4)  // frequency of timers peripherals
74
 #define HAL_TIMER_RATE         ((SystemCoreClock) / 4)  // frequency of timers peripherals
51
 #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
75
 #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
66
 #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
90
 #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
67
 #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
91
 #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
68
 
92
 
69
-#define HAL_STEP_TIMER_ISR  extern "C" void TIMER0_IRQHandler(void)
70
-#define HAL_TEMP_TIMER_ISR  extern "C" void TIMER1_IRQHandler(void)
71
-
72
-// PWM timer
73
-#define HAL_PWM_TIMER      LPC_TIM3
74
-#define HAL_PWM_TIMER_ISR  extern "C" void TIMER3_IRQHandler(void)
75
-#define HAL_PWM_TIMER_IRQn TIMER3_IRQn
93
+#define HAL_STEP_TIMER_ISR _HAL_TIMER_ISR(STEP_TIMER_NUM)
94
+#define HAL_TEMP_TIMER_ISR _HAL_TIMER_ISR(TEMP_TIMER_NUM)
76
 
95
 
77
-// --------------------------------------------------------------------------
78
-// Types
79
-// --------------------------------------------------------------------------
80
-
81
-// --------------------------------------------------------------------------
82
-// Public Variables
83
-// --------------------------------------------------------------------------
96
+// Timer references by index
97
+#define STEP_TIMER _HAL_TIMER(STEP_TIMER_NUM)
98
+#define TEMP_TIMER _HAL_TIMER(TEMP_TIMER_NUM)
84
 
99
 
85
 // --------------------------------------------------------------------------
100
 // --------------------------------------------------------------------------
86
 // Public functions
101
 // Public functions
90
 
105
 
91
 FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
106
 FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
92
   switch (timer_num) {
107
   switch (timer_num) {
93
-    case 0:
94
-      LPC_TIM0->MR0 = compare;
95
-      if (LPC_TIM0->TC > compare)
96
-        LPC_TIM0->TC = compare - 5; // generate an immediate stepper ISR
97
-      break;
98
-    case 1:
99
-      LPC_TIM1->MR0 = compare;
100
-      if (LPC_TIM1->TC > compare)
101
-        LPC_TIM1->TC = compare - 5; // make sure we don't have one extra long period
102
-      break;
108
+    case 0: STEP_TIMER->MR0 = compare; break; // Stepper Timer Match Register 0
109
+    case 1: TEMP_TIMER->MR0 = compare; break; //    Temp Timer Match Register 0
103
   }
110
   }
104
 }
111
 }
105
 
112
 
106
 FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
113
 FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
107
   switch (timer_num) {
114
   switch (timer_num) {
108
-    case 0: return LPC_TIM0->MR0;
109
-    case 1: return LPC_TIM1->MR0;
115
+    case 0: return STEP_TIMER->MR0; // Stepper Timer Match Register 0
116
+    case 1: return TEMP_TIMER->MR0; //    Temp Timer Match Register 0
110
   }
117
   }
111
   return 0;
118
   return 0;
112
 }
119
 }
113
 
120
 
114
 FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
121
 FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
115
   switch (timer_num) {
122
   switch (timer_num) {
116
-    case 0: return LPC_TIM0->TC;
117
-    case 1: return LPC_TIM1->TC;
123
+    case 0: return STEP_TIMER->TC; // Stepper Timer Count
124
+    case 1: return TEMP_TIMER->TC; //    Temp Timer Count
118
   }
125
   }
119
   return 0;
126
   return 0;
120
 }
127
 }
124
   if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
131
   if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
125
 }
132
 }
126
 
133
 
127
-void HAL_timer_enable_interrupt(const uint8_t timer_num);
128
-void HAL_timer_disable_interrupt(const uint8_t timer_num);
129
-bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
130
-void HAL_timer_isr_prologue(const uint8_t timer_num);
134
+FORCE_INLINE static void HAL_timer_enable_interrupt(const uint8_t timer_num) {
135
+  switch (timer_num) {
136
+    case 0: NVIC_EnableIRQ(TIMER0_IRQn); // Enable interrupt handler
137
+    case 1: NVIC_EnableIRQ(TIMER1_IRQn); // Enable interrupt handler
138
+  }
139
+}
140
+
141
+FORCE_INLINE static void HAL_timer_disable_interrupt(const uint8_t timer_num) {
142
+  switch (timer_num) {
143
+    case 0: NVIC_DisableIRQ(TIMER0_IRQn); // Disable interrupt handler
144
+    case 1: NVIC_DisableIRQ(TIMER1_IRQn); // Disable interrupt handler
145
+  }
146
+}
147
+
148
+// This function is missing from CMSIS
149
+FORCE_INLINE static bool NVIC_GetEnableIRQ(IRQn_Type IRQn) {
150
+  return (NVIC->ISER[((uint32_t)IRQn) >> 5] & (1 << ((uint32_t)IRQn) & 0x1F)) != 0;
151
+}
152
+
153
+FORCE_INLINE static bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
154
+  switch (timer_num) {
155
+    case 0: return NVIC_GetEnableIRQ(TIMER0_IRQn); // Check if interrupt is enabled or not
156
+    case 1: return NVIC_GetEnableIRQ(TIMER1_IRQn); // Check if interrupt is enabled or not
157
+  }
158
+  return false;
159
+}
160
+
161
+FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
162
+  switch (timer_num) {
163
+    case 0: SBI(STEP_TIMER->IR, SBIT_CNTEN); break;
164
+    case 1: SBI(TEMP_TIMER->IR, SBIT_CNTEN); break;
165
+  }
166
+}
167
+
131
 #define HAL_timer_isr_epilogue(TIMER_NUM)
168
 #define HAL_timer_isr_epilogue(TIMER_NUM)
132
 
169
 
133
-#endif // _HAL_TIMERS_DUE_H
170
+#endif // _HAL_TIMERS_H

+ 3
- 1
Marlin/src/HAL/HAL_LPC1768/LPC1768_PWM.cpp View File

78
 
78
 
79
 #define NUM_ISR_PWMS 20
79
 #define NUM_ISR_PWMS 20
80
 
80
 
81
+#define HAL_PWM_TIMER      LPC_TIM3
82
+#define HAL_PWM_TIMER_ISR  extern "C" void TIMER3_IRQHandler(void)
83
+#define HAL_PWM_TIMER_IRQn TIMER3_IRQn
81
 
84
 
82
 #define LPC_PORT_OFFSET         (0x0020)
85
 #define LPC_PORT_OFFSET         (0x0020)
83
 #define LPC_PIN(pin)            (1UL << pin)
86
 #define LPC_PIN(pin)            (1UL << pin)
84
 #define LPC_GPIO(port)          ((volatile LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + LPC_PORT_OFFSET * port))
87
 #define LPC_GPIO(port)          ((volatile LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + LPC_PORT_OFFSET * port))
85
 
88
 
86
-
87
 typedef struct {            // holds all data needed to control/init one of the PWM channels
89
 typedef struct {            // holds all data needed to control/init one of the PWM channels
88
   bool                active_flag;    // THIS TABLE ENTRY IS ACTIVELY TOGGLING A PIN
90
   bool                active_flag;    // THIS TABLE ENTRY IS ACTIVELY TOGGLING A PIN
89
   pin_t               pin;
91
   pin_t               pin;

Loading…
Cancel
Save