root / proj / libs / peripherals / src / timer.c @ 283
History | View | Annotate | Download (2.95 KB)
1 | 146 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "timer.h" |
||
4 | 187 | up20180655 | #include "graph.h" |
5 | #include "sprite.h" |
||
6 | 146 | up20180655 | |
7 | 281 | up20180642 | #define TIMER_FREQ 1193182 /**< @brief clock frequency for timer in PC and AT */ |
8 | #define TIMER_MIN_FREQ (TIMER_FREQ/UINT16_MAX) + ((TIMER_FREQ % UINT16_MAX) ? 1 : 0) /**< @brief mininum frequency for timer */ |
||
9 | 146 | up20180655 | |
10 | 281 | up20180642 | /* I/O port addresses */
|
11 | |||
12 | #define TIMER_0 0x40 /**< @brief Timer 0 count register */ |
||
13 | #define TIMER_1 0x41 /**< @brief Timer 1 count register */ |
||
14 | #define TIMER_2 0x42 /**< @brief Timer 2 count register */ |
||
15 | #define TIMER_CTRL 0x43 /**< @brief Control register */ |
||
16 | |||
17 | #define SPEAKER_CTRL 0x61 /**< @brief Register for speaker control */ |
||
18 | |||
19 | /* Timer control */
|
||
20 | |||
21 | /* Timer selection: bits 7 and 6 */
|
||
22 | |||
23 | #define TIMER_SEL0 0x00 /**< @brief Control Word for Timer 0 */ |
||
24 | #define TIMER_SEL1 BIT(6) /**< @brief Control Word for Timer 1 */ |
||
25 | #define TIMER_SEL2 BIT(7) /**< @brief Control Word for Timer 2 */ |
||
26 | #define TIMER_RB_CMD (BIT(7) | BIT(6)) /**< @brief Read Back Command */ |
||
27 | |||
28 | /* Register selection: bits 5 and 4 */
|
||
29 | |||
30 | #define TIMER_LSB BIT(4) /**< @brief Initialize Counter LSB only */ |
||
31 | #define TIMER_MSB BIT(5) /**< @brief Initialize Counter MSB only */ |
||
32 | #define TIMER_LSB_MSB (TIMER_LSB | TIMER_MSB) /**< @brief Initialize LSB first and MSB afterwards */ |
||
33 | #define TIMER_INMODE_MASK 0x30 /**< @brief Mask for Timer Counter Mode */ |
||
34 | #define TIMER_INMODE_POS 4 /**< @brief Bit position of Timer Counter Mode */ |
||
35 | |||
36 | /* Operating mode: bits 3, 2 and 1 */
|
||
37 | |||
38 | #define TIMER_SQR_WAVE (BIT(2) | BIT(1)) /**< @brief Mode 3: square wave generator */ |
||
39 | #define TIMER_RATE_GEN BIT(2) /**< @brief Mode 2: rate generator */ |
||
40 | #define TIMER_MODE_MASK 0x0E /**< @brief Mask for mode */ |
||
41 | #define TIMER_MODE_POS 1 /**< @brief Position of smallest bit from mode */ |
||
42 | #define TIMER_MODE_2ALT 0x6 /**< @brief Alternative notation for mode 2 */ |
||
43 | #define TIMER_MODE_3ALT 0x7 /**< @brief Alternative notation for mode 3 */ |
||
44 | #define TIMER_MODE_RED2 0x03 /**< @brief Reduce 3-bit mode to 2-bit mode */ |
||
45 | |||
46 | /* Counting mode: bit 0 */
|
||
47 | |||
48 | #define TIMER_BCD 0x01 /**< @brief Count in BCD */ |
||
49 | #define TIMER_BIN 0x00 /**< @brief Count in binary */ |
||
50 | |||
51 | /* READ-BACK COMMAND FORMAT */
|
||
52 | |||
53 | #define TIMER_RB_COUNT_ BIT(5) /**< @brief Read counter value on read-back (0 to activate) */ |
||
54 | #define TIMER_RB_STATUS_ BIT(4) /**< @brief Read status value on read-back (0 to activate) */ |
||
55 | #define TIMER_RB_SEL(n) BIT((n) + 1) /**< @brief Select timer to read information from */ |
||
56 | |||
57 | 146 | up20180655 | int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
58 | if (interrupt_id == NULL) return 1; |
||
59 | *interrupt_id = interrupt_bit; |
||
60 | return (sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, interrupt_id));
|
||
61 | } |
||
62 | |||
63 | uint32_t no_interrupts = 0;
|
||
64 | void (timer_int_handler)() {
|
||
65 | no_interrupts++; |
||
66 | } |