|
|
@@ -45,6 +45,8 @@ NbusBridge::NbusBridge(NbusCommunicator *nc) {
|
|
|
}
|
|
|
_communicator = nc;
|
|
|
_num_slaves = 0;
|
|
|
+ _run_state = STATE_STOPPED;
|
|
|
+ _pdu.sa = BROADCAST_ADDRESS;
|
|
|
}
|
|
|
|
|
|
NbusBridge::~NbusBridge() {
|
|
|
@@ -60,18 +62,21 @@ void NbusBridge::scan(){
|
|
|
Nbus_pdu pdu;
|
|
|
pdu.fc = FC_ECHO;
|
|
|
pdu.sa = SLAVE_ADDRESS_MODULE;
|
|
|
+ _pdu.ma = SLAVE_ADDRESS_MODULE;
|
|
|
uint8_t data[4] = {110, 66, 117, 115}; // nBus
|
|
|
const uint8_t data_offset = 4;
|
|
|
+ uint8_t slave_index = 0;
|
|
|
|
|
|
for(uint32_t i = 1 ; i < MAX_SLAVES; i++){
|
|
|
pdu.ma = i;
|
|
|
- frame = _communicator->send(&pdu, data, 4);
|
|
|
+ frame = _communicator->sendAndReceive(&pdu, data, 4);
|
|
|
if (!frame->IsEmpty()){
|
|
|
response = frame->GetFrame();
|
|
|
if(response[0+data_offset] == 110 && response[1+data_offset] == 66 && response[2+data_offset] == 117 && response[3+data_offset] == 115) {
|
|
|
_num_slaves++;
|
|
|
_slaves[i].setAddress(i);
|
|
|
_slaves[i].setCommunicator(_communicator);
|
|
|
+ _slave_adress[slave_index++] = i;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -110,7 +115,41 @@ void NbusBridge::sendResponseToMaster(DataFrame *response_frame){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void NbusBridge::broadcastStart(){
|
|
|
+ _pdu.fc = FC_START;
|
|
|
+ _pdu.ma = BROADCAST_ADDRESS;
|
|
|
+ _communicator->send(&_pdu, NULL, 0);
|
|
|
+ _run_state = STATE_RUNNING;
|
|
|
+}
|
|
|
+
|
|
|
+void NbusBridge::broadcastStop(){
|
|
|
+ _pdu.fc = FC_STOP;
|
|
|
+ _pdu.ma = BROADCAST_ADDRESS;
|
|
|
+ _communicator->send(&_pdu, NULL, 0);
|
|
|
+ _run_state = STATE_STOPPED;
|
|
|
+}
|
|
|
+
|
|
|
+void NbusBridge::process_broadcast(uint8_t *rxFrame){
|
|
|
+ switch(FUNCTION_CODE(rxFrame)) {
|
|
|
+
|
|
|
+ case FC_STOP: /* 2 */
|
|
|
+ this->broadcastStop();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case FC_START: /* 3 */
|
|
|
+ this->broadcastStart();
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
void NbusBridge::processRequest(uint8_t *rxFrame, uint8_t size){
|
|
|
+ if (MODULE_ADDRESS(rxFrame) == 0){
|
|
|
+ process_broadcast(rxFrame);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
uint8_t send_reponse = 1;
|
|
|
NbusSlave *selected_slave = getSlave(MODULE_ADDRESS(rxFrame));
|
|
|
if (selected_slave == NULL){
|
|
|
@@ -137,13 +176,12 @@ void NbusBridge::processRequest(uint8_t *rxFrame, uint8_t size){
|
|
|
_frame_nbus_internal = selected_slave->nbus_echo();
|
|
|
break;
|
|
|
|
|
|
- case FC_STOP: /* 2 */
|
|
|
- break;
|
|
|
-
|
|
|
- case FC_START: /* 3 */
|
|
|
- break;
|
|
|
-
|
|
|
case FC_PARAM: /* 4 */
|
|
|
+ if(size == RX_META){
|
|
|
+ _frame_nbus_internal = selected_slave->nbus_sensor_parameters(SENSOR_ADDRESS(rxFrame));
|
|
|
+ } else{
|
|
|
+ _frame_nbus_internal = selected_slave->nbus_sensor_parameter(SENSOR_ADDRESS(rxFrame), rxFrame[RX_META-1]);
|
|
|
+ }
|
|
|
break;
|
|
|
|
|
|
case FC_SENSOR_CNT: /* 5 */
|
|
|
@@ -180,7 +218,7 @@ void NbusBridge::processRequest(uint8_t *rxFrame, uint8_t size){
|
|
|
break;
|
|
|
|
|
|
case FC_SENSOR_FORMAT: /* 0xF => 15 */
|
|
|
- _frame_nbus_internal = selected_slave->nbus_sensor_parameters(SENSOR_ADDRESS(rxFrame));
|
|
|
+ _frame_nbus_internal = selected_slave->nbus_sensor_format(SENSOR_ADDRESS(rxFrame));
|
|
|
break;
|
|
|
default:
|
|
|
send_reponse = 0;
|
|
|
@@ -191,3 +229,24 @@ void NbusBridge::processRequest(uint8_t *rxFrame, uint8_t size){
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+void NbusBridge::processRunningState(){
|
|
|
+ if (_run_state == STATE_STOPPED ){
|
|
|
+ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
|
|
|
+ NbusSlave *slave;
|
|
|
+ for(uint32_t i=0; i< _num_slaves; i++){
|
|
|
+ slave = getSlave(_slave_adress[i]);
|
|
|
+ if(slave != NULL && slave->isActive()){
|
|
|
+ for(uint32_t k = 0 ; k <= slave->nbus_get_sensor_count(false) ; k++){
|
|
|
+ _workerFrame = slave->nbus_sensor_getData(k);
|
|
|
+ sendResponseToMaster(_workerFrame);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ HAL_Delay(1); // TREBA VYSKUSAT
|
|
|
+}
|