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.32 $
    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 //   2019/12/11  Release for DPA 4.11
   15 //   2018/10/25  Release for DPA 3.03
   16 //   2017/03/13  Release for DPA 3.00
   17 //   2016/02/08  Release for DPA 2.26
   18 //
   19 // *********************************************************************
   20 
   21 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   22 
   23 // Default IQRF include (modify the path according to your setup)
   24 #include "IQRF.h"
   25 
   26 // Default DPA header (modify the path according to your setup)
   27 #include "DPA.h"
   28 // Default Custom DPA Handler header (modify the path according to your setup)
   29 #include "DPAcustomHandler.h"
   30 
   31 // The application demonstrates simple handler used to connect external device using UART
   32 
   33 // Before uploading this example make sure:
   34 // 1. Peripheral UART is enabled at HWP Configuration. It will be automatically opened.
   35 
   36 //############################################################################################
   37 
   38 // HW Profile ID
   39 #define HWPid     0x123F
   40 // HW Profile ID Version
   41 #define HWPidVer  0x0000
   42 
   43 
   44 #ifdef TR7xD
   45 // Bonding button, must be often (depends on used TRxD module) redefined because it might conflict with UART TX signal
   46 #define IsButton  ( !PORTA.0 )
   47 #else
   48 // At TR7xG button is fixed at RB.4 but UART can be remapped using PPS
   49 #define PPS_UART() \
   50 do { \
   51   /* RC3 == TX (default DPA setting is RC6) ? */ \
   52   if ( RC3PPS != 0x10 ) { \
   53     /* No, make custom PPS */ \
   54     unlockPPS(); \
   55     RC3PPS = 0x10;    /* RC3 = TX */ \
   56     RXPPS = 0b10.100; /* RC4 = RX */ \
   57     lockPPS(); } \
   58 } while(0)
   59 #endif
   60 
   61 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   62 //############################################################################################
   63 bit CustomDpaHandler()
   64 //############################################################################################
   65 {
   66   // Handler presence mark
   67   clrwdt();
   68 
   69   // Detect DPA event to handle
   70   switch ( GetDpaEvent() )
   71   {
   72     // -------------------------------------------------
   73     case DpaEvent_Interrupt:
   74       // Do an extra quick background interrupt work
   75       // ! 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.
   76       // ! 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.
   77       // ! 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.
   78       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   79       // ! Make sure race condition does not occur when accessing those variables at other places.
   80       // ! 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.
   81       // ! Do not call any OS functions except setINDFx().
   82       // ! Do not use any OS variables especially for writing access.
   83       // ! 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.
   84 
   85       return Carry;
   86 
   87 #ifdef PPS_UART
   88       // -------------------------------------------------
   89     case DpaEvent_Idle:
   90       // Do a quick background work when RF packet is not received
   91 
   92       PPS_UART();
   93       break;
   94 #endif
   95 
   96       // Define custom button (un)bonding only when a button is redefined
   97 #ifdef IsButton
   98       // -------------------------------------------------
   99     case DpaEvent_BondingButton:
  100       // Called to allow a bonding button customization
  101 
  102       userReg1.0 = 0;
  103       if ( IsButton )
  104         userReg1.0 = 1;
  105 
  106       goto DpaHandleReturnTRUE;
  107 #endif
  108 
  109       // -------------------------------------------------
  110     case DpaEvent_DpaRequest:
  111       // Called to interpret DPA request for peripherals
  112       // -------------------------------------------------
  113       // Peripheral enumeration
  114       if ( IsDpaEnumPeripheralsRequest() )
  115       {
  116         // We do not implement user peripheral
  117         //_DpaMessage.EnumPeripheralsAnswer.UserPerNr |=  0;
  118         // Report HWPID
  119         _DpaMessage.EnumPeripheralsAnswer.HWPID |= HWPid;
  120         _DpaMessage.EnumPeripheralsAnswer.HWPIDver |= HWPidVer;
  121 
  122 DpaHandleReturnTRUE:
  123         return TRUE;
  124       }
  125   }
  126 
  127   return FALSE;
  128 }
  129 
  130 //############################################################################################
  131 // 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)
  132 #include "DPAcustomHandler.h"
  133 //############################################################################################