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