uint8_t型とunsigned char型に対応した多バイトシリアル通信用ライブラリ
Dependents: multiserial_test serial_check_controller receiverA receiver_transmitter ... more
MultiSerial.cpp@15:2304fcb6a594, 2014-10-04 (annotated)
- Committer:
- lilac0112_1
- Date:
- Sat Oct 04 02:27:08 2014 +0000
- Revision:
- 15:2304fcb6a594
- Parent:
- 14:a1ff72d48831
- Child:
- 17:5e2c78dbbd40
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hatter | 0:77c15e33dfd1 | 1 | /* |
Hatter | 0:77c15e33dfd1 | 2 | *mbed Multibyte Serial Library |
Hatter | 0:77c15e33dfd1 | 3 | */ |
Hatter | 0:77c15e33dfd1 | 4 | |
Hatter | 7:f15a96ec54e1 | 5 | #include "mbed.h" |
Hatter | 0:77c15e33dfd1 | 6 | #include "MultiSerial.h" |
Hatter | 13:afc65bff2e6b | 7 | #include "RawSerial.h" |
Hatter | 0:77c15e33dfd1 | 8 | |
Hatter | 9:26262b75f40b | 9 | |
Hatter | 12:5539a5dcdeff | 10 | MultiSerial::MultiSerial(PinName tx,PinName rx) : __serial__(tx,rx){ |
Hatter | 12:5539a5dcdeff | 11 | /* |
Hatter | 9:26262b75f40b | 12 | switch(state){ |
Hatter | 9:26262b75f40b | 13 | |
Hatter | 9:26262b75f40b | 14 | case read: |
Hatter | 9:26262b75f40b | 15 | { |
Hatter | 9:26262b75f40b | 16 | __serial__.attach(this,&MultiSerial::RX,Serial::RxIrq); |
Hatter | 9:26262b75f40b | 17 | uint8_t k = __serial__.getc(); |
Hatter | 9:26262b75f40b | 18 | break; |
Hatter | 9:26262b75f40b | 19 | } |
Hatter | 9:26262b75f40b | 20 | |
Hatter | 9:26262b75f40b | 21 | case write: |
Hatter | 9:26262b75f40b | 22 | { |
Hatter | 9:26262b75f40b | 23 | __serial__.attach(this,&MultiSerial::TX,Serial::TxIrq); |
Hatter | 9:26262b75f40b | 24 | __serial__.putc(1); |
Hatter | 9:26262b75f40b | 25 | break; |
Hatter | 9:26262b75f40b | 26 | } |
Hatter | 9:26262b75f40b | 27 | |
Hatter | 9:26262b75f40b | 28 | case both: |
Hatter | 9:26262b75f40b | 29 | { |
Hatter | 9:26262b75f40b | 30 | __serial__.attach(this,&MultiSerial::RX,Serial::RxIrq); |
Hatter | 9:26262b75f40b | 31 | uint8_t m = __serial__.getc(); |
Hatter | 9:26262b75f40b | 32 | __serial__.attach(this,&MultiSerial::TX,Serial::TxIrq); |
Hatter | 9:26262b75f40b | 33 | __serial__.putc(1); |
Hatter | 9:26262b75f40b | 34 | } |
Hatter | 12:5539a5dcdeff | 35 | |
Hatter | 9:26262b75f40b | 36 | } |
Hatter | 12:5539a5dcdeff | 37 | */ |
Hatter | 9:26262b75f40b | 38 | } |
Hatter | 12:5539a5dcdeff | 39 | |
Hatter | 12:5539a5dcdeff | 40 | void MultiSerial::start_write(){ |
Hatter | 12:5539a5dcdeff | 41 | |
Hatter | 12:5539a5dcdeff | 42 | __stop_write = 0; |
Hatter | 13:afc65bff2e6b | 43 | __serial__.attach(this,&MultiSerial::TX,RawSerial::TxIrq); |
Hatter | 12:5539a5dcdeff | 44 | __serial__.putc(1); |
Hatter | 12:5539a5dcdeff | 45 | |
Hatter | 12:5539a5dcdeff | 46 | } |
Hatter | 12:5539a5dcdeff | 47 | |
Hatter | 12:5539a5dcdeff | 48 | void MultiSerial::stop_write(){ |
Hatter | 12:5539a5dcdeff | 49 | |
Hatter | 12:5539a5dcdeff | 50 | __stop_write = 1; |
Hatter | 12:5539a5dcdeff | 51 | |
Hatter | 12:5539a5dcdeff | 52 | } |
Hatter | 12:5539a5dcdeff | 53 | |
Hatter | 12:5539a5dcdeff | 54 | |
Hatter | 12:5539a5dcdeff | 55 | void MultiSerial::start_read(){ |
Hatter | 12:5539a5dcdeff | 56 | |
Hatter | 12:5539a5dcdeff | 57 | __stop_read = 0; |
Hatter | 13:afc65bff2e6b | 58 | __serial__.attach(this,&MultiSerial::RX,RawSerial::RxIrq); |
Hatter | 12:5539a5dcdeff | 59 | uint8_t k = __serial__.getc(); |
Hatter | 12:5539a5dcdeff | 60 | |
Hatter | 12:5539a5dcdeff | 61 | } |
Hatter | 12:5539a5dcdeff | 62 | |
Hatter | 12:5539a5dcdeff | 63 | void MultiSerial::stop_read(){ |
Hatter | 12:5539a5dcdeff | 64 | |
Hatter | 12:5539a5dcdeff | 65 | __stop_read = 1; |
Hatter | 12:5539a5dcdeff | 66 | |
Hatter | 12:5539a5dcdeff | 67 | } |
Hatter | 7:f15a96ec54e1 | 68 | |
Hatter | 9:26262b75f40b | 69 | void MultiSerial::read_data(uint8_t* readData,uint8_t readKey){ |
Hatter | 0:77c15e33dfd1 | 70 | |
Hatter | 7:f15a96ec54e1 | 71 | __readData = readData; |
Hatter | 7:f15a96ec54e1 | 72 | __readSize = __SIZE(__readData); |
Hatter | 7:f15a96ec54e1 | 73 | __readKey = readKey; |
Hatter | 7:f15a96ec54e1 | 74 | } |
Hatter | 7:f15a96ec54e1 | 75 | |
Hatter | 7:f15a96ec54e1 | 76 | |
Hatter | 9:26262b75f40b | 77 | void MultiSerial::write_data(uint8_t* writeData,uint8_t writeKey){ |
Hatter | 0:77c15e33dfd1 | 78 | |
Hatter | 7:f15a96ec54e1 | 79 | __writeData=writeData; |
Hatter | 7:f15a96ec54e1 | 80 | __writeSize = __SIZE(__writeData); |
Hatter | 7:f15a96ec54e1 | 81 | __writeKey = writeKey; |
Hatter | 7:f15a96ec54e1 | 82 | } |
Hatter | 7:f15a96ec54e1 | 83 | |
Hatter | 7:f15a96ec54e1 | 84 | |
Hatter | 7:f15a96ec54e1 | 85 | void MultiSerial::TX(void){//送信バッファ空き割り込み用関数 |
Hatter | 0:77c15e33dfd1 | 86 | |
Hatter | 12:5539a5dcdeff | 87 | if(__stop_write==1){return;} |
Hatter | 12:5539a5dcdeff | 88 | |
Hatter | 7:f15a96ec54e1 | 89 | static uint8_t tx=__writeSize+2, i; |
Hatter | 7:f15a96ec54e1 | 90 | static uint8_t txData[MAX_DATA_NUM]={__writeKey}; |
Hatter | 7:f15a96ec54e1 | 91 | static uint8_t tx_checkcode=0; |
Hatter | 7:f15a96ec54e1 | 92 | if(tx >= __writeSize+2){ |
Hatter | 12:5539a5dcdeff | 93 | txData[KEY] = __writeKey; |
Hatter | 7:f15a96ec54e1 | 94 | |
Hatter | 7:f15a96ec54e1 | 95 | for(int k=1;k<=__writeSize;k++){ |
Hatter | 7:f15a96ec54e1 | 96 | txData[k] = __writeData[k-1]; |
Hatter | 7:f15a96ec54e1 | 97 | } |
Hatter | 0:77c15e33dfd1 | 98 | |
Hatter | 7:f15a96ec54e1 | 99 | //送るデータ = センサ等のデータ |
Hatter | 7:f15a96ec54e1 | 100 | for(i=KEY+1, tx_checkcode=0; i<__writeSize+1; i++){ |
Hatter | 7:f15a96ec54e1 | 101 | tx_checkcode ^= txData[i]; |
Hatter | 7:f15a96ec54e1 | 102 | }//CHECKCODE作成 |
Hatter | 7:f15a96ec54e1 | 103 | txData[__writeSize+1] = tx_checkcode; |
Hatter | 7:f15a96ec54e1 | 104 | tx=0; |
Hatter | 7:f15a96ec54e1 | 105 | } |
Hatter | 7:f15a96ec54e1 | 106 | __serial__.putc(txData[tx]); |
Hatter | 7:f15a96ec54e1 | 107 | tx++; |
Hatter | 7:f15a96ec54e1 | 108 | }//受信割り込み用関数 |
Hatter | 7:f15a96ec54e1 | 109 | |
Hatter | 7:f15a96ec54e1 | 110 | void MultiSerial::RX(void){ |
Hatter | 12:5539a5dcdeff | 111 | |
Hatter | 12:5539a5dcdeff | 112 | if(__stop_read==1){return;} |
Hatter | 12:5539a5dcdeff | 113 | |
Hatter | 7:f15a96ec54e1 | 114 | static uint8_t rx=0, i; |
Hatter | 7:f15a96ec54e1 | 115 | static uint8_t rxData[MAX_DATA_NUM]={__readKey}; |
Hatter | 7:f15a96ec54e1 | 116 | static uint8_t rx_checkcode=0; |
Hatter | 7:f15a96ec54e1 | 117 | |
Hatter | 7:f15a96ec54e1 | 118 | rxData[rx] = __serial__.getc(); |
Hatter | 7:f15a96ec54e1 | 119 | |
Hatter | 7:f15a96ec54e1 | 120 | if(rxData[KEY]==__readKey){ |
Hatter | 7:f15a96ec54e1 | 121 | rx++; |
Hatter | 0:77c15e33dfd1 | 122 | } |
Hatter | 0:77c15e33dfd1 | 123 | |
Hatter | 7:f15a96ec54e1 | 124 | if(rx==__readSize+1){ |
Hatter | 7:f15a96ec54e1 | 125 | for(i=KEY+1, rx_checkcode=0; i<__readSize+1; i++){ |
Hatter | 7:f15a96ec54e1 | 126 | rx_checkcode ^= rxData[i]; |
Hatter | 7:f15a96ec54e1 | 127 | }//CHECKCODE作成 |
Hatter | 7:f15a96ec54e1 | 128 | } |
Hatter | 6:49c82ca5aa37 | 129 | |
Hatter | 7:f15a96ec54e1 | 130 | if(rx >= __readSize+2){ |
Hatter | 7:f15a96ec54e1 | 131 | if(rxData[__readSize+1]==rx_checkcode){ |
Hatter | 6:49c82ca5aa37 | 132 | |
Hatter | 7:f15a96ec54e1 | 133 | for(int m=1;m<=__readSize;m++){ |
Hatter | 7:f15a96ec54e1 | 134 | __readData[m-1] = rxData[m]; |
Hatter | 7:f15a96ec54e1 | 135 | } |
Hatter | 6:49c82ca5aa37 | 136 | |
Hatter | 7:f15a96ec54e1 | 137 | //使うデータ = 受け取ったデータ |
Hatter | 7:f15a96ec54e1 | 138 | } |
Hatter | 7:f15a96ec54e1 | 139 | rx=0; |
Hatter | 7:f15a96ec54e1 | 140 | } |
Hatter | 6:49c82ca5aa37 | 141 | } |
Hatter | 14:a1ff72d48831 | 142 | |
Hatter | 14:a1ff72d48831 | 143 | uint8_t MultiSerial::readable_check(){ |
Hatter | 14:a1ff72d48831 | 144 | |
Hatter | 14:a1ff72d48831 | 145 | return __serial__.readable(); |
Hatter | 14:a1ff72d48831 | 146 | |
Hatter | 14:a1ff72d48831 | 147 | } |