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