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