Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others

Committer:
modtronix-com
Date:
Fri Aug 19 15:52:51 2016 +1000
Revision:
19:42ae82a8f571
Parent:
17:86034c970ea0
Added tag v1.1 for changeset 37e7c8fac8c7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix-com 7:709130701ac7 1 /**
modtronix-com 7:709130701ac7 2 * File: mx_timer.cpp
modtronix-com 7:709130701ac7 3 *
modtronix-com 7:709130701ac7 4 * Author: Modtronix Engineering - www.modtronix.com
modtronix-com 7:709130701ac7 5 *
modtronix-com 7:709130701ac7 6 * Description:
modtronix-com 7:709130701ac7 7 *
modtronix-com 7:709130701ac7 8 * Software License Agreement:
modtronix-com 7:709130701ac7 9 * This software has been written or modified by Modtronix Engineering. The code
modtronix-com 7:709130701ac7 10 * may be modified and can be used free of charge for commercial and non commercial
modtronix-com 7:709130701ac7 11 * applications. If this is modified software, any license conditions from original
modtronix-com 7:709130701ac7 12 * software also apply. Any redistribution must include reference to 'Modtronix
modtronix-com 7:709130701ac7 13 * Engineering' and web link(www.modtronix.com) in the file header.
modtronix-com 7:709130701ac7 14 *
modtronix-com 7:709130701ac7 15 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
modtronix-com 7:709130701ac7 16 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
modtronix-com 7:709130701ac7 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
modtronix-com 7:709130701ac7 18 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
modtronix-com 7:709130701ac7 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
modtronix-com 7:709130701ac7 20 */
modtronix-com 7:709130701ac7 21 #ifndef MODTRONIX_NZ32S_MX_TICK_H_
modtronix-com 7:709130701ac7 22 #define MODTRONIX_NZ32S_MX_TICK_H_
modtronix-com 7:709130701ac7 23
modtronix-com 17:86034c970ea0 24 #include "nz32s_default_config.h"
modtronix-com 17:86034c970ea0 25
modtronix-com 7:709130701ac7 26 #define MODTRONIX_NZ32S_MX_TICK_INC MxTick::increment()
modtronix-com 7:709130701ac7 27
modtronix-com 7:709130701ac7 28 /** A general purpose milli-second and second timer.
modtronix-com 7:709130701ac7 29 *
modtronix-com 7:709130701ac7 30 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 31 * to a maximum of 2^31-1 millseconds = 596 Hours = 24.8 Days
modtronix-com 7:709130701ac7 32 *
modtronix-com 7:709130701ac7 33 * Example:
modtronix-com 7:709130701ac7 34 * @code
modtronix-com 7:709130701ac7 35 * // Count the time to toggle a LED
modtronix-com 7:709130701ac7 36 *
modtronix-com 7:709130701ac7 37 * #include "mbed.h"
modtronix-com 13:328bfac0e686 38 * #include "mx_tick.h"
modtronix-com 7:709130701ac7 39 *
modtronix-com 7:709130701ac7 40 * MxTimer mxTmr;
modtronix-com 7:709130701ac7 41 * int tmrLED = 0;
modtronix-com 7:709130701ac7 42 * DigitalOut led(LED1);
modtronix-com 7:709130701ac7 43 *
modtronix-com 7:709130701ac7 44 int main() {
modtronix-com 7:709130701ac7 45
modtronix-com 7:709130701ac7 46 //Main loop
modtronix-com 7:709130701ac7 47 while(1) {
modtronix-com 7:709130701ac7 48 //Flash LED
modtronix-com 7:709130701ac7 49 if (mxTmr.read_ms() >= tmrLED) {
modtronix-com 7:709130701ac7 50 tmrLED += 1000; //Wait 1000mS before next LED toggle
modtronix-com 7:709130701ac7 51 led = !led;
modtronix-com 7:709130701ac7 52 }
modtronix-com 7:709130701ac7 53 }
modtronix-com 7:709130701ac7 54 }
modtronix-com 7:709130701ac7 55 * @endcode
modtronix-com 7:709130701ac7 56 */
modtronix-com 7:709130701ac7 57 class MxTick {
modtronix-com 7:709130701ac7 58
modtronix-com 7:709130701ac7 59 public:
modtronix-com 7:709130701ac7 60 MxTick(bool autoInc = true);
modtronix-com 7:709130701ac7 61
modtronix-com 7:709130701ac7 62 static inline void increment() {
modtronix-com 7:709130701ac7 63 tickMs++;
modtronix-com 7:709130701ac7 64 if (--countMs == 0) {
modtronix-com 7:709130701ac7 65 countMs = 1000;
modtronix-com 7:709130701ac7 66 tickSec++;
modtronix-com 7:709130701ac7 67 }
modtronix-com 7:709130701ac7 68 }
modtronix-com 7:709130701ac7 69
modtronix-com 7:709130701ac7 70
modtronix-com 7:709130701ac7 71 /** Get the current 32-bit milli-second tick value.
modtronix-com 7:709130701ac7 72 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 73 * to a maximum of 2^31-1 millseconds = 596 Hours = 24.8 Days
modtronix-com 7:709130701ac7 74 *
modtronix-com 7:709130701ac7 75 * This function is thread save!
modtronix-com 7:709130701ac7 76 *
modtronix-com 7:709130701ac7 77 * @return The tick value in milli-seconds
modtronix-com 7:709130701ac7 78 */
modtronix-com 7:709130701ac7 79 static inline int read_ms() {
modtronix-com 7:709130701ac7 80 return (int)tickMs;
modtronix-com 7:709130701ac7 81 }
modtronix-com 7:709130701ac7 82
modtronix-com 7:709130701ac7 83 /** Get the current 32-bit "10 milli-second" tick value. It is incremented each 10ms.
modtronix-com 7:709130701ac7 84 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 85 * to a maximum of 2^31-1 * 10 millseconds = 5960 Hours = 248 Days
modtronix-com 7:709130701ac7 86 *
modtronix-com 7:709130701ac7 87 * This function is NOT thread save. Takes multiple cycles to calculate value.
modtronix-com 7:709130701ac7 88 *
modtronix-com 7:709130701ac7 89 * Note this function takes MUCH longer than read_ms() and read_sec().
modtronix-com 7:709130701ac7 90 *
modtronix-com 7:709130701ac7 91 * @return The tick value in 10 x milli-seconds
modtronix-com 7:709130701ac7 92 */
modtronix-com 7:709130701ac7 93 static int read_ms10() {
modtronix-com 7:709130701ac7 94 //For example, if tickMs was 456789, we only want to use the 78 part.
modtronix-com 7:709130701ac7 95 //The 456 is seconds, and we get that from tickSec, so don't use it
modtronix-com 7:709130701ac7 96 //The 9 is 1xms, and don't need that
modtronix-com 7:709130701ac7 97 // 456789 / 10 = 45678
modtronix-com 7:709130701ac7 98 // 45678 % 100 = 89
modtronix-com 7:709130701ac7 99 return (int)( ((tickMs/10)%100) + (tickSec*100) );
modtronix-com 7:709130701ac7 100 }
modtronix-com 7:709130701ac7 101
modtronix-com 7:709130701ac7 102 /** Get the current 32-bit "100 milli-second" tick value. It is incremented each 100ms.
modtronix-com 7:709130701ac7 103 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 104 * to a maximum of 2^31-1 * 100 millseconds = 59600 Hours = 2480 Days = 6.79 Years
modtronix-com 7:709130701ac7 105 *
modtronix-com 7:709130701ac7 106 * This function is NOT thread save. Takes multiple cycles to calculate value.
modtronix-com 7:709130701ac7 107 *
modtronix-com 7:709130701ac7 108 * Note this function takes MUCH longer than read_ms() and read_sec().
modtronix-com 7:709130701ac7 109 *
modtronix-com 7:709130701ac7 110 * @return The tick value in 100 x milli-seconds
modtronix-com 7:709130701ac7 111 */
modtronix-com 7:709130701ac7 112 static int read_ms100() {
modtronix-com 7:709130701ac7 113 //For example, if tickMs was 456789, we only want to use the 7 part(100 x Ms).
modtronix-com 7:709130701ac7 114 //The 456 is seconds, and we get that from tickSec, so don't use it
modtronix-com 7:709130701ac7 115 //The 8 is 1xMs, the 7 is 10xMs, and we don't need them
modtronix-com 7:709130701ac7 116 // 456789 / 100 = 4567
modtronix-com 7:709130701ac7 117 // 45678 % 10 = 7
modtronix-com 7:709130701ac7 118 return (int)( ((tickMs/100)%10) + (tickSec*10) );
modtronix-com 7:709130701ac7 119 }
modtronix-com 7:709130701ac7 120
modtronix-com 7:709130701ac7 121 /** Get the current 32-bit second tick value.
modtronix-com 7:709130701ac7 122 * Because the read_sec() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 123 * to a maximum of 2^31-1 seconds = 596,523 Hours = 24855 Days = 68 Years
modtronix-com 7:709130701ac7 124 *
modtronix-com 7:709130701ac7 125 * This function is thread save!
modtronix-com 7:709130701ac7 126 *
modtronix-com 7:709130701ac7 127 * @return The tick value in seconds
modtronix-com 7:709130701ac7 128 */
modtronix-com 7:709130701ac7 129 static inline int read_sec() {
modtronix-com 7:709130701ac7 130 return (int)tickSec;
modtronix-com 7:709130701ac7 131 }
modtronix-com 7:709130701ac7 132
modtronix-com 7:709130701ac7 133 protected:
modtronix-com 7:709130701ac7 134 static uint16_t countMs;
modtronix-com 7:709130701ac7 135 static int32_t tickSec;
modtronix-com 7:709130701ac7 136 static int32_t tickMs;
modtronix-com 13:328bfac0e686 137 static bool running;
modtronix-com 7:709130701ac7 138 };
modtronix-com 7:709130701ac7 139
modtronix-com 7:709130701ac7 140
modtronix-com 7:709130701ac7 141
modtronix-com 7:709130701ac7 142 #endif /* MODTRONIX_NZ32S_MX_TICK_H_ */