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