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 | * ä¸è¨èä½æ¨©è
|
---|
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 388 2019-05-22 11:25:18Z coas-nagasima $
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * ã·ãªã¢ã«ã¤ã³ã¿ãã§ã¼ã¹ãã©ã¤ãã®ã¿ã¼ã²ããä¾åé¨ï¼GR-CITRUSç¨ï¼
|
---|
58 | */
|
---|
59 |
|
---|
60 | #include "gr_citrus.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 | #include "syssvc/siofd.h"
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * SIOãã¼ãåæåãããã¯ã®å®ç¾©
|
---|
72 | */
|
---|
73 | typedef struct sio_port_initialization_block {
|
---|
74 | char *path; /* ãã¡ã¤ã«ã®ãã¹å */
|
---|
75 | } SIOPINIB;
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * SIOãã¼ã管çãããã¯ã®å®ç¾©
|
---|
79 | */
|
---|
80 | struct sio_port_control_block {
|
---|
81 | const SIOPINIB *p_siopinib; /* SIOãã¼ãåæåããã㯠*/
|
---|
82 | intptr_t exinf; /* æ¡å¼µæ
|
---|
83 | å ± */
|
---|
84 | bool_t opened; /* ãªã¼ãã³æ¸ã¿ãã©ã° */
|
---|
85 | struct termios saved_term; /* å
|
---|
86 | ã®ç«¯æ«å¶å¾¡æ
|
---|
87 | å ± */
|
---|
88 |
|
---|
89 | int_t read_fd; /* èªåºãç¨ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ */
|
---|
90 | bool_t rcv_flag; /* åä¿¡æåãããã¡æå¹ãã©ã° */
|
---|
91 | char rcv_buf; /* åä¿¡æåãããã¡ */
|
---|
92 | bool_t rcv_rdy; /* åä¿¡éç¥ã³ã¼ã«ããã¯è¨±å¯ãã©ã° */
|
---|
93 |
|
---|
94 | int_t write_fd; /* æ¸è¾¼ã¿ç¨ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ */
|
---|
95 | bool_t snd_flag; /* éä¿¡æåãããã¡æå¹ãã©ã° */
|
---|
96 | char snd_buf; /* éä¿¡æåãããã¡ */
|
---|
97 | bool_t snd_rdy; /* éä¿¡éç¥ã³ã¼ã«ããã¯è¨±å¯ãã©ã° */
|
---|
98 | };
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * SIOãã¼ãåæåãããã¯
|
---|
102 | */
|
---|
103 | const SIOPINIB siopinib_table[TNUM_SIOP] = {
|
---|
104 | { NULL }
|
---|
105 | };
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * SIOãã¼ã管çãããã¯ã®ã¨ãªã¢
|
---|
109 | */
|
---|
110 | SIOPCB siopcb_table[TNUM_SIOP];
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * SIOãã¼ãIDãã管çãããã¯ãåãåºãããã®ãã¯ã
|
---|
114 | */
|
---|
115 | #define INDEX_SIOP(siopid) ((uint_t)((siopid) - 1))
|
---|
116 | #define get_siopcb(siopid) (&(siopcb_table[INDEX_SIOP(siopid)]))
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * SIOãã©ã¤ãã®åæå
|
---|
120 | */
|
---|
121 | void
|
---|
122 | sio_initialize(intptr_t exinf)
|
---|
123 | {
|
---|
124 | SIOPCB *p_siopcb;
|
---|
125 | uint_t i;
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * SIOãã¼ã管çãããã¯ã®åæå
|
---|
129 | */
|
---|
130 | for (i = 0; i < TNUM_SIOP; i++) {
|
---|
131 | p_siopcb = &(siopcb_table[i]);
|
---|
132 | p_siopcb->p_siopinib = &(siopinib_table[i]);
|
---|
133 | p_siopcb->opened = false;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * SIOãã©ã¤ãã®çµäºå¦ç
|
---|
139 | */
|
---|
140 | void
|
---|
141 | sio_terminate(intptr_t exinf)
|
---|
142 | {
|
---|
143 | uint_t i;
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * ãªã¼ãã³ããã¦ããSIOãã¼ãã®ã¯ãã¼ãº
|
---|
147 | */
|
---|
148 | for (i = 0; i < TNUM_SIOP; i++) {
|
---|
149 | sio_cls_por(&(siopcb_table[i]));
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * SIOãã¼ãã®ãªã¼ãã³
|
---|
155 | */
|
---|
156 | SIOPCB *
|
---|
157 | sio_opn_por(ID siopid, intptr_t exinf)
|
---|
158 | {
|
---|
159 | SIOPCB *p_siopcb;
|
---|
160 | const SIOPINIB *p_siopinib;
|
---|
161 | int_t fd;
|
---|
162 | struct termios term;
|
---|
163 |
|
---|
164 | p_siopcb = get_siopcb(siopid);
|
---|
165 | p_siopinib = p_siopcb->p_siopinib;
|
---|
166 |
|
---|
167 | if (!(p_siopcb->opened)) {
|
---|
168 | if (p_siopinib->path != NULL) {
|
---|
169 | fd = siofd_open(p_siopinib->path, O_RDWR, 0777);
|
---|
170 | assert(fd >= 0);
|
---|
171 | p_siopcb->read_fd = fd;
|
---|
172 | p_siopcb->write_fd = fd;
|
---|
173 | }
|
---|
174 | else {
|
---|
175 | fd = STDIN_FILENO; /* æ¨æºå
|
---|
176 | ¥åºåã使ã */
|
---|
177 | p_siopcb->read_fd = STDIN_FILENO;
|
---|
178 | p_siopcb->write_fd = STDOUT_FILENO;
|
---|
179 | }
|
---|
180 | siofd_fcntl(fd, F_SETOWN, getpid());
|
---|
181 | siofd_fcntl(fd, F_SETFL, (O_NONBLOCK | O_ASYNC));
|
---|
182 |
|
---|
183 | siofd_tcgetattr(fd, &(p_siopcb->saved_term));
|
---|
184 | term = p_siopcb->saved_term;
|
---|
185 | term.c_lflag &= ~(ECHO | ICANON);
|
---|
186 | siofd_tcsetattr(fd, TCSAFLUSH, &term);
|
---|
187 |
|
---|
188 | p_siopcb->exinf = exinf;
|
---|
189 | p_siopcb->rcv_flag = false;
|
---|
190 | p_siopcb->rcv_rdy = false;
|
---|
191 | p_siopcb->snd_flag = false;
|
---|
192 | p_siopcb->snd_rdy = false;
|
---|
193 | p_siopcb->opened = true;
|
---|
194 | }
|
---|
195 | return(p_siopcb);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * SIOãã¼ãã®ã¯ãã¼ãº
|
---|
200 | */
|
---|
201 | void
|
---|
202 | sio_cls_por(SIOPCB *p_siopcb)
|
---|
203 | {
|
---|
204 | int_t fd;
|
---|
205 |
|
---|
206 | if (p_siopcb->opened) {
|
---|
207 | fd = p_siopcb->read_fd;
|
---|
208 | siofd_tcsetattr(fd, TCSAFLUSH, &(p_siopcb->saved_term));
|
---|
209 | siofd_fcntl(fd, F_SETFL, 0);
|
---|
210 |
|
---|
211 | if (p_siopcb->p_siopinib->path != NULL) {
|
---|
212 | siofd_close(p_siopcb->read_fd);
|
---|
213 | }
|
---|
214 |
|
---|
215 | p_siopcb->opened = false;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * SIOã®å²è¾¼ã¿ãµã¼ãã¹ã«ã¼ãã³
|
---|
221 | */
|
---|
222 | bool_t
|
---|
223 | sio_isr_snd(ID siopid)
|
---|
224 | {
|
---|
225 | SIOPCB *p_siopcb = get_siopcb(siopid);
|
---|
226 | int_t n;
|
---|
227 |
|
---|
228 | if (p_siopcb->snd_flag) {
|
---|
229 | if ((n = siofd_write(p_siopcb->write_fd, &(p_siopcb->snd_buf), 1)) > 0) {
|
---|
230 | p_siopcb->snd_flag = false;
|
---|
231 | if (p_siopcb->snd_rdy) {
|
---|
232 | sio_irdy_snd(p_siopcb->exinf);
|
---|
233 | return p_siopcb->snd_rdy;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * SIOã®å²è¾¼ã¿ãµã¼ãã¹ã«ã¼ãã³
|
---|
243 | */
|
---|
244 | bool_t
|
---|
245 | sio_isr_rcv(ID siopid, char c)
|
---|
246 | {
|
---|
247 | SIOPCB *p_siopcb = get_siopcb(siopid);
|
---|
248 |
|
---|
249 | if (!p_siopcb->rcv_flag) {
|
---|
250 | p_siopcb->rcv_buf = c;
|
---|
251 | p_siopcb->rcv_flag = true;
|
---|
252 | if (p_siopcb->rcv_rdy) {
|
---|
253 | sio_irdy_rcv(p_siopcb->exinf);
|
---|
254 | return p_siopcb->rcv_rdy;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * SIOãã¼ãã¸ã®æåéä¿¡
|
---|
263 | */
|
---|
264 | bool_t
|
---|
265 | sio_snd_chr(SIOPCB *p_siopcb, char c)
|
---|
266 | {
|
---|
267 | int_t n;
|
---|
268 |
|
---|
269 | if (!p_siopcb->snd_flag) {
|
---|
270 | if ((n = siofd_write(p_siopcb->write_fd, &c, 1)) > 0) {
|
---|
271 | return(true);
|
---|
272 | }
|
---|
273 | else {
|
---|
274 | assert(n < 0 && errno == EAGAIN);
|
---|
275 | p_siopcb->snd_flag = true;
|
---|
276 | p_siopcb->snd_buf = c;
|
---|
277 | return(true);
|
---|
278 | }
|
---|
279 | }
|
---|
280 | else {
|
---|
281 | return(false);
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * SIOãã¼ãããã®æååä¿¡
|
---|
287 | */
|
---|
288 | int_t
|
---|
289 | sio_rcv_chr(SIOPCB *p_siopcb)
|
---|
290 | {
|
---|
291 | char c;
|
---|
292 | int_t n;
|
---|
293 |
|
---|
294 | if (p_siopcb->rcv_flag) {
|
---|
295 | p_siopcb->rcv_flag = false;
|
---|
296 | return((int_t)(uint8_t)(p_siopcb->rcv_buf));
|
---|
297 | }
|
---|
298 | else if ((n = siofd_read(p_siopcb->read_fd, &c, 1)) > 0) {
|
---|
299 | return((int_t)(uint8_t) c);
|
---|
300 | }
|
---|
301 | else {
|
---|
302 | assert(n < 0 && errno == EAGAIN);
|
---|
303 | return(-1);
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * SIOãã¼ãããã®ã³ã¼ã«ããã¯ã®è¨±å¯
|
---|
309 | */
|
---|
310 | void
|
---|
311 | sio_ena_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
|
---|
312 | {
|
---|
313 | switch (cbrtn) {
|
---|
314 | case SIO_RDY_SND:
|
---|
315 | p_siopcb->snd_rdy = true;
|
---|
316 | break;
|
---|
317 | case SIO_RDY_RCV:
|
---|
318 | p_siopcb->rcv_rdy = true;
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * SIOãã¼ãããã®ã³ã¼ã«ããã¯ã®ç¦æ¢
|
---|
325 | */
|
---|
326 | void
|
---|
327 | sio_dis_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
|
---|
328 | {
|
---|
329 | switch (cbrtn) {
|
---|
330 | case SIO_RDY_SND:
|
---|
331 | p_siopcb->snd_rdy = false;
|
---|
332 | break;
|
---|
333 | case SIO_RDY_RCV:
|
---|
334 | p_siopcb->rcv_rdy = false;
|
---|
335 | break;
|
---|
336 | }
|
---|
337 | }
|
---|