[93] | 1 | /*
|
---|
| 2 | * TOPPERS/SSP 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 | * Copyright (C) 2012 by Meika Sugimoto
|
---|
| 9 | * Copyright (C) 2015 by Naoki Saito
|
---|
| 10 | * Nagoya Municipal Industrial Research Institute, JAPAN
|
---|
| 11 | *
|
---|
| 12 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 13 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 14 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 15 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 16 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 17 | * スコード中に含まれていること.
|
---|
| 18 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 19 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 20 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 21 | * の無保証規定を掲載すること.
|
---|
| 22 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 23 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 24 | * と.
|
---|
| 25 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 26 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 27 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 28 | * 報告すること.
|
---|
| 29 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 30 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 31 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 32 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 33 | * 免責すること.
|
---|
| 34 | *
|
---|
| 35 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 36 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 37 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 38 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 39 | * の責任を負わない.
|
---|
| 40 | *
|
---|
| 41 | */
|
---|
| 42 |
|
---|
| 43 | /*
|
---|
| 44 | * シリアルドライバ(CQ-FRM-FM3用)
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | #include <sil.h>
|
---|
| 48 | #include "kernel_int.h"
|
---|
| 49 | #include "target_serial.h"
|
---|
| 50 | #include "target_syssvc.h"
|
---|
| 51 |
|
---|
| 52 | /*
|
---|
| 53 | * レジスタ設定値
|
---|
| 54 | */
|
---|
| 55 | #define PORT2SIOPID(x) ((x) + 1)
|
---|
| 56 | #define INDEX_PORT(x) ((x) - 1)
|
---|
| 57 | #define GET_SIOPCB(x) (&siopcb_table[INDEX_PORT(x)])
|
---|
| 58 |
|
---|
| 59 | /*
|
---|
| 60 | * USARTレジスタ定義
|
---|
| 61 | * #define MFS_SMR(ch) (MFS_BASEADDR((ch)) + 0x00)
|
---|
| 62 | * #define MFS_SCR(ch) (MFS_BASEADDR((ch)) + 0x01)
|
---|
| 63 | * #define MFS_ESCR(ch) (MFS_BASEADDR((ch)) + 0x04)
|
---|
| 64 | * #define MFS_SSR(ch) (MFS_BASEADDR((ch)) + 0x05)
|
---|
| 65 | * #define MFS_RDRTDR(ch) (MFS_BASEADDR((ch)) + 0x08)
|
---|
| 66 | * #define MFS_BGR0(ch) (MFS_BASEADDR((ch)) + 0x0B)
|
---|
| 67 | * #define MFS_BGR1(ch) (MFS_BASEADDR((ch)) + 0x0C)
|
---|
| 68 | *
|
---|
| 69 | */
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | * ビット割り当てはバイトアクセス用に記述している
|
---|
| 73 | */
|
---|
| 74 |
|
---|
| 75 | /* SCR */
|
---|
| 76 | #define SCR_UPCL (1U << 7)
|
---|
| 77 | #define SCR_RIE (1U << 4)
|
---|
| 78 | #define SCR_TIE (1U << 3)
|
---|
| 79 | #define SCR_TEIE (1U << 2)
|
---|
| 80 | #define SCR_RXE (1U << 1)
|
---|
| 81 | #define SCR_TXE (1U << 0)
|
---|
| 82 |
|
---|
| 83 | /* SMR */
|
---|
| 84 | #define SMR_MODE_ASYNC_NORMAL (0U << 5)
|
---|
| 85 | #define SMR_MODE_ASYNC_MULTI (1U << 5)
|
---|
| 86 | #define SMR_MODE_CLKSYNC (2U << 5)
|
---|
| 87 | #define SMR_MODE_LIN (3U << 5)
|
---|
| 88 | #define SMR_MODE_I2C (4U << 5)
|
---|
| 89 |
|
---|
| 90 | #define SMR_WAKEUP (1U << 4)
|
---|
| 91 |
|
---|
| 92 | #define SMR_SBL_1OR3 (0U << 3) /* ESCR.ESBL = 0で1 */
|
---|
| 93 | #define SMR_SBL_2OR4 (1U << 3) /* ESCR.ESBL = 0で2 */
|
---|
| 94 |
|
---|
| 95 | #define SMR_BDS_LSBFIRST (0U << 2)
|
---|
| 96 | #define SMR_BDS_MSBFIRST (1U << 2)
|
---|
| 97 |
|
---|
| 98 | #define SMR_SOE_DISABLE (0U << 0)
|
---|
| 99 | #define SMR_SOE_ENABLE (1U << 0)
|
---|
| 100 |
|
---|
| 101 | /* SSR */
|
---|
| 102 | #define SSR_REC (1U << 7)
|
---|
| 103 | #define SSR_PE (1U << 5)
|
---|
| 104 | #define SSR_FRE (1U << 4)
|
---|
| 105 | #define SSR_ORE (1U << 3)
|
---|
| 106 | #define SSR_RDRF (1U << 2)
|
---|
| 107 | #define SSR_TDRE (1U << 1)
|
---|
| 108 | #define SSR_TBI (1U << 0)
|
---|
| 109 |
|
---|
| 110 | /* ESCR */
|
---|
| 111 | #define ESCR_FLWEN_ENABLE (1U << 7)
|
---|
| 112 | #define ESCR_FLWEN_DISABLE (0U << 7)
|
---|
| 113 | #define ESCR_ESBL (1U << 6)
|
---|
| 114 | #define ESCR_INV_NRZ (0U << 5)
|
---|
| 115 | #define ESCR_INV_INVNRZ (1U << 5)
|
---|
| 116 | #define ESCR_PEN (1U << 4)
|
---|
| 117 | #define ESCR_PEN_DISABLE (0U << 4)
|
---|
| 118 | #define ESCR_PEN_ENABLE (1U << 4)
|
---|
| 119 | #define ESCR_P_EVEN (0U << 3)
|
---|
| 120 | #define ESCR_P_ODD (1U << 3)
|
---|
| 121 | #define ESCR_DATALEN_8 (0U << 0)
|
---|
| 122 | #define ESCR_DATALEN_5 (1U << 0)
|
---|
| 123 | #define ESCR_DATALEN_6 (2U << 0)
|
---|
| 124 | #define ESCR_DATALEN_7 (3U << 0)
|
---|
| 125 | #define ESCR_DATALEN_9 (4U << 0)
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | /*
|
---|
| 129 | * シリアルI/Oポート管理ブロックエリア
|
---|
| 130 | */
|
---|
| 131 | SIOPCB siopcb_table[TNUM_PORT];
|
---|
| 132 |
|
---|
| 133 | static const uint32_t sioreg_table[TNUM_PORT] = {
|
---|
| 134 | MFS_BASEADDR(0),
|
---|
| 135 | #if (TNUM_PORT >= 2)
|
---|
| 136 | MFS_BASEADDR(4)
|
---|
| 137 | #endif
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 | Inline bool_t sio_putready(SIOPCB* siopcb)
|
---|
| 141 | {
|
---|
| 142 | return ((sil_reb_mem((void*)MFS_SSR(siopcb->port)) & SSR_TDRE) != 0);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | Inline bool_t sio_getready(SIOPCB* siopcb)
|
---|
| 146 | {
|
---|
| 147 | return ((sil_reb_mem((void*)MFS_SSR(siopcb->port)) & SSR_RDRF) != 0);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /*
|
---|
| 151 | * 低レベル出力の初期化
|
---|
| 152 | */
|
---|
| 153 |
|
---|
| 154 | void target_low_output_init(ID siopid)
|
---|
| 155 | {
|
---|
| 156 | uint32_t ch = INDEX_PORT(siopid);
|
---|
| 157 |
|
---|
| 158 | /* 初期化済みの場合はすぐにリターン */
|
---|
| 159 | if((sil_reb_mem((void*)MFS_SCR(ch)) & (SCR_TXE | SCR_RXE)) != 0)
|
---|
| 160 | {
|
---|
| 161 | return ;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /* USARTの無効化,リセット */
|
---|
| 165 | sil_wrb_mem((void *)MFS_SCR(ch) , SCR_UPCL);
|
---|
| 166 | sil_wrb_mem((void *)MFS_SCR(ch) , 0x0);
|
---|
| 167 |
|
---|
| 168 | /* 通信モード設定 */
|
---|
| 169 | sil_wrb_mem((void *)MFS_SMR(ch) ,
|
---|
| 170 | (SMR_MODE_ASYNC_NORMAL | SMR_SBL_1OR3 | SMR_BDS_LSBFIRST | SMR_SOE_DISABLE));
|
---|
| 171 | sil_wrb_mem((void *)MFS_ESCR(ch) ,
|
---|
| 172 | (ESCR_FLWEN_DISABLE | ESCR_INV_NRZ | ESCR_PEN_DISABLE | ESCR_DATALEN_8));
|
---|
| 173 | /* 通信速度設定 */
|
---|
| 174 | sil_wrh_mem((void *)MFS_BGR0(ch) , BPS_SETTING); /* ハーフワードアクセスする */
|
---|
| 175 | /* FIFO無効化 */
|
---|
| 176 | sil_wrb_mem((void *)MFS_FCR0(ch) , 0x00);
|
---|
| 177 | sil_wrb_mem((void *)MFS_FCR1(ch) , 0x00);
|
---|
| 178 |
|
---|
| 179 | /* USARTの有効化 */
|
---|
| 180 | sil_orb((void *)MFS_SMR(ch) , SMR_SOE_ENABLE);
|
---|
| 181 | sil_orb((void*)MFS_SCR(ch) , (SCR_TXE | SCR_RXE));
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /*
|
---|
| 185 | * ターゲットのシリアル初期化
|
---|
| 186 | */
|
---|
| 187 | void target_usart_init(ID siopid)
|
---|
| 188 | {
|
---|
| 189 | target_low_output_init(siopid);
|
---|
| 190 |
|
---|
| 191 | /* 割込み許可 */
|
---|
| 192 | x_clear_int(INTNO_SIO_TX);
|
---|
| 193 | x_clear_int(INTNO_SIO_RX);
|
---|
| 194 | /* 割込み禁止 */
|
---|
| 195 | (void)ena_int(INTNO_SIO_TX);
|
---|
| 196 | (void)ena_int(INTNO_SIO_RX);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /*
|
---|
| 200 | * ターゲットのシリアル終了
|
---|
| 201 | */
|
---|
| 202 | void target_usart_term(ID siopid)
|
---|
| 203 | {
|
---|
| 204 | /* 割込み禁止 */
|
---|
| 205 | (void)dis_int(INTNO_SIO_TX);
|
---|
| 206 | (void)dis_int(INTNO_SIO_RX);
|
---|
| 207 |
|
---|
| 208 | /* USARTの無効化 */
|
---|
| 209 | sil_andb((void*)MFS_SCR(INDEX_PORT(siopid)) , (SCR_TXE | SCR_RIE));
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /*
|
---|
| 213 | * SIO初期化
|
---|
| 214 | */
|
---|
| 215 | void sio_initialize(intptr_t exinf)
|
---|
| 216 | {
|
---|
| 217 | int i;
|
---|
| 218 |
|
---|
| 219 | for (i = 0; i < TNUM_PORT; i++) {
|
---|
| 220 | siopcb_table[i].port = i;
|
---|
| 221 | siopcb_table[i].reg = sioreg_table[i];
|
---|
| 222 | siopcb_table[i].exinf = 0;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | /*
|
---|
| 227 | * シリアルオープン
|
---|
| 228 | */
|
---|
| 229 | SIOPCB *sio_opn_por(ID siopid, intptr_t exinf)
|
---|
| 230 | {
|
---|
| 231 | SIOPCB* siopcb;
|
---|
| 232 |
|
---|
| 233 | if (siopid > TNUM_PORT) {
|
---|
| 234 | return NULL;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | siopcb = GET_SIOPCB(siopid);
|
---|
| 238 | siopcb->exinf = exinf;
|
---|
| 239 |
|
---|
| 240 | target_usart_init(siopid);
|
---|
| 241 |
|
---|
| 242 | return siopcb;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /*
|
---|
| 246 | * シリアルクローズ
|
---|
| 247 | */
|
---|
| 248 | void sio_cls_por(SIOPCB *p_siopcb)
|
---|
| 249 | {
|
---|
| 250 | target_usart_term(p_siopcb->port);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /*
|
---|
| 254 | * 割込みハンドラ
|
---|
| 255 | */
|
---|
| 256 | void sio_isr_tx(intptr_t exinf)
|
---|
| 257 | {
|
---|
| 258 | SIOPCB* siopcb = GET_SIOPCB(exinf);
|
---|
| 259 |
|
---|
| 260 | if (sio_putready(siopcb)) {
|
---|
| 261 | sio_irdy_snd(siopcb->exinf);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | void sio_isr_rx(intptr_t exinf)
|
---|
| 266 | {
|
---|
| 267 | SIOPCB* siopcb = GET_SIOPCB(exinf);
|
---|
| 268 |
|
---|
| 269 | if (sio_getready(siopcb)) {
|
---|
| 270 | sio_irdy_rcv(siopcb->exinf);
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | /*
|
---|
| 275 | * 1文字送信
|
---|
| 276 | */
|
---|
| 277 | bool_t sio_snd_chr(SIOPCB *siopcb, char c)
|
---|
| 278 | {
|
---|
| 279 | bool_t ret = false;
|
---|
| 280 |
|
---|
| 281 | if (sio_putready(siopcb)) {
|
---|
| 282 | sil_wrb_mem((void*)MFS_RDRTDR(siopcb->port), c);
|
---|
| 283 |
|
---|
| 284 | ret = true;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return ret;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /*
|
---|
| 291 | * 1文字受信
|
---|
| 292 | */
|
---|
| 293 | int_t sio_rcv_chr(SIOPCB *siopcb)
|
---|
| 294 | {
|
---|
| 295 | int_t c = -1;
|
---|
| 296 |
|
---|
| 297 | if (sio_getready(siopcb)) {
|
---|
| 298 | c = (int)sil_reb_mem((void*)MFS_RDRTDR(siopcb->port));
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | return c;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | /*
|
---|
| 305 | * コールバックの許可
|
---|
| 306 | */
|
---|
| 307 | void sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 308 | {
|
---|
| 309 | switch (cbrtn) {
|
---|
| 310 | case SIO_RDY_SND:
|
---|
| 311 | sil_orb((void*)MFS_SCR(siopcb->port), SCR_TIE);
|
---|
| 312 | break;
|
---|
| 313 | case SIO_RDY_RCV:
|
---|
| 314 | sil_orb((void*)MFS_SCR(siopcb->port), SCR_RIE);
|
---|
| 315 | break;
|
---|
| 316 | default:
|
---|
| 317 | break;
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | /*
|
---|
| 322 | * コールバックの禁止
|
---|
| 323 | */
|
---|
| 324 | void sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 325 | {
|
---|
| 326 | switch (cbrtn) {
|
---|
| 327 | case SIO_RDY_SND:
|
---|
| 328 | sil_andb((void*)MFS_SCR(siopcb->port), ~SCR_TIE);
|
---|
| 329 | break;
|
---|
| 330 | case SIO_RDY_RCV:
|
---|
| 331 | sil_andb((void*)MFS_SCR(siopcb->port), ~SCR_RIE);
|
---|
| 332 | break;
|
---|
| 333 | default:
|
---|
| 334 | break;
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | /*
|
---|
| 339 | * 1文字出力(ポーリングでの出力)
|
---|
| 340 | */
|
---|
| 341 | void sio_pol_snd_chr(char c, ID siopid)
|
---|
| 342 | {
|
---|
| 343 | sil_wrb_mem((void*)MFS_RDRTDR(INDEX_PORT(siopid)), (int)c);
|
---|
| 344 |
|
---|
| 345 | while ((sil_reb_mem((void*)MFS_SSR(INDEX_PORT(siopid))) & SSR_TDRE) == 0U)
|
---|
| 346 | ;
|
---|
| 347 | }
|
---|