1 // *********************************************************************
    2 //   Main Custom DPA Handler header                                    *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: DPAcustomHandler.h,v $
    7 // Version: $Revision: 1.75 $
    8 // Date:    $Date: 2017/08/10 12:05:49 $
    9 //
   10 // Revision history:
   11 //   2017/08/14  Release for DPA 3.01
   12 //   2017/03/13  Release for DPA 3.00
   13 //   2016/09/12  Release for DPA 2.28
   14 //   2016/04/14  Release for DPA 2.27
   15 //   2016/03/03  Release for DPA 2.26
   16 //   2016/01/21  Release for DPA 2.25
   17 //   2015/12/01  Release for DPA 2.24
   18 //   2015/10/23  Release for DPA 2.23
   19 //   2015/09/25  Release for DPA 2.22
   20 //   2015/09/03  Release for DPA 2.21
   21 //   2015/08/05  Release for DPA 2.20
   22 //   2014/10/31  Release for DPA 2.10
   23 //   2014/04/30  Release for DPA 2.00
   24 //   2013/10/03  Release for DPA 1.00
   25 //
   26 // *********************************************************************
   27 
   28 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   29 
   30 #ifndef _CUSTOM_DPA_HANDLER_
   31 #define _CUSTOM_DPA_HANDLER_
   32 
   33 //############################################################################################
   34 // 1st include 
   35 
   36 // Custom DPA Handler routine declaration
   37 bit CustomDpaHandler();
   38 
   39 // Various DPA flags shared between DPA and Custom DPA Handler
   40 uns8 DpaFlags @ usedBank4[0];
   41 // TRUE if remote bonding is enabled
   42 bit ProvidesRemoteBonding @ DpaFlags.0;
   43 #ifdef COORDINATOR_CUSTOM_HANDLER
   44 // [C] TRUE if interface master is not connected (detected)
   45 bit IFaceMasterNotConnected @ DpaFlags.2;
   46 #endif
   47 // [N] [CN] DPA by interface notification is sent also when there was a "reading" like DPA request
   48 bit EnableIFaceNotificationOnRead @ DpaFlags.3;
   49 // [N] [NC] TRUE when node was just bonded using default bonding procedure
   50 bit NodeWasBonded @ DpaFlags.4;
   51 
   52 // [C] Ticks (decrementing) counter usable for timing in the coordinator's Customer DPA Handler
   53 uns16 DpaTicks @ usedBank4[1];
   54 // [N] [NC] toutRF for LP mode, read from configuration memory after reset
   55 uns8 LPtoutRF @ usedBank4[3];
   56 // DPA Request/Response HWPID
   57 uns16 _HWPID @ usedBank4[4];
   58 // Identifies type of reset (stored at UserReg0 upon module reset). See Reset chapter at IQRF User's Guide for more information
   59 uns8 ResetType @ usedBank4[6];
   60 // User DPA Values to return
   61 uns8 UserDpaValue @ usedBank4[7];
   62 // Network depth of the DPA request/response, increases on bridging, decreases on back-bridging
   63 uns8 NetDepth @ usedBank4[8];
   64 // TRUE when node was at DPA Service Mode after last boot
   65 bit DSMactivated @ usedBank4[9].0;
   66 // If set to TRUE, then LP RX mode in the main loop can be terminated by pin, see _RLPMAT
   67 bit LpRxPinTerminate @ usedBank4[9].1;
   68 // Count of remotely bonded nodes
   69 uns8 RemoteBondingCount @ usedBank4[10];
   70 
   71 // Macro to return an error from the peripheral handler. If the code size is not an issue this macro is the right choice.
   72 #define DpaApiReturnPeripheralError(error) do { \
   73     DpaApiSetPeripheralError( error ); \
   74     return Carry; \
   75   } while( 0 )
   76 
   77 // DPA API functions, see documentation for details
   78 #define DpaApiRfTxDpaPacket( dpaValue, netDepthAndFlags ) DpaApiEntry( dpaValue, netDepthAndFlags, DPA_API_RFTX_DPAPACKET )
   79 #define DpaApiReadConfigByte( index )                     DpaApiEntry( index, param3.low8, DPA_API_READ_CONFIG_BYTE )
   80 #define DpaApiLocalRequest()                              DpaApiEntry( param2, param3.low8, DPA_API_LOCAL_REQUEST )
   81 #define DpaApiSetPeripheralError( error )                 DpaApiEntry( error, param3.low8, DPA_API_SET_PERIPHERAL_ERROR )
   82 #define DpaApiSendToIFaceMaster( dpaValue, flags )        DpaApiEntry( dpaValue, flags, DPA_API_SEND_TO_IFACEMASTER )
   83 #define DpaApiSetRfDefaults()                             DpaApiEntry( param2, param3.low8, DPA_API_SET_RF_DEFAULTS )
   84 
   85 #ifdef COORDINATOR_CUSTOM_HANDLER
   86 #define DpaApiRfTxDpaPacketCoordinator()                  DpaApiEntry( param2, param3.low8, DPA_API_COORDINATOR_RFTX_DPAPACKET )
   87 #endif
   88 
   89 #ifdef COORDINATOR_CUSTOM_HANDLER
   90 #undef  DpaEvent_Interrupt
   91 #undef  DpaEvent_BeforeSleep
   92 #undef  DpaEvent_AfterSleep
   93 #undef  DpaEvent_FrcValue
   94 #undef  DpaEvent_FrcResponseTime
   95 #else
   96 #undef  DpaEvent_ReceiveDpaResponse
   97 #undef  DpaEvent_IFaceReceive
   98 #endif
   99 
  100 // To detect overlapping code in case someone would put some code before this header by mistake
  101 #pragma origin __APPLICATION_ADDRESS
  102 #pragma updateBank 0
  103 
  104 //############################################################################################
  105 // Main IQRF entry point jumps to the main DPA entry point
  106 void APPLICATION()
  107 //############################################################################################
  108 {
  109   #asm
  110     DW  __MOVLP( MAIN_DPA_ADDRESS >> 8 );
  111   DW  __GOTO( MAIN_DPA_ADDRESS );
  112   #endasm
  113 
  114 #ifndef NO_CUSTOM_DPA_HANDLER
  115     // Fake call to force CustomDpaHandler() compilation
  116     CustomDpaHandler();
  117 #endif
  118 
  119   // Fake call to force DpaApiEntry() compilation
  120   DpaApiEntry( param2, param3.low8, W );
  121 }
  122 
  123 //############################################################################################
  124 // Entry stub to the real DPA API entry
  125 #pragma origin DPA_API_ADDRESS_ENTRY
  126 uns8  DpaApiEntry( uns8 par1 @ param2, uns8 par2 @ param3.low8, uns8 apiIndex @ W )
  127 //############################################################################################
  128 {
  129   #asm
  130     DW  __MOVLP( DPA_API_ADDRESS >> 8 );
  131   DW  __CALL( DPA_API_ADDRESS );
  132   DW  __MOVLP( DPA_API_ADDRESS_ENTRY >> 8 );
  133   #endasm
  134 
  135     return W;
  136 }
  137 
  138 //############################################################################################
  139 // Use the gap for routines used by handler
  140 #pragma origin DPA_API_ADDRESS_ENTRY + 0x08
  141 
  142 //############################################################################################
  143 
  144 #ifndef NO_CUSTOM_DPA_HANDLER
  145 // Next comes Custom DPA handler routine
  146 #pragma origin CUSTOM_HANDLER_ADDRESS
  147 #endif
  148 
  149 #pragma updateBank 1
  150 
  151 //############################################################################################
  152 #else 
  153 //############################################################################################
  154 // 2nd include
  155 
  156 #ifndef NO_CUSTOM_DPA_HANDLER
  157 // Code bumper to detect too long code of Custom DPA handler + other routines
  158 #pragma origin CUSTOM_HANDLER_ADDRESS_END
  159 // To avoid adding some code after handler by mistake
  160 #pragma origin __MAX_FLASH_ADDRESS
  161 #endif
  162 
  163 #endif
  164 //############################################################################################