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

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

ファイル一式の追加

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}
174
175/*
176 * UARTからのポーリング出力
177 */
178void
179sercom_putc(uint32_t siopid, char c)
180{
181 while(!(SERCOM2_REGS->USART_INT.SERCOM_INTFLAG & SERCOM_USART_INT_INTFLAG_DRE_Msk));
182 SERCOM2_REGS->USART_INT.SERCOM_DATA = c;
183}
184
185
186/*
187 * 文字を受信したか?
188 */
189Inline bool_t
190uart_getready(SIOPCB *p_siopcb)
191{
192 return(SERCOM2_REGS->USART_INT.SERCOM_INTFLAG & SERCOM_USART_INT_INTENSET_RXC_Msk);
193}
194
195/*
196 * 文字を送信できるか?
197 */
198Inline bool_t
199uart_putready(SIOPCB *p_siopcb)
200{
201 return(SERCOM2_REGS->USART_INT.SERCOM_INTFLAG & SERCOM_USART_INT_INTFLAG_DRE_Msk);
202}
203
204/*
205 * 受信した文字の取り出し
206 */
207Inline uint8_t
208uart_getchar(SIOPCB *p_siopcb)
209{
210 return SERCOM2_REGS->USART_INT.SERCOM_DATA;
211}
212
213/*
214 * 送信する文字の書き込み
215 */
216Inline void
217uart_putchar(SIOPCB *p_siopcb, uint8_t c)
218{
219 SERCOM2_REGS->USART_INT.SERCOM_DATA = c;
220}
221
222/*
223 * 送信割込み許可
224 */
225Inline void
226uart_enable_send(SIOPCB *p_siopcb)
227{
228 SERCOM2_REGS->USART_INT.SERCOM_INTENSET = SERCOM_USART_INT_INTENSET_TXC_Msk;
229}
230
231/*
232 * 送信割込み禁止
233 */
234Inline void
235uart_disable_send(SIOPCB *p_siopcb)
236{
237 SERCOM2_REGS->USART_INT.SERCOM_INTENCLR = SERCOM_USART_INT_INTENCLR_TXC_Msk;
238}
239
240
241/*
242 * 受信割込み許可
243 */
244Inline void
245uart_enable_rcv(SIOPCB *p_siopcb)
246{
247 SERCOM2_REGS->USART_INT.SERCOM_INTENSET = SERCOM_USART_INT_INTENSET_RXC_Msk;
248}
249
250/*
251 * 受信割込み禁止
252 */
253Inline void
254uart_disable_rcv(SIOPCB *p_siopcb)
255{
256 SERCOM2_REGS->USART_INT.SERCOM_INTENCLR = SERCOM_USART_INT_INTENCLR_RXC_Msk;
257}
258
259
260/*
261 * SIOドライバの初期化
262 */
263void
264sio_initialize(intptr_t exinf)
265{
266 SIOPCB *p_siopcb;
267 uint_t i;
268 ER ercd;
269
270 /*
271 * シリアルI/Oポート管理ブロックの初期化
272 */
273 for (p_siopcb = siopcb_table, i = 0; i < TNUM_SIOP; p_siopcb++, i++) {
274 p_siopcb->p_siopinib = &(siopinib_table[i]);
275 p_siopcb->openflag = false;
276 p_siopcb->sendflag = false;
277 }
278}
279
280/*
281 * SIOドライバの終了処理
282 */
283void
284sio_terminate(intptr_t exinf)
285{
286 SIOPCB *p_siopcb;
287 uint_t i;
288
289 /*
290 * オープンされているシリアルI/Oポートのクローズ
291 */
292 for (i = 0; i < TNUM_SIOP; i++) {
293 p_siopcb = &(siopcb_table[i]);
294 if (p_siopcb->openflag) {
295 sio_cls_por(p_siopcb);
296 }
297 }
298}
299
300/*
301 * シリアルI/Oポートのオープン
302 */
303SIOPCB *
304sercom_uart_opn_por(SIOPCB *p_siopcb, intptr_t exinf)
305{
306 p_siopcb->exinf = exinf;
307 p_siopcb->getready = p_siopcb->putready = false;
308 p_siopcb->openflag = true;
309
310 sercom_uart_devinit(p_siopcb);
311
312 return(p_siopcb);
313}
314
315
316/*
317 * シリアルI/Oポートのオープン
318 */
319SIOPCB *
320sio_opn_por(ID siopid, intptr_t exinf)
321{
322 SIOPCB *p_siopcb = get_siopcb(siopid);
323 bool_t opnflg;
324
325 /*
326 * オープンしたポートがあるかをopnflgに読んでおく.
327 */
328 opnflg = p_siopcb->openflag;
329
330 /*
331 * デバイス依存のオープン処理.
332 */
333 sercom_uart_opn_por(p_siopcb, exinf);
334
335 /*
336 * シリアルI/O割込みのマスクを解除する.
337 */
338 if (!opnflg) {
339 (void)ena_int(INTNO_SIO_RX);
340 (void)ena_int(INTNO_SIO_TX);
341 }
342 return(p_siopcb);
343}
344
345/*
346 * シリアルI/Oポートのクローズ
347 */
348void
349sio_cls_por(SIOPCB *p_siopcb)
350{
351 /*
352 * デバイス依存のクローズ処理.
353 */
354 p_siopcb->openflag = false;
355
356 /*
357 * シリアルI/O割込みをマスクする.
358 */
359 dis_int(INTNO_SIO_RX);
360 dis_int(INTNO_SIO_TX);
361}
362
363/*
364 * SIOの割込みハンドラ
365 */
366void
367sio_isr(intptr_t exinf)
368{
369 SIOPCB *p_siopcb = get_siopcb(exinf);
370
371 if (uart_getready(p_siopcb)) {
372 /*
373 * 受信通知コールバックルーチンを呼び出す.
374 */
375 sio_irdy_rcv(p_siopcb->exinf);
376 }
377 if (uart_putready(p_siopcb)) {
378 /*
379 * 送信可能コールバックルーチンを呼び出す.
380 */
381 sio_irdy_snd(p_siopcb->exinf);
382 }
383}
384
385
386/*
387 * シリアルI/Oポートへの文字送信
388 */
389bool_t
390sio_snd_chr(SIOPCB *siopcb, char c)
391{
392 if (uart_putready(siopcb)){
393 uart_putchar(siopcb, c);
394 return(true);
395 }
396 return(false);
397}
398
399/*
400 * シリアルI/Oポートからの文字受信
401 */
402int_t
403sio_rcv_chr(SIOPCB *siopcb)
404{
405 if (uart_getready(siopcb)) {
406 return((int_t)(uint8_t) uart_getchar(siopcb));
407 }
408 return(-1);
409}
410
411/*
412 * シリアルI/Oポートからのコールバックの許可
413 */
414void
415sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
416{
417 switch (cbrtn) {
418 case SIO_RDY_SND:
419 uart_enable_send(siopcb);
420 break;
421 case SIO_RDY_RCV:
422 uart_enable_rcv(siopcb);
423 break;
424 }
425}
426
427/*
428 * シリアルI/Oポートからのコールバックの禁止
429 */
430void
431sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
432{
433 switch (cbrtn) {
434 case SIO_RDY_SND:
435 uart_disable_send(siopcb);
436 break;
437 case SIO_RDY_RCV:
438 uart_disable_rcv(siopcb);
439 break;
440 }
441}
Note: See TracBrowser for help on using the repository browser.