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

Committer:
modtronix-com
Date:
Thu Sep 10 20:09:01 2015 +1000
Revision:
7:709130701ac7
Child:
13:328bfac0e686
Added MxTick class, circular and command buffer bug fixes

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 7:709130701ac7 24 #define MODTRONIX_NZ32S_MX_TICK_INC MxTick::increment()
modtronix-com 7:709130701ac7 25
modtronix-com 7:709130701ac7 26 /** A general purpose milli-second and second timer.
modtronix-com 7:709130701ac7 27 *
modtronix-com 7:709130701ac7 28 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 29 * to a maximum of 2^31-1 millseconds = 596 Hours = 24.8 Days
modtronix-com 7:709130701ac7 30 *
modtronix-com 7:709130701ac7 31 * Example:
modtronix-com 7:709130701ac7 32 * @code
modtronix-com 7:709130701ac7 33 * // Count the time to toggle a LED
modtronix-com 7:709130701ac7 34 *
modtronix-com 7:709130701ac7 35 * #include "mbed.h"
modtronix-com 7:709130701ac7 36 *
modtronix-com 7:709130701ac7 37 * MxTimer mxTmr;
modtronix-com 7:709130701ac7 38 * int tmrLED = 0;
modtronix-com 7:709130701ac7 39 * DigitalOut led(LED1);
modtronix-com 7:709130701ac7 40 *
modtronix-com 7:709130701ac7 41 int main() {
modtronix-com 7:709130701ac7 42
modtronix-com 7:709130701ac7 43 //Main loop
modtronix-com 7:709130701ac7 44 while(1) {
modtronix-com 7:709130701ac7 45 //Flash LED
modtronix-com 7:709130701ac7 46 if (mxTmr.read_ms() >= tmrLED) {
modtronix-com 7:709130701ac7 47 tmrLED += 1000; //Wait 1000mS before next LED toggle
modtronix-com 7:709130701ac7 48 led = !led;
modtronix-com 7:709130701ac7 49 }
modtronix-com 7:709130701ac7 50 }
modtronix-com 7:709130701ac7 51 }
modtronix-com 7:709130701ac7 52 * @endcode
modtronix-com 7:709130701ac7 53 */
modtronix-com 7:709130701ac7 54 class MxTick {
modtronix-com 7:709130701ac7 55
modtronix-com 7:709130701ac7 56 public:
modtronix-com 7:709130701ac7 57 MxTick(bool autoInc = true);
modtronix-com 7:709130701ac7 58
modtronix-com 7:709130701ac7 59 static inline void increment() {
modtronix-com 7:709130701ac7 60 tickMs++;
modtronix-com 7:709130701ac7 61 if (--countMs == 0) {
modtronix-com 7:709130701ac7 62 countMs = 1000;
modtronix-com 7:709130701ac7 63 tickSec++;
modtronix-com 7:709130701ac7 64 }
modtronix-com 7:709130701ac7 65 }
modtronix-com 7:709130701ac7 66
modtronix-com 7:709130701ac7 67
modtronix-com 7:709130701ac7 68 /** Get the current 32-bit milli-second tick value.
modtronix-com 7:709130701ac7 69 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 70 * to a maximum of 2^31-1 millseconds = 596 Hours = 24.8 Days
modtronix-com 7:709130701ac7 71 *
modtronix-com 7:709130701ac7 72 * This function is thread save!
modtronix-com 7:709130701ac7 73 *
modtronix-com 7:709130701ac7 74 * @return The tick value in milli-seconds
modtronix-com 7:709130701ac7 75 */
modtronix-com 7:709130701ac7 76 static inline int read_ms() {
modtronix-com 7:709130701ac7 77 return (int)tickMs;
modtronix-com 7:709130701ac7 78 }
modtronix-com 7:709130701ac7 79
modtronix-com 7:709130701ac7 80 /** Get the current 32-bit "10 milli-second" tick value. It is incremented each 10ms.
modtronix-com 7:709130701ac7 81 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 82 * to a maximum of 2^31-1 * 10 millseconds = 5960 Hours = 248 Days
modtronix-com 7:709130701ac7 83 *
modtronix-com 7:709130701ac7 84 * This function is NOT thread save. Takes multiple cycles to calculate value.
modtronix-com 7:709130701ac7 85 *
modtronix-com 7:709130701ac7 86 * Note this function takes MUCH longer than read_ms() and read_sec().
modtronix-com 7:709130701ac7 87 *
modtronix-com 7:709130701ac7 88 * @return The tick value in 10 x milli-seconds
modtronix-com 7:709130701ac7 89 */
modtronix-com 7:709130701ac7 90 static int read_ms10() {
modtronix-com 7:709130701ac7 91 //For example, if tickMs was 456789, we only want to use the 78 part.
modtronix-com 7:709130701ac7 92 //The 456 is seconds, and we get that from tickSec, so don't use it
modtronix-com 7:709130701ac7 93 //The 9 is 1xms, and don't need that
modtronix-com 7:709130701ac7 94 // 456789 / 10 = 45678
modtronix-com 7:709130701ac7 95 // 45678 % 100 = 89
modtronix-com 7:709130701ac7 96 return (int)( ((tickMs/10)%100) + (tickSec*100) );
modtronix-com 7:709130701ac7 97 }
modtronix-com 7:709130701ac7 98
modtronix-com 7:709130701ac7 99 /** Get the current 32-bit "100 milli-second" tick value. It is incremented each 100ms.
modtronix-com 7:709130701ac7 100 * Because the read_ms() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 101 * to a maximum of 2^31-1 * 100 millseconds = 59600 Hours = 2480 Days = 6.79 Years
modtronix-com 7:709130701ac7 102 *
modtronix-com 7:709130701ac7 103 * This function is NOT thread save. Takes multiple cycles to calculate value.
modtronix-com 7:709130701ac7 104 *
modtronix-com 7:709130701ac7 105 * Note this function takes MUCH longer than read_ms() and read_sec().
modtronix-com 7:709130701ac7 106 *
modtronix-com 7:709130701ac7 107 * @return The tick value in 100 x milli-seconds
modtronix-com 7:709130701ac7 108 */
modtronix-com 7:709130701ac7 109 static int read_ms100() {
modtronix-com 7:709130701ac7 110 //For example, if tickMs was 456789, we only want to use the 7 part(100 x Ms).
modtronix-com 7:709130701ac7 111 //The 456 is seconds, and we get that from tickSec, so don't use it
modtronix-com 7:709130701ac7 112 //The 8 is 1xMs, the 7 is 10xMs, and we don't need them
modtronix-com 7:709130701ac7 113 // 456789 / 100 = 4567
modtronix-com 7:709130701ac7 114 // 45678 % 10 = 7
modtronix-com 7:709130701ac7 115 return (int)( ((tickMs/100)%10) + (tickSec*10) );
modtronix-com 7:709130701ac7 116 }
modtronix-com 7:709130701ac7 117
modtronix-com 7:709130701ac7 118 /** Get the current 32-bit second tick value.
modtronix-com 7:709130701ac7 119 * Because the read_sec() function return a 32 bit value, it can be used to time up
modtronix-com 7:709130701ac7 120 * to a maximum of 2^31-1 seconds = 596,523 Hours = 24855 Days = 68 Years
modtronix-com 7:709130701ac7 121 *
modtronix-com 7:709130701ac7 122 * This function is thread save!
modtronix-com 7:709130701ac7 123 *
modtronix-com 7:709130701ac7 124 * @return The tick value in seconds
modtronix-com 7:709130701ac7 125 */
modtronix-com 7:709130701ac7 126 static inline int read_sec() {
modtronix-com 7:709130701ac7 127 return (int)tickSec;
modtronix-com 7:709130701ac7 128 }
modtronix-com 7:709130701ac7 129
modtronix-com 7:709130701ac7 130 protected:
modtronix-com 7:709130701ac7 131 static uint16_t countMs;
modtronix-com 7:709130701ac7 132 static int32_t tickSec;
modtronix-com 7:709130701ac7 133 static int32_t tickMs;
modtronix-com 7:709130701ac7 134 };
modtronix-com 7:709130701ac7 135
modtronix-com 7:709130701ac7 136
modtronix-com 7:709130701ac7 137
modtronix-com 7:709130701ac7 138 #endif /* MODTRONIX_NZ32S_MX_TICK_H_ */