浏览代码

STM32 FastIO using register access (#12276)

Karl Andersson 6 年前
父节点
当前提交
1946f729fd

+ 6
- 0
Marlin/src/HAL/HAL_STM32/HAL.cpp 查看文件

80
 // HAL initialization task
80
 // HAL initialization task
81
 void HAL_init(void) {
81
 void HAL_init(void) {
82
 
82
 
83
+  FastIO_init();
84
+
83
   #if ENABLED(SDSUPPORT)
85
   #if ENABLED(SDSUPPORT)
84
     OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
86
     OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
85
   #endif
87
   #endif
86
 
88
 
89
+  #if PIN_EXISTS(LED)
90
+    OUT_WRITE(LED_PIN, LOW);
91
+  #endif
92
+
87
   #if ENABLED(EEPROM_EMULATED_WITH_SRAM)
93
   #if ENABLED(EEPROM_EMULATED_WITH_SRAM)
88
   // Enable access to backup SRAM
94
   // Enable access to backup SRAM
89
   __HAL_RCC_PWR_CLK_ENABLE();
95
   __HAL_RCC_PWR_CLK_ENABLE();

+ 34
- 0
Marlin/src/HAL/HAL_STM32/fastio_STM32.cpp 查看文件

1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ * Copyright (C) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+#ifdef ARDUINO_ARCH_STM32
24
+
25
+#include "../../inc/MarlinConfig.h"
26
+
27
+GPIO_TypeDef* FastIOPortMap[LastPort + 1];
28
+
29
+void FastIO_init() {
30
+  for (uint8_t i = 0; i < NUM_DIGITAL_PINS; i++)
31
+    FastIOPortMap[STM_PORT(digitalPin[i])] = get_GPIO_Port(STM_PORT(digitalPin[i]));
32
+}
33
+
34
+#endif

+ 34
- 6
Marlin/src/HAL/HAL_STM32/fastio_STM32.h 查看文件

24
 
24
 
25
 /**
25
 /**
26
  * Fast I/O interfaces for STM32
26
  * Fast I/O interfaces for STM32
27
- * These use GPIO functions instead of Direct Port Manipulation, as on AVR.
27
+ * These use GPIO register access for fast port manipulation.
28
  */
28
  */
29
 
29
 
30
+// --------------------------------------------------------------------------
31
+// Public Variables
32
+// --------------------------------------------------------------------------
33
+
34
+extern GPIO_TypeDef * FastIOPortMap[];
35
+
36
+// --------------------------------------------------------------------------
37
+// Public functions
38
+// --------------------------------------------------------------------------
39
+
40
+void FastIO_init(); // Must be called before using fast io macros
41
+
42
+// --------------------------------------------------------------------------
43
+// Defines
44
+// --------------------------------------------------------------------------
45
+
30
 #define _BV(b) (1 << (b))
46
 #define _BV(b) (1 << (b))
47
+#define _BV32(b) (1UL << (b))
48
+
49
+#if defined(STM32F0xx) || defined(STM32F1xx) || defined(STM32F3xx) || defined(STM32L0xx) || defined(STM32L4xx)
50
+  #define _WRITE(IO, V) do { \
51
+    if (V) FastIOPortMap[STM_PORT(digitalPin[IO])]->BSRR = _BV32(STM_PIN(digitalPin[IO])) ; \
52
+    else   FastIOPortMap[STM_PORT(digitalPin[IO])]->BRR  = _BV32(STM_PIN(digitalPin[IO])) ; \
53
+  } while(0)
54
+#else
55
+  #define _WRITE(IO, V) (FastIOPortMap[STM_PORT(digitalPin[IO])]->BSRR = _BV32(STM_PIN(digitalPin[IO] + (V ? 0 : 16))))
56
+#endif
31
 
57
 
32
-#define READ(IO)                digitalRead(IO)
33
-#define WRITE(IO,V)             digitalWrite(IO,V)
34
-#define WRITE_VAR(IO,V)         WRITE(IO,V)
58
+#define _READ(IO)               bool(READ_BIT(FastIOPortMap[STM_PORT(digitalPin[IO])]->IDR, _BV32(STM_PIN(digitalPin[IO]))))
59
+#define _TOGGLE(IO)             (FastIOPortMap[STM_PORT(digitalPin[IO])]->ODR ^= _BV32(STM_PIN(digitalPin[IO])))
35
 
60
 
36
 #define _GET_MODE(IO)
61
 #define _GET_MODE(IO)
37
 #define _SET_MODE(IO,M)         pinMode(IO, M)
62
 #define _SET_MODE(IO,M)         pinMode(IO, M)
38
 #define _SET_OUTPUT(IO)         pinMode(IO, OUTPUT)                               /*!< Output Push Pull Mode & GPIO_NOPULL   */
63
 #define _SET_OUTPUT(IO)         pinMode(IO, OUTPUT)                               /*!< Output Push Pull Mode & GPIO_NOPULL   */
39
 
64
 
65
+#define WRITE_VAR(IO,V)         _WRITE(IO,V)
66
+#define WRITE(IO,V)             _WRITE(IO,V)
67
+#define READ(IO)                _READ(IO)
68
+#define TOGGLE(IO)              _TOGGLE(IO)
69
+
40
 #define OUT_WRITE(IO,V)         do{ _SET_OUTPUT(IO); WRITE(IO,V); }while(0)
70
 #define OUT_WRITE(IO,V)         do{ _SET_OUTPUT(IO); WRITE(IO,V); }while(0)
41
 
71
 
42
 #define SET_INPUT(IO)           _SET_MODE(IO, INPUT)                              /*!< Input Floating Mode                   */
72
 #define SET_INPUT(IO)           _SET_MODE(IO, INPUT)                              /*!< Input Floating Mode                   */
44
 #define SET_INPUT_PULLDOWN(IO)  _SET_MODE(IO, INPUT_PULLDOWN)                     /*!< Input with Pull-down activation       */
74
 #define SET_INPUT_PULLDOWN(IO)  _SET_MODE(IO, INPUT_PULLDOWN)                     /*!< Input with Pull-down activation       */
45
 #define SET_OUTPUT(IO)          OUT_WRITE(IO, LOW)
75
 #define SET_OUTPUT(IO)          OUT_WRITE(IO, LOW)
46
 
76
 
47
-#define TOGGLE(IO)              OUT_WRITE(IO, !READ(IO))
48
-
49
 #define GET_INPUT(IO)
77
 #define GET_INPUT(IO)
50
 #define GET_OUTPUT(IO)
78
 #define GET_OUTPUT(IO)
51
 #define GET_TIMER(IO)
79
 #define GET_TIMER(IO)

+ 3
- 3
Marlin/src/lcd/dogm/ultralcd_st7920_u8glib_rrd_AVR.cpp 查看文件

73
   #define CPU_ST7920_DELAY_2 DELAY_NS(0)
73
   #define CPU_ST7920_DELAY_2 DELAY_NS(0)
74
   #define CPU_ST7920_DELAY_3 DELAY_NS(189)
74
   #define CPU_ST7920_DELAY_3 DELAY_NS(189)
75
 #elif defined(ARDUINO_ARCH_STM32)
75
 #elif defined(ARDUINO_ARCH_STM32)
76
-  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
77
-  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
78
-  #define CPU_ST7920_DELAY_3 DELAY_NS(0)
76
+  #define CPU_ST7920_DELAY_1 DELAY_NS(300)
77
+  #define CPU_ST7920_DELAY_2 DELAY_NS(40)
78
+  #define CPU_ST7920_DELAY_3 DELAY_NS(340)
79
 #elif F_CPU == 16000000
79
 #elif F_CPU == 16000000
80
   #define CPU_ST7920_DELAY_1 DELAY_NS(0)
80
   #define CPU_ST7920_DELAY_1 DELAY_NS(0)
81
   #define CPU_ST7920_DELAY_2 DELAY_NS(0)
81
   #define CPU_ST7920_DELAY_2 DELAY_NS(0)

正在加载...
取消
保存