source: asp_wo_cfg/trunk/target/macosx_gcc/target_serial.c@ 49

Last change on this file since 49 was 49, checked in by ertl-hiro, 12 years ago

asp_wo_kernelをコミット。

  • Property svn:keywords set to Id
File size: 7.3 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-2012 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: target_serial.c 49 2012-09-06 04:41:53Z ertl-hiro $
39 */
40
41/*
42 * シリアルI/Oデバイス(SIO)ドライバ(Mac OS X用)
43 */
44
45#include "macosx.h"
46#include <kernel.h>
47#include <t_syslog.h>
48#include <errno.h>
49#include <unistd.h>
50#include <fcntl.h>
51#include <termios.h>
52#include "unix_sigio.h"
53#include "target_serial.h"
54
55/*
56 * シリアルI/Oポート初期化ブロックの定義
57 */
58typedef struct sio_port_initialization_block {
59 char *path; /* ファイルのパス名 */
60} SIOPINIB;
61
62/*
63 * シリアルI/Oポート管理ブロックの定義
64 */
65struct sio_port_control_block {
66 const SIOPINIB *p_siopinib; /* シリアルI/Oポート初期化ブロック */
67 intptr_t exinf; /* 拡張情報 */
68 bool_t openflag; /* オープン済みフラグ */
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 * シリアルI/Oポート初期化ブロック
84 */
85const SIOPINIB siopinib_table[TNUM_SIOP] = {
86 { NULL }
87};
88
89/*
90 * シリアルI/Oポート管理ブロックのエリア
91 */
92SIOPCB 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 * SIOドライバの初期化
102 */
103void
104sio_initialize(intptr_t exinf)
105{
106 T_CISR cisr;
107 SIOPCB *p_siopcb;
108 uint_t i;
109 ER ercd;
110
111 /*
112 * UNIX用ノンブロッキングI/Oサポートモジュールの初期化
113 */
114 unix_sigio_initialize(0);
115
116 /*
117 * SIOドライバ(Mac OS X用)の初期化処理(target_serial.cfg相当の処理)
118 *
119 * ATT_ISR({ TA_NULL, 0, INTNO_SIGIO, sio_isr, 1 });
120 */
121 cisr.isratr = TA_NULL;
122 cisr.exinf = 0;
123 cisr.intno = INTNO_SIGIO;
124 cisr.isr = sio_isr;
125 cisr.isrpri = 1;
126 ercd = att_isr(&cisr);
127 assert(ercd == E_OK);
128
129 /*
130 * シリアルI/Oポート管理ブロックの初期化(元のsio_initializeの処理)
131 *
132 * ATT_INI({ TA_NULL, 0, sio_initialize });
133 */
134 for (i = 0; i < TNUM_SIOP; i++) {
135 p_siopcb = &(siopcb_table[i]);
136 p_siopcb->p_siopinib = &(siopinib_table[i]);
137 p_siopcb->openflag = false;
138 }
139}
140
141/*
142 * SIOドライバの終了処理
143 */
144void
145sio_terminate(intptr_t exinf)
146{
147 SIOPCB *p_siopcb;
148 uint_t i;
149
150 /*
151 * オープンされているシリアルI/Oポートのクローズ
152 */
153 for (i = 0; i < TNUM_SIOP; i++) {
154 p_siopcb = &(siopcb_table[i]);
155 if (p_siopcb->openflag) {
156 sio_cls_por(p_siopcb);
157 }
158 }
159}
160
161/*
162 * シリアルI/Oポートのオープン
163 */
164SIOPCB *
165sio_opn_por(ID siopid, intptr_t exinf)
166{
167 SIOPCB *p_siopcb;
168 const SIOPINIB *p_siopinib;
169 int_t fd;
170 struct termios term;
171
172 p_siopcb = get_siopcb(siopid);
173 p_siopinib = p_siopcb->p_siopinib;
174
175 if (p_siopinib->path != NULL) {
176 fd = open(p_siopinib->path, O_RDWR, 0777);
177 assert(fd >= 0);
178 p_siopcb->read_fd = fd;
179 p_siopcb->write_fd = fd;
180 }
181 else {
182 fd = STDIN_FILENO; /* 標準入出力を使う */
183 p_siopcb->read_fd = STDIN_FILENO;
184 p_siopcb->write_fd = STDOUT_FILENO;
185 }
186 fcntl(fd, F_SETOWN, getpid());
187 fcntl(fd, F_SETFL, (O_NONBLOCK | O_ASYNC));
188
189 tcgetattr(fd, &(p_siopcb->saved_term));
190 term = p_siopcb->saved_term;
191 term.c_lflag &= ~(ECHO | ICANON);
192 tcsetattr(fd, TCSAFLUSH, &term);
193
194 p_siopcb->exinf = exinf;
195 p_siopcb->rcv_flag = false;
196 p_siopcb->rcv_rdy = false;
197 p_siopcb->snd_flag = false;
198 p_siopcb->snd_rdy = false;
199 p_siopcb->openflag = true;
200 return(p_siopcb);
201}
202
203/*
204 * シリアルI/Oポートのクローズ
205 */
206void
207sio_cls_por(SIOPCB *p_siopcb)
208{
209 int_t fd;
210
211 fd = p_siopcb->read_fd;
212 tcsetattr(fd, TCSAFLUSH, &(p_siopcb->saved_term));
213 fcntl(fd, F_SETFL, 0);
214
215 if (p_siopcb->p_siopinib->path != NULL) {
216 close(p_siopcb->read_fd);
217 }
218 p_siopcb->openflag = false;
219}
220
221/*
222 * SIOの割込みサービスルーチン
223 */
224void
225sio_isr(intptr_t exinf)
226{
227 SIOPCB *p_siopcb = &(siopcb_table[0]);
228 int_t n;
229
230 if (p_siopcb->snd_flag) {
231 if ((n = write(p_siopcb->write_fd, &(p_siopcb->snd_buf), 1)) > 0) {
232 p_siopcb->snd_flag = false;
233 if (p_siopcb->snd_rdy) {
234 sio_irdy_snd(p_siopcb->exinf);
235 }
236 }
237 }
238 if (!p_siopcb->rcv_flag) {
239 if ((n = read(p_siopcb->read_fd, &(p_siopcb->rcv_buf), 1)) > 0) {
240 p_siopcb->rcv_flag = true;
241 if (p_siopcb->rcv_rdy) {
242 sio_irdy_rcv(p_siopcb->exinf);
243 }
244 }
245 }
246}
247
248/*
249 * シリアルI/Oポートへの文字送信
250 */
251bool_t
252sio_snd_chr(SIOPCB *p_siopcb, char c)
253{
254 int_t n;
255
256 if (!p_siopcb->snd_flag) {
257 if ((n = write(p_siopcb->write_fd, &c, 1)) > 0) {
258 return(true);
259 }
260 else {
261 assert(n < 0 && errno == EAGAIN);
262 p_siopcb->snd_flag = true;
263 p_siopcb->snd_buf = c;
264 return(true);
265 }
266 }
267 else {
268 return(false);
269 }
270}
271
272/*
273 * シリアルI/Oポートからの文字受信
274 */
275int_t
276sio_rcv_chr(SIOPCB *p_siopcb)
277{
278 char c;
279 int_t n;
280
281 if (p_siopcb->rcv_flag) {
282 p_siopcb->rcv_flag = false;
283 return((int_t)(uint8_t)(p_siopcb->rcv_buf));
284 }
285 else if ((n = read(p_siopcb->read_fd, &c, 1)) > 0) {
286 return((int_t)(uint8_t) c);
287 }
288 else {
289 assert(n < 0 && errno == EAGAIN);
290 return(-1);
291 }
292}
293
294/*
295 * シリアルI/Oポートからのコールバックの許可
296 */
297void
298sio_ena_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
299{
300 switch (cbrtn) {
301 case SIO_RDY_SND:
302 p_siopcb->snd_rdy = true;
303 break;
304 case SIO_RDY_RCV:
305 p_siopcb->rcv_rdy = true;
306 break;
307 }
308}
309
310/*
311 * シリアルI/Oポートからのコールバックの禁止
312 */
313void
314sio_dis_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
315{
316 switch (cbrtn) {
317 case SIO_RDY_SND:
318 p_siopcb->snd_rdy = false;
319 break;
320 case SIO_RDY_RCV:
321 p_siopcb->rcv_rdy = false;
322 break;
323 }
324}
Note: See TracBrowser for help on using the repository browser.