-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimerTwo.h
More file actions
164 lines (137 loc) · 7.3 KB
/
Copy pathTimerTwo.h
File metadata and controls
164 lines (137 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/******************************************************************************************************************************************************
* COPYRIGHT
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* \verbatim
* Copyright (c) Andreas Burnickl All rights reserved.
*
* \endverbatim
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* FILE DESCRIPTION
* -------------------------------------------------------------------------------------------------------------------------------------------------*/
/** \file TimerTwo.h
* \brief Main header file of TimerTwo library
*
* \details Arduino library to use Timer two
*
*
*****************************************************************************************************************************************************/
#ifndef _TIMERTWO_H_
#define _TIMERTWO_H_
/******************************************************************************************************************************************************
* INCLUDES
*****************************************************************************************************************************************************/
#include "Arduino.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <StandardTypes.h>
/******************************************************************************************************************************************************
* LOCAL CONSTANT MACROS
*****************************************************************************************************************************************************/
/* Timer2 is 8 bit */
#define TIMERTWO_NUMBER_OF_BITS 8u
#define TIMERTWO_RESOLUTION (1uL << TIMERTWO_NUMBER_OF_BITS)
/* OC2A Chip Pin 17, Pin name PB3 */
#define TIMERTWO_A_ARDUINO_PIN 11u
//#define TIMERTWO_A_PORT_PIN PORTB3
/* OC2B Chip Pin 5, Pin name PD3 */
#define TIMERTWO_B_ARDUINO_PIN 3u
//#define TIMERTWO_A_PORT_PIN PORTD3
#define TIMERTWO_REG_CS_GP 0u
#define TIMERTWO_REG_CS_GM B111
#define TIMERTWO_MAX_PRESCALER 1024u
#if __cplusplus < 201103L
# define nullptr NULL
#endif
/******************************************************************************************************************************************************
* LOCAL FUNCTION MACROS
*****************************************************************************************************************************************************/
/******************************************************************************************************************************************************
* GLOBAL DATA TYPES AND STRUCTURES
*****************************************************************************************************************************************************/
/******************************************************************************************************************************************************
* CLASS TimerTwo
*****************************************************************************************************************************************************/
class TimerTwo
{
/******************************************************************************************************************************************************
* P U B L I C D A T A T Y P E S A N D S T R U C T U R E S
******************************************************************************************************************************************************/
public:
/* Timer ISR callback function */
typedef void (*TimerIsrCallbackF_void)(void);
#if F_CPU < 8000000uL
using TimeType = uint32_t;
#else
using TimeType = uint16_t;
#endif
/* Type which describes the internal state of the TimerTwo */
enum StateType {
STATE_INIT,
STATE_IDLE,
STATE_RUNNING,
STATE_STOPPED
};
/* Type which includes the values of the Clock Select Bit Group */
enum ClockSelectType {
REG_CS_NO_CLOCK,
REG_CS_NO_PRESCALER,
REG_CS_PRESCALE_8,
REG_CS_PRESCALE_32,
REG_CS_PRESCALE_64,
REG_CS_PRESCALE_128,
REG_CS_PRESCALE_256,
REG_CS_PRESCALE_1024
};
/*
Info: PWM for Pin 11 can not be used because in Mode 5 (PWM, Phase Correct)
OCRA is TOP value of the Timer/Counter. So Duty Cycle for OC2A Pin 11
can not be set, otherwise counter top value will be overwritten.
*/
/* Type which includes the Pwm Pins */
enum PwmPinType {
//PWM_PIN_11 = TIMERTWO_A_ARDUINO_PIN,
PWM_PIN_3 = TIMERTWO_B_ARDUINO_PIN
};
/******************************************************************************************************************************************************
* P R I V A T E D A T A A N D F U N C T I N O N S
******************************************************************************************************************************************************/
private:
TimerTwo();
~TimerTwo();
TimerTwo(const TimerTwo&);
TimerIsrCallbackF_void TimerIsrOverflowCallback;
StateType State;
ClockSelectType ClockSelectBitGroup;
// methods
byte getPrescaleShiftScale();
byte getTimerCycles(TimeType);
/******************************************************************************************************************************************************
* P U B L I C F U N C T I O N S
******************************************************************************************************************************************************/
public:
static TimerTwo& getInstance();
// get methods
StateType getState() const { return State; }
TimerIsrCallbackF_void getTimerIsrCallbackFunction() const { return TimerIsrOverflowCallback; }
TimeType getPeriodMax() { return TimeType{((TIMERTWO_RESOLUTION / (F_CPU / 1000000uL)) * TIMERTWO_MAX_PRESCALER * 2u) - 1u}; }
// set methods
// methods
StdReturnType init(TimeType = 1000uL, TimerIsrCallbackF_void = nullptr);
StdReturnType setPeriod(TimeType);
StdReturnType enablePwm(PwmPinType, byte);
StdReturnType disablePwm(PwmPinType);
StdReturnType setPwmDuty(PwmPinType, byte);
StdReturnType start();
void stop();
StdReturnType resume();
StdReturnType attachInterrupt(TimerIsrCallbackF_void);
void detachInterrupt();
StdReturnType read(TimeType&);
void callTimerIsrOverflowCallback() { TimerIsrOverflowCallback(); }
};
/* TimerTwo will be pre-instantiated in TimerTwo source file */
extern TimerTwo& Timer2;
#endif
/******************************************************************************************************************************************************
* E N D O F F I L E
*****************************************************************************************************************************************************/