constrained.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. .. _constrained:
  2. MicroPython on Microcontrollers
  3. ===============================
  4. MicroPython is designed to be capable of running on microcontrollers. These
  5. have hardware limitations which may be unfamiliar to programmers more familiar
  6. with conventional computers. In particular the amount of RAM and nonvolatile
  7. "disk" (flash memory) storage is limited. This tutorial offers ways to make
  8. the most of the limited resources. Because MicroPython runs on controllers
  9. based on a variety of architectures, the methods presented are generic: in some
  10. cases it will be necessary to obtain detailed information from platform specific
  11. documentation.
  12. Flash Memory
  13. ------------
  14. On the Pyboard the simple way to address the limited capacity is to fit a micro
  15. SD card. In some cases this is impractical, either because the device does not
  16. have an SD card slot or for reasons of cost or power consumption; hence the
  17. on-chip flash must be used. The firmware including the MicroPython subsystem is
  18. stored in the onboard flash. The remaining capacity is available for use. For
  19. reasons connected with the physical architecture of the flash memory part of
  20. this capacity may be inaccessible as a filesystem. In such cases this space may
  21. be employed by incorporating user modules into a firmware build which is then
  22. flashed to the device.
  23. There are two ways to achieve this: frozen modules and frozen bytecode. Frozen
  24. modules store the Python source with the firmware. Frozen bytecode uses the
  25. cross compiler to convert the source to bytecode which is then stored with the
  26. firmware. In either case the module may be accessed with an import statement:
  27. .. code::
  28. import mymodule
  29. The procedure for producing frozen modules and bytecode is platform dependent;
  30. instructions for building the firmware can be found in the README files in the
  31. relevant part of the source tree.
  32. In general terms the steps are as follows:
  33. * Clone the MicroPython `repository <https://github.com/micropython/micropython>`_.
  34. * Acquire the (platform specific) toolchain to build the firmware.
  35. * Build the cross compiler.
  36. * Place the modules to be frozen in a specified directory (dependent on whether
  37. the module is to be frozen as source or as bytecode).
  38. * Build the firmware. A specific command may be required to build frozen
  39. code of either type - see the platform documentation.
  40. * Flash the firmware to the device.
  41. RAM
  42. ---
  43. When reducing RAM usage there are two phases to consider: compilation and
  44. execution. In addition to memory consumption, there is also an issue known as
  45. heap fragmentation. In general terms it is best to minimise the repeated
  46. creation and destruction of objects. The reason for this is covered in the
  47. section covering the `heap`_.
  48. Compilation Phase
  49. ~~~~~~~~~~~~~~~~~
  50. When a module is imported, MicroPython compiles the code to bytecode which is
  51. then executed by the MicroPython virtual machine (VM). The bytecode is stored
  52. in RAM. The compiler itself requires RAM, but this becomes available for use
  53. when the compilation has completed.
  54. If a number of modules have already been imported the situation can arise where
  55. there is insufficient RAM to run the compiler. In this case the import
  56. statement will produce a memory exception.
  57. If a module instantiates global objects on import it will consume RAM at the
  58. time of import, which is then unavailable for the compiler to use on subsequent
  59. imports. In general it is best to avoid code which runs on import; a better
  60. approach is to have initialisation code which is run by the application after
  61. all modules have been imported. This maximises the RAM available to the
  62. compiler.
  63. If RAM is still insufficient to compile all modules one solution is to
  64. precompile modules. MicroPython has a cross compiler capable of compiling Python
  65. modules to bytecode (see the README in the mpy-cross directory). The resulting
  66. bytecode file has a .mpy extension; it may be copied to the filesystem and
  67. imported in the usual way. Alternatively some or all modules may be implemented
  68. as frozen bytecode: on most platforms this saves even more RAM as the bytecode
  69. is run directly from flash rather than being stored in RAM.
  70. Execution Phase
  71. ~~~~~~~~~~~~~~~
  72. There are a number of coding techniques for reducing RAM usage.
  73. **Constants**
  74. MicroPython provides a ``const`` keyword which may be used as follows:
  75. .. code::
  76. from micropython import const
  77. ROWS = const(33)
  78. _COLS = const(0x10)
  79. a = ROWS
  80. b = _COLS
  81. In both instances where the constant is assigned to a variable the compiler
  82. will avoid coding a lookup to the name of the constant by substituting its
  83. literal value. This saves bytecode and hence RAM. However the ``ROWS`` value
  84. will occupy at least two machine words, one each for the key and value in the
  85. globals dictionary. The presence in the dictionary is necessary because another
  86. module might import or use it. This RAM can be saved by prepending the name
  87. with an underscore as in ``_COLS``: this symbol is not visible outside the
  88. module so will not occupy RAM.
  89. The argument to ``const()`` may be anything which, at compile time, evaluates
  90. to an integer e.g. ``0x100`` or ``1 << 8``. It can even include other const
  91. symbols that have already been defined, e.g. ``1 << BIT``.
  92. **Constant data structures**
  93. Where there is a substantial volume of constant data and the platform supports
  94. execution from Flash, RAM may be saved as follows. The data should be located in
  95. Python modules and frozen as bytecode. The data must be defined as `bytes`
  96. objects. The compiler 'knows' that `bytes` objects are immutable and ensures
  97. that the objects remain in flash memory rather than being copied to RAM. The
  98. `ustruct` module can assist in converting between `bytes` types and other
  99. Python built-in types.
  100. When considering the implications of frozen bytecode, note that in Python
  101. strings, floats, bytes, integers and complex numbers are immutable. Accordingly
  102. these will be frozen into flash. Thus, in the line
  103. .. code::
  104. mystring = "The quick brown fox"
  105. the actual string "The quick brown fox" will reside in flash. At runtime a
  106. reference to the string is assigned to the *variable* ``mystring``. The reference
  107. occupies a single machine word. In principle a long integer could be used to
  108. store constant data:
  109. .. code::
  110. bar = 0xDEADBEEF0000DEADBEEF
  111. As in the string example, at runtime a reference to the arbitrarily large
  112. integer is assigned to the variable ``bar``. That reference occupies a
  113. single machine word.
  114. It might be expected that tuples of integers could be employed for the purpose
  115. of storing constant data with minimal RAM use. With the current compiler this
  116. is ineffective (the code works, but RAM is not saved).
  117. .. code::
  118. foo = (1, 2, 3, 4, 5, 6, 100000)
  119. At runtime the tuple will be located in RAM. This may be subject to future
  120. improvement.
  121. **Needless object creation**
  122. There are a number of situations where objects may unwittingly be created and
  123. destroyed. This can reduce the usability of RAM through fragmentation. The
  124. following sections discuss instances of this.
  125. **String concatenation**
  126. Consider the following code fragments which aim to produce constant strings:
  127. .. code::
  128. var = "foo" + "bar"
  129. var1 = "foo" "bar"
  130. var2 = """\
  131. foo\
  132. bar"""
  133. Each produces the same outcome, however the first needlessly creates two string
  134. objects at runtime, allocates more RAM for concatenation before producing the
  135. third. The others perform the concatenation at compile time which is more
  136. efficient, reducing fragmentation.
  137. Where strings must be dynamically created before being fed to a stream such as
  138. a file it will save RAM if this is done in a piecemeal fashion. Rather than
  139. creating a large string object, create a substring and feed it to the stream
  140. before dealing with the next.
  141. The best way to create dynamic strings is by means of the string ``format()``
  142. method:
  143. .. code::
  144. var = "Temperature {:5.2f} Pressure {:06d}\n".format(temp, press)
  145. **Buffers**
  146. When accessing devices such as instances of UART, I2C and SPI interfaces, using
  147. pre-allocated buffers avoids the creation of needless objects. Consider these
  148. two loops:
  149. .. code::
  150. while True:
  151. var = spi.read(100)
  152. # process data
  153. buf = bytearray(100)
  154. while True:
  155. spi.readinto(buf)
  156. # process data in buf
  157. The first creates a buffer on each pass whereas the second re-uses a pre-allocated
  158. buffer; this is both faster and more efficient in terms of memory fragmentation.
  159. **Bytes are smaller than ints**
  160. On most platforms an integer consumes four bytes. Consider the two calls to the
  161. function ``foo()``:
  162. .. code::
  163. def foo(bar):
  164. for x in bar:
  165. print(x)
  166. foo((1, 2, 0xff))
  167. foo(b'\1\2\xff')
  168. In the first call a tuple of integers is created in RAM. The second efficiently
  169. creates a `bytes` object consuming the minimum amount of RAM. If the module
  170. were frozen as bytecode, the `bytes` object would reside in flash.
  171. **Strings Versus Bytes**
  172. Python3 introduced Unicode support. This introduced a distinction between a
  173. string and an array of bytes. MicroPython ensures that Unicode strings take no
  174. additional space so long as all characters in the string are ASCII (i.e. have
  175. a value < 126). If values in the full 8-bit range are required `bytes` and
  176. `bytearray` objects can be used to ensure that no additional space will be
  177. required. Note that most string methods (e.g. :meth:`str.strip()`) apply also to `bytes`
  178. instances so the process of eliminating Unicode can be painless.
  179. .. code::
  180. s = 'the quick brown fox' # A string instance
  181. b = b'the quick brown fox' # A bytes instance
  182. Where it is necessary to convert between strings and bytes the :meth:`str.encode`
  183. and the :meth:`bytes.decode` methods can be used. Note that both strings and bytes
  184. are immutable. Any operation which takes as input such an object and produces
  185. another implies at least one RAM allocation to produce the result. In the
  186. second line below a new bytes object is allocated. This would also occur if ``foo``
  187. were a string.
  188. .. code::
  189. foo = b' empty whitespace'
  190. foo = foo.lstrip()
  191. **Runtime compiler execution**
  192. The Python funcitons `eval` and `exec` invoke the compiler at runtime, which
  193. requires significant amounts of RAM. Note that the ``pickle`` library from
  194. `micropython-lib` employs `exec`. It may be more RAM efficient to use the
  195. `ujson` library for object serialisation.
  196. **Storing strings in flash**
  197. Python strings are immutable hence have the potential to be stored in read only
  198. memory. The compiler can place in flash strings defined in Python code. As with
  199. frozen modules it is necessary to have a copy of the source tree on the PC and
  200. the toolchain to build the firmware. The procedure will work even if the
  201. modules have not been fully debugged, so long as they can be imported and run.
  202. After importing the modules, execute:
  203. .. code::
  204. micropython.qstr_info(1)
  205. Then copy and paste all the Q(xxx) lines into a text editor. Check for and
  206. remove lines which are obviously invalid. Open the file qstrdefsport.h which
  207. will be found in ports/stm32 (or the equivalent directory for the architecture in
  208. use). Copy and paste the corrected lines at the end of the file. Save the file,
  209. rebuild and flash the firmware. The outcome can be checked by importing the
  210. modules and again issuing:
  211. .. code::
  212. micropython.qstr_info(1)
  213. The Q(xxx) lines should be gone.
  214. .. _heap:
  215. The Heap
  216. --------
  217. When a running program instantiates an object the necessary RAM is allocated
  218. from a fixed size pool known as the heap. When the object goes out of scope (in
  219. other words becomes inaccessible to code) the redundant object is known as
  220. "garbage". A process known as "garbage collection" (GC) reclaims that memory,
  221. returning it to the free heap. This process runs automatically, however it can
  222. be invoked directly by issuing `gc.collect()`.
  223. The discourse on this is somewhat involved. For a 'quick fix' issue the
  224. following periodically:
  225. .. code::
  226. gc.collect()
  227. gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
  228. Fragmentation
  229. ~~~~~~~~~~~~~
  230. Say a program creates an object ``foo``, then an object ``bar``. Subsequently
  231. ``foo`` goes out of scope but ``bar`` remains. The RAM used by ``foo`` will be
  232. reclaimed by GC. However if ``bar`` was allocated to a higher address, the
  233. RAM reclaimed from ``foo`` will only be of use for objects no bigger than
  234. ``foo``. In a complex or long running program the heap can become fragmented:
  235. despite there being a substantial amount of RAM available, there is insufficient
  236. contiguous space to allocate a particular object, and the program fails with a
  237. memory error.
  238. The techniques outlined above aim to minimise this. Where large permanent buffers
  239. or other objects are required it is best to instantiate these early in the
  240. process of program execution before fragmentation can occur. Further improvements
  241. may be made by monitoring the state of the heap and by controlling GC; these are
  242. outlined below.
  243. Reporting
  244. ~~~~~~~~~
  245. A number of library functions are available to report on memory allocation and
  246. to control GC. These are to be found in the `gc` and `micropython` modules.
  247. The following example may be pasted at the REPL (``ctrl e`` to enter paste mode,
  248. ``ctrl d`` to run it).
  249. .. code::
  250. import gc
  251. import micropython
  252. gc.collect()
  253. micropython.mem_info()
  254. print('-----------------------------')
  255. print('Initial free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
  256. def func():
  257. a = bytearray(10000)
  258. gc.collect()
  259. print('Func definition: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
  260. func()
  261. print('Func run free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
  262. gc.collect()
  263. print('Garbage collect free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
  264. print('-----------------------------')
  265. micropython.mem_info(1)
  266. Methods employed above:
  267. * `gc.collect()` Force a garbage collection. See footnote.
  268. * `micropython.mem_info()` Print a summary of RAM utilisation.
  269. * `gc.mem_free()` Return the free heap size in bytes.
  270. * `gc.mem_alloc()` Return the number of bytes currently allocated.
  271. * ``micropython.mem_info(1)`` Print a table of heap utilisation (detailed below).
  272. The numbers produced are dependent on the platform, but it can be seen that
  273. declaring the function uses a small amount of RAM in the form of bytecode
  274. emitted by the compiler (the RAM used by the compiler has been reclaimed).
  275. Running the function uses over 10KiB, but on return ``a`` is garbage because it
  276. is out of scope and cannot be referenced. The final `gc.collect()` recovers
  277. that memory.
  278. The final output produced by ``micropython.mem_info(1)`` will vary in detail but
  279. may be interpreted as follows:
  280. ====== =================
  281. Symbol Meaning
  282. ====== =================
  283. . free block
  284. h head block
  285. = tail block
  286. m marked head block
  287. T tuple
  288. L list
  289. D dict
  290. F float
  291. B byte code
  292. M module
  293. ====== =================
  294. Each letter represents a single block of memory, a block being 16 bytes. So each
  295. line of the heap dump represents 0x400 bytes or 1KiB of RAM.
  296. Control of Garbage Collection
  297. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  298. A GC can be demanded at any time by issuing `gc.collect()`. It is advantageous
  299. to do this at intervals, firstly to pre-empt fragmentation and secondly for
  300. performance. A GC can take several milliseconds but is quicker when there is
  301. little work to do (about 1ms on the Pyboard). An explicit call can minimise that
  302. delay while ensuring it occurs at points in the program when it is acceptable.
  303. Automatic GC is provoked under the following circumstances. When an attempt at
  304. allocation fails, a GC is performed and the allocation re-tried. Only if this
  305. fails is an exception raised. Secondly an automatic GC will be triggered if the
  306. amount of free RAM falls below a threshold. This threshold can be adapted as
  307. execution progresses:
  308. .. code::
  309. gc.collect()
  310. gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
  311. This will provoke a GC when more than 25% of the currently free heap becomes
  312. occupied.
  313. In general modules should instantiate data objects at runtime using constructors
  314. or other initialisation functions. The reason is that if this occurs on
  315. initialisation the compiler may be starved of RAM when subsequent modules are
  316. imported. If modules do instantiate data on import then `gc.collect()` issued
  317. after the import will ameliorate the problem.
  318. String Operations
  319. -----------------
  320. MicroPython handles strings in an efficient manner and understanding this can
  321. help in designing applications to run on microcontrollers. When a module
  322. is compiled, strings which occur multiple times are stored once only, a process
  323. known as string interning. In MicroPython an interned string is known as a ``qstr``.
  324. In a module imported normally that single instance will be located in RAM, but
  325. as described above, in modules frozen as bytecode it will be located in flash.
  326. String comparisons are also performed efficiently using hashing rather than
  327. character by character. The penalty for using strings rather than integers may
  328. hence be small both in terms of performance and RAM usage - a fact which may
  329. come as a surprise to C programmers.
  330. Postscript
  331. ----------
  332. MicroPython passes, returns and (by default) copies objects by reference. A
  333. reference occupies a single machine word so these processes are efficient in
  334. RAM usage and speed.
  335. Where variables are required whose size is neither a byte nor a machine word
  336. there are standard libraries which can assist in storing these efficiently and
  337. in performing conversions. See the `array`, `ustruct` and `uctypes`
  338. modules.
  339. Footnote: gc.collect() return value
  340. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  341. On Unix and Windows platforms the `gc.collect()` method returns an integer
  342. which signifies the number of distinct memory regions that were reclaimed in the
  343. collection (more precisely, the number of heads that were turned into frees). For
  344. efficiency reasons bare metal ports do not return this value.