source: azure_iot_hub/trunk/asp3_dcre/target/gr_citrus_gcc/target_serial.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 8.0 KB
Line 
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 */
73typedef struct sio_port_initialization_block {
74 char *path; /* ファイルのパス名 */
75} SIOPINIB;
76
77/*
78 * SIOポート管理ブロックの定義
79 */
80struct 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 */
103const SIOPINIB siopinib_table[TNUM_SIOP] = {
104 { NULL }
105};
106
107/*
108 * SIOポート管理ブロックのエリア
109 */
110SIOPCB 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 */
121void
122sio_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 */
140void
141sio_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 */
156SIOPCB *
157sio_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 */
201void
202sio_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 */
222bool_t
223sio_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 */
244bool_t
245sio_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 */
264bool_t
265sio_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 */
288int_t
289sio_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 */
310void
311sio_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 */
326void
327sio_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}
Note: See TracBrowser for help on using the repository browser.