1 // ********************************************************************************
    2 //   Custom DPA Handler code example - Shows custom bonding using remapped button *
    3 // ********************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-BondingButton.c,v $
    7 // Version: $Revision: 1.17 $
    8 // Date:    $Date: 2023/03/07 08:03:13 $
    9 //
   10 // Revision history:
   11 //   2023/03/07  Release for DPA 4.30
   12 //   2022/10/05  Release for DPA 4.18
   13 //   2022/02/24  Release for DPA 4.17
   14 //   2018/??/??  Release for DPA 3.03
   15 //
   16 // *********************************************************************
   17 
   18 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   19 
   20 // Default IQRF include (modify the path according to your setup)
   21 #include "IQRF.h"
   22 
   23 // Default DPA header (modify the path according to your setup)
   24 #include "DPA.h"
   25 // Default Custom DPA Handler header (modify the path according to your setup)
   26 #include "DPAcustomHandler.h"
   27 
   28 #if !defined( TR7xD )
   29 #error This example is intended for TR7xD
   30 #endif
   31 
   32 // This code illustrates bonding using a button connected to the custom pin.
   33 // Except going to sleep the code behaves the same way as the standard bonding procedure.
   34 
   35 // Custom bonding button. Change to the pin and an active level of your choice.
   36 #define IsButton  ( !PORTA.0 )
   37 
   38 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   39 //############################################################################################
   40 bit CustomDpaHandler()
   41 //############################################################################################
   42 {
   43   // Handler presence mark
   44   clrwdt();
   45 
   46   // Detect DPA event to handle
   47   switch ( GetDpaEvent() )
   48   {
   49     // -------------------------------------------------
   50     case DpaEvent_Interrupt:
   51       // Do an extra quick background interrupt work
   52       // ! The time spent handling this event is critical.If there is no interrupt to handle return immediately otherwise keep the code as fast as possible.
   53       // ! Make sure the event is the 1st case in the main switch statement at the handler routine.This ensures that the event is handled as the 1st one.
   54       // ! It is desirable that this event is handled with immediate return even if it is not used by the custom handler because the Interrupt event is raised on every MCU interrupt and the “empty” return handler ensures the shortest possible interrupt routine response time.
   55       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   56       // ! Make sure race condition does not occur when accessing those variables at other places.
   57       // ! Make sure( inspect.lst file generated by C compiler ) compiler does not create any hidden temporary local variable( occurs when using division, multiplication or bit shifts ) at the event handler code.The name of such variable is usually Cnumbercnt.
   58       // ! Do not call any OS functions except setINDFx().
   59       // ! Do not use any OS variables especially for writing access.
   60       // ! All above rules apply also to any other function being called from the event handler code, although calling any function from Interrupt event is not recommended because of additional MCU stack usage.
   61 
   62 DpaHandleReturnTRUE:
   63       return TRUE;
   64 
   65       // -------------------------------------------------
   66     case DpaEvent_BondingButton:
   67       // Called to allow a bonding button customization
   68 
   69       userReg1.0 = 0;
   70       if ( IsButton )
   71         userReg1.0 = 1;
   72 
   73       return TRUE;
   74   }
   75 
   76   return FALSE;
   77 }
   78 
   79 //############################################################################################
   80 // Default Custom DPA Handler header; 2nd include implementing a Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup)
   81 #include "DPAcustomHandler.h"
   82 //############################################################################################