1 // *********************************************************************
    2 //   Custom DPA Handler code example - Using TMR6 MCU timer            *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Timer.c,v $
    7 // Version: $Revision: 1.36 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2017/03/13  Release for DPA 3.00
   12 //   2016/09/12  Release for DPA 2.28
   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 // 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 initializes TMR6 (at Reset or Init event) and uses it to pulse LED at Idle event.
   30 // TMR6 if driven by the internal PIC RC oscillator. See CustomDpaHandler-TimerCalibrated.c for a more precise timer implementation.
   31 // This example works only at STD mode, not at LP mode
   32 
   33 // If next symbol is defined then initialize timer at Reset event so it could be used already during Reset events (custom un/bonding)
   34 //#define   EARLY_INITIALIZATION
   35 
   36 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   37 //############################################################################################
   38 bit CustomDpaHandler()
   39 //############################################################################################
   40 {
   41   // Handler presence mark
   42   clrwdt();
   43 
   44   // Place for local static variables used only within CustomDpaHandler() among more events
   45   static uns8 tmrCounter;
   46 
   47   // Detect DPA event to handle
   48   switch ( GetDpaEvent() )
   49   {
   50     // -------------------------------------------------
   51     case DpaEvent_Interrupt:
   52       // Do an extra quick background interrupt work
   53       // ! 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.
   54       // ! 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.
   55       // ! 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.
   56       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   57       // ! Make sure race condition does not occur when accessing those variables at other places.
   58       // ! 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.
   59       // ! Do not call any OS functions except setINDFx().
   60       // ! Do not use any OS variables especially for writing access.
   61       // ! 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.
   62 
   63       //  If TMR6 interrupt occurred
   64       if ( TMR6IF )
   65       {
   66         // Unmask interrupt
   67         TMR6IF = 0;
   68         // Decrement count
   69         if ( tmrCounter != 0 )
   70           tmrCounter--;
   71       }
   72 
   73       return Carry;
   74 
   75       // -------------------------------------------------
   76     case DpaEvent_Idle:
   77       // Do a quick background work when RF packet is not received
   78 
   79       if ( tmrCounter == 0 )
   80       {
   81         // Pulse LEDR every 1.0 s
   82         tmrCounter = 1000 / 10;
   83         pulseLEDG();
   84       }
   85 
   86       break;
   87 
   88       // -------------------------------------------------
   89 #ifdef EARLY_INITIALIZATION
   90     case DpaEvent_Reset:
   91       // Called after module is reset
   92       // Could be called more times, but initialize Timer only once
   93       if ( !TMR6IE )
   94 #else
   95     // -------------------------------------------------
   96     case DpaEvent_Init:
   97       // Do a one time initialization before main loop starts
   98 #endif
   99     {
  100       // Setup TMR6 to generate ticks on the background (ticks every 10ms)
  101 #if F_OSC == 16000000
  102       PR6 = 250 - 1;
  103       T6CON = 0b0.1001.1.10;    // Prescaler 16, Postscaler 10, 16 * 10 * 250 = 40000 = 4MHz * 10ms
  104 #else
  105 #error Unsupported oscillator frequency
  106 #endif
  107 
  108       TMR6IE = TRUE;
  109     }
  110     break;
  111 
  112     // -------------------------------------------------
  113     case DpaEvent_AfterSleep:
  114       // Called on wake-up from sleep
  115       TMR6IE = TRUE;
  116       TMR6ON = TRUE;
  117       break;
  118 
  119       // -------------------------------------------------
  120     case DpaEvent_BeforeSleep:
  121       // Called before going to sleep   (the same handling as DpaEvent_DisableInterrupts event)
  122       // To minimize the power consumption, no MCU pin must be left as a digital input without defined input level value.
  123       //   So, unused pins in given hardware should be set as outputs:
  124                                 // Generated by IQRF IDE according to the TR selection in a project
  125 #if defined(TR72D)
  126                                 // TR pin C1 (MCU pin RA0): should be set as an output
  127       LATA.0 = 0;                   // Low level
  128       TRISA.0 = 0;                  // Output
  129 
  130                                 // TR pin C2 (MCU pin RC2): should be set as an output
  131       LATC.2 = 0;                   // Low level
  132       TRISC.2 = 0;                  // Output
  133 
  134                                 // TR pin C5 (MCU pins RA5, RB4 and RC6 connected in parallel):
  135                                 //   All MCU pins can be set as an input, but pin RB4 must be configured with internal pull-up (default activated).
  136       TRISA.5 = 1;                  // Input
  137       TRISB.4 = 1;                  // Input
  138       TRISC.6 = 1;                  // Input
  139 
  140                                 // TR pin C6 (MCU pin RC3): should be set as an output
  141       LATC.3 = 0;                   // Low level
  142       TRISC.3 = 0;                  // Output
  143 
  144                                 // TR pin C7 (MCU pin RC4): should be set as an output
  145       LATC.4 = 0;                   // Low level
  146       TRISC.4 = 0;                  // Output
  147 
  148                                 // TR pin C8 (MCU pins RC5 and RC7 connected in parallel):
  149                                 //   Only one MCU pin should be set as an output
  150       LATC.5 = 0;                   // Low level
  151       TRISC.5 = 0;                  // Output
  152       TRISC.7 = 1;                  // Input
  153 
  154 #elif defined(TR76D)
  155                                 // TR pin Q14 (MCU pin RA0): should be set as an output
  156       LATA.0 = 0;                   // Low level
  157       TRISA.0 = 0;                  // Output
  158 
  159                                 // TR pin Q15 (MCU pin RC2): should be set as an output
  160       LATC.2 = 0;                   // Low level
  161       TRISC.2 = 0;                  // Output
  162 
  163                                 // TR pin Q4 (MCU pin RC6): should be set as an output
  164       LATC.6 = 0;                   // Low level
  165       TRISC.6 = 0;                  // Output
  166 
  167                                 // TR pin Q5 (MCU pin RC7): should be set as an output
  168       LATC.7 = 0;                   // Low level
  169       TRISC.7 = 0;                  // Output
  170 
  171                                 // TR pin Q6 (MCU pin RC3): should be set as an output
  172       LATC.3 = 0;                   // Low level
  173       TRISC.3 = 0;                  // Output
  174 
  175                                 // TR pin Q7 (MCU pin RC4): should be set as an output
  176       LATC.4 = 0;                   // Low level
  177       TRISC.4 = 0;                  // Output
  178 
  179                                 // TR pin Q8 (MCU pin RC5): should be set as an output
  180       LATC.5 = 0;                   // Low level
  181       TRISC.5 = 0;                  // Output
  182 
  183                                 // TR pin Q9 (MCU pin RA5): should be set as an output
  184       LATA.5 = 0;                   // Low level
  185       TRISA.5 = 0;                  // Output
  186 
  187                                 // TR LED pins Q10 and Q11 (MCU pins RB7 and RA2) are set as outputs by OS.
  188                                 // TR pin Q12 (MCU pin RB4) is set as an input with internal pull-up activated as default.
  189 #else
  190       #warning Unsupported TR module selected.Modify the pin setting.
  191 #endif
  192         // -------------------------------------------------
  193     case DpaEvent_DisableInterrupts:
  194       // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond, and Run RFPGM)
  195       // Must not use TMR6 any more
  196       TMR6ON = FALSE;
  197       TMR6IE = FALSE;
  198       break;
  199   }
  200 
  201   return FALSE;
  202 }
  203 //############################################################################################
  204 // 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)
  205 #include "DPAcustomHandler.h"
  206 //############################################################################################