1 // *********************************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation - SPI slave  *
    3 // *********************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserPeripheral-SPIslave.c,v $
    7 // Version: $Revision: 1.7 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2020/09/03  Release for DPA 4.15
   12 //
   13 // *********************************************************************
   14 
   15 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   16 
   17 // This example implements Slave SPI in STD mode as a user peripheral the standard way it was implemented as embedded peripheral in the prior DPA 4.15
   18 // The implementation uses the structure TPerUartWriteRead_Request as it was in the past.
   19 
   20 // Default IQRF include (modify the path according to your setup)
   21 #include "IQRF.h"
   22 
   23 // Default DPA header (modify the path according to your setup)
   24 #include "DPA.h"
   25 // Default Custom DPA Handler header (modify the path according to your setup)
   26 #include "DPAcustomHandler.h"
   27 
   28 //############################################################################################
   29 
   30 // Defines some previously defined symbols
   31 #define PNUM_SPI                        0x08
   32 #define CMD_SPI_WRITE_READ              0
   33 #define PERIPHERAL_SPI_MAX_DATA_LENGTH  sizeof( _DpaMessage.PerUartWriteRead_Request.WrittenData )
   34 
   35 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   36 //############################################################################################
   37 bit CustomDpaHandler()
   38 //############################################################################################
   39 {
   40 #pragma updateBank default = UserBank_01
   41 
   42   // Handler presence mark
   43   clrwdt();
   44 
   45   // Detect DPA event to handle
   46   switch ( GetDpaEvent() )
   47   {
   48     // -------------------------------------------------
   49     case DpaEvent_Interrupt:
   50       // Do an extra quick background interrupt work
   51       // ! 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.
   52       // ! 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.
   53       // ! 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.
   54       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   55       // ! Make sure race condition does not occur when accessing those variables at other places.
   56       // ! 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.
   57       // ! Do not call any OS functions except setINDFx().
   58       // ! Do not use any OS variables especially for writing access.
   59       // ! 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.
   60       return Carry;
   61 
   62       // -------------------------------------------------
   63     case DpaEvent_Init:
   64       // Do a one time initialization before main loop starts
   65     case DpaEvent_AfterSleep:
   66       // Called after woken up after sleep
   67 
   68       enableSPI();
   69       break;
   70 
   71       // -------------------------------------------------
   72     case DpaEvent_BeforeSleep:
   73       // Called before going to sleep
   74     case DpaEvent_DisableInterrupts:
   75       // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM)
   76 
   77       disableSPI();
   78       break;
   79 
   80       // -------------------------------------------------
   81     case DpaEvent_DpaRequest:
   82       // Called to interpret DPA request for peripherals
   83       // -------------------------------------------------
   84       // Peripheral enumeration
   85       if ( IsDpaEnumPeripheralsRequest() )
   86       {
   87         // We re-implement 1 embedded SPI peripheral even when it is not enabled in the configuration
   88         _DpaMessage.EnumPeripheralsAnswer.EmbeddedPers[PNUM_SPI / 8] |= 1 << ( PNUM_SPI % 8 );
   89 
   90         _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x571F;
   91 
   92 DpaHandleReturnTRUE:
   93         return TRUE;
   94       }
   95       // -------------------------------------------------
   96       // Get information about peripheral
   97       else if ( IsDpaPeripheralInfoRequest() )
   98       {
   99         if ( _PNUM == PNUM_SPI )
  100         {
  101           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_SPI;
  102           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_READ_WRITE;
  103           _DpaMessage.PeripheralInfoAnswer.Par1 = sizeof( _DpaMessage.PerUartWriteRead_Request.WrittenData );
  104           goto DpaHandleReturnTRUE;
  105         }
  106 
  107         break;
  108       }
  109       // -------------------------------------------------
  110       else
  111       {
  112         // Handle peripheral command
  113         if ( _PNUM == PNUM_SPI )
  114         {
  115           // Check DPA command value
  116           if ( _PCMD != CMD_SPI_WRITE_READ )
  117           {
  118             W = ERROR_PCMD;
  119 
  120 _DpaApiReturnPeripheralErrorW:
  121             DpaApiReturnPeripheralError( W );
  122           }
  123 
  124           // Check data length
  125           if ( _DpaDataLength < sizeof( _DpaMessage.PerUartWriteRead_Request.ReadTimeout ) )
  126           {
  127             W = ERROR_DATA_LEN;
  128             goto _DpaApiReturnPeripheralErrorW;
  129           }
  130 
  131           // Any data to write to SPI?
  132           if ( --_DpaDataLength != 0 )
  133           {
  134             // SPI write
  135             stopSPI();
  136             setFSR01( _FSR_RF, _FSR_COM );
  137             FSR0 += offsetof( TPerUartWriteRead_Request, WrittenData );
  138             copyMemoryBlock( FSR0, FSR1, _DpaDataLength );
  139             startSPI( _DpaDataLength );
  140           }
  141 
  142           // No data read yet
  143           _DpaDataLength = 0;
  144           // Any data to read?
  145           if ( _DpaMessage.PerUartWriteRead_Request.ReadTimeout != 0xff )
  146           {
  147             // Wait specified time before reading
  148             while ( --_DpaMessage.PerUartWriteRead_Request.ReadTimeout != (uns8)-1 )
  149               waitMS( 10 );
  150 
  151             // SPI read
  152             if ( !getStatusSPI() && _SPIRX )
  153             {
  154               if ( _SPICRCok )
  155               {
  156                 setFSR01( _FSR_COM, _FSR_RF );
  157                 // Limit maximum data length
  158                 _DpaDataLength = SPIpacketLength;
  159                 if ( _DpaDataLength > PERIPHERAL_SPI_MAX_DATA_LENGTH )
  160                   _DpaDataLength = PERIPHERAL_SPI_MAX_DATA_LENGTH;
  161 
  162                 copyMemoryBlock( FSR0, FSR1, _DpaDataLength );
  163               }
  164 
  165               startSPI( 0 );
  166             }
  167           }
  168 
  169           goto DpaHandleReturnTRUE;
  170         }
  171       }
  172   }
  173 
  174   return FALSE;
  175 }
  176 
  177 //############################################################################################
  178 // 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)
  179 #include "DPAcustomHandler.h"
  180 //############################################################################################