source: mypx/target/macosx_gcc/target_serial.c @ 7

このファイルの 7 以降における最終更新内容7 で kominami が 2015/09/12 9:22:12 に更新しました

TOPPERS/ASP 1.9.1

ファイルサイズ: 6.8 KB
Rev 
[7]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 *  Ÿåµ­Ãøºîž¢ŒÔ€Ï¡€°Ê²Œ€Î(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 *              ¥·¥ê¥¢¥ëI/O¥Ç¥Ð¥€¥¹¡ÊSIO¡Ë¥É¥é¥€¥Ð¡ÊMac OS XÍÑ¡Ë
43 */
44
45#include "macosx.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
54/*
55 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥ÈœéŽü²œ¥Ö¥í¥Ã¥¯€ÎÄêµÁ
56 */
57typedef struct sio_port_initialization_block {
58        char            *path;                  /* ¥Õ¥¡¥€¥ë€Î¥Ñ¥¹ÌŸ */
59} SIOPINIB;
60
61/*
62 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥ÈŽÉÍý¥Ö¥í¥Ã¥¯€ÎÄêµÁ
63 */
64struct sio_port_control_block {
65        const SIOPINIB *p_siopinib;     /* ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥ÈœéŽü²œ¥Ö¥í¥Ã¥¯ */
66        intptr_t        exinf;                  /* ³ÈÄ¥ŸðÊó */
67        bool_t          openflag;               /* ¥ª¡Œ¥×¥óºÑ€ß¥Õ¥é¥° */
68        struct termios saved_term;      /* žµ€ÎÃŒËöÀ©žæŸðÊó */
69
70        int_t           read_fd;                /* ÆÉœÐ€·ÍÑ¥Õ¥¡¥€¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ */
71        bool_t          rcv_flag;               /* Œõ¿®Êž»ú¥Ð¥Ã¥Õ¥¡Í­žú¥Õ¥é¥° */
72        char            rcv_buf;                /* Œõ¿®Êž»ú¥Ð¥Ã¥Õ¥¡ */
73        bool_t          rcv_rdy;                /* Œõ¿®ÄÌÃÎ¥³¡Œ¥ë¥Ð¥Ã¥¯µö²Ä¥Õ¥é¥° */
74
75        int_t           write_fd;               /* œñ¹þ€ßÍÑ¥Õ¥¡¥€¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿ */
76        bool_t          snd_flag;               /* Á÷¿®Êž»ú¥Ð¥Ã¥Õ¥¡Í­žú¥Õ¥é¥° */
77        char            snd_buf;                /* Á÷¿®Êž»ú¥Ð¥Ã¥Õ¥¡ */
78        bool_t          snd_rdy;                /* Á÷¿®ÄÌÃÎ¥³¡Œ¥ë¥Ð¥Ã¥¯µö²Ä¥Õ¥é¥° */
79};
80
81/*
82 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥ÈœéŽü²œ¥Ö¥í¥Ã¥¯
83 */
84const SIOPINIB siopinib_table[TNUM_SIOP] = {
85        { NULL }
86};
87
88/*
89 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥ÈŽÉÍý¥Ö¥í¥Ã¥¯€Î¥š¥ê¥¢
90 */
91SIOPCB  siopcb_table[TNUM_SIOP];
92
93/*
94 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥ÈID€«€éŽÉÍý¥Ö¥í¥Ã¥¯€òŒè€êœÐ€¹€¿€á€Î¥Þ¥¯¥í
95 */
96#define INDEX_SIOP(siopid)      ((uint_t)((siopid) - 1))
97#define get_siopcb(siopid)      (&(siopcb_table[INDEX_SIOP(siopid)]))
98
99/*
100 *  SIO¥É¥é¥€¥Ð€ÎœéŽü²œ
101 */
102void
103sio_initialize(intptr_t exinf)
104{
105        SIOPCB  *p_siopcb;
106        uint_t  i;
107
108        /*
109         *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥ÈŽÉÍý¥Ö¥í¥Ã¥¯€ÎœéŽü²œ
110         */
111        for (i = 0; i < TNUM_SIOP; i++) {
112                p_siopcb = &(siopcb_table[i]);
113                p_siopcb->p_siopinib = &(siopinib_table[i]);
114                p_siopcb->openflag = false;
115        }
116}
117
118/*
119 *  SIO¥É¥é¥€¥Ð€ÎœªÎ»œèÍý
120 */
121void
122sio_terminate(intptr_t exinf)
123{
124        SIOPCB  *p_siopcb;
125        uint_t  i;
126
127        /*
128         *  ¥ª¡Œ¥×¥ó€µ€ì€Æ€€€ë¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥È€Î¥¯¥í¡Œ¥º
129         */
130        for (i = 0; i < TNUM_SIOP; i++) {
131                p_siopcb = &(siopcb_table[i]);
132                if (p_siopcb->openflag) {
133                        sio_cls_por(p_siopcb);
134                }
135        }
136}
137
138/*
139 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥È€Î¥ª¡Œ¥×¥ó
140 */
141SIOPCB *
142sio_opn_por(ID siopid, intptr_t exinf)
143{
144        SIOPCB                  *p_siopcb;
145        const SIOPINIB  *p_siopinib;
146        int_t                   fd;
147        struct termios  term;
148
149        p_siopcb = get_siopcb(siopid);
150        p_siopinib = p_siopcb->p_siopinib;
151
152        if (p_siopinib->path != NULL) {
153                fd = open(p_siopinib->path, O_RDWR, 0777);
154                assert(fd >= 0);
155                p_siopcb->read_fd = fd;
156                p_siopcb->write_fd = fd;
157        }
158        else {
159                fd = STDIN_FILENO;                                      /* ÉžœàÆþœÐÎÏ€ò»È€Š */
160                p_siopcb->read_fd = STDIN_FILENO;
161                p_siopcb->write_fd = STDOUT_FILENO;
162        }
163        fcntl(fd, F_SETOWN, getpid());
164        fcntl(fd, F_SETFL, (O_NONBLOCK | O_ASYNC));
165
166        tcgetattr(fd, &(p_siopcb->saved_term));
167        term = p_siopcb->saved_term;
168        term.c_lflag &= ~(ECHO | ICANON);
169        tcsetattr(fd, TCSAFLUSH, &term);
170
171        p_siopcb->exinf = exinf;
172        p_siopcb->rcv_flag = false;
173        p_siopcb->rcv_rdy = false;
174        p_siopcb->snd_flag = false;
175        p_siopcb->snd_rdy = false;
176        p_siopcb->openflag = true;
177        return(p_siopcb);
178}
179
180/*
181 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥È€Î¥¯¥í¡Œ¥º
182 */
183void
184sio_cls_por(SIOPCB *p_siopcb)
185{
186        int_t   fd;
187
188        fd = p_siopcb->read_fd;
189        tcsetattr(fd, TCSAFLUSH, &(p_siopcb->saved_term));
190        fcntl(fd, F_SETFL, 0);
191
192        if (p_siopcb->p_siopinib->path != NULL) {
193                close(p_siopcb->read_fd);
194        }
195        p_siopcb->openflag = false;
196}
197
198/*
199 *  SIO€Î³ä¹þ€ß¥µ¡Œ¥Ó¥¹¥ë¡Œ¥Á¥ó
200 */
201void
202sio_isr(intptr_t exinf)
203{
204        SIOPCB  *p_siopcb = &(siopcb_table[0]);
205        int_t   n;
206
207        if (p_siopcb->snd_flag) {
208                if ((n = write(p_siopcb->write_fd, &(p_siopcb->snd_buf), 1)) > 0) {
209                        p_siopcb->snd_flag = false;
210                        if (p_siopcb->snd_rdy) {
211                                sio_irdy_snd(p_siopcb->exinf);
212                        }
213                }
214        }
215        if (!p_siopcb->rcv_flag) {
216                if ((n = read(p_siopcb->read_fd, &(p_siopcb->rcv_buf), 1)) > 0) {
217                        p_siopcb->rcv_flag = true;
218                        if (p_siopcb->rcv_rdy) {
219                                sio_irdy_rcv(p_siopcb->exinf);
220                        }
221                }
222        }
223}
224
225/*
226 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥È€Ø€ÎÊž»úÁ÷¿®
227 */
228bool_t
229sio_snd_chr(SIOPCB *p_siopcb, char c)
230{
231        int_t   n;
232
233        if (!p_siopcb->snd_flag) {
234                if ((n = write(p_siopcb->write_fd, &c, 1)) > 0) {
235                        return(true);
236                }
237                else {
238                        assert(n < 0 && errno == EAGAIN);
239                        p_siopcb->snd_flag = true;
240                        p_siopcb->snd_buf = c;
241                        return(true);
242                }
243        }
244        else {
245                return(false);
246        }
247}
248
249/*
250 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥È€«€é€ÎÊž»úŒõ¿®
251 */
252int_t
253sio_rcv_chr(SIOPCB *p_siopcb)
254{
255        char    c;
256        int_t   n;
257
258        if (p_siopcb->rcv_flag) {
259                p_siopcb->rcv_flag = false;
260                return((int_t)(uint8_t)(p_siopcb->rcv_buf));
261        }
262        else if ((n = read(p_siopcb->read_fd, &c, 1)) > 0) {
263                return((int_t)(uint8_t) c);
264        }
265        else {
266                assert(n < 0 && errno == EAGAIN);
267                return(-1);
268        }
269}
270
271/*
272 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥È€«€é€Î¥³¡Œ¥ë¥Ð¥Ã¥¯€Îµö²Ä
273 */
274void
275sio_ena_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
276{
277        switch (cbrtn) {
278        case SIO_RDY_SND:
279                p_siopcb->snd_rdy = true;
280                break;
281        case SIO_RDY_RCV:
282                p_siopcb->rcv_rdy = true;
283                break;
284        }
285}
286
287/*
288 *  ¥·¥ê¥¢¥ëI/O¥Ý¡Œ¥È€«€é€Î¥³¡Œ¥ë¥Ð¥Ã¥¯€Î¶Ø»ß
289 */
290void
291sio_dis_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
292{
293        switch (cbrtn) {
294        case SIO_RDY_SND:
295                p_siopcb->snd_rdy = false;
296                break;
297        case SIO_RDY_RCV:
298                p_siopcb->rcv_rdy = false;
299                break;
300        }
301}
詳しい使い方は TracBrowser を参照してください。