source: asp_ewarm/asp-1.7.0/target/stm32_discovery_EWARM2/target_serial.c@ 61

Last change on this file since 61 was 61, checked in by ertl-honda, 11 years ago

ASP for EWARM のコミット.

File size: 6.5 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) 2007 by Embedded and Real-Time Systems Laboratory
7 * Graduate School of Information Science, Nagoya Univ., JAPAN
8 *
9 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
10 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
11 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
12 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
13 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
14 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
15 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
16 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
17ƒƒ“ƒgi—˜—p
18 * ŽÒƒ}ƒjƒ…
19ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
20 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
21 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
22 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
23 * ‚ƁD
24 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
25ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
26ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
27 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
28 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
29 * •ñ‚·‚邱‚ƁD
30 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
31 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
32 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
33 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
34 * –Ɛӂ·‚邱‚ƁD
35 *
36 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
37 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
38 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
39 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
40 * ‚̐ӔC‚𕉂í‚È‚¢D
41 *
42 */
43
44/*
45 * ƒVƒŠƒAƒ‹ƒhƒ‰ƒCƒoiSTM32 DISCOVERY—pj
46 */
47
48#include <kernel.h>
49#include <sil.h>
50#include "target_serial.h"
51#include "target_syssvc.h"
52
53/*
54 * ƒŒƒWƒXƒ^Ý’è’l
55 */
56#define PORT2SIOPID(x) ((x) + 1)
57#define INDEX_PORT(x) ((x) - 1)
58#define GET_SIOPCB(x) (&siopcb_table[INDEX_PORT(x)])
59
60/*
61 * USARTƒŒƒWƒXƒ^’è‹`
62 */
63#define USART_SR(x) (x)
64#define USART_DR(x) (x + 0x04)
65#define USART_BRR(x) (x + 0x08)
66#define USART_CR1(x) (x + 0x0C)
67#define USART_CR2(x) (x + 0x10)
68#define USART_CR3(x) (x + 0x14)
69#define USART_GTPR(x) (x + 0x18)
70
71#define SR_TXE (0x0080)
72#define SR_RXNE (0x0020)
73#define SR_ORE (0x0008)
74#define SR_FE (0x0002)
75#define SR_PE (0x0001)
76#define CR1_UE (0x2000)
77#define CR1_TXEIE (0x0080)
78#define CR1_RXNEIE (0x0020)
79#define CR1_TE (0x0008)
80#define CR1_RE (0x0004)
81#define CR3_EIE (0x0001)
82
83/*
84 * ƒVƒŠƒAƒ‹I/Oƒ|[ƒgŠÇ—ƒuƒƒbƒNƒGƒŠƒA
85 */
86SIOPCB siopcb_table[TNUM_PORT];
87
88static const uint32_t sioreg_table[TNUM_PORT] = {
89 USART1_BASE,
90};
91
92#pragma inline
93bool_t sio_putready(SIOPCB* siopcb)
94{
95 return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_TXE) != 0;
96}
97
98#pragma inline
99bool_t sio_getready(SIOPCB* siopcb)
100{
101 return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_RXNE) != 0;
102}
103
104/*
105 * ƒ^[ƒQƒbƒg‚̃VƒŠƒAƒ‹‰Šú‰»
106 */
107void target_usart_init(ID siopid)
108{
109 uint32_t tmp, usartdiv, fraction;
110 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
111 uint32_t src_clock;
112
113 /* USART‚Ì–³Œø‰» */
114 sil_andw((void*)USART_CR1(reg), ~CR1_UE);
115
116 /* 1STOP BIT */
117 sil_wrw_mem((void*)USART_CR2(reg), 0);
118
119 /* 1START BIT, 8DATA bits, Parity‚È‚µ */
120 sil_wrw_mem((void*)USART_CR1(reg), 0);
121
122 /* CR3‰Šú‰» */
123 sil_wrw_mem((void*)USART_CR3(reg), 0);
124
125 /* ’ʐM‘¬“xÝ’è */
126 if (siopid == 1) {
127 /* fck=72MHz */
128 src_clock = PCLK2_CLOCK;
129 } else {
130 /* fck=36MHz */
131 src_clock = PCLK1_CLOCK;
132 }
133 tmp = (1000 * (src_clock / 100)) / ((BPS_SETTING / 100) * 16);
134 usartdiv = (tmp / 1000) << 4;
135 fraction = tmp - ((usartdiv >> 4) * 1000);
136 fraction = ((16 * fraction) + 500) / 1000;
137 usartdiv |= (fraction & 0x0F);
138 sil_wrw_mem((void*)USART_BRR(reg), usartdiv);
139
140 /* ‘—ŽóM‚Ì—LŒø‰»AƒGƒ‰[Š„ž‚Ý‚Ì—LŒø‰» */
141 sil_orw((void*)USART_CR1(reg), CR1_RE | CR1_TE);
142 sil_orw((void*)USART_CR3(reg), CR3_EIE);
143
144 /* USART‚Ì—LŒø‰» */
145 sil_orw((void*)USART_CR1(reg), CR1_UE);
146}
147
148/*
149 * ƒ^[ƒQƒbƒg‚̃VƒŠƒAƒ‹I—¹
150 */
151void target_usart_term(ID siopid)
152{
153 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
154
155 /* USART‚Ì–³Œø‰» */
156 sil_andw((void*)USART_CR1(reg), ~CR1_UE);
157}
158
159/*
160 * SIO‰Šú‰»
161 */
162void sio_initialize(intptr_t exinf)
163{
164 int i;
165
166 for (i = 0; i < TNUM_PORT; i++) {
167 siopcb_table[i].port = i;
168 siopcb_table[i].reg = sioreg_table[i];
169 siopcb_table[i].exinf = 0;
170 }
171}
172
173/*
174 * ƒVƒŠƒAƒ‹ƒI[ƒvƒ“
175 */
176SIOPCB *sio_opn_por(ID siopid, intptr_t exinf)
177{
178 SIOPCB* siopcb;
179
180 if (siopid > TNUM_PORT) {
181 return NULL;
182 }
183
184 siopcb = GET_SIOPCB(siopid);
185 siopcb->exinf = exinf;
186
187 target_usart_init(siopid);
188
189 return siopcb;
190}
191
192/*
193 * ƒVƒŠƒAƒ‹ƒNƒ[ƒY
194 */
195void sio_cls_por(SIOPCB *p_siopcb)
196{
197 target_usart_term(PORT2SIOPID(p_siopcb->port));
198}
199
200/*
201 * Š„ž‚݃nƒ“ƒhƒ‰
202 */
203void sio_isr(intptr_t exinf)
204{
205 SIOPCB* siopcb = GET_SIOPCB(exinf);
206
207 if (sio_putready(siopcb)) {
208 sio_irdy_snd(siopcb->exinf);
209 }
210 if (sio_getready(siopcb)) {
211 sio_irdy_rcv(siopcb->exinf);
212 }
213}
214
215/*
216 * 1•¶Žš‘—M
217 */
218bool_t sio_snd_chr(SIOPCB *siopcb, char_t c)
219{
220 bool_t stat;
221
222 if (sio_putready(siopcb)) {
223 sil_wrw_mem((void*)USART_DR(siopcb->reg), c);
224 stat = true;
225 }
226 else
227 {
228 stat = false;
229 }
230
231 return stat;
232}
233
234/*
235 * 1•¶ŽšŽóM
236 */
237int_t sio_rcv_chr(SIOPCB *siopcb)
238{
239 int_t c = -1;
240
241 if (sio_getready(siopcb)) {
242 c = sil_rew_mem((void*)USART_DR(siopcb->reg)) & 0xFF;
243 }
244
245 return c;
246}
247
248/*
249 * ƒR[ƒ‹ƒoƒbƒN‚Ì‹–‰Â
250 */
251void sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
252{
253 switch (cbrtn) {
254 case SIO_RDY_SND:
255 sil_orw((void*)USART_CR1(siopcb->reg), CR1_TXEIE);
256 break;
257 case SIO_RDY_RCV:
258 sil_orw((void*)USART_CR1(siopcb->reg), CR1_RXNEIE);
259 break;
260 default:
261 break;
262 }
263}
264
265/*
266 * ƒR[ƒ‹ƒoƒbƒN‚Ì‹ÖŽ~
267 */
268void sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
269{
270 switch (cbrtn) {
271 case SIO_RDY_SND:
272 sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_TXEIE);
273 break;
274 case SIO_RDY_RCV:
275 sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_RXNEIE);
276 break;
277 default:
278 break;
279 }
280}
281
282/*
283 * 1•¶Žšo—́iƒ|[ƒŠƒ“ƒO‚ł̏o—́j
284 */
285void sio_pol_snd_chr(char_t c, ID siopid)
286{
287 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
288
289 sil_wrw_mem((void*)USART_DR(reg), c);
290
291 while ((sil_rew_mem((void*)USART_SR(reg)) & SR_TXE) == 0) ;
292}
Note: See TracBrowser for help on using the repository browser.