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