[337] | 1 | /*
|
---|
| 2 | * TOPPERS/ASP Kernel
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
| 4 | * Advanced Standard Profile Kernel
|
---|
[387] | 5 | *
|
---|
| 6 | * Copyright (C) 2006-2018 by Embedded and Real-Time Systems Laboratory
|
---|
| 7 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 8 | *
|
---|
[337] | 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 | * 免責すること.
|
---|
[387] | 31 | *
|
---|
[337] | 32 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 33 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 34 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 35 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 36 | * の責任を負わない.
|
---|
[387] | 37 | *
|
---|
[337] | 38 | * @(#) $Id$
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | /*
|
---|
[387] | 42 | * シリアルインタフェースドライバのターゲット依存部(GR-CITRUS用)
|
---|
[337] | 43 | */
|
---|
[387] | 44 |
|
---|
| 45 | #include "gr_citrus.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>
|
---|
[337] | 52 | #include "target_serial.h"
|
---|
[387] | 53 | #include "syssvc/siofd.h"
|
---|
[337] | 54 |
|
---|
| 55 | /*
|
---|
[387] | 56 | * SIOポート初期化ブロックの定義
|
---|
[374] | 57 | */
|
---|
| 58 | typedef struct sio_port_initialization_block {
|
---|
[387] | 59 | char *path; /* ファイルのパス名 */
|
---|
[374] | 60 | } SIOPINIB;
|
---|
| 61 |
|
---|
| 62 | /*
|
---|
[387] | 63 | * SIOポート管理ブロックの定義
|
---|
[374] | 64 | */
|
---|
| 65 | struct sio_port_control_block {
|
---|
[387] | 66 | const SIOPINIB *p_siopinib; /* SIOポート初期化ブロック */
|
---|
| 67 | intptr_t exinf; /* 拡張情報 */
|
---|
| 68 | bool_t opened; /* オープン済みフラグ */
|
---|
| 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; /* 送信通知コールバック許可フラグ */
|
---|
[374] | 80 | };
|
---|
| 81 |
|
---|
| 82 | /*
|
---|
[387] | 83 | * SIOポート初期化ブロック
|
---|
[374] | 84 | */
|
---|
[387] | 85 | const SIOPINIB siopinib_table[TNUM_SIOP] = {
|
---|
| 86 | { NULL }
|
---|
[374] | 87 | };
|
---|
| 88 |
|
---|
| 89 | /*
|
---|
[387] | 90 | * SIOポート管理ブロックのエリア
|
---|
[374] | 91 | */
|
---|
[387] | 92 | SIOPCB siopcb_table[TNUM_SIOP];
|
---|
[374] | 93 |
|
---|
[387] | 94 | /*
|
---|
| 95 | * SIOポートIDから管理ブロックを取り出すためのマクロ
|
---|
| 96 | */
|
---|
| 97 | #define INDEX_SIOP(siopid) ((uint_t)((siopid) - 1))
|
---|
| 98 | #define get_siopcb(siopid) (&(siopcb_table[INDEX_SIOP(siopid)]))
|
---|
[374] | 99 |
|
---|
| 100 | /*
|
---|
[337] | 101 | * SIOドライバの初期化
|
---|
| 102 | */
|
---|
| 103 | void
|
---|
| 104 | sio_initialize(intptr_t exinf)
|
---|
| 105 | {
|
---|
[374] | 106 | SIOPCB *p_siopcb;
|
---|
| 107 | uint_t i;
|
---|
| 108 |
|
---|
| 109 | /*
|
---|
[387] | 110 | * SIOポート管理ブロックの初期化
|
---|
[374] | 111 | */
|
---|
[387] | 112 | for (i = 0; i < TNUM_SIOP; i++) {
|
---|
| 113 | p_siopcb = &(siopcb_table[i]);
|
---|
[374] | 114 | p_siopcb->p_siopinib = &(siopinib_table[i]);
|
---|
[387] | 115 | p_siopcb->opened = false;
|
---|
[374] | 116 | }
|
---|
[337] | 117 | }
|
---|
| 118 |
|
---|
| 119 | /*
|
---|
[387] | 120 | * SIOドライバの終了処理
|
---|
[337] | 121 | */
|
---|
[387] | 122 | void
|
---|
| 123 | sio_terminate(intptr_t exinf)
|
---|
| 124 | {
|
---|
| 125 | uint_t i;
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 128 | * オープンされているSIOポートのクローズ
|
---|
| 129 | */
|
---|
| 130 | for (i = 0; i < TNUM_SIOP; i++) {
|
---|
| 131 | sio_cls_por(&(siopcb_table[i]));
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /*
|
---|
| 136 | * SIOポートのオープン
|
---|
| 137 | */
|
---|
[337] | 138 | SIOPCB *
|
---|
| 139 | sio_opn_por(ID siopid, intptr_t exinf)
|
---|
| 140 | {
|
---|
[387] | 141 | SIOPCB *p_siopcb;
|
---|
| 142 | const SIOPINIB *p_siopinib;
|
---|
| 143 | int_t fd;
|
---|
| 144 | struct termios term;
|
---|
[337] | 145 |
|
---|
[374] | 146 | p_siopcb = get_siopcb(siopid);
|
---|
[387] | 147 | p_siopinib = p_siopcb->p_siopinib;
|
---|
[337] | 148 |
|
---|
[387] | 149 | if (!(p_siopcb->opened)) {
|
---|
| 150 | if (p_siopinib->path != NULL) {
|
---|
| 151 | fd = siofd_open(p_siopinib->path, O_RDWR, 0777);
|
---|
| 152 | assert(fd >= 0);
|
---|
| 153 | p_siopcb->read_fd = fd;
|
---|
| 154 | p_siopcb->write_fd = fd;
|
---|
| 155 | }
|
---|
| 156 | else {
|
---|
| 157 | fd = STDIN_FILENO; /* 標準入出力を使う */
|
---|
| 158 | p_siopcb->read_fd = STDIN_FILENO;
|
---|
| 159 | p_siopcb->write_fd = STDOUT_FILENO;
|
---|
| 160 | }
|
---|
| 161 | siofd_fcntl(fd, F_SETOWN, getpid());
|
---|
| 162 | siofd_fcntl(fd, F_SETFL, (O_NONBLOCK | O_ASYNC));
|
---|
[374] | 163 |
|
---|
[387] | 164 | siofd_tcgetattr(fd, &(p_siopcb->saved_term));
|
---|
| 165 | term = p_siopcb->saved_term;
|
---|
| 166 | term.c_lflag &= ~(ECHO | ICANON);
|
---|
| 167 | siofd_tcsetattr(fd, TCSAFLUSH, &term);
|
---|
[374] | 168 |
|
---|
[387] | 169 | p_siopcb->exinf = exinf;
|
---|
| 170 | p_siopcb->rcv_flag = false;
|
---|
| 171 | p_siopcb->rcv_rdy = false;
|
---|
| 172 | p_siopcb->snd_flag = false;
|
---|
| 173 | p_siopcb->snd_rdy = false;
|
---|
| 174 | p_siopcb->opened = true;
|
---|
| 175 | }
|
---|
| 176 | return(p_siopcb);
|
---|
[337] | 177 | }
|
---|
| 178 |
|
---|
| 179 | /*
|
---|
[387] | 180 | * SIOポートのクローズ
|
---|
[337] | 181 | */
|
---|
| 182 | void
|
---|
| 183 | sio_cls_por(SIOPCB *p_siopcb)
|
---|
| 184 | {
|
---|
[387] | 185 | int_t fd;
|
---|
[337] | 186 |
|
---|
[387] | 187 | if (p_siopcb->opened) {
|
---|
| 188 | fd = p_siopcb->read_fd;
|
---|
| 189 | siofd_tcsetattr(fd, TCSAFLUSH, &(p_siopcb->saved_term));
|
---|
| 190 | siofd_fcntl(fd, F_SETFL, 0);
|
---|
| 191 |
|
---|
| 192 | if (p_siopcb->p_siopinib->path != NULL) {
|
---|
| 193 | siofd_close(p_siopcb->read_fd);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | p_siopcb->opened = false;
|
---|
| 197 | }
|
---|
[337] | 198 | }
|
---|
| 199 |
|
---|
| 200 | /*
|
---|
[387] | 201 | * SIOの割込みサービスルーチン
|
---|
[337] | 202 | */
|
---|
| 203 | bool_t
|
---|
[387] | 204 | sio_isr_snd(ID siopid)
|
---|
| 205 | {
|
---|
| 206 | SIOPCB *p_siopcb = get_siopcb(siopid);
|
---|
| 207 | int_t n;
|
---|
| 208 |
|
---|
| 209 | if (p_siopcb->snd_flag) {
|
---|
| 210 | if ((n = siofd_write(p_siopcb->write_fd, &(p_siopcb->snd_buf), 1)) > 0) {
|
---|
| 211 | p_siopcb->snd_flag = false;
|
---|
| 212 | if (p_siopcb->snd_rdy) {
|
---|
| 213 | sio_irdy_snd(p_siopcb->exinf);
|
---|
| 214 | return p_siopcb->snd_rdy;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | return false;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /*
|
---|
| 223 | * SIOの割込みサービスルーチン
|
---|
| 224 | */
|
---|
| 225 | bool_t
|
---|
| 226 | sio_isr_rcv(ID siopid, char c)
|
---|
| 227 | {
|
---|
| 228 | SIOPCB *p_siopcb = get_siopcb(siopid);
|
---|
| 229 |
|
---|
| 230 | if (!p_siopcb->rcv_flag) {
|
---|
| 231 | p_siopcb->rcv_buf = c;
|
---|
| 232 | p_siopcb->rcv_flag = true;
|
---|
| 233 | if (p_siopcb->rcv_rdy) {
|
---|
| 234 | sio_irdy_rcv(p_siopcb->exinf);
|
---|
| 235 | return p_siopcb->rcv_rdy;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | return false;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /*
|
---|
| 243 | * SIOポートへの文字送信
|
---|
| 244 | */
|
---|
| 245 | bool_t
|
---|
[374] | 246 | sio_snd_chr(SIOPCB *p_siopcb, char c)
|
---|
[337] | 247 | {
|
---|
[387] | 248 | int_t n;
|
---|
| 249 |
|
---|
| 250 | if (!p_siopcb->snd_flag) {
|
---|
| 251 | if ((n = siofd_write(p_siopcb->write_fd, &c, 1)) > 0) {
|
---|
| 252 | return(true);
|
---|
| 253 | }
|
---|
| 254 | else {
|
---|
| 255 | assert(n < 0 && errno == EAGAIN);
|
---|
| 256 | p_siopcb->snd_flag = true;
|
---|
| 257 | p_siopcb->snd_buf = c;
|
---|
| 258 | return(true);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | else {
|
---|
| 262 | return(false);
|
---|
| 263 | }
|
---|
[337] | 264 | }
|
---|
| 265 |
|
---|
| 266 | /*
|
---|
[387] | 267 | * SIOポートからの文字受信
|
---|
[337] | 268 | */
|
---|
| 269 | int_t
|
---|
[374] | 270 | sio_rcv_chr(SIOPCB *p_siopcb)
|
---|
[337] | 271 | {
|
---|
[387] | 272 | char c;
|
---|
| 273 | int_t n;
|
---|
| 274 |
|
---|
| 275 | if (p_siopcb->rcv_flag) {
|
---|
| 276 | p_siopcb->rcv_flag = false;
|
---|
| 277 | return((int_t)(uint8_t)(p_siopcb->rcv_buf));
|
---|
| 278 | }
|
---|
| 279 | else if ((n = siofd_read(p_siopcb->read_fd, &c, 1)) > 0) {
|
---|
| 280 | return((int_t)(uint8_t) c);
|
---|
| 281 | }
|
---|
| 282 | else {
|
---|
| 283 | assert(n < 0 && errno == EAGAIN);
|
---|
| 284 | return(-1);
|
---|
| 285 | }
|
---|
[337] | 286 | }
|
---|
| 287 |
|
---|
| 288 | /*
|
---|
[387] | 289 | * SIOポートからのコールバックの許可
|
---|
[337] | 290 | */
|
---|
| 291 | void
|
---|
[374] | 292 | sio_ena_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
|
---|
[337] | 293 | {
|
---|
[374] | 294 | switch (cbrtn) {
|
---|
| 295 | case SIO_RDY_SND:
|
---|
[387] | 296 | p_siopcb->snd_rdy = true;
|
---|
[374] | 297 | break;
|
---|
| 298 | case SIO_RDY_RCV:
|
---|
[387] | 299 | p_siopcb->rcv_rdy = true;
|
---|
[374] | 300 | break;
|
---|
| 301 | }
|
---|
[337] | 302 | }
|
---|
| 303 |
|
---|
| 304 | /*
|
---|
[387] | 305 | * SIOポートからのコールバックの禁止
|
---|
[337] | 306 | */
|
---|
| 307 | void
|
---|
[374] | 308 | sio_dis_cbr(SIOPCB *p_siopcb, uint_t cbrtn)
|
---|
[337] | 309 | {
|
---|
[374] | 310 | switch (cbrtn) {
|
---|
| 311 | case SIO_RDY_SND:
|
---|
[387] | 312 | p_siopcb->snd_rdy = false;
|
---|
[374] | 313 | break;
|
---|
| 314 | case SIO_RDY_RCV:
|
---|
[387] | 315 | p_siopcb->rcv_rdy = false;
|
---|
[374] | 316 | break;
|
---|
| 317 | }
|
---|
[337] | 318 | }
|
---|