This release contains a widespread set of updates and optimisations to the micro:bit
runtime, with a view to reducing the SRAM footprint of the whole system. This is to
provide as much usable HEAP storage for application programs as possible.
Specific updates and optimisations include:
- Additional compilation flags to allow the core micro:bit runtime to be configured.
These are defined in MicroBitConfig.h
- A custom heap allocator. This is now included for two reasons:
1) To provide a simple mechanism to to utilise both the mbed heap space and other memory
regions (such as unused memory in the SoftDevice region) as a single virtual heap.
2) To address some issues that have been noted that are attributable to heap fragmentation.
The micro:bit heap allocator has a simple algorithm, but one that is chosen to respond
well to the relativelt high 'heap churn' found in the micro:bit environment.
All micro:bit components and user programs now use this heap allocator trasparently.
- Updates to BLE services to remove persistent references to their GATT services. This consumes
vast amounts SRAM, rather unecessarily. Instead only handles to the relevant GATT characteristics
are now stored. This specifically includes:
- Updates to the Fiber scheduler to save SRAM. More specifically:
+ Removed the need to hold an empty processor context to intialise fibers.
+ The IDLE fiber now runs without a stack
+ fiber stacks are now only created when a fiber is descheduled for the first time, thereby reducing heap churn.
+ the 'main' fiber is now recycled into the fiber_pool if it leaves app_main()
+ fibers created through invoke() now only maintains the necessary part of teh parent stack that is needed, thereby
reducing the stack size of spawned fibers.
- Updates to the Message Bus to reduce the overall memory footprint of processing events. More specifically:
+ Event handlers are now always called using invoke(), such that non-blocking event handlers no longer need
a dedicated fiber to execute - thereby saving SRAM and processor time.
+ Processing of events from the event queue is now rate paced. Events only continue to be processed as long as there
are no fibers on the run queue. i.e. event processing is no longer greedy, thereby reducing the number of fibers
created on the runqueue.
- Updates to BLUEZOENE code to bring up core BLE services even if they are not enabled by default. This allows
programs that do not require BLE to operate to benefit from the full range of SRAM, whilst still allowing the
device to be programmed over BLE.
- Updates to the Soft Device initialisation configuration, reducing the size of the GATT table held in the top 1.8K
of its 8K memory region to around 800 bytes. This is sufficient to run the default set of BLE services on the micro:bit
so the additional memory is configured as HEAP storage by MicroBitHeapAllocator.
- Minor changes to a range of components to integrate with the above changes.
+ rename of free() to release() in DynamicPWM to avoid namespace collision with MicroBitHeap free()
+ rename of fork_on_block to invoke() to enhance readbility.
- Many code cleanups and updates to out of date comments.
The microbit fiber scheduler is often used to service event handlers by the microbit message bus.
This provides a very elegant decoupling of user code from system code, interrupt handlers and
also allows users to make blocking calls within event handlers. However, many event handlers are
non-blocking, so launching a dedicated fiber is wasteful in terms of time and SRAM.
This patch adds fork_on_block() to the scheduler. Inspired by the UNIX copy_on_write
technique, this optimisation is semantically equivalent to a create_fiber() call, but will first attempt
to execute the given function in the context of the currently running fiber, and *only* create a fiber
if the given code attempts a blocking operation.
More specifically, this update:
- adds fork_on_block() functions for parameterised and non-parameterised functions.
- adds fields to the fiber context to record the status of parent/child fibers.
- adds optimised ASM functions to store and restore Cortex M0 register context.
- adds a utility function to determine if the processor is executing in interrupt context.
- updates to sleep() and wait_for_event() to handle fork_on_block semantics.
- minor code optimsations within the scheduler.