[61] | 1 | /*
|
---|
| 2 | * TOPPERS/ASP Kernel
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
| 4 | * Advanced Standard Profile Kernel
|
---|
| 5 | *
|
---|
| 6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
| 7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
| 8 | * Copyright (C) 2006-2011 by Embedded and Real-Time Systems Laboratory
|
---|
| 9 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 10 | *
|
---|
| 11 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
| 12 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
| 13 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
| 14 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
| 15 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
| 16 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
| 17 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
| 18 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
| 19 | gip
|
---|
| 20 | * Ò}j
|
---|
| 21 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
| 22 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
| 23 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
| 24 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
| 25 | * ÆD
|
---|
| 26 | * (a) Äzzɺ¤hL
|
---|
| 27 | gipÒ}j
|
---|
| 28 | AÈÇjÉCãLÌ
|
---|
| 29 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
| 30 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
| 31 | * ñ·é±ÆD
|
---|
| 32 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
| 33 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
| 34 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
| 35 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
| 36 | * ÆÓ·é±ÆD
|
---|
| 37 | *
|
---|
| 38 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
| 39 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
| 40 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
| 41 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
| 42 | * ÌÓCðíÈ¢D
|
---|
| 43 | *
|
---|
| 44 | * @(#) $Id: serial.c 2055 2011-04-10 11:36:13Z ertl-hiro $
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | /*
|
---|
| 48 | * VAC^tF[XhCo
|
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 | #include <kernel.h>
|
---|
| 52 | #include <t_syslog.h>
|
---|
| 53 | #include "target_syssvc.h"
|
---|
| 54 | #include "target_serial.h"
|
---|
| 55 | #include "serial.h"
|
---|
| 56 | #include "kernel_cfg.h"
|
---|
| 57 |
|
---|
| 58 | /*
|
---|
| 59 | * obt@TCYÌftHglÆobt@Ìè`
|
---|
| 60 | */
|
---|
| 61 | #ifndef SERIAL_RCV_BUFSZ1
|
---|
| 62 | #define SERIAL_RCV_BUFSZ1 256 /* |[g1ÌóMobt@TCY */
|
---|
| 63 | #endif /* SERIAL_RCV_BUFSZ1 */
|
---|
| 64 |
|
---|
| 65 | #ifndef SERIAL_SND_BUFSZ1
|
---|
| 66 | #define SERIAL_SND_BUFSZ1 256 /* |[g1ÌMobt@TCY */
|
---|
| 67 | #endif /* SERIAL_SND_BUFSZ1 */
|
---|
| 68 |
|
---|
| 69 | static char_t rcv_buffer1[SERIAL_RCV_BUFSZ1];
|
---|
| 70 | static char_t snd_buffer1[SERIAL_SND_BUFSZ1];
|
---|
| 71 |
|
---|
| 72 | #if TNUM_PORT >= 2 /* |[g2ÉÖ·éè` */
|
---|
| 73 |
|
---|
| 74 | #ifndef SERIAL_RCV_BUFSZ2
|
---|
| 75 | #define SERIAL_RCV_BUFSZ2 256 /* |[g2ÌóMobt@TCY */
|
---|
| 76 | #endif /* SERIAL_RCV_BUFSZ2 */
|
---|
| 77 |
|
---|
| 78 | #ifndef SERIAL_SND_BUFSZ2
|
---|
| 79 | #define SERIAL_SND_BUFSZ2 256 /* |[g2ÌMobt@TCY */
|
---|
| 80 | #endif /* SERIAL_SND_BUFSZ2 */
|
---|
| 81 |
|
---|
| 82 | static char_t rcv_buffer2[SERIAL_RCV_BUFSZ2];
|
---|
| 83 | static char_t snd_buffer2[SERIAL_SND_BUFSZ2];
|
---|
| 84 |
|
---|
| 85 | #endif /* TNUM_PORT >= 2 */
|
---|
| 86 |
|
---|
| 87 | #if TNUM_PORT >= 3 /* |[g3ÉÖ·éè` */
|
---|
| 88 |
|
---|
| 89 | #ifndef SERIAL_RCV_BUFSZ3
|
---|
| 90 | #define SERIAL_RCV_BUFSZ3 256 /* |[g3ÌóMobt@TCY */
|
---|
| 91 | #endif /* SERIAL_RCV_BUFSZ3 */
|
---|
| 92 |
|
---|
| 93 | #ifndef SERIAL_SND_BUFSZ3
|
---|
| 94 | #define SERIAL_SND_BUFSZ3 256 /* |[g3ÌMobt@TCY */
|
---|
| 95 | #endif /* SERIAL_SND_BUFSZ3 */
|
---|
| 96 |
|
---|
| 97 | static char_t rcv_buffer3[SERIAL_RCV_BUFSZ3];
|
---|
| 98 | static char_t snd_buffer3[SERIAL_SND_BUFSZ3];
|
---|
| 99 |
|
---|
| 100 | #endif /* TNUM_PORT >= 3 */
|
---|
| 101 |
|
---|
| 102 | #if TNUM_PORT >= 4 /* |[g4ÉÖ·éè` */
|
---|
| 103 |
|
---|
| 104 | #ifndef SERIAL_RCV_BUFSZ4
|
---|
| 105 | #define SERIAL_RCV_BUFSZ4 256 /* |[g4ÌóMobt@TCY */
|
---|
| 106 | #endif /* SERIAL_RCV_BUFSZ4 */
|
---|
| 107 |
|
---|
| 108 | #ifndef SERIAL_SND_BUFSZ4
|
---|
| 109 | #define SERIAL_SND_BUFSZ4 256 /* |[g4ÌMobt@TCY */
|
---|
| 110 | #endif /* SERIAL_SND_BUFSZ4 */
|
---|
| 111 |
|
---|
| 112 | static char_t rcv_buffer4[SERIAL_RCV_BUFSZ4];
|
---|
| 113 | static char_t snd_buffer4[SERIAL_SND_BUFSZ4];
|
---|
| 114 |
|
---|
| 115 | #endif /* TNUM_PORT >= 4 */
|
---|
| 116 |
|
---|
| 117 | /*
|
---|
| 118 | * t[§äÉÖA·éèÆ}N
|
---|
| 119 | */
|
---|
| 120 | #define FC_STOP '\023' /* Rg[-S */
|
---|
| 121 | #define FC_START '\021' /* Rg[-Q */
|
---|
| 122 |
|
---|
| 123 | #define BUFCNT_STOP(bufsz) ((bufsz) * 3 / 4) /* STOPðéî¶ */
|
---|
| 124 | #define BUFCNT_START(bufsz) ((bufsz) / 2) /* STARTðéî¶ */
|
---|
| 125 |
|
---|
| 126 | /*
|
---|
| 127 | * VA|[gú»ubN
|
---|
| 128 | */
|
---|
| 129 | typedef struct serial_port_initialization_block {
|
---|
| 130 | ID rcv_semid; /* óMobt@ÇpZ}tHÌID */
|
---|
| 131 | ID snd_semid; /* Mobt@ÇpZ}tHÌID */
|
---|
| 132 | uint_t rcv_bufsz; /* óMobt@TCY */
|
---|
| 133 | char_t *rcv_buffer; /* óMobt@ */
|
---|
| 134 | uint_t snd_bufsz; /* Mobt@TCY */
|
---|
| 135 | char_t *snd_buffer; /* Mobt@ */
|
---|
| 136 | } SPINIB;
|
---|
| 137 |
|
---|
| 138 | static const SPINIB spinib_table[TNUM_PORT] = {
|
---|
| 139 | { SERIAL_RCV_SEM1, SERIAL_SND_SEM1,
|
---|
| 140 | SERIAL_RCV_BUFSZ1, rcv_buffer1,
|
---|
| 141 | SERIAL_SND_BUFSZ1, snd_buffer1 },
|
---|
| 142 | #if TNUM_PORT >= 2
|
---|
| 143 | { SERIAL_RCV_SEM2, SERIAL_SND_SEM2,
|
---|
| 144 | SERIAL_RCV_BUFSZ2, rcv_buffer2,
|
---|
| 145 | SERIAL_SND_BUFSZ2, snd_buffer2 },
|
---|
| 146 | #endif /* TNUM_PORT >= 2 */
|
---|
| 147 | #if TNUM_PORT >= 3
|
---|
| 148 | { SERIAL_RCV_SEM3, SERIAL_SND_SEM3,
|
---|
| 149 | SERIAL_RCV_BUFSZ3, rcv_buffer3,
|
---|
| 150 | SERIAL_SND_BUFSZ3, snd_buffer3 },
|
---|
| 151 | #endif /* TNUM_PORT >= 3 */
|
---|
| 152 | #if TNUM_PORT >= 4
|
---|
| 153 | { SERIAL_RCV_SEM4, SERIAL_SND_SEM4,
|
---|
| 154 | SERIAL_RCV_BUFSZ4, rcv_buffer4,
|
---|
| 155 | SERIAL_SND_BUFSZ4, snd_buffer4 },
|
---|
| 156 | #endif /* TNUM_PORT >= 4 */
|
---|
| 157 | };
|
---|
| 158 |
|
---|
| 159 | /*
|
---|
| 160 | * VA|[gÇubN
|
---|
| 161 | */
|
---|
| 162 | typedef struct serial_port_control_block {
|
---|
| 163 | const SPINIB *p_spinib; /* VA|[gú»ubN */
|
---|
| 164 | SIOPCB *p_siopcb; /* VAI/O|[gÇubN */
|
---|
| 165 | bool_t openflag; /* I[vÏÝtO */
|
---|
| 166 | bool_t errorflag; /* G[tO */
|
---|
| 167 | uint_t ioctl; /* ®ì§äÌÝèl */
|
---|
| 168 |
|
---|
| 169 | uint_t rcv_read_ptr; /* óMobt@Çoµ|C^ */
|
---|
| 170 | uint_t rcv_write_ptr; /* óMobt@Ý|C^ */
|
---|
| 171 | uint_t rcv_count; /* óMobt@̶ */
|
---|
| 172 | char_t rcv_fc_chr; /* é׫START/STOP */
|
---|
| 173 | bool_t rcv_stopped; /* STOPðÁ½óÔ©H */
|
---|
| 174 |
|
---|
| 175 | uint_t snd_read_ptr; /* Mobt@Çoµ|C^ */
|
---|
| 176 | uint_t snd_write_ptr; /* Mobt@Ý|C^ */
|
---|
| 177 | uint_t snd_count; /* Mobt@̶ */
|
---|
| 178 | bool_t snd_stopped; /* STOPðó¯æÁ½óÔ©H */
|
---|
| 179 | } SPCB;
|
---|
| 180 |
|
---|
| 181 | static SPCB spcb_table[TNUM_PORT];
|
---|
| 182 |
|
---|
| 183 | /*
|
---|
| 184 | * VA|[gID©çVA|[gÇubNðæèo·½ßÌ}N
|
---|
| 185 | */
|
---|
| 186 | #define INDEX_PORT(portid) ((uint_t)((portid) - 1))
|
---|
| 187 | #define get_spcb(portid) (&(spcb_table[INDEX_PORT(portid)]))
|
---|
| 188 |
|
---|
| 189 | /*
|
---|
| 190 | * |C^ÌCNg
|
---|
| 191 | */
|
---|
| 192 | #define INC_PTR(ptr, bufsz) { if (++(ptr) == (bufsz)) { (ptr) = 0; }}
|
---|
| 193 |
|
---|
| 194 | /*
|
---|
| 195 | * T[rXR[Äoµ}N
|
---|
| 196 | *
|
---|
| 197 | * T[rXR[ÄoµðÜÞ®expð]¿µCÔlªG[iÌljÌê
|
---|
| 198 | * ÉÍCercÉercd_expð]¿µ½lðãüµCerror_exitÉgoto·éD
|
---|
| 199 | */
|
---|
| 200 | #define SVC(exp, ercd_exp) \
|
---|
| 201 | { if ((exp) < 0) { ercd = (ercd_exp); goto error_exit; }}
|
---|
| 202 |
|
---|
| 203 | /*
|
---|
| 204 | * E_SYSG[̶¬
|
---|
| 205 | */
|
---|
| 206 | static ER
|
---|
| 207 | gen_ercd_sys(SPCB *p_spcb)
|
---|
| 208 | {
|
---|
| 209 | p_spcb->errorflag = true;
|
---|
| 210 | return(E_SYS);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /*
|
---|
| 214 | * Ò¿ÉüéT[rXR[©çÌG[ÌÏ·
|
---|
| 215 | */
|
---|
| 216 | static ER
|
---|
| 217 | gen_ercd_wait(ER rercd, SPCB *p_spcb)
|
---|
| 218 | {
|
---|
| 219 | switch (MERCD(rercd)) {
|
---|
| 220 | case E_RLWAI:
|
---|
| 221 | case E_DLT:
|
---|
| 222 | return(rercd);
|
---|
| 223 | default:
|
---|
| 224 | p_spcb->errorflag = true;
|
---|
| 225 | return(E_SYS);
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /*
|
---|
| 230 | * VAC^tF[XhCoÌú»[`
|
---|
| 231 | */
|
---|
| 232 | void
|
---|
| 233 | serial_initialize(intptr_t exinf)
|
---|
| 234 | {
|
---|
| 235 | uint_t i;
|
---|
| 236 | SPCB *p_spcb;
|
---|
| 237 |
|
---|
| 238 | for (p_spcb = spcb_table, i = 0; i < TNUM_PORT; p_spcb++, i++) {
|
---|
| 239 | p_spcb->p_spinib = &(spinib_table[i]);
|
---|
| 240 | p_spcb->openflag = false;
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /*
|
---|
| 245 | * VA|[gÌI[viT[rXR[j
|
---|
| 246 | */
|
---|
| 247 | ER
|
---|
| 248 | serial_opn_por(ID portid)
|
---|
| 249 | {
|
---|
| 250 | SPCB *p_spcb;
|
---|
| 251 | ER ercd;
|
---|
| 252 |
|
---|
| 253 | if (sns_dpn()) { /* ReLXgÌ`FbN */
|
---|
| 254 | return(E_CTX);
|
---|
| 255 | }
|
---|
| 256 | if (!(1 <= portid && portid <= TNUM_PORT)) {
|
---|
| 257 | return(E_ID); /* |[gÔÌ`FbN */
|
---|
| 258 | }
|
---|
| 259 | p_spcb = get_spcb(portid);
|
---|
| 260 |
|
---|
| 261 | SVC(dis_dsp(), gen_ercd_sys(p_spcb));
|
---|
| 262 | if (p_spcb->openflag) { /* I[vÏÝ©Ì`FbN */
|
---|
| 263 | ercd = E_OBJ;
|
---|
| 264 | }
|
---|
| 265 | else {
|
---|
| 266 | /*
|
---|
| 267 | * ÏÌú»
|
---|
| 268 | */
|
---|
| 269 | p_spcb->ioctl = (IOCTL_ECHO | IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV);
|
---|
| 270 |
|
---|
| 271 | p_spcb->rcv_read_ptr = p_spcb->rcv_write_ptr = 0U;
|
---|
| 272 | p_spcb->rcv_count = 0U;
|
---|
| 273 | p_spcb->rcv_fc_chr = '\0';
|
---|
| 274 | p_spcb->rcv_stopped = false;
|
---|
| 275 |
|
---|
| 276 | p_spcb->snd_read_ptr = p_spcb->snd_write_ptr = 0U;
|
---|
| 277 | p_spcb->snd_count = 0U;
|
---|
| 278 | p_spcb->snd_stopped = false;
|
---|
| 279 |
|
---|
| 280 | /*
|
---|
| 281 | * ±êÈ~CÝðÖ~·éD
|
---|
| 282 | */
|
---|
| 283 | if (loc_cpu() < 0) {
|
---|
| 284 | ercd = E_SYS;
|
---|
| 285 | goto error_exit_enadsp;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | /*
|
---|
| 289 | * n[hEFA˶ÌI[v
|
---|
| 290 | */
|
---|
| 291 | p_spcb->p_siopcb = sio_opn_por(portid, (intptr_t) p_spcb);
|
---|
| 292 |
|
---|
| 293 | /*
|
---|
| 294 | * óMÊmR[obNð·éD
|
---|
| 295 | */
|
---|
| 296 | sio_ena_cbr(p_spcb->p_siopcb, SIO_RDY_RCV);
|
---|
| 297 | p_spcb->openflag = true;
|
---|
| 298 | p_spcb->errorflag = false;
|
---|
| 299 |
|
---|
| 300 | if (unl_cpu() < 0) {
|
---|
| 301 | p_spcb->errorflag = true;
|
---|
| 302 | ercd = E_SYS;
|
---|
| 303 | goto error_exit_enadsp;
|
---|
| 304 | }
|
---|
| 305 | ercd = E_OK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | error_exit_enadsp:
|
---|
| 309 | SVC(ena_dsp(), gen_ercd_sys(p_spcb));
|
---|
| 310 |
|
---|
| 311 | error_exit:
|
---|
| 312 | return(ercd);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /*
|
---|
| 316 | * VA|[gÌN[YiT[rXR[j
|
---|
| 317 | */
|
---|
| 318 | ER
|
---|
| 319 | serial_cls_por(ID portid)
|
---|
| 320 | {
|
---|
| 321 | SPCB *p_spcb;
|
---|
| 322 | ER ercd;
|
---|
| 323 | bool_t eflag = false;
|
---|
| 324 |
|
---|
| 325 | if (sns_dpn()) { /* ReLXgÌ`FbN */
|
---|
| 326 | return(E_CTX);
|
---|
| 327 | }
|
---|
| 328 | if (!(1 <= portid && portid <= TNUM_PORT)) {
|
---|
| 329 | return(E_ID); /* |[gÔÌ`FbN */
|
---|
| 330 | }
|
---|
| 331 | p_spcb = get_spcb(portid);
|
---|
| 332 |
|
---|
| 333 | SVC(dis_dsp(), gen_ercd_sys(p_spcb));
|
---|
| 334 | if (!(p_spcb->openflag)) { /* I[vÏÝ©Ì`FbN */
|
---|
| 335 | ercd = E_OBJ;
|
---|
| 336 | }
|
---|
| 337 | else {
|
---|
| 338 | /*
|
---|
| 339 | * n[hEFA˶ÌN[Y
|
---|
| 340 | */
|
---|
| 341 | if (loc_cpu() < 0) {
|
---|
| 342 | eflag = true;
|
---|
| 343 | }
|
---|
| 344 | sio_cls_por(p_spcb->p_siopcb);
|
---|
| 345 | p_spcb->openflag = false;
|
---|
| 346 | if (unl_cpu() < 0) {
|
---|
| 347 | eflag = true;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | /*
|
---|
| 351 | * Z}tHÌú»
|
---|
| 352 | */
|
---|
| 353 | if (ini_sem(p_spcb->p_spinib->snd_semid) < 0) {
|
---|
| 354 | eflag = true;
|
---|
| 355 | }
|
---|
| 356 | if (ini_sem(p_spcb->p_spinib->rcv_semid) < 0) {
|
---|
| 357 | eflag = true;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | /*
|
---|
| 361 | * G[R[hÌÝè
|
---|
| 362 | */
|
---|
| 363 | if (eflag) {
|
---|
| 364 | ercd = gen_ercd_sys(p_spcb);
|
---|
| 365 | }
|
---|
| 366 | else {
|
---|
| 367 | ercd = E_OK;
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 | SVC(ena_dsp(), gen_ercd_sys(p_spcb));
|
---|
| 371 |
|
---|
| 372 | error_exit:
|
---|
| 373 | return(ercd);
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /*
|
---|
| 377 | * VA|[gÖ̶M
|
---|
| 378 | *
|
---|
| 379 | * p_spcbÅwè³êéVAI/O|[gÉεÄC¶cðM·éD¶
|
---|
| 380 | * ðMWX^É¢ê½êÉÍtrueðÔ·D»¤ÅÈ¢êÉÍCM
|
---|
| 381 | * WX^ªó¢½±ÆðÊm·éR[obNÖðµCfalseðÔ·D
|
---|
| 382 | * ±ÌÖÍCCPUbNóÔÅÄÑo³êéD
|
---|
| 383 | */
|
---|
| 384 | Inline bool_t
|
---|
| 385 | serial_snd_chr(SPCB *p_spcb, char_t c)
|
---|
| 386 | {
|
---|
| 387 | if (sio_snd_chr(p_spcb->p_siopcb, c)) {
|
---|
| 388 | return(true);
|
---|
| 389 | }
|
---|
| 390 | else {
|
---|
| 391 | sio_ena_cbr(p_spcb->p_siopcb, SIO_RDY_SND);
|
---|
| 392 | return(false);
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | /*
|
---|
| 397 | * VA|[gÖÌ1¶M
|
---|
| 398 | */
|
---|
| 399 | static ER_BOOL
|
---|
| 400 | serial_wri_chr(SPCB *p_spcb, char_t c)
|
---|
| 401 | {
|
---|
| 402 | bool_t buffer_full;
|
---|
| 403 | ER ercd, rercd;
|
---|
| 404 |
|
---|
| 405 | /*
|
---|
| 406 | * LFÌOÉCRðM·éD
|
---|
| 407 | */
|
---|
| 408 | if (c == '\n' && (p_spcb->ioctl & IOCTL_CRLF) != 0U) {
|
---|
| 409 | SVC(rercd = serial_wri_chr(p_spcb, '\r'), rercd);
|
---|
| 410 | if ((bool_t) rercd) {
|
---|
| 411 | SVC(rercd = wai_sem(p_spcb->p_spinib->snd_semid),
|
---|
| 412 | gen_ercd_wait(rercd, p_spcb));
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | SVC(loc_cpu(), gen_ercd_sys(p_spcb));
|
---|
| 417 | if (p_spcb->snd_count == 0U && !(p_spcb->snd_stopped)
|
---|
| 418 | && serial_snd_chr(p_spcb, c)) {
|
---|
| 419 | /*
|
---|
| 420 | * VAI/OfoCXÌMWX^ɶðüêé±Æɬ÷µ
|
---|
| 421 | * ½êD
|
---|
| 422 | */
|
---|
| 423 | buffer_full = false;
|
---|
| 424 | }
|
---|
| 425 | else {
|
---|
| 426 | /*
|
---|
| 427 | * Mobt@ɶðüêéD
|
---|
| 428 | */
|
---|
| 429 | p_spcb->p_spinib->snd_buffer[p_spcb->snd_write_ptr] = c;
|
---|
| 430 | INC_PTR(p_spcb->snd_write_ptr, p_spcb->p_spinib->snd_bufsz);
|
---|
| 431 | p_spcb->snd_count++;
|
---|
| 432 | buffer_full = (p_spcb->snd_count == p_spcb->p_spinib->snd_bufsz);
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | SVC(unl_cpu(), gen_ercd_sys(p_spcb));
|
---|
| 436 | ercd = (ER_BOOL) buffer_full;
|
---|
| 437 |
|
---|
| 438 | error_exit:
|
---|
| 439 | return(ercd);
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | /*
|
---|
| 443 | * VA|[gÖ̶ñMiT[rXR[j
|
---|
| 444 | */
|
---|
| 445 | ER_UINT
|
---|
| 446 | serial_wri_dat(ID portid, const char_t *buf, uint_t len)
|
---|
| 447 | {
|
---|
| 448 | SPCB *p_spcb;
|
---|
| 449 | bool_t buffer_full;
|
---|
| 450 | uint_t wricnt = 0U;
|
---|
| 451 | ER ercd, rercd;
|
---|
| 452 |
|
---|
| 453 | if (sns_dpn()) { /* ReLXgÌ`FbN */
|
---|
| 454 | return(E_CTX);
|
---|
| 455 | }
|
---|
| 456 | if (!(1 <= portid && portid <= TNUM_PORT)) {
|
---|
| 457 | return(E_ID); /* |[gÔÌ`FbN */
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | p_spcb = get_spcb(portid);
|
---|
| 461 | if (!(p_spcb->openflag)) { /* I[vÏÝ©Ì`FbN */
|
---|
| 462 | return(E_OBJ);
|
---|
| 463 | }
|
---|
| 464 | if (p_spcb->errorflag) { /* G[óÔ©Ì`FbN */
|
---|
| 465 | return(E_SYS);
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | buffer_full = true; /* [vÌ1ñßÍwai_sem·é */
|
---|
| 469 | while (wricnt < len) {
|
---|
| 470 | if (buffer_full) {
|
---|
| 471 | SVC(rercd = wai_sem(p_spcb->p_spinib->snd_semid),
|
---|
| 472 | gen_ercd_wait(rercd, p_spcb));
|
---|
| 473 | }
|
---|
| 474 | SVC(rercd = serial_wri_chr(p_spcb, *buf++), rercd);
|
---|
| 475 | wricnt++;
|
---|
| 476 | buffer_full = (bool_t) rercd;
|
---|
| 477 | }
|
---|
| 478 | if (!buffer_full) {
|
---|
| 479 | SVC(sig_sem(p_spcb->p_spinib->snd_semid), gen_ercd_sys(p_spcb));
|
---|
| 480 | }
|
---|
| 481 | ercd = E_OK;
|
---|
| 482 |
|
---|
| 483 | error_exit:
|
---|
| 484 | return(wricnt > 0U ? (ER_UINT) wricnt : ercd);
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | /*
|
---|
| 488 | * VA|[g©çÌ1¶óM
|
---|
| 489 | */
|
---|
| 490 | static bool_t
|
---|
| 491 | serial_rea_chr(SPCB *p_spcb, char_t *p_c)
|
---|
| 492 | {
|
---|
| 493 | bool_t buffer_empty;
|
---|
| 494 | ER ercd;
|
---|
| 495 |
|
---|
| 496 | SVC(loc_cpu(), gen_ercd_sys(p_spcb));
|
---|
| 497 |
|
---|
| 498 | /*
|
---|
| 499 | * óMobt@©ç¶ðæèo·D
|
---|
| 500 | */
|
---|
| 501 | *p_c = p_spcb->p_spinib->rcv_buffer[p_spcb->rcv_read_ptr];
|
---|
| 502 | INC_PTR(p_spcb->rcv_read_ptr, p_spcb->p_spinib->rcv_bufsz);
|
---|
| 503 | p_spcb->rcv_count--;
|
---|
| 504 | buffer_empty = (p_spcb->rcv_count == 0U);
|
---|
| 505 |
|
---|
| 506 | /*
|
---|
| 507 | * STARTðM·éD
|
---|
| 508 | */
|
---|
| 509 | if (p_spcb->rcv_stopped && p_spcb->rcv_count
|
---|
| 510 | <= BUFCNT_START(p_spcb->p_spinib->rcv_bufsz)) {
|
---|
| 511 | if (!serial_snd_chr(p_spcb, FC_START)) {
|
---|
| 512 | p_spcb->rcv_fc_chr = FC_START;
|
---|
| 513 | }
|
---|
| 514 | p_spcb->rcv_stopped = false;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | SVC(unl_cpu(), gen_ercd_sys(p_spcb));
|
---|
| 518 | ercd = (ER_BOOL) buffer_empty;
|
---|
| 519 |
|
---|
| 520 | error_exit:
|
---|
| 521 | return(ercd);
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | /*
|
---|
| 525 | * VA|[g©ç̶ñóMiT[rXR[j
|
---|
| 526 | */
|
---|
| 527 | ER_UINT
|
---|
| 528 | serial_rea_dat(ID portid, char_t *buf, uint_t len)
|
---|
| 529 | {
|
---|
| 530 | SPCB *p_spcb;
|
---|
| 531 | bool_t buffer_empty;
|
---|
| 532 | uint_t reacnt = 0U;
|
---|
| 533 | char_t c = '\0'; /* RpCÌxð}~·é½ßÉú»·é */
|
---|
| 534 | ER ercd, rercd;
|
---|
| 535 |
|
---|
| 536 | if (sns_dpn()) { /* ReLXgÌ`FbN */
|
---|
| 537 | return(E_CTX);
|
---|
| 538 | }
|
---|
| 539 | if (!(1 <= portid && portid <= TNUM_PORT)) {
|
---|
| 540 | return(E_ID); /* |[gÔÌ`FbN */
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | p_spcb = get_spcb(portid);
|
---|
| 544 | if (!(p_spcb->openflag)) { /* I[vÏÝ©Ì`FbN */
|
---|
| 545 | return(E_OBJ);
|
---|
| 546 | }
|
---|
| 547 | if (p_spcb->errorflag) { /* G[óÔ©Ì`FbN */
|
---|
| 548 | return(E_SYS);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | buffer_empty = true; /* [vÌ1ñßÍwai_sem·é */
|
---|
| 552 | while (reacnt < len) {
|
---|
| 553 | if (buffer_empty) {
|
---|
| 554 | SVC(rercd = wai_sem(p_spcb->p_spinib->rcv_semid),
|
---|
| 555 | gen_ercd_wait(rercd, p_spcb));
|
---|
| 556 | }
|
---|
| 557 | SVC(rercd = serial_rea_chr(p_spcb, &c), rercd);
|
---|
| 558 | *buf++ = c;
|
---|
| 559 | reacnt++;
|
---|
| 560 | buffer_empty = (bool_t) rercd;
|
---|
| 561 |
|
---|
| 562 | /*
|
---|
| 563 | * GR[obND
|
---|
| 564 | */
|
---|
| 565 | if ((p_spcb->ioctl & IOCTL_ECHO) != 0U) {
|
---|
| 566 | SVC(rercd = wai_sem(p_spcb->p_spinib->snd_semid),
|
---|
| 567 | gen_ercd_wait(rercd, p_spcb));
|
---|
| 568 | SVC(rercd = serial_wri_chr(p_spcb, c), rercd);
|
---|
| 569 | if (!((bool_t) rercd)) {
|
---|
| 570 | SVC(sig_sem(p_spcb->p_spinib->snd_semid),
|
---|
| 571 | gen_ercd_sys(p_spcb));
|
---|
| 572 | }
|
---|
| 573 | }
|
---|
| 574 | }
|
---|
| 575 | if (!buffer_empty) {
|
---|
| 576 | SVC(sig_sem(p_spcb->p_spinib->rcv_semid), gen_ercd_sys(p_spcb));
|
---|
| 577 | }
|
---|
| 578 | ercd = E_OK;
|
---|
| 579 |
|
---|
| 580 | error_exit:
|
---|
| 581 | return(reacnt > 0U ? (ER_UINT) reacnt : ercd);
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | /*
|
---|
| 585 | * VA|[ģäiT[rXR[j
|
---|
| 586 | */
|
---|
| 587 | ER
|
---|
| 588 | serial_ctl_por(ID portid, uint_t ioctl)
|
---|
| 589 | {
|
---|
| 590 | SPCB *p_spcb;
|
---|
| 591 |
|
---|
| 592 | if (sns_dpn()) { /* ReLXgÌ`FbN */
|
---|
| 593 | return(E_CTX);
|
---|
| 594 | }
|
---|
| 595 | if (!(1 <= portid && portid <= TNUM_PORT)) {
|
---|
| 596 | return(E_ID); /* |[gÔÌ`FbN */
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | p_spcb = get_spcb(portid);
|
---|
| 600 | if (!(p_spcb->openflag)) { /* I[vÏÝ©Ì`FbN */
|
---|
| 601 | return(E_OBJ);
|
---|
| 602 | }
|
---|
| 603 | if (p_spcb->errorflag) { /* G[óÔ©Ì`FbN */
|
---|
| 604 | return(E_SYS);
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | p_spcb->ioctl = ioctl;
|
---|
| 608 | return(E_OK);
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | /*
|
---|
| 612 | * VA|[góÔÌQÆiT[rXR[j
|
---|
| 613 | */
|
---|
| 614 | ER
|
---|
| 615 | serial_ref_por(ID portid, T_SERIAL_RPOR *pk_rpor)
|
---|
| 616 | {
|
---|
| 617 | SPCB *p_spcb;
|
---|
| 618 |
|
---|
| 619 | if (sns_dpn()) { /* ReLXgÌ`FbN */
|
---|
| 620 | return(E_CTX);
|
---|
| 621 | }
|
---|
| 622 | if (!(1 <= portid && portid <= TNUM_PORT)) {
|
---|
| 623 | return(E_ID); /* |[gÔÌ`FbN */
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | p_spcb = get_spcb(portid);
|
---|
| 627 | if (!(p_spcb->openflag)) { /* I[vÏÝ©Ì`FbN */
|
---|
| 628 | return(E_OBJ);
|
---|
| 629 | }
|
---|
| 630 | if (p_spcb->errorflag) { /* G[óÔ©Ì`FbN */
|
---|
| 631 | return(E_SYS);
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | pk_rpor->reacnt = p_spcb->rcv_count;
|
---|
| 635 | pk_rpor->wricnt = p_spcb->snd_count;
|
---|
| 636 | return(E_OK);
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | /*
|
---|
| 640 | * VA|[g©çÌMÂ\R[obN
|
---|
| 641 | */
|
---|
| 642 | void
|
---|
| 643 | sio_irdy_snd(intptr_t exinf)
|
---|
| 644 | {
|
---|
| 645 | SPCB *p_spcb;
|
---|
| 646 |
|
---|
| 647 | p_spcb = (SPCB *) exinf;
|
---|
| 648 | if (p_spcb->rcv_fc_chr != '\0') {
|
---|
| 649 | /*
|
---|
| 650 | * START/STOP ðM·éD
|
---|
| 651 | */
|
---|
| 652 | (void) sio_snd_chr(p_spcb->p_siopcb, p_spcb->rcv_fc_chr);
|
---|
| 653 | p_spcb->rcv_fc_chr = '\0';
|
---|
| 654 | }
|
---|
| 655 | else if (!(p_spcb->snd_stopped) && p_spcb->snd_count > 0U) {
|
---|
| 656 | /*
|
---|
| 657 | * Mobt@©ç¶ðæèoµÄM·éD
|
---|
| 658 | */
|
---|
| 659 | (void) sio_snd_chr(p_spcb->p_siopcb,
|
---|
| 660 | p_spcb->p_spinib->snd_buffer[p_spcb->snd_read_ptr]);
|
---|
| 661 | INC_PTR(p_spcb->snd_read_ptr, p_spcb->p_spinib->snd_bufsz);
|
---|
| 662 | if (p_spcb->snd_count == p_spcb->p_spinib->snd_bufsz) {
|
---|
| 663 | if (isig_sem(p_spcb->p_spinib->snd_semid) < 0) {
|
---|
| 664 | p_spcb->errorflag = true;
|
---|
| 665 | }
|
---|
| 666 | }
|
---|
| 667 | p_spcb->snd_count--;
|
---|
| 668 | }
|
---|
| 669 | else {
|
---|
| 670 | /*
|
---|
| 671 | * M·×«¶ªÈ¢êÍCMÂ\R[obNðÖ~·éD
|
---|
| 672 | */
|
---|
| 673 | sio_dis_cbr(p_spcb->p_siopcb, SIO_RDY_SND);
|
---|
| 674 | }
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 | /*
|
---|
| 678 | * VA|[g©çÌóMÊmR[obN
|
---|
| 679 | */
|
---|
| 680 | void
|
---|
| 681 | sio_irdy_rcv(intptr_t exinf)
|
---|
| 682 | {
|
---|
| 683 | SPCB *p_spcb;
|
---|
| 684 | char_t c;
|
---|
| 685 |
|
---|
| 686 | p_spcb = (SPCB *) exinf;
|
---|
| 687 | c = (char_t) sio_rcv_chr(p_spcb->p_siopcb);
|
---|
| 688 | if ((p_spcb->ioctl & IOCTL_FCSND) != 0U && c == FC_STOP) {
|
---|
| 689 | /*
|
---|
| 690 | * Mðêâ~·éDM̶ͻÌÜÜM·éD
|
---|
| 691 | */
|
---|
| 692 | p_spcb->snd_stopped = true;
|
---|
| 693 | }
|
---|
| 694 | else if (p_spcb->snd_stopped && (c == FC_START
|
---|
| 695 | || (p_spcb->ioctl & IOCTL_FCANY) != 0U)) {
|
---|
| 696 | /*
|
---|
| 697 | * MðÄJ·éD
|
---|
| 698 | */
|
---|
| 699 | p_spcb->snd_stopped = false;
|
---|
| 700 | if (p_spcb->snd_count > 0U) {
|
---|
| 701 | c = p_spcb->p_spinib->snd_buffer[p_spcb->snd_read_ptr];
|
---|
| 702 | if (serial_snd_chr(p_spcb, c)) {
|
---|
| 703 | INC_PTR(p_spcb->snd_read_ptr, p_spcb->p_spinib->snd_bufsz);
|
---|
| 704 | if (p_spcb->snd_count == p_spcb->p_spinib->snd_bufsz) {
|
---|
| 705 | if (isig_sem(p_spcb->p_spinib->snd_semid) < 0) {
|
---|
| 706 | p_spcb->errorflag = true;
|
---|
| 707 | }
|
---|
| 708 | }
|
---|
| 709 | p_spcb->snd_count--;
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
| 713 | else if ((p_spcb->ioctl & IOCTL_FCSND) != 0U && c == FC_START) {
|
---|
| 714 | /*
|
---|
| 715 | * MÉεÄt[§äµÄ¢éêCSTART ÍÌÄéD
|
---|
| 716 | */
|
---|
| 717 | }
|
---|
| 718 | else if (p_spcb->rcv_count == p_spcb->p_spinib->rcv_bufsz) {
|
---|
| 719 | /*
|
---|
| 720 | * obt@tÌêCóMµ½¶ðÌÄéD
|
---|
| 721 | */
|
---|
| 722 | }
|
---|
| 723 | else {
|
---|
| 724 | /*
|
---|
| 725 | * óMµ½¶ðóMobt@ÉüêéD
|
---|
| 726 | */
|
---|
| 727 | p_spcb->p_spinib->rcv_buffer[p_spcb->rcv_write_ptr] = c;
|
---|
| 728 | INC_PTR(p_spcb->rcv_write_ptr, p_spcb->p_spinib->rcv_bufsz);
|
---|
| 729 | if (p_spcb->rcv_count == 0U) {
|
---|
| 730 | if (isig_sem(p_spcb->p_spinib->rcv_semid) < 0) {
|
---|
| 731 | p_spcb->errorflag = true;
|
---|
| 732 | }
|
---|
| 733 | }
|
---|
| 734 | p_spcb->rcv_count++;
|
---|
| 735 |
|
---|
| 736 | /*
|
---|
| 737 | * STOPðM·éD
|
---|
| 738 | */
|
---|
| 739 | if ((p_spcb->ioctl & IOCTL_FCRCV) != 0U && !(p_spcb->rcv_stopped)
|
---|
| 740 | && p_spcb->rcv_count
|
---|
| 741 | >= BUFCNT_STOP(p_spcb->p_spinib->rcv_bufsz)) {
|
---|
| 742 | if (!serial_snd_chr(p_spcb, FC_STOP)) {
|
---|
| 743 | p_spcb->rcv_fc_chr = FC_STOP;
|
---|
| 744 | }
|
---|
| 745 | p_spcb->rcv_stopped = true;
|
---|
| 746 | }
|
---|
| 747 | }
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | /*
|
---|
| 751 | * VAC^tF[XhCo©çÌ¢M¶Ìæoµ
|
---|
| 752 | */
|
---|
| 753 | bool_t
|
---|
| 754 | serial_get_chr(ID portid, char_t *p_c)
|
---|
| 755 | {
|
---|
| 756 | SPCB *p_spcb;
|
---|
| 757 |
|
---|
| 758 | if (1 <= portid && portid <= TNUM_PORT) { /* |[gÔÌ`FbN */
|
---|
| 759 | p_spcb = get_spcb(portid);
|
---|
| 760 | if (p_spcb->openflag) { /* I[vÏÝ©Ì`FbN */
|
---|
| 761 | if (p_spcb->snd_count > 0U) {
|
---|
| 762 | *p_c = p_spcb->p_spinib->snd_buffer[p_spcb->snd_read_ptr];
|
---|
| 763 | INC_PTR(p_spcb->snd_read_ptr, p_spcb->p_spinib->snd_bufsz);
|
---|
| 764 | p_spcb->snd_count--;
|
---|
| 765 | return(true);
|
---|
| 766 | }
|
---|
| 767 | }
|
---|
| 768 | }
|
---|
| 769 | return(false);
|
---|
| 770 | }
|
---|