Browse Source

Set Interrupt Priorities and Grouping for STM32F103. (#10517)

J.C. Nelson 6 years ago
parent
commit
5b5e322356

+ 56
- 5
Marlin/src/HAL/HAL_STM32F1/HAL.cpp View File

34
 #include "HAL.h"
34
 #include "HAL.h"
35
 #include <STM32ADC.h>
35
 #include <STM32ADC.h>
36
 
36
 
37
-//#include <Wire.h>
38
-
39
 // --------------------------------------------------------------------------
37
 // --------------------------------------------------------------------------
40
 // Externals
38
 // Externals
41
 // --------------------------------------------------------------------------
39
 // --------------------------------------------------------------------------
42
 
40
 
43
 // --------------------------------------------------------------------------
41
 // --------------------------------------------------------------------------
44
-// Local defines
42
+// Types
45
 // --------------------------------------------------------------------------
43
 // --------------------------------------------------------------------------
46
 
44
 
45
+#define __I
46
+#define __IO
47
+ typedef struct
48
+ {
49
+   __I  uint32_t CPUID;                   /*!< Offset: 0x000 (R/ )  CPUID Base Register                                   */
50
+   __IO uint32_t ICSR;                    /*!< Offset: 0x004 (R/W)  Interrupt Control and State Register                  */
51
+   __IO uint32_t VTOR;                    /*!< Offset: 0x008 (R/W)  Vector Table Offset Register                          */
52
+   __IO uint32_t AIRCR;                   /*!< Offset: 0x00C (R/W)  Application Interrupt and Reset Control Register      */
53
+   __IO uint32_t SCR;                     /*!< Offset: 0x010 (R/W)  System Control Register                               */
54
+   __IO uint32_t CCR;                     /*!< Offset: 0x014 (R/W)  Configuration Control Register                        */
55
+   __IO uint8_t  SHP[12];                 /*!< Offset: 0x018 (R/W)  System Handlers Priority Registers (4-7, 8-11, 12-15) */
56
+   __IO uint32_t SHCSR;                   /*!< Offset: 0x024 (R/W)  System Handler Control and State Register             */
57
+   __IO uint32_t CFSR;                    /*!< Offset: 0x028 (R/W)  Configurable Fault Status Register                    */
58
+   __IO uint32_t HFSR;                    /*!< Offset: 0x02C (R/W)  HardFault Status Register                             */
59
+   __IO uint32_t DFSR;                    /*!< Offset: 0x030 (R/W)  Debug Fault Status Register                           */
60
+   __IO uint32_t MMFAR;                   /*!< Offset: 0x034 (R/W)  MemManage Fault Address Register                      */
61
+   __IO uint32_t BFAR;                    /*!< Offset: 0x038 (R/W)  BusFault Address Register                             */
62
+   __IO uint32_t AFSR;                    /*!< Offset: 0x03C (R/W)  Auxiliary Fault Status Register                       */
63
+   __I  uint32_t PFR[2];                  /*!< Offset: 0x040 (R/ )  Processor Feature Register                            */
64
+   __I  uint32_t DFR;                     /*!< Offset: 0x048 (R/ )  Debug Feature Register                                */
65
+   __I  uint32_t ADR;                     /*!< Offset: 0x04C (R/ )  Auxiliary Feature Register                            */
66
+   __I  uint32_t MMFR[4];                 /*!< Offset: 0x050 (R/ )  Memory Model Feature Register                         */
67
+   __I  uint32_t ISAR[5];                 /*!< Offset: 0x060 (R/ )  Instruction Set Attributes Register                   */
68
+        uint32_t RESERVED0[5];
69
+   __IO uint32_t CPACR;                   /*!< Offset: 0x088 (R/W)  Coprocessor Access Control Register                   */
70
+ } SCB_Type;
71
+
47
 // --------------------------------------------------------------------------
72
 // --------------------------------------------------------------------------
48
-// Types
73
+// Variables
49
 // --------------------------------------------------------------------------
74
 // --------------------------------------------------------------------------
50
 
75
 
51
 // --------------------------------------------------------------------------
76
 // --------------------------------------------------------------------------
52
-// Variables
77
+// Local defines
53
 // --------------------------------------------------------------------------
78
 // --------------------------------------------------------------------------
79
+#define SCS_BASE            (0xE000E000UL)                            /*!< System Control Space Base Address  */
80
+#define SCB_BASE            (SCS_BASE +  0x0D00UL)                    /*!< System Control Block Base Address  */
81
+
82
+#define SCB                 ((SCB_Type       *)     SCB_BASE      )   /*!< SCB configuration struct           */
83
+
84
+/* SCB Application Interrupt and Reset Control Register Definitions */
85
+#define SCB_AIRCR_VECTKEY_Pos              16                                             /*!< SCB AIRCR: VECTKEY Position */
86
+#define SCB_AIRCR_VECTKEY_Msk              (0xFFFFUL << SCB_AIRCR_VECTKEY_Pos)            /*!< SCB AIRCR: VECTKEY Mask */
87
+
88
+#define SCB_AIRCR_PRIGROUP_Pos              8                                             /*!< SCB AIRCR: PRIGROUP Position */
89
+#define SCB_AIRCR_PRIGROUP_Msk             (7UL << SCB_AIRCR_PRIGROUP_Pos)                /*!< SCB AIRCR: PRIGROUP Mask */
54
 
90
 
55
 // --------------------------------------------------------------------------
91
 // --------------------------------------------------------------------------
56
 // Public Variables
92
 // Public Variables
123
 // --------------------------------------------------------------------------
159
 // --------------------------------------------------------------------------
124
 // Private functions
160
 // Private functions
125
 // --------------------------------------------------------------------------
161
 // --------------------------------------------------------------------------
162
+static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
163
+  uint32_t reg_value;
164
+  uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07);               /* only values 0..7 are used          */
165
+
166
+  reg_value  =  SCB->AIRCR;                                                   /* read old register configuration    */
167
+  reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk);             /* clear bits to change               */
168
+  reg_value  =  (reg_value                                 |
169
+                ((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) |
170
+                (PriorityGroupTmp << 8));                                     /* Insert write key and priorty group */
171
+  SCB->AIRCR =  reg_value;
172
+}
126
 
173
 
127
 // --------------------------------------------------------------------------
174
 // --------------------------------------------------------------------------
128
 // Public functions
175
 // Public functions
129
 // --------------------------------------------------------------------------
176
 // --------------------------------------------------------------------------
130
 
177
 
178
+void HAL_init(void) {
179
+  NVIC_SetPriorityGrouping(0x3);
180
+}
181
+
131
 /* VGPV Done with defines
182
 /* VGPV Done with defines
132
 // disable interrupts
183
 // disable interrupts
133
 void cli(void) { noInterrupts(); }
184
 void cli(void) { noInterrupts(); }

+ 4
- 0
Marlin/src/HAL/HAL_STM32F1/HAL.h View File

108
   #define NUM_SERIAL 1
108
   #define NUM_SERIAL 1
109
 #endif
109
 #endif
110
 
110
 
111
+// Use HAL_init() to set interrupt grouping.
112
+#define HAL_INIT
113
+void HAL_init(); 
114
+
111
 /**
115
 /**
112
  * TODO: review this to return 1 for pins that are not analog input
116
  * TODO: review this to return 1 for pins that are not analog input
113
  */
117
  */

+ 22
- 16
Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.cpp View File

109
        */
109
        */
110
       break;
110
       break;
111
   }
111
   }
112
-  nvic_irq_set_priority(irq_num, 0xF); // this is the lowest settable priority, but should still be over USB
112
+
113
+  /**
114
+   * Give the Stepper ISR a higher priority (lower number)
115
+   * so it automatically preempts the Temperature ISR.
116
+   */
113
 
117
 
114
   switch (timer_num) {
118
   switch (timer_num) {
115
     case STEP_TIMER_NUM:
119
     case STEP_TIMER_NUM:
119
       timer_set_reload(STEP_TIMER_DEV, 0xFFFF);
123
       timer_set_reload(STEP_TIMER_DEV, 0xFFFF);
120
       timer_set_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, (HAL_STEPPER_TIMER_RATE / frequency)));
124
       timer_set_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, (HAL_STEPPER_TIMER_RATE / frequency)));
121
       timer_attach_interrupt(STEP_TIMER_DEV, STEP_TIMER_CHAN, stepTC_Handler);
125
       timer_attach_interrupt(STEP_TIMER_DEV, STEP_TIMER_CHAN, stepTC_Handler);
126
+      nvic_irq_set_priority(irq_num, 1);
122
       timer_generate_update(STEP_TIMER_DEV);
127
       timer_generate_update(STEP_TIMER_DEV);
123
       timer_resume(STEP_TIMER_DEV);
128
       timer_resume(STEP_TIMER_DEV);
124
       break;
129
       break;
129
       timer_set_reload(TEMP_TIMER_DEV, 0xFFFF);
134
       timer_set_reload(TEMP_TIMER_DEV, 0xFFFF);
130
       timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, ((F_CPU / TEMP_TIMER_PRESCALE) / frequency)));
135
       timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, min(HAL_TIMER_TYPE_MAX, ((F_CPU / TEMP_TIMER_PRESCALE) / frequency)));
131
       timer_attach_interrupt(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, tempTC_Handler);
136
       timer_attach_interrupt(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, tempTC_Handler);
137
+      nvic_irq_set_priority(irq_num, 2);
132
       timer_generate_update(TEMP_TIMER_DEV);
138
       timer_generate_update(TEMP_TIMER_DEV);
133
       timer_resume(TEMP_TIMER_DEV);
139
       timer_resume(TEMP_TIMER_DEV);
134
       break;
140
       break;
166
 timer_dev* get_timer_dev(int number) {
172
 timer_dev* get_timer_dev(int number) {
167
   switch (number) {
173
   switch (number) {
168
     #if STM32_HAVE_TIMER(1)
174
     #if STM32_HAVE_TIMER(1)
169
-    case 1: return &timer1;
175
+      case 1: return &timer1;
170
     #endif
176
     #endif
171
     #if STM32_HAVE_TIMER(2)
177
     #if STM32_HAVE_TIMER(2)
172
-    case 2: return &timer2;
178
+      case 2: return &timer2;
173
     #endif
179
     #endif
174
     #if STM32_HAVE_TIMER(3)
180
     #if STM32_HAVE_TIMER(3)
175
-    case 3: return &timer3;
181
+      case 3: return &timer3;
176
     #endif
182
     #endif
177
     #if STM32_HAVE_TIMER(4)
183
     #if STM32_HAVE_TIMER(4)
178
-    case 4: return &timer4;
184
+      case 4: return &timer4;
179
     #endif
185
     #endif
180
     #if STM32_HAVE_TIMER(5)
186
     #if STM32_HAVE_TIMER(5)
181
-    case 5: return &timer5;
187
+      case 5: return &timer5;
182
     #endif
188
     #endif
183
     #if STM32_HAVE_TIMER(6)
189
     #if STM32_HAVE_TIMER(6)
184
-    case 6: return &timer6;
190
+      case 6: return &timer6;
185
     #endif
191
     #endif
186
     #if STM32_HAVE_TIMER(7)
192
     #if STM32_HAVE_TIMER(7)
187
-    case 7: return &timer7;
193
+      case 7: return &timer7;
188
     #endif
194
     #endif
189
     #if STM32_HAVE_TIMER(8)
195
     #if STM32_HAVE_TIMER(8)
190
-    case 8: return &timer8;
196
+      case 8: return &timer8;
191
     #endif
197
     #endif
192
     #if STM32_HAVE_TIMER(9)
198
     #if STM32_HAVE_TIMER(9)
193
-    case 9: return &timer9;
199
+      case 9: return &timer9;
194
     #endif
200
     #endif
195
     #if STM32_HAVE_TIMER(10)
201
     #if STM32_HAVE_TIMER(10)
196
-    case 10: return &timer10;
202
+      case 10: return &timer10;
197
     #endif
203
     #endif
198
     #if STM32_HAVE_TIMER(11)
204
     #if STM32_HAVE_TIMER(11)
199
-    case 11: return &timer11;
205
+      case 11: return &timer11;
200
     #endif
206
     #endif
201
     #if STM32_HAVE_TIMER(12)
207
     #if STM32_HAVE_TIMER(12)
202
-    case 12: return &timer12;
208
+      case 12: return &timer12;
203
     #endif
209
     #endif
204
     #if STM32_HAVE_TIMER(13)
210
     #if STM32_HAVE_TIMER(13)
205
-    case 13: return &timer14;
211
+      case 13: return &timer14;
206
     #endif
212
     #endif
207
     #if STM32_HAVE_TIMER(14)
213
     #if STM32_HAVE_TIMER(14)
208
-    case 14: return &timer14;
214
+      case 14: return &timer14;
209
     #endif
215
     #endif
210
-    }
216
+  }
211
 }
217
 }
212
 
218
 
213
 #endif // __STM32F1__
219
 #endif // __STM32F1__

+ 1
- 1
Marlin/src/HAL/HAL_STM32F1/fastio_Stm32f1.h View File

32
 #include <libmaple/gpio.h>
32
 #include <libmaple/gpio.h>
33
 
33
 
34
 #define READ(IO)              (PIN_MAP[IO].gpio_device->regs->IDR & (1U << PIN_MAP[IO].gpio_bit) ? HIGH : LOW)
34
 #define READ(IO)              (PIN_MAP[IO].gpio_device->regs->IDR & (1U << PIN_MAP[IO].gpio_bit) ? HIGH : LOW)
35
-#define WRITE(IO,V)           (PIN_MAP[IO].gpio_device->regs->BSRR = (1U << PIN_MAP[IO].gpio_bit) << (16 * !(bool)v))
35
+#define WRITE(IO,V)           (PIN_MAP[IO].gpio_device->regs->BSRR = (1U << PIN_MAP[IO].gpio_bit) << (16 * !(bool)V))
36
 #define TOGGLE(IO)            (PIN_MAP[IO].gpio_device->regs->ODR = PIN_MAP[IO].gpio_device->regs->ODR ^ (1U << PIN_MAP[IO].gpio_bit))
36
 #define TOGGLE(IO)            (PIN_MAP[IO].gpio_device->regs->ODR = PIN_MAP[IO].gpio_device->regs->ODR ^ (1U << PIN_MAP[IO].gpio_bit))
37
 #define WRITE_VAR(IO,V)       WRITE(io,V)
37
 #define WRITE_VAR(IO,V)       WRITE(io,V)
38
 
38
 

Loading…
Cancel
Save