source: asp3_tinet_ecnl_rx/trunk/asp3_dcre/target/gr_citrus_gcc/target_serial.c@ 387

Last change on this file since 387 was 387, checked in by coas-nagasima, 5 years ago

ファイルディスクリプタ処理を更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 8.0 KB
Line 
1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2006-2018 by Embedded and Real-Time Systems Laboratory
7 * Graduate School of Information Science, Nagoya Univ., JAPAN
8 *
9 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
10 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
11 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
12 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
13 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
14 * スコード中に含まれていること.
15 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
16 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
17 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
18 * の無保証規定を掲載すること.
19 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
20 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
21 * と.
22 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
23 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
24 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
25 * 報告すること.
26 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
27 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
28 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
29 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
30 * 免責すること.
31 *
32 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
33 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
34 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
35 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
36 * の責任を負わない.
37 *
38 * @(#) $Id$
39 */
40
41/*
42 * シリアルインタフェースドライバのターゲット依存部(GR-CITRUS用)
43 */
44
45#include "gr_citrus.h"
46#include <t_stddef.h>
47#include <t_syslog.h>
48#include <errno.h>
49#include <unistd.h>
50#include <fcntl.h>
51#include <termios.h>
52#include "target_serial.h"
53#include "syssvc/siofd.h"
54
55/*
56 * SIOポート初期化ブロックの定義
57 */
58typedef struct sio_port_initialization_block {
59 char *path; /* ファイルのパス名 */
60} SIOPINIB;
61
62/*
63 * SIOポート管理ブロックの定義
64 */
65struct sio_port_control_block {
66 const SIOPINIB *p_siopinib; /* SIOポート初期化ブロック */
67 intptr_t exinf; /* 拡張情報 */
68 bool_t opened; /* オープン済みフラグ */
69 struct termios saved_term; /* 元の端末制御情報 */
70
71 int_t read_fd; /* 読出し用ファイルディスクリプタ */
72 bool_t rcv_flag; /* 受信文字バッファ有効フラグ */
73 char rcv_buf; /* 受信文字バッファ */
74 bool_t rcv_rdy; /* 受信通知コールバック許可フラグ */
75
76 int_t write_fd; /* 書込み用ファイルディスクリプタ */
77 bool_t snd_flag; /* 送信文字バッファ有効フラグ */
78 char snd_buf; /* 送信文字バッファ */
79 bool_t snd_rdy; /* 送信通知コールバック許可フラグ */
80};
81
82/*
83 * SIOポート初期化ブロック
84 */
85const SIOPINIB siopinib_table[TNUM_SIOP] = {
86 { NULL }
87};
88
89/*
90 * SIOポート管理ブロックのエリア
91 */
92SIOPCB siopcb_table[TNUM_SIOP];
93
94/*
95 * SIOポートIDから管理ブロックを取り出すためのマクロ
96 */
97#define INDEX_SIOP(siopid) ((uint_t)((siopid) - 1))
98#define get_siopcb(siopid) (&(siopcb_table[INDEX_SIOP(siopid)]))
99
100/*
101 * SIOドライバの初期化
102 */
103void
104sio_initialize(intptr_t exinf)
105{
106 SIOPCB *p_siopcb;
107 uint_t i;
108
109 /*
110 * SIOポート管理ブロックの初期化
111 */
112 for (i = 0; i < TNUM_SIOP; i++) {
113 p_siopcb = &(siopcb_table[i]);
114 p_siopcb->p_siopinib = &(siopinib_table[i]);
115 p_siopcb->opened = false;
116 }
117}
118
119/*
120 * SIOドライバの終了処理
121 */
122void
123sio_terminate(intptr_t exinf)
124{
125 uint_t i;
126
127 /*
128 * オープンされているSIOポートのクローズ
129 */
130 for (i = 0; i < TNUM_SIOP; i++) {
131 sio_cls_por(&(siopcb_table[i]));
132 }
133}
134
135/*
136 * SIOポートのオープン
137 */
138SIOPCB *
139sio_opn_por(ID siopid, intptr_t exinf)
140{
141 SIOPCB *p_siopcb;
142 const SIOPINIB *p_siopinib;
143 int_t fd;
144 struct termios term;
145
146 p_siopcb = get_siopcb(siopid);
147 p_siopinib = p_siopcb->p_siopinib;
148
149 if (!(p_siopcb->opened)) {
150 if (p_siopinib->path != NULL) {
151 fd = siofd_open(p_siopinib->path, O_RDWR, 0777);
152 assert(fd >= 0);
153 p_siopcb->read_fd = fd;
154 p_siopcb->write_fd = fd;
155 }
156 else {
157 fd = STDIN_FILENO; /* 標準入出力を使う */
158 p_siopcb->read_fd = STDIN_FILENO;
159 p_siopcb->write_fd = STDOUT_FILENO;
160 }
161 siofd_fcntl(fd, F_SETOWN, getpid());
162 siofd_fcntl(fd, F_SETFL, (O_NONBLOCK | O_ASYNC));
163
164 siofd_tcgetattr(fd, &(p_siopcb->saved_term));
165 term = p_siopcb->saved_term;
166 term.c_lflag &= ~(ECHO | ICANON);
167 siofd_tcsetattr(fd, TCSAFLUSH, &term);
168
169 p_siopcb->exinf = exinf;
170 p_siopcb->rcv_flag = false;
171 p_siopcb->rcv_rdy = false;
172 p_siopcb->snd_flag = false;
173 p_siopcb->snd_rdy = false;
174 p_siopcb->opened = true;
175 }
176 return(p_siopcb);
177}
178
179/*
180 * SIOポートのクローズ
181 */
182void
183sio_cls_por(SIOPCB *p_siopcb)
184{
185 int_t fd;
186
187 if (p_siopcb->opened) {
188 fd = p_siopcb->read_fd;
189 siofd_tcsetattr(fd, TCSAFLUSH, &(p_siopcb->saved_term));
190 siofd_fcntl(fd, F_SETFL, 0);
191
192 if (p_siopcb->p_siopinib->path != NULL) {
193 siofd_close(p_siopcb->read_fd);
194 }
195
196 p_siopcb->opened = false;
197 }
198}
199
200/*
201 * SIOの割込みサービスルーチン
202 */
203bool_t
204sio_isr_snd(ID siopid)
205{
206 SIOPCB *p_siopcb = get_siopcb(siopid);
207 int_t n;
208
209 if (p_siopcb->snd_flag) {
210 if ((n = siofd_write(p_siopcb->write_fd, &(p_siopcb->snd_buf), 1)) > 0) {
211 p_siopcb->snd_flag = false;
212 if (p_siopcb->snd_rdy) {
213 sio_irdy_snd(p_siopcb->exinf);
214 return p_siopcb->snd_rdy;
215 }
216 }
217 }
218
219 return false;
220}
221
222/*
223 * SIOの割込みサービスルーチン
224 */
225bool_t
226sio_isr_rcv(ID siopid, char c)
227{
228 SIOPCB *p_siopcb = get_siopcb(siopid);
229
230 if (!p_siopcb->rcv_flag) {
231 p_siopcb->rcv_buf = c;
232 p_siopcb->rcv_flag = true;
233 if (p_siopcb->rcv_rdy) {
234 sio_irdy_rcv(p_siopcb->exinf);
235 return p_siopcb->rcv_rdy;
236 }
237 }
238
239 return false;
240}
241
242/*
243 * SIOポートへの文字送信
244 */
245bool_t
246sio_snd_chr(SIOPCB *p_siopcb, char c)
247{
248 int_t n;
249
250 if (!p_siopcb->snd_flag) {
251 if ((n = siofd_write(p_siopcb->write_fd, &c, 1)) > 0) {
252 return(true);
253 }
254 else {
255 assert(n < 0 && errno == EAGAIN);
256 p_siopcb->snd_flag = true;
257 p_siopcb->snd_buf = c;
258 return(true);
259 }
260 }
261 else {
262 return(false);
263 }
264}
265
266/*
267 * SIOポートからの文字受信
268 */
269int_t
270sio_rcv_chr(SIOPCB *p_siopcb)
271{
272 char c;
273 int_t n;
274
275 if (p_siopcb->rcv_flag) {
276 p_siopcb->rcv_flag = false;
277 return((int_t)(uint8_t)(p_siopcb->rcv_buf));
278 }
279 else if ((n = siofd_read(p_siopcb->read_fd, &c, 1)) > 0) {
280 return((int_t)(uint8_t) c);
281 }
282 else {
283 assert(n < 0 && errno == EAGAIN);
284 return(-1);
285 }
286}
287
288/*
289 * SIOポートからのコールバックの許可
290 */
291void
292sio_ena_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
293{
294 switch (cbrtn) {
295 case SIO_RDY_SND:
296 p_siopcb->snd_rdy = true;
297 break;
298 case SIO_RDY_RCV:
299 p_siopcb->rcv_rdy = true;
300 break;
301 }
302}
303
304/*
305 * SIOポートからのコールバックの禁止
306 */
307void
308sio_dis_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
309{
310 switch (cbrtn) {
311 case SIO_RDY_SND:
312 p_siopcb->snd_rdy = false;
313 break;
314 case SIO_RDY_RCV:
315 p_siopcb->rcv_rdy = false;
316 break;
317 }
318}
Note: See TracBrowser for help on using the repository browser.