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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_mtime.c] - Blame information for rev 74

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2
// # << NEORV32: neorv32_mtime.c - Machine System Timer (MTIME) HW Driver >>                       #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6 71 zero_gravi
// # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
7 2 zero_gravi
// #                                                                                               #
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 NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file neorv32_mtime.c
38
 * @brief Machine System Timer (MTIME) HW driver source file.
39
 *
40 44 zero_gravi
 * @note These functions should only be used if the MTIME unit was synthesized (IO_MTIME_EN = true).
41 2 zero_gravi
 **************************************************************************/
42
 
43
#include "neorv32.h"
44 51 zero_gravi
#include "neorv32_mtime.h"
45 2 zero_gravi
 
46
 
47
/**********************************************************************//**
48
 * Check if MTIME unit was synthesized.
49
 *
50
 * @return 0 if MTIME was not synthesized, 1 if MTIME is available.
51
 **************************************************************************/
52
int neorv32_mtime_available(void) {
53
 
54 64 zero_gravi
  if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_MTIME)) {
55 2 zero_gravi
    return 1;
56
  }
57
  else {
58
    return 0;
59
  }
60
}
61
 
62
 
63
/**********************************************************************//**
64 11 zero_gravi
 * Set current system time.
65 2 zero_gravi
 *
66
 * @note The MTIME timer increments with the primary processor clock.
67
 *
68 11 zero_gravi
 * @param[in] time New system time (uint64_t)
69
 **************************************************************************/
70
void neorv32_mtime_set_time(uint64_t time) {
71
 
72 12 zero_gravi
  union {
73
    uint64_t uint64;
74 71 zero_gravi
    uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
75 12 zero_gravi
  } cycles;
76
 
77
  cycles.uint64 = time;
78
 
79 64 zero_gravi
  NEORV32_MTIME.TIME_LO = 0;
80
  NEORV32_MTIME.TIME_HI = cycles.uint32[1];
81
  NEORV32_MTIME.TIME_LO = cycles.uint32[0];
82 12 zero_gravi
 
83 11 zero_gravi
}
84
 
85
 
86
/**********************************************************************//**
87
 * Get current system time.
88
 *
89
 * @note The MTIME timer increments with the primary processor clock.
90
 *
91 2 zero_gravi
 * @return Current system time (uint64_t)
92
 **************************************************************************/
93
uint64_t neorv32_mtime_get_time(void) {
94
 
95 12 zero_gravi
  union {
96
    uint64_t uint64;
97 71 zero_gravi
    uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
98 12 zero_gravi
  } cycles;
99
 
100
  uint32_t tmp1, tmp2, tmp3;
101
  while(1) {
102 64 zero_gravi
    tmp1 = NEORV32_MTIME.TIME_HI;
103
    tmp2 = NEORV32_MTIME.TIME_LO;
104
    tmp3 = NEORV32_MTIME.TIME_HI;
105 12 zero_gravi
    if (tmp1 == tmp3) {
106
      break;
107
    }
108
  }
109
 
110
  cycles.uint32[0] = tmp2;
111
  cycles.uint32[1] = tmp3;
112
 
113
  return cycles.uint64;
114 2 zero_gravi
}
115
 
116
 
117
/**********************************************************************//**
118
 * Set compare time register (MTIMECMP) for generating interrupts.
119
 *
120
 * @note The interrupt is triggered when MTIME >= MTIMECMP.
121 59 zero_gravi
 * @note Global interrupts and the timer interrupt source have to be enabled .
122 2 zero_gravi
 *
123
 * @param[in] timecmp System time for interrupt (uint64_t)
124
 **************************************************************************/
125
void neorv32_mtime_set_timecmp(uint64_t timecmp) {
126
 
127 59 zero_gravi
  union {
128
    uint64_t uint64;
129 71 zero_gravi
    uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
130 59 zero_gravi
  } cycles;
131
 
132
  cycles.uint64 = timecmp;
133
 
134 64 zero_gravi
  NEORV32_MTIME.TIMECMP_LO = -1; // prevent MTIMECMP from temporarily becoming smaller than the lesser of the old and new values
135
  NEORV32_MTIME.TIMECMP_HI = cycles.uint32[1];
136
  NEORV32_MTIME.TIMECMP_LO = cycles.uint32[0];
137 2 zero_gravi
}
138
 
139
 
140
/**********************************************************************//**
141
 * Get compare time register (MTIMECMP).
142
 *
143
 * @return Current MTIMECMP value.
144
 **************************************************************************/
145
uint64_t neorv32_mtime_get_timecmp(void) {
146
 
147 64 zero_gravi
  union {
148
    uint64_t uint64;
149 71 zero_gravi
    uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
150 64 zero_gravi
  } cycles;
151
 
152
  cycles.uint32[0] = NEORV32_MTIME.TIMECMP_LO;
153
  cycles.uint32[1] = NEORV32_MTIME.TIMECMP_HI;
154
 
155
  return cycles.uint64;
156 2 zero_gravi
}

powered by: WebSVN 2.1.0

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