[441] | 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 | * 上記著作権者は,以下の(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 | * シリアルインタフェースドライバのターゲット依存部(NTShell用)
|
---|
| 43 | */
|
---|
| 44 |
|
---|
| 45 | #include <t_stddef.h>
|
---|
| 46 | #include <t_syslog.h>
|
---|
| 47 | #include <errno.h>
|
---|
| 48 | #include <termios.h>
|
---|
| 49 | #include <unistd.h>
|
---|
| 50 | #include <fcntl.h>
|
---|
| 51 | #include "tSIOPortNTShellMain_tecsgen.h"
|
---|
| 52 | #include "syssvc/siofd.h"
|
---|
| 53 |
|
---|
| 54 | extern int shell_errno;
|
---|
| 55 |
|
---|
| 56 | /*
|
---|
| 57 | * SIOポートのオープン
|
---|
| 58 | */
|
---|
| 59 | void
|
---|
| 60 | eSIOPort_open(CELLIDX idx)
|
---|
| 61 | {
|
---|
| 62 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 63 | int_t fd;
|
---|
| 64 | struct termios term;
|
---|
| 65 |
|
---|
| 66 | if (!VAR_opened) {
|
---|
| 67 | /*
|
---|
| 68 | * 既にオープンしている場合は、二重にオープンしない.
|
---|
| 69 | */
|
---|
| 70 | if (ATTR_path != NULL) {
|
---|
| 71 | fd = siofd_open(ATTR_path, O_RDWR, 0777);
|
---|
| 72 | assert(fd >= 0);
|
---|
| 73 | VAR_read_fd = fd;
|
---|
| 74 | VAR_write_fd = fd;
|
---|
| 75 | }
|
---|
| 76 | else {
|
---|
| 77 | fd = STDIN_FILENO; /* 標準入出力を使う */
|
---|
| 78 | VAR_read_fd = STDIN_FILENO;
|
---|
| 79 | VAR_write_fd = STDOUT_FILENO;
|
---|
| 80 | }
|
---|
| 81 | siofd_fcntl(fd, F_SETOWN, getpid());
|
---|
| 82 | siofd_fcntl(fd, F_SETFL, (O_NONBLOCK | O_ASYNC));
|
---|
| 83 |
|
---|
| 84 | siofd_tcgetattr(fd, &VAR_saved_term);
|
---|
| 85 | term = VAR_saved_term;
|
---|
| 86 | term.c_lflag &= ~(ECHO | ICANON);
|
---|
| 87 | siofd_tcsetattr(fd, TCSAFLUSH, &term);
|
---|
| 88 |
|
---|
| 89 | VAR_rcv_flag = false;
|
---|
| 90 | VAR_rcv_rdy = false;
|
---|
| 91 | VAR_snd_flag = false;
|
---|
| 92 | VAR_snd_rdy = false;
|
---|
| 93 | VAR_opened = true;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /*
|
---|
| 98 | * SIOポートのクローズ
|
---|
| 99 | */
|
---|
| 100 | void
|
---|
| 101 | eSIOPort_close(CELLIDX idx)
|
---|
| 102 | {
|
---|
| 103 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 104 | int_t fd;
|
---|
| 105 |
|
---|
| 106 | if (VAR_opened) {
|
---|
| 107 | fd = VAR_read_fd;
|
---|
| 108 | siofd_tcsetattr(fd, TCSAFLUSH, &VAR_saved_term);
|
---|
| 109 | siofd_fcntl(fd, F_SETFL, 0);
|
---|
| 110 |
|
---|
| 111 | if (ATTR_path != NULL) {
|
---|
| 112 | siofd_close(VAR_read_fd);
|
---|
| 113 | }
|
---|
| 114 | VAR_opened = false;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /*
|
---|
| 119 | * SIOポートへの文字送信
|
---|
| 120 | */
|
---|
| 121 | bool_t
|
---|
| 122 | eSIOPort_putChar(CELLIDX idx, char c)
|
---|
| 123 | {
|
---|
| 124 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 125 | int_t n;
|
---|
| 126 |
|
---|
| 127 | if (!VAR_snd_flag) {
|
---|
| 128 | if ((n = siofd_write(VAR_write_fd, &c, 1)) > 0) {
|
---|
| 129 | return(true);
|
---|
| 130 | }
|
---|
| 131 | else {
|
---|
| 132 | assert(n < 0 && shell_errno == EAGAIN);
|
---|
| 133 | VAR_snd_flag = true;
|
---|
| 134 | VAR_snd_buf = c;
|
---|
| 135 | return(true);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | else {
|
---|
| 139 | return(false);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * SIOポートからの文字受信
|
---|
| 145 | */
|
---|
| 146 | int_t
|
---|
| 147 | eSIOPort_getChar(CELLIDX idx)
|
---|
| 148 | {
|
---|
| 149 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 150 | char c;
|
---|
| 151 | int_t n;
|
---|
| 152 |
|
---|
| 153 | if (VAR_rcv_flag) {
|
---|
| 154 | VAR_rcv_flag = false;
|
---|
| 155 | return((int_t)(uint8_t)(VAR_rcv_buf));
|
---|
| 156 | }
|
---|
| 157 | else if ((n = siofd_read(VAR_read_fd, &c, 1)) > 0) {
|
---|
| 158 | return((int_t)(uint8_t) c);
|
---|
| 159 | }
|
---|
| 160 | else {
|
---|
| 161 | assert(n < 0 && shell_errno == EAGAIN);
|
---|
| 162 | return(-1);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /*
|
---|
| 167 | * SIOポートからのコールバックの許可
|
---|
| 168 | */
|
---|
| 169 | void
|
---|
| 170 | eSIOPort_enableCBR(CELLIDX idx, uint_t cbrtn)
|
---|
| 171 | {
|
---|
| 172 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 173 |
|
---|
| 174 | switch (cbrtn) {
|
---|
| 175 | case SIOSendReady:
|
---|
| 176 | VAR_snd_rdy = true;
|
---|
| 177 | break;
|
---|
| 178 | case SIOReceiveReady:
|
---|
| 179 | VAR_rcv_rdy = true;
|
---|
| 180 | break;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /*
|
---|
| 185 | * SIOポートからのコールバックの禁止
|
---|
| 186 | */
|
---|
| 187 | void
|
---|
| 188 | eSIOPort_disableCBR(CELLIDX idx, uint_t cbrtn)
|
---|
| 189 | {
|
---|
| 190 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 191 |
|
---|
| 192 | switch (cbrtn) {
|
---|
| 193 | case SIOSendReady:
|
---|
| 194 | VAR_snd_rdy = false;
|
---|
| 195 | break;
|
---|
| 196 | case SIOReceiveReady:
|
---|
| 197 | VAR_rcv_rdy = false;
|
---|
| 198 | break;
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /*
|
---|
| 203 | * SIOの割込みサービスルーチン
|
---|
| 204 | */
|
---|
| 205 | bool_t
|
---|
| 206 | sio_isr_snd(ID siopid)
|
---|
| 207 | {
|
---|
| 208 | if (!(1 <= siopid && siopid <= tSIOPortNTShellMain_N_CELL))
|
---|
| 209 | return false;
|
---|
| 210 | CELLIDX idx = &tSIOPortNTShellMain_CB_tab[siopid - 1];
|
---|
| 211 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 212 | int_t n;
|
---|
| 213 |
|
---|
| 214 | if (VAR_snd_flag) {
|
---|
| 215 | if ((n = siofd_write(VAR_write_fd, &(VAR_snd_buf), 1)) > 0) {
|
---|
| 216 | VAR_snd_flag = false;
|
---|
| 217 | if (VAR_snd_rdy) {
|
---|
| 218 | ciSIOCBR_readySend();
|
---|
| 219 | return VAR_snd_rdy;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | return false;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /*
|
---|
| 228 | * SIOの割込みサービスルーチン
|
---|
| 229 | */
|
---|
| 230 | bool_t
|
---|
| 231 | sio_isr_rcv(ID siopid, char c)
|
---|
| 232 | {
|
---|
| 233 | if (!(1 <= siopid && siopid <= tSIOPortNTShellMain_N_CELL))
|
---|
| 234 | return false;
|
---|
| 235 | CELLIDX idx = &tSIOPortNTShellMain_CB_tab[siopid - 1];
|
---|
| 236 | CELLCB *p_cellcb = GET_CELLCB(idx);
|
---|
| 237 |
|
---|
| 238 | if (!VAR_rcv_flag) {
|
---|
| 239 | VAR_rcv_buf = c;
|
---|
| 240 | VAR_rcv_flag = true;
|
---|
| 241 | if (VAR_rcv_rdy) {
|
---|
| 242 | ciSIOCBR_readyReceive();
|
---|
| 243 | return VAR_rcv_rdy;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | return false;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /*
|
---|
| 251 | * SIOドライバの終了処理
|
---|
| 252 | */
|
---|
| 253 | void
|
---|
| 254 | eTerminate_main(CELLIDX idx)
|
---|
| 255 | {
|
---|
| 256 | eSIOPort_close(idx);
|
---|
| 257 | }
|
---|