3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
6 * Copyright (C) 2007 by Embedded and Real-Time Systems Laboratory
7 * Graduate School of Information Science, Nagoya Univ., JAPAN
9 * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
10 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
11 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
12 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
13 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
15 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
16 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
17 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
19 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
20 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
22 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
23 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
24 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
26 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
27 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
28 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
29 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
32 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
33 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
34 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
35 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
46 #include "target_serial.h"
47 #include "target_syssvc.h"
52 #define PORT2SIOPID(x) ((x) + 1)
53 #define INDEX_PORT(x) ((x) - 1)
54 #define GET_SIOPCB(x) (&siopcb_table[INDEX_PORT(x)])
59 #define USART_SR(x) (x)
60 #define USART_DR(x) (x + 0x04)
61 #define USART_BRR(x) (x + 0x08)
62 #define USART_CR1(x) (x + 0x0C)
63 #define USART_CR2(x) (x + 0x10)
64 #define USART_CR3(x) (x + 0x14)
65 #define USART_GTPR(x) (x + 0x18)
67 #define SR_TXE (0x0080)
68 #define SR_RXNE (0x0020)
69 #define SR_ORE (0x0008)
70 #define SR_FE (0x0002)
71 #define SR_PE (0x0001)
72 #define CR1_UE (0x2000)
73 #define CR1_TXEIE (0x0080)
74 #define CR1_RXNEIE (0x0020)
75 #define CR1_TE (0x0008)
76 #define CR1_RE (0x0004)
77 #define CR3_EIE (0x0001)
82 SIOPCB siopcb_table[TNUM_PORT];
84 static const uint32_t sioreg_table[TNUM_PORT] = {
91 Inline bool_t sio_putready(SIOPCB* siopcb)
93 return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_TXE) != 0;
96 Inline bool_t sio_getready(SIOPCB* siopcb)
98 return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_RXNE) != 0;
104 void target_usart_init(ID siopid)
106 uint32_t tmp, usartdiv, fraction;
107 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
111 sil_andw((void*)USART_CR1(reg), ~CR1_UE);
114 sil_wrw_mem((void*)USART_CR2(reg), 0);
116 /* 1START BIT, 8DATA bits, Parityなし */
117 sil_wrw_mem((void*)USART_CR1(reg), 0);
120 sil_wrw_mem((void*)USART_CR3(reg), 0);
125 src_clock = PCLK2_CLOCK;
128 src_clock = PCLK1_CLOCK;
130 tmp = (1000 * (src_clock / 100)) / ((BPS_SETTING / 100) * 16);
131 usartdiv = (tmp / 1000) << 4;
132 fraction = tmp - ((usartdiv >> 4) * 1000);
133 fraction = ((16 * fraction) + 500) / 1000;
134 usartdiv |= (fraction & 0x0F);
135 sil_wrw_mem((void*)USART_BRR(reg), usartdiv);
137 /* 送受信の有効化、エラー割込みの有効化 */
138 sil_orw((void*)USART_CR1(reg), CR1_RE | CR1_TE);
139 sil_orw((void*)USART_CR3(reg), CR3_EIE);
142 sil_orw((void*)USART_CR1(reg), CR1_UE);
148 void target_usart_term(ID siopid)
150 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
153 sil_andw((void*)USART_CR1(reg), ~CR1_UE);
159 void sio_initialize(intptr_t exinf)
163 for (i = 0; i < TNUM_PORT; i++) {
164 siopcb_table[i].port = i;
165 siopcb_table[i].reg = sioreg_table[i];
166 siopcb_table[i].exinf = 0;
173 SIOPCB *sio_opn_por(ID siopid, intptr_t exinf)
177 if (siopid > TNUM_PORT) {
181 siopcb = GET_SIOPCB(siopid);
182 siopcb->exinf = exinf;
184 target_usart_init(siopid);
192 void sio_cls_por(SIOPCB *p_siopcb)
194 target_usart_term(PORT2SIOPID(p_siopcb->port));
200 void sio_isr(intptr_t exinf)
202 SIOPCB* siopcb = GET_SIOPCB(exinf);
204 if (sio_putready(siopcb)) {
205 sio_irdy_snd(siopcb->exinf);
207 if (sio_getready(siopcb)) {
208 sio_irdy_rcv(siopcb->exinf);
215 bool_t sio_snd_chr(SIOPCB *siopcb, char_t c)
217 if (sio_putready(siopcb)) {
218 sil_wrw_mem((void*)USART_DR(siopcb->reg), c);
229 int_t sio_rcv_chr(SIOPCB *siopcb)
233 if (sio_getready(siopcb)) {
234 c = sil_rew_mem((void*)USART_DR(siopcb->reg)) & 0xFF;
243 void sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
247 sil_orw((void*)USART_CR1(siopcb->reg), CR1_TXEIE);
250 sil_orw((void*)USART_CR1(siopcb->reg), CR1_RXNEIE);
260 void sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
264 sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_TXEIE);
267 sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_RXNEIE);
277 void sio_pol_snd_chr(char_t c, ID siopid)
279 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
281 sil_wrw_mem((void*)USART_DR(reg), c);
283 while ((sil_rew_mem((void*)USART_SR(reg)) & SR_TXE) == 0) ;