/******************************************************** * TestMaster_initialisation is responsible to * - setup master RPDO 1 to receive TPDO 1 from id 2 * - setup master RPDO 2 to receive TPDO 1 from id 3 ********************************************************/ void TestMaster_initialisation(CO_Data* d) { UNS32 PDO1_COBID = 0x0182; UNS32 PDO2_COBID = 0x0183; UNS32 size = sizeof(UNS32); eprintf("TestMaster_initialisation\n"); /***************************************** * Define RPDO to match slave ID=2 TPDO1* *****************************************/ writeLocalDict( &TestMaster_Data, /*CO_Data* d*/ 0x1400, /*UNS16 index*/ 0x01, /*UNS8 subind*/ &PDO1_COBID, /*void * pSourceData,*/ &size, /* UNS8 * pExpectedSize*/ RW); /* UNS8 checkAccess */ /***************************************** * Define RPDO to match slave ID=3 TPDO1* *****************************************/ writeLocalDict( &TestMaster_Data, /*CO_Data* d*/ 0x1401, /*UNS16 index*/ 0x01, /*UNS8 subind*/ &PDO2_COBID, /*void * pSourceData,*/ &size, /* UNS8 * pExpectedSize*/ RW); /* UNS8 checkAccess */ }
void CANUi::on_pushButton_guardSet_clicked() { if(ui->radiob_heartBeat->isChecked()){ int _nodeNum = ui->spinBox_nodeNum->value(); UNS32 _time = ui->spinBox_guardTime->value(); for(int i = 1; i <= _nodeNum; i++){ {//0x1016 : Consumer Heartbeat Time nodeid(bit 23 : 16) + time(bit 15 : 0) ms UNS32 _sourceData = _time + (i << 16); UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1016, (UNS8)i, (UNS32*)&_sourceData, &_dataSize, 1 )){ ui->textBrowser->append(tr("writeLocalDict 0x1016 %1: fail !").arg(i)); } } } } else { int _nodeNum = ui->spinBox_nodeNum->value(); UNS32 _time = 0; for(int i = 1; i <= _nodeNum; i++){ {//0x1016 : Consumer Heartbeat Time nodeid(bit 23 : 16) + time(bit 15 : 0) ms UNS32 _sourceData = _time + (i << 16); UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1016, (UNS8)i, (UNS32*)&_sourceData, &_dataSize, RW )){ ui->textBrowser->append(tr("writeLocalDict 0x1016 %1: fail !").arg(i)); } } } } }
void CANUi::on_pushButton_sdoSet_clicked() { UNS8 nodeId = ui->spinBox_sdoID->value(); {//0x1280 : SDO1 COB_ID_Client_to_Server bit30:dyn 0x600 + nodeid UNS32 _sourceData = 0x600 + nodeId; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1280, (UNS8)1, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1280 fail !"); } } {//0x1280 : SDO1 COB_ID_Server_to_Client 0x580 + nodeid UNS32 _sourceData = 0x580 + nodeId; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1280, (UNS8)2, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1280 fail !"); } } {//0x1280 : Node_ID_of_the_SDO_Server nodeid UNS8 _sourceData = nodeId; UNS32 _dataSize = sizeof(UNS8); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1280, (UNS8)3, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1280 fail !"); } } UNS32 _dataSize; UNS8 _dataType = ui->comboBox_sdoDatatype->currentData().value<UNS8>(); if(_dataType == uint8){ _dataSize = sizeof(UNS8); } else if(_dataType == uint16){ _dataSize = sizeof(UNS16); } else{ _dataSize = sizeof(UNS32); } UNS32 _sourceData = ui->spinBox_sdoData->value(); UNS16 _index = ui->spinBox_sdoIndex->value(); UNS8 _subIndex = ui->spinBox_sdoSubIndex->value(); int res = writeNetworkDictCallBack (&master_Data, /*CO_Data* d*/ /**TestSlave_Data.bDeviceNodeId, UNS8 nodeId*/ nodeId, /*UNS8 nodeId*/ _index, /*UNS16 index*/ _subIndex, /*UNS8 subindex*/ _dataSize, /*UNS8 count*/ _dataType, /*UNS8 dataType*/ &_sourceData,/*void *data*/ WriteResultNetworkDict, /*SDOCallback_t Callback*/ 0); /* use block mode */ }
void CANUi::on_pushButton_sdoRead_clicked() { UNS8 nodeId = ui->spinBox_sdoID->value(); {//0x1280 : SDO1 COB_ID_Client_to_Server bit30:dyn 0x600 + nodeid UNS32 _sourceData = 0x600 + nodeId; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1280, (UNS8)1, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1280 fail !"); } } {//0x1280 : SDO1 COB_ID_Server_to_Client 0x580 + nodeid UNS32 _sourceData = 0x580 + nodeId; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1280, (UNS8)2, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1280 fail !"); } } {//0x1280 : Node_ID_of_the_SDO_Server nodeid UNS8 _sourceData = nodeId; UNS32 _dataSize = sizeof(UNS8); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1280, (UNS8)3, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1280 fail !"); } } UNS16 _index = ui->spinBox_sdoIndex->value(); UNS8 _subIndex = ui->spinBox_sdoSubIndex->value(); UNS32 _dataSize; UNS8 _dataType = ui->comboBox_sdoDatatype->currentData().value<UNS8>(); if(_dataType == uint8){ _dataSize = sizeof(UNS8); } else if(_dataType == uint16){ _dataSize = sizeof(UNS16); } else{ _dataSize = sizeof(UNS32); } int res = readNetworkDictCallback (&master_Data, nodeId, _index, _subIndex, _dataType, ReadResultNetworkDict, 0); }
void CANUi::on_checkBox_sync_toggled(bool checked) { if(checked){ stopSYNC(&master_Data); {//0x1005 : SYNC COB ID gen(bit 30) COB-ID : 80 UNS32 _sourceData = 0x40000080; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1005, (UNS8)0, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1005 fail !"); } } {//0x1006 : SYNC Cycle Period us UNS32 _sourceData = ui->spinBox_syncTime->value(); ui->textBrowser->append(tr("%1").arg(_sourceData)); UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1006, (UNS8)0, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1006 fail !"); } } startSYNC(&master_Data); } else{ stopSYNC(&master_Data); {//0x1005 : SYNC COB ID gen(bit 30) COB-ID : 80 UNS32 _sourceData = 0x00000080; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1005, (UNS8)0, (UNS32*)&_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1005 fail !"); } } {//0x1006 : SYNC Cycle Period us UNS32 _sourceData = 0; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1006, (UNS8)0, (UNS32*)&_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1006 fail !"); } } } }
void CANUi::on_checkBox_pdoReceive_toggled(bool checked) { if(checked){ {//0x1600 : RPDO1 COB-ID 0x180 + nodeid UNS32 _sourceData = ui->spinBox_rpdoCOB->value() + ui->spinBox_pdonodeID->value(); UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1400, (UNS8)1, (UNS32*)&_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1400 fail !"); } } }else{ {//0x1600 : RPDO1 COB-ID 0x180 + nodeid UNS32 _sourceData = 0; UNS32 _dataSize = sizeof(UNS32); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, (UNS16)0x1400, (UNS8)1, (UNS32*)&_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict 0x1400 fail !"); } } } }
static void can_enable_heartbeat(uint16_t time) { // checkAccess needs to be set to 1 UNS16 Timer_Data[1] = {time}; UNS32 s = sizeof(UNS16); writeLocalDict(&Sensor_Board_Data, // CO_Data* for this uC 0x1017, // Index 0x00, // Sub-Index Timer_Data, // void * SourceData Location &s, // UNS8 * Size of Data 1); // UNS8 checkAccess }
void SillySlave_initialisation(CO_Data* d ) { UNS32 PDO1_COBID = 0x0180 + NODE_MASTER; UNS8 size = sizeof(PDO1_COBID); printf("SillySlave_initialisation\n"); /* sets TXPDO1 COB-ID to match master node ID */ writeLocalDict( &SillySlave_Data, /*CO_Data* d*/ 0x1800, /*UNS16 index*/ 0x01, /*UNS8 subind*/ &PDO1_COBID, /*void * pSourceData,*/ &size, /* UNS8 * pExpectedSize*/ RW); /* UNS8 checkAccess */ /* value sent to master at each SYNC received */ LifeSignal = 0; }
void CANUi::on_pushButton_dicSet_clicked() { UNS32 _dataSize; UNS8 _dataType = ui->comboBox_datatype->currentData().value<UNS8>(); if(_dataType == uint8){ _dataSize = sizeof(UNS8); } else if(_dataType == uint16){ _dataSize = sizeof(UNS16); } else{ _dataSize = sizeof(UNS32); } UNS32 _sourceData = ui->spinBox_dicData->value(); UNS16 _index = ui->spinBox_index->value(); UNS8 _subIndex = ui->spinBox_subInxdex->value(); if(OD_SUCCESSFUL != writeLocalDict(&master_Data, _index, _subIndex, &_sourceData, &_dataSize, 1 )){ ui->textBrowser->append("writeLocalDict fail !"); } }
static void CheckLSSAndContinue(CO_Data* d, UNS8 command) { UNS32 dat1; UNS8 dat2; printf("CheckLSS->"); if(getConfigResultNetworkNode (d, command, &dat1, &dat2) != LSS_FINISHED){ eprintf("Master : Failed in LSS comand %d. Trying again\n", command); } else { init_step_LSS++; switch(command){ case LSS_CONF_NODE_ID: switch(dat1){ case 0: printf("Node ID change succesful\n");break; case 1: printf("Node ID change error:out of range\n");break; case 0xFF:printf("Node ID change error:specific error\n");break; default:break; } break; case LSS_CONF_BIT_TIMING: switch(dat1){ case 0: printf("Baud rate change succesful\n");break; case 1: printf("Baud rate change error: change baud rate not supported\n");break; case 0xFF:printf("Baud rate change error:specific error\n");break; default:break; } break; case LSS_CONF_STORE: switch(dat1){ case 0: printf("Store configuration succesful\n");break; case 1: printf("Store configuration error:not supported\n");break; case 0xFF:printf("Store configuration error:specific error\n");break; default:break; } break; case LSS_CONF_ACT_BIT_TIMING: if(dat1==0){ UNS8 LSS_mode=LSS_WAITING_MODE; UNS32 SINC_cicle=50000;// us UNS32 size = sizeof(UNS32); /* The slaves are now configured (nodeId and Baudrate) via the LSS services. * Switch the LSS state to WAITING and restart the slaves. */ /*TODO: change the baud rate of the master!!*/ MasterBoard.baudrate="250K"; printf("Master : Switch Delay period finished. Switching to LSS WAITING state\n"); configNetworkNode(d,LSS_SM_GLOBAL,&LSS_mode,0,NULL); printf("Master : Restarting all the slaves\n"); masterSendNMTstateChange (d, 0x00, NMT_Reset_Comunication); printf("Master : Starting the SYNC producer\n"); writeLocalDict( d, /*CO_Data* d*/ 0x1006, /*UNS16 index*/ 0x00, /*UNS8 subind*/ &SINC_cicle, /*void * pSourceData,*/ &size, /* UNS8 * pExpectedSize*/ RW); /* UNS8 checkAccess */ return; } else{ UNS16 Switch_delay=1; UNS8 LSS_mode=LSS_CONFIGURATION_MODE; eprintf("Master : unable to activate bit timing. trying again\n"); configNetworkNode(d,LSS_CONF_ACT_BIT_TIMING,&Switch_delay,0,CheckLSSAndContinue); return; } break; case LSS_SM_SELECTIVE_SERIAL: printf("Slave in LSS CONFIGURATION state\n"); break; case LSS_IDENT_REMOTE_SERIAL_HIGH: printf("node identified\n"); break; case LSS_IDENT_REMOTE_NON_CONF: if(dat1==0) eprintf("There are no-configured remote slave(s) in the net\n"); else { UNS16 Switch_delay=1; UNS8 LSS_mode=LSS_CONFIGURATION_MODE; /*The configuration of the slaves' nodeId ended. * Start the configuration of the baud rate. */ eprintf("Master : There are not no-configured slaves in the net\n"); eprintf("Switching all the nodes to LSS CONFIGURATION state\n"); configNetworkNode(d,LSS_SM_GLOBAL,&LSS_mode,0,NULL); eprintf("LSS=>Activate Bit Timing\n"); configNetworkNode(d,LSS_CONF_ACT_BIT_TIMING,&Switch_delay,0,CheckLSSAndContinue); return; } break; case LSS_INQ_VENDOR_ID: printf("Slave VendorID %x\n", dat1); break; case LSS_INQ_PRODUCT_CODE: printf("Slave Product Code %x\n", dat1); break; case LSS_INQ_REV_NUMBER: printf("Slave Revision Number %x\n", dat1); break; case LSS_INQ_SERIAL_NUMBER: printf("Slave Serial Number %x\n", dat1); break; case LSS_INQ_NODE_ID: printf("Slave nodeid %x\n", dat1); break; #ifdef CO_ENABLE_LSS_FS case LSS_IDENT_FASTSCAN: if(dat1==0) printf("Slave node identified with FastScan\n"); else { printf("There is not unconfigured node in the net\n"); return; } init_step_LSS++; break; #endif } } printf("\n"); ConfigureLSSNode(d); }
static void ConfigureSlaveNode(CO_Data* d, UNS8 nodeId) { /* Master configure heartbeat producer time at 0 ms * for slaves node-id 0x02 and 0x03 by DCF concise */ UNS8 Transmission_Type = 0x01; UNS16 Slave_Prod_Heartbeat_T=1000;//ms UNS32 Master_Cons_Heartbeat_Base=0x05DC; //1500ms UNS32 abortCode; UNS8 res; eprintf("Master : ConfigureSlaveNode %2.2x\n", nodeId); switch(++init_step[nodeId-2]){ case 1: /*First step : setup Slave's TPDO 1 to be transmitted on SYNC*/ eprintf("Master : set slave %2.2x TPDO 1 transmit type\n", nodeId); res = writeNetworkDictCallBack (d, /*CO_Data* d*/ nodeId, /*UNS8 nodeId*/ 0x1800, /*UNS16 index*/ 0x02, /*UNS8 subindex*/ 1, /*UNS8 count*/ 0, /*UNS8 dataType*/ &Transmission_Type,/*void *data*/ CheckSDOAndContinue, /*SDOCallback_t Callback*/ 0); /*UNS8 useBlockMode*/ break; case 2: /* Second step : Set the new heartbeat producer time in the slave */ { UNS32 Master_Cons_Heartbeat_T=Master_Cons_Heartbeat_Base + (nodeId * 0x10000); UNS32 size = sizeof(UNS32); eprintf("Master : set slave %2.2x Producer Heartbeat Time = %d\n", nodeId,Slave_Prod_Heartbeat_T); res = writeNetworkDictCallBack (d, /*CO_Data* d*/ nodeId, /*UNS8 nodeId*/ 0x1017, /*UNS16 index*/ 0x00, /*UNS8 subindex*/ 2, /*UNS8 count*/ 0, /*UNS8 dataType*/ &Slave_Prod_Heartbeat_T,/*void *data*/ CheckSDOAndContinue, /*SDOCallback_t Callback*/ 0); /*UNS8 useBlockMode*/ break; /* Set the new heartbeat consumer time in the master*/ eprintf("Master : set Consumer Heartbeat Time for slave %2.2x = %d\n", nodeId,Master_Cons_Heartbeat_T); writeLocalDict( &TestMaster_Data, /*CO_Data* d*/ 0x1016, /*UNS16 index*/ nodeId-1, /*UNS8 subind*/ &Master_Cons_Heartbeat_T, /*void * pSourceData,*/ &size, /* UNS8 * pExpectedSize*/ RW); /* UNS8 checkAccess */ } break; case 3: /****************************** START *******************************/ /* Put the master in operational mode */ setState(d, Operational); /* Ask slave node to go in operational mode */ masterSendNMTstateChange (d, nodeId, NMT_Start_Node); } }
int main(int argc, char *argv[]) { UNS8 node_id = 0; s_BOARD MasterBoard = {"1", "125K"}; char* dll_file_name; /* process command line arguments */ if (argc < 2) { printf("USAGE: win32test <node_id> [can driver dll filename [baud rate]]\n"); return 1; } node_id = atoi(argv[1]); if (node_id < 2 || node_id > 127) { printf("ERROR: node_id shoule be >=2 and <= 127\n"); return 1; } if (argc > 2) dll_file_name = argv[2]; else dll_file_name = "can_uvccm_win32.dll"; if (argc > 3) MasterBoard.baudrate = argv[3]; // load can driver if (!LoadCanDriver(dll_file_name)) { printf("ERROR: could not load diver %s\n", dll_file_name); return 1; } if (canOpen(&MasterBoard,&win32test_Data)) { /* Defining the node Id */ setNodeId(&win32test_Data, 0x01); /* init */ setState(&win32test_Data, Initialisation); /****************************** START *******************************/ /* Put the master in operational mode */ setState(&win32test_Data, Operational); /* Ask slave node to go in operational mode */ masterSendNMTstateChange (&win32test_Data, 0, NMT_Start_Node); printf("\nStarting node %d (%xh) ...\n",(int)node_id,(int)node_id); /* wait untill mode will switch to operational state*/ if (GetChangeStateResults(node_id, Operational, 3000) != 0xFF) { /* modify Client SDO 1 Parameter */ UNS32 COB_ID_Client_to_Server_Transmit_SDO = 0x600 + node_id; UNS32 COB_ID_Server_to_Client_Receive_SDO = 0x580 + node_id; UNS32 Node_ID_of_the_SDO_Server = node_id; UNS8 ExpectedSize = sizeof (UNS32); if (OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 1, &COB_ID_Client_to_Server_Transmit_SDO, &ExpectedSize, RW) && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 2, &COB_ID_Server_to_Client_Receive_SDO, &ExpectedSize, RW) && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 3, &Node_ID_of_the_SDO_Server, &ExpectedSize, RW)) { UNS32 dev_type = 0; char device_name[64]=""; char hw_ver[64]=""; char sw_ver[64]=""; UNS32 vendor_id = 0; UNS32 prod_code = 0; UNS32 ser_num = 0; UNS8 size; UNS8 res; printf("\nnode_id: %d (%xh) info\n",(int)node_id,(int)node_id); printf("********************************************\n"); size = sizeof (dev_type); res = ReadSDO(node_id, 0x1000, 0, uint32, &dev_type, &size); printf("device type: %d\n",dev_type & 0xFFFF); size = sizeof (device_name); res = ReadSDO(node_id, 0x1008, 0, visible_string, device_name, &size); printf("device name: %s\n",device_name); size = sizeof (hw_ver); res = ReadSDO(node_id, 0x1009, 0, visible_string, hw_ver, &size); printf("HW version: %s\n",hw_ver); size = sizeof (sw_ver); res = ReadSDO(node_id, 0x100A, 0, visible_string, sw_ver, &size); printf("SW version: %s\n",sw_ver); size = sizeof (vendor_id); res = ReadSDO(node_id, 0x1018, 1, uint32, &vendor_id, &size); printf("vendor id: %d\n",vendor_id); size = sizeof (prod_code); res = ReadSDO(node_id, 0x1018, 2, uint32, &prod_code, &size); printf("product code: %d\n",prod_code); size = sizeof (ser_num); res = ReadSDO(node_id, 0x1018, 4, uint32, &ser_num, &size); printf("serial number: %d\n",ser_num); printf("********************************************\n"); } else { printf("ERROR: Object dictionary access failed\n"); } } else { printf("ERROR: node_id %d (%xh) is not responding\n",(int)node_id,(int)node_id); } masterSendNMTstateChange (&win32test_Data, 0x02, NMT_Stop_Node); setState(&win32test_Data, Stopped); canClose(&win32test_Data); } return 0; }