1 // *********************************************************************
    2 //   Custom DPA Handler code example - UART repeater                   *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UARTrepeater.c,v $
    7 // Version: $Revision: 1.20 $
    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/09/12  Release for DPA 2.28
   14 //   2015/08/05  Release for DPA 2.20
   15 //   2014/10/31  Release for DPA 2.10
   16 //
   17 // *********************************************************************
   18 
   19 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   20 
   21 // Default IQRF include (modify the path according to your setup)
   22 #include "IQRF.h"
   23 
   24 // Default DPA header (modify the path according to your setup)
   25 #include "DPA.h"
   26 // Default Custom DPA Handler header (modify the path according to your setup)
   27 #include "DPAcustomHandler.h"
   28 
   29 // This example opens its UART peripheral and periodically TX data previously RX from UART, the data are available at peripheral RAM too
   30 // This example works only at STD mode, not at LP mode
   31 
   32 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   33 //############################################################################################
   34 bit CustomDpaHandler()
   35 //############################################################################################
   36 {
   37   // Handler presence mark
   38   clrwdt();
   39 
   40   // Detect DPA event to handle
   41   switch ( GetDpaEvent() )
   42   {
   43     // -------------------------------------------------
   44     case DpaEvent_Interrupt:
   45       // Do an extra quick background interrupt work
   46       // ! 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.
   47       // ! 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.
   48       // ! 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.
   49       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   50       // ! Make sure race condition does not occur when accessing those variables at other places.
   51       // ! 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.
   52       // ! Do not call any OS functions except setINDFx().
   53       // ! Do not use any OS variables especially for writing access.
   54       // ! 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.
   55 
   56 DpaHandleReturnTRUE:
   57       return TRUE;
   58 
   59       // -------------------------------------------------
   60     case DpaEvent_Idle:
   61       // Do a quick background work when RF packet is not received
   62 
   63       // No data RX from UART yet
   64       _DpaDataLength = 0;
   65       for ( ;; )
   66       {
   67         // TX and RX data to/from UART
   68         _PNUM = PNUM_UART;
   69         _PCMD = CMD_UART_WRITE_READ;
   70         // Wait 0 ms for RX
   71         _DpaMessage.PerUartSpiWriteRead_Request.ReadTimeout = 0;
   72         // Copy previously RX data to TX
   73         copyMemoryBlock( PeripheralRam + offsetof( TPerUartSpiWriteRead_Request, WrittenData ), _DpaMessage.PerUartSpiWriteRead_Request.WrittenData, _DpaDataLength );
   74         // One more byte to the request
   75         _DpaDataLength++;
   76         // TX/RX from UART
   77         DpaApiLocalRequest();
   78         // Error TX/RX UART?
   79         if ( _PNUM == PNUM_ERROR_FLAG )
   80         {
   81           setLEDR();
   82           break;
   83         }
   84 
   85         // No data RX?
   86         if ( _DpaDataLength == 0 )
   87           break;
   88 
   89         // Indicate RX data
   90         pulseLEDG();
   91         // Copy to RAM peripheral RX data length and ...
   92         PeripheralRam[0] = _DpaDataLength;
   93         // RX data too
   94         copyMemoryBlock( _DpaMessage.Response.PData, PeripheralRam + 1, _DpaDataLength );
   95       }
   96       break;
   97 
   98       // -------------------------------------------------
   99     case DpaEvent_Init:
  100       // Do a one time initialization work before main loop starts
  101 
  102       // Open UART peripheral
  103       _PNUM = PNUM_UART;
  104       _PCMD = CMD_UART_OPEN;
  105       _DpaMessage.PerUartOpen_Request.BaudRate = DpaBaud_115200;
  106       _DpaDataLength = sizeof( _DpaMessage.PerUartOpen_Request );
  107       DpaApiLocalRequest();
  108       // Error opening UART?
  109       if ( _PNUM == PNUM_ERROR_FLAG )
  110         setLEDR();
  111 
  112       break;
  113   }
  114 
  115   return FALSE;
  116 }
  117 //############################################################################################
  118 // 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) 
  119 #include "DPAcustomHandler.h"
  120 //############################################################################################