Timer.h 2.42 KB
Newer Older
uepls's avatar
uepls committed
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
/*
 * Timer.h
 *
 *    Author: Max Zuidberg
 *     Email: m.zuidberg@icloud.com
 */

#ifndef TIMER_H_
#define TIMER_H_


/*
 * stdbool.h:               Boolean definitions for the C99 standard
 * stdint.h:                Variable definitions for the C99 standard
 * inc/hw_types.h:          Macros for hardware access, both direct and via the
 *                          bit-band region.
 * inc/hw_memmap.h:         Macros defining the memory map of the Tiva C Series
 *                          device. This includes defines such as peripheral
 *                          base address locations such as GPIO_PORTF_BASE.
 * inc/hw_ints.h:           Macros defining the interrupt assignments
 * driverlib/sysctl.h:      Defines and macros for the System Control API of
 *                          DriverLib. This includes API functions such as
 *                          SysCtlClockSet.
 * driverlib/interrupt.h:   Defines and macros for NVIC Controller (Interrupt)
 *                          API of driverLib. This includes API functions such
 *                          as IntEnable and IntPrioritySet.
 * driverlib/timer.h:       Defines and macros for Timer API of driverLib.
 *                          This includes API functions such as TimerConfigure.
 * System.h:                Access to current CPU clock and other functions.
 */
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/timer.h"
#include "System.h"


// Defines for direct register access
// Base address for the System Control Registers (Datasheet page 237)
#define SYS_CTL_BASE 0x400FE000

// Offset to add to SYS_CTL_BASE to access RCGCTIMER Register.
// (Datasheet page 338)
#define SYS_CTL_RCGCTIMER_OFFSET 0x604

// Actual RCGCTIMER Register
#define SYS_CTL_RCGCTIMER_REG HWREG(SYS_CTL_BASE + SYS_CTL_RCGCTIMER_OFFSET)


class Timer
{
public:
    Timer();
    virtual ~Timer();
    void init(System* sys, uint32_t base, void (*ISR)(void), uint32_t freq = 0);
    void start();
    void stop();
    void clearInterruptFlag();
    void setPeriodUS(uint32_t periodUS);
    void setFreq(uint32_t frequency);
    uint32_t getFreq();
    uint32_t getPeriodUS();

private:
    uint32_t timerBase;
    uint32_t timerLoadValue, timerPrescale, timerPeriodUS = 0, timerFreq = 0;
    uint_fast8_t timerNumber;
    System* timerSys;
};

#endif /* TIMER_H_ */