1 // *********************************************************************
    2 //   Custom DPA Handler code example - UART repeater                   *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UARTrepeater.c,v $
    7 // Version: $Revision: 1.31 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2020/09/03  Release for DPA 4.15
   12 //   2019/12/11  Release for DPA 4.11
   13 //   2018/10/25  Release for DPA 3.03
   14 //   2017/03/13  Release for DPA 3.00
   15 //   2016/09/12  Release for DPA 2.28
   16 //   2015/08/05  Release for DPA 2.20
   17 //   2014/10/31  Release for DPA 2.10
   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 // This example opens its UART peripheral and periodically TX data previously RX from UART, the data are available at peripheral RAM too
   32 // This example works only at STD mode, not at LP mode
   33 
   34 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   35 //############################################################################################
   36 bit CustomDpaHandler()
   37 //############################################################################################
   38 {
   39   // Handler presence mark
   40   clrwdt();
   41 
   42   // Detect DPA event to handle
   43   switch ( GetDpaEvent() )
   44   {
   45     // -------------------------------------------------
   46     case DpaEvent_Interrupt:
   47       // Do an extra quick background interrupt work
   48       // ! 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.
   49       // ! 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.
   50       // ! 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.
   51       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   52       // ! Make sure race condition does not occur when accessing those variables at other places.
   53       // ! 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.
   54       // ! Do not call any OS functions except setINDFx().
   55       // ! Do not use any OS variables especially for writing access.
   56       // ! 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.
   57 
   58 DpaHandleReturnTRUE:
   59       return TRUE;
   60 
   61       // -------------------------------------------------
   62     case DpaEvent_Idle:
   63       // Do a quick background work when RF packet is not received
   64 
   65       // No data RX from UART yet
   66       _DpaDataLength = 0;
   67       for ( ;; )
   68       {
   69         // TX and RX data to/from UART
   70         _PNUM = PNUM_UART;
   71         _PCMD = CMD_UART_WRITE_READ;
   72         // Wait 0 ms for RX
   73         _DpaMessage.PerUartWriteRead_Request.ReadTimeout = 0;
   74         // Copy previously RX data to TX
   75         copyMemoryBlock( PeripheralRam + offsetof( TPerUartWriteRead_Request, WrittenData ), _DpaMessage.PerUartWriteRead_Request.WrittenData, _DpaDataLength );
   76         // One more byte to the request
   77         _DpaDataLength++;
   78         // TX/RX from UART
   79         DpaApiLocalRequest();
   80         // Error TX/RX UART?
   81         if ( _PNUM == PNUM_ERROR_FLAG )
   82         {
   83           setLEDR();
   84           break;
   85         }
   86 
   87         // No data RX?
   88         if ( _DpaDataLength == 0 )
   89           break;
   90 
   91         // Indicate RX data
   92         pulseLEDG();
   93         // Copy to RAM peripheral RX data length and ...
   94         PeripheralRam[0] = _DpaDataLength;
   95         // RX data too
   96         copyMemoryBlock( _DpaMessage.Response.PData, PeripheralRam + 1, _DpaDataLength );
   97       }
   98       break;
   99   }
  100 
  101   return FALSE;
  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 //############################################################################################