Browse Source

Make F_CPU a compile-time constant (#21051)

X-Ryl669 4 years ago
parent
commit
56462cf082
No account linked to committer's email address

+ 6
- 0
Marlin/src/HAL/STM32/HAL.cpp View File

63
 void HAL_init() {
63
 void HAL_init() {
64
   FastIO_init();
64
   FastIO_init();
65
 
65
 
66
+  // Ensure F_CPU is a constant expression. 
67
+  // If the compiler breaks here, it means that delay code that should compute at compile time will not work.
68
+  // So better safe than sorry here.
69
+  constexpr int cpuFreq = F_CPU;
70
+  UNUSED(cpuFreq);
71
+
66
   #if ENABLED(SDSUPPORT) && DISABLED(SDIO_SUPPORT) && (defined(SDSS) && SDSS != -1)
72
   #if ENABLED(SDSUPPORT) && DISABLED(SDIO_SUPPORT) && (defined(SDSS) && SDSS != -1)
67
     OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
73
     OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
68
   #endif
74
   #endif

+ 6
- 0
Marlin/src/HAL/STM32/inc/Conditionals_adv.h View File

24
 #if defined(USBD_USE_CDC_MSC) && DISABLED(NO_SD_HOST_DRIVE)
24
 #if defined(USBD_USE_CDC_MSC) && DISABLED(NO_SD_HOST_DRIVE)
25
   #define HAS_SD_HOST_DRIVE 1
25
   #define HAS_SD_HOST_DRIVE 1
26
 #endif
26
 #endif
27
+
28
+// Fix F_CPU not being a compile-time constant in STSTM32 framework
29
+#ifdef BOARD_F_CPU
30
+  #undef F_CPU
31
+  #define F_CPU BOARD_F_CPU
32
+#endif

+ 13
- 0
buildroot/share/PlatformIO/scripts/common-cxxflags.py View File

11
   #"-Wno-sign-compare"
11
   #"-Wno-sign-compare"
12
 ])
12
 ])
13
 
13
 
14
+#
15
+# Add CPU frequency as a compile time constant instead of a runtime variable
16
+#
17
+def add_cpu_freq():
18
+	if 'BOARD_F_CPU' in env:
19
+		env['BUILD_FLAGS'].append('-DBOARD_F_CPU=' + env['BOARD_F_CPU'])
20
+
14
 # Useful for JTAG debugging
21
 # Useful for JTAG debugging
15
 #
22
 #
16
 # It will separe release and debug build folders.
23
 # It will separe release and debug build folders.
20
 #
27
 #
21
 if env.GetBuildType() == "debug":
28
 if env.GetBuildType() == "debug":
22
 	env['BUILD_DIR'] = '$PROJECT_BUILD_DIR/$PIOENV/debug'
29
 	env['BUILD_DIR'] = '$PROJECT_BUILD_DIR/$PIOENV/debug'
30
+
31
+# On some platform, F_CPU is a runtime variable. Since it's used to convert from ns 
32
+# to CPU cycles, this adds overhead preventing small delay (in the order of less than 
33
+# 30 cycles) to be generated correctly. By using a compile time constant instead
34
+# the compiler will perform the computation and this overhead will be avoided
35
+add_cpu_freq()

Loading…
Cancel
Save