]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/hw_gptm.c
switching to dedicated endian switching function
[arm-crypto-lib.git] / test_src / hw_gptm.c
1 /* hw_gptm.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdint.h>
21 #include "hw_regs.h"
22 #include "hw_gptm.h"
23
24 static const
25 uint32_t timer_base[] = { TIMER0_BASE, TIMER1_BASE, TIMER2_BASE, TIMER3_BASE };
26
27 uint32_t gptm_read_timer(uint8_t timerno, uint8_t b){
28         if(timerno>3){
29                 return 0;
30         }
31         return HW_REG(timer_base[timerno]+(b?GPTM_TBV_OFFSET:GPTM_TAV_OFFSET));
32 }
33
34 void gptm_write_timer(uint8_t timerno, uint8_t b, uint32_t value){
35         if(timerno>3){
36                 return;
37         }
38         HW_REG(timer_base[timerno]+(b?GPTM_TBV_OFFSET:GPTM_TAV_OFFSET)) = value;
39 }
40
41 void gptm_start_timer(uint8_t timerno, uint8_t b){
42         if(timerno>3){
43                 return;
44         }
45         HW_REG(timer_base[timerno]+GPTM_CTL_OFFSET) |= _BV(b?GPTM_TBEN:GPTM_TAEN);
46 }
47
48 void gptm_stop_timer(uint8_t timerno, uint8_t b){
49         if(timerno>3){
50                 return;
51         }
52         HW_REG(timer_base[timerno]+GPTM_CTL_OFFSET) &= ~_BV(b?GPTM_TBEN:GPTM_TAEN);
53 }
54
55 void gptm_set_timer_prescaler(uint8_t timerno, uint8_t b, uint8_t prescaler){
56         if(timerno>3){
57                 return;
58         }
59         HW_REG(timer_base[timerno]+(b?GPTM_TBPR_OFFSET:GPTM_TAPR_OFFSET)) =
60                         prescaler;
61 }
62
63 void gptm_set_timer_loadvalue(uint8_t timerno, uint8_t b, uint32_t value){
64         if(timerno>3){
65                 return;
66         }
67         HW_REG(timer_base[timerno]+(b?GPTM_TBILR_OFFSET:GPTM_TAILR_OFFSET)) =
68                         value;
69 }
70
71 void gptm_set_timer_mode(uint8_t timerno, uint8_t b, uint8_t mode){
72         if(timerno>3){
73                 return;
74         }
75         uint32_t tmp;
76         tmp = HW_REG(timer_base[timerno]+(b?GPTM_TBMR_OFFSET:GPTM_TAMR_OFFSET));
77         tmp &= ~3;
78         tmp |= mode;
79         HW_REG(timer_base[timerno]+(b?GPTM_TBMR_OFFSET:GPTM_TAMR_OFFSET)) = tmp;
80 }
81
82 uint8_t gptm_get_timer_running(uint8_t timerno, uint8_t b){
83         if(timerno>3){
84                 return 0;
85         }
86         return (HW_REG(timer_base[timerno]+GPTM_CTL_OFFSET)>>(b?GPTM_TBEN:GPTM_TAEN))&1;
87 }
88
89
90 void gptm_set_timer_32periodic(uint8_t timerno){
91         if(timerno>3){
92                 return;
93         }
94         volatile uint8_t i;
95         HW_REG(SYSCTL_BASE+RCGC1_OFFSET) |= _BV(16+timerno);
96         for(i=8; i>0; --i)
97                 ;
98         gptm_stop_timer(timerno, 0);
99         HW_REG(timer_base[timerno]+GPTM_CTL_OFFSET) = 0;
100         gptm_set_timer_mode(timerno, 0, GPTM_MODE_PERIODIC);
101         HW_REG(timer_base[timerno]+GPTM_TAMR_OFFSET) |= _BV(4);
102         gptm_set_timer_loadvalue(timerno, 0, 0xffffffff);
103 }
104
105