Skip to content

Commit

Permalink
Merge pull request #12 from martinwork/firmware-codal
Browse files Browse the repository at this point in the history
Firmware modified to support CODAL
  • Loading branch information
JohnVidler authored Apr 21, 2022
2 parents 409ce86 + 38fc831 commit 0ab338c
Show file tree
Hide file tree
Showing 10 changed files with 20,935 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#################
# Project files #
#################
firmware/build
.build
projectfiles

Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "microbit-v2-samples"]
path = microbit-v2-samples
url = https://github.com/lancaster-university/microbit-v2-samples
ignore = dirty
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ BBC micro:bit.
However, if you'd like to extend or improve the firmware,
then building it will be the first step.

### Building the micro:bit V1 firmware from source

Building the firmware is done with Yotta, and instructions for setting up your
environment can be found at:

Expand All @@ -127,6 +129,38 @@ Drag this file to your micro:bit's USB drive to install it.

You can use the test suite to confirm that it works.

### Building the micro:bit V2 firmware from source

Building the firmware is done with the CODAL build system, and instructions for installing the pre-requisite tools can be found at:

[microbit-v2-samples](https://github.com/lancaster-university/microbit-v2-samples)

Once you have verified your build toolchain works by building the `microbit-v2-samples` example,
you can build the firmware as follows:

cd microbit-firmata
git submodule update --init
cd firmware
python buildv2.py

The compiled firmware will be at:

../microbit-v2-samples/MICROBIT.hex

Drag this file to your micro:bit's USB drive to install it.

You can use the test suite to confirm that it works.

### Creating a universal hex for micro:bit V1 and micro:bit V2

A tool which can easily create a .hex file that will support all micro:bit variants can be found at:

[micro:bit Universal Hex Creator](https://tech.microbit.org/software/universal-hex-creator/)

There is more information about the .HEX file format here:

[.HEX file format](https://tech.microbit.org/software/hex-format/)

### License

This software is under the MIT open source license.
Expand Down
31 changes: 27 additions & 4 deletions client/MBFirmataClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class MicrobitFirmataClient {
this.boardVersion = '';
this.firmataVersion = '';
this.firmwareVersion = '';
this.firmwareVersionNumber = 257; //1.1

this.buttonAPressed = false;
this.buttonBPressed = false;
Expand All @@ -58,6 +59,8 @@ class MicrobitFirmataClient {
// statistics:
this.analogUpdateCount = 0;
this.channelUpdateCounts = new Array(16).fill(0);

this.updateEventIDs();
}

addConstants() {
Expand Down Expand Up @@ -107,6 +110,23 @@ class MicrobitFirmataClient {
this.INPUT_PULLDOWN = 0x0F; // micro:bit extension; not defined by Firmata
}

updateEventIDs() {
// These IDs changed between firmware 1.0 (DAL <= 2.1.1) and 1.1 (DAL >= 2.2.0-rc6 and CODAL)
if ( this.firmwareVersionNumber >= 257) { // v1.1+
this.MICROBIT_ID_DISPLAY = 7;
this.MICROBIT_ID_GESTURE = 13;
this.MICROBIT_ID_IO_P0 = 100;
this.MICROBIT_ID_IO_P1 = 101;
this.MICROBIT_ID_IO_P2 = 102;
} else {
this.MICROBIT_ID_DISPLAY = 6;
this.MICROBIT_ID_GESTURE = 27;
this.MICROBIT_ID_IO_P0 = 7;
this.MICROBIT_ID_IO_P1 = 8;
this.MICROBIT_ID_IO_P2 = 9;
}
}

// Connecting/Disconnecting

connect() {
Expand All @@ -116,7 +136,7 @@ class MicrobitFirmataClient {
.then((ports) => {
for (var i = 0; i < ports.length; i++) {
var p = ports[i];
if ((p.vendorId == '0d28') && (p.productId == '0204')) {
if ((p.vendorId == '0d28' || p.vendorId == '0D28') && (p.productId == '0204')) {
return p.comName;
}
}
Expand Down Expand Up @@ -181,6 +201,8 @@ class MicrobitFirmataClient {
var id = usbSerialNumber.slice(0, 4);
if ('9900' == id) return '1.3';
if ('9901' == id) return '1.5';
if ('9903' == id) return '2.0';
if ('9904' == id) return '2.0';
return 'unrecognized board';
}

Expand Down Expand Up @@ -296,7 +318,9 @@ class MicrobitFirmataClient {
}
var firmwareName = new TextDecoder().decode(Buffer.from(utf8Bytes));
this.firmwareVersion = firmwareName + ' ' + major + '.' + minor;
}
this.firmwareVersionNumber = 256 * major + minor;
this.updateEventIDs();
}

receivedDigitalUpdate(chan, pinMask) {
var pinNum = 8 * chan;
Expand Down Expand Up @@ -324,7 +348,6 @@ class MicrobitFirmataClient {
const MICROBIT_BUTTON_EVT_DOWN = 1;
const MICROBIT_BUTTON_EVT_UP = 2;

const MICROBIT_ID_DISPLAY = 6;
const MICROBIT_DISPLAY_EVT_ANIMATION_COMPLETE = 1;

var sourceID =
Expand All @@ -344,7 +367,7 @@ class MicrobitFirmataClient {
if (eventID == MICROBIT_BUTTON_EVT_DOWN) this.buttonBPressed = true;
if (eventID == MICROBIT_BUTTON_EVT_UP) this.buttonBPressed = false;
}
if ((sourceID == MICROBIT_ID_DISPLAY) &&
if ((sourceID == this.MICROBIT_ID_DISPLAY) &&
(eventID == MICROBIT_DISPLAY_EVT_ANIMATION_COMPLETE)) {
this.isScrolling = false;
}
Expand Down
4 changes: 3 additions & 1 deletion client/mbTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class ConnectivityTest {
class Test2 {
constructor() {
console.log('String scroll test...');
mb.enableDisplay(true);
mb.scrollString('abc', 80);
}
step() {
Expand All @@ -151,6 +152,7 @@ class Test2 {
class Test3 {
constructor() {
console.log('Number scroll test...');
mb.enableDisplay(true);
mb.scrollInteger(-123, 80);
}
step() {
Expand Down Expand Up @@ -400,7 +402,7 @@ class Test8 {
return '';
}
gotEvent(sourceID, eventID) {
if (27 == sourceID) {
if (mb.MICROBIT_ID_GESTURE == sourceID) {
this.events[eventID]++;
this.lastEvent = eventID;
this.showGestureEvents();
Expand Down
90 changes: 90 additions & 0 deletions firmware/buildv2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python

# MIT License
#
# Copyright (c) 2019 Micro:bit Educational Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import stat
import shutil
import subprocess

################################################################
# FILE UTILS

def fileutils_rmtree(path):
if os.path.exists(path):
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
filename = os.path.join(root, name)
os.chmod(filename, stat.S_IWRITE)
os.remove(filename)
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(path)


def fileutils_remove(path):
if os.path.exists(path):
os.remove(path)


def fileutils_rename(src,dst):
if os.path.exists(src):
os.rename(src,dst)


def fileutils_copytree(src,dst):
if os.path.exists(src):
shutil.copytree(src,dst)

################################################################
# Build in sibling folder microbit-v2-samples:
# replace microbit-v2-samples/source with our source folder
# change to microbit-v2-samples and run build.py
# restore microbit-v2-samples/source

V2ROOT="../microbit-v2-samples"
V2SOURCE="../microbit-v2-samples/source"
V2NOTSOURCE="../microbit-v2-samples/notsource"

SOURCE="./source"
BACK="../firmware"

# rename default source folder out of the way

fileutils_rmtree(V2NOTSOURCE)
fileutils_rename(V2SOURCE, V2NOTSOURCE)

# copy our source folder into place

fileutils_copytree(SOURCE, V2SOURCE);

# change directory and run the build

os.chdir(V2ROOT)
subprocess.call(["python", "build.py"])
os.chdir(BACK)

#restore original source folder

fileutils_rmtree(V2SOURCE)
fileutils_rename(V2NOTSOURCE, V2SOURCE)
2 changes: 1 addition & 1 deletion firmware/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "The micro:bit runtime common abstraction with examples.",
"license": "MIT",
"dependencies": {
"microbit": "lancaster-university/microbit#v2.1.1"
"microbit": "lancaster-university/microbit#v2.2.0-rc6"
},
"targetDependencies": {},
"bin": "source"
Expand Down
Loading

0 comments on commit 0ab338c

Please sign in to comment.