Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
uart.cpp@25:f3a6e7eec9c3, 2018-12-12 (annotated)
- Committer:
- MasashiNomura
- Date:
- Wed Dec 12 23:52:22 2018 +0000
- Revision:
- 25:f3a6e7eec9c3
- Parent:
- 21:78302ecdb661
- Child:
- 26:732bc37fbefd
2018/12/11
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| takeru0x1103 | 0:ecd925601fc6 | 1 | #include "typedef.h" |
| takeru0x1103 | 0:ecd925601fc6 | 2 | #include "uart.h" |
| takeru0x1103 | 19:4b0fe9a5ec38 | 3 | #include "mbed.h" |
| takeru0x1103 | 19:4b0fe9a5ec38 | 4 | #include "globalFlags.h" |
| takeru0x1103 | 0:ecd925601fc6 | 5 | |
| takeru0x1103 | 8:1ca49cb18290 | 6 | //ログ吐出し様シリアルポートインスタンス |
| takeru0x1103 | 8:1ca49cb18290 | 7 | //---------------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 8 | #define WIREED_SERIAL |
| takeru0x1103 | 0:ecd925601fc6 | 9 | #ifdef WIREED_SERIAL |
| takeru0x1103 | 0:ecd925601fc6 | 10 | Serial sp(USBTX, USBRX); //有線のポート |
| takeru0x1103 | 8:1ca49cb18290 | 11 | #define BAUD_RATE 115200 |
| takeru0x1103 | 0:ecd925601fc6 | 12 | #else |
| takeru0x1103 | 0:ecd925601fc6 | 13 | Serial sp(p9, p10);//TWILITEをモジュールを接続したポート |
| takeru0x1103 | 0:ecd925601fc6 | 14 | #define BAUD_RATE 115200 |
| takeru0x1103 | 0:ecd925601fc6 | 15 | #endif |
| MasashiNomura | 7:bfbbf605be43 | 16 | |
| MasashiNomura | 14:76a56d517103 | 17 | //Serial sp46Axis(p28,p27); |
| MasashiNomura | 7:bfbbf605be43 | 18 | |
| takeru0x1103 | 8:1ca49cb18290 | 19 | //Asciiコード |
| takeru0x1103 | 8:1ca49cb18290 | 20 | #define CR 0x0D |
| takeru0x1103 | 8:1ca49cb18290 | 21 | #define LF 0x0A |
| takeru0x1103 | 19:4b0fe9a5ec38 | 22 | |
| takeru0x1103 | 19:4b0fe9a5ec38 | 23 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 24 | //グローバル変数 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 25 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 26 | char cmdBuf[G_CMD_BUF_SIZ] ={0,} ;// コマンドバッファ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 27 | static UCHAR bp=0;// バッファポインタ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 28 | |
| takeru0x1103 | 19:4b0fe9a5ec38 | 29 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 30 | //受信コマンドバッファクリア |
| takeru0x1103 | 19:4b0fe9a5ec38 | 31 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 32 | static void xBufClear(){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 33 | memset(&cmdBuf[0] , 0x00 , sizeof(cmdBuf) ); //コマンドバッファクリア |
| takeru0x1103 | 19:4b0fe9a5ec38 | 34 | bp=0; //バッファポインタを先頭に戻す |
| takeru0x1103 | 19:4b0fe9a5ec38 | 35 | } |
| takeru0x1103 | 19:4b0fe9a5ec38 | 36 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 37 | //エンターが押されてコマンドを転送 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 38 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 39 | static void xSendMsg(){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 40 | |
| takeru0x1103 | 19:4b0fe9a5ec38 | 41 | memcpy(g_CmdBuf, cmdBuf, sizeof(g_CmdBuf)); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 42 | gf_CmdPrs = true; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 43 | xBufClear(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 44 | } |
| takeru0x1103 | 19:4b0fe9a5ec38 | 45 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 46 | //バックスペースが来た時の処理 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 47 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 48 | static void xBs(){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 49 | cmdBuf[bp] = 0x0; //現在位置をNULLにする |
| takeru0x1103 | 19:4b0fe9a5ec38 | 50 | if(bp>0) bp--; //インデックスを戻す(先頭でなければ) |
| takeru0x1103 | 19:4b0fe9a5ec38 | 51 | } |
| takeru0x1103 | 19:4b0fe9a5ec38 | 52 | |
| takeru0x1103 | 19:4b0fe9a5ec38 | 53 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 54 | //コマンドバッファへ格納 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 55 | //------------------------------------------------------------- |
| takeru0x1103 | 19:4b0fe9a5ec38 | 56 | static void xPush(char cBuf){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 57 | //バッファ終端だったらエラー |
| takeru0x1103 | 19:4b0fe9a5ec38 | 58 | if(bp < sizeof(cmdBuf)-1){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 59 | cmdBuf[bp] = cBuf;//受信文字をバッファへ格納 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 60 | bp++;//次へ進める |
| takeru0x1103 | 19:4b0fe9a5ec38 | 61 | } |
| takeru0x1103 | 19:4b0fe9a5ec38 | 62 | } |
| takeru0x1103 | 19:4b0fe9a5ec38 | 63 | |
| MasashiNomura | 25:f3a6e7eec9c3 | 64 | //------------------------------------------------------------- |
| MasashiNomura | 25:f3a6e7eec9c3 | 65 | //ESCが来た時の処理 |
| MasashiNomura | 25:f3a6e7eec9c3 | 66 | //------------------------------------------------------------- |
| MasashiNomura | 25:f3a6e7eec9c3 | 67 | static void xEsc(){ |
| MasashiNomura | 25:f3a6e7eec9c3 | 68 | //バッファクリア |
| MasashiNomura | 25:f3a6e7eec9c3 | 69 | xBufClear(); |
| MasashiNomura | 25:f3a6e7eec9c3 | 70 | //ESCをセット |
| MasashiNomura | 25:f3a6e7eec9c3 | 71 | xPush(ESC); |
| MasashiNomura | 25:f3a6e7eec9c3 | 72 | xSendMsg(); |
| MasashiNomura | 25:f3a6e7eec9c3 | 73 | } |
| MasashiNomura | 25:f3a6e7eec9c3 | 74 | |
| takeru0x1103 | 19:4b0fe9a5ec38 | 75 | //============================================================= |
| takeru0x1103 | 19:4b0fe9a5ec38 | 76 | // コマンドバッファへ受信キャラを格納 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 77 | //============================================================= |
| takeru0x1103 | 19:4b0fe9a5ec38 | 78 | static void xPushBuf(UCHAR iRxChar){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 79 | switch(iRxChar){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 80 | //▼タブ |
| takeru0x1103 | 21:78302ecdb661 | 81 | case 0x21: //!マーク |
| takeru0x1103 | 21:78302ecdb661 | 82 | // gf.push(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 83 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 84 | //▼バックスペース |
| takeru0x1103 | 19:4b0fe9a5ec38 | 85 | case BS : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 86 | xBs(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 87 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 88 | //▼エスケープ キャリッジリターン |
| takeru0x1103 | 19:4b0fe9a5ec38 | 89 | case ESC: |
| MasashiNomura | 25:f3a6e7eec9c3 | 90 | xEsc(); |
| MasashiNomura | 25:f3a6e7eec9c3 | 91 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 92 | case CR : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 93 | /*何もしない*/ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 94 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 95 | //▼ラインフィード(改行) |
| takeru0x1103 | 19:4b0fe9a5ec38 | 96 | case LF : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 97 | xSendMsg(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 98 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 99 | //▼スペース |
| takeru0x1103 | 19:4b0fe9a5ec38 | 100 | case SPC: |
| takeru0x1103 | 19:4b0fe9a5ec38 | 101 | xPush(0x0);//Null文字にしてバッファ格納 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 102 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 103 | //▼//その他 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 104 | default : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 105 | xPush(iRxChar);//コマンドバッファへプッシュ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 106 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 107 | }//switch |
| takeru0x1103 | 19:4b0fe9a5ec38 | 108 | } |
| takeru0x1103 | 19:4b0fe9a5ec38 | 109 | |
| takeru0x1103 | 0:ecd925601fc6 | 110 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| takeru0x1103 | 0:ecd925601fc6 | 111 | // UART 受信割り込みハンドラ |
| takeru0x1103 | 0:ecd925601fc6 | 112 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| takeru0x1103 | 0:ecd925601fc6 | 113 | void uartRxIntHndler(){ |
| takeru0x1103 | 0:ecd925601fc6 | 114 | //1Byte抜き出す |
| takeru0x1103 | 0:ecd925601fc6 | 115 | UCHAR buf = sp.getc(); |
| takeru0x1103 | 0:ecd925601fc6 | 116 | //エコーバック |
| takeru0x1103 | 17:f9610f3cfa1b | 117 | sp.putc(buf); |
| takeru0x1103 | 0:ecd925601fc6 | 118 | //コマンドバッファに突っ込む |
| takeru0x1103 | 19:4b0fe9a5ec38 | 119 | xPushBuf(buf); |
| takeru0x1103 | 0:ecd925601fc6 | 120 | } |
| takeru0x1103 | 0:ecd925601fc6 | 121 | |
| takeru0x1103 | 0:ecd925601fc6 | 122 | //============================================================= |
| takeru0x1103 | 0:ecd925601fc6 | 123 | //初期設定 |
| takeru0x1103 | 0:ecd925601fc6 | 124 | //============================================================= |
| takeru0x1103 | 0:ecd925601fc6 | 125 | void uartInit(){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 126 | //バッファクリア |
| takeru0x1103 | 19:4b0fe9a5ec38 | 127 | xBufClear(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 128 | |
| takeru0x1103 | 0:ecd925601fc6 | 129 | //ボーレート設定 |
| takeru0x1103 | 0:ecd925601fc6 | 130 | sp.baud(BAUD_RATE); |
| takeru0x1103 | 0:ecd925601fc6 | 131 | |
| takeru0x1103 | 8:1ca49cb18290 | 132 | //受信割り込みハンドラ登録(登録するハンドラ,割り込み要因) |
| takeru0x1103 | 8:1ca49cb18290 | 133 | sp.attach(uartRxIntHndler,Serial::RxIrq); |
| takeru0x1103 | 0:ecd925601fc6 | 134 | |
| takeru0x1103 | 0:ecd925601fc6 | 135 | sp.printf("***********\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 136 | sp.printf("UART open!!\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 137 | sp.printf("-----------\r\n"); |
| takeru0x1103 | 18:5aa48aec9cae | 138 | sp.printf("INT16 = %d\r\n" ,sizeof(INT16) ); |
| takeru0x1103 | 18:5aa48aec9cae | 139 | sp.printf("unsigned int = %d\r\n" ,sizeof(unsigned int) ); |
| takeru0x1103 | 18:5aa48aec9cae | 140 | sp.printf("long = %d\r\n" ,sizeof(unsigned long) ); |
| takeru0x1103 | 18:5aa48aec9cae | 141 | sp.printf("unsigned int = %d\r\n" ,sizeof(unsigned int) ); |
| takeru0x1103 | 0:ecd925601fc6 | 142 | sp.printf("***********\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 143 | } |