首页 > 代码库 > 驱动代码

驱动代码

   1 1 /*******************************************************************************   2    2 ********************************************************************************   3    3 **   4    4 ** File Name   5    5 ** ---------   6    6 **   7    7 ** aplink.c   8    8 **   9    9 ********************************************************************************  10   11   12 1270   13 1271 /*------------------------------------------------------------------------------  14 1272 ** aplink_RunCfgStateMachine()  15 1273 **------------------------------------------------------------------------------  16 1274 */  17 1275   18 1276 void aplink_RunCfgStateMachine( void )  19 1277 {  20 1278    UTIL_BufMsgType*  psQEntry;  21 1279    static UINT16     iMapCntr;  22 1280    static UINT16     iSingleEntrySize;  23 1281    UINT16*           piTemp;  24 1282   25 1283   26 1284    switch( aplink_s.eCfgState )  27 1285    {  28 1286    case APLINK_CFG_SM_INIT:  29 1287       /*  30 1288       ** Initialise variables etc.  31 1289       */  32 1290   33 1291       aplink_s.psReadCfg   = NULL;  34 1292       aplink_s.psWriteCfg  = NULL;  35 1293   36 1294       aplink_s.iReadPdLength  = 0;  37 1295       aplink_s.iWritePdLength = 0;  38 1296   39 1297       aplink_s.eCfgState = APLINK_CFG_SM_READ_MODULE_TYPE;  40 1298   41 1299       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_INIT." );  42 1300       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_READ_MODULE_TYPE." );  43 1301   44 1302   45 1303       break; /* End case APLINK_CFG_SM_INIT: */  46 1304   47 1305   48 1306    case APLINK_CFG_SM_READ_MODULE_TYPE:  49 1307       /*  50 1308       ** Allocate a buffer.  51 1309       */  52 1310   53 1311       psQEntry = UTIL_AllocateMsgBuffer( &APLINK_asCmdToAbccBuf[ 0 ],  54 1312                                          ABCC_NBR_SIMULTANEOUS_APPL_COMMANDS );  55 1313   56 1314       if( psQEntry == NULL )  57 1315       {  58 1316          ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_OUT_OF_BUFFERS );  59 1317       }  60 1318   61 1319   62 1320       /*  63 1321       ** Read the module type of the ABCC module.  64 1322       */  65 1323   66 1324       psQEntry->sMsg.sHeader.bSourceId = (UINT8)0x01;  67 1325       psQEntry->sMsg.sHeader.bDestObj  = (UINT8)ABP_OBJ_NUM_ANB;  68 1326       psQEntry->sMsg.sHeader.iInstance = 0x0001;  69 1327       psQEntry->sMsg.sHeader.bCmd      = (UINT8)( ABP_CMD_GET_ATTR | ABP_MSG_HEADER_C_BIT );  70 1328       psQEntry->sMsg.sHeader.bDataSize = (UINT8)0x00;  71 1329       psQEntry->sMsg.sHeader.bCmdExt0  = (UINT8)ABP_ANB_IA_MODULE_TYPE;  72 1330       psQEntry->sMsg.sHeader.bCmdExt1  = (UINT8)0x00;  73 1331   74 1332       /*  75 1333       ** Post the message to the ABCC.  76 1334       */  77 1335   78 1336       (void)aplink_AddFragAndPostMsg( psQEntry );  79 1337   80 1338   81 1339       /*  82 1340       ** Wait for the response.  83 1341       */  84 1342   85 1343       aplink_s.eCfgState = APLINK_CFG_SM_WAIT_MODULE_TYPE_RSP;  86 1344   87 1345       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_WAIT_MODULE_TYPE_RSP." );  88 1346   89 1347       break; /* End case APLINK_CFG_SM_READ_MODULE_TYPE */  90 1348   91 1349   92 1350    case APLINK_CFG_SM_WAIT_MODULE_TYPE_RSP:  93 1351       /*  94 1352       ** Check if theres a new response for us.  95 1353       */  96 1354   97 1355       if( UTIL_GetFirstMessageFromQueue( aplink_s.xMsgsFromAbccQueue, &psQEntry ) == UTIL_OK )  98 1356       {  99 1357          /* 100 1358          ** Decrease the number of messages that are processing... 101 1359          */ 102 1360  103 1361          aplink_s.bNbrOfCmdsActiveToAbcc--; 104 1362  105 1363  106 1364          /* 107 1365          ** We have received a new message. 108 1366          */ 109 1367  110 1368          if( ( psQEntry->sMsg.sHeader.bSourceId == (UINT8)0x01 ) && 111 1369              ( psQEntry->sMsg.sHeader.bDestObj == (UINT8)ABP_OBJ_NUM_ANB ) && 112 1370              ( psQEntry->sMsg.sHeader.iInstance == 0x0001 ) && 113 1371              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_C_BIT ) ) ) && 114 1372              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_E_BIT ) ) ) && 115 1373              ( psQEntry->sMsg.sHeader.bDataSize == (UINT8)ABP_ANB_IA_MODULE_TYPE_DS ) && 116 1374              ( psQEntry->sMsg.sHeader.bCmdExt0 == (UINT8)ABP_ANB_IA_MODULE_TYPE ) && 117 1375              ( psQEntry->sMsg.sHeader.bCmdExt1 == (UINT8)0x00 ) ) 118 1376          { 119 1377             /* 120 1378             ** Response is OK. Read the data attached. 121 1379             */ 122 1380  123 1381             piTemp = (UINT16*)&psQEntry->sMsg.abData[ 0 ]; 124 1382             aplink_s.iModuleType = UTIL_WordToNative( *piTemp ); 125 1383  126 1384             ABCC_CbfDebugInfo( "APLINK/Configuration: Module type response received." ); 127 1385  128 1386             /* 129 1387             ** Read the network data-type. 130 1388             */ 131 1389  132 1390             aplink_s.eCfgState = APLINK_CFG_SM_READ_NW_TYPE; 133 1391  134 1392             ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_READ_NW_TYPE." ); 135 1393          } 136 1394          else 137 1395          { 138 1396             /* 139 1397             ** There is some error in the response. 140 1398             */ 141 1399  142 1400             ABCC_CbfDebugInfo( "APLINK/Configuration: Module type error response." ); 143 1401  144 1402             /* 145 1403             ** Signal a fatal error to the application 146 1404             */ 147 1405  148 1406             ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_INVALID_MODULE_TYPE_RECEIVED ); 149 1407  150 1408             /* 151 1409             ** Change driver state, so the applicaton is able to send a RESET 152 1410             */ 153 1411  154 1412             APLINK_ChangeDriverState( APLINK_SM_IN_RUN_NO_PD ); 155 1413  156 1414          } /* End ( Response is not OK? ) */ 157 1415  158 1416          /* 159 1417          ** Free the allocated buffer. 160 1418          */ 161 1419  162 1420          UTIL_FreeMsgBuffer( psQEntry ); 163 1421  164 1422       } /* End if( New message? ) */ 165 1423  166 1424       break; /* End case APLINK_CFG_SM_WAIT_MODULE_TYPE_RSP: */ 167 1425  168 1426  169 1427    case APLINK_CFG_SM_READ_NW_TYPE: 170 1428       /* 171 1429       ** Allocate a buffer. 172 1430       */ 173 1431  174 1432       psQEntry = UTIL_AllocateMsgBuffer( &APLINK_asCmdToAbccBuf[ 0 ], 175 1433                                          ABCC_NBR_SIMULTANEOUS_APPL_COMMANDS ); 176 1434  177 1435       if( psQEntry == NULL ) 178 1436       { 179 1437          ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_OUT_OF_BUFFERS ); 180 1438       } 181 1439  182 1440  183 1441       /* 184 1442       ** Read the network type of the ABCC module. 185 1443       */ 186 1444  187 1445       psQEntry->sMsg.sHeader.bSourceId  = (UINT8)0x01; 188 1446       psQEntry->sMsg.sHeader.bDestObj   = (UINT8)ABP_OBJ_NUM_NW; 189 1447       psQEntry->sMsg.sHeader.iInstance  = 0x0001; 190 1448       psQEntry->sMsg.sHeader.bCmd       = (UINT8)( ABP_CMD_GET_ATTR | ABP_MSG_HEADER_C_BIT ); 191 1449       psQEntry->sMsg.sHeader.bDataSize  = (UINT8)0x00; 192 1450       psQEntry->sMsg.sHeader.bCmdExt0   = (UINT8)ABP_NW_IA_NW_TYPE; 193 1451       psQEntry->sMsg.sHeader.bCmdExt1   = (UINT8)0x00; 194 1452  195 1453  196 1454       /* 197 1455       ** Post the message to the ABCC module. 198 1456       */ 199 1457  200 1458       (void)aplink_AddFragAndPostMsg( psQEntry ); 201 1459  202 1460  203 1461       /* 204 1462       ** Fetch the response. 205 1463       */ 206 1464  207 1465       aplink_s.eCfgState = APLINK_CFG_SM_READ_NW_TYPE_RSP; 208 1466  209 1467       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_READ_NW_TYPE_RSP." ); 210 1468  211 1469       break; /* End case APLINK_CFG_SM_READ_NW_TYPE: */ 212 1470  213 1471  214 1472    case APLINK_CFG_SM_READ_NW_TYPE_RSP: 215 1473       /* 216 1474       ** Check if theres a new response for us. 217 1475       */ 218 1476  219 1477       if( UTIL_GetFirstMessageFromQueue( aplink_s.xMsgsFromAbccQueue, &psQEntry ) == UTIL_OK ) 220 1478       { 221 1479          /* 222 1480          ** Decrease the number of messages that are processing... 223 1481          */ 224 1482  225 1483          aplink_s.bNbrOfCmdsActiveToAbcc--; 226 1484  227 1485  228 1486          /* 229 1487          ** We have received a new message. 230 1488          */ 231 1489  232 1490          if( ( psQEntry->sMsg.sHeader.bSourceId == (UINT8)0x01 ) && 233 1491              ( psQEntry->sMsg.sHeader.bDestObj == (UINT8)ABP_OBJ_NUM_NW ) && 234 1492              ( psQEntry->sMsg.sHeader.iInstance == 0x0001 ) && 235 1493              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_C_BIT ) ) ) && 236 1494              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_E_BIT ) ) ) && 237 1495              ( psQEntry->sMsg.sHeader.bDataSize == (UINT8)ABP_NW_IA_NW_TYPE_DS ) && 238 1496              ( psQEntry->sMsg.sHeader.bCmdExt0 == (UINT8)ABP_NW_IA_NW_TYPE ) && 239 1497              ( psQEntry->sMsg.sHeader.bCmdExt1 == (UINT8)0x00 ) ) 240 1498          { 241 1499             /* 242 1500             ** Response is OK. Read the data attached. 243 1501             */ 244 1502  245 1503             piTemp = (UINT16*)&psQEntry->sMsg.abData[ 0 ]; 246 1504             aplink_s.iNetworkType = UTIL_WordToNative( *piTemp ); 247 1505 //                         AT24CXX_WriteOneByte(10,aplink_s.iNetworkType);/* 引起死机 */ 248 1506                      fsmc_sram_test_write(aplink_s.iNetworkType,2); 249 1507                       250 1508             ABCC_CbfDebugInfo( "APLINK/Configuration: Network type response received." ); 251 1509                       252 1510                       253 1511  254 1512             /* 255 1513             ** Read the network parameter support. 256 1514             */ 257 1515  258 1516             aplink_s.eCfgState = APLINK_CFG_SM_READ_NW_PARAM_SUPPORT; 259 1517  260 1518             ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_READ_NW_PARAM_SUPPORT." ); 261 1519  262 1520          } 263 1521          else 264 1522          { 265 1523             /* 266 1524             ** There is some error in the response. 267 1525             */ 268 1526  269 1527             ABCC_CbfDebugInfo( "APLINK/Configuration: Network Type error response." ); 270 1528  271 1529             /* 272 1530             ** Signal a fatal error to the application 273 1531             */ 274 1532  275 1533             ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_INVALID_NETWORK_TYPE_RECEIVED ); 276 1534  277 1535             /* 278 1536             ** Change driver state, so the applicaton is able to send a RESET 279 1537             */ 280 1538  281 1539             APLINK_ChangeDriverState( APLINK_SM_IN_RUN_NO_PD ); 282 1540  283 1541          } /* End if( Response OK? ) */ 284 1542  285 1543          /* 286 1544          ** Free the allocated buffer. 287 1545          */ 288 1546  289 1547          UTIL_FreeMsgBuffer( psQEntry ); 290 1548  291 1549       } /* End if( New message? ) */ 292 1550              293 1551  294 1552       break; /* End case APLINK_CFG_SM_READ_NW_TYPE_RSP: */ 295 1553  296 1554  297 1555    case APLINK_CFG_SM_READ_NW_PARAM_SUPPORT: 298 1556       /* 299 1557       ** Allocate a buffer. 300 1558       */ 301 1559  302 1560       psQEntry = UTIL_AllocateMsgBuffer( &APLINK_asCmdToAbccBuf[ 0 ], 303 1561                                          ABCC_NBR_SIMULTANEOUS_APPL_COMMANDS ); 304 1562  305 1563       if( psQEntry == NULL ) 306 1564       { 307 1565          ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_OUT_OF_BUFFERS ); 308 1566       } 309 1567  310 1568  311 1569       /* 312 1570       ** Read the network parameter support of the ABCC module. 313 1571       */ 314 1572  315 1573       psQEntry->sMsg.sHeader.bSourceId  = (UINT8)0x01; 316 1574       psQEntry->sMsg.sHeader.bDestObj   = (UINT8)ABP_OBJ_NUM_NW; 317 1575       psQEntry->sMsg.sHeader.iInstance  = 0x0001; 318 1576       psQEntry->sMsg.sHeader.bCmd       = (UINT8)( ABP_CMD_GET_ATTR | ABP_MSG_HEADER_C_BIT ); 319 1577       psQEntry->sMsg.sHeader.bDataSize  = (UINT8)0x00; 320 1578       psQEntry->sMsg.sHeader.bCmdExt0   = (UINT8)ABP_NW_IA_PARAM_SUPPORT; 321 1579       psQEntry->sMsg.sHeader.bCmdExt1   = (UINT8)0x00; 322 1580  323 1581  324 1582       /* 325 1583       ** Post the message to the ABCC module. 326 1584       */ 327 1585  328 1586       (void)aplink_AddFragAndPostMsg( psQEntry ); 329 1587  330 1588  331 1589       /* 332 1590       ** Fetch the response. 333 1591       */ 334 1592  335 1593       aplink_s.eCfgState = APLINK_CFG_SM_READ_NW_PARAM_SUPPORT_RSP; 336 1594  337 1595       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_READ_NW_PARAM_SUPPORT_RSP." ); 338 1596  339 1597       break; /* End case APLINK_CFG_SM_READ_NW_PARAM_SUPPORT: */ 340 1598  341 1599  342 1600    case APLINK_CFG_SM_READ_NW_PARAM_SUPPORT_RSP: 343 1601       /* 344 1602       ** Check if theres a new response for us. 345 1603       */ 346 1604  347 1605       if( UTIL_GetFirstMessageFromQueue( aplink_s.xMsgsFromAbccQueue, &psQEntry ) == UTIL_OK ) 348 1606       { 349 1607          /* 350 1608          ** Decrease the number of messages that are processing... 351 1609          */ 352 1610  353 1611          aplink_s.bNbrOfCmdsActiveToAbcc--; 354 1612  355 1613  356 1614          /* 357 1615          ** We have received a new message. 358 1616          */ 359 1617  360 1618          if( ( psQEntry->sMsg.sHeader.bSourceId == (UINT8)0x01 ) && 361 1619              ( psQEntry->sMsg.sHeader.bDestObj == (UINT8)ABP_OBJ_NUM_NW ) && 362 1620              ( psQEntry->sMsg.sHeader.iInstance == 0x0001 ) && 363 1621              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_C_BIT ) ) ) && 364 1622              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_E_BIT ) ) ) && 365 1623              ( psQEntry->sMsg.sHeader.bDataSize == (UINT8)ABP_NW_IA_PARAM_SUPPORT_DS ) && 366 1624              ( psQEntry->sMsg.sHeader.bCmdExt0 == (UINT8)ABP_NW_IA_PARAM_SUPPORT ) && 367 1625              ( psQEntry->sMsg.sHeader.bCmdExt1 == (UINT8)0x00 ) ) 368 1626          { 369 1627             /* 370 1628             ** Response is OK. Read the data attached. 371 1629             */ 372 1630  373 1631             aplink_s.bParamSupport = psQEntry->sMsg.abData[ 0 ]; 374 1632  375 1633             ABCC_CbfDebugInfo( "APLINK/Configuration: Parameter support response received." ); 376 1634  377 1635  378 1636             /* 379 1637             ** Request pointers for the configuration. 380 1638             */ 381 1639  382 1640             ABCC_CbfAutoCfgRequest( aplink_s.iModuleType, 383 1641                                     aplink_s.iNetworkType, 384 1642                                     aplink_s.bParamSupport, 385 1643                                     &aplink_s.psReadCfg, 386 1644                                     &aplink_s.psWriteCfg ); 387 1645  388 1646  389 1647             /* 390 1648             ** Read the network data format support. 391 1649             */ 392 1650  393 1651             aplink_s.eCfgState = APLINK_CFG_SM_READ_NW_DATA_FORMAT; 394 1652  395 1653             ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_READ_NW_DATA_FORMAT." ); 396 1654          } 397 1655          else 398 1656          { 399 1657             /* 400 1658             ** There is some error in the response. 401 1659             */ 402 1660  403 1661             ABCC_CbfDebugInfo( "APLINK/Configuration: Parameter support error response." ); 404 1662  405 1663             /* 406 1664             ** Signal a fatal error to the application 407 1665             */ 408 1666  409 1667             ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_INVALID_PARAM_SUPPORT_RECEIVED ); 410 1668  411 1669             /* 412 1670             ** Change driver state, so the applicaton is able to send a RESET 413 1671             */ 414 1672  415 1673             APLINK_ChangeDriverState( APLINK_SM_IN_RUN_NO_PD ); 416 1674  417 1675          } /* End if( Response OK? ) */ 418 1676  419 1677          /* 420 1678          ** Free the allocated buffer. 421 1679          */ 422 1680  423 1681          UTIL_FreeMsgBuffer( psQEntry ); 424 1682  425 1683       } /* End if( New message? ) */ 426 1684              427 1685  428 1686  429 1687       break; /* End case APLINK_CFG_SM_READ_NW_PARAM_SUPPORT_RSP: */ 430 1688  431 1689  432 1690    case APLINK_CFG_SM_READ_NW_DATA_FORMAT: 433 1691       /* 434 1692       ** Allocate a buffer. 435 1693       */ 436 1694  437 1695       psQEntry = UTIL_AllocateMsgBuffer( &APLINK_asCmdToAbccBuf[ 0 ], 438 1696                                          ABCC_NBR_SIMULTANEOUS_APPL_COMMANDS ); 439 1697  440 1698       if( psQEntry == NULL ) 441 1699       { 442 1700          ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_OUT_OF_BUFFERS ); 443 1701       } 444 1702  445 1703  446 1704       /* 447 1705       ** Read the Network data format of the module. 448 1706       */ 449 1707  450 1708       psQEntry->sMsg.sHeader.bSourceId  = (UINT8)0x01; 451 1709       psQEntry->sMsg.sHeader.bDestObj   = (UINT8)ABP_OBJ_NUM_NW; 452 1710       psQEntry->sMsg.sHeader.iInstance  = 0x0001; 453 1711       psQEntry->sMsg.sHeader.bCmd       = (UINT8)( ABP_CMD_GET_ATTR | ABP_MSG_HEADER_C_BIT ); 454 1712       psQEntry->sMsg.sHeader.bDataSize  = (UINT8)0x00; 455 1713       psQEntry->sMsg.sHeader.bCmdExt0   = (UINT8)ABP_NW_IA_DATA_FORMAT; 456 1714       psQEntry->sMsg.sHeader.bCmdExt1   = (UINT8)0x00; 457 1715  458 1716       /* 459 1717       ** Post the message to the ABCC. 460 1718       */ 461 1719  462 1720       (void)aplink_AddFragAndPostMsg( psQEntry ); 463 1721  464 1722  465 1723       /* 466 1724       ** Wait for the response. 467 1725       */ 468 1726  469 1727       aplink_s.eCfgState = APLINK_CFG_SM_WAIT_NW_DATA_FORMAT_RSP; 470 1728  471 1729       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_WAIT_NW_DATA_FORMAT_RSP." ); 472 1730  473 1731       break; /* End case APLINK_CFG_SM_READ_NW_DATA_FORMAT: */ 474 1732  475 1733  476 1734    case APLINK_CFG_SM_WAIT_NW_DATA_FORMAT_RSP: 477 1735       /* 478 1736       ** Check if theres a new response for us. 479 1737       */ 480 1738  481 1739       if( UTIL_GetFirstMessageFromQueue( aplink_s.xMsgsFromAbccQueue, &psQEntry ) == UTIL_OK ) 482 1740       { 483 1741          /* 484 1742          ** Decrease the number of messages that are processing... 485 1743          */ 486 1744  487 1745          aplink_s.bNbrOfCmdsActiveToAbcc--; 488 1746  489 1747  490 1748          /* 491 1749          ** We have received a new message. 492 1750          */ 493 1751  494 1752          if( ( psQEntry->sMsg.sHeader.bSourceId == (UINT8)0x01 ) && 495 1753              ( psQEntry->sMsg.sHeader.bDestObj == (UINT8)ABP_OBJ_NUM_NW ) && 496 1754              ( psQEntry->sMsg.sHeader.iInstance == 0x0001 ) && 497 1755              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_C_BIT ) ) ) && 498 1756              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_E_BIT ) ) ) && 499 1757              ( psQEntry->sMsg.sHeader.bDataSize == (UINT8)ABP_NW_IA_DATA_FORMAT_DS ) && 500 1758              ( psQEntry->sMsg.sHeader.bCmdExt0 == (UINT8)ABP_NW_IA_DATA_FORMAT ) && 501 1759              ( psQEntry->sMsg.sHeader.bCmdExt1 == (UINT8)0x00 ) ) 502 1760          { 503 1761             /* 504 1762             ** Signal the network data format to the application. 505 1763             */ 506 1764  507 1765             ABCC_CbfNetworkDataFormat( (ABCC_DataFormatType)psQEntry->sMsg.abData[ 0 ] ); 508 1766  509 1767             ABCC_CbfDebugInfo( "APLINK/Configuration: Network data-type response received." ); 510 1768  511 1769             iMapCntr = 0; 512 1770  513 1771             /* 514 1772             ** Change state 515 1773             */ 516 1774  517 1775             aplink_s.eCfgState = APLINK_CFG_SM_NEXT_READ; 518 1776  519 1777          } 520 1778          else 521 1779          { 522 1780             /* 523 1781             ** There is some error in the response. 524 1782             */ 525 1783  526 1784             ABCC_CbfDebugInfo( "APLINK/Configuration: Network data-format error response." ); 527 1785  528 1786             /* 529 1787             ** Signal a fatal error to the application 530 1788             */ 531 1789  532 1790             ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_INVALID_DATA_FORMAT_RECEIVED ); 533 1791  534 1792             /* 535 1793             ** Change driver state, so the applicaton is able to send a RESET 536 1794             */ 537 1795  538 1796             APLINK_ChangeDriverState( APLINK_SM_IN_RUN_NO_PD ); 539 1797  540 1798          } /* End if( New Message OK? ) */ 541 1799  542 1800          /* 543 1801          ** Free the allocated buffer. 544 1802          */ 545 1803  546 1804          UTIL_FreeMsgBuffer( psQEntry ); 547 1805  548 1806       } /* End if( New message? ) */ 549 1807              550 1808  551 1809                 ABCC_CbfCfgSetAddress( Network_Address()); 552 1810  553 1811       break; /* End case APLINK_CFG_SM_WAIT_NW_DATA_FORMAT_RSP: */ 554 1812  555 1813  556 1814    case APLINK_CFG_SM_SEND_READ_MAPPING: 557 1815       /* 558 1816       ** Allocate a buffer. 559 1817       */ 560 1818  561 1819       psQEntry = UTIL_AllocateMsgBuffer( &APLINK_asCmdToAbccBuf[ 0 ], 562 1820                                          ABCC_NBR_SIMULTANEOUS_APPL_COMMANDS ); 563 1821  564 1822       if( psQEntry == NULL ) 565 1823       { 566 1824          ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_OUT_OF_BUFFERS ); 567 1825       } 568 1826  569 1827  570 1828       /* 571 1829       ** Send a Read mapping message 572 1830       */ 573 1831  574 1832       psQEntry->sMsg.sHeader.bSourceId = (UINT8)0x01; 575 1833       psQEntry->sMsg.sHeader.bDestObj  = (UINT8)ABP_OBJ_NUM_NW; 576 1834       psQEntry->sMsg.sHeader.iInstance = 0x0001; 577 1835       psQEntry->sMsg.sHeader.bCmd      = (UINT8)( ABP_NW_CMD_MAP_ADI_READ_AREA | ABP_MSG_HEADER_C_BIT ); 578 1836       psQEntry->sMsg.sHeader.bDataSize = (UINT8)0x04; 579 1837       psQEntry->sMsg.sHeader.bCmdExt0  = (UINT8)( aplink_s.psReadCfg->psMaps[ iMapCntr ].iAdiNbr & 0x00FF ); 580 1838       psQEntry->sMsg.sHeader.bCmdExt1  = (UINT8)( ( aplink_s.psReadCfg->psMaps[ iMapCntr ].iAdiNbr >> 8 ) & 0x00FF ); 581 1839  582 1840       /* 583 1841       ** Assign the data type and the number of elements. 584 1842       */ 585 1843  586 1844             if(fsmc_sram_test_read(2) == 0X90) 587 1845             { 588 1846         aplink_s.psReadCfg->psMaps[ iMapCntr ].bDataType = 0; 589 1847                 aplink_s.psReadCfg->psMaps[ iMapCntr ].bNbrElements = 16; 590 1848                  psQEntry->sMsg.abData[ 0 ] = aplink_s.psReadCfg->psMaps[ iMapCntr ].bDataType; 591 1849                  psQEntry->sMsg.abData[ 1 ] = aplink_s.psReadCfg->psMaps[ iMapCntr ].bNbrElements; 592 1850             } 593 1851             else 594 1852             { 595 1853              psQEntry->sMsg.abData[ 0 ] = aplink_s.psReadCfg->psMaps[ iMapCntr ].bDataType; 596 1854        psQEntry->sMsg.abData[ 1 ] = aplink_s.psReadCfg->psMaps[ iMapCntr ].bNbrElements; 597 1855             } 598 1856 //             printf("%2X\r\n",psQEntry->sMsg.abData[ 0 ] ); 599 1857 //             printf("%2X\r\n",psQEntry->sMsg.abData[ 1 ] );             600 1858  601 1859       /* 602 1860       ** Assign the order number. Low byte first. 603 1861       */ 604 1862  605 1863       psQEntry->sMsg.abData[ 2 ]  = (UINT8)( aplink_s.psReadCfg->psMaps[ iMapCntr ].iOrderNumber & 0x00FF ); 606 1864       psQEntry->sMsg.abData[ 3 ]  = (UINT8)( ( aplink_s.psReadCfg->psMaps[ iMapCntr ].iOrderNumber >> 8 ) & 0x00FF ); 607 1865  608 1866       /* 609 1867       ** Store the number of bytes of this mapping. 610 1868       */ 611 1869  612 1870       iSingleEntrySize = ( aplink_GetSizeOfDataType( aplink_s.psReadCfg->psMaps[ iMapCntr ].bDataType ) * 613 1871                            aplink_s.psReadCfg->psMaps[ iMapCntr ].bNbrElements ); 614 1872  615 1873  616 1874       /* 617 1875       ** Post the message to the ABCC. 618 1876       */ 619 1877  620 1878       (void)aplink_AddFragAndPostMsg( psQEntry ); 621 1879  622 1880  623 1881       /* 624 1882       ** Change state 625 1883       */ 626 1884  627 1885       aplink_s.eCfgState = APLINK_CFG_SM_WAIT_READ_MAPPING_RSP; 628 1886  629 1887       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_WAIT_READ_MAPPING_RSP." ); 630 1888  631 1889       break; /* End case APLINK_CFG_SM_SEND_READ_MAPPING: */ 632 1890  633 1891  634 1892    case APLINK_CFG_SM_WAIT_READ_MAPPING_RSP: 635 1893       /* 636 1894       ** Waite for a response 637 1895       */ 638 1896  639 1897       if( UTIL_GetFirstMessageFromQueue( aplink_s.xMsgsFromAbccQueue, &psQEntry ) == UTIL_OK ) 640 1898       { 641 1899          /* 642 1900          ** Decrease the number of messages that are processing... 643 1901          */ 644 1902  645 1903          aplink_s.bNbrOfCmdsActiveToAbcc--; 646 1904  647 1905  648 1906          /* 649 1907          ** We have received a new message. 650 1908          */ 651 1909  652 1910          if( ( psQEntry->sMsg.sHeader.bSourceId == (UINT8)0x01 ) && 653 1911              ( psQEntry->sMsg.sHeader.bDestObj == (UINT8)ABP_OBJ_NUM_NW ) && 654 1912              ( psQEntry->sMsg.sHeader.iInstance == 0x0001 ) && 655 1913              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_C_BIT ) ) ) && 656 1914              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_E_BIT ) ) ) && 657 1915              ( psQEntry->sMsg.sHeader.bDataSize == (UINT8)0x01 ) && 658 1916              ( psQEntry->sMsg.sHeader.bCmdExt0 == (UINT8)( aplink_s.psReadCfg->psMaps[ iMapCntr ].iAdiNbr & 0x00FF ) ) && 659 1917              ( psQEntry->sMsg.sHeader.bCmdExt1 == (UINT8)( ( aplink_s.psReadCfg->psMaps[ iMapCntr ].iAdiNbr >> 8 ) & 0x00FF ) ) ) 660 1918          { 661 1919             ABCC_CbfDebugInfo( "APLINK/Configuration: Read map added OK." ); 662 1920  663 1921             /* 664 1922             ** Response is OK. Read the data attached. 665 1923             */ 666 1924  667 1925             aplink_s.psReadCfg->psMaps[ iMapCntr ].bAreaOffset = psQEntry->sMsg.abData[ 0 ]; 668 1926             aplink_s.psReadCfg->psMaps[ iMapCntr ].bMapStatus  = (UINT8)1; 669 1927  670 1928             /* 671 1929             ** Add the mapped data size. 672 1930             */ 673 1931  674 1932             aplink_s.iReadPdLength += iSingleEntrySize; 675 1933  676 1934          } 677 1935          else 678 1936          { 679 1937             /* 680 1938             ** Error in message 681 1939             */ 682 1940  683 1941             ABCC_CbfDebugInfo( "APLINK/Configuration: Read map error." ); 684 1942  685 1943             /* 686 1944             ** Response is NOT OK. Mark the mapping as not mapped. 687 1945             */ 688 1946  689 1947             aplink_s.psReadCfg->psMaps[ iMapCntr ].bAreaOffset = (UINT8)0; 690 1948             aplink_s.psReadCfg->psMaps[ iMapCntr ].bMapStatus  = (UINT8)0; 691 1949  692 1950          } /* End if( correct message? ) */ 693 1951  694 1952  695 1953          /* 696 1954          ** Increase the number of mappings. 697 1955          */ 698 1956  699 1957          iMapCntr++; 700 1958  701 1959  702 1960          /* 703 1961          ** Check for more mappings. 704 1962          */ 705 1963  706 1964          aplink_s.eCfgState = APLINK_CFG_SM_NEXT_READ; 707 1965  708 1966  709 1967          /* 710 1968          ** Free the allocated buffer. 711 1969          */ 712 1970  713 1971          UTIL_FreeMsgBuffer( psQEntry ); 714 1972  715 1973       } /* End if( New Response? ) */ 716 1974  717 1975       break; /* End case APLINK_CFG_SM_WAIT_READ_MAPPING_RSP: */ 718 1976  719 1977  720 1978    case APLINK_CFG_SM_NEXT_READ: 721 1979       /* 722 1980       ** Check if there are more mappings. 723 1981       */ 724 1982  725 1983       if( ( aplink_s.psReadCfg != NULL ) && 726 1984           ( aplink_s.psReadCfg->iNbrMaps != 0 ) && 727 1985           ( aplink_s.psReadCfg->iNbrMaps <= 256 ) && 728 1986           ( iMapCntr < aplink_s.psReadCfg->iNbrMaps ) ) 729 1987       { 730 1988          /* 731 1989          ** There are more "READ"-mappings left. Change state. 732 1990          */ 733 1991  734 1992          aplink_s.eCfgState = APLINK_CFG_SM_SEND_READ_MAPPING; 735 1993       } 736 1994       else 737 1995       { 738 1996          /* 739 1997          ** Check if there is a write mapping available 740 1998          */ 741 1999  742 2000          if( ( aplink_s.psWriteCfg != NULL ) && 743 2001              ( aplink_s.psWriteCfg->iNbrMaps != 0 ) && 744 2002              ( aplink_s.psWriteCfg->iNbrMaps <= 256 ) ) 745 2003          { 746 2004             /* 747 2005             ** There are at least one write mapping available 748 2006             */ 749 2007  750 2008             iMapCntr = 0; 751 2009  752 2010             /* 753 2011             ** Change state 754 2012             */ 755 2013  756 2014             aplink_s.eCfgState = APLINK_CFG_SM_SEND_WRITE_MAPPING; 757 2015  758 2016             ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_SEND_WRITE_MAPPING." ); 759 2017          } 760 2018          else 761 2019          { 762 2020             /* 763 2021             ** There are NO "WRITE"-mappings. Change driver state. 764 2022             */ 765 2023  766 2024             APLINK_ChangeDriverState( APLINK_SM_IN_RUN_NO_PD ); 767 2025  768 2026  769 2027             /* 770 2028             ** No read or write mapping available 771 2029             */ 772 2030  773 2031             ABCC_CbfCfgCompleted(); 774 2032  775 2033          } /* End if write mapping OK? ) */ 776 2034  777 2035       } /* End if( More Read mappings? ) */ 778 2036  779 2037       break; /* End case APLINK_CFG_SM_NEXT_READ: */ 780 2038  781 2039  782 2040    case APLINK_CFG_SM_SEND_WRITE_MAPPING: 783 2041       /* 784 2042       ** Allocate a buffer. 785 2043       */ 786 2044  787 2045       psQEntry = UTIL_AllocateMsgBuffer( &APLINK_asCmdToAbccBuf[ 0 ], 788 2046                                          ABCC_NBR_SIMULTANEOUS_APPL_COMMANDS ); 789 2047  790 2048       if( psQEntry == NULL ) 791 2049       { 792 2050          ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_OUT_OF_BUFFERS ); 793 2051       } 794 2052  795 2053  796 2054       /* 797 2055       ** Send a Write mapping meassage 798 2056       */ 799 2057  800 2058       psQEntry->sMsg.sHeader.bSourceId  = (UINT8)0x01; 801 2059       psQEntry->sMsg.sHeader.bDestObj   = (UINT8)ABP_OBJ_NUM_NW; 802 2060       psQEntry->sMsg.sHeader.iInstance  = 0x0001; 803 2061       psQEntry->sMsg.sHeader.bCmd       = (UINT8)( ABP_NW_CMD_MAP_ADI_WRITE_AREA | ABP_MSG_HEADER_C_BIT ); 804 2062       psQEntry->sMsg.sHeader.bDataSize  = (UINT8)0x04; 805 2063       psQEntry->sMsg.sHeader.bCmdExt0   = (UINT8)( aplink_s.psWriteCfg->psMaps[ iMapCntr ].iAdiNbr & 0x00FF ); 806 2064       psQEntry->sMsg.sHeader.bCmdExt1   = (UINT8)( ( aplink_s.psWriteCfg->psMaps[ iMapCntr ].iAdiNbr >> 8 ) & 0x00FF ); 807 2065  808 2066       /* 809 2067       ** Assign the data type and number of elements. 810 2068       */ 811 2069  812 2070             if(fsmc_sram_test_read(2) == 0X90) 813 2071             { 814 2072                 aplink_s.psWriteCfg->psMaps[ iMapCntr ].bDataType = 0; 815 2073                 aplink_s.psWriteCfg->psMaps[ iMapCntr ].bNbrElements = 16; 816 2074                 psQEntry->sMsg.abData[ 0 ] = aplink_s.psWriteCfg->psMaps[ iMapCntr ].bDataType; 817 2075                 psQEntry->sMsg.abData[ 1 ] = aplink_s.psWriteCfg->psMaps[ iMapCntr ].bNbrElements; 818 2076             } 819 2077      else 820 2078             { 821 2079                 psQEntry->sMsg.abData[ 0 ] = aplink_s.psWriteCfg->psMaps[ iMapCntr ].bDataType; 822 2080                 psQEntry->sMsg.abData[ 1 ] = aplink_s.psWriteCfg->psMaps[ iMapCntr ].bNbrElements;  823 2081             } 824 2082  825 2083       /* 826 2084       ** Assign the order number. Low byte first. 827 2085       */ 828 2086  829 2087       psQEntry->sMsg.abData[ 2 ]  = (UINT8)( aplink_s.psWriteCfg->psMaps[ iMapCntr ].iOrderNumber & 0x00FF ); 830 2088       psQEntry->sMsg.abData[ 3 ]  = (UINT8)( ( aplink_s.psWriteCfg->psMaps[ iMapCntr ].iOrderNumber >> 8 ) & 0x00FF ); 831 2089  832 2090  833 2091       /* 834 2092       ** Store the number of bytes of this mapping. 835 2093       */ 836 2094  837 2095       iSingleEntrySize = ( aplink_GetSizeOfDataType( aplink_s.psWriteCfg->psMaps[ iMapCntr ].bDataType ) * 838 2096                            aplink_s.psWriteCfg->psMaps[ iMapCntr ].bNbrElements ); 839 2097  840 2098       /* 841 2099       ** Post the message to the ABCC. 842 2100       */ 843 2101  844 2102       (void)aplink_AddFragAndPostMsg( psQEntry ); 845 2103  846 2104  847 2105       /* 848 2106       ** Change state 849 2107       */ 850 2108  851 2109       aplink_s.eCfgState = APLINK_CFG_SM_WAIT_WRITE_MAPPING_RSP; 852 2110  853 2111       ABCC_CbfDebugInfo( "APLINK/Configuration: In APLINK_CFG_SM_WAIT_WRITE_MAPPING_RSP." ); 854 2112  855 2113       break; /* End case APLINK_CFG_SM_SEND_WRITE_MAPPING: */ 856 2114  857 2115  858 2116    case APLINK_CFG_SM_WAIT_WRITE_MAPPING_RSP: 859 2117       /* 860 2118       ** Waite for a response 861 2119       */ 862 2120  863 2121       if( UTIL_GetFirstMessageFromQueue( aplink_s.xMsgsFromAbccQueue, &psQEntry ) == UTIL_OK ) 864 2122       { 865 2123          /* 866 2124          ** Decrease the number of messages that are processing... 867 2125          */ 868 2126  869 2127          aplink_s.bNbrOfCmdsActiveToAbcc--; 870 2128  871 2129  872 2130          /* 873 2131          ** We have received a new message. 874 2132          */ 875 2133  876 2134          if( ( psQEntry->sMsg.sHeader.bSourceId == (UINT8)0x01 ) && 877 2135              ( psQEntry->sMsg.sHeader.bDestObj == (UINT8)ABP_OBJ_NUM_NW ) && 878 2136              ( psQEntry->sMsg.sHeader.iInstance == 0x0001 ) && 879 2137              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_C_BIT ) ) ) && 880 2138              ( !( (BOOL8)( psQEntry->sMsg.sHeader.bCmd & (UINT8)ABP_MSG_HEADER_E_BIT ) ) ) && 881 2139              ( psQEntry->sMsg.sHeader.bDataSize == (UINT8)0x01 ) && 882 2140              ( psQEntry->sMsg.sHeader.bCmdExt0 == (UINT8)( aplink_s.psWriteCfg->psMaps[ iMapCntr ].iAdiNbr & 0x00FF ) ) && 883 2141              ( psQEntry->sMsg.sHeader.bCmdExt1 == (UINT8)( ( aplink_s.psWriteCfg->psMaps[ iMapCntr ].iAdiNbr >> 8 ) & 0x00FF ) ) ) 884 2142          { 885 2143             /* 886 2144             ** Response is OK. Read the data attached. 887 2145             */ 888 2146        889 2147             aplink_s.psWriteCfg->psMaps[ iMapCntr ].bAreaOffset = psQEntry->sMsg.abData[ 0 ]; 890 2148             aplink_s.psWriteCfg->psMaps[ iMapCntr ].bMapStatus  = (UINT8)1; 891 2149  892 2150             /* 893 2151             ** Add the mapped data size. 894 2152             */ 895 2153  896 2154             aplink_s.iWritePdLength += iSingleEntrySize; 897 2155  898 2156  899 2157             ABCC_CbfDebugInfo( "APLINK/Configuration: Write map OK." ); 900 2158          } 901 2159          else 902 2160          { 903 2161             /* 904 2162             ** Error in message 905 2163             */ 906 2164  907 2165             ABCC_CbfDebugInfo( "APLINK/Configuration: Write map error." ); 908 2166  909 2167             /* 910 2168             ** Response is NOT OK. Mark the map. 911 2169             */ 912 2170  913 2171             aplink_s.psWriteCfg->psMaps[ iMapCntr ].bAreaOffset = (UINT8)0; 914 2172             aplink_s.psWriteCfg->psMaps[ iMapCntr ].bMapStatus  = (UINT8)0; 915 2173  916 2174          } /* End if( correct message? ) */ 917 2175  918 2176  919 2177          /* 920 2178          ** Increase the number of mappings. 921 2179          */ 922 2180  923 2181          iMapCntr++; 924 2182  925 2183  926 2184          /* 927 2185          ** Check if more mappings... 928 2186          */ 929 2187  930 2188          aplink_s.eCfgState = APLINK_CFG_SM_NEXT_WRITE; 931 2189  932 2190                      933 2191          /* 934 2192          ** Free the allocated buffer. 935 2193          */ 936 2194  937 2195          UTIL_FreeMsgBuffer( psQEntry ); 938 2196                   939 2197 //                      ABCC_CbfCfgSetAddress(Network_Address()); 940 2198 //                     ABCC_CbfCfgSetBand(Network_Band());   //这儿加的设置波特率的函数 941 2199  942 2200       } /* End if( New Response? ) */ 943 2201  944 2202       break; /* End case APLINK_CFG_SM_WAIT_WRITE_MAPPING_RSP: */ 945 2203  946 2204  947 2205    case APLINK_CFG_SM_NEXT_WRITE: 948 2206       /* 949 2207       ** Check if there are more "WRITE"-mappings to process. 950 2208       */ 951 2209  952 2210       if( iMapCntr < aplink_s.psWriteCfg->iNbrMaps ) 953 2211       { 954 2212          /* 955 2213          ** Change state 956 2214          */ 957 2215  958 2216          aplink_s.eCfgState = APLINK_CFG_SM_SEND_WRITE_MAPPING; 959 2217       } 960 2218       else 961 2219       { 962 2220          /* 963 2221          ** No more mappings to process. 964 2222          ** Change driver state 965 2223          */ 966 2224          967 2225          APLINK_ChangeDriverState( APLINK_SM_IN_RUN_NO_PD ); 968 2226  969 2227          /* 970 2228          ** No more mapping available 971 2229          */ 972 2230                  973 2231                  974 2232 //        if(fsmc_sram_test_read(2) == 0X90)/*只有在模块是CC-Link时,才配置波特率*/ 975 2233 //         { 976 2234                               977 2235 // //      } 978 2236          ABCC_CbfCfgCompleted(); 979 2237  980 2238       } /* End if( More Write mappings? ) */ 981 2239  982 2240       break; /* End case APLINK_CFG_SM_NEXT_WRITE: */ 983 2241  984 2242  985 2243    default: 986 2244  987 2245       /* 988 2246       ** Signal a fatal error to the application 989 2247       */ 990 2248  991 2249       ABCC_CbfDriverError( ABCC_SEV_FATAL, ABCC_EC_ILLEGAL_CFG_STATE ); 992 2250  993 2251       break; 994 2252  995 2253    } /* End switch( aplink_s.eCfgState ) */ 996 2254  997 2255 } /* End of aplink_RunCfgStateMachine() */ 998 2256  999 2257 1000 2258 /*------------------------------------------------------------------------------1001 2259 ** aplink_HandleQueuedMsgsToAppl()1002 2260 **------------------------------------------------------------------------------1003 2261 */1004 22621006  1 void ABCC_CbfCfgSetBand( UINT8 band )1007  2 {1008  3     1009  4     /* Message Definition */1010  5    ABP_MsgType sMsg;1011  6    sMsg.sHeader.bSourceId = 4;// SourceId = 31012  7    sMsg.sHeader.bDestObj = 4;// Object Anybus Object (04h)1013  8    sMsg.sHeader.iInstance = 2;// Instance = 11014  9    sMsg.sHeader.bCmd = 0x42;// Command, Set attribute1015 10    sMsg.sHeader.bDataSize = 1;// Data size = 1 byte1016 11    sMsg.sHeader.bCmdExt0 = 5;// Attribute = 5 (‘setup Complete‘-flag)1017 12    sMsg.sHeader.bCmdExt1 = 0;// (reserved)1018 13    sMsg.abData[ 0 ] = band;// Data1019 14    ABCC_SendMessage( &sMsg ); // Send Message1020 15 1021 16 } /* End of  ABCC_CbfCfgSetBand() */  //设置波特率的函数

 

驱动代码