[86] | 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) 2013-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 | * シリアルドライバ(LPCXPRESSO-LPC812 用)
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | #include <kernel.h>
|
---|
| 48 | #include <sil.h>
|
---|
| 49 | //#include "kernel_int.h"
|
---|
| 50 | #include "target_serial.h"
|
---|
| 51 | #include "target_syssvc.h"
|
---|
| 52 |
|
---|
| 53 | /*
|
---|
| 54 | * レジスタ設定値
|
---|
| 55 | */
|
---|
| 56 | #define INDEX2SIOPID(index) ((index) + 1) // インデックスからポートIDへ
|
---|
| 57 | #define SIOPID2INDEX(siopid) ((siopid) - 1) // ポートIDからインデックスへ
|
---|
| 58 | #define GET_SIOPCB(siopid) (&siopcb_table[SIOPID2INDEX(siopid)])
|
---|
| 59 | #define GET_SIOPID(siopcb) ((siopcb-(uint32_t)siopcb_table)/sizeof(siopcb_table[0]))
|
---|
| 60 |
|
---|
| 61 | /*
|
---|
| 62 | * シリアルI/Oポート管理ブロックエリア
|
---|
| 63 | */
|
---|
| 64 | SIOPCB siopcb_table[TNUM_PORT];
|
---|
| 65 |
|
---|
| 66 | // シリアルドライバ(GDIC)のポートIDに対応するシリアルポート(PDIC)のレジスタベースアドレス
|
---|
| 67 | // 対応関係を変更したい場合(例えばポートID1をUSART1に対応づける場合),ここを変更する
|
---|
| 68 | static const uint32_t sioreg_table[TNUM_PORT] = {
|
---|
| 69 | TADR_USART_BASE(0),
|
---|
| 70 | #if (TNUM_PORT == 2)
|
---|
| 71 | TADR_USART_BASE(1),
|
---|
| 72 | #endif
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | Inline bool_t sio_putready(SIOPCB* siopcb)
|
---|
| 76 | {
|
---|
| 77 | uint32_t tmp;
|
---|
| 78 | tmp = sil_rew_mem((void *)TADR_USART_STAT(siopcb->reg));
|
---|
| 79 |
|
---|
| 80 | return (tmp & TBITPTN_USART_STAT_TXRDY) != 0;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | Inline bool_t sio_getready(SIOPCB* siopcb)
|
---|
| 84 | {
|
---|
| 85 | uint32_t tmp;
|
---|
| 86 | tmp = sil_rew_mem((void *)TADR_USART_STAT(siopcb->reg));
|
---|
| 87 |
|
---|
| 88 | return (tmp & TBITPTN_USART_STAT_RXRDY) != 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | static bool_t initialized[TNUM_SIOP] = {false};
|
---|
| 92 | /*
|
---|
| 93 | * 低レベル出力の初期化
|
---|
| 94 | */
|
---|
| 95 | void target_low_output_init(ID siopid)
|
---|
| 96 | {
|
---|
| 97 | uint32_t base = TADR_USART_BASE(SIOPID2INDEX(siopid));
|
---|
| 98 | volatile uint32_t tmp;
|
---|
| 99 |
|
---|
| 100 | if(initialized[siopid]) {return;}
|
---|
| 101 |
|
---|
| 102 | // UART回路に供給するクロック(メインクロックの分周比)
|
---|
| 103 | sil_wrw_mem((void *)TADR_SYSCON_UARTCLKDIV, TVALUE_SYSCON_UARTCLKDIV);
|
---|
| 104 |
|
---|
| 105 | /* USARTの無効化,リセット */
|
---|
| 106 | dis_int(INT_UART0);
|
---|
| 107 | tmp = sil_rew_mem((void *)TADR_USART_CFG(base));
|
---|
| 108 | tmp &= ~TBITPTN_USART_CFG_ENABLE;
|
---|
| 109 | sil_wrw_mem((void *)TADR_USART_CFG(base), tmp);
|
---|
| 110 |
|
---|
| 111 | // UART0のクロックをON
|
---|
| 112 | tmp = sil_rew_mem((void *)TADR_SYSCON_SYSAHBCLKCTRL);
|
---|
| 113 | tmp |= TBITPTN_SYSCON_SYSAHBCLKCTRL_UART0;
|
---|
| 114 | sil_wrw_mem((void *)TADR_SYSCON_SYSAHBCLKCTRL, tmp);
|
---|
| 115 |
|
---|
| 116 | // デバイスをリセット
|
---|
| 117 | tmp = sil_rew_mem((void *)TADR_SYSCON_PRESETCTRL);
|
---|
| 118 | tmp &= ~TBITPTN_SYSCON_PRESETCTRL_USART0_RST_N;
|
---|
| 119 | sil_wrw_mem((void *)TADR_SYSCON_PRESETCTRL, tmp);
|
---|
| 120 | tmp |= TBITPTN_SYSCON_PRESETCTRL_USART0_RST_N;
|
---|
| 121 | sil_wrw_mem((void *)TADR_SYSCON_PRESETCTRL, tmp);
|
---|
| 122 |
|
---|
| 123 | /* 通信モード設定 */
|
---|
| 124 | tmp = (TBITPTN_USART_CFG_DATALEN_8BIT | TBITPTN_USART_CFG_PARITYSEL_NOPARITY | TBITPTN_USART_CFG_STOPLEN_1BIT);
|
---|
| 125 | sil_wrw_mem((void *)TADR_USART_CFG(base), tmp);
|
---|
| 126 |
|
---|
| 127 | /* 通信速度設定 */
|
---|
| 128 | sil_wrw_mem((void *)TADR_USART_BRG(base), TVALUE_USART_BRG(UART_BAUDRATE));
|
---|
| 129 | sil_wrw_mem((void *)TADR_SYSCON_UARTFRGDIV, TVALUE_SYSCON_UARTFRGDIV);
|
---|
| 130 | sil_wrw_mem((void *)TADR_SYSCON_UARTFRGMULT, TVALUE_SYSCON_UARTFRGMULT(UART_BAUDRATE));
|
---|
| 131 |
|
---|
| 132 | // CTS/RTSのクリア
|
---|
| 133 | sil_wrw_mem((void *)TADR_USART_STAT(base), (TBITPTN_USART_STAT_DELTACTS | TBITPTN_USART_STAT_DELTARXBRK));
|
---|
| 134 |
|
---|
| 135 | // UARTの割込み許可
|
---|
| 136 | // ena_int(INT_UART0);
|
---|
| 137 |
|
---|
| 138 | // UARTの個別の割込み有効/無効(受信:有効,送信:無効)
|
---|
| 139 | // sil_wrw_mem((void *)TADR_USART_INTENSET(base), TBITPTN_USART_INTENSET_RXRDYEN);
|
---|
| 140 | // sil_wrw_mem((void *)TADR_USART_INTENCLR(base), TBITPTN_USART_INTENCLR_TXRDYCLR);
|
---|
| 141 |
|
---|
| 142 | /* USARTの有効化 */
|
---|
| 143 | tmp = sil_rew_mem((void *)TADR_USART_CFG(base));
|
---|
| 144 | tmp |= TBITPTN_USART_CFG_ENABLE;
|
---|
| 145 | sil_wrw_mem((void *)TADR_USART_CFG(base), tmp);
|
---|
| 146 |
|
---|
| 147 | initialized[siopid] = true;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /*
|
---|
| 151 | * ターゲットのシリアル初期化
|
---|
| 152 | */
|
---|
| 153 | void target_usart_init(ID siopid)
|
---|
| 154 | {
|
---|
| 155 | target_low_output_init(siopid);
|
---|
| 156 |
|
---|
| 157 | /* 割込み許可 */
|
---|
| 158 | (void)ena_int(INTNO_SIO);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * ターゲットのシリアル終了
|
---|
| 163 | */
|
---|
| 164 | void target_usart_term(uint32_t base)
|
---|
| 165 | {
|
---|
| 166 | uint32_t tmp;
|
---|
| 167 |
|
---|
| 168 | /* 割込み禁止 */
|
---|
| 169 | (void)dis_int(INTNO_SIO);
|
---|
| 170 |
|
---|
| 171 | /* USARTの無効化 */
|
---|
| 172 | tmp = sil_rew_mem((void *)TADR_USART_CFG(base));
|
---|
| 173 | tmp &= ~TBITPTN_USART_CFG_ENABLE;
|
---|
| 174 | sil_wrw_mem((void *)TADR_USART_CFG(base), tmp);
|
---|
| 175 |
|
---|
| 176 | int_t siopid = GET_SIOPID(base);
|
---|
| 177 | initialized[siopid] = false;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /*
|
---|
| 181 | * SIO初期化
|
---|
| 182 | */
|
---|
| 183 | void sio_initialize(intptr_t exinf)
|
---|
| 184 | {
|
---|
| 185 | int i;
|
---|
| 186 |
|
---|
| 187 | for (i = 0; i < TNUM_PORT; i++) {
|
---|
| 188 | siopcb_table[i].reg = sioreg_table[i];
|
---|
| 189 | siopcb_table[i].exinf = 0;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /*
|
---|
| 194 | * シリアルオープン
|
---|
| 195 | */
|
---|
| 196 | SIOPCB *sio_opn_por(ID siopid, intptr_t exinf)
|
---|
| 197 | {
|
---|
| 198 | SIOPCB* siopcb;
|
---|
| 199 |
|
---|
| 200 | if (siopid > TNUM_PORT || siopid < 0) {
|
---|
| 201 | return NULL;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | target_usart_init(siopid);
|
---|
| 205 |
|
---|
| 206 | siopcb = GET_SIOPCB(siopid);
|
---|
| 207 | siopcb->exinf = exinf;
|
---|
| 208 |
|
---|
| 209 | return siopcb;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /*
|
---|
| 213 | * シリアルクローズ
|
---|
| 214 | */
|
---|
| 215 | void sio_cls_por(SIOPCB *p_siopcb)
|
---|
| 216 | {
|
---|
| 217 | target_usart_term(p_siopcb->reg);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /*
|
---|
| 221 | * 割込みハンドラ
|
---|
| 222 | */
|
---|
| 223 | void sio_isr(intptr_t exinf)
|
---|
| 224 | {
|
---|
| 225 | SIOPCB* siopcb = GET_SIOPCB(exinf);
|
---|
| 226 |
|
---|
| 227 | if (sio_putready(siopcb)) {
|
---|
| 228 | sio_irdy_snd(siopcb->exinf);
|
---|
| 229 | }
|
---|
| 230 | if (sio_getready(siopcb)) {
|
---|
| 231 | sio_irdy_rcv(siopcb->exinf);
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 | /*
|
---|
| 237 | * 1文字送信
|
---|
| 238 | */
|
---|
| 239 | bool_t sio_snd_chr(SIOPCB *siopcb, char c)
|
---|
| 240 | {
|
---|
| 241 | bool_t ret = false;
|
---|
| 242 |
|
---|
| 243 | if (sio_putready(siopcb)) {
|
---|
| 244 | sil_wrw_mem((void *)TADR_USART_TXDATA(siopcb->reg), c);
|
---|
| 245 | ret = true;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | return ret;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /*
|
---|
| 252 | * 1文字受信
|
---|
| 253 | */
|
---|
| 254 | int_t sio_rcv_chr(SIOPCB *siopcb)
|
---|
| 255 | {
|
---|
| 256 | int_t c = -1;
|
---|
| 257 |
|
---|
| 258 | if (sio_getready(siopcb)) {
|
---|
| 259 | c = (int_t)sil_rew_mem((void *)TADR_USART_RXDATA(siopcb->reg));
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | return c;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /*
|
---|
| 266 | * コールバックの許可
|
---|
| 267 | */
|
---|
| 268 | void sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 269 | {
|
---|
| 270 | switch (cbrtn) {
|
---|
| 271 | case SIO_RDY_SND:
|
---|
| 272 | sil_wrw_mem((void *)TADR_USART_INTENSET(siopcb->reg), TBITPTN_USART_INTENSET_TXRDYEN);
|
---|
| 273 | break;
|
---|
| 274 | case SIO_RDY_RCV:
|
---|
| 275 | sil_wrw_mem((void *)TADR_USART_INTENSET(siopcb->reg), TBITPTN_USART_INTENSET_RXRDYEN);
|
---|
| 276 | break;
|
---|
| 277 | default:
|
---|
| 278 | break;
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /*
|
---|
| 283 | * コールバックの禁止
|
---|
| 284 | */
|
---|
| 285 | void sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 286 | {
|
---|
| 287 | switch (cbrtn) {
|
---|
| 288 | case SIO_RDY_SND:
|
---|
| 289 | sil_wrw_mem((void *)TADR_USART_INTENCLR(siopcb->reg), TBITPTN_USART_INTENCLR_TXRDYCLR);
|
---|
| 290 | break;
|
---|
| 291 | case SIO_RDY_RCV:
|
---|
| 292 | sil_wrw_mem((void *)TADR_USART_INTENCLR(siopcb->reg), TBITPTN_USART_INTENCLR_RXRDYCLR);
|
---|
| 293 | break;
|
---|
| 294 | default:
|
---|
| 295 | break;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /*
|
---|
| 300 | * 1文字出力(ポーリングでの出力)
|
---|
| 301 | */
|
---|
| 302 | void sio_pol_snd_chr(char c, ID siopid)
|
---|
| 303 | {
|
---|
| 304 | uint32_t base = TADR_USART_BASE(SIOPID2INDEX(siopid));
|
---|
| 305 | volatile uint32_t tmp;
|
---|
| 306 |
|
---|
| 307 | do {
|
---|
| 308 | tmp = sil_rew_mem((void *)TADR_USART_STAT(base));
|
---|
| 309 | } while((tmp & TBITPTN_USART_STAT_TXRDY) == 0);
|
---|
| 310 |
|
---|
| 311 | sil_wrw_mem((void *)TADR_USART_TXDATA(base), (uint32_t)c);
|
---|
| 312 | }
|
---|