OpenCores
URL https://opencores.org/ocsvn/neo430/neo430/trunk

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [sw/] [lib/] [neo430/] [source/] [neo430_timer.c] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 zero_gravi
// #################################################################################################
2
// #  < neo430_timer.c - Timer helper functions >                                                  #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEO430 Processor - https://github.com/stnolting/neo430                                    #
33
// #################################################################################################
34
 
35
#include "neo430.h"
36
#include "neo430_timer.h"
37
 
38
 
39
/* ------------------------------------------------------------
40
 * INFO Activate Timer
41
 * ------------------------------------------------------------ */
42
void neo430_timer_enable(void) {
43
 
44
  TMR_CT |= (1<<TMR_CT_EN);
45
}
46
 
47
 
48
/* ------------------------------------------------------------
49
 * INFO Deactivate (and reset) Timer
50
 * ------------------------------------------------------------ */
51
void neo430_timer_disable(void) {
52
 
53
  TMR_CT = 0;
54
}
55
 
56
 
57
/* ------------------------------------------------------------
58
 * INFO Start Timer
59
 * ------------------------------------------------------------ */
60
void neo430_timer_run(void) {
61
 
62
  TMR_CT |= (1<<TMR_CT_RUN);
63
}
64
 
65
 
66
/* ------------------------------------------------------------
67
 * INFO Pause Timer
68
 * ------------------------------------------------------------ */
69
void neo430_timer_pause(void) {
70
 
71
  TMR_CT &= ~(1<<TMR_CT_RUN);
72
}
73
 
74
 
75
/* ------------------------------------------------------------
76
 * INFO Configure timer period
77
 * PARAM Timer frequency in Hz (1 ... F_CPU/2), uint16_t pointer to store computed THRES value
78
 * RETURN 0 if successful, 0xff if error
79
 * ------------------------------------------------------------ */
80
uint8_t neo430_timer_config_freq(uint32_t f_timer, uint16_t *thres) {
81
 
82
  uint32_t clock = CLOCKSPEED_32bit;
83
  uint32_t ticks = (clock / f_timer) >> 1; // divide by lowest prescaler (= f/2)
84
 
85
  uint8_t prsc = 0;
86
 
87
  if (ticks == 0)
88
    return 0xff; // frequency too high!
89
 
90
  // find prescaler
91
  while(prsc < 8) {
92
    if (ticks <= 0x0000ffff) {
93
      break;
94
    }
95
    else {
96
      if ((prsc == 2) || (prsc == 4)) {
97
        ticks >>= 3;
98
      }
99
      else {
100
        ticks >>= 1;
101
      }
102
      prsc++;
103
    }
104
  }
105
 
106
  TMR_THRES = (uint16_t)ticks;
107
  *thres = (uint16_t)ticks;
108
  TMR_CT &= ~(7<<TMR_CT_PRSC0); // clear prsc bits
109
  TMR_CT |= (prsc<<TMR_CT_PRSC0);
110
 
111
  return 0;
112
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.