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