1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2007 by GJ Business Division RICOH COMPANY,LTD. JAPAN
|
---|
7 | * Copyright (C) 2005-2013 by Embedded and Real-Time Systems Laboratory
|
---|
8 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
9 | * Copyright (C) 2015 by Naoki Saito
|
---|
10 | * Nagoya Municipal Industrial Research Institute, JAPAN
|
---|
11 | *
|
---|
12 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
13 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
14 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
15 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
16 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
17 | * スコード中に含まれていること.
|
---|
18 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
19 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
20 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
21 | * の無保証規定を掲載すること.
|
---|
22 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
23 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
24 | * と.
|
---|
25 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
26 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
27 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
28 | * 報告すること.
|
---|
29 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
30 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
31 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
32 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
33 | * 免責すること.
|
---|
34 | *
|
---|
35 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
36 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
37 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
38 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
39 | * の責任を負わない.
|
---|
40 | *
|
---|
41 | * @(#) $Id: target_serial.c 619 2014-03-18 06:10:27Z nmir-saito $
|
---|
42 | */
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * シリアルI/Oデバイス(SIO)ドライバ(AT91SKYEYE用)
|
---|
46 | */
|
---|
47 |
|
---|
48 | #include <kernel.h>
|
---|
49 | #include <t_syslog.h>
|
---|
50 | #include "at91skyeye.h"
|
---|
51 | #include "target_serial.h"
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * シリアルI/Oポート初期化ブロックの定義
|
---|
55 | */
|
---|
56 | typedef struct sio_port_initialization_block
|
---|
57 | {
|
---|
58 | void* us_thr;
|
---|
59 | void* us_rpr;
|
---|
60 | void* us_rcr;
|
---|
61 | void* us_csr;
|
---|
62 | }
|
---|
63 | SIOPINIB;
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * シリアルI/Oポート管理ブロックの定義
|
---|
67 | */
|
---|
68 | struct sio_port_control_block
|
---|
69 | {
|
---|
70 | const SIOPINIB *p_siopinib; /* シリアルI/Oポート初期化ブロック */
|
---|
71 | intptr_t exinf; /* 拡張情報 */
|
---|
72 | bool_t openflag; /* オープン済みフラグ */
|
---|
73 | bool_t sendflag; /* 送信割込みイネーブルフラグ */
|
---|
74 | bool_t getready; /* 文字を受信した状態 */
|
---|
75 | bool_t putready; /* 文字を送信できる状態 */
|
---|
76 | };
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * シリアルI/Oポート初期化ブロック
|
---|
80 | */
|
---|
81 | const SIOPINIB siopinib_table[TNUM_SIOP] = {
|
---|
82 | {(void*)(USART0_THR),
|
---|
83 | (void*)(USART0_RPR),
|
---|
84 | (void*)(USART0_RCR),
|
---|
85 | (void*)(USART0_CSR)
|
---|
86 | }
|
---|
87 | };
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * シリアルI/Oポート管理ブロックのエリア
|
---|
91 | */
|
---|
92 | SIOPCB siopcb_table[TNUM_SIOP];
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * シリアルI/OポートIDから管理ブロックを取り出すためのマクロ
|
---|
96 | */
|
---|
97 | #define INDEX_SIOP(siopid) ((uint_t)((siopid) - 1))
|
---|
98 | #define get_siopcb(siopid) (&(siopcb_table[INDEX_SIOP(siopid)]))
|
---|
99 |
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * 受信用バッファ
|
---|
103 | */
|
---|
104 | static uint8_t usart_rev_buf;
|
---|
105 |
|
---|
106 | void
|
---|
107 | at91skyeye_init_uart(void)
|
---|
108 | {
|
---|
109 | /* 受信データの格納先アドレスの設定 */
|
---|
110 | sil_wrw_mem((void *)(USART0_RPR), (uint32_t)(&usart_rev_buf));
|
---|
111 | sil_wrw_mem((void *)(USART0_RCR), 0x0001);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * 文字を受信したか?
|
---|
116 | */
|
---|
117 | Inline bool_t
|
---|
118 | uart_getready(SIOPCB *p_siopcb)
|
---|
119 | {
|
---|
120 | return(sil_rew_mem((void*)(p_siopcb->p_siopinib->us_rcr)) == 0);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * 文字を送信できるか?
|
---|
125 | */
|
---|
126 | Inline bool_t
|
---|
127 | uart_putready(SIOPCB *p_siopcb)
|
---|
128 | {
|
---|
129 | return(true);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * 受信した文字の取り出し
|
---|
134 | */
|
---|
135 | Inline uint8_t
|
---|
136 | uart_getchar(SIOPCB *p_siopcb)
|
---|
137 | {
|
---|
138 | char c;
|
---|
139 |
|
---|
140 | c = usart_rev_buf;
|
---|
141 | sil_wrw_mem((void *)(p_siopcb->p_siopinib->us_rpr), (uint32_t)(&usart_rev_buf));
|
---|
142 | sil_wrw_mem((void *)(p_siopcb->p_siopinib->us_rcr), 0x0001);
|
---|
143 |
|
---|
144 | return(c);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * 送信する文字の書き込み
|
---|
149 | */
|
---|
150 | Inline void
|
---|
151 | uart_putchar(SIOPCB *p_siopcb, uint8_t c)
|
---|
152 | {
|
---|
153 | sil_wrw_mem((void*)(p_siopcb->p_siopinib->us_thr),c);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * 送信割込み許可
|
---|
158 | */
|
---|
159 | Inline void
|
---|
160 | uart_enable_send(SIOPCB *p_siopcb)
|
---|
161 | {
|
---|
162 | }
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * 送信割込み禁止
|
---|
166 | */
|
---|
167 | Inline void
|
---|
168 | uart_disable_send(SIOPCB *p_siopcb)
|
---|
169 | {
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * 受信割込み許可
|
---|
176 | */
|
---|
177 | Inline void
|
---|
178 | uart_enable_rcv(SIOPCB *p_siopcb)
|
---|
179 | {
|
---|
180 | sil_wrw_mem((void*)(USART0_IER),0x1);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * 受信割込み禁止
|
---|
185 | */
|
---|
186 | Inline void
|
---|
187 | uart_disable_rcv(SIOPCB *p_siopcb)
|
---|
188 | {
|
---|
189 | sil_wrw_mem((void*)(USART0_IDR),0x1);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * SIOドライバの初期化
|
---|
195 | */
|
---|
196 | void
|
---|
197 | sio_initialize(intptr_t exinf)
|
---|
198 | {
|
---|
199 | SIOPCB *p_siopcb;
|
---|
200 | uint_t i;
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * シリアルI/Oポート管理ブロックの初期化
|
---|
204 | */
|
---|
205 | for (p_siopcb = siopcb_table, i = 0; i < TNUM_SIOP; p_siopcb++, i++) {
|
---|
206 | p_siopcb->p_siopinib = &(siopinib_table[i]);
|
---|
207 | p_siopcb->openflag = false;
|
---|
208 | p_siopcb->sendflag = false;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * シリアルI/Oポートのオープン
|
---|
214 | */
|
---|
215 | SIOPCB *
|
---|
216 | at91skyeye_uart_opn_por(SIOPCB *p_siopcb, intptr_t exinf)
|
---|
217 | {
|
---|
218 | p_siopcb->exinf = exinf;
|
---|
219 | p_siopcb->getready = p_siopcb->putready = false;
|
---|
220 | p_siopcb->openflag = true;
|
---|
221 |
|
---|
222 | return(p_siopcb);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * シリアルI/Oポートのオープン
|
---|
228 | */
|
---|
229 | SIOPCB *
|
---|
230 | sio_opn_por(ID siopid, intptr_t exinf)
|
---|
231 | {
|
---|
232 | SIOPCB *p_siopcb = get_siopcb(siopid);
|
---|
233 | bool_t opnflg;
|
---|
234 | ER ercd;
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * オープンしたポートがあるかをopnflgに読んでおく.
|
---|
238 | */
|
---|
239 | opnflg = p_siopcb->openflag;
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * デバイス依存のオープン処理.
|
---|
243 | */
|
---|
244 | at91skyeye_uart_opn_por(p_siopcb, exinf);
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * シリアルI/O割込みのマスクを解除する.
|
---|
248 | */
|
---|
249 | if (!opnflg) {
|
---|
250 | ercd = ena_int(INTNO_SIO);
|
---|
251 | assert(ercd == E_OK);
|
---|
252 | }
|
---|
253 | return(p_siopcb);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * シリアルI/Oポートのクローズ
|
---|
258 | */
|
---|
259 | void
|
---|
260 | sio_cls_por(SIOPCB *p_siopcb)
|
---|
261 | {
|
---|
262 | /*
|
---|
263 | * デバイス依存のクローズ処理.
|
---|
264 | */
|
---|
265 | p_siopcb->openflag = false;
|
---|
266 |
|
---|
267 | /*
|
---|
268 | * シリアルI/O割込みをマスクする.
|
---|
269 | */
|
---|
270 | dis_int(INTNO_SIO);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*
|
---|
274 | * SIOの割込みハンドラ
|
---|
275 | */
|
---|
276 | void
|
---|
277 | sio_handler0(void)
|
---|
278 | {
|
---|
279 | SIOPCB *p_siopcb = &(siopcb_table[0]);
|
---|
280 |
|
---|
281 | if (uart_getready(p_siopcb)) {
|
---|
282 | /*
|
---|
283 | * 受信通知コールバックルーチンを呼び出す.
|
---|
284 | */
|
---|
285 | sio_irdy_rcv(p_siopcb->exinf);
|
---|
286 | }
|
---|
287 | if (uart_putready(p_siopcb)) {
|
---|
288 | /*
|
---|
289 | * 送信可能コールバックルーチンを呼び出す.
|
---|
290 | */
|
---|
291 | sio_irdy_snd(p_siopcb->exinf);
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | void
|
---|
297 | sio_handler1(void)
|
---|
298 | {
|
---|
299 | SIOPCB *p_siopcb = &(siopcb_table[0]);
|
---|
300 |
|
---|
301 | if (uart_getready(p_siopcb)) {
|
---|
302 | /*
|
---|
303 | * 受信通知コールバックルーチンを呼び出す.
|
---|
304 | */
|
---|
305 | sio_irdy_rcv(p_siopcb->exinf);
|
---|
306 | }
|
---|
307 | if (uart_putready(p_siopcb)) {
|
---|
308 | /*
|
---|
309 | * 送信可能コールバックルーチンを呼び出す.
|
---|
310 | */
|
---|
311 | sio_irdy_snd(p_siopcb->exinf);
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * シリアルI/Oポートへの文字送信
|
---|
319 | */
|
---|
320 | bool_t
|
---|
321 | sio_snd_chr(SIOPCB *siopcb, char c)
|
---|
322 | {
|
---|
323 | if (uart_putready(siopcb)){
|
---|
324 | uart_putchar(siopcb, c);
|
---|
325 | return(true);
|
---|
326 | }
|
---|
327 | return(false);
|
---|
328 | }
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * シリアルI/Oポートからの文字受信
|
---|
332 | */
|
---|
333 | int_t
|
---|
334 | sio_rcv_chr(SIOPCB *siopcb)
|
---|
335 | {
|
---|
336 | if (uart_getready(siopcb)) {
|
---|
337 | return((int_t)(uint8_t) uart_getchar(siopcb));
|
---|
338 | }
|
---|
339 | return(-1);
|
---|
340 | }
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * シリアルI/Oポートからのコールバックの許可
|
---|
344 | */
|
---|
345 | void
|
---|
346 | sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
347 | {
|
---|
348 | switch (cbrtn) {
|
---|
349 | case SIO_RDY_SND:
|
---|
350 | uart_enable_send(siopcb);
|
---|
351 | break;
|
---|
352 | case SIO_RDY_RCV:
|
---|
353 | uart_enable_rcv(siopcb);
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | /*
|
---|
359 | * シリアルI/Oポートからのコールバックの禁止
|
---|
360 | */
|
---|
361 | void
|
---|
362 | sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
363 | {
|
---|
364 | switch (cbrtn) {
|
---|
365 | case SIO_RDY_SND:
|
---|
366 | uart_disable_send(siopcb);
|
---|
367 | break;
|
---|
368 | case SIO_RDY_RCV:
|
---|
369 | uart_disable_rcv(siopcb);
|
---|
370 | break;
|
---|
371 | }
|
---|
372 | }
|
---|