source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/usart.c@ 303

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

nucleo_f401re依存部の追加

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