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