source: asp3_wo_tecs/trunk/target/macosx_xcode/target_serial.c@ 302

Last change on this file since 302 was 302, checked in by ertl-honda, 7 years ago

TECSレスのASP3の開発のため以下のtrunkからコピー
http://dev.toppers.jp/svn/asp3/branches/WO_TECS-3.C.0

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