Nagano kosen robocon
Embed:
(wiki syntax)
Show/hide line numbers
RS485.cpp
00001 00002 /* 00003 ############# # 00004 # # 00005 # # 00006 # # 00007 まじ ######################### 00008 # # 00009 # # 00010 # # 00011 # ############# 00012 */ 00013 00014 #include "RS485.h" 00015 #include "mbed.h" 00016 #include "ActuatorHub/ActuatorHub.h" 00017 #include "Master/Master.h" 00018 #include "../../LED/LED.h" 00019 #include "../../System/Using.h" 00020 00021 namespace RS485 { 00022 DigitalOut MasterSelectBit(MASTER_SELECTBIT_PIN); 00023 DigitalOut HubSelectBit(HUB_SELECTBIT_PIN); 00024 Serial MasterRS485Uart(MASTER_RS485UART_TX, MASTER_RS485UART_RX); 00025 Serial HubRS485Uart(HUB_RS485UART_TX,HUB_RS485UART_RX); 00026 Ticker LostTimer; 00027 Timeout sendTimer; 00028 00029 //Serial pc(USBTX, USBRX, 38400); 00030 00031 void HubTransmit(); 00032 void MasterReceive(); 00033 void MasterTransmit(); 00034 void LostTimer_func(); 00035 void SendTimer_func(); 00036 00037 namespace 00038 { 00039 volatile char buf[MASTER_RS485_RECEIVE_DATA_SIZE+1]; 00040 bool communicationLost = true; 00041 uint8_t timerCount = 0; 00042 00043 uint8_t sendCnt = 0; 00044 } 00045 00046 void RS485::Initialize() { 00047 HubSelectBit = 1; 00048 HubRS485Uart.baud(38400); 00049 HubRS485Uart.attach(HubTransmit,Serial::TxIrq); 00050 00051 MasterSelectBit = 1; //送信からスタート 00052 MasterRS485Uart.baud(38400); 00053 MasterRS485Uart.attach(NULL, Serial::RxIrq); 00054 MasterRS485Uart.attach(MasterTransmit, Serial::TxIrq); 00055 LostTimer.attach(LostTimer_func, 0.01); 00056 } 00057 00058 void HubTransmit() { 00059 //__disable_irq(); 00060 HubRS485Uart.putc(HubRS485SendBuffer.GetData()); 00061 //__enable_irq(); 00062 } 00063 00064 bool RS485::IsCommunicationLost() {return communicationLost;} 00065 00066 void MasterReceive() { 00067 __disable_irq(); 00068 static Phase phase = ReceiveWait; 00069 static uint8_t count = 0; 00070 static uint8_t checkSum = 0; 00071 00072 uint8_t data = MasterRS485Uart.getc(); 00073 00074 switch(phase) 00075 { 00076 case DataReceive : 00077 if(count < MASTER_RS485_RECEIVE_DATA_SIZE+1) { 00078 buf[count] = data; 00079 if(count < MASTER_RS485_RECEIVE_DATA_SIZE) checkSum += data; 00080 count++; 00081 } else { 00082 if(count == MASTER_RS485_RECEIVE_DATA_SIZE+1 && data == '\r' && checkSum == buf[count-1]) { 00083 for(uint8_t i=0; i<count-1; i++) masterReceiveBuffer[i] = buf[i]; 00084 static uint8_t cnt = 0; 00085 if(cnt >= 0x0f) { 00086 LED_DEBUG1 = !LED_DEBUG1; 00087 cnt = 0; 00088 } else cnt++; 00089 communicationLost = false; 00090 } 00091 phase = ReceiveWait; 00092 MasterSelectBit = 1; //送信 00093 MasterRS485Uart.attach(NULL, Serial::RxIrq); 00094 MasterRS485Uart.attach(MasterTransmit, Serial::TxIrq); 00095 //sendTimer.attach(SendTimer_func, 0.01); 00096 timerCount = 0; 00097 } 00098 break; 00099 00100 case ReceiveWait : 00101 if(data == '*') { 00102 phase = DataReceive; 00103 count = 0; 00104 checkSum = 0; 00105 } 00106 break; 00107 00108 default: 00109 phase = ReceiveWait; 00110 break; 00111 } 00112 00113 //pc.putc(data); 00114 00115 __enable_irq(); 00116 } 00117 00118 void MasterTransmit() { 00119 __disable_irq(); 00120 timerCount = 0; 00121 00122 MasterRS485Uart.putc(masterSendBuffer[sendCnt]); 00123 //pc.putc(masterSendBuffer[sendCnt]); 00124 00125 if(sendCnt == MASTER_RS485_SEND_DATA_SIZE+4) { 00126 MasterSelectBit = 0; 00127 MasterRS485Uart.attach(NULL, Serial::TxIrq); 00128 MasterRS485Uart.attach(MasterReceive, Serial::RxIrq); 00129 sendCnt = 0; 00130 } else sendCnt++; 00131 __enable_irq(); 00132 } 00133 00134 void LostTimer_func() { 00135 timerCount++; 00136 if(timerCount >= 2 && timerCount < 20) { 00137 MasterSelectBit = 1; 00138 sendCnt = 0; 00139 MasterRS485Uart.attach(NULL, Serial::RxIrq); 00140 MasterRS485Uart.attach(MasterTransmit, Serial::TxIrq); 00141 } 00142 if(timerCount >= 80) { 00143 timerCount = 0; 00144 MasterSelectBit = 1; 00145 sendCnt = 0; 00146 MasterRS485Uart.attach(NULL, Serial::RxIrq); 00147 MasterRS485Uart.attach(MasterTransmit, Serial::TxIrq); 00148 communicationLost = true; 00149 } 00150 00151 } 00152 00153 void SendTimer_func() { 00154 MasterRS485Uart.attach(MasterTransmit, Serial::TxIrq); 00155 } 00156 }
Generated on Wed Jul 13 2022 00:47:24 by
1.7.2