Teensy development platform for PlatformIO compatible with GCC10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 jaren geleden
3 jaren geleden
3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # Copyright 2014-present PlatformIO <contact@platformio.org>
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Arduino
  16. Arduino Wiring-based Framework allows writing cross-platform software to
  17. control devices attached to a wide range of Arduino boards to create all
  18. kinds of creative coding, interactive objects, spaces or physical experiences.
  19. http://arduino.cc/en/Reference/HomePage
  20. """
  21. from io import open
  22. from os import listdir
  23. from os.path import isdir, isfile, join
  24. from SCons.Script import DefaultEnvironment
  25. env = DefaultEnvironment()
  26. platform = env.PioPlatform()
  27. FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoteensy-cxx20")
  28. FRAMEWORK_VERSION = platform.get_package_version("framework-arduinoteensy-cxx20")
  29. BUILD_CORE = env.BoardConfig().get("build.core")
  30. assert isdir(FRAMEWORK_DIR)
  31. BUILTIN_USB_FLAGS = (
  32. "USB_SERIAL",
  33. "USB_DUAL_SERIAL",
  34. "USB_TRIPLE_SERIAL",
  35. "USB_KEYBOARDONLY",
  36. "USB_TOUCHSCREEN",
  37. "USB_HID_TOUCHSCREEN",
  38. "USB_HID",
  39. "USB_SERIAL_HID",
  40. "USB_MIDI",
  41. "USB_MIDI4",
  42. "USB_MIDI16",
  43. "USB_MIDI_SERIAL",
  44. "USB_MIDI4_SERIAL",
  45. "USB_MIDI16_SERIAL",
  46. "USB_AUDIO",
  47. "USB_MIDI_AUDIO_SERIAL",
  48. "USB_MIDI16_AUDIO_SERIAL",
  49. "USB_MTPDISK",
  50. "USB_RAWHID",
  51. "USB_FLIGHTSIM",
  52. "USB_FLIGHTSIM_JOYSTICK",
  53. "USB_EVERYTHING",
  54. "USB_DISABLED",
  55. )
  56. if not set(env.get("CPPDEFINES", [])) & set(BUILTIN_USB_FLAGS):
  57. env.Append(CPPDEFINES=["USB_SERIAL"])
  58. env.Replace(
  59. SIZEPROGREGEXP=r"^(?:\.text|\.text\.progmem|\.text\.itcm|\.data)\s+([0-9]+).*",
  60. SIZEDATAREGEXP=r"^(?:\.usbdescriptortable|\.dmabuffers|\.usbbuffers|\.data|\.bss|\.noinit|\.text\.itcm|\.text\.itcm\.padding)\s+([0-9]+).*"
  61. )
  62. env.Append(
  63. CPPDEFINES=[
  64. ("ARDUINO", 10805),
  65. ("TEENSYDUINO", 149),
  66. "CORE_TEENSY"
  67. ],
  68. CPPPATH=[
  69. join(FRAMEWORK_DIR, "cores", BUILD_CORE)
  70. ],
  71. LIBSOURCE_DIRS=[
  72. join(FRAMEWORK_DIR, "libraries")
  73. ]
  74. )
  75. if "BOARD" in env and BUILD_CORE == "teensy":
  76. env.Append(
  77. ASFLAGS=["-x", "assembler-with-cpp"],
  78. CCFLAGS=[
  79. "-Os", # optimize for size
  80. "-Wall", # show warnings
  81. "-ffunction-sections", # place each function in its own section
  82. "-fdata-sections",
  83. "-mmcu=$BOARD_MCU"
  84. ],
  85. CXXFLAGS=[
  86. "-fno-exceptions",
  87. "-felide-constructors",
  88. "-std=gnu++11",
  89. "-fpermissive"
  90. ],
  91. CPPDEFINES=[
  92. ("F_CPU", "$BOARD_F_CPU"),
  93. "LAYOUT_US_ENGLISH"
  94. ],
  95. LINKFLAGS=[
  96. "-Os",
  97. "-Wl,--gc-sections,--relax",
  98. "-mmcu=$BOARD_MCU"
  99. ],
  100. LIBS=["m"]
  101. )
  102. elif "BOARD" in env and BUILD_CORE in ("teensy3", "teensy4", "teensy41"):
  103. env.Replace(
  104. AR="arm-none-eabi-gcc-ar",
  105. RANLIB="$AR"
  106. )
  107. env.Append(
  108. ASFLAGS=["-x", "assembler-with-cpp"],
  109. CCFLAGS=[
  110. "-Wall", # show warnings
  111. "-ffunction-sections", # place each function in its own section
  112. "-fdata-sections",
  113. "-mthumb",
  114. "-mcpu=%s" % env.BoardConfig().get("build.cpu"),
  115. "-fsingle-precision-constant"
  116. ],
  117. CXXFLAGS=[
  118. "-fno-exceptions",
  119. "-felide-constructors",
  120. "-fno-rtti",
  121. "-std=gnu++20",
  122. "-Wno-error=narrowing",
  123. "-fpermissive"
  124. ],
  125. CPPDEFINES=[
  126. ("F_CPU", "$BOARD_F_CPU"),
  127. "LAYOUT_US_ENGLISH"
  128. ],
  129. RANLIBFLAGS=["-s"],
  130. LINKFLAGS=[
  131. "-Wl,--gc-sections,--relax",
  132. "-mthumb",
  133. "-mcpu=%s" % env.BoardConfig().get("build.cpu"),
  134. "-Wl,--defsym=__rtc_localtime=$UNIX_TIME",
  135. "-fsingle-precision-constant"
  136. ],
  137. LIBS=["m", "stdc++"]
  138. )
  139. if not env.BoardConfig().get("build.ldscript", ""):
  140. env.Replace(LDSCRIPT_PATH=env.BoardConfig().get("build.arduino.ldscript", ""))
  141. if env.BoardConfig().id_ in ("teensy35", "teensy36", "teensy40", "teensy41"):
  142. fpv_version = "4-sp"
  143. if env.BoardConfig().id_.startswith("teensy4"):
  144. fpv_version = "5"
  145. env.Append(
  146. CCFLAGS=[
  147. "-mfloat-abi=hard",
  148. "-mfpu=fpv%s-d16" % fpv_version
  149. ],
  150. LINKFLAGS=[
  151. "-mfloat-abi=hard",
  152. "-mfpu=fpv%s-d16" % fpv_version
  153. ]
  154. )
  155. # Optimization
  156. if "TEENSY_OPT_FASTER_LTO" in env['CPPDEFINES']:
  157. env.Append(
  158. CCFLAGS=["-O2", "-flto", "-fno-fat-lto-objects"],
  159. LINKFLAGS=["-O2", "-flto", "-fno-fat-lto-objects", "-fuse-linker-plugin"]
  160. )
  161. elif "TEENSY_OPT_FAST" in env['CPPDEFINES']:
  162. env.Append(
  163. CCFLAGS=["-O1"],
  164. LINKFLAGS=["-O1"]
  165. )
  166. elif "TEENSY_OPT_FAST_LTO" in env['CPPDEFINES']:
  167. env.Append(
  168. CCFLAGS=["-O1", "-flto", "-fno-fat-lto-objects"],
  169. LINKFLAGS=["-O1", "-flto", "-fno-fat-lto-objects", "-fuse-linker-plugin"]
  170. )
  171. elif "TEENSY_OPT_FASTEST" in env['CPPDEFINES']:
  172. env.Append(
  173. CCFLAGS=["-O3"],
  174. LINKFLAGS=["-O3"]
  175. )
  176. elif "TEENSY_OPT_FASTEST_LTO" in env['CPPDEFINES']:
  177. env.Append(
  178. CCFLAGS=["-O3", "-flto", "-fno-fat-lto-objects"],
  179. LINKFLAGS=["-O3", "-flto", "-fno-fat-lto-objects", "-fuse-linker-plugin"]
  180. )
  181. elif "TEENSY_OPT_FASTEST_PURE_CODE" in env['CPPDEFINES']:
  182. env.Append(
  183. CCFLAGS=["-O3", "-mpure-code"],
  184. CPPDEFINES=["__PURE_CODE__"],
  185. LINKFLAGS=["-O3", "-mpure-code"]
  186. )
  187. elif "TEENSY_OPT_FASTEST_PURE_CODE_LTO" in env['CPPDEFINES']:
  188. env.Append(
  189. CCFLAGS=["-O3", "-mpure-code", "-flto", "-fno-fat-lto-objects"],
  190. CPPDEFINES=["__PURE_CODE__"],
  191. LINKFLAGS=["-O3", "-mpure-code", "-flto", "-fno-fat-lto-objects", "-fuse-linker-plugin"]
  192. )
  193. elif "TEENSY_OPT_DEBUG" in env['CPPDEFINES']:
  194. env.Append(
  195. CCFLAGS=["-g", "-Og"],
  196. LINKFLAGS=["-g", "-Og"]
  197. )
  198. elif "TEENSY_OPT_DEBUG_LTO" in env['CPPDEFINES']:
  199. env.Append(
  200. CCFLAGS=["-g", "-Og", "-flto", "-fno-fat-lto-objects"],
  201. LINKFLAGS=["-g", "-Og", "-flto", "-fno-fat-lto-objects", "-fuse-linker-plugin"]
  202. )
  203. elif "TEENSY_OPT_SMALLEST_CODE_LTO" in env['CPPDEFINES']:
  204. env.Append(
  205. CCFLAGS=["-Os", "--specs=nano.specs", "-flto", "-fno-fat-lto-objects"],
  206. LINKFLAGS=["-Os", "--specs=nano.specs", "-flto", "-fno-fat-lto-objects", "-fuse-linker-plugin"]
  207. )
  208. elif "TEENSY_OPT_FASTER" in env['CPPDEFINES']:
  209. env.Append(
  210. CCFLAGS=["-O2"],
  211. LINKFLAGS=["-O2"]
  212. )
  213. elif "TEENSY_OPT_SMALLEST_CODE" in env['CPPDEFINES']:
  214. env.Append(
  215. CCFLAGS=["-Os", "--specs=nano.specs"],
  216. LINKFLAGS=["-Os", "--specs=nano.specs"]
  217. )
  218. # default profiles
  219. else:
  220. # for Teensy LC => TEENSY_OPT_SMALLEST_CODE
  221. if env.BoardConfig().id_ == "teensylc":
  222. env.Append(
  223. CCFLAGS=["-Os", "--specs=nano.specs"],
  224. LINKFLAGS=["-Os", "--specs=nano.specs"]
  225. )
  226. # for others => TEENSY_OPT_FASTER
  227. else:
  228. env.Append(
  229. CCFLAGS=["-O2"],
  230. LINKFLAGS=["-O2"]
  231. )
  232. env.Append(
  233. ASFLAGS=env.get("CCFLAGS", [])[:]
  234. )
  235. if "cortex-m" in env.BoardConfig().get("build.cpu", ""):
  236. board = env.subst("$BOARD")
  237. math_lib = "arm_cortex%s_math"
  238. if board in ("teensy35", "teensy36"):
  239. math_lib = math_lib % "M4lf"
  240. elif board in ("teensy30", "teensy31"):
  241. math_lib = math_lib % "M4l"
  242. elif board.startswith("teensy4"):
  243. math_lib = math_lib % "M7lfsp"
  244. else:
  245. math_lib = math_lib % "M0l"
  246. env.Prepend(LIBS=[math_lib])
  247. # Teensy 2.x Core
  248. if BUILD_CORE == "teensy":
  249. env.Append(CPPPATH=[join(FRAMEWORK_DIR, "cores")])
  250. # search relative includes in teensy directories
  251. core_dir = join(FRAMEWORK_DIR, "cores", "teensy")
  252. for item in sorted(listdir(core_dir)):
  253. file_path = join(core_dir, item)
  254. if not isfile(file_path):
  255. continue
  256. content = None
  257. content_changed = False
  258. with open(file_path, encoding="latin-1") as fp:
  259. content = fp.read()
  260. if '#include "../' in content:
  261. content_changed = True
  262. content = content.replace('#include "../', '#include "')
  263. if not content_changed:
  264. continue
  265. with open(file_path, "w", encoding="latin-1") as fp:
  266. fp.write(content)
  267. else:
  268. env.Prepend(LIBPATH=[join(FRAMEWORK_DIR, "cores", BUILD_CORE)])
  269. #
  270. # Target: Build Core Library
  271. #
  272. libs = []
  273. if "build.variant" in env.BoardConfig():
  274. env.Append(
  275. CPPPATH=[
  276. join(FRAMEWORK_DIR, "variants",
  277. env.BoardConfig().get("build.variant"))
  278. ]
  279. )
  280. libs.append(env.BuildLibrary(
  281. join("$BUILD_DIR", "FrameworkArduinoVariant"),
  282. join(FRAMEWORK_DIR, "variants", env.BoardConfig().get("build.variant"))
  283. ))
  284. libs.append(env.BuildLibrary(
  285. join("$BUILD_DIR", "FrameworkArduino"),
  286. join(FRAMEWORK_DIR, "cores", BUILD_CORE)
  287. ))
  288. env.Prepend(LIBS=libs)