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