1 // *********************************************************************
    2 //   Main Custom DPA Handler header                                    *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: DPAcustomHandler.h,v $
    7 // Version: $Revision: 1.173 $
    8 // Date:    $Date: 2025/10/02 11:31:04 $
    9 //
   10 // Revision history:
   11 //   2025/05/30  Release for DPA 4.33
   12 //   2024/11/05  Release for DPA 4.32
   13 //   2024/04/17  Release for DPA 4.31
   14 //   2023/03/07  Release for DPA 4.30
   15 //   2022/10/05  Release for DPA 4.18
   16 //   2022/02/24  Release for DPA 4.17
   17 //   2021/08/20  Release for DPA 4.16
   18 //   2020/09/03  Release for DPA 4.15
   19 //   2020/04/03  Release for DPA 4.14
   20 //   2020/02/27  Release for DPA 4.13
   21 //   2020/01/09  Release for DPA 4.12
   22 //   2019/12/11  Release for DPA 4.11
   23 //   2019/10/09  Release for DPA 4.10
   24 //   2019/06/12  Release for DPA 4.03
   25 //   2019/06/03  Release for DPA 4.02
   26 //   2019/03/07  Release for DPA 4.01
   27 //   2019/01/10  Release for DPA 4.00
   28 //   2018/10/25  Release for DPA 3.03
   29 //   2017/11/16  Release for DPA 3.02
   30 //   2017/08/14  Release for DPA 3.01
   31 //   2017/03/13  Release for DPA 3.00
   32 //   2016/09/12  Release for DPA 2.28
   33 //   2016/04/14  Release for DPA 2.27
   34 //   2016/03/03  Release for DPA 2.26
   35 //   2016/01/21  Release for DPA 2.25
   36 //   2015/12/01  Release for DPA 2.24
   37 //   2015/10/23  Release for DPA 2.23
   38 //   2015/09/25  Release for DPA 2.22
   39 //   2015/09/03  Release for DPA 2.21
   40 //   2015/08/05  Release for DPA 2.20
   41 //   2014/10/31  Release for DPA 2.10
   42 //   2014/04/30  Release for DPA 2.00
   43 //   2013/10/03  Release for DPA 1.00
   44 //
   45 // *********************************************************************
   46 
   47 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   48 // IQRF Standards documentation https://doc.iqrf.org/
   49 
   50 #ifndef _CUSTOM_DPA_HANDLER_
   51 #define _CUSTOM_DPA_HANDLER_
   52 
   53 //############################################################################################
   54 // 1st include
   55 
   56 // Custom DPA Handler routine declaration
   57 bit CustomDpaHandler();
   58 
   59 // Various DPA flags shared between DPA and Custom DPA Handler
   60 uns8 DpaFlags @ usedBank4[0];
   61 
   62 // [C][N] TRUE if I2C timeout occurred at the last DpaApiI2C?() call
   63 bit I2CwasTimeout @ DpaFlags.0;
   64 // [N] Flag for the 1st DpaApiSleep
   65 bit FirstDpaApiSleep @ DpaFlags.1;
   66 
   67 #ifdef COORDINATOR_CUSTOM_HANDLER
   68 // [C] TRUE if interface master is not connected (detected)
   69 bit IFaceMasterNotConnected @ DpaFlags.2;
   70 #endif
   71 // [N] DPA by interface notification is sent also when there was a "reading" like DPA request
   72 bit EnableIFaceNotificationOnRead @ DpaFlags.3;
   73 // [N] TRUE when node was just bonded using default bonding procedure
   74 bit NodeWasBonded @ DpaFlags.4;
   75 // [N] When TRUE, then next call of DpaApiRfTxDpaPacket sends non-routed packet
   76 bit NonroutedRfTxDpaPacket @ DpaFlags.7;
   77 
   78 // [C] Ticks (decrementing) counter usable for timing in the coordinator's Customer DPA Handler
   79 uns16 DpaTicks @ usedBank4[1];
   80 // [N] toutRF for LP mode, read from configuration memory after reset
   81 uns8 LPtoutRF @ usedBank4[3];
   82 // DPA Request/Response HWPID
   83 uns16 _HWPID @ usedBank4[4];
   84 // Identifies type of reset (stored at UserReg0 upon module reset). See Reset chapter at IQRF User's Guide for more information
   85 uns8 ResetType @ usedBank4[6];
   86 // User DPA Values to return
   87 uns8 UserDpaValue @ usedBank4[7];
   88 // Network depth of the DPA request/response, increases on bridging, decreases on back-bridging
   89 uns8 NetDepth @ usedBank4[8];
   90 // TRUE when node was at DPA Service Mode after last boot
   91 bit DSMactivated @ usedBank4[9].0;
   92 // If set to TRUE, then LP RX mode in the main loop can be terminated by pin, see _RLPMAT
   93 bit LpRxPinTerminate @ usedBank4[9].1;
   94 // If set to TRUE, then [C] executes asynchronous DPA requests received from [N]
   95 bit AsyncReqAtCoordinator @ usedBank4[9].2;
   96 // RX filter used at the DPA main loop checkRF call
   97 uns8 RxFilter @ usedBank4[11];
   98 // Countdown variable for button bonding before going to deep sleep
   99 uns16 BondingSleepCountdown @ usedBank4[12];
  100 #define BONDING_SLEEP_COUNTDOWN_UNIT  290
  101 // Non-zero pseudo-random value, read-only, updated on every Reset and Idle event, at [N] only.
  102 uns16 Random @ usedBank4[14];
  103 // DPA value from the received packet or just to be sent to the interface.
  104 uns8 DpaValue @ usedBank4[16];
  105 // If non-zero then timeout value for DpaApiI2C?() calls
  106 uns8 I2Ctimeout @ usedBank4[17];
  107 #if !defined( TR7xD )
  108 // Current DPA Menu
  109 uns8 CurrentDpaMenu @  usedBank4[19];
  110 #endif
  111 
  112 // Macro to return an error from the peripheral handler. If the code size is not an issue this macro is the right choice.
  113 #define DpaApiReturnPeripheralError(error) do { \
  114     DpaApiSetPeripheralError( error ); \
  115     return Carry; \
  116   } while( 0 )
  117 
  118 // DPA API functions, see documentation for details
  119 #define DpaApiRfTxDpaPacket( dpaValue, netDepthAndFlags ) DpaApiEntry( dpaValue, netDepthAndFlags, DPA_API_RFTX_DPAPACKET )
  120 #define DpaApiReadConfigByte( index )                     DpaApiEntry( index, param3.low8, DPA_API_READ_CONFIG_BYTE )
  121 #define DpaApiLocalRequest()                              DpaApiEntry( param2, param3.low8, DPA_API_LOCAL_REQUEST )
  122 #define DpaApiSetPeripheralError( error )                 DpaApiEntry( error, param3.low8, DPA_API_SET_PERIPHERAL_ERROR )
  123 #define DpaApiSendToIFaceMaster( dpaValue, flags )        DpaApiEntry( dpaValue, flags, DPA_API_SEND_TO_IFACEMASTER )
  124 #define DpaApiSetRfDefaults()                             DpaApiEntry( param2, param3.low8, DPA_API_SET_RF_DEFAULTS )
  125 #define DpaApiLocalFrc( frcCommand, replyTxPower )        DpaApiEntry( frcCommand, replyTxPower, DPA_API_LOCAL_FRC )
  126 #define DpaApiCrc8( crc8, data )                          DpaApiEntry( crc8, data, DPA_API_CRC8 )
  127 #define DpaApiAggregateFrc()                              DpaApiEntry( param2, param3.low8, DPA_API_AGGREGATE_FRC )
  128 #define DpaApiSetOTK()                                    DpaApiEntry( param2, param3.low8, DPA_API_SET_OTK )
  129 #define DpaApiI2Cinit( frequency )                        DpaApiEntry( frequency, param3.low8, DPA_API_I2C_INIT )
  130 #define DpaApiI2Cstart( address )                         DpaApiEntry( address, param3.low8, DPA_API_I2C_START )
  131 #define DpaApiI2Cwrite( data )                            DpaApiEntry( data, param3.low8, DPA_API_I2C_WRITE )
  132 #define DpaApiI2Cread( ack )                              DpaApiEntry( ack, param3.low8, DPA_API_I2C_READ )
  133 #define DpaApiI2Cstop()                                   DpaApiEntry( param2, param3.low8, DPA_API_I2C_STOP )
  134 #define DpaApiI2CwaitForACK( address )                    DpaApiEntry( address, param3.low8, DPA_API_I2C_WAIT_FOR_ACK )
  135 #define DpaApiI2Cshutdown()                               DpaApiEntry( param2, param3.low8, DPA_API_I2C_SHUTDOWN )
  136 #define DpaApiI2CwaitForIdle()                            DpaApiEntry( param2, param3.low8, DPA_API_I2C_WAIT_FOR_IDLE )
  137 #define DpaApiSleep( wdtcon )                             DpaApiEntry( wdtcon, param3.low8, DPA_API_SLEEP )
  138 #define DpaApiAfterSleep()                                DpaApiEntry( param2, param3.low8, DPA_API_AFTER_SLEEP )
  139 #define DpaApiRandom()                                    DpaApiEntry( param2, param3.low8, DPA_API_RANDOM )
  140 
  141 #ifdef COORDINATOR_CUSTOM_HANDLER
  142 #define DpaApiRfTxDpaPacketCoordinator()                  DpaApiEntry( param2, param3.low8, DPA_API_COORDINATOR_RFTX_DPAPACKET )
  143 #endif
  144 
  145 #if !defined( TR7xD )
  146 #define DpaApiMenu( menu, flags )                         DpaApiEntry( menu, flags, DPA_API_MENU )
  147 #define DpaApiMenuIndicateResult( ok )                    DpaApiEntry( ok, param3.low8, DPA_API_MENU_INDICATE_RESULT )
  148 #define DpaApiMenuExecute( menuAndItem )                  DpaApiEntry( menuAndItem, param3.low8, DPA_API_MENU_EXECUTE )
  149 #define DpaApiDeepSleep( wdtcon )                         DpaApiEntry( wdtcon, param3.low8, DPA_API_DEEP_SLEEP )
  150 #endif
  151 
  152 #define DpaApiLocalFrc_StackSaver( frcCommand, replyTxPower ) \
  153   param2 = frcCommand; \
  154   param3.low8 = replyTxPower; \
  155   W = DPA_API_LOCAL_FRC; \
  156   #asm \
  157     DW  __MOVLP( DPA_API_ADDRESS >> 8 ); \
  158     DW  __CALL( DPA_API_ADDRESS ); \
  159     DW  __MOVLP( CUSTOM_HANDLER_ADDRESS >> 8 ); \
  160   #endasm \
  161 
  162 #define I2CcomputeFrequency( I2Cfrequency)                ( ( ( F_OSC ) / ( ( I2Cfrequency ) * 4 ) ) - 1 )
  163 
  164 // Wrapper routines to be called instead of DpaApi??? macros to decrease code size if more than ~2 calls are needed
  165 void _DpaApiI2Cinit( uns8 frequency );
  166 void _DpaApiI2Cstart( uns8 address );
  167 void _DpaApiI2Cwrite( uns8 data );
  168 uns8 _DpaApiI2Cread( uns8 ack );
  169 void _DpaApiI2Cstop();
  170 void _DpaApiI2CwaitForACK( uns8 address );
  171 void _DpaApiI2Cshutdown();
  172 void _DpaApiI2CwaitForIdle();
  173 void _DpaApiSleep( uns8 wdtcon );
  174 #if !defined( TR7xD )
  175 void _DpaApiDeepSleep( uns8 wdtcon );
  176 void _DpaApiMenuIndicateResult( uns8 ok );
  177 #endif
  178 void _DpaApiSleepFirst( uns8 wdtcon );
  179 void _DpaApiAfterSleep();
  180 uns8 _DpaApiRandom();
  181 void _DpaApiSetRfDefaults();
  182 
  183 // Helper "multi" function to decrease code size if used more times
  184 void _DpaApiI2Cwrite0();
  185 void _DpaApiI2CwriteAndStop( uns8 data );
  186 uns8 _DpaApiI2CreadACK();
  187 uns8 _DpaApiI2CreadNACK();
  188 uns8 _DpaApiI2CreadNACKandStop();
  189 
  190 #ifdef COORDINATOR_CUSTOM_HANDLER
  191 #undef  DpaEvent_Interrupt
  192 #undef  DpaEvent_BeforeSleep
  193 #undef  DpaEvent_AfterSleep
  194 #undef  DpaEvent_FrcValue
  195 #undef  DpaEvent_FrcResponseTime
  196 #else
  197 #undef  DpaEvent_ReceiveDpaResponse
  198 #undef  DpaEvent_IFaceReceive
  199 #endif
  200 
  201 // To detect overlapping code in case someone would put some code before this header by mistake
  202 #pragma origin __APPLICATION_ADDRESS
  203 #pragma updateBank 0
  204 
  205 //############################################################################################
  206 // Main IQRF entry point jumps to the main DPA entry point
  207 void APPLICATION()
  208 //############################################################################################
  209 {
  210   #asm
  211     DW  __MOVLP( MAIN_DPA_ADDRESS >> 8 );
  212   DW  __GOTO( MAIN_DPA_ADDRESS );
  213   #endasm
  214 
  215 #ifndef NO_CUSTOM_DPA_HANDLER
  216     // Fake call to force CustomDpaHandler() compilation
  217     CustomDpaHandler();
  218 #endif
  219 
  220   // Fake call to force DpaApiEntry() compilation
  221   DpaApiEntry( param2, param3.low8, W );
  222 }
  223 
  224 //############################################################################################
  225 // Entry stub to the real DPA API entry
  226 #pragma origin DPA_API_ADDRESS_ENTRY
  227 uns8  DpaApiEntry( uns8 par1 @ param2, uns8 par2 @ param3.low8, uns8 apiIndex @ W )
  228 //############################################################################################
  229 {
  230   #asm
  231     DW  __MOVLP( DPA_API_ADDRESS >> 8 );
  232   DW  __CALL( DPA_API_ADDRESS );
  233   DW  __MOVLP( DPA_API_ADDRESS_ENTRY >> 8 );
  234   #endasm
  235 
  236     return W;
  237 }
  238 
  239 //############################################################################################
  240 #pragma origin DPA_API_ADDRESS_ENTRY + 0x08
  241 
  242 //############################################################################################
  243 
  244 #ifndef NO_CUSTOM_DPA_HANDLER
  245 // Next comes Custom DPA handler routine
  246 #pragma origin CUSTOM_HANDLER_ADDRESS
  247 #endif
  248 
  249 #pragma updateBank 1
  250 
  251 //############################################################################################
  252 #else // _CUSTOM_DPA_HANDLER_
  253 //############################################################################################
  254 // 2nd include
  255 
  256 #ifndef NO_CUSTOM_DPA_HANDLER
  257 
  258 // Library wrappers and helpers to decrease code size
  259 //############################################################################################
  260 #pragma library 1
  261 //############################################################################################
  262 uns8 _DpaApiUserBank_01( uns8 apiIndex @ W )
  263 //############################################################################################
  264 {
  265 #pragma updateBank exit = UserBank_01
  266   return DpaApiEntry( param2, param3.low8, apiIndex );
  267 }
  268 //############################################################################################
  269 void _DpaApiI2Cinit( uns8 frequency @ W )
  270 //############################################################################################
  271 {
  272 #pragma updateBank exit = UserBank_01
  273   param2 = frequency;
  274   _DpaApiUserBank_01( DPA_API_I2C_INIT );
  275 }
  276 //############################################################################################
  277 void _DpaApiI2Cstart( uns8 address @ W )
  278 //############################################################################################
  279 {
  280 #pragma updateBank exit = UserBank_01
  281   param2 = address;
  282   _DpaApiUserBank_01( DPA_API_I2C_START );
  283 }
  284 //############################################################################################
  285 void _DpaApiI2Cwrite0()
  286 //############################################################################################
  287 {
  288 #pragma updateBank exit = UserBank_01
  289   _DpaApiI2Cwrite( 0 );
  290 }
  291 //############################################################################################
  292 void _DpaApiI2Cwrite( uns8 data @ W )
  293 //############################################################################################
  294 {
  295 #pragma updateBank exit = UserBank_01
  296   param2 = data;
  297   _DpaApiUserBank_01( DPA_API_I2C_WRITE );
  298 }
  299 //############################################################################################
  300 uns8 _DpaApiI2CreadNACKandStop()
  301 //############################################################################################
  302 {
  303 #pragma updateBank exit = UserBank_01
  304   param2 = _DpaApiI2CreadNACK();
  305   _DpaApiI2Cstop(); /* Must not use param2 */
  306   return param2;
  307 }
  308 //############################################################################################
  309 uns8 _DpaApiI2CreadACK()
  310 //############################################################################################
  311 {
  312 #pragma updateBank exit = UserBank_01
  313   return _DpaApiI2Cread( 1 );
  314 }
  315 //############################################################################################
  316 uns8 _DpaApiI2CreadNACK()
  317 //############################################################################################
  318 {
  319 #pragma updateBank exit = UserBank_01
  320   return _DpaApiI2Cread( 0 );
  321 }
  322 //############################################################################################
  323 uns8 _DpaApiI2Cread( uns8 ack @ W )
  324 //############################################################################################
  325 {
  326 #pragma updateBank exit = UserBank_01
  327   param2 = ack;
  328   return _DpaApiUserBank_01( DPA_API_I2C_READ );
  329 }
  330 //############################################################################################
  331 void _DpaApiI2CwriteAndStop( uns8 data @ W )
  332 //############################################################################################
  333 {
  334 #pragma updateBank exit = UserBank_01
  335   _DpaApiI2Cwrite( data );
  336   _DpaApiI2Cstop();
  337 }
  338 //############################################################################################
  339 void _DpaApiI2Cstop()
  340 //############################################################################################
  341 {
  342 #pragma updateBank exit = UserBank_01
  343   _DpaApiUserBank_01( DPA_API_I2C_STOP );
  344 }
  345 //############################################################################################
  346 void _DpaApiI2CwaitForACK( uns8 address @ W )
  347 //############################################################################################
  348 {
  349 #pragma updateBank exit = UserBank_01
  350   DpaApiI2CwaitForACK( address );
  351 }
  352 //############################################################################################
  353 void _DpaApiI2Cshutdown()
  354 //############################################################################################
  355 {
  356 #pragma updateBank exit = UserBank_01
  357   _DpaApiUserBank_01( DPA_API_I2C_SHUTDOWN );
  358 }
  359 //############################################################################################
  360 void _DpaApiI2CwaitForIdle()
  361 //############################################################################################
  362 {
  363 #pragma updateBank exit = UserBank_01
  364   _DpaApiUserBank_01( DPA_API_I2C_WAIT_FOR_IDLE );
  365 }
  366 //############################################################################################
  367 void _DpaApiSleep( uns8 wdtcon @ W )
  368 //############################################################################################
  369 {
  370 #pragma updateBank exit = UserBank_01
  371   param2 = wdtcon;
  372   _DpaApiUserBank_01( DPA_API_SLEEP );
  373 }
  374 #if !defined( TR7xD )
  375 //############################################################################################
  376 void _DpaApiDeepSleep( uns8 wdtcon @ W )
  377 //############################################################################################
  378 {
  379 #pragma updateBank exit = UserBank_01
  380   param2 = wdtcon;
  381   _DpaApiUserBank_01( DPA_API_DEEP_SLEEP );
  382 }
  383 #endif
  384 //############################################################################################
  385 void _DpaApiSleepFirst( uns8 wdtcon @ W )
  386 //############################################################################################
  387 {
  388 #pragma updateBank exit = UserBank_01
  389   FirstDpaApiSleep = TRUE;
  390   _DpaApiSleep( wdtcon );
  391 }
  392 //############################################################################################
  393 void _DpaApiAfterSleep()
  394 //############################################################################################
  395 {
  396 #pragma updateBank exit = UserBank_01
  397   _DpaApiUserBank_01( DPA_API_AFTER_SLEEP );
  398 }
  399 //############################################################################################
  400 uns8 _DpaApiRandom()
  401 //############################################################################################
  402 {
  403 #pragma updateBank exit = UserBank_01
  404   return _DpaApiUserBank_01( DPA_API_RANDOM );
  405 }
  406 //############################################################################################
  407 void _DpaApiSetRfDefaults()
  408 //############################################################################################
  409 {
  410 #pragma updateBank exit = UserBank_01
  411   _DpaApiUserBank_01( DPA_API_SET_RF_DEFAULTS );
  412 }
  413 #if !defined( TR7xD )
  414 //############################################################################################
  415 void _DpaApiMenuIndicateResult( uns8 ok @ W )
  416 //############################################################################################
  417 {
  418 #pragma updateBank exit = UserBank_01
  419   param2 = ok;
  420   _DpaApiUserBank_01( DPA_API_MENU_INDICATE_RESULT );
  421 }
  422 #endif
  423 //############################################################################################
  424 
  425 
  426 
  427 // Code bumper to detect too long code of Custom DPA handler + other routines
  428 #pragma origin CUSTOM_HANDLER_ADDRESS_END
  429 // To avoid adding some code after handler by mistake
  430 #pragma origin __MAX_FLASH_ADDRESS
  431 #endif
  432 
  433 #endif  // _CUSTOM_DPA_HANDLER_
  434 //############################################################################################