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@19:4b0fe9a5ec38, 2018-12-05 (annotated)
- Committer:
- takeru0x1103
- Date:
- Wed Dec 05 00:12:38 2018 +0000
- Revision:
- 19:4b0fe9a5ec38
- Parent:
- 18:5aa48aec9cae
- Child:
- 21:78302ecdb661
???????
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 | |
| takeru0x1103 | 19:4b0fe9a5ec38 | 64 | //============================================================= |
| takeru0x1103 | 19:4b0fe9a5ec38 | 65 | // コマンドバッファへ受信キャラを格納 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 66 | //============================================================= |
| takeru0x1103 | 19:4b0fe9a5ec38 | 67 | static void xPushBuf(UCHAR iRxChar){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 68 | switch(iRxChar){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 69 | //▼タブ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 70 | case TAB: //姿勢制御ON/OFF |
| takeru0x1103 | 19:4b0fe9a5ec38 | 71 | gf_AttCntEna = !gf_AttCntEna; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 72 | sp.printf("Attitude control %d\r\n",gf_AttCntEna); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 73 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 74 | //▼バックスペース |
| takeru0x1103 | 19:4b0fe9a5ec38 | 75 | case BS : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 76 | xBs(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 77 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 78 | //▼エスケープ キャリッジリターン |
| takeru0x1103 | 19:4b0fe9a5ec38 | 79 | case ESC: |
| takeru0x1103 | 19:4b0fe9a5ec38 | 80 | case CR : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 81 | /*何もしない*/ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 82 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 83 | //▼ラインフィード(改行) |
| takeru0x1103 | 19:4b0fe9a5ec38 | 84 | case LF : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 85 | xSendMsg(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 86 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 87 | //▼スペース |
| takeru0x1103 | 19:4b0fe9a5ec38 | 88 | case SPC: |
| takeru0x1103 | 19:4b0fe9a5ec38 | 89 | xPush(0x0);//Null文字にしてバッファ格納 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 90 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 91 | //▼//その他 |
| takeru0x1103 | 19:4b0fe9a5ec38 | 92 | default : |
| takeru0x1103 | 19:4b0fe9a5ec38 | 93 | xPush(iRxChar);//コマンドバッファへプッシュ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 94 | break; |
| takeru0x1103 | 19:4b0fe9a5ec38 | 95 | }//switch |
| takeru0x1103 | 19:4b0fe9a5ec38 | 96 | } |
| takeru0x1103 | 19:4b0fe9a5ec38 | 97 | |
| takeru0x1103 | 0:ecd925601fc6 | 98 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| takeru0x1103 | 0:ecd925601fc6 | 99 | // UART 受信割り込みハンドラ |
| takeru0x1103 | 0:ecd925601fc6 | 100 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| takeru0x1103 | 0:ecd925601fc6 | 101 | void uartRxIntHndler(){ |
| takeru0x1103 | 0:ecd925601fc6 | 102 | //1Byte抜き出す |
| takeru0x1103 | 0:ecd925601fc6 | 103 | UCHAR buf = sp.getc(); |
| takeru0x1103 | 0:ecd925601fc6 | 104 | //エコーバック |
| takeru0x1103 | 17:f9610f3cfa1b | 105 | sp.putc(buf); |
| takeru0x1103 | 0:ecd925601fc6 | 106 | //コマンドバッファに突っ込む |
| takeru0x1103 | 19:4b0fe9a5ec38 | 107 | xPushBuf(buf); |
| takeru0x1103 | 0:ecd925601fc6 | 108 | } |
| takeru0x1103 | 0:ecd925601fc6 | 109 | |
| takeru0x1103 | 0:ecd925601fc6 | 110 | //============================================================= |
| takeru0x1103 | 0:ecd925601fc6 | 111 | //初期設定 |
| takeru0x1103 | 0:ecd925601fc6 | 112 | //============================================================= |
| takeru0x1103 | 0:ecd925601fc6 | 113 | void uartInit(){ |
| takeru0x1103 | 19:4b0fe9a5ec38 | 114 | //バッファクリア |
| takeru0x1103 | 19:4b0fe9a5ec38 | 115 | xBufClear(); |
| takeru0x1103 | 19:4b0fe9a5ec38 | 116 | |
| takeru0x1103 | 0:ecd925601fc6 | 117 | //ボーレート設定 |
| takeru0x1103 | 0:ecd925601fc6 | 118 | sp.baud(BAUD_RATE); |
| takeru0x1103 | 0:ecd925601fc6 | 119 | |
| takeru0x1103 | 8:1ca49cb18290 | 120 | //受信割り込みハンドラ登録(登録するハンドラ,割り込み要因) |
| takeru0x1103 | 8:1ca49cb18290 | 121 | sp.attach(uartRxIntHndler,Serial::RxIrq); |
| takeru0x1103 | 0:ecd925601fc6 | 122 | |
| takeru0x1103 | 0:ecd925601fc6 | 123 | sp.printf("***********\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 124 | sp.printf("UART open!!\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 125 | sp.printf("-----------\r\n"); |
| takeru0x1103 | 18:5aa48aec9cae | 126 | sp.printf("INT16 = %d\r\n" ,sizeof(INT16) ); |
| takeru0x1103 | 18:5aa48aec9cae | 127 | sp.printf("unsigned int = %d\r\n" ,sizeof(unsigned int) ); |
| takeru0x1103 | 18:5aa48aec9cae | 128 | sp.printf("long = %d\r\n" ,sizeof(unsigned long) ); |
| takeru0x1103 | 18:5aa48aec9cae | 129 | sp.printf("unsigned int = %d\r\n" ,sizeof(unsigned int) ); |
| takeru0x1103 | 0:ecd925601fc6 | 130 | sp.printf("***********\r\n"); |
| takeru0x1103 | 0:ecd925601fc6 | 131 | } |