stm32_serialbuffer.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #
  2. # stm32_serialbuffer.py
  3. #
  4. import pioutil
  5. if pioutil.is_pio_build():
  6. env = pioutil.env
  7. # Get a build flag's value or None
  8. def getBuildFlagValue(name):
  9. for flag in build_flags:
  10. if isinstance(flag, list) and flag[0] == name:
  11. return flag[1]
  12. return None
  13. # Get an overriding buffer size for RX or TX from the build flags
  14. def getInternalSize(side):
  15. return getBuildFlagValue(f"MF_{side}_BUFFER_SIZE") or \
  16. getBuildFlagValue(f"SERIAL_{side}_BUFFER_SIZE") or \
  17. getBuildFlagValue(f"USART_{side}_BUF_SIZE")
  18. # Get the largest defined buffer size for RX or TX
  19. def getBufferSize(side, default):
  20. # Get a build flag value or fall back to the given default
  21. internal = int(getInternalSize(side) or default)
  22. flag = side + "_BUFFER_SIZE"
  23. # Return the largest value
  24. return max(int(mf[flag]), internal) if flag in mf else internal
  25. # Add a build flag if it's not already defined
  26. def tryAddFlag(name, value):
  27. if getBuildFlagValue(name) is None:
  28. env.Append(BUILD_FLAGS=[f"-D{name}={value}"])
  29. # Marlin uses the `RX_BUFFER_SIZE` \ `TX_BUFFER_SIZE` options to
  30. # configure buffer sizes for receiving \ transmitting serial data.
  31. # Stm32duino uses another set of defines for the same purpose, so this
  32. # script gets the values from the configuration and uses them to define
  33. # `SERIAL_RX_BUFFER_SIZE` and `SERIAL_TX_BUFFER_SIZE` as global build
  34. # flags so they are available for use by the platform.
  35. #
  36. # The script will set the value as the default one (64 bytes)
  37. # or the user-configured one, whichever is higher.
  38. #
  39. # Marlin's default buffer sizes are 128 for RX and 32 for TX.
  40. # The highest value is taken (128/64).
  41. #
  42. # If MF_*_BUFFER_SIZE, SERIAL_*_BUFFER_SIZE, USART_*_BUF_SIZE, are
  43. # defined, the first of these values will be used as the minimum.
  44. build_flags = env.ParseFlags(env.get('BUILD_FLAGS'))["CPPDEFINES"]
  45. mf = env["MARLIN_FEATURES"]
  46. # Get the largest defined buffer sizes for RX or TX, using defaults for undefined
  47. rxBuf = getBufferSize("RX", 128)
  48. txBuf = getBufferSize("TX", 64)
  49. # Provide serial buffer sizes to the stm32duino platform
  50. tryAddFlag("SERIAL_RX_BUFFER_SIZE", rxBuf)
  51. tryAddFlag("SERIAL_TX_BUFFER_SIZE", txBuf)
  52. tryAddFlag("USART_RX_BUF_SIZE", rxBuf)
  53. tryAddFlag("USART_TX_BUF_SIZE", txBuf)