updates
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal-eddystone by
Revision 31:87789e55bac7, committed 2016-07-13
- Comitter:
- LancasterUniversity
- Date:
- Wed Jul 13 12:18:10 2016 +0100
- Parent:
- 30:db87179335d5
- Child:
- 32:ece16b5987dd
- Commit message:
- Synchronized with git rev 3923dfff
Author: James Devine
microbit-dal: increase granularity of the system wide timer
Added an mbed Timer instance to maintain a microsecond level timestamp.
Added a function system_timer_current_time_us() which now returns the
time since power on in microseconds.
MicroBitEvents now call system_timer_current_time_us() giving
microsecond level timestamps.
Changed in this revision
--- a/inc/core/MicroBitSystemTimer.h Wed Jul 13 12:18:08 2016 +0100 +++ b/inc/core/MicroBitSystemTimer.h Wed Jul 13 12:18:10 2016 +0100 @@ -43,7 +43,7 @@ #include "MicroBitComponent.h" /** - * Initialises the system wide timer. + * Initialises a system wide timer, used to drive the various components used in the runtime. * * This must be called before any components register to receive periodic periodic callbacks. * @@ -70,11 +70,26 @@ int system_timer_get_period(); /** + * Updates the current time in microseconds, since power on. + * + * If the mbed Timer hasn't been initialised, it will be initialised + * on the first call to this function. + */ +inline void update_time(); + +/** * Determines the time since the device was powered on. * * @return the current time since power on in milliseconds */ -unsigned long system_timer_current_time(); +uint64_t system_timer_current_time(); + +/** + * Determines the time since the device was powered on. + * + * @return the current time since power on in microseconds + */ +uint64_t system_timer_current_time_us(); /** * Timer callback. Called from interrupt context, once per period. @@ -146,4 +161,4 @@ } }; -#endif +#endif \ No newline at end of file
--- a/inc/drivers/MicroBitStorage.h Wed Jul 13 12:18:08 2016 +0100 +++ b/inc/drivers/MicroBitStorage.h Wed Jul 13 12:18:10 2016 +0100 @@ -162,13 +162,9 @@ * * @param data a pointer to the beginning of the data to be persisted. * - * @param dataSize the size of the data to be persisted - * - * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large, - * MICROBIT_NO_RESOURCES if the storage page is full + * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if the storage page is full */ - int put(const char* key, uint8_t* data, int dataSize); - + int put(const char* key, uint8_t* data); /** * Places a given key, and it's corresponding value into flash at the earliest @@ -178,12 +174,9 @@ * * @param data a pointer to the beginning of the data to be persisted. * - * @param dataSize the size of the data to be persisted - * - * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large, - * MICROBIT_NO_RESOURCES if the storage page is full + * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if the storage page is full */ - int put(ManagedString key, uint8_t* data, int dataSize); + int put(ManagedString key, uint8_t* data); /** * Retreives a KeyValuePair identified by a given key.
--- a/inc/types/MicroBitEvent.h Wed Jul 13 12:18:08 2016 +0100 +++ b/inc/types/MicroBitEvent.h Wed Jul 13 12:18:10 2016 +0100 @@ -52,7 +52,7 @@ uint16_t source; // ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A. uint16_t value; // Component specific code indicating the cause of the event. - uint32_t timestamp; // Time at which the event was generated. ms since power on. + uint64_t timestamp; // Time at which the event was generated. us since power on. /** * Constructor. @@ -103,4 +103,4 @@ MicroBitEventQueueItem(MicroBitEvent evt); }; -#endif +#endif \ No newline at end of file
--- a/source/bluetooth/MicroBitBLEManager.cpp Wed Jul 13 12:18:08 2016 +0100 +++ b/source/bluetooth/MicroBitBLEManager.cpp Wed Jul 13 12:18:10 2016 +0100 @@ -99,7 +99,7 @@ if(memcmp(attribStore.sys_attrs[deviceID].sys_attr, attrib.sys_attr, len) != 0) { attribStore.sys_attrs[deviceID] = attrib; - manager->storage->put(key, (uint8_t *)&attribStore, sizeof(attribStore)); + manager->storage->put(key, (uint8_t *)&attribStore); } } }
--- a/source/core/MicroBitSystemTimer.cpp Wed Jul 13 12:18:08 2016 +0100 +++ b/source/core/MicroBitSystemTimer.cpp Wed Jul 13 12:18:10 2016 +0100 @@ -42,18 +42,21 @@ * Time since power on. Measured in milliseconds. * When stored as an unsigned long, this gives us approx 50 days between rollover, which is ample. :-) */ -static unsigned long ticks = 0; +static uint64_t time_us = 0; static unsigned int tick_period = 0; // Array of components which are iterated during a system tick static MicroBitComponent* systemTickComponents[MICROBIT_SYSTEM_COMPONENTS]; // Periodic callback interrupt -static Ticker *timer = NULL; +static Ticker *ticker = NULL; + +// System timer. +static Timer *timer = NULL; /** - * Initialises the system wide timer. + * Initialises a system wide timer, used to drive the various components used in the runtime. * * This must be called before any components register to receive periodic periodic callbacks. * @@ -63,8 +66,14 @@ */ int system_timer_init(int period) { + if (ticker == NULL) + ticker = new Ticker(); + if (timer == NULL) - timer = new Ticker(); + { + timer = new Timer(); + timer->start(); + } return system_timer_set_period(period); } @@ -83,11 +92,11 @@ // If a timer is already running, ensure it is disabled before reconfiguring. if (tick_period) - timer->detach(); + ticker->detach(); // register a period callback to drive the scheduler and any other registered components. tick_period = period; - timer->attach_us(system_timer_tick, period * 1000); + ticker->attach_us(system_timer_tick, period * 1000); return MICROBIT_OK; } @@ -103,13 +112,40 @@ } /** + * Updates the current time in microseconds, since power on. + * + * If the mbed Timer hasn't been initialised, it will be initialised + * on the first call to this function. + */ +void update_time() +{ + // If we haven't been initialized, bring up the timer with the default period. + if (timer == NULL || ticker == NULL) + system_timer_init(SYSTEM_TICK_PERIOD_MS); + + time_us += timer->read_us(); + timer->reset(); +} + +/** * Determines the time since the device was powered on. * * @return the current time since power on in milliseconds */ -unsigned long system_timer_current_time() +uint64_t system_timer_current_time() { - return ticks; + return system_timer_current_time_us() / 1000; +} + +/** + * Determines the time since the device was powered on. + * + * @return the current time since power on in microseconds + */ +uint64_t system_timer_current_time_us() +{ + update_time(); + return time_us; } /** @@ -120,8 +156,7 @@ */ void system_timer_tick() { - // increment our real-time counter. - ticks += system_timer_get_period(); + update_time(); // Update any components registered for a callback for(int i = 0; i < MICROBIT_SYSTEM_COMPONENTS; i++) @@ -144,7 +179,7 @@ int i = 0; // If we haven't been initialized, bring up the timer with the default period. - if (timer == NULL) + if (timer == NULL || ticker == NULL) system_timer_init(SYSTEM_TICK_PERIOD_MS); while(systemTickComponents[i] != NULL && i < MICROBIT_SYSTEM_COMPONENTS) @@ -178,4 +213,4 @@ systemTickComponents[i] = NULL; return MICROBIT_OK; -} +} \ No newline at end of file
--- a/source/drivers/MicroBitCompass.cpp Wed Jul 13 12:18:08 2016 +0100 +++ b/source/drivers/MicroBitCompass.cpp Wed Jul 13 12:18:10 2016 +0100 @@ -703,7 +703,7 @@ void MicroBitCompass::setCalibration(CompassSample calibration) { if(this->storage != NULL) - this->storage->put(ManagedString("compassCal"), (uint8_t *)&calibration, sizeof(CompassSample)); + this->storage->put(ManagedString("compassCal"), (uint8_t *)&calibration); average = calibration; status |= MICROBIT_COMPASS_STATUS_CALIBRATED;
--- a/source/drivers/MicroBitStorage.cpp Wed Jul 13 12:18:08 2016 +0100 +++ b/source/drivers/MicroBitStorage.cpp Wed Jul 13 12:18:10 2016 +0100 @@ -209,22 +209,14 @@ * * @param data a pointer to the beginning of the data to be persisted. * - * @param dataSize the size of the data to be persisted - * - * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large, - * MICROBIT_NO_RESOURCES if the storage page is full + * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if the storage page is full */ -int MicroBitStorage::put(const char *key, uint8_t *data, int dataSize) +int MicroBitStorage::put(const char *key, uint8_t *data) { KeyValuePair pair = KeyValuePair(); - int keySize = strlen(key) + 1; - - if(keySize > (int)sizeof(pair.key) || dataSize > (int)sizeof(pair.value) || dataSize < 0) - return MICROBIT_INVALID_PARAMETER; - - memcpy(pair.key, key, keySize); - memcpy(pair.value, data, dataSize); + memcpy(pair.key, key, min(sizeof(pair.key), strlen(key))); + memcpy(pair.value, data, sizeof(pair.value)); //calculate our various offsets. uint32_t pg_size = NRF_FICR->CODEPAGESIZE; @@ -298,14 +290,11 @@ * * @param data a pointer to the beginning of the data to be persisted. * - * @param dataSize the size of the data to be persisted - * - * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large, - * MICROBIT_NO_RESOURCES if the storage page is full + * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if the storage page is full */ -int MicroBitStorage::put(ManagedString key, uint8_t* data, int dataSize) +int MicroBitStorage::put(ManagedString key, uint8_t* data) { - return put((char *)key.toCharArray(), data, dataSize); + return put((char *)key.toCharArray(), data); } /**
--- a/source/drivers/MicroBitThermometer.cpp Wed Jul 13 12:18:08 2016 +0100 +++ b/source/drivers/MicroBitThermometer.cpp Wed Jul 13 12:18:10 2016 +0100 @@ -245,7 +245,7 @@ int MicroBitThermometer::setOffset(int offset) { if(this->storage != NULL) - this->storage->put(ManagedString("tempCal"), (uint8_t *)&offset, sizeof(int)); + this->storage->put(ManagedString("tempCal"), (uint8_t *)&offset); this->offset = offset;
--- a/source/types/MicroBitEvent.cpp Wed Jul 13 12:18:08 2016 +0100 +++ b/source/types/MicroBitEvent.cpp Wed Jul 13 12:18:10 2016 +0100 @@ -58,7 +58,7 @@ { this->source = source; this->value = value; - this->timestamp = system_timer_current_time(); + this->timestamp = system_timer_current_time_us(); if(mode != CREATE_ONLY) this->fire(); @@ -71,7 +71,7 @@ { this->source = 0; this->value = 0; - this->timestamp = system_timer_current_time(); + this->timestamp = system_timer_current_time_us(); } /** @@ -94,4 +94,4 @@ { this->evt = evt; this->next = NULL; -} +} \ No newline at end of file