source: asp_wio_terminal/trunk/target/samd51_gcc/target_serial.c@ 463

Last change on this file since 463 was 463, checked in by msugi, 3 years ago

シリアルの2重初期化を修正

File size: 10.4 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) 2015 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 2708 2015-11-22 10:35:09Z ertl-honda $
54 */
55
56/*
57 * シリアルI/Oデバイス(SIO)ドライバ(SAMD51用)
58 */
59#include <kernel.h>
60#include <t_syslog.h>
61//#undef assert
62//#include "samd55.h"
63#include "target_serial.h"
64//#include "hri_l11.h"
65//#include "peripheral_clk_config.h"
66//#include "hal_gpio.h"
67
68/*
69 * シリアルI/Oポート初期化ブロックの定義
70 */
71typedef struct sio_port_initialization_block
72{
73// Sercom* p_sercom; /* 使用するSERCOMへのポインタ */
74 uint32_t bps; /* ボーレート */
75}
76SIOPINIB;
77
78/*
79 * シリアルI/Oポート管理ブロックの定義
80 */
81struct sio_port_control_block
82{
83 const SIOPINIB* p_siopinib; /* 初期化ブロック */
84 intptr_t exinf; /* 拡張情
85å ± */
86 bool_t openflag; /* オープン済みフラグ */
87 bool_t devinitd; /* デバイス初期化済みフラグ */
88 bool_t sendflag; /* 送信割込みイネーブルフラグ */
89 bool_t getready; /* 文字を受信した状æ…
90‹ */
91 bool_t putready; /* 文字を送信できる状æ…
92‹ */
93};
94
95/*
96 * シリアルI/Oポート初期化ブロック
97 */
98const SIOPINIB siopinib_table[TNUM_SIOP] = {
99// {SERCOM2, 115200}
100 {115200}
101};
102
103/*
104 * シリアルI/Oポート管理ブロックのエリア
105 */
106SIOPCB siopcb_table[TNUM_SIOP];
107
108/*
109 * シリアルI/OポートIDから管理ブロックを取り出すためのマクロ
110 */
111#define INDEX_SIOP(siopid) ((uint_t)((siopid) - 1))
112#define get_siopcb(siopid) (&(siopcb_table[INDEX_SIOP(siopid)]))
113
114
115/* SERCOM2 USART baud value for 115200 Hz baud rate */
116#define SERCOM2_USART_INT_BAUD_VALUE (64328U)
117
118static void
119sercom_uart_devinit(SIOPCB *p_siopcb)
120{
121 /* ToDo */
122
123 /*
124 * Configures USART Clock Mode
125 * Configures TXPO and RXPO
126 * Configures Data Order
127 * Configures Standby Mode
128 * Configures Sampling rate
129 * Configures IBON
130 */
131 SERCOM2_REGS->USART_INT.SERCOM_CTRLA = SERCOM_USART_INT_CTRLA_MODE_USART_INT_CLK | SERCOM_USART_INT_CTRLA_RXPO(0x1) | SERCOM_USART_INT_CTRLA_TXPO(0x2) | SERCOM_USART_INT_CTRLA_DORD_Msk | SERCOM_USART_INT_CTRLA_IBON_Msk | SERCOM_USART_INT_CTRLA_FORM(0x0) | SERCOM_USART_INT_CTRLA_SAMPR(0) ;
132
133 /* Configure Baud Rate */
134 SERCOM2_REGS->USART_INT.SERCOM_BAUD = SERCOM_USART_INT_BAUD_BAUD(SERCOM2_USART_INT_BAUD_VALUE);
135
136 /*
137 * Configures RXEN
138 * Configures TXEN
139 * Configures CHSIZE
140 * Configures Parity
141 * Configures Stop bits
142 */
143 SERCOM2_REGS->USART_INT.SERCOM_CTRLB = SERCOM_USART_INT_CTRLB_CHSIZE_8_BIT | SERCOM_USART_INT_CTRLB_SBMODE_1_BIT | SERCOM_USART_INT_CTRLB_RXEN_Msk | SERCOM_USART_INT_CTRLB_TXEN_Msk;
144
145 /* Wait for sync */
146 while(SERCOM2_REGS->USART_INT.SERCOM_SYNCBUSY);
147
148 /* Enable the UART after the configurations */
149 SERCOM2_REGS->USART_INT.SERCOM_CTRLA |= SERCOM_USART_INT_CTRLA_ENABLE_Msk;
150
151 /* Wait for sync */
152 while(SERCOM2_REGS->USART_INT.SERCOM_SYNCBUSY);
153
154 /* Enable error interrupt */
155 SERCOM2_REGS->USART_INT.SERCOM_INTENSET = SERCOM_USART_INT_INTENSET_ERROR_Msk;
156
157
158 /* Enable Receive Complete interrupt */
159 SERCOM2_REGS->USART_INT.SERCOM_INTENSET = SERCOM_USART_INT_INTENSET_RXC_Msk;
160}
161
162/*
163 * カーネル起動時のログ出力用の初期化
164 */
165void
166sercom_init_uart(uint32_t siopid)
167{
168 SIOPCB *p_siopcb;
169
170 p_siopcb = get_siopcb(siopid);
171 p_siopcb->p_siopinib = &siopinib_table[INDEX_SIOP(siopid)];
172 sercom_uart_devinit(p_siopcb);
173 p_siopcb->devinitd = true;
174}
175
176/*
177 * UARTからのポーリング出力
178 */
179void
180sercom_putc(uint32_t siopid, char c)
181{
182 while(!(SERCOM2_REGS->USART_INT.SERCOM_INTFLAG & SERCOM_USART_INT_INTFLAG_DRE_Msk));
183 SERCOM2_REGS->USART_INT.SERCOM_DATA = c;
184}
185
186
187/*
188 * 文字を受信したか?
189 */
190Inline bool_t
191uart_getready(SIOPCB *p_siopcb)
192{
193 return(SERCOM2_REGS->USART_INT.SERCOM_INTFLAG & SERCOM_USART_INT_INTENSET_RXC_Msk);
194}
195
196/*
197 * 文字を送信できるか?
198 */
199Inline bool_t
200uart_putready(SIOPCB *p_siopcb)
201{
202 return(SERCOM2_REGS->USART_INT.SERCOM_INTFLAG & SERCOM_USART_INT_INTFLAG_DRE_Msk);
203}
204
205/*
206 * 受信した文字の取り出し
207 */
208Inline uint8_t
209uart_getchar(SIOPCB *p_siopcb)
210{
211 return SERCOM2_REGS->USART_INT.SERCOM_DATA;
212}
213
214/*
215 * 送信する文字の書き込み
216 */
217Inline void
218uart_putchar(SIOPCB *p_siopcb, uint8_t c)
219{
220 SERCOM2_REGS->USART_INT.SERCOM_DATA = c;
221}
222
223/*
224 * 送信割込み許可
225 */
226Inline void
227uart_enable_send(SIOPCB *p_siopcb)
228{
229 SERCOM2_REGS->USART_INT.SERCOM_INTENSET = SERCOM_USART_INT_INTENSET_TXC_Msk;
230}
231
232/*
233 * 送信割込み禁止
234 */
235Inline void
236uart_disable_send(SIOPCB *p_siopcb)
237{
238 SERCOM2_REGS->USART_INT.SERCOM_INTENCLR = SERCOM_USART_INT_INTENCLR_TXC_Msk;
239}
240
241
242/*
243 * 受信割込み許可
244 */
245Inline void
246uart_enable_rcv(SIOPCB *p_siopcb)
247{
248 SERCOM2_REGS->USART_INT.SERCOM_INTENSET = SERCOM_USART_INT_INTENSET_RXC_Msk;
249}
250
251/*
252 * 受信割込み禁止
253 */
254Inline void
255uart_disable_rcv(SIOPCB *p_siopcb)
256{
257 SERCOM2_REGS->USART_INT.SERCOM_INTENCLR = SERCOM_USART_INT_INTENCLR_RXC_Msk;
258}
259
260
261/*
262 * SIOドライバの初期化
263 */
264void
265sio_initialize(intptr_t exinf)
266{
267 SIOPCB *p_siopcb;
268 uint_t i;
269 ER ercd;
270
271 /*
272 * シリアルI/Oポート管理ブロックの初期化
273 */
274 for (p_siopcb = siopcb_table, i = 0; i < TNUM_SIOP; p_siopcb++, i++) {
275 p_siopcb->p_siopinib = &(siopinib_table[i]);
276 p_siopcb->openflag = false;
277 p_siopcb->sendflag = false;
278 }
279}
280
281/*
282 * SIOドライバの終了処理
283 */
284void
285sio_terminate(intptr_t exinf)
286{
287 SIOPCB *p_siopcb;
288 uint_t i;
289
290 /*
291 * オープンされているシリアルI/Oポートのクローズ
292 */
293 for (i = 0; i < TNUM_SIOP; i++) {
294 p_siopcb = &(siopcb_table[i]);
295 if (p_siopcb->openflag) {
296 sio_cls_por(p_siopcb);
297 }
298 }
299}
300
301/*
302 * シリアルI/Oポートのオープン
303 */
304SIOPCB *
305sercom_uart_opn_por(SIOPCB *p_siopcb, intptr_t exinf)
306{
307 p_siopcb->exinf = exinf;
308 p_siopcb->getready = p_siopcb->putready = false;
309 p_siopcb->openflag = true;
310
311 if(!p_siopcb->devinitd)
312 {
313 sercom_uart_devinit(p_siopcb);
314 p_siopcb->devinitd = true;
315 }
316
317 return(p_siopcb);
318}
319
320
321/*
322 * シリアルI/Oポートのオープン
323 */
324SIOPCB *
325sio_opn_por(ID siopid, intptr_t exinf)
326{
327 SIOPCB *p_siopcb = get_siopcb(siopid);
328 bool_t opnflg;
329
330 /*
331 * オープンしたポートがあるかをopnflgに読んでおく.
332 */
333 opnflg = p_siopcb->openflag;
334
335 /*
336 * デバイス依存のオープン処理.
337 */
338 sercom_uart_opn_por(p_siopcb, exinf);
339
340 /*
341 * シリアルI/O割込みのマスクを解除する.
342 */
343 if (!opnflg) {
344 (void)ena_int(INTNO_SIO_RX);
345 (void)ena_int(INTNO_SIO_TX);
346 }
347 return(p_siopcb);
348}
349
350/*
351 * シリアルI/Oポートのクローズ
352 */
353void
354sio_cls_por(SIOPCB *p_siopcb)
355{
356 /*
357 * デバイス依存のクローズ処理.
358 */
359 p_siopcb->openflag = false;
360
361 /*
362 * シリアルI/O割込みをマスクする.
363 */
364 dis_int(INTNO_SIO_RX);
365 dis_int(INTNO_SIO_TX);
366}
367
368/*
369 * SIOの割込みハンドラ
370 */
371void
372sio_isr(intptr_t exinf)
373{
374 SIOPCB *p_siopcb = get_siopcb(exinf);
375
376 if (uart_getready(p_siopcb)) {
377 /*
378 * 受信通知コールバックルーチンを呼び出す.
379 */
380 sio_irdy_rcv(p_siopcb->exinf);
381 }
382 if (uart_putready(p_siopcb)) {
383 /*
384 * 送信可能コールバックルーチンを呼び出す.
385 */
386 sio_irdy_snd(p_siopcb->exinf);
387 }
388}
389
390
391/*
392 * シリアルI/Oポートへの文字送信
393 */
394bool_t
395sio_snd_chr(SIOPCB *siopcb, char c)
396{
397 if (uart_putready(siopcb)){
398 uart_putchar(siopcb, c);
399 return(true);
400 }
401 return(false);
402}
403
404/*
405 * シリアルI/Oポートからの文字受信
406 */
407int_t
408sio_rcv_chr(SIOPCB *siopcb)
409{
410 if (uart_getready(siopcb)) {
411 return((int_t)(uint8_t) uart_getchar(siopcb));
412 }
413 return(-1);
414}
415
416/*
417 * シリアルI/Oポートからのコールバックの許可
418 */
419void
420sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
421{
422 switch (cbrtn) {
423 case SIO_RDY_SND:
424 uart_enable_send(siopcb);
425 break;
426 case SIO_RDY_RCV:
427 uart_enable_rcv(siopcb);
428 break;
429 }
430}
431
432/*
433 * シリアルI/Oポートからのコールバックの禁止
434 */
435void
436sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
437{
438 switch (cbrtn) {
439 case SIO_RDY_SND:
440 uart_disable_send(siopcb);
441 break;
442 case SIO_RDY_RCV:
443 uart_disable_rcv(siopcb);
444 break;
445 }
446}
Note: See TracBrowser for help on using the repository browser.