1 | /*
|
---|
2 | * TOPPERS/SSP Kernel
|
---|
3 | * Smallest Set Profile Kernel
|
---|
4 | *
|
---|
5 | * Copyright (C) 2013-2015 by Embedded and Real-Time Systems Laboratory
|
---|
6 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
7 | * Copyright (C) 2018,2019 by Naoki Saito
|
---|
8 | * Nagoya Municipal Industrial Research Institute, JAPAN
|
---|
9 | *
|
---|
10 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
11 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
12 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
13 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
14 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
15 | * スコード中に含まれていること.
|
---|
16 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
17 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
18 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
19 | * の無保証規定を掲載すること.
|
---|
20 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
21 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
22 | * と.
|
---|
23 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
24 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
25 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
26 | * 報告すること.
|
---|
27 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
28 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
29 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
30 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
31 | * 免責すること.
|
---|
32 | *
|
---|
33 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
34 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
35 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
36 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
37 | * の責任を負わない.
|
---|
38 | *
|
---|
39 | * $Id: core_timer.c 386 2019-04-17 03:13:00Z nmir-saito $
|
---|
40 | */
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * タイマドライバ(Generic Timer)
|
---|
44 | */
|
---|
45 | #include "kernel_impl.h"
|
---|
46 | #include "time_event.h"
|
---|
47 | #include "target_timer.h"
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * タイマの周波数(単位: kHz)
|
---|
51 | */
|
---|
52 | static uint32_t timer_clock;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * タイマの設定値(比較対象とする値)
|
---|
56 | */
|
---|
57 | static uint64_t timer_cval;
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * タイマの開始
|
---|
61 | */
|
---|
62 | Inline void
|
---|
63 | target_timer_start(void)
|
---|
64 | {
|
---|
65 | uint32_t cntp_ctl;
|
---|
66 |
|
---|
67 | cntp_ctl = clock_read_ctrl();
|
---|
68 | cntp_ctl |= ((uint32_t)CNTP_CTL_ENABLE);
|
---|
69 | clock_write_ctrl(cntp_ctl);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * タイマの停止
|
---|
74 | */
|
---|
75 | Inline void
|
---|
76 | target_timer_stop(void)
|
---|
77 | {
|
---|
78 | uint32_t cntp_ctl;
|
---|
79 | cntp_ctl = clock_read_ctrl();
|
---|
80 | cntp_ctl &= ~CNTP_CTL_ENABLE;
|
---|
81 | clock_write_ctrl(cntp_ctl);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * タイマの起動処理
|
---|
86 | */
|
---|
87 | void
|
---|
88 | target_timer_initialize(intptr_t exinf)
|
---|
89 | {
|
---|
90 | uint32_t cyc;
|
---|
91 | uint32_t timer_clock_hz;
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * タイマの停止
|
---|
95 | */
|
---|
96 | target_timer_stop();
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * プリスケーラ(=2^31 --> divider=1)
|
---|
100 | */
|
---|
101 | sil_wrw_mem((uint32_t *)(0x40000008), 0x80000000);
|
---|
102 |
|
---|
103 | timer_clock_hz = get_timer_freq(); // 19.2MHz
|
---|
104 | timer_clock = timer_clock_hz / 1000;
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * 1ティックのクロック数
|
---|
108 | */
|
---|
109 | cyc = (timer_clock * (TIC_NUME) / (TIC_DENO));
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * タイマの開始
|
---|
113 | */
|
---|
114 | clock_write_tval((uint32_t)cyc);
|
---|
115 | target_timer_start();
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * タイマの設定値
|
---|
119 | */
|
---|
120 | timer_cval = clock_read_cval();
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * タイマの停止処理
|
---|
125 | */
|
---|
126 | void
|
---|
127 | target_timer_terminate(intptr_t exinf)
|
---|
128 | {
|
---|
129 | /*
|
---|
130 | * タイマの停止
|
---|
131 | */
|
---|
132 | clock_write_ctrl((uint32_t)0);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * タイマ割込みハンドラ
|
---|
137 | */
|
---|
138 | void
|
---|
139 | target_timer_handler(void)
|
---|
140 | {
|
---|
141 | uint32_t cyc;
|
---|
142 | uint64_t pct;
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * タイマの停止
|
---|
146 | */
|
---|
147 | target_timer_stop();
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * 1ティックのクロック数
|
---|
151 | */
|
---|
152 | cyc = (timer_clock * (TIC_NUME) / (TIC_DENO));
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * タイマの設定値
|
---|
156 | */
|
---|
157 | timer_cval = timer_cval + (uint64_t)cyc;
|
---|
158 |
|
---|
159 |
|
---|
160 | // カウンタから読み出し
|
---|
161 | pct = clock_read_counter();
|
---|
162 |
|
---|
163 | while (timer_cval > pct + cyc) {
|
---|
164 | timer_cval = timer_cval - (uint64_t)cyc;
|
---|
165 | }
|
---|
166 | while (timer_cval < pct) {
|
---|
167 | timer_cval = timer_cval + (uint64_t)cyc;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * タイマの設定
|
---|
172 | */
|
---|
173 | clock_write_cval((uint64_t)timer_cval);
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * タイマの開始
|
---|
177 | */
|
---|
178 | target_timer_start();
|
---|
179 |
|
---|
180 | i_begin_int(INTNO_TIMER);
|
---|
181 | signal_time(); /* タイムティックの供給 */
|
---|
182 | i_end_int(INTNO_TIMER);
|
---|
183 | }
|
---|