1 // ********************************************************************************
    2 //   Custom DPA Handler code example - Shows custom bonding using remapped button *
    3 // ********************************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-BondingButton.c,v $
    7 // Version: $Revision: 1.1 $
    8 // Date:    $Date: 2018/09/20 12:05:34 $
    9 //
   10 // Revision history:
   11 //   2018/??/??  Release for DPA 3.03
   12 //
   13 // *********************************************************************
   14 
   15 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   16 
   17 // Default IQRF include (modify the path according to your setup)
   18 #include "IQRF.h"
   19 
   20 // Default DPA header (modify the path according to your setup)
   21 #include "DPA.h"
   22 // Default Custom DPA Handler header (modify the path according to your setup)
   23 #include "DPAcustomHandler.h"
   24 
   25 // This code illustrates bonding using a button connected to the custom pin.
   26 // Except going to sleep the code behaves the same way as the standard bonding procedure.
   27 
   28 // Custom bonding button. Change to the pin and an active level of your choice.
   29 #define IsButton  ( !PORTA.0 )
   30 
   31 // The example will not work at demo DPA version because demo DPA does not support the following Custom DPA Handler events: DpaEvent_Interrupt, DpaEvent_BeforeSleep, DpaEvent_AfterSleep, DpaEvent_Reset, DpaEvent_DisableInterrupts, DpaEvent_ReceiveDpaResponse, DpaEvent_IFaceReceive, DpaEvent_ReceiveDpaRequest, DpaEvent_BeforeSendingDpaResponse
   32 
   33 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   34 //############################################################################################
   35 bit CustomDpaHandler()
   36 //############################################################################################
   37 {
   38   // Handler presence mark
   39   clrwdt();
   40 
   41   // Detect DPA event to handle
   42   switch ( GetDpaEvent() )
   43   {
   44     // -------------------------------------------------
   45     case DpaEvent_Interrupt:
   46       // Do an extra quick background interrupt work
   47       // ! 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.
   48       // ! 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.
   49       // ! 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.
   50       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   51       // ! Make sure race condition does not occur when accessing those variables at other places.
   52       // ! 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.
   53       // ! Do not call any OS functions except setINDFx().
   54       // ! Do not use any OS variables especially for writing access.
   55       // ! 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.
   56 
   57 DpaHandleReturnTRUE:
   58       return TRUE;
   59 
   60       // -------------------------------------------------
   61     case DpaEvent_BondingButton:
   62       // Called to allow a bonding button customization 
   63 
   64       userReg1.0 = 0;
   65       if ( IsButton )
   66         userReg1.0 = 1;
   67 
   68       return TRUE;
   69   }
   70 
   71   return FALSE;
   72 }
   73 
   74 //############################################################################################
   75 // Default Custom DPA Handler header; 2nd include to implement Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup) 
   76 #include "DPAcustomHandler.h"
   77 //############################################################################################