GadgetSeed  0.9.6
console.c
[詳解]
1 /** @file
2  @brief コンソールIO
3 
4  GadgetSeed はバイト入出力をもつデバイスドライバをコンソールとして
5  使用することが出来ます。
6 
7  コンソール出力には tprintf() 関数でフォーマットされた文字列を出力ます。
8 
9  @date 2007.03.17
10  @author Takashi SHUDO
11 
12  @page console_io コンソールIO
13 
14  GadgetSeedは標準化されたコンソールIOがあります。
15 
16  コンソールIOはUNIX系OSの標準入出力に似ています。
17 
18  コンソールIOは「標準入力」、「標準出力」、「エラー出力」の3種類があります。\n
19  これはらの入出力には何らかのデバイスドライバを割り当てることができます。\n
20  また、入出力デバイスはタスク毎に固有のデバイスを割り当てることができます。
21 
22  コンソールデバイスにはUARTデバイスを使うことを推奨します。\n
23  また、コンソールデバイスは @ref command_shell のインタフェースとして使用されます。
24 
25 
26  ---
27  @section console_api コンソールIO API
28 
29  @subsection standard_in_out_device_register_api 標準入出力デバイス登録API
30 
31  以下のAPIはシステムの標準入出力デバイスを登録します。\n
32  これらのAPIで登録されたデバイスは生成されたタスクの標準入出力デバイスとなります。\n
33  これらのAPIはシステムの初期化時に使用して下さい。\n
34  コマンドシェルを使用する場合は、必ず何らかのデバイスを登録する必要があります。
35 
36  include ファイル : console.h
37 
38  | API名 | 機能 |
39  |:--------------------------|:--------------------------------------|
40  | register_console_in_dev() | @copybrief register_console_in_dev |
41  | register_console_out_dev()| @copybrief register_console_out_dev |
42  | register_error_out_dev() | @copybrief register_error_out_dev |
43 
44  @subsection strings_out_api 文字列出力API
45 
46  GadgetSeedはprintf()に似た書式付きの文字列出力APIがあります。
47 
48  include ファイル : tprintf.h
49 
50  | API名 | 機能 |
51  |:--------------------------|:----------------------|
52  | tprintf() | @copybrief tprintf |
53  | eprintf() | @copybrief eprintf |
54 
55  tprintf() は、 register_console_out_dev() で登録されたデバイスに出力されます。\n
56  eprintf() は、 register_error_out_dev() で登録されたデバイスに出力されます。
57 
58  @subsection strings_in_api コンソール入力API
59 
60  標準入力から入力データを取得するには以下のAPIを使用します。
61 
62  include ファイル : console.h
63 
64  | API名 | 機能 |
65  |:--------------------------|:----------------------|
66  | cgets() | @copybrief cgets |
67  | cgetc() | @copybrief cgetc |
68  | cwait() | @copybrief cwait |
69  | cgetcnw() | @copybrief cgetcnw |
70 
71  @subsection standard_in_out_device_change_api 標準入出力デバイス変更API
72 
73  実行中のタスクは、標準入出力デバイスは動的に変更することができます。\n
74  標準入出力デバイスを変更するには、以下のAPIを使用します。
75 
76  include ファイル : syscall.h
77 
78  | API名 | 機能 |
79  |:--------------------------|:--------------------------------------|
80  | set_console_in_device() | @copybrief set_console_in_device |
81  | set_console_out_device() | @copybrief set_console_out_device |
82  | set_error_out_device() | @copybrief set_error_out_device |
83 */
84 
85 #include "console.h"
86 #include "tprintf.h"
87 #include "task/tcb.h"
88 
89 struct st_device *con_in_dev; ///< デフォルト標準入力デバイス
90 struct st_device *con_out_dev; ///< デフォルト標準出力デバイス
91 struct st_device *con_err_dev; ///< デフォルトエラー出力デバイス
92 
93 extern struct st_tcb *run_task;
94 
95 /**
96  @brief システム標準のコンソール入力デバイスを登録する
97 
98  @param[in] in_dev コンソール入力デバイス
99 */
100 void register_console_in_dev(const struct st_device *in_dev)
101 {
102  con_in_dev = (struct st_device *)in_dev;
103 }
104 
105 /**
106  @brief システム標準のコンソール出力デバイスを登録する
107 
108  @param[in] out_dev コンソール出力デバイス
109 */
110 void register_console_out_dev(const struct st_device *out_dev)
111 {
112  con_out_dev = (struct st_device *)out_dev;
113 }
114 
115 /**
116  @brief 全てのコンソール入出力デバイスを初期化する
117 */
119 {
120  con_out_dev = 0;
121  con_in_dev = 0;
122  con_err_dev = 0;
123 }
124 
125 /*
126  * コンソールI/O関数
127  */
128 
129 /**
130  @brief 標準出力より文字列を出力する
131 
132  @param[in] str 文字列ポインタ
133  @param[in] len 文字列のバイト数
134 
135  @return 出力した文字数
136 */
137 int cputs(unsigned char *str, unsigned int len)
138 {
139 #if 0
140  return write_device(run_task->stdout_dev, str, len);
141 #else // LF = CRLF
142  int i;
143 
144  for(i=0; i<len; i++) {
145  if(*str == '\n') {
146  putc_device(run_task->stdout_dev, '\r');
147  }
148  putc_device(run_task->stdout_dev, *str);
149  str ++;
150  }
151 
152  return i;
153 #endif
154 }
155 
156 /**
157  @brief 標準出力より1文字を出力する
158 
159  @param[in] td 文字
160 
161  @return 出力した文字数
162 */
163 int cputc(unsigned char td)
164 {
165  return putc_device(run_task->stdout_dev, td);
166 }
167 
168 /**
169  @brief 標準入力より文字列を取得する
170 
171  @param[in] rd 入力文字ポインタ
172 
173  @return !=0:取得文字有り
174 */
175 int cgets(unsigned char *str, unsigned int count)
176 {
177  int rt;
178 
179  if(run_task->stdin_dev != 0) {
180  (void)select_device(run_task->stdin_dev, 0);
181  rt = read_device(run_task->stdin_dev, str, count);
182  if(rt != 0) {
183  return rt;
184  }
185  }
186 
187  return 0;
188 }
189 
190 /**
191  @brief 標準入力より1文字を取得する
192 
193  @param[out] rd 取得文字ポインタ
194 
195  @return !=0:入力文字有り
196 */
197 int cgetc(unsigned char *rd)
198 {
199  int rt;
200 
201  if(run_task->stdin_dev != 0) {
202  (void)select_device(run_task->stdin_dev, 0);
203  rt = getc_device(run_task->stdin_dev, rd);
204  if(rt != 0) {
205  return rt;
206  }
207  }
208 
209  return 0;
210 }
211 
212 /**
213  @brief 標準入力より入力を待つ
214 
215  @param[in] timeout タイムアウト時間(ms)
216 
217  @return !=0:入力文字有り
218 */
219 int cwait(unsigned int timeout)
220 {
221  if(run_task->stdin_dev != 0) {
222  return select_device(run_task->stdin_dev, timeout);
223  }
224 
225  return 0;
226 }
227 
228 /**
229  @brief 標準入力より1文字を取得する(待ち無し)
230 
231  @param[out] rd 取得文字ポインタ
232 
233  @return !=0:取得文字有り
234 */
235 int cgetcnw(unsigned char *rd)
236 {
237  int rt;
238 
239  if(run_task->stdin_dev != 0) {
240  rt = getc_device(run_task->stdin_dev, rd);
241  if(rt != 0) {
242  return rt;
243  }
244  }
245 
246  return 0;
247 }
248 
249 
250 /**
251  @brief システム標準のエラー出力デバイスを登録する
252 
253  @param[in] err_dev エラー出力デバイス
254 */
255 void register_error_out_dev(const struct st_device *err_dev)
256 {
257  con_err_dev = (struct st_device *)err_dev;
258 }
259 
260 
261 /**
262  @brief エラー出力より文字列を出力する
263 
264  @param[in] str 文字列ポインタ
265  @param[in] len 文字列のバイト数
266 */
267 int eputs(unsigned char *str, unsigned int len)
268 {
269 #if 0
270  return write_device(run_task->error_dev, str, len);
271 #else // LF = CRLF
272  int i;
273 
274  for(i=0; i<len; i++) {
275  if(*str == '\n') {
276  putc_device(run_task->error_dev, '\r');
277  }
278  putc_device(run_task->error_dev, *str);
279  str ++;
280  }
281 
282  return i;
283 #endif
284 }
285 
286 
287 /**
288  @brief 標準入力デバイスを設定する
289 
290  @param[in] dev デバイス
291 */
293 {
294  if(dev != 0) {
295  run_task->stdin_dev = dev;
296  } else {
297  run_task->stdin_dev = con_in_dev;
298  }
299 }
300 
301 
302 /**
303  @brief 標準出力デバイスを設定する
304 
305  @param[in] dev デバイス
306 */
308 {
309  if(dev != 0) {
310  run_task->stdout_dev = dev;
311  } else {
312  run_task->stdout_dev = con_out_dev;
313  }
314 }
315 
316 
317 /**
318  @brief エラー出力デバイスを設定する
319 
320  @param[in] dev デバイス
321 */
323 {
324  if(dev != 0) {
325  run_task->error_dev = dev;
326  } else {
327  run_task->error_dev = con_err_dev;
328  }
329 }
struct st_device * con_err_dev
デフォルトエラー出力デバイス
Definition: console.c:91
タスクコンテキストブロック
struct st_device * con_in_dev
デフォルト標準入力デバイス
Definition: console.c:89
void register_console_out_dev(const struct st_device *out_dev)
システム標準のコンソール出力デバイスを登録する
Definition: console.c:110
void register_console_in_dev(const struct st_device *in_dev)
システム標準のコンソール入力デバイスを登録する
Definition: console.c:100
void set_console_out_device_ISR(struct st_device *dev)
標準出力デバイスを設定する
Definition: console.c:307
int write_device(struct st_device *dev, const void *buf, unsigned int count)
デバイスにデータを書き込む
Definition: device.c:451
int read_device(struct st_device *dev, void *buf, unsigned int count)
デバイスよりデータを読み出す
Definition: device.c:378
int eputs(unsigned char *str, unsigned int len)
エラー出力より文字列を出力する
Definition: console.c:267
struct st_device * stdin_dev
タスク標準入力デバイス
Definition: tcb.h:52
int cwait(unsigned int timeout)
標準入力より入力を待つ
Definition: console.c:219
int cgetcnw(unsigned char *rd)
標準入力より1文字を取得する(待ち無し)
Definition: console.c:235
struct st_device * stdout_dev
タスク標準出力デバイス
Definition: tcb.h:53
int cputs(unsigned char *str, unsigned int len)
標準出力より文字列を出力する
Definition: console.c:137
int getc_device(struct st_device *dev, unsigned char *data)
デバイスよりデータを1バイト読み出す
Definition: device.c:429
void register_error_out_dev(const struct st_device *err_dev)
システム標準のエラー出力デバイスを登録する
Definition: console.c:255
int putc_device(struct st_device *dev, unsigned char data)
デバイスにデータを1バイト書き込む
Definition: device.c:502
void set_console_in_device_ISR(struct st_device *dev)
標準入力デバイスを設定する
Definition: console.c:292
int cputc(unsigned char td)
標準出力より1文字を出力する
Definition: console.c:163
struct st_device * con_out_dev
デフォルト標準出力デバイス
Definition: console.c:90
struct st_device * error_dev
タスクエラー出力デバイス
Definition: tcb.h:54
int cgetc(unsigned char *rd)
標準入力より1文字を取得する
Definition: console.c:197
int select_device(struct st_device *dev, unsigned int timeout)
デバイスのアクセス準備完了を待つ
Definition: device.c:582
コンソールI/O
void set_error_out_device_ISR(struct st_device *dev)
エラー出力デバイスを設定する
Definition: console.c:322
デバイスドライバ構造体
Definition: device.h:25
void init_console_device(void)
全てのコンソール入出力デバイスを初期化する
Definition: console.c:118
int cgets(unsigned char *str, unsigned int count)
標準入力より文字列を取得する
Definition: console.c:175
タスクコンテキスト
Definition: tcb.h:32
機能限定printf