1 // ***********************************************************************
    2 //   Custom DPA Handler code example - External device connected to UART *
    3 // ***********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UART.c,v $
    7 // Version: $Revision: 1.19 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2019/12/11  Release for DPA 4.11
   12 //   2018/10/25  Release for DPA 3.03
   13 //   2017/03/13  Release for DPA 3.00
   14 //   2016/02/08  Release for DPA 2.26
   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 // The application demonstrates simple handler used to connect external device using UART
   29 
   30 // Before uploading this example make sure:
   31 // 1. Peripheral UART is enabled at HWP Configuration. It will be automatically opened.
   32 
   33 //############################################################################################
   34 
   35 // HW Profile ID
   36 #define HWPid     0x123F
   37 // HW Profile ID Version
   38 #define HWPidVer  0x0000
   39 
   40 // Bonding button, must be often (depends on used DCTR module) redefined because it might conflict with UART TX signal
   41 #define IsButton  ( !PORTA.0 )
   42 
   43 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   44 //############################################################################################
   45 bit CustomDpaHandler()
   46 //############################################################################################
   47 {
   48   // Handler presence mark
   49   clrwdt();
   50 
   51   // Detect DPA event to handle
   52   switch ( GetDpaEvent() )
   53   {
   54     // -------------------------------------------------
   55     case DpaEvent_Interrupt:
   56       // Do an extra quick background interrupt work
   57       // ! 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.
   58       // ! 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.
   59       // ! 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.
   60       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   61       // ! Make sure race condition does not occur when accessing those variables at other places.
   62       // ! 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.
   63       // ! Do not call any OS functions except setINDFx().
   64       // ! Do not use any OS variables especially for writing access.
   65       // ! 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.
   66 
   67       return Carry;
   68 
   69       // Define custom button (un)bonding only when a button is redefined
   70 #ifdef IsButton
   71       // -------------------------------------------------
   72     case DpaEvent_BondingButton:
   73       // Called to allow a bonding button customization
   74 
   75       userReg1.0 = 0;
   76       if ( IsButton )
   77         userReg1.0 = 1;
   78 
   79       goto DpaHandleReturnTRUE;
   80 #endif
   81 
   82       // -------------------------------------------------
   83     case DpaEvent_DpaRequest:
   84       // Called to interpret DPA request for peripherals
   85       // -------------------------------------------------
   86       // Peripheral enumeration
   87       if ( IsDpaEnumPeripheralsRequest() )
   88       {
   89         // We do not implement user peripheral
   90         //_DpaMessage.EnumPeripheralsAnswer.UserPerNr = 0;
   91         // Report HWPID
   92         _DpaMessage.EnumPeripheralsAnswer.HWPID = HWPid;
   93         _DpaMessage.EnumPeripheralsAnswer.HWPIDver = HWPidVer;
   94 
   95 DpaHandleReturnTRUE:
   96         return TRUE;
   97       }
   98   }
   99 
  100   return FALSE;
  101 }
  102 
  103 //############################################################################################
  104 // 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)
  105 #include "DPAcustomHandler.h"
  106 //############################################################################################