1 // *********************************************************************
    2 //   General public DPA header file                                    *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: DPA.h,v $
    7 // Version: $Revision: 1.250 $
    8 // Date:    $Date: 2019/06/12 11:37:17 $
    9 //
   10 // Revision history:
   11 //   2019/06/12  Release for DPA 4.03
   12 //   2019/06/03  Release for DPA 4.02
   13 //   2019/03/07  Release for DPA 4.01
   14 //   2019/01/10  Release for DPA 4.00
   15 //   2018/10/25  Release for DPA 3.03
   16 //   2017/11/16  Release for DPA 3.02
   17 //   2017/08/14  Release for DPA 3.01
   18 //   2017/03/13  Release for DPA 3.00
   19 //   2016/09/12  Release for DPA 2.28
   20 //   2016/04/14  Release for DPA 2.27
   21 //   2016/03/03  Release for DPA 2.26
   22 //   2016/01/21  Release for DPA 2.25
   23 //   2015/12/01  Release for DPA 2.24
   24 //   2015/10/23  Release for DPA 2.23
   25 //   2015/09/25  Release for DPA 2.22
   26 //   2015/09/03  Release for DPA 2.21
   27 //   2015/08/05  Release for DPA 2.20
   28 //   2014/10/31  Release for DPA 2.10
   29 //   2014/04/30  Release for DPA 2.00
   30 //   2013/10/03  Release for DPA 1.00
   31 //
   32 // *********************************************************************
   33 
   34 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   35 // IQRF Standards documentation https://www.iqrfalliance.org/techDocs/
   36 
   37 #ifndef _DPA_HEADER_
   38 #define _DPA_HEADER_
   39 
   40 //############################################################################################
   41 
   42 // DPA version
   43 #define DPA_VERSION_MASTER          0x0403
   44 
   45 #ifdef __CC5X__
   46 // Compiled only at CC5X
   47 #if __CC5X__ < 3703
   48 #error Insufficient CC5X compiler version, V3.7C is minimum
   49 #endif
   50 
   51 #if IQRFOS < 403
   52 #error IQRF OS 4.03+ is required
   53 #endif
   54 
   55 // Bank for custom variables
   56 #pragma rambank = UserBank_01
   57 
   58 // Main DPA API entry address (also start of the licensed FLASH) 
   59 #define DPA_API_ADDRESS             __LICENSED_FLASH
   60 
   61 // Main DPA entry address
   62 #define MAIN_DPA_ADDRESS            ( DPA_API_ADDRESS + 4 )
   63 
   64 // Main DPA API entry address stub
   65 #define DPA_API_ADDRESS_ENTRY       0x3A08
   66 
   67 // Address of the DPA Custom Handler
   68 #define CUSTOM_HANDLER_ADDRESS      0x3A20
   69 
   70 // Address of the DPA Custom Handler end + 1
   71 #define CUSTOM_HANDLER_ADDRESS_END  0x3D80
   72 
   73 // DPA API entry function
   74 uns8  DpaApiEntry( uns8 par1, uns8 par2, uns8 apiIndex );
   75 
   76 // DPA API codes
   77 #define DPA_API_RFTX_DPAPACKET              0
   78 #define DPA_API_READ_CONFIG_BYTE            1
   79 #define DPA_API_SEND_TO_IFACEMASTER         2
   80 #define DPA_API_COORDINATOR_RFTX_DPAPACKET  3
   81 #define DPA_API_LOCAL_REQUEST               4
   82 #define DPA_API_SET_PERIPHERAL_ERROR        5
   83 #define DPA_API_SET_RF_DEFAULTS             6
   84 
   85 // Used buffer size symbols
   86 #define sizeofBufferAUX                     sizeof( bufferAUX )
   87 #define sizeofBufferCOM                     sizeof( bufferCOM )
   88 
   89 #define STRUCTATTR
   90 
   91 #else //__CC5X__
   92 // Not compiled at CC5X
   93 
   94 // Define CC5X types
   95 typedef uint8_t   uns8;
   96 typedef uint16_t  uns16;
   97 
   98 // Define some types missing at Arduino
   99 #ifdef Arduino_h
  100 typedef int8_t  int8;
  101 typedef int16_t int16;
  102 #endif
  103 
  104 // Fake buffer sizes
  105 #define sizeofBufferAUX 64
  106 #define sizeofBufferCOM 64
  107 
  108 // Disables alignment of members of structures
  109 #define STRUCTATTR  __attribute__((packed))
  110 
  111 #endif  // __CC5X__
  112 
  113 // Indexes of configuration bytes used by DpaApiReadConfigByte( index )
  114 // Checksum
  115 #define CFGIND_CHECKSUM         0x00
  116 // Embedded peripherals
  117 #define CFGIND_DPA_PERIPHERALS  0x01
  118 // DPA configuration flags
  119 #define CFGIND_DPA_FLAGS        0x05
  120 // Main RF channel, used by the subordinate network
  121 #define CFGIND_CHANNEL_2ND_A    0x06
  122 // Second RF channel, used by the subordinate network
  123 #define CFGIND_CHANNEL_2ND_B    0x07
  124 // TX power
  125 #define CFGIND_TXPOWER          0x08
  126 // RX filter used by checkRF()
  127 #define CFGIND_RXFILTER         0x09
  128 // toutRF for LP mode
  129 #define CFGIND_DPA_LP_TOUTRF    0x0A
  130 // UART interface baud rate
  131 #define CFGIND_DPA_UART_IFACE_SPEED 0x0B
  132 // Alternate DSM channel
  133 #define CFGIND_ALTERNATE_DSM_CHANNEL 0x0C
  134 // Main RF channel
  135 #define CFGIND_CHANNEL_A        0x11
  136 // Second RF channel
  137 #define CFGIND_CHANNEL_B        0x12
  138 
  139 // 0: checks only mandatory precondition in order to prevent critical side-effects
  140 // 1: as above plus checks meaningful parameter conditions
  141 // 2: full implemented parameter checking (default)
  142 #ifndef PARAM_CHECK_LEVEL
  143 #define PARAM_CHECK_LEVEL 2
  144 #endif
  145 
  146 // "foursome" at IFace structure
  147 typedef struct
  148 {
  149   // Node address low byte
  150   uns8  NADRlow;
  151   // Node address high byte
  152   uns8  NADRhigh;
  153   // Peripheral number
  154   uns8  PNUM;
  155   // Peripheral command
  156   uns8  PCMD;
  157   // HWPID
  158   uns16 HWPID;
  159 } STRUCTATTR TDpaIFaceHeader;
  160 
  161 // Maximum command PCMD value (except reserved 0x3F = CMD_GET_PER_INFO)
  162 #define PCMD_MAX                    0x7f
  163 // Bit mask at PCMD that indicates DPA Response message
  164 #define RESPONSE_FLAG               0x80
  165 
  166 // IQMESH coordinator address
  167 #define COORDINATOR_ADDRESS         0x00
  168 // IQMESH broadcast address
  169 #define BROADCAST_ADDRESS           0xff
  170 // IQMESH temporary address, assigned by pre-bonding before authorization is done
  171 #define TEMPORARY_ADDRESS           0xfe
  172 // Address of the local device addressed by IFace
  173 #define LOCAL_ADDRESS               0xfc
  174 // Maximum IQMESH network device address
  175 #define MAX_ADDRESS                 ( 240 - 1 )
  176 
  177 // Time slots lengths in 10 ms
  178 #define MIN_STD_TIMESLOT    4
  179 #define MAX_STD_TIMESLOT    6
  180 
  181 #define MIN_LP_TIMESLOT     8
  182 #define MAX_LP_TIMESLOT     10
  183 
  184 // Long diagnostics slot time
  185 #define LONG_DIAG_TIMESLOT  20 
  186 
  187 // Maximum number of DPA PData bytes ( minus 8 = 6B foursome + 8b error code + 8b DpaValue )
  188 #define DPA_MAX_DATA_LENGTH         ( sizeofBufferCOM - sizeof( TDpaIFaceHeader ) - 2 * sizeof( uns8 ) )
  189 
  190 // Maximum number of peripherals info that can fit in the message
  191 #define MAX_PERIPHERALS_PER_BLOCK_INFO  ( DPA_MAX_DATA_LENGTH / sizeof( TPeripheralInfoAnswer ) )
  192 
  193 // Embedded peripheral numbers
  194 #define PNUM_COORDINATOR    0x00
  195 #define PNUM_NODE           0x01
  196 #define PNUM_OS             0x02
  197 #define PNUM_EEPROM         0x03
  198 #define PNUM_EEEPROM        0x04
  199 #define PNUM_RAM            0x05
  200 #define PNUM_LEDR           0x06
  201 #define PNUM_LEDG           0x07
  202 #define PNUM_SPI            0x08
  203 #define PNUM_IO             0x09
  204 #define PNUM_THERMOMETER    0x0A
  205 #define PNUM_UART           0x0C
  206 #define PNUM_FRC            0x0D
  207 
  208 // Number of the 1st user peripheral
  209 #define PNUM_USER           0x20
  210 // Number of the last user peripheral
  211 #define PNUM_USER_MAX       0x3E
  212 // Maximum peripheral number
  213 #define PNUM_MAX            0x7F
  214 
  215 // Fake peripheral number used to flag DPA response with error sent by RF
  216 #define PNUM_ERROR_FLAG     0xFE
  217 // Special peripheral used for enumeration
  218 #define PNUM_ENUMERATION    0xFF
  219 
  220 // DPA Commands for embedded peripherals
  221 #define CMD_COORDINATOR_ADDR_INFO  0
  222 #define CMD_COORDINATOR_DISCOVERED_DEVICES 1
  223 #define CMD_COORDINATOR_BONDED_DEVICES 2
  224 #define CMD_COORDINATOR_CLEAR_ALL_BONDS 3
  225 #define CMD_COORDINATOR_BOND_NODE 4
  226 #define CMD_COORDINATOR_REMOVE_BOND 5
  227 #define CMD_COORDINATOR_DISCOVERY 7
  228 #define CMD_COORDINATOR_SET_DPAPARAMS 8
  229 #define CMD_COORDINATOR_SET_HOPS 9
  230 #define CMD_COORDINATOR_BACKUP 11
  231 #define CMD_COORDINATOR_RESTORE 12
  232 #define CMD_COORDINATOR_AUTHORIZE_BOND 13
  233 #define CMD_COORDINATOR_BRIDGE 14
  234 #define CMD_COORDINATOR_SMART_CONNECT 18
  235 #define CMD_COORDINATOR_SET_MID 19
  236 
  237 #define CMD_NODE_READ 0
  238 #define CMD_NODE_REMOVE_BOND 1
  239 #define CMD_NODE_BACKUP 6
  240 #define CMD_NODE_RESTORE 7
  241 #define CMD_NODE_VALIDATE_BONDS 8
  242 
  243 #define CMD_OS_READ 0
  244 #define CMD_OS_RESET 1
  245 #define CMD_OS_READ_CFG 2
  246 #define CMD_OS_RFPGM 3
  247 #define CMD_OS_SLEEP 4
  248 #define CMD_OS_BATCH 5
  249 #define CMD_OS_SET_SECURITY 6
  250 #define CMD_OS_RESTART 8
  251 #define CMD_OS_WRITE_CFG_BYTE 9
  252 #define CMD_OS_LOAD_CODE 10
  253 #define CMD_OS_SELECTIVE_BATCH 11
  254 #define CMD_OS_TEST_RF_SIGNAL 12
  255 #define CMD_OS_WRITE_CFG 15
  256 
  257 #define CMD_RAM_READ 0
  258 #define CMD_RAM_WRITE 1
  259 #define CMD_RAM_READ_ANY    15
  260 
  261 #define CMD_EEPROM_READ CMD_RAM_READ
  262 #define CMD_EEPROM_WRITE CMD_RAM_WRITE
  263 
  264 #define CMD_EEEPROM_XREAD ( CMD_RAM_READ + 2 )
  265 #define CMD_EEEPROM_XWRITE ( CMD_RAM_WRITE + 2 )
  266 
  267 #define CMD_LED_SET_OFF 0
  268 #define CMD_LED_SET_ON 1
  269 #define CMD_LED_PULSE 3
  270 #define CMD_LED_FLASHING 4
  271 
  272 #define CMD_SPI_WRITE_READ 0
  273 
  274 #define CMD_IO_DIRECTION  0
  275 #define CMD_IO_SET  1
  276 #define CMD_IO_GET  2
  277 
  278 #define CMD_THERMOMETER_READ 0
  279 
  280 #define CMD_UART_OPEN 0
  281 #define CMD_UART_CLOSE 1
  282 #define CMD_UART_WRITE_READ 2
  283 #define CMD_UART_CLEAR_WRITE_READ 3
  284 
  285 #define CMD_FRC_SEND 0
  286 #define CMD_FRC_EXTRARESULT 1
  287 #define CMD_FRC_SEND_SELECTIVE 2
  288 #define CMD_FRC_SET_PARAMS 3
  289 
  290 #define CMD_GET_PER_INFO  0x3f
  291 
  292 // DPA peripheral type
  293 typedef enum
  294 {
  295   PERIPHERAL_TYPE_DUMMY = 0x00,
  296   PERIPHERAL_TYPE_COORDINATOR = 0x01,
  297   PERIPHERAL_TYPE_NODE = 0x02,
  298   PERIPHERAL_TYPE_OS = 0x03,
  299   PERIPHERAL_TYPE_EEPROM = 0x04,
  300   PERIPHERAL_TYPE_BLOCK_EEPROM = 0x05,
  301   PERIPHERAL_TYPE_RAM = 0x06,
  302   PERIPHERAL_TYPE_LED = 0x07,
  303   PERIPHERAL_TYPE_SPI = 0x08,
  304   PERIPHERAL_TYPE_IO = 0x09,
  305   PERIPHERAL_TYPE_UART = 0x0a,
  306   PERIPHERAL_TYPE_THERMOMETER = 0x0b,
  307   PERIPHERAL_TYPE_ADC = 0x0c,
  308   PERIPHERAL_TYPE_PWM = 0x0d,
  309   PERIPHERAL_TYPE_FRC = 0x0e,
  310   // Starts peripheral type number interval for user peripherals
  311   PERIPHERAL_TYPE_USER_AREA = 0x80
  312 } TDpaPeripheralType;
  313 
  314 // Peripheral extended information
  315 typedef enum
  316 {
  317   PERIPHERAL_TYPE_EXTENDED_DEFAULT = 0x00,
  318   PERIPHERAL_TYPE_EXTENDED_READ = 0x01,
  319   PERIPHERAL_TYPE_EXTENDED_WRITE = 0x02,
  320   PERIPHERAL_TYPE_EXTENDED_READ_WRITE = PERIPHERAL_TYPE_EXTENDED_READ | PERIPHERAL_TYPE_EXTENDED_WRITE
  321 } TDpaPeripheralTypeExtended;
  322 
  323 // Response packet error codes
  324 typedef enum
  325 {
  326   // No error
  327   STATUS_NO_ERROR = 0,
  328 
  329   // General fail
  330   ERROR_FAIL = 1,
  331   // Incorrect PCMD
  332   ERROR_PCMD = 2,
  333   // Incorrect PNUM or PCMD
  334   ERROR_PNUM = 3,
  335   // Incorrect Address value when addressing memory type peripherals
  336   ERROR_ADDR = 4,
  337   // Incorrect Data length
  338   ERROR_DATA_LEN = 5,
  339   // Incorrect Data
  340   ERROR_DATA = 6,
  341   // Incorrect HWPID used
  342   ERROR_HWPID = 7,
  343   // Incorrect NADR
  344   ERROR_NADR = 8,
  345   // IFACE data consumed by Custom DPA Handler
  346   ERROR_IFACE_CUSTOM_HANDLER = 9,
  347   // Custom DPA Handler is missing
  348   ERROR_MISSING_CUSTOM_DPA_HANDLER = 10,
  349 
  350   // Beginning of the user code error interval
  351   ERROR_USER_FROM = 0x20,
  352   // End of the user code error interval
  353   ERROR_USER_TO = 0x3f,
  354 
  355   // Bit/flag reserved for a future use
  356   STATUS_RESERVED_FLAG = 0x40,
  357   // Bit to flag asynchronous DPA Response from [N]
  358   STATUS_ASYNC_RESPONSE = 0x80,
  359   // Error code used to mark DPA Confirmation
  360   STATUS_CONFIRMATION = 0xff
  361 } TErrorCodes;
  362 
  363 // Embedded FRC commands
  364 typedef enum
  365 {
  366   // 2 bits
  367   FRC_Ping = 0x00,
  368   FRC_UART_SPI_data = 0x01,
  369   FRC_AcknowledgedBroadcastBits = 0x02,
  370   FRC_PrebondedAlive = 0x03,
  371   FRC_SupplyVoltage = 0x04,
  372   // 1 byte
  373   FRC_Temperature = 0x80,
  374   FRC_AcknowledgedBroadcastBytes = 0x81,
  375   FRC_MemoryRead = 0x82,
  376   FRC_MemoryReadPlus1 = 0x83,
  377   FRC_FrcResponseTime = 0x84,
  378   FRC_TestRFsignal = 0x85,
  379   // 4 bytes
  380   FRC_PrebondedMemoryReadPlus1 = 0xF8,
  381   FRC_MemoryRead4B = 0xFA
  382 } TFRCommands;
  383 
  384 // Intervals of user FRC codes
  385 #define FRC_USER_BIT_FROM     0x40
  386 #define FRC_USER_BIT_TO       0x7F
  387 #define FRC_USER_BYTE_FROM    0xC0
  388 #define FRC_USER_BYTE_TO      0xDF
  389 #define FRC_USER_2BYTE_FROM   0xF0
  390 #define FRC_USER_2BYTE_TO     0xF7
  391 #define FRC_USER_4BYTE_FROM   0xFC
  392 #define FRC_USER_4BYTE_TO     0xFF
  393 
  394 // No HWPID specified
  395 #define HWPID_Default         0x0000
  396 // Use this type to override HWPID check
  397 #define HWPID_DoNotCheck      0xFfFf
  398 
  399 // RAM peripheral block definitions
  400 #define PERIPHERAL_RAM_LENGTH       48
  401 
  402 // Start address of EEPROM peripheral in the real EEPROM
  403 #ifndef COORDINATOR_CUSTOM_HANDLER // Node
  404 #define PERIPHERAL_EEPROM_START     ( (uns8)0x00 )
  405 #else // Coordinator
  406 #define PERIPHERAL_EEPROM_START     ( (uns8)0x80 )
  407 #endif
  408 
  409 // Length of the real serial EEEPROM from the EEEPROM DPA peripheral write point of view
  410 #define EEEPROM_REAL_LENGTH                 0x4000
  411 
  412 // Starting address of the Autoexec DPA storage at external EEPROM
  413 #define AUTOEXEC_EEEPROM_ADDR               0x0000
  414 // Length of the autoexec memory block
  415 #define AUTOEXEC_LENGTH                     sizeofBufferAUX
  416 
  417 // Starting address of the IO Setup DPA storage at external EEPROM
  418 #define IOSETUP_EEEPROM_ADDR                ( AUTOEXEC_EEEPROM_ADDR + AUTOEXEC_LENGTH )
  419 // Length of the IO setup memory block
  420 #define IOSETUP_LENGTH                      sizeofBufferAUX
  421 
  422 // ---------------------------------------------------------
  423 
  424 // Enumerate peripherals structure
  425 typedef struct
  426 {
  427   uns16 DpaVersion;
  428   uns8  UserPerNr;
  429   uns8  EmbeddedPers[PNUM_USER / 8];
  430   uns16 HWPID;
  431   uns16 HWPIDver;
  432   uns8  Flags;
  433   uns8  UserPer[( PNUM_MAX - PNUM_USER + 1 + 7 ) / 8];
  434 } STRUCTATTR TEnumPeripheralsAnswer;
  435 
  436 #define FlagUserPer(UserPersArray,UserPerNumber)    UserPersArray[((UserPerNumber)-PNUM_USER) / 8] |= (uns8)0x01 << (((UserPerNumber)-PNUM_USER) % 8);
  437 
  438 // Get peripheral info structure (CMD_GET_PER_INFO)
  439 typedef struct
  440 {
  441   uns8  PerTE;
  442   uns8  PerT;
  443   uns8  Par1;
  444   uns8  Par2;
  445 } STRUCTATTR TPeripheralInfoAnswer;
  446 
  447 // Error DPA response (PNUM_ERROR_FLAG)
  448 typedef struct
  449 {
  450   uns8  ErrN;
  451   uns8  PNUMoriginal;
  452 } STRUCTATTR TErrorAnswer;
  453 
  454 // Structure returned by CMD_COORDINATOR_ADDR_INFO
  455 typedef struct
  456 {
  457   uns8  DevNr;
  458   uns8  DID;
  459 } STRUCTATTR TPerCoordinatorAddrInfo_Response;
  460 
  461 // Structure for CMD_COORDINATOR_BOND_NODE
  462 typedef struct
  463 {
  464   uns8  ReqAddr;
  465   uns8  BondingTestRetries;
  466 } STRUCTATTR TPerCoordinatorBondNode_Request;
  467 
  468 // Structure returned by CMD_COORDINATOR_BOND_NODE or CMD_COORDINATOR_SMART_CONNECT
  469 typedef struct
  470 {
  471   uns8  BondAddr;
  472   uns8  DevNr;
  473 } STRUCTATTR TPerCoordinatorBondNodeSmartConnect_Response;
  474 
  475 // Structure for CMD_COORDINATOR_REMOVE_BOND
  476 typedef struct
  477 {
  478   uns8  BondAddr;
  479 } STRUCTATTR TPerCoordinatorRemoveBond_Request;
  480 
  481 // Structure returned by CMD_COORDINATOR_REMOVE_BOND
  482 typedef struct
  483 {
  484   uns8  DevNr;
  485 } STRUCTATTR TPerCoordinatorRemoveBond_Response;
  486 
  487 // Structure for CMD_COORDINATOR_DISCOVERY
  488 typedef struct
  489 {
  490   uns8  TxPower;
  491   uns8  MaxAddr;
  492 } STRUCTATTR TPerCoordinatorDiscovery_Request;
  493 
  494 // Structure returned by CMD_COORDINATOR_DISCOVERY
  495 typedef struct
  496 {
  497   uns8  DiscNr;
  498 } STRUCTATTR TPerCoordinatorDiscovery_Response;
  499 
  500 // Structure for and also returned by CMD_COORDINATOR_SET_DPAPARAMS
  501 typedef struct
  502 {
  503   uns8  DpaParam;
  504 } STRUCTATTR TPerCoordinatorSetDpaParams_Request_Response;
  505 
  506 // Structure for and also returned by CMD_COORDINATOR_SET_HOPS
  507 typedef struct
  508 {
  509   uns8  RequestHops;
  510   uns8  ResponseHops;
  511 } STRUCTATTR TPerCoordinatorSetHops_Request_Response;
  512 
  513 // Structure for CMD_COORDINATOR_BACKUP and CMD_NODE_BACKUP
  514 typedef struct
  515 {
  516   uns8  Index;
  517 } STRUCTATTR TPerCoordinatorNodeBackup_Request;
  518 
  519 // Structure returned by CMD_COORDINATOR_BACKUP and CMD_NODE_BACKUP
  520 typedef struct
  521 {
  522   uns8  NetworkData[49];
  523 } STRUCTATTR TPerCoordinatorNodeBackup_Response;
  524 
  525 // Structure for CMD_COORDINATOR_RESTORE and CMD_NODE_RESTORE
  526 typedef struct
  527 {
  528   uns8  NetworkData[49];
  529 } STRUCTATTR TPerCoordinatorNodeRestore_Request;
  530 
  531 // Structure for CMD_COORDINATOR_AUTHORIZE_BOND
  532 typedef struct
  533 {
  534   uns8  ReqAddr;
  535   uns8  MID[4];
  536 } STRUCTATTR TPerCoordinatorAuthorizeBond_Request;
  537 
  538 // Structure returned by CMD_COORDINATOR_AUTHORIZE_BOND
  539 typedef struct
  540 {
  541   uns8  BondAddr;
  542   uns8  DevNr;
  543 } STRUCTATTR TPerCoordinatorAuthorizeBond_Response;
  544 
  545 // Structure for CMD_COORDINATOR_BRIDGE
  546 typedef struct
  547 {
  548   TDpaIFaceHeader subHeader;
  549   uns8  subPData[DPA_MAX_DATA_LENGTH - sizeof( TDpaIFaceHeader )];
  550 } STRUCTATTR TPerCoordinatorBridge_Request;
  551 
  552 // Structure returned by CMD_COORDINATOR_BRIDGE
  553 typedef struct
  554 {
  555   TDpaIFaceHeader subHeader;
  556   uns8  subRespCode;
  557   uns8  subDpaValue;
  558   uns8  subPData[DPA_MAX_DATA_LENGTH - sizeof( TDpaIFaceHeader ) - 2 * sizeof( uns8 )];
  559 } STRUCTATTR TPerCoordinatorBridge_Response;
  560 
  561 // Structure for CMD_COORDINATOR_SMART_CONNECT
  562 typedef struct
  563 {
  564   uns8  ReqAddr;
  565   uns8  BondingTestRetries;
  566   uns8  IBK[16];
  567   uns8  MID[4];
  568   uns8  reserved0[2];
  569   uns8  VirtualDeviceAddress;
  570   uns8  reserved1[9];
  571   uns8  UserData[4];
  572 } STRUCTATTR TPerCoordinatorSmartConnect_Request;
  573 
  574 // Structure for CMD_COORDINATOR_SET_MID
  575 typedef struct
  576 {
  577   uns8  MID[4];
  578   uns8  BondAddr;
  579 } STRUCTATTR TPerCoordinatorSetMID_Request;
  580 
  581 // Structure returned by CMD_NODE_READ
  582 typedef struct
  583 {
  584   uns8  ntwADDR;
  585   uns8  ntwVRN;
  586   uns8  ntwZIN;
  587   uns8  ntwDID;
  588   uns8  ntwPVRN;
  589   uns16 ntwUSERADDRESS;
  590   uns16 ntwID;
  591   uns8  ntwVRNFNZ;
  592   uns8  ntwCFG;
  593   uns8  Flags;
  594 } STRUCTATTR TPerNodeRead_Response;
  595 
  596 // Structures for CMD_NODE_VALIDATE_BONDS
  597 typedef struct
  598 {
  599   uns8  Address;
  600   uns8  MID[4];
  601 } STRUCTATTR TPerNodeValidateBondsItem;
  602 
  603 // Structure for CMD_NODE_VALIDATE_BONDS
  604 typedef struct
  605 {
  606   TPerNodeValidateBondsItem Bonds[DPA_MAX_DATA_LENGTH / sizeof( TPerNodeValidateBondsItem )];
  607 } STRUCTATTR TPerNodeValidateBonds_Request;
  608 
  609 // Structure returned by CMD_OS_READ
  610 typedef struct
  611 {
  612   uns8  MID[4];
  613   uns8  OsVersion;
  614   uns8  McuType;
  615   uns16 OsBuild;
  616   uns8  Rssi;
  617   uns8  SupplyVoltage;
  618   uns8  Flags;
  619   uns8  SlotLimits;
  620   uns8  IBK[16];
  621 } STRUCTATTR TPerOSRead_Response;
  622 
  623 // Structure returned by CMD_OS_READ_CFG
  624 typedef struct
  625 {
  626   uns8  Checksum;
  627   uns8  Configuration[31];
  628   uns8  RFPGM;
  629   uns8  Undocumented[1];
  630 } STRUCTATTR TPerOSReadCfg_Response;
  631 
  632 // Structure for CMD_OS_WRITE_CFG
  633 typedef struct
  634 {
  635   uns8  Undefined;
  636   uns8  Configuration[31];
  637   uns8  RFPGM;
  638 } STRUCTATTR TPerOSWriteCfg_Request;
  639 
  640 // Structures for CMD_OS_WRITE_CFG_BYTE
  641 typedef struct
  642 {
  643   uns8  Address;
  644   uns8  Value;
  645   uns8  Mask;
  646 } STRUCTATTR TPerOSWriteCfgByteTriplet;
  647 
  648 // Structure for CMD_OS_WRITE_CFG_BYTE
  649 typedef struct
  650 {
  651   TPerOSWriteCfgByteTriplet Triplets[DPA_MAX_DATA_LENGTH / sizeof( TPerOSWriteCfgByteTriplet )];
  652 } STRUCTATTR TPerOSWriteCfgByte_Request;
  653 
  654 // Structure for CMD_OS_SET_SECURITY
  655 typedef struct
  656 {
  657   uns8  Type;
  658   uns8  Data[16];
  659 } STRUCTATTR TPerOSSetSecurity_Request;
  660 
  661 // Structure for CMD_OS_LOAD_CODE
  662 typedef struct
  663 {
  664   uns8  Flags;
  665   uns16 Address;
  666   uns16 Length;
  667   uns16 CheckSum;
  668 } STRUCTATTR TPerOSLoadCode_Request;
  669 
  670 // Structure for CMD_OS_SLEEP
  671 typedef struct
  672 {
  673   uns16 Time;
  674   uns8  Control;
  675 } STRUCTATTR TPerOSSleep_Request;
  676 
  677 // Structure for CMD_OS_SELECTIVE_BATCH
  678 typedef struct
  679 {
  680   uns8  SelectedNodes[30];
  681   uns8  Requests[DPA_MAX_DATA_LENGTH - 30];
  682 } STRUCTATTR TPerOSSelectiveBatch_Request;
  683 
  684 // Structure for CMD_OS_TEST_RF_SIGNAL request
  685 typedef struct
  686 {
  687   uns8  Channel;
  688   uns8  RXfilter;
  689   uns16 Time;
  690 } STRUCTATTR TPerOSTestRfSignal_Request;
  691 
  692 // Structure for CMD_OS_TEST_RF_SIGNAL response
  693 typedef struct
  694 {
  695   uns8  Counter;
  696 } STRUCTATTR TPerOSTestRfSignal_Response;
  697 
  698 // Structure for general memory request
  699 typedef struct
  700 {
  701   // Address of data to write or read
  702   uns8  Address;
  703 
  704   union
  705   {
  706     // Memory read request
  707     struct
  708     {
  709       // Length of data to read
  710       uns8  Length;
  711     } Read;
  712 
  713     // Size of Address field
  714 #define MEMORY_WRITE_REQUEST_OVERHEAD   ( sizeof( uns8 ) )
  715 
  716     // Memory write request
  717     struct
  718     {
  719       uns8  PData[DPA_MAX_DATA_LENGTH - MEMORY_WRITE_REQUEST_OVERHEAD];
  720     } Write;
  721 
  722   } ReadWrite;
  723 } STRUCTATTR TPerMemoryRequest;
  724 
  725 // Structure for general extended memory request
  726 typedef struct
  727 {
  728   // Address of data to write or read
  729   uns16 Address;
  730 
  731   union
  732   {
  733     // Memory read request
  734     struct
  735     {
  736       // Length of data to read
  737       uns8  Length;
  738     } Read;
  739 
  740     // Size of Address field
  741 #define XMEMORY_WRITE_REQUEST_OVERHEAD  ( sizeof( uns16 ) )
  742 
  743     // Memory write request
  744     struct
  745     {
  746       uns8  PData[DPA_MAX_DATA_LENGTH - XMEMORY_WRITE_REQUEST_OVERHEAD];
  747     } Write;
  748 
  749   } ReadWrite;
  750 } STRUCTATTR TPerXMemoryRequest;
  751 
  752 // Structure for CMD_IO requests
  753 typedef struct
  754 {
  755   uns8  Port;
  756   uns8  Mask;
  757   uns8  Value;
  758 } STRUCTATTR TPerIOTriplet;
  759 
  760 typedef struct
  761 {
  762   uns8  Header; // == PNUM_IO_DELAY
  763   uns16 Delay;
  764 } STRUCTATTR TPerIODelay;
  765 
  766 // Union for CMD_IO_SET and CMD_IO_DIRECTION requests
  767 typedef union
  768 {
  769   TPerIOTriplet Triplets[DPA_MAX_DATA_LENGTH / sizeof( TPerIOTriplet )];
  770   TPerIODelay   Delays[DPA_MAX_DATA_LENGTH / sizeof( TPerIODelay )];
  771 } STRUCTATTR TPerIoDirectionAndSet_Request;
  772 
  773 // Structure returned by CMD_THERMOMETER_READ
  774 typedef struct
  775 {
  776   int8  IntegerValue;
  777   int16 SixteenthValue;
  778 } STRUCTATTR TPerThermometerRead_Response;
  779 
  780 // Structure for CMD_UART_OPEN
  781 typedef struct
  782 {
  783   uns8  BaudRate;
  784 } STRUCTATTR TPerUartOpen_Request;
  785 
  786 // Structure for CMD_UART_[CLEAR_]WRITE_READ and CMD_SPI_WRITE_READ
  787 typedef struct
  788 {
  789   uns8  ReadTimeout;
  790   uns8  WrittenData[DPA_MAX_DATA_LENGTH - sizeof( uns8 )];
  791 } STRUCTATTR TPerUartSpiWriteRead_Request;
  792 
  793 // Structure for CMD_FRC_SEND
  794 typedef struct
  795 {
  796   uns8  FrcCommand;
  797   uns8  UserData[30];
  798 } STRUCTATTR TPerFrcSend_Request;
  799 
  800 // Structure for CMD_FRC_SEND_SELECTIVE
  801 typedef struct
  802 {
  803   uns8  FrcCommand;
  804   uns8  SelectedNodes[30];
  805   uns8  UserData[25];
  806 } STRUCTATTR TPerFrcSendSelective_Request;
  807 
  808 // Structure returned by CMD_FRC_SEND and CMD_FRC_SEND_SELECTIVE
  809 typedef struct
  810 {
  811   uns8  Status;
  812   uns8  FrcData[DPA_MAX_DATA_LENGTH - sizeof( uns8 )];
  813 } STRUCTATTR TPerFrcSend_Response;
  814 
  815 // Structure for request and response of CMD_FRC_SET_PARAMS
  816 typedef struct
  817 {
  818   uns8  FRCresponseTime;
  819 } STRUCTATTR TPerFrcSetParams_RequestResponse;
  820 
  821 // Interface and CMD_COORDINATOR_BRIDGE confirmation structure
  822 typedef struct
  823 {
  824   // Number of hops
  825   uns8  Hops;
  826   // Time slot length in 10ms
  827   uns8  TimeSlotLength;
  828   // Number of hops for response
  829   uns8  HopsResponse;
  830 } STRUCTATTR TIFaceConfirmation;
  831 
  832 // ---------------------------------------------------------
  833 
  834 // DPA Message data structure (packet w/o NADR, PNUM, PCMD, HWPID)
  835 typedef union
  836 {
  837   // General DPA request
  838   struct
  839   {
  840     uns8    PData[DPA_MAX_DATA_LENGTH];
  841   } Request;
  842 
  843   // General DPA response
  844   struct
  845   {
  846     uns8    PData[DPA_MAX_DATA_LENGTH];
  847   } Response;
  848 
  849   // Enumerate peripherals structure
  850   TEnumPeripheralsAnswer EnumPeripheralsAnswer;
  851 
  852   // Get peripheral info structure (CMD_GET_PER_INFO)
  853   TPeripheralInfoAnswer PeripheralInfoAnswer;
  854 
  855   // Get peripheral info structure (CMD_GET_PER_INFO) for more peripherals
  856   TPeripheralInfoAnswer PeripheralInfoAnswers[MAX_PERIPHERALS_PER_BLOCK_INFO];
  857 
  858   // Error DPA response (PNUM_ERROR_FLAG)
  859   TErrorAnswer ErrorAnswer;
  860 
  861   // Structure returned by CMD_COORDINATOR_ADDR_INFO
  862   TPerCoordinatorAddrInfo_Response PerCoordinatorAddrInfo_Response;
  863 
  864   // Structure for CMD_COORDINATOR_BOND_NODE
  865   TPerCoordinatorBondNode_Request PerCoordinatorBondNode_Request;
  866 
  867   // Structure returned by CMD_COORDINATOR_BOND_NODE or CMD_COORDINATOR_SMART_CONNECT
  868   TPerCoordinatorBondNodeSmartConnect_Response PerCoordinatorBondNodeSmartConnect_Response;
  869 
  870   // Structure for CMD_COORDINATOR_REMOVE_BOND
  871   TPerCoordinatorRemoveBond_Request PerCoordinatorRemoveBond_Request;
  872 
  873   // Structure returned by CMD_COORDINATOR_REMOVE_BOND 
  874   TPerCoordinatorRemoveBond_Response PerCoordinatorRemoveBond_Response;
  875 
  876   // Structure for CMD_COORDINATOR_DISCOVERY
  877   TPerCoordinatorDiscovery_Request PerCoordinatorDiscovery_Request;
  878 
  879   // Structure returned by CMD_COORDINATOR_DISCOVERY
  880   TPerCoordinatorDiscovery_Response PerCoordinatorDiscovery_Response;
  881 
  882   // Structure for and also returned by CMD_COORDINATOR_SET_DPAPARAMS
  883   TPerCoordinatorSetDpaParams_Request_Response PerCoordinatorSetDpaParams_Request_Response;
  884 
  885   // Structure for and also returned by CMD_COORDINATOR_SET_HOPS
  886   TPerCoordinatorSetHops_Request_Response PerCoordinatorSetHops_Request_Response;
  887 
  888   // Structure for CMD_COORDINATOR_BACKUP and CMD_NODE_BACKUP
  889   TPerCoordinatorNodeBackup_Request PerCoordinatorNodeBackup_Request;
  890 
  891   // Structure returned by CMD_COORDINATOR_BACKUP and CMD_NODE_BACKUP
  892   TPerCoordinatorNodeBackup_Response PerCoordinatorNodeBackup_Response;
  893 
  894   // Structure for CMD_COORDINATOR_RESTORE and CMD_NODE_RESTORE
  895   TPerCoordinatorNodeRestore_Request PerCoordinatorNodeRestore_Request;
  896 
  897   // Structure for CMD_COORDINATOR_AUTHORIZE_BOND
  898   TPerCoordinatorAuthorizeBond_Request PerCoordinatorAuthorizeBond_Request;
  899 
  900   // Structure returned by CMD_COORDINATOR_AUTHORIZE_BOND
  901   TPerCoordinatorAuthorizeBond_Response PerCoordinatorAuthorizeBond_Response;
  902 
  903   // Structure for CMD_COORDINATOR_BRIDGE
  904   TPerCoordinatorBridge_Request PerCoordinatorBridge_Request;
  905 
  906   // Structure returned by CMD_COORDINATOR_BRIDGE
  907   TPerCoordinatorBridge_Response PerCoordinatorBridge_Response;
  908 
  909   // Structure for CMD_COORDINATOR_SMART_CONNECT
  910   TPerCoordinatorSmartConnect_Request PerCoordinatorSmartConnect_Request;
  911 
  912   // Structure for CMD_COORDINATOR_SET_MID
  913   TPerCoordinatorSetMID_Request PerCoordinatorSetMID_Request;
  914 
  915   // Structure returned by CMD_NODE_READ
  916   TPerNodeRead_Response PerNodeRead_Response;
  917 
  918   // Structure for CMD_NODE_VALIDATE_BONDS
  919   TPerNodeValidateBonds_Request PerNodeValidateBonds_Request;
  920 
  921   // Structure returned by CMD_OS_READ
  922   TPerOSRead_Response PerOSRead_Response;
  923 
  924   // Structure returned by CMD_OS_READ_CFG
  925   TPerOSReadCfg_Response PerOSReadCfg_Response;
  926 
  927   // Structure for CMD_OS_WRITE_CFG
  928   TPerOSWriteCfg_Request PerOSWriteCfg_Request;
  929 
  930   // Structure for CMD_OS_WRITE_CFG_BYTE
  931   TPerOSWriteCfgByte_Request PerOSWriteCfgByte_Request;
  932 
  933   // Structure for CMD_OS_SET_SECURITY
  934   TPerOSSetSecurity_Request PerOSSetSecurity_Request;
  935 
  936   // Structure for CMD_OS_LOAD_CODE
  937   TPerOSLoadCode_Request PerOSLoadCode_Request;
  938 
  939   // Structure for CMD_OS_SLEEP
  940   TPerOSSleep_Request PerOSSleep_Request;
  941 
  942   // Structure for CMD_OS_SELECTIVE_BATCH
  943   TPerOSSelectiveBatch_Request PerOSSelectiveBatch_Request;
  944 
  945   // Structure for CMD_OS_TEST_RF_SIGNAL request
  946   TPerOSTestRfSignal_Request PerOSTestRfSignal_Request;
  947 
  948   // Structure for CMD_OS_TEST_RF_SIGNAL response
  949   TPerOSTestRfSignal_Response PerOSTestRfSignal_Response;
  950 
  951   // Structure for general memory request
  952   TPerMemoryRequest MemoryRequest;
  953 
  954   // Structure for general extended memory request
  955   TPerXMemoryRequest XMemoryRequest;
  956 
  957   // Structure for CMD_IO requests
  958   TPerIoDirectionAndSet_Request PerIoDirectionAndSet_Request;
  959 
  960   // Structure returned by CMD_THERMOMETER_READ
  961   TPerThermometerRead_Response PerThermometerRead_Response;
  962 
  963   // Structure for CMD_UART_OPEN
  964   TPerUartOpen_Request PerUartOpen_Request;
  965 
  966   // Structure for CMD_UART_[CLEAR_]WRITE_READ and CMD_SPI_WRITE_READ
  967   TPerUartSpiWriteRead_Request PerUartSpiWriteRead_Request;
  968 
  969   // Structure for CMD_FRC_SEND
  970   TPerFrcSend_Request PerFrcSend_Request;
  971 
  972   // Structure returned by CMD_FRC_SEND and CMD_FRC_SEND_SELECTIVE
  973   TPerFrcSend_Response PerFrcSend_Response;
  974 
  975   // Structure for CMD_FRC_SEND_SELECTIVE
  976   TPerFrcSendSelective_Request PerFrcSendSelective_Request;
  977 
  978   // Structure for request and response of CMD_FRC_SET_PARAMS
  979   TPerFrcSetParams_RequestResponse PerFrcSetParams_RequestResponse;
  980 
  981   // Interface and CMD_COORDINATOR_BRIDGE confirmation structure
  982   TIFaceConfirmation IFaceConfirmation;
  983 } TDpaMessage;
  984 
  985 // Custom DPA Handler events
  986 #define DpaEvent_DpaRequest               0
  987 #define DpaEvent_Interrupt                1
  988 #define DpaEvent_Idle                     2
  989 #define DpaEvent_Init                     3
  990 #define DpaEvent_Notification             4
  991 #define DpaEvent_AfterRouting             5
  992 #define DpaEvent_BeforeSleep              6
  993 #define DpaEvent_AfterSleep               7
  994 #define DpaEvent_Reset                    8
  995 #define DpaEvent_DisableInterrupts        9
  996 #define DpaEvent_FrcValue                 10
  997 #define DpaEvent_ReceiveDpaResponse       11
  998 #define DpaEvent_IFaceReceive             12
  999 #define DpaEvent_ReceiveDpaRequest        13
 1000 #define DpaEvent_BeforeSendingDpaResponse 14
 1001 #define DpaEvent_PeerToPeer               15
 1002 #define DpaEvent_UserDpaValue             17
 1003 #define DpaEvent_FrcResponseTime          18
 1004 #define DpaEvent_BondingButton            19
 1005 
 1006 #define DpaEvent_LAST                     DpaEvent_BondingButton
 1007 
 1008 // Types of the diagnostic DPA Value that is returned inside DPA response 
 1009 typedef enum
 1010 {
 1011   DpaValueType_RSSI = 0,
 1012   DpaValueType_SupplyVoltage = 1,
 1013   DpaValueType_System = 2,
 1014   DpaValueType_User = 3
 1015 } TDpaValueType;
 1016 
 1017 // Type (color) of LED peripheral
 1018 typedef enum
 1019 {
 1020   LED_COLOR_RED = 0,
 1021   LED_COLOR_GREEN = 1,
 1022   LED_COLOR_BLUE = 2,
 1023   LED_COLOR_YELLOW = 3,
 1024   LED_COLOR_WHITE = 4,
 1025   LED_COLOR_UNKNOWN = 0xff
 1026 } TLedColor;
 1027 
 1028 // Baud rates
 1029 typedef enum
 1030 {
 1031   DpaBaud_1200 = 0x00,
 1032   DpaBaud_2400 = 0x01,
 1033   DpaBaud_4800 = 0x02,
 1034   DpaBaud_9600 = 0x03,
 1035   DpaBaud_19200 = 0x04,
 1036   DpaBaud_38400 = 0x05,
 1037   DpaBaud_57600 = 0x06,
 1038   DpaBaud_115200 = 0x07,
 1039   DpaBaud_230400 = 0x08
 1040 } TBaudRates;
 1041 
 1042 // Useful PNUM_IO definitions
 1043 typedef enum
 1044 {
 1045   PNUM_IO_PORTA = 0x00,
 1046   PNUM_IO_TRISA = 0x00,
 1047   PNUM_IO_PORTB = 0x01,
 1048   PNUM_IO_TRISB = 0x01,
 1049   PNUM_IO_PORTC = 0x02,
 1050   PNUM_IO_TRISC = 0x02,
 1051   PNUM_IO_PORTE = 0x04,
 1052   PNUM_IO_TRISE = 0x04,
 1053   PNUM_IO_WPUB = 0x11,
 1054   PNUM_IO_DELAY = 0xff
 1055 } PNUM_IO_Definitions;
 1056 
 1057 // To test for enumeration peripherals request
 1058 #define IsDpaEnumPeripheralsRequestNoSize() ( _PNUM == PNUM_ENUMERATION && _PCMD == CMD_GET_PER_INFO )
 1059 
 1060 #if PARAM_CHECK_LEVEL >= 2
 1061 #define IsDpaEnumPeripheralsRequest() ( IsDpaEnumPeripheralsRequestNoSize() && _DpaDataLength == 0 )
 1062 #else
 1063 #define IsDpaEnumPeripheralsRequest() IsDpaEnumPeripheralsRequestNoSize()
 1064 #endif
 1065 
 1066 // To test for peripherals information request
 1067 #define IsDpaPeripheralInfoRequestNoSize()  ( _PNUM != PNUM_ENUMERATION && _PCMD == CMD_GET_PER_INFO )
 1068 
 1069 #if PARAM_CHECK_LEVEL >= 2
 1070 #define IsDpaPeripheralInfoRequest()  ( IsDpaPeripheralInfoRequestNoSize() && _DpaDataLength == 0 )
 1071 #else
 1072 #define IsDpaPeripheralInfoRequest()  IsDpaPeripheralInfoRequestNoSize()
 1073 #endif
 1074 
 1075 // Optimized macro for both testing enumeration peripherals ELSE peripherals information. See examples
 1076 #define IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequestNoSize() if ( _PCMD == CMD_GET_PER_INFO ) if ( _PNUM == PNUM_ENUMERATION )
 1077 
 1078 #if PARAM_CHECK_LEVEL >= 2
 1079 #define IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest() if ( _DpaDataLength == 0 && _PCMD == CMD_GET_PER_INFO ) if ( _PNUM == PNUM_ENUMERATION )
 1080 #else
 1081 #define IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest() IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequestNoSize()
 1082 #endif
 1083 
 1084 #ifdef __CC5X__
 1085 
 1086 // DPA message at bufferRF
 1087 TDpaMessage DpaRfMessage @bufferRF;
 1088 
 1089 // Actual allocation of the RAM Peripheral memory block @ UserBank_02
 1090 bank12 uns8  PeripheralRam[PERIPHERAL_RAM_LENGTH] @ 0x620;
 1091 
 1092 // Actual DPA message parameters at memory
 1093 #define _NADR           RX
 1094 #define _NADRhigh       RTAUX
 1095 #define _PNUM           PNUM
 1096 #define _PCMD           PCMD
 1097 #define _DpaDataLength  DLEN
 1098 #define _DpaMessage     DpaRfMessage
 1099 
 1100 // Return actual DPA user routine event
 1101 #define GetDpaEvent()   userReg0
 1102 
 1103 // Stores DPA Params inside DPA request/response
 1104 #define _DpaParams                    PPAR
 1105 // Get DPA Value type out of the DPA Params
 1106 #define DpaValueType()                ( _DpaParams & 0b11 )
 1107 
 1108 // Traffic indication active: from the store in case of DPA request
 1109 bit IsDpaTrafficIndication            @_DpaParams.2;
 1110 // Long diagnostic time slot request: from the store in case of DPA request
 1111 bit IsDpaLongTimeslot                 @_DpaParams.3;
 1112 
 1113 // Include assembler definitions
 1114 #include "HexCodes.h"
 1115 
 1116 // Next code must start at the IQRF APPLICATION routine entry point
 1117 #pragma origin __APPLICATION_ADDRESS
 1118 
 1119 #endif  // __CC5X__
 1120 #endif  // _DPA_HEADER_
 1121 //############################################################################################