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{\tgEF
|
---|
10 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
11 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
12 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
13 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
14 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
15 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
16 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
17 | gip
|
---|
18 | * Ò}j
|
---|
19 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
20 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
21 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
22 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
23 | * ÆD
|
---|
24 | * (a) Äzzɺ¤hL
|
---|
25 | gipÒ}j
|
---|
26 | AÈÇjÉCãLÌ
|
---|
27 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
28 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
29 | * ñ·é±ÆD
|
---|
30 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
31 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
32 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
33 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
34 | * ÆÓ·é±ÆD
|
---|
35 | *
|
---|
36 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
37 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
38 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
39 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
40 | * ÌÓCðíÈ¢D
|
---|
41 | *
|
---|
42 | */
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * VAhCoiSTM32 DISCOVERYpj
|
---|
46 | */
|
---|
47 |
|
---|
48 | #include <kernel.h>
|
---|
49 | #include <sil.h>
|
---|
50 | #include "target_serial.h"
|
---|
51 | #include "target_syssvc.h"
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * WX^Ýè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 | * USARTWX^è`
|
---|
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 | * VAI/O|[gÇubNGA
|
---|
85 | */
|
---|
86 | SIOPCB siopcb_table[TNUM_PORT];
|
---|
87 |
|
---|
88 | static const uint32_t sioreg_table[TNUM_PORT] = {
|
---|
89 | USART1_BASE,
|
---|
90 | };
|
---|
91 |
|
---|
92 | #pragma inline
|
---|
93 | bool_t sio_putready(SIOPCB* siopcb)
|
---|
94 | {
|
---|
95 | return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_TXE) != 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | #pragma inline
|
---|
99 | bool_t sio_getready(SIOPCB* siopcb)
|
---|
100 | {
|
---|
101 | return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_RXNE) != 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * ^[QbgÌVAú»
|
---|
106 | */
|
---|
107 | void 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ø»AG[ÝÌ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 | * ^[QbgÌVAI¹
|
---|
150 | */
|
---|
151 | void 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 | */
|
---|
162 | void 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 | * VAI[v
|
---|
175 | */
|
---|
176 | SIOPCB *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 | * VAN[Y
|
---|
194 | */
|
---|
195 | void sio_cls_por(SIOPCB *p_siopcb)
|
---|
196 | {
|
---|
197 | target_usart_term(PORT2SIOPID(p_siopcb->port));
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Ýnh
|
---|
202 | */
|
---|
203 | void 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 | */
|
---|
218 | bool_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 | */
|
---|
237 | int_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[obNÌÂ
|
---|
250 | */
|
---|
251 | void 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[obNÌÖ~
|
---|
267 | */
|
---|
268 | void 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 | */
|
---|
285 | void 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 | }
|
---|