Commit graph

188 commits

Author SHA1 Message Date
Joe Finney d1d78b9b35 microbit: added MicroBitStorage for Persistent data
Integrated simple 1K block buffer to host small amount of persistent
data and a 1K FLASH memory block located immediately below the FOTA/DFU
bootloader and APPDATA region that intended to hold core configuration
information, such as:

* BLE bonding information.
* Compass Calibration data.
* Thermometer Calibration data.

The block is resident at 0x0003B400 - 0x0003B800.
2016-02-02 21:22:49 +00:00
James Devine 3fa9bb09c2 microbit: fixed an off by 10 bug
Missed a zero in a previous commit, that caused a system lockup due to
a missed timer event.

This commit also changes the minimum display brightness to 4, to also
minimise another feature that needs fixing in the future, where the
timer does not interrupt in the expected time frame.
2016-02-02 01:13:39 +00:00
James Devine a27b2314be microbit: modified the range of read() MicroBitLightSensor
Previously the range of MicrobitLightSensor read() was 0 - 100,
this didn't align with other APIs.

The new range is 0-255 which aligns with display brightness to give
a more consistent overall API.
2016-02-01 23:58:33 +00:00
Joe Finney e983b80b0b Merge branch 'master' into ble-bondtablepurge 2016-02-01 22:50:05 +00:00
Joe Finney e7132b0d13 microbit: Change to BLE bonding table replacement policy
Previous revisions of microbit-dal would handle the case of a bonding request
when the bond table was full by attempting to purge the bonding table on
demand. This suffered two flaws:

 1) Nordic's device manager does not support the purging of the bonding table
while a connection is active (including a pairing request).

 2) Some devices (e.g. Android 4.4.2 take two entries in the bonding table
when paired), thus making predictions of how 'full' the table is challenging.

This patch employs a simpler replacement policy. The maximum size of the bond
table has been increased to 6 (from 4). However, if the bond table contains 4
or more entries when entering pairing mode it will be purged in advance of any
pairing requetss being initiated.
2016-02-01 22:30:03 +00:00
James Devine 97c49d3279 Merge pull request #87 from lancaster-university/broadcast-radio
Adding Multicast peer-to-peer radio support MicroBitRadio!
2016-02-01 21:50:54 +00:00
James Devine 004b701419 Merge pull request #86 from lancaster-university/light-sensor
Adding MicroBitLightSensor to the DAL
2016-02-01 21:50:22 +00:00
James Devine 8150b2ac67 microbit: modified as per suggestions
* This commit fixes an exposed member variable
  that was not intended to be public 'tickPeriod'

* normalised the light level to return values between 0 - 100
2016-02-01 21:46:15 +00:00
Joe Finney 9026ca6e98 microbit: Optimisation of BLE mutex.
The BLE stack locks out the CPU for long periods of time when necessary. This
can be alleviated by eans of a flag that release the CPU between radio events.

This can cause issues from applications with tighter timing requirements.
This patch only enables this MUTEX when a BLE connection is active, rather
than automatically at startup. Therefore, application that do not actively use
BLE will not suffer this penalty.
2016-02-01 19:25:07 +00:00
Joe Finney 6838b38fc1 microbit: Remove redundant characteristics in BLE DeviceInformationService
The MANUFACTURER and HARDWARE_REVISION fields from this standard service have
been removed, as they carried no useful information.
2016-02-01 18:59:48 +00:00
Joe Finney 8bec31fe76 microbit: Additional validation on MicroBitRadio methods
MicroBitRadio methods that touch internal RADIO registers now ensure that
the BLE stack is not running before changing such registers.
2016-02-01 18:19:02 +00:00
Joe Finney d7aa95e867 microbit: minor updates to consistently use the MICROBIT_RADIO_HEADER_SIZE constant 2016-02-01 18:05:02 +00:00
Joe Finney 12c9ed246a microbit: Whitespace fixes 2016-02-01 17:54:33 +00:00
Joe Finney 420a73673d microbit: rename of MicroBitRadioEvent methods to align with MessageBus naming conventions. 2016-02-01 17:43:17 +00:00
Joe Finney 336c30f2ec microbit: Integrate Multicast peer to peer communications into MicroBit.cpp
- Added instance variable and constructor call in MircoBit.cpp
  - Added event definitions for RADIO related indications
2016-02-01 04:40:21 +00:00
Joe Finney d9d2343ab6 microbit: Add Multicast peer-to-peer radio capability
Integration of new radio mode that permits simple local area multicast communication
directly between micro:bits using the device's on board radio hardware.

It should be noted that this implementation build directly on the nrf51822
hardware. It is not a BLE protocol, and must (currently) be operated with BLE

A standard on the wire configuration is defined, to promote future
interoperability between projects and languages. A lightweight
packet format is also defined and adhered to, so as to promote future
extensibility without sacrificing compatibility. The notion of groups is also
introduced, that enable multiple groups of children to work in the same area
without compromising each other's projects.

In adition to a low level packet radio driver, two higher level protocols are
defined that allow simple user datagram communication between micro:bits and
an EventBus protocol that allows the micro:bit's MessageBus to be extended to
other micro:bits.
2016-02-01 04:29:27 +00:00
Joe Finney e3a23988f6 microbit: Promote constants in MicroBitBLEManager to .h file
Moved configuration constants from .cpp to .h file, so that they can be reused
more easily from other code, such as the broadcast radio code.
2016-02-01 04:25:51 +00:00
James Devine 6b0e9cf489 microbit: integrated MicroBitLightSensor with MicroBitDisplay
Previously, to light sense a user would have to configure a
MicroBitLightSensor instance themselves, and flip the display mode
manually. This is difficult in languages that target our API.

This commit resolves that by adding a new method readLightLevel which
flips the display mode, and instantiates a light sensor.

When changing the mode, the tickSpeed is also modified to reduce
artefacts on the display.
2016-01-29 14:13:41 +00:00
James Devine 2049e63039 microbit: added MicroBitLightSensor
Added a new class called MicroBitLightSensor which interleaves sensing
with the display.

This class takes advantage of the previous commits which add a dynamic
system tick, and a new display mode for the display.

A future optimisation would be for the display to handle the instantion
of a light sensor when the display mode is changed!
2016-01-29 14:10:52 +00:00
James Devine 6e316dc741 microbit: added accessor/mutator for systemTick
This commit adds a mutator that dynamically reconfigures the
systemTicker to call systemTick at a different period to the default.

The accessor returns the current tick speed in milliseconds.

The accessor is now also used in scheduler_tick to keep timing as
accurate as possible.

MICROBIT_DISPLAY_REFRESH_PERIOD has now been removed, and replaced with
2016-01-29 14:07:36 +00:00
James Devine 6f812aa474 microbit: added new display mode, and accessor
This commit introduces a new display mode,
every “frame” is dropped allowing for other components that use the
display to use this dropped frame for processing. This is in
preparation for Light Sensing on the 5x5 LED matrix.

An accessor has also been added to MicroBitDisplay to check the current
display mode!
2016-01-29 14:03:24 +00:00
James Devine 6939ad25d9 microbit: formatting changes to MicroBitFiber
Corrected whitespace in MicroBitFiber.
2016-01-29 13:57:34 +00:00
Joe Finney 0bbb5b9123 microbit: Code cleanup
Removal of debugging information and redundant algorithms.
2016-01-28 17:09:01 +00:00
Joe Finney 8756bbd103 WIP: Lower RAM footprint compass calibration
- moved to single precision floats
- optimised transpose/multiply operation
- optimisation of algorithm
2016-01-26 21:52:22 +00:00
Joe Finney e3fdc5a290 microbit: Support for bonding only available in pairing mode
Update to BLE initialisation code. Ability to initiate pairing is now
restricted to pairing mode.
2016-01-25 21:05:15 +00:00
Joe Finney 99d7231945 microbit: Persistance of BLE Service Chnaged metadata
BLE maintains metadata on its characterisitics, some of which record whether
or not a central device (phone/tablet) wishes to receive notifications when a
given BLE characteristic changes value. One such characterisitic is used as
part of a core BLE service to indicate of the BLE profile of a device has

IOS is heavily reliant on this feature, and expects the state of this metadata
to be persistent across device reboots. This patch ensures that this metadata
is always set to allow Service Changed indications to be propagated to central
2016-01-25 21:05:13 +00:00
Joe Finney 6a7436e5c7 Merge pull request #48 from remay/quiet-gcc-build
Tidy up gcc -Wall fixes

Thanks @remay!
2016-01-25 20:55:37 +00:00
Damien George 3160ba6268 Make MicroBit::seedRandom a public function with 2 variants.
seedRandom() seeds from the hardware RNG, whilst seedRandom(uint32_t)
seeds from the given 32-bit number.
2016-01-25 15:20:25 +00:00
Joe Finney a4c0915fff microbit: BUGFIX Prevent BLE Magnetometer service from initiating compass

MicroBitCompass initiates a calibration procedure on demand if a heading is
requested from an uncalibrated device. The BLE MagnetometerService also
attempts to retrieve a heading periodically, but it is not good behaviour to
initiate calibration as a response to this.

This patch introduces a check such that the BLE MagnetometerService will only
attempt to read heading data from a previously calibrated compass.
2016-01-19 00:32:02 +00:00
Joe Finney 4f1870fda9 Merge branch 'master' into ble-whitelisting 2016-01-17 00:44:35 +00:00
Joe Finney 13eb3c07e7 microbit: Add explicit service changed notification
BLE central devices (such as mobile phones/tablets) are permitted to cache the
list of services and characteristics provided by a BLE peripheral such as the
micro:bit. This was causing compatibility problems with IOS during FOTA
flashing, as the the list of BLE services may change after flashing.
2016-01-17 00:23:00 +00:00
Joe Finney 1c9ae30d1c microbit: Added basic bond table management
The Nordic BLE stack contains a fixed size bond table - a list of devices that
have paired with our device (in the case the micro:bit). The default mbed
wrapper around the nordic stack does not account for the case where the bond
table is full.

This patch implements basic bounds checking within the MicroBitBLEManager so
that this table cannot overrun. If a new device attempts to pair when the bond
table is full, the table is emptied prior to completing bonding.
2016-01-16 21:05:46 +00:00
Joe Finney e754ecf3aa microbit: Setting of default BLE transmission power level
Added configuration option in MicroBitConfig to allow the default power level
to be defined. Based on experiments undertaken by the BBC and subsequent policies defined by
the BBC's child protection panel, to is set by default it's lowest level (0),
in order to protect children's privacy.

It should be noted that this is 30dB below the device's default setting, so will
inevitably leave the device more susceptible to interference, and therefore
significantly reduce the reliability of the device. This will likely present
itself through increased packet loss, reduced thorughout, lost data and
connection drop outs. The normal BLE power level on commercial devices
translates to  power level of 6.

The author has witnessed an inability to connect to a micro:bit from a
distance of 40cm under domestic conditions using a Samsung S5 mini
running Android 4.4.2.
2016-01-16 17:29:46 +00:00
Joe Finney 07852b0cd6 microbit: Added support for BLE transmission power control
Added new method to MicroBitBLEManager to allow the radio transmission power
to be controlled. Provides a linear power level scale in the range 0..7.
2016-01-16 17:07:20 +00:00
Joe Finney f17151a0bf microbit: Added optional support for whitelisting and limited advertising
Added MicroBitConfig.h option to enable/disable whitelisting
Added MicroBitConfig.h option to enable/disable limited period advertising
Added support for limited period advertising
Minor code cleanups
2016-01-16 16:54:58 +00:00
James Devine b2484dcc97 microbit: patch for animation/scroll behaviour for images
See bbc/microbit-extras/issues/#1100 or lancaster-university/microbit-dal/issues/5 for full details.


A stride of 0 would lock the calling fiber indefinitely, which was not the expected behaviour
for many people.

This update fixes this issue in both animate and scroll, returning immediately when there is
a stride of zero.
2016-01-14 12:56:58 +00:00
Robert May 098a2378e3 Merge lancaster-university/master and silence warnings. 2016-01-13 20:51:49 +01:00
James Devine acf6bf76eb microbit: further fixes to #73
issue #73 highlighted an issue whereby the destruction of an instance
registered as an idle or system component, would result in a hardfault.

This was due to not deregistering idle or system callbacks.

This patch has been applied to all components currently in use by the
idle or system callbacks.
2016-01-13 16:25:34 +00:00
James Devine 1cc814da60 microbit: further formatting corrections
Corrected trailing whitespace, and tabulated lines with no content for:

* MicroBitCompass.h (.cpp)
* MicroBitDisplay.h (.cpp)
* MicroBitMessageBus.h (.cpp)
2016-01-13 16:16:18 +00:00
James Devine d8240cfb74 microbit: formatting changes to MicroBitAccelerometer.h (.cpp)
Removed trailing whitespace, and tabulated lines with no content.
2016-01-13 16:01:51 +00:00
James Devine a209857007 microbit: fix for #74
Replaced newer struct-style initialisation of CompassSample in MicroBit.cpp
with the more traditional constructor-style initialisation, to enfore
C98 compliance.

Closes #74
2016-01-13 15:12:13 +00:00
James Devine 7b7998e4eb microbit: formatting changes to MicroBit.h (.cpp)
Removed trailing whitespace, and tabulated lines with no content.
2016-01-13 15:04:29 +00:00
James Devine 8d3f3affbd microbit: fix for #73
The destructor for MicroBitButton didn't take into account the fact that
an instance can be destructed after registering a callback. This would then
result in a HardFault if an instance was destructed.

A clear repro case was registering a Touch input on a pin (abstracted as a button)
and swapping to a DigitalIn.
2016-01-13 14:15:50 +00:00
James Devine d2df88417b microbit: whitespace changes to MicroBitButton.h and .cpp
Cleared the files of trailing whitespace, and tabulated lines
2016-01-13 14:07:10 +00:00
Joe Finney 90cf35a38a WIP: compiling but untested first cut at whitelisting code 2016-01-13 00:20:37 +00:00
James Devine 983e9e1ad3 microbit: added Servo API to MicroBitPin
* Added a number of #defines to MicroBitPin.h which ultimately lead to retabulation of the #defines

* Added two new instance methods:

	- setServoValue() which sets the default servo period (20ms) and accepts a value in the range
			  0 - 180, which maps to a duty cycle between 5% and 10% respectively. This
			  mapping is also configurable.

	- setServoPulseUs() which accepts a pulse width in microseconds, and maps it onto the PWM
2016-01-12 20:54:09 +00:00
James Devine 2650aaa569 microbit: MicroBitPin api additions
Based on the previous commit, the api for MicroBitPin
has two new accessor methods:

	* getAnalogPeriodUs
	* getAnalogPeriod

There are also whitespace changes in the MicroBitPin head file
as well as some minor textual changes.
2016-01-12 20:52:50 +00:00
James Devine 32c40cdc29 microbit: DynamicPwm fixes and additions
This commit introduces an initial draft fix
for a bug where it would take two "setPeriods" for the
PWM class to update to the expected value.

This commit also adds three new methods to a DynamicPwm instance:

	* write() -  which is a lightweight wrapper around PwmOut write,
	  so that the value can be trapped. The trapping of the value
	  is necessary for the bug fix.
	* getPeriod() - which returns the value of a static variable,
	  containing the current period shared across the pwm bus.
	* getValue() - which returns a scaled value in the range 0-1024
	  representing the current duty cycle.
2016-01-12 20:50:42 +00:00
James Devine 500f851991 microbit: whitespace corrections
Corrected additional whitespace on some lines of source/MicroBitPin.cpp
2016-01-12 20:50:42 +00:00
James Devine 643f408ed2 microbit: modified the behaviour of pwm allocation
Dynamic pwm previously didn't round robin transient pwm channels,
and would therefore bias towards the first transient channel it found.
This update ensures that there is now an equal opportunity for all
channels to be considered for redirection.
2016-01-12 20:50:42 +00:00