1 // **************************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation - PWM *
    3 // **************************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserPeripheral-PWM.c,v $
    7 // Version: $Revision: 1.31 $
    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 //   2014/04/30  Release for DPA 2.00
   17 //
   18 // *********************************************************************
   19 
   20 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   21 
   22 // This example implements the user peripheral PWM having the standard PWM peripheral from demo DPA behavior
   23 // This example works only at STD mode, not at LP mode
   24 
   25 // Default IQRF include (modify the path according to your setup)
   26 #include "IQRF.h"
   27 
   28 // Default DPA header (modify the path according to your setup)
   29 #include "DPA.h"
   30 // Default Custom DPA Handler header (modify the path according to your setup)
   31 #include "DPAcustomHandler.h"
   32 
   33 // Structure for CMD_PWM_SET
   34 typedef struct
   35 {
   36   uns8  Prescaler;
   37   uns8  Period;
   38   uns8  Duty;
   39 } STRUCTATTR TPerPwmSet_Request;
   40 
   41 TPerPwmSet_Request PerPwmSet_Request @ _DpaMessage.Request.PData;
   42 
   43 //############################################################################################
   44 
   45 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   46 //############################################################################################
   47 bit CustomDpaHandler()
   48 //############################################################################################
   49 {
   50   // Handler presence mark
   51   clrwdt();
   52 
   53   // Detect DPA event to handle
   54   switch ( GetDpaEvent() )
   55   {
   56     // -------------------------------------------------
   57     case DpaEvent_Interrupt:
   58       // Do an extra quick background interrupt work
   59       // ! 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.
   60       // ! 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.
   61       // ! 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.
   62       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   63       // ! Make sure race condition does not occur when accessing those variables at other places.
   64       // ! 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.
   65       // ! Do not call any OS functions except setINDFx().
   66       // ! Do not use any OS variables especially for writing access.
   67       // ! 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.
   68 
   69 DpaHandleReturnTRUE:
   70       return TRUE;
   71 
   72       // -------------------------------------------------
   73     case DpaEvent_DpaRequest:
   74       // Called to interpret DPA request for peripherals
   75       // -------------------------------------------------
   76       // Peripheral enumeration
   77       if ( IsDpaEnumPeripheralsRequest() )
   78       {
   79         // We implement 1 user peripheral
   80         _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 1;
   81         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
   82         _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F;
   83         _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0xabcd;
   84 
   85         goto DpaHandleReturnTRUE;
   86       }
   87       // -------------------------------------------------
   88       // Get information about peripheral
   89       else if ( IsDpaPeripheralInfoRequest() )
   90       {
   91         if ( _PNUM == PNUM_USER + 0 )
   92         {
   93           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_PWM;
   94           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_WRITE;
   95           goto DpaHandleReturnTRUE;
   96         }
   97 
   98         break;
   99       }
  100       // -------------------------------------------------
  101       else
  102       {
  103         // Handle peripheral command
  104         if ( _PNUM == PNUM_USER + 0 )
  105         {
  106           // Check command
  107           if ( _PCMD != 0 )
  108             DpaApiReturnPeripheralError( ERROR_PCMD );
  109 
  110           // Check data length
  111           if ( _DpaDataLength != sizeof( PerPwmSet_Request ) )
  112             DpaApiReturnPeripheralError( ERROR_DATA_LEN );
  113 
  114           // Read module info into bufferINFO
  115           moduleInfo();
  116 
  117           // We use FSR0 to access PerPwmSet structure and to avoid to many MOVLB MCU instructions
  118           FSR0 = (uns16)&PerPwmSet_Request;
  119 
  120 #define _Prescaler  FSR0[offsetof(TPerPwmSet_Request,Prescaler)]
  121 #define _Period     FSR0[offsetof(TPerPwmSet_Request,Period)]
  122 #define _Duty       FSR0[offsetof(TPerPwmSet_Request,Duty)]
  123 
  124           // prescaler has to be in range 0 - 3
  125           // 2 LSbs of duty cycle is coded into 4 and 5 bit of prescaler 0b00xx0000
  126           if ( ( 0x03 < _Prescaler ) && ( 0x03 < ( _Prescaler >> 4 ) ) )
  127             DpaApiReturnPeripheralError( ERROR_DATA );
  128 
  129           // Definitions used for TR72 having connected pins
  130 #define _OUT_A5 TRISA.5
  131 #define _OUT_B4 TRISB.4
  132 #define _OUT_C6 TRISC.6
  133 #define _PIN_C6 LATC.6
  134 
  135           // PWM setup
  136           if ( 0x00 == _Prescaler && 0x00 == _Period && 0x00 == _Duty )
  137           {
  138             // Stop PWM
  139             _PIN_C6 = 0;
  140 
  141             T6CON = 0;
  142             CCPTMRS0 = 0;
  143             CCP3CON = 0;
  144           }
  145           else
  146           { // Start PWM
  147             // TR module with connected pins?
  148             if ( bufferINFO[5].7 == 0 )
  149             {
  150               _OUT_A5 = 1;
  151               _OUT_B4 = 1;
  152             }
  153 
  154             // PWM duty cycle
  155             CCPR3L = _Duty;
  156             // Change duty cycle only
  157             CCP3CON = 0b00001100 | ( _Prescaler & 0b00110000 );
  158 
  159             if ( _Period != PR6 || ( ( _Prescaler ^ T6CON ) & 0b00000011 ) != 0 )
  160             {
  161               // Prescaler
  162               T6CON = _Prescaler & 0b00000011;
  163 
  164               // Set timer6 as CCP3 timer
  165               CCPTMRS0 = 0b00100000;
  166               // CCP3 single mode coded from prescaler byte
  167               // Select RC6 as P3A function output
  168               CCP3SEL = 0;
  169 
  170               PR6 = _Period;        // PWM period
  171 
  172               TMR6IF = 0;
  173               TMR6ON = 1;
  174               // To send a complete duty cycle and period on the first PWM
  175               while ( !TMR6IF );
  176             }
  177 
  178             _OUT_C6 = 0;
  179           }
  180 
  181 WriteNoError:
  182           _DpaDataLength = 0;
  183           goto DpaHandleReturnTRUE;
  184         }
  185       }
  186   }
  187 
  188   return FALSE;
  189 }
  190 
  191 //############################################################################################
  192 // 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) 
  193 #include "DPAcustomHandler.h"
  194 //############################################################################################