Skip to content

Commit

Permalink
Added Partial Flashing Service (#9)
Browse files Browse the repository at this point in the history
* Memory Map + Partial Flash Service

* Moved Partial Flash Service to Management Mode

* PF Service appears in YT Build not PXT

* PF in PXT

* Swapped DFU and PF Services

* Fixing Flash Writew

* write flash via bluetooth

* Partial Flash -  Fixed Address

* Update module.json

* Adjust flash pointer

* Adjust flash start

* Adjust Flash Pointer

* Added offset to xfer

* Writing to FLASH_END

* Writing to FLASH_END

* Modified PFS read callback

* Reenable BLE Security

* removed pf

* Check diff

* Check diff

* Replaced ContextSwitch

* PF

* Test. Writing Hex File to 0x30000

* Writing to PXT empty space

* Reversed byte order

* Flash 16 bytes at once

* Copy data from static var to local var

* blocks -> bytes

* Fixed offset

* Fixed Offset

* Flash to 0x30000 + offset

* Bypass scratch page

* Changed byte order

* Write Without Response

* Without Fast BLE

* Added fast BLE

* Updated Connection Parameters

* Iterate through BLE data bytes

* Get Hashes From PXT Build

* Copy Hash from Flash

* Testing FLASH_END

* Read Hash From Flash

* Read Hash from Flash

* Reading correct hash from mem

* Endianess

* Modified MAGIC

* Fix Start Address

* Sequence #

* Write 0xdeadbeef to 0x36000 on PF Seq # error

* Group 4 packet blocks for writing

* Added error case

* Increased flash write size

* Blocks

* Fire event to write

* Decreased flash time

* Reformat

* Reduced hash size in MemoryMap

* Partial Flashing w/ Notifications

* One Characteristic - WRITE/WRITE_WOUT_RESP/NOTIFY

* Modified WRITE Notification

* Tidied - Moved end of transfer logic

* Removed VI Swap files

* Added Pairing Mode Event ID

* Modified Retransmit behaviour

* updateFlash caused stack overflow

* Removed flash write from memory map

* Instantiate Memory Map where used

* Word aligned. Added Status and Reset commands

* Fixed: storage. becomes storage->

* Fixed warnings: switch case fall through

* Fix Hash

* Rebuild Map everytime

* Hash Error

* Edited animation. Hash testing

* persistent memory map issues

* Moved EOT

* Modified BLE animation. Removed globals from PFS

* Updated MM

* Added incomplete flash flag

* Re added CCS.s
  • Loading branch information
microbit-sam authored Apr 16, 2018
1 parent 2cff906 commit 3413d54
Show file tree
Hide file tree
Showing 12 changed files with 877 additions and 26 deletions.
32 changes: 29 additions & 3 deletions inc/bluetooth/MicroBitBLEManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ DEALINGS IN THE SOFTWARE.
#include "MicroBitButtonService.h"
#include "MicroBitIOPinService.h"
#include "MicroBitTemperatureService.h"
#include "MicroBitPartialFlashingService.h"
#include "ExternalEvents.h"
#include "MicroBitButton.h"
#include "MicroBitStorage.h"
Expand All @@ -78,6 +79,9 @@ DEALINGS IN THE SOFTWARE.
#define MICROBIT_BLE_STATUS_STORE_SYSATTR 0x02
#define MICROBIT_BLE_STATUS_DISCONNECT 0x04

#define MICROBIT_BLE_MODE_PAIRING 0x00
#define MICROBIT_BLE_MODE_APPLICATION 0x01

extern const int8_t MICROBIT_BLE_POWER_LEVEL[];

struct BLESysAttribute
Expand Down Expand Up @@ -146,7 +150,7 @@ class MicroBitBLEManager : MicroBitComponent
* @param enableBonding If true, the security manager enabled bonding.
*
* @code
* bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true);
* bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus);
* @endcode
*/
void init(ManagedString deviceName, ManagedString serialNumber, EventModel &messageBus, bool enableBonding);
Expand Down Expand Up @@ -220,7 +224,7 @@ class MicroBitBLEManager : MicroBitComponent
void stopAdvertising();

/**
* A member function used to defer writes to flash, in order to prevent a write collision with
* A member function used to defer writes to flash, in order to prevent a write collision with
* softdevice.
* @param handle The handle offered by soft device during pairing.
* */
Expand Down Expand Up @@ -281,6 +285,19 @@ class MicroBitBLEManager : MicroBitComponent
int advertiseEddystoneUid(const char* uid_namespace, const char* uid_instance, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL);
#endif

/**
* Restarts in BLE Mode
*
*/
void restartInBLEMode();

/**
* Get current BLE mode; application, pairing
* #define MICROBIT_BLE_MODE_PAIRING 0x00
* #define MICROBIT_BLE_MODE_APPLICATION 0x01
*/
uint8_t getBLEMode();

private:
/**
* Displays the device's ID code as a histogram on the provided MicroBitDisplay instance.
Expand All @@ -289,12 +306,21 @@ class MicroBitBLEManager : MicroBitComponent
*/
void showNameHistogram(MicroBitDisplay &display);


/**
* Displays pairing mode animation
*
* @param display The display instance used for displaying the histogram.
*/
void showManagementModeAnimation(MicroBitDisplay &display);

#define MICROBIT_BLE_DISCONNECT_AFTER_PAIRING_DELAY 500
unsigned long pairing_completed_at_time;
unsigned long pairing_completed_at_time;

int pairingStatus;
ManagedString passKey;
ManagedString deviceName;
uint8_t bleMode = MICROBIT_BLE_MODE_APPLICATION;
};

#endif
106 changes: 106 additions & 0 deletions inc/bluetooth/MicroBitPartialFlashingService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
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.
*/

#ifndef MICROBIT_PARTIAL_FLASH_SERVICE_H
#define MICROBIT_PARTIAL_FLASH_SERVICE_H

#include "MicroBitConfig.h"
#include "MicroBitBLEManager.h"
#include "ble/BLE.h"
#include "MicroBitMemoryMap.h"

#include "MicroBitFlash.h"
#include "MicroBitStorage.h"

#include "MicroBitComponent.h"
#include "MicroBitEvent.h"
#include "MicroBitListener.h"
#include "EventModel.h"

#define PARTIAL_FLASHING_VERSION 0x01

// UUIDs for our service and characteristics
extern const uint8_t MicroBitPartialFlashingServiceUUID[];
extern const uint8_t MicroBitPartialFlashingServiceCharacteristicUUID[];

/**
* Class definition for the custom MicroBit Partial Flash Service.
* Provides a BLE service to remotely read the memory map and flash the PXT program.
*/
class MicroBitPartialFlashingService
{
public:
/**
* Constructor.
* Create a representation of the Partial Flash Service
* @param _ble The instance of a BLE device that we're running on.
* @param _memoryMap An instance of MicroBiteMemoryMap to interface with.
*/
MicroBitPartialFlashingService(BLEDevice &_ble, EventModel &_messageBus);

/**
* Callback. Invoked when any of our attributes are written via BLE.
*/
void onDataWritten(const GattWriteCallbackParams *params);

/**
* Callback. Invoked when any of our attributes are read via BLE.
*/
void onDataRead(GattReadAuthCallbackParams *params);

private:
// M:B Bluetooth stack and MessageBus
BLEDevice &ble;
EventModel &messageBus;

/**
* Writing to flash inside MicroBitEvent rather than in the ISR
*/
void partialFlashingEvent(MicroBitEvent e);

// The base address to write to. Bit masked: (0xFFFF0000 & region.endAddress) >> 16
uint8_t baseAddress = 0x3;

// Handles to access each characteristic when they are held by Soft Device.
GattAttribute::Handle_t partialFlashCharacteristicHandle;

/**
* Process a Partial Flashing data packet
*/
void flashData(uint8_t *data);

// Ensure packets are in order
uint8_t packetCount = 0;
uint8_t blockPacketCount = 0;

// Keep track of blocks of data
uint32_t block[16];
uint8_t blockNum = 0;
uint16_t offset = 0;

};


#endif
13 changes: 8 additions & 5 deletions inc/core/MicroBitComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ DEALINGS IN THE SOFTWARE.
#define MICROBIT_ID_MULTIBUTTON_ATTACH 31
#define MICROBIT_ID_SERIAL 32

#define MICROBIT_ID_PFLASH_NOTIFICATION 33
#define MICROBIT_ID_PAIRING_MODE 34

#define MICROBIT_ID_MESSAGE_BUS_LISTENER 1021 // Message bus indication that a handler for a given ID has been registered.
#define MICROBIT_ID_NOTIFY_ONE 1022 // Notfication channel, for general purpose synchronisation
#define MICROBIT_ID_NOTIFY 1023 // Notfication channel, for general purpose synchronisation
Expand All @@ -81,17 +84,17 @@ DEALINGS IN THE SOFTWARE.
*
* All components should inherit from this class.
*
* If a component requires regular updates, then that component can be added to the
* If a component requires regular updates, then that component can be added to the
* to the systemTick and/or idleTick queues. This provides a simple, extensible mechanism
* for code that requires periodic/occasional background processing but does not warrant
* the complexity of maintaining its own thread.
* the complexity of maintaining its own thread.
*
* Two levels of support are available.
* Two levels of support are available.
*
* systemTick() provides a periodic callback during the
* micro:bit's system timer interrupt. This provides a guaranteed periodic callback, but in interrupt context
* and is suitable for code with lightweight processing requirements, but strict time constraints.
*
*
* idleTick() provides a periodic callback whenever the scheduler is idle. This provides occasional, callbacks
* in the main thread context, but with no guarantees of frequency. This is suitable for non-urgent background tasks.
*
Expand Down Expand Up @@ -128,7 +131,7 @@ class MicroBitComponent

/**
* The idle thread will call this member function once the component has been added to the array
* of idle components using fiber_add_idle_component.
* of idle components using fiber_add_idle_component.
*/
virtual void idleTick()
{
Expand Down
18 changes: 12 additions & 6 deletions inc/core/MicroBitConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,21 @@ DEALINGS IN THE SOFTWARE.

// Defines where in memory persistent data is stored.
#ifndef KEY_VALUE_STORE_PAGE
#define KEY_VALUE_STORE_PAGE (PAGE_SIZE * (NRF_FICR->CODESIZE - 17))
#define KEY_VALUE_STORE_PAGE (PAGE_SIZE * (NRF_FICR->CODESIZE - 17))
#endif

#ifndef BLE_BOND_DATA_PAGE
#ifndef BLE_BOND_DATA_PAGE
#define BLE_BOND_DATA_PAGE (PAGE_SIZE * (NRF_FICR->CODESIZE - 18))
#endif

#ifndef MEMORY_MAP_PAGE
#define MEMORY_MAP_PAGE (PAGE_SIZE * (NRF_FICR->CODESIZE - 19))
#endif

// Scratch moved from page 19 to page 20
// MicroBitFileSystem uses DEFAULT_SCRATCH_PAGE to mark end of FileSystem
#ifndef DEFAULT_SCRATCH_PAGE
#define DEFAULT_SCRATCH_PAGE (PAGE_SIZE * (NRF_FICR->CODESIZE - 19))
#define DEFAULT_SCRATCH_PAGE (PAGE_SIZE * (NRF_FICR->CODESIZE - 20))
#endif

// Address of the end of the current program in FLASH memory.
Expand Down Expand Up @@ -130,7 +136,7 @@ extern uint32_t __etext;
// For standard S110 builds, this should be word aligned and in the range 0x300 - 0x700.
// Any unused memory will be automatically reclaimed as HEAP memory if both MICROBIT_HEAP_REUSE_SD and MICROBIT_HEAP_ALLOCATOR are enabled.
#ifndef MICROBIT_SD_GATT_TABLE_SIZE
#define MICROBIT_SD_GATT_TABLE_SIZE 0x300
#define MICROBIT_SD_GATT_TABLE_SIZE 0x400
#endif

//
Expand Down Expand Up @@ -372,7 +378,7 @@ extern uint32_t __etext;
// Defines the logical block size for the file system.
// Must be a factor of the physical PAGE_SIZE (ideally a power of two less).
//
#ifndef MBFS_BLOCK_SIZE
#ifndef MBFS_BLOCK_SIZE
#define MBFS_BLOCK_SIZE 256
#endif

Expand All @@ -382,7 +388,7 @@ extern uint32_t __etext;
// Should be <= MBFS_BLOCK_SIZE.
//
#ifndef MBFS_CACHE_SIZE
#define MBFS_CACHE_SIZE 0
#define MBFS_CACHE_SIZE 0
#endif

//
Expand Down
2 changes: 1 addition & 1 deletion inc/drivers/MicroBitFlash.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MicroBitFlash
* Erase an entire page.
* @param page_address address of first word of page.
*/
void erase_page(uint32_t* page_address);
uint8_t erase_page(uint32_t* page_address);

/**
* Write to flash memory, assuming that a write is valid
Expand Down
Loading

0 comments on commit 3413d54

Please sign in to comment.