1 // ********************************************************************************
    2 //   Custom DPA Handler code example - Coordinator pulses all LEDs in the network *
    3 // ********************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Coordinator-PulseLEDs.c,v $
    7 // Version: $Revision: 1.27 $
    8 // Date:    $Date: 2021/04/26 15:13:50 $
    9 //
   10 // Revision history:
   11 //   2017/03/13  Release for DPA 3.00
   12 //   2015/08/05  Release for DPA 2.20
   13 //   2014/10/31  Release for DPA 2.10
   14 //   2014/04/30  Release for DPA 2.00
   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 // Implement Custom DPA Handler for Coordinator
   24 #define COORDINATOR_CUSTOM_HANDLER
   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 coordinator example periodically pulses all LEDs at the network if the coordinator is not connected to its master
   32 
   33 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   34 //############################################################################################
   35 bit CustomDpaHandler()
   36 //############################################################################################
   37 {
   38   // Handler presence mark
   39   clrwdt();
   40 
   41   // Detect DPA event to handle (unused event handlers can be commented out or even deleted)
   42   switch ( GetDpaEvent() )
   43   {
   44     // -------------------------------------------------
   45     case DpaEvent_Idle:
   46       // Do a quick background work when RF packet is not received
   47 
   48       // The following block of code demonstrates autonomous sending of packets if the [C] is not connected to the interface master
   49       // (if the highest bit is set, then DpaTicks overflown from 0 to 0xffff
   50       if ( IFaceMasterNotConnected && DpaTicks.15 == 1 )
   51       {
   52         // Disable interrupts to make sure 16b variable DpaTicks access is atomic
   53         GIE = FALSE;
   54         // Setup "timer" for 10 seconds
   55         DpaTicks = 100L * 10 - 1;
   56         GIE = TRUE;
   57 
   58         // DPA request is broadcast
   59         _NADR = BROADCAST_ADDRESS;
   60         _NADRhigh = 0;
   61         // Use IO peripheral to work with LEDs even if LED peripherals are not present
   62         _PNUM = PNUM_IO;
   63         // Make a both LEDs pulse
   64         _PCMD = CMD_IO_SET;
   65         // Any HWPID
   66         _HWPID = HWPID_DoNotCheck;
   67 
   68         // LEDG=1 => Set PORTB.7 to 1
   69         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Port = PNUM_IO_PORTB; // PORTB
   70         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Mask = 0b1000.0000;    // bit 7
   71         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Value = 0b1000.0000;   // bit 7 = 1
   72 
   73         // LEDR=1 => Set PORTA.2 to 1
   74         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[1].Port = PNUM_IO_PORTA; // PORTA
   75         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[1].Mask = 0b0000.0100;    // bit 2
   76         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[1].Value = 0b0000.0100;   // bit 2 = 1
   77 
   78         // 64 ms delay
   79         _DpaMessage.PerIoDirectionAndSet_Request.Delays[2].Header = PNUM_IO_DELAY;  // delay
   80         _DpaMessage.PerIoDirectionAndSet_Request.Delays[2].Delay = 64;              // 64
   81 
   82         // LEDR=0 => Set PORTA.2 to 0
   83         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[3].Port = PNUM_IO_PORTA;  // PORTA
   84         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[3].Mask = 0b0000.0100;    // bit 2
   85         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[3].Value = 0b0000.0000;   // bit 2 = 0
   86 
   87         // LEDG=0 => Set PORTB.7 to 0
   88         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[4].Port = PNUM_IO_PORTB;  // PORTB
   89         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[4].Mask = 0b1000.0000;    // bit 7
   90         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[4].Value = 0b0000.0000;   // bit 7 = 0
   91 
   92         // Setup the correct length of data
   93         _DpaDataLength = 5 * sizeof( _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0] );
   94         // Send the DPA request
   95         DpaApiRfTxDpaPacketCoordinator();
   96 
   97         // Do local a LEDG pulse
   98         pulseLEDG();
   99       }
  100 
  101       break;
  102 
  103     case DpaEvent_Init: // -------------------------------------------------
  104       // Enable LED during special system actions (discovery, FRC)
  105       _systemLEDindication = TRUE;
  106       break;
  107   }
  108 
  109   return FALSE;
  110 }
  111 
  112 //############################################################################################
  113 // 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) 
  114 #include "DPAcustomHandler.h"
  115 //############################################################################################