source: rtos_arduino/trunk/asp_1.9.2/arch/arm_m_gcc/stm32f/usart.c@ 136

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

ライブラリとOS及びベーシックなサンプルの追加.

File size: 7.2 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,2011,2013 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 */
54
55/*
56 * シリアルドライバ(STM32F USART用)
57 */
58
59#include <kernel.h>
60#include <sil.h>
61#include "usart.h"
62#include "target_syssvc.h"
63
64/*
65 * レジスタ設定値
66 */
67#define PORT2SIOPID(x) ((x) + 1)
68#define INDEX_PORT(x) ((x) - 1)
69#define GET_SIOPCB(x) (&siopcb_table[INDEX_PORT(x)])
70
71/*
72 * USARTレジスタ定義
73 */
74#define USART_SR(x) (x)
75#define USART_DR(x) (x + 0x04)
76#define USART_BRR(x) (x + 0x08)
77#define USART_CR1(x) (x + 0x0C)
78#define USART_CR2(x) (x + 0x10)
79#define USART_CR3(x) (x + 0x14)
80#define USART_GTPR(x) (x + 0x18)
81
82#define SR_TXE (0x0080)
83#define SR_RXNE (0x0020)
84#define SR_ORE (0x0008)
85#define SR_FE (0x0002)
86#define SR_PE (0x0001)
87#define CR1_UE (0x2000)
88#define CR1_TXEIE (0x0080)
89#define CR1_RXNEIE (0x0020)
90#define CR1_TE (0x0008)
91#define CR1_RE (0x0004)
92#define CR3_EIE (0x0001)
93
94/*
95 * シリアルポートの管理ブロック
96 */
97struct sio_port_control_block {
98 ID port;
99 uint32_t reg;
100 intptr_t exinf;
101};
102
103/*
104 * シリアルI/Oポート管理ブロックエリア
105 */
106SIOPCB siopcb_table[TNUM_PORT];
107
108static const uint32_t sioreg_table[TNUM_PORT] = {
109 USART1_BASE,
110#if (TNUM_PORT >= 2)
111 USART2_BASE
112#endif
113};
114
115Inline bool_t
116sio_putready(SIOPCB* siopcb)
117{
118 return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_TXE) != 0;
119}
120
121Inline bool_t
122sio_getready(SIOPCB* siopcb)
123{
124 return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_RXNE) != 0;
125}
126
127/*
128 * ターゲットのシリアル初期化
129 */
130void
131usart_init(ID siopid)
132{
133 uint32_t tmp, usartdiv, fraction;
134 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
135 uint32_t src_clock;
136
137 /* USARTの無効化 */
138 sil_andw((void*)USART_CR1(reg), ~CR1_UE);
139
140 /* 1STOP BIT */
141 sil_wrw_mem((void*)USART_CR2(reg), 0);
142
143 /* 1START BIT, 8DATA bits, Parityなし */
144 sil_wrw_mem((void*)USART_CR1(reg), 0);
145
146 /* CR3初期化 */
147 sil_wrw_mem((void*)USART_CR3(reg), 0);
148
149 /* 通信速度設定 */
150 if (siopid == 1) {
151 /* USART1のみPCLK2を使用する */
152 src_clock = PCLK2_CLOCK;
153 } else {
154 src_clock = PCLK1_CLOCK;
155 }
156 tmp = (1000 * (src_clock / 100)) / ((BPS_SETTING / 100) * 16);
157 usartdiv = (tmp / 1000) << 4;
158 fraction = tmp - ((usartdiv >> 4) * 1000);
159 fraction = ((16 * fraction) + 500) / 1000;
160 usartdiv |= (fraction & 0x0F);
161 sil_wrw_mem((void*)USART_BRR(reg), usartdiv);
162
163 /* 送受信の有効化、エラー割込みの有効化 */
164 sil_orw((void*)USART_CR1(reg), CR1_RE | CR1_TE);
165 sil_orw((void*)USART_CR3(reg), CR3_EIE);
166
167 /* USARTの有効化 */
168 sil_orw((void*)USART_CR1(reg), CR1_UE);
169}
170
171/*
172 * ターゲットのシリアル終了
173 */
174static void
175usart_term(ID siopid)
176{
177 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
178
179 /* USARTの無効化 */
180 sil_andw((void*)USART_CR1(reg), ~CR1_UE);
181}
182
183/*
184 * SIO初期化
185 */
186void
187sio_initialize(intptr_t exinf)
188{
189 int i;
190
191 for (i = 0; i < TNUM_PORT; i++) {
192 siopcb_table[i].port = i;
193 siopcb_table[i].reg = sioreg_table[i];
194 siopcb_table[i].exinf = 0;
195 }
196}
197
198/*
199 * シリアルオープン
200 */
201SIOPCB
202*sio_opn_por(ID siopid, intptr_t exinf)
203{
204 SIOPCB* siopcb;
205
206 if (siopid > TNUM_PORT) {
207 return NULL;
208 }
209
210 siopcb = GET_SIOPCB(siopid);
211 siopcb->exinf = exinf;
212
213 usart_init(siopid);
214
215 return siopcb;
216}
217
218/*
219 * シリアルクローズ
220 */
221void
222sio_cls_por(SIOPCB *p_siopcb)
223{
224 usart_term(PORT2SIOPID(p_siopcb->port));
225}
226
227/*
228 * 割込みサービスルーチン
229 */
230void
231sio_isr(intptr_t exinf)
232{
233 SIOPCB* siopcb = GET_SIOPCB(exinf);
234
235 if (sio_putready(siopcb)) {
236 sio_irdy_snd(siopcb->exinf);
237 }
238 if (sio_getready(siopcb)) {
239 sio_irdy_rcv(siopcb->exinf);
240 }
241}
242
243/*
244 * 1文字送信
245 */
246bool_t
247sio_snd_chr(SIOPCB *siopcb, char c)
248{
249 if (sio_putready(siopcb)) {
250 sil_wrw_mem((void*)USART_DR(siopcb->reg), c);
251
252 return true;
253 }
254
255 return false;
256}
257
258/*
259 * 1文字受信
260 */
261int_t
262sio_rcv_chr(SIOPCB *siopcb)
263{
264 int_t c = -1;
265
266 if (sio_getready(siopcb)) {
267 c = sil_rew_mem((void*)USART_DR(siopcb->reg)) & 0xFF;
268 }
269
270 return c;
271}
272
273/*
274 * コールバックの許可
275 */
276void
277sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
278{
279 switch (cbrtn) {
280 case SIO_RDY_SND:
281 sil_orw((void*)USART_CR1(siopcb->reg), CR1_TXEIE);
282 break;
283 case SIO_RDY_RCV:
284 sil_orw((void*)USART_CR1(siopcb->reg), CR1_RXNEIE);
285 break;
286 default:
287 break;
288 }
289}
290
291/*
292 * コールバックの禁止
293 */
294void
295sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
296{
297 switch (cbrtn) {
298 case SIO_RDY_SND:
299 sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_TXEIE);
300 break;
301 case SIO_RDY_RCV:
302 sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_RXNEIE);
303 break;
304 default:
305 break;
306 }
307}
308
309/*
310 * 1文字出力(ポーリングでの出力)
311 */
312void
313sio_pol_snd_chr(char c, ID siopid)
314{
315 uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
316
317 sil_wrw_mem((void*)USART_DR(reg), c);
318
319 while ((sil_rew_mem((void*)USART_SR(reg)) & SR_TXE) == 0) ;
320}
Note: See TracBrowser for help on using the repository browser.