![](/media/cache/profiles/P1030212_resized.JPG.50x50_q85.jpg)
明石高専ロボ研 mbedライブラリ
Dependents: MDD_L432KC USB2RS485 pathtracking odometry ... more
scrp_slave.cpp@11:eaf2e3166d20, 2021-02-26 (annotated)
- Committer:
- TanakaRobo
- Date:
- Fri Feb 26 18:19:21 2021 +0000
- Revision:
- 11:eaf2e3166d20
- Parent:
- 10:25af94dd1668
- Child:
- 12:7f0b6e7b9626
add new function; can disable interrupt; can get port, id and cmd number; responce any id mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TanakaRobo | 0:ca84ed7518f5 | 1 | #include "scrp_slave.hpp" |
TanakaRobo | 0:ca84ed7518f5 | 2 | |
TanakaRobo | 0:ca84ed7518f5 | 3 | #define STX 0x41 |
TanakaRobo | 0:ca84ed7518f5 | 4 | #define DMY 0xff |
TanakaRobo | 0:ca84ed7518f5 | 5 | |
TanakaRobo | 11:eaf2e3166d20 | 6 | ScrpSlave::ScrpSlave(PinName TX1,PinName RX1,uint32_t addr,bool interrupt) |
TanakaRobo | 11:eaf2e3166d20 | 7 | :port1(TX1,RX1,115200),port2(port1),address_(addr),interrupt_(interrupt){ |
TanakaRobo | 0:ca84ed7518f5 | 8 | mode_ = 0; |
TanakaRobo | 7:4ad54efe2fdd | 9 | init(); |
TanakaRobo | 0:ca84ed7518f5 | 10 | } |
TanakaRobo | 0:ca84ed7518f5 | 11 | |
TanakaRobo | 11:eaf2e3166d20 | 12 | ScrpSlave::ScrpSlave(PinName TX1,PinName RX1,PinName REDE1,uint32_t addr,bool interrupt) |
TanakaRobo | 11:eaf2e3166d20 | 13 | :port1(TX1,RX1,115200),port2(port1),address_(addr),interrupt_(interrupt){ |
TanakaRobo | 0:ca84ed7518f5 | 14 | mode_ = 1; |
TanakaRobo | 2:141358d84ff4 | 15 | rede_ = new DigitalOut(REDE1,0); |
TanakaRobo | 7:4ad54efe2fdd | 16 | init(); |
TanakaRobo | 0:ca84ed7518f5 | 17 | } |
TanakaRobo | 0:ca84ed7518f5 | 18 | |
TanakaRobo | 11:eaf2e3166d20 | 19 | ScrpSlave::ScrpSlave(PinName TX1,PinName RX1,PinName TX2,PinName RX2,uint32_t addr,bool interrupt) |
TanakaRobo | 11:eaf2e3166d20 | 20 | :port1(TX1,RX1,115200),port2(TX2,RX2,115200),address_(addr),interrupt_(interrupt){ |
TanakaRobo | 0:ca84ed7518f5 | 21 | mode_ = 2; |
TanakaRobo | 7:4ad54efe2fdd | 22 | serial_[1] = &port2; |
TanakaRobo | 11:eaf2e3166d20 | 23 | if(interrupt_){ |
TanakaRobo | 11:eaf2e3166d20 | 24 | serial_[1]->attach(callback(this,&ScrpSlave::receive1),Serial::RxIrq); |
TanakaRobo | 11:eaf2e3166d20 | 25 | } |
TanakaRobo | 7:4ad54efe2fdd | 26 | init(); |
TanakaRobo | 0:ca84ed7518f5 | 27 | } |
TanakaRobo | 0:ca84ed7518f5 | 28 | |
TanakaRobo | 11:eaf2e3166d20 | 29 | ScrpSlave::ScrpSlave(PinName TX1,PinName RX1,PinName REDE1,PinName TX2,PinName RX2,uint32_t addr,bool interrupt) |
TanakaRobo | 11:eaf2e3166d20 | 30 | :port1(TX1,RX1,115200),port2(TX2,RX2,115200),address_(addr),interrupt_(interrupt){ |
TanakaRobo | 0:ca84ed7518f5 | 31 | mode_ = 3; |
TanakaRobo | 2:141358d84ff4 | 32 | rede_ = new DigitalOut(REDE1,0); |
TanakaRobo | 7:4ad54efe2fdd | 33 | serial_[1] = &port2; |
TanakaRobo | 11:eaf2e3166d20 | 34 | if(interrupt_){ |
TanakaRobo | 11:eaf2e3166d20 | 35 | serial_[1]->attach(callback(this,&ScrpSlave::receive1),Serial::RxIrq); |
TanakaRobo | 11:eaf2e3166d20 | 36 | } |
TanakaRobo | 7:4ad54efe2fdd | 37 | init(); |
TanakaRobo | 0:ca84ed7518f5 | 38 | } |
TanakaRobo | 0:ca84ed7518f5 | 39 | |
TanakaRobo | 7:4ad54efe2fdd | 40 | void ScrpSlave::init(){ |
TanakaRobo | 2:141358d84ff4 | 41 | for(int i = 0;i<2;i++){ |
TanakaRobo | 2:141358d84ff4 | 42 | wait_data_[i] = false; |
TanakaRobo | 2:141358d84ff4 | 43 | stx_flag_[i] = false; |
TanakaRobo | 2:141358d84ff4 | 44 | id_ok_[i] = false; |
TanakaRobo | 5:a7894e6982ea | 45 | get_responce_[i] = false; |
TanakaRobo | 5:a7894e6982ea | 46 | } |
TanakaRobo | 5:a7894e6982ea | 47 | for(int i = 1;i<256;++i){ |
TanakaRobo | 5:a7894e6982ea | 48 | procs_[i] = NULL; |
TanakaRobo | 2:141358d84ff4 | 49 | } |
TanakaRobo | 10:25af94dd1668 | 50 | responceFunc_ = NULL; |
TanakaRobo | 11:eaf2e3166d20 | 51 | all_receive_ = false; |
TanakaRobo | 7:4ad54efe2fdd | 52 | serial_[0] = &port1; |
TanakaRobo | 11:eaf2e3166d20 | 53 | if(interrupt_){ |
TanakaRobo | 11:eaf2e3166d20 | 54 | serial_[0]->attach(callback(this,&ScrpSlave::receive0),Serial::RxIrq); |
TanakaRobo | 11:eaf2e3166d20 | 55 | } |
TanakaRobo | 11:eaf2e3166d20 | 56 | if(address_ < 256){ |
TanakaRobo | 11:eaf2e3166d20 | 57 | my_id_ = address_;//引数addressが0~254の時その値をidとして使用 |
TanakaRobo | 11:eaf2e3166d20 | 58 | all_receive_ = (address_ == 255);//引数addressが255の時すべてのidに応答する、全受信モードになる |
TanakaRobo | 5:a7894e6982ea | 59 | return; |
TanakaRobo | 5:a7894e6982ea | 60 | } |
TanakaRobo | 5:a7894e6982ea | 61 | //フラッシュメモリーのアクセスにエラーが出たら、アドレスは10に設定される。 |
TanakaRobo | 0:ca84ed7518f5 | 62 | flash_ = new FlashIAP; |
TanakaRobo | 0:ca84ed7518f5 | 63 | if(flash_->init()==0){ |
TanakaRobo | 0:ca84ed7518f5 | 64 | if(flash_->read(&my_id_,address_,1) != 0){ |
TanakaRobo | 8:82727add54ce | 65 | send2(222,222,222); |
TanakaRobo | 0:ca84ed7518f5 | 66 | my_id_ = 10; |
TanakaRobo | 0:ca84ed7518f5 | 67 | } |
TanakaRobo | 0:ca84ed7518f5 | 68 | }else{ |
TanakaRobo | 8:82727add54ce | 69 | send2(111,111,111); |
TanakaRobo | 0:ca84ed7518f5 | 70 | my_id_ = 10; |
TanakaRobo | 0:ca84ed7518f5 | 71 | } |
TanakaRobo | 0:ca84ed7518f5 | 72 | } |
TanakaRobo | 0:ca84ed7518f5 | 73 | |
TanakaRobo | 11:eaf2e3166d20 | 74 | void ScrpSlave::receive(){ |
TanakaRobo | 11:eaf2e3166d20 | 75 | if(interrupt_){ |
TanakaRobo | 11:eaf2e3166d20 | 76 | return; |
TanakaRobo | 11:eaf2e3166d20 | 77 | } |
TanakaRobo | 11:eaf2e3166d20 | 78 | while(port1.readable()){ |
TanakaRobo | 11:eaf2e3166d20 | 79 | check(0); |
TanakaRobo | 11:eaf2e3166d20 | 80 | } |
TanakaRobo | 11:eaf2e3166d20 | 81 | if(mode_ > 1){ |
TanakaRobo | 11:eaf2e3166d20 | 82 | while(port2.readable()){ |
TanakaRobo | 11:eaf2e3166d20 | 83 | check(1); |
TanakaRobo | 11:eaf2e3166d20 | 84 | } |
TanakaRobo | 11:eaf2e3166d20 | 85 | } |
TanakaRobo | 11:eaf2e3166d20 | 86 | } |
TanakaRobo | 11:eaf2e3166d20 | 87 | |
TanakaRobo | 8:82727add54ce | 88 | void ScrpSlave::receive0(){ |
TanakaRobo | 0:ca84ed7518f5 | 89 | check(0); |
TanakaRobo | 0:ca84ed7518f5 | 90 | } |
TanakaRobo | 0:ca84ed7518f5 | 91 | |
TanakaRobo | 8:82727add54ce | 92 | void ScrpSlave::receive1(){ |
TanakaRobo | 0:ca84ed7518f5 | 93 | check(1); |
TanakaRobo | 0:ca84ed7518f5 | 94 | } |
TanakaRobo | 0:ca84ed7518f5 | 95 | |
TanakaRobo | 10:25af94dd1668 | 96 | void ScrpSlave::attachResponce(void (*func)(uint8_t id, uint8_t cmd, int16_t responce)){ |
TanakaRobo | 10:25af94dd1668 | 97 | responceFunc_ = func; |
TanakaRobo | 10:25af94dd1668 | 98 | } |
TanakaRobo | 10:25af94dd1668 | 99 | |
TanakaRobo | 0:ca84ed7518f5 | 100 | void ScrpSlave::addCMD(uint8_t cmd, bool (*proc)(int rx_data, int& tx_data)){ |
TanakaRobo | 0:ca84ed7518f5 | 101 | if(cmd == 0 || cmd == 254 || cmd == 253)return; |
TanakaRobo | 0:ca84ed7518f5 | 102 | procs_[cmd] = proc; |
TanakaRobo | 0:ca84ed7518f5 | 103 | } |
TanakaRobo | 0:ca84ed7518f5 | 104 | |
TanakaRobo | 0:ca84ed7518f5 | 105 | void ScrpSlave::changeID(uint8_t id){ |
TanakaRobo | 11:eaf2e3166d20 | 106 | if(address_ < 256){ |
TanakaRobo | 5:a7894e6982ea | 107 | return; |
TanakaRobo | 5:a7894e6982ea | 108 | } |
TanakaRobo | 0:ca84ed7518f5 | 109 | flash_->erase(address_,flash_->get_sector_size(address_)); |
TanakaRobo | 0:ca84ed7518f5 | 110 | flash_->program(&id,address_,1); |
TanakaRobo | 0:ca84ed7518f5 | 111 | } |
TanakaRobo | 0:ca84ed7518f5 | 112 | |
TanakaRobo | 11:eaf2e3166d20 | 113 | bool ScrpSlave::send1(uint8_t id,uint8_t cmd,int16_t tx_data, bool flag){ |
TanakaRobo | 11:eaf2e3166d20 | 114 | return sending(0,id,cmd,tx_data,flag); |
TanakaRobo | 11:eaf2e3166d20 | 115 | } |
TanakaRobo | 11:eaf2e3166d20 | 116 | |
TanakaRobo | 11:eaf2e3166d20 | 117 | bool ScrpSlave::send2(uint8_t id,uint8_t cmd,int16_t tx_data, bool flag){ |
TanakaRobo | 11:eaf2e3166d20 | 118 | return sending((mode_ > 1),id,cmd,tx_data,flag); |
TanakaRobo | 0:ca84ed7518f5 | 119 | } |
TanakaRobo | 0:ca84ed7518f5 | 120 | |
TanakaRobo | 11:eaf2e3166d20 | 121 | bool ScrpSlave::sending(int port,uint8_t id,uint8_t cmd,int16_t tx_data,bool flag){ |
TanakaRobo | 11:eaf2e3166d20 | 122 | if(!serial_[port]->writeable()){ |
TanakaRobo | 11:eaf2e3166d20 | 123 | return false; |
TanakaRobo | 11:eaf2e3166d20 | 124 | } |
TanakaRobo | 11:eaf2e3166d20 | 125 | if(flag){ |
TanakaRobo | 11:eaf2e3166d20 | 126 | wait_data_[port] = true;//データ返信待ち |
TanakaRobo | 11:eaf2e3166d20 | 127 | get_responce_[port] = false; |
TanakaRobo | 11:eaf2e3166d20 | 128 | } |
TanakaRobo | 0:ca84ed7518f5 | 129 | uint8_t tx_dataL = tx_data; |
TanakaRobo | 0:ca84ed7518f5 | 130 | uint8_t tx_dataH = tx_data >> 8; |
TanakaRobo | 0:ca84ed7518f5 | 131 | uint8_t tx_sum = id + cmd + tx_dataL + tx_dataH; |
TanakaRobo | 0:ca84ed7518f5 | 132 | |
TanakaRobo | 11:eaf2e3166d20 | 133 | const uint8_t data[8] = {DMY, STX, id, cmd, tx_dataL, tx_dataH, tx_sum, DMY}; |
TanakaRobo | 11:eaf2e3166d20 | 134 | memcpy(send_data_[port],data,8); |
TanakaRobo | 11:eaf2e3166d20 | 135 | if(interrupt_){ |
TanakaRobo | 11:eaf2e3166d20 | 136 | prime(port); |
TanakaRobo | 11:eaf2e3166d20 | 137 | }else{ |
TanakaRobo | 11:eaf2e3166d20 | 138 | sendNoInterrupt(port); |
TanakaRobo | 0:ca84ed7518f5 | 139 | } |
TanakaRobo | 11:eaf2e3166d20 | 140 | return true; |
TanakaRobo | 11:eaf2e3166d20 | 141 | } |
TanakaRobo | 11:eaf2e3166d20 | 142 | |
TanakaRobo | 11:eaf2e3166d20 | 143 | void ScrpSlave::sendNoInterrupt(uint8_t port){ |
TanakaRobo | 0:ca84ed7518f5 | 144 | if(mode_%2 == 1 && port == 0){ |
TanakaRobo | 0:ca84ed7518f5 | 145 | rede_->write(1); |
TanakaRobo | 0:ca84ed7518f5 | 146 | } |
TanakaRobo | 2:141358d84ff4 | 147 | for(int i = 0;i < 8;i++){ |
TanakaRobo | 11:eaf2e3166d20 | 148 | serial_[port]->putc(send_data_[port][i]); |
TanakaRobo | 2:141358d84ff4 | 149 | while(!serial_[port]->writeable()); |
TanakaRobo | 3:28c77df7c0b6 | 150 | } |
TanakaRobo | 0:ca84ed7518f5 | 151 | if(mode_%2 == 1 && port == 0){ |
TanakaRobo | 0:ca84ed7518f5 | 152 | rede_->write(0); |
TanakaRobo | 0:ca84ed7518f5 | 153 | } |
TanakaRobo | 5:a7894e6982ea | 154 | } |
TanakaRobo | 5:a7894e6982ea | 155 | |
TanakaRobo | 10:25af94dd1668 | 156 | bool ScrpSlave::getResponce(uint8_t port){ |
TanakaRobo | 10:25af94dd1668 | 157 | if(port > 1 || (port == 1 && mode_ < 2)){ |
TanakaRobo | 10:25af94dd1668 | 158 | return false; |
TanakaRobo | 10:25af94dd1668 | 159 | } |
TanakaRobo | 10:25af94dd1668 | 160 | return get_responce_[port]; |
TanakaRobo | 10:25af94dd1668 | 161 | } |
TanakaRobo | 10:25af94dd1668 | 162 | |
TanakaRobo | 5:a7894e6982ea | 163 | bool ScrpSlave::isWaiting(uint8_t port){ |
TanakaRobo | 5:a7894e6982ea | 164 | if(port > 1 || (port == 1 && mode_ < 2)){ |
TanakaRobo | 5:a7894e6982ea | 165 | return false; |
TanakaRobo | 0:ca84ed7518f5 | 166 | } |
TanakaRobo | 5:a7894e6982ea | 167 | return wait_data_[port]; |
TanakaRobo | 5:a7894e6982ea | 168 | } |
TanakaRobo | 5:a7894e6982ea | 169 | |
TanakaRobo | 5:a7894e6982ea | 170 | int16_t ScrpSlave::receiveData(uint8_t port){ |
TanakaRobo | 5:a7894e6982ea | 171 | //ポート指定が正しいかどうか。 |
TanakaRobo | 5:a7894e6982ea | 172 | if(port > 1 || (port == 1 && mode_ < 2)){ |
TanakaRobo | 5:a7894e6982ea | 173 | return -1; |
TanakaRobo | 5:a7894e6982ea | 174 | } |
TanakaRobo | 5:a7894e6982ea | 175 | //データがあるか確認。 |
TanakaRobo | 5:a7894e6982ea | 176 | if(get_responce_[port]){ |
TanakaRobo | 5:a7894e6982ea | 177 | return rx_data_[port]; |
TanakaRobo | 5:a7894e6982ea | 178 | }else{ |
TanakaRobo | 5:a7894e6982ea | 179 | return -1; |
TanakaRobo | 5:a7894e6982ea | 180 | } |
TanakaRobo | 0:ca84ed7518f5 | 181 | } |
TanakaRobo | 0:ca84ed7518f5 | 182 | |
TanakaRobo | 11:eaf2e3166d20 | 183 | uint8_t ScrpSlave::receiveCmd(){ |
TanakaRobo | 11:eaf2e3166d20 | 184 | return rx_cmd_;//受信したcmd番号を返す。 |
TanakaRobo | 11:eaf2e3166d20 | 185 | } |
TanakaRobo | 11:eaf2e3166d20 | 186 | |
TanakaRobo | 11:eaf2e3166d20 | 187 | uint8_t ScrpSlave::receiveId(){ |
TanakaRobo | 11:eaf2e3166d20 | 188 | return rx_id_;//受信したidを返す。 |
TanakaRobo | 11:eaf2e3166d20 | 189 | } |
TanakaRobo | 11:eaf2e3166d20 | 190 | |
TanakaRobo | 11:eaf2e3166d20 | 191 | uint8_t ScrpSlave::receivePort(){ |
TanakaRobo | 11:eaf2e3166d20 | 192 | return receive_port_;//直近で受信したポートを返す。 |
TanakaRobo | 11:eaf2e3166d20 | 193 | } |
TanakaRobo | 11:eaf2e3166d20 | 194 | |
TanakaRobo | 0:ca84ed7518f5 | 195 | void ScrpSlave::check(int port){ |
TanakaRobo | 2:141358d84ff4 | 196 | if(id_ok_[port]){ |
TanakaRobo | 2:141358d84ff4 | 197 | tmp_data_[port][data_count_[port]] = serial_[port]->getc(); |
TanakaRobo | 2:141358d84ff4 | 198 | data_count_[port]++; |
TanakaRobo | 2:141358d84ff4 | 199 | if(data_count_[port] > 4){ |
TanakaRobo | 2:141358d84ff4 | 200 | stx_flag_[port] = false;//通信フラグクリア |
TanakaRobo | 2:141358d84ff4 | 201 | id_ok_[port] = false; |
TanakaRobo | 2:141358d84ff4 | 202 | |
TanakaRobo | 2:141358d84ff4 | 203 | uint8_t sum = 0; |
TanakaRobo | 2:141358d84ff4 | 204 | for(int i = 0;i<4;i++){ |
TanakaRobo | 2:141358d84ff4 | 205 | sum += tmp_data_[port][i]; |
TanakaRobo | 2:141358d84ff4 | 206 | } |
TanakaRobo | 11:eaf2e3166d20 | 207 | if(sum != tmp_data_[port][4]){//check sum照合 |
TanakaRobo | 2:141358d84ff4 | 208 | return; |
TanakaRobo | 2:141358d84ff4 | 209 | } |
TanakaRobo | 11:eaf2e3166d20 | 210 | receive_port_ = port;//受信したポート番号を保存 |
TanakaRobo | 11:eaf2e3166d20 | 211 | uint8_t rx_id = tmp_data_[port][0]; |
TanakaRobo | 11:eaf2e3166d20 | 212 | uint8_t rx_cmd = tmp_data_[port][1]; |
TanakaRobo | 2:141358d84ff4 | 213 | rx_data_[port] = (int16_t)(tmp_data_[port][2] + ((int16_t)tmp_data_[port][3] << 8)); |
TanakaRobo | 2:141358d84ff4 | 214 | if(wait_data_[port]){//データ返信待ち時 |
TanakaRobo | 2:141358d84ff4 | 215 | wait_data_[port] = false; |
TanakaRobo | 5:a7894e6982ea | 216 | get_responce_[port] = true; |
TanakaRobo | 10:25af94dd1668 | 217 | if(responceFunc_ != NULL){ |
TanakaRobo | 11:eaf2e3166d20 | 218 | responceFunc_(rx_id,rx_cmd,rx_data_[port]); |
TanakaRobo | 10:25af94dd1668 | 219 | } |
TanakaRobo | 2:141358d84ff4 | 220 | return; |
TanakaRobo | 5:a7894e6982ea | 221 | }else if(get_responce_[port]){ |
TanakaRobo | 5:a7894e6982ea | 222 | get_responce_[port] = false; |
TanakaRobo | 2:141358d84ff4 | 223 | } |
TanakaRobo | 11:eaf2e3166d20 | 224 | rx_cmd_ = rx_cmd;//メンバ変数に保存 |
TanakaRobo | 2:141358d84ff4 | 225 | bool broadcast = (tmp_data_[port][0] == 255); |
TanakaRobo | 2:141358d84ff4 | 226 | |
TanakaRobo | 2:141358d84ff4 | 227 | int tx_data = rx_data_[port]; |
TanakaRobo | 2:141358d84ff4 | 228 | if(rx_cmd == 0){//通信テスト |
TanakaRobo | 2:141358d84ff4 | 229 | }else if(rx_cmd == 254){//id変更 |
TanakaRobo | 2:141358d84ff4 | 230 | uint8_t new_id = rx_data_[port]; |
TanakaRobo | 2:141358d84ff4 | 231 | my_id_ = new_id; |
TanakaRobo | 2:141358d84ff4 | 232 | changeID(new_id); |
TanakaRobo | 2:141358d84ff4 | 233 | }else if(rx_cmd == 253){//id確認 |
TanakaRobo | 2:141358d84ff4 | 234 | tx_data = my_id_; |
TanakaRobo | 11:eaf2e3166d20 | 235 | rx_cmd = 250 + all_receive_*5; |
TanakaRobo | 2:141358d84ff4 | 236 | broadcast = false; |
TanakaRobo | 2:141358d84ff4 | 237 | }else if(procs_[rx_cmd] == NULL || !procs_[rx_cmd](rx_data_[port],tx_data)){ |
TanakaRobo | 2:141358d84ff4 | 238 | return; |
TanakaRobo | 2:141358d84ff4 | 239 | } |
TanakaRobo | 2:141358d84ff4 | 240 | if(broadcast){ |
TanakaRobo | 11:eaf2e3166d20 | 241 | return;//全体送信の時はレスポンスを返さない。 |
TanakaRobo | 2:141358d84ff4 | 242 | } |
TanakaRobo | 2:141358d84ff4 | 243 | uint8_t tx_dataL = tx_data; |
TanakaRobo | 2:141358d84ff4 | 244 | uint8_t tx_dataH = tx_data >> 8; |
TanakaRobo | 2:141358d84ff4 | 245 | uint8_t tx_sum = my_id_ + rx_cmd + tx_dataL + tx_dataH; |
TanakaRobo | 2:141358d84ff4 | 246 | |
TanakaRobo | 2:141358d84ff4 | 247 | const uint8_t data[8] = {DMY, STX, my_id_, rx_cmd, tx_dataL, tx_dataH, tx_sum, DMY}; |
TanakaRobo | 8:82727add54ce | 248 | memcpy(send_data_[port],data,8); |
TanakaRobo | 11:eaf2e3166d20 | 249 | if(interrupt_){ |
TanakaRobo | 11:eaf2e3166d20 | 250 | prime(port); |
TanakaRobo | 11:eaf2e3166d20 | 251 | }else{ |
TanakaRobo | 11:eaf2e3166d20 | 252 | sendNoInterrupt(port); |
TanakaRobo | 11:eaf2e3166d20 | 253 | } |
TanakaRobo | 0:ca84ed7518f5 | 254 | } |
TanakaRobo | 2:141358d84ff4 | 255 | }else if(stx_flag_[port]){ |
TanakaRobo | 2:141358d84ff4 | 256 | uint8_t get_data = serial_[port]->getc(); |
TanakaRobo | 11:eaf2e3166d20 | 257 | if(get_data == my_id_ || get_data == 255 || (all_receive_ && !wait_data_[port])){ |
TanakaRobo | 8:82727add54ce | 258 | id_ok_[port] = true; |
TanakaRobo | 8:82727add54ce | 259 | wait_data_[port] = false; |
TanakaRobo | 8:82727add54ce | 260 | tmp_data_[port][0] = get_data; |
TanakaRobo | 8:82727add54ce | 261 | data_count_[port]++; |
TanakaRobo | 8:82727add54ce | 262 | }else if(wait_data_[port]){ |
TanakaRobo | 2:141358d84ff4 | 263 | id_ok_[port] = true; |
TanakaRobo | 2:141358d84ff4 | 264 | tmp_data_[port][0] = get_data; |
TanakaRobo | 2:141358d84ff4 | 265 | data_count_[port]++; |
TanakaRobo | 2:141358d84ff4 | 266 | }else{ |
TanakaRobo | 2:141358d84ff4 | 267 | stx_flag_[port] = false; |
TanakaRobo | 2:141358d84ff4 | 268 | } |
TanakaRobo | 11:eaf2e3166d20 | 269 | rx_id_ = get_data;//メンバ変数に保存 |
TanakaRobo | 2:141358d84ff4 | 270 | }else if(serial_[port]->getc() == STX){ |
TanakaRobo | 2:141358d84ff4 | 271 | stx_flag_[port] = true; |
TanakaRobo | 2:141358d84ff4 | 272 | data_count_[port] = 0; |
TanakaRobo | 4:39ef4d91dc34 | 273 | id_ok_[port] = false; |
TanakaRobo | 4:39ef4d91dc34 | 274 | //id_ok_[port] = wait_data_[port];//データ返信待ち時はidチェック無し |
TanakaRobo | 0:ca84ed7518f5 | 275 | } |
TanakaRobo | 0:ca84ed7518f5 | 276 | return; |
TanakaRobo | 0:ca84ed7518f5 | 277 | } |
TanakaRobo | 0:ca84ed7518f5 | 278 | |
TanakaRobo | 10:25af94dd1668 | 279 | void ScrpSlave::dataSend0(){ |
TanakaRobo | 2:141358d84ff4 | 280 | while(serial_[0]->writeable()){ |
TanakaRobo | 2:141358d84ff4 | 281 | if(data_count_[0] < 8){ |
TanakaRobo | 2:141358d84ff4 | 282 | serial_[0]->putc(send_data_[0][data_count_[0]++]); |
TanakaRobo | 2:141358d84ff4 | 283 | }else{ |
TanakaRobo | 2:141358d84ff4 | 284 | serial_[0]->attach(NULL, Serial::TxIrq); |
TanakaRobo | 2:141358d84ff4 | 285 | if(mode_%2 == 1){ |
TanakaRobo | 2:141358d84ff4 | 286 | rede_->write(0); |
TanakaRobo | 2:141358d84ff4 | 287 | } |
TanakaRobo | 2:141358d84ff4 | 288 | break; |
TanakaRobo | 2:141358d84ff4 | 289 | } |
TanakaRobo | 2:141358d84ff4 | 290 | } |
TanakaRobo | 2:141358d84ff4 | 291 | } |
TanakaRobo | 2:141358d84ff4 | 292 | |
TanakaRobo | 10:25af94dd1668 | 293 | void ScrpSlave::dataSend1(){ |
TanakaRobo | 2:141358d84ff4 | 294 | while(serial_[1]->writeable()){ |
TanakaRobo | 2:141358d84ff4 | 295 | if(data_count_[1] < 8){ |
TanakaRobo | 2:141358d84ff4 | 296 | serial_[1]->putc(send_data_[1][data_count_[1]++]); |
TanakaRobo | 2:141358d84ff4 | 297 | }else{ |
TanakaRobo | 2:141358d84ff4 | 298 | serial_[1]->attach(NULL, Serial::TxIrq); |
TanakaRobo | 2:141358d84ff4 | 299 | break; |
TanakaRobo | 2:141358d84ff4 | 300 | } |
TanakaRobo | 2:141358d84ff4 | 301 | } |
TanakaRobo | 2:141358d84ff4 | 302 | } |
TanakaRobo | 2:141358d84ff4 | 303 | |
TanakaRobo | 2:141358d84ff4 | 304 | void ScrpSlave::prime(int port){ |
TanakaRobo | 2:141358d84ff4 | 305 | serial_[port]->attach(NULL, Serial::TxIrq); |
TanakaRobo | 2:141358d84ff4 | 306 | data_count_[port] = 0; |
TanakaRobo | 8:82727add54ce | 307 | if(port == 0){ |
TanakaRobo | 2:141358d84ff4 | 308 | if(mode_%2 == 1){ |
TanakaRobo | 2:141358d84ff4 | 309 | rede_->write(1); |
TanakaRobo | 2:141358d84ff4 | 310 | } |
TanakaRobo | 10:25af94dd1668 | 311 | dataSend0(); |
TanakaRobo | 10:25af94dd1668 | 312 | serial_[0]->attach(callback(this, &ScrpSlave::dataSend0), Serial::TxIrq); |
TanakaRobo | 2:141358d84ff4 | 313 | }else{ |
TanakaRobo | 10:25af94dd1668 | 314 | dataSend1(); |
TanakaRobo | 10:25af94dd1668 | 315 | serial_[1]->attach(callback(this, &ScrpSlave::dataSend1), Serial::TxIrq); |
TanakaRobo | 2:141358d84ff4 | 316 | } |
TanakaRobo | 2:141358d84ff4 | 317 | } |
TanakaRobo | 2:141358d84ff4 | 318 | |
TanakaRobo | 0:ca84ed7518f5 | 319 | ScrpSlave::~ScrpSlave(){ |
TanakaRobo | 0:ca84ed7518f5 | 320 | delete flash_; |
TanakaRobo | 0:ca84ed7518f5 | 321 | if(mode_%2 == 1){ |
TanakaRobo | 0:ca84ed7518f5 | 322 | delete rede_; |
TanakaRobo | 0:ca84ed7518f5 | 323 | } |
TanakaRobo | 0:ca84ed7518f5 | 324 | } |