Arduino functions millis, micros and eventually some more to come

Dependents:   DigitalCamera_OV5642_WIZwiki-W7500 BMC F746NG_TestAll Prelude_OV5642_dev

Arduino basic compatibility millis(), micros(), evtl. more to come.

micros() and millis() are uint32_t and have a correct overflow at 2^32-1. micros() has a granularity/resolution of 50. This can be changed by a define. 1 microsecond is not realistic because every count needs an irq. A resolution of 50 microseconds means 20KHz irq. That should not take away more than 1 percent overall processing time.

Committer:
eduardoG26
Date:
Mon Mar 30 13:50:18 2015 +0000
Revision:
3:abbc3308dfa1
Parent:
2:215810c8e89e
TICKER_PERIOD_US changed to 50

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eduardoG26 1:d1b960698e70 1 /*
eduardoG26 1:d1b960698e70 2 The MIT License (MIT)
eduardoG26 1:d1b960698e70 3
eduardoG26 1:d1b960698e70 4 Copyright (c) 2014 calima engineering
eduardoG26 1:d1b960698e70 5
eduardoG26 1:d1b960698e70 6 Permission is hereby granted, free of charge, to any person obtaining a copy
eduardoG26 1:d1b960698e70 7 of this software and associated documentation files (the "Software"), to deal
eduardoG26 1:d1b960698e70 8 in the Software without restriction, including without limitation the rights
eduardoG26 1:d1b960698e70 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
eduardoG26 1:d1b960698e70 10 copies of the Software, and to permit persons to whom the Software is
eduardoG26 1:d1b960698e70 11 furnished to do so, subject to the following conditions:
eduardoG26 1:d1b960698e70 12
eduardoG26 1:d1b960698e70 13 The above copyright notice and this permission notice shall be included in
eduardoG26 1:d1b960698e70 14 all copies or substantial portions of the Software.
eduardoG26 1:d1b960698e70 15
eduardoG26 1:d1b960698e70 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
eduardoG26 1:d1b960698e70 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
eduardoG26 1:d1b960698e70 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
eduardoG26 1:d1b960698e70 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
eduardoG26 1:d1b960698e70 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
eduardoG26 1:d1b960698e70 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
eduardoG26 1:d1b960698e70 22 THE SOFTWARE.
eduardoG26 1:d1b960698e70 23 */
eduardoG26 0:571ea8a1bbae 24 // millis as in Arduino...
eduardoG26 0:571ea8a1bbae 25
eduardoG26 0:571ea8a1bbae 26 #include "mbed.h"
eduardoG26 2:215810c8e89e 27 #include "Arduino.h"
eduardoG26 0:571ea8a1bbae 28
eduardoG26 1:d1b960698e70 29 #ifdef DEBUG_MILLIS
eduardoG26 1:d1b960698e70 30 #if DEBUG_MILLIS
eduardoG26 1:d1b960698e70 31 DigitalOut DebugMillisPin(D2);
eduardoG26 1:d1b960698e70 32 #endif
eduardoG26 1:d1b960698e70 33 #endif
eduardoG26 1:d1b960698e70 34
eduardoG26 2:215810c8e89e 35 // Period of IRQ in µs. This is the minimum (!) granularity of micros().
eduardoG26 3:abbc3308dfa1 36 #define TICKER_PERIOD_US (50uL)
eduardoG26 0:571ea8a1bbae 37
eduardoG26 2:215810c8e89e 38 static volatile uint32_t MilliSeconds_u32, MicroSeconds_u32, MicroSecondsLast_u32;
eduardoG26 0:571ea8a1bbae 39
eduardoG26 2:215810c8e89e 40 static Ticker ArduinoTicker;
eduardoG26 2:215810c8e89e 41
eduardoG26 2:215810c8e89e 42 static void TickerIRQ()
eduardoG26 0:571ea8a1bbae 43 {
eduardoG26 1:d1b960698e70 44 #ifdef DEBUG_MILLIS
eduardoG26 1:d1b960698e70 45 #if DEBUG_MILLIS
eduardoG26 1:d1b960698e70 46 DebugMillisPin = not DebugMillisPin;
eduardoG26 1:d1b960698e70 47 #endif
eduardoG26 1:d1b960698e70 48 #endif
eduardoG26 2:215810c8e89e 49 MicroSeconds_u32 += TICKER_PERIOD_US;
eduardoG26 2:215810c8e89e 50 if ((MicroSeconds_u32 - MicroSecondsLast_u32) >= 1000uL) {
eduardoG26 2:215810c8e89e 51 MilliSeconds_u32++;
eduardoG26 2:215810c8e89e 52 MicroSecondsLast_u32 += 1000uL;
eduardoG26 2:215810c8e89e 53 }
eduardoG26 0:571ea8a1bbae 54 }
eduardoG26 0:571ea8a1bbae 55
eduardoG26 0:571ea8a1bbae 56 uint32_t millis ()
eduardoG26 0:571ea8a1bbae 57 {
eduardoG26 2:215810c8e89e 58 // Not critical since variable is atomic
eduardoG26 0:571ea8a1bbae 59 return MilliSeconds_u32;
eduardoG26 0:571ea8a1bbae 60 }
eduardoG26 0:571ea8a1bbae 61
eduardoG26 2:215810c8e89e 62 uint32_t micros ()
eduardoG26 0:571ea8a1bbae 63 {
eduardoG26 2:215810c8e89e 64 // Not critical since variable is atomic
eduardoG26 2:215810c8e89e 65 return MicroSeconds_u32;
eduardoG26 2:215810c8e89e 66 }
eduardoG26 2:215810c8e89e 67
eduardoG26 2:215810c8e89e 68 void StartArduinoTimer ()
eduardoG26 2:215810c8e89e 69 {
eduardoG26 2:215810c8e89e 70 //arduino_timer.start();
eduardoG26 2:215810c8e89e 71 ArduinoTicker.attach_us (&TickerIRQ, TICKER_PERIOD_US);
eduardoG26 0:571ea8a1bbae 72 }
eduardoG26 0:571ea8a1bbae 73
eduardoG26 0:571ea8a1bbae 74 // End of file