microbit-dal/source/MicroBitPin.cpp

216 lines
6.4 KiB
C++
Raw Normal View History

#include "MicroBit.h"
microbit: Memory Optimisation Mega Update 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: + MicroBitDFUService + MicroBitEventService + DeviceInformationService - 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.
2015-08-31 22:25:10 +00:00
#include "MicroBitPin.h"
/**
* Constructor.
* Create a Button representation with the given ID.
* @param id the ID of the new Pin object.
* @param name the pin name for this MicroBitPin instance to represent
* @param capability the capability of this pin, can it only be digital? can it only be analog? can it be both?
*
* Example:
* @code
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
* @endcode
*/
MicroBitPin::MicroBitPin(int id, PinName name, PinCapability capability)
{
//set mandatory attributes
this->id = id;
this->name = name;
this->capability = capability;
// Power up in a disconnected, low power state.
// If we're unused, this is how it will stay...
this->status = 0x00;
this->pin = NULL;
}
/**
* Disconnect any attached mbed IO from this pin.
* Used only when pin changes mode (i.e. Input/Output/Analog/Digital)
*/
void MicroBitPin::disconnect()
{
// This is a bit ugly, but rarely used code.
// It would be much better to use some polymorphism here, but the mBed I/O classes aren't arranged in an inheritance hierarchy... yet. :-)
if (status & IO_STATUS_DIGITAL_IN)
delete ((DigitalIn *)pin);
if (status & IO_STATUS_DIGITAL_OUT)
delete ((DigitalOut *)pin);
if (status & IO_STATUS_ANALOG_IN){
NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; // forcibly disable the ADC - BUG in mbed....
delete ((AnalogIn *)pin);
}
if (status & IO_STATUS_ANALOG_OUT)
{
if(((DynamicPwm *)pin)->getPinName() == name)
microbit: Memory Optimisation Mega Update 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: + MicroBitDFUService + MicroBitEventService + DeviceInformationService - 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.
2015-08-31 22:25:10 +00:00
((DynamicPwm *)pin)->release();
}
if (status & IO_STATUS_TOUCH_IN)
delete ((MicroBitButton *)pin);
this->pin = NULL;
this->status = status & IO_STATUS_EVENTBUS_ENABLED; //retain event bus status
}
/**
* Configures this IO pin as a digital output (if necessary) and sets the pin to 'value'.
* @param value 0 (LO) or 1 (HI)
*
* Example:
* @code
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
* P0.setDigitalValue(1); // P0 is now HI!
* @endcode
*/
void MicroBitPin::setDigitalValue(int value)
{
//check if this pin has a digital mode...
if(!(PIN_CAPABILITY_DIGITAL & capability) || value < 0 || value > 1)
return;
// Move into a Digital input state if necessary.
if (!(status & IO_STATUS_DIGITAL_OUT)){
disconnect();
pin = new DigitalOut(name);
status |= IO_STATUS_DIGITAL_OUT;
}
//write the value!
((DigitalOut *)pin)->write(value);
}
/**
* Configures this IO pin as a digital input (if necessary) and tests its current value.
* @return 1 if this input is high, 0 otherwise.
*
* Example:
* @code
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
* P0.getDigitalValue(); // P0 is either 0 or 1;
* @endcode
*/
int MicroBitPin::getDigitalValue()
{
//check if this pin has a digital mode...
if(!(PIN_CAPABILITY_DIGITAL & capability))
return MICROBIT_IO_OP_NA;
// Move into a Digital input state if necessary.
if (!(status & IO_STATUS_DIGITAL_IN)){
disconnect();
pin = new DigitalIn(name,PullDown); //pull down!
status |= IO_STATUS_DIGITAL_IN;
}
return ((DigitalIn *)pin)->read();
}
/**
* Configures this IO pin as an analogue output (if necessary and possible).
* Change the DAC value to the given level.
* @param value the level to set on the output pin, in the range 0..255
* @note We have a maximum of 3 PWM channels for this device - one is reserved for the display... the other two are reconfigured dynamically when they are required.
*/
void MicroBitPin::setAnalogValue(int value)
{
//check if this pin has an analogue mode...
if(!(PIN_CAPABILITY_ANALOG & capability))
return;
//sanitise the brightness level
if(value < 0 || value > MICROBIT_PIN_MAX_OUTPUT)
return;
float level = (float)value / float(MICROBIT_PIN_MAX_OUTPUT);
// Move into an analogue input state if necessary, if we are no longer the focus of a DynamicPWM instance, allocate ourselves again!
if (!(status & IO_STATUS_ANALOG_OUT) || !(((DynamicPwm *)pin)->getPinName() == name)){
disconnect();
pin = (void *)DynamicPwm::allocate(name);
status |= IO_STATUS_ANALOG_OUT;
}
//perform a write with an extra check! :)
if(((DynamicPwm *)pin)->getPinName() == name)
((DynamicPwm *)pin)->write(level);
}
/**
* Configures this IO pin as an analogue input (if necessary and possible).
* @return the current analogue level on the pin, in the range 0-0xFFFF
*
* Example:
* @code
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_BOTH);
* P0.getAnalogValue(); // P0 is a value in the range of 0 - 0xFFFF
* @endcode
*/
int MicroBitPin::getAnalogValue()
{
//check if this pin has an analogue mode...
if(!(PIN_CAPABILITY_ANALOG & capability))
return MICROBIT_IO_OP_NA;
// Move into an analogue input state if necessary.
if (!(status & IO_STATUS_ANALOG_IN)){
disconnect();
pin = new AnalogIn(name);
status |= IO_STATUS_ANALOG_IN;
}
//perform a read!
return ((AnalogIn *)pin)->read_u16();
}
/**
* Configures this IO pin as a makey makey style touch sensor (if necessary) and tests its current debounced state.
* @return 1 if pin is touched, 0 otherwise.
*
* Example:
* @code
* MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL);
* if(P0.isTouched())
* {
* uBit.display.clear();
* }
* @endcode
*/
int MicroBitPin::isTouched()
{
//check if this pin has a touch mode...
if(!(PIN_CAPABILITY_TOUCH & capability))
return MICROBIT_IO_OP_NA;
// Move into a touch input state if necessary.
if (!(status & IO_STATUS_TOUCH_IN)){
disconnect();
pin = new MicroBitButton(id, name);
status |= IO_STATUS_TOUCH_IN;
}
return ((MicroBitButton *)pin)->isPressed();
}
/**
* Configures the PWM period of the analog output to the given value.
* If this pin is not configured as an analog output, the operation
* has no effect.
*
* @param period The new period for the analog output in milliseconds.
*/
void MicroBitPin::setAnalogPeriod(int period)
{
if (status & IO_STATUS_ANALOG_OUT)
((DynamicPwm *)pin)->setPeriodUs(period*1000);
}