1 /****************************************************************************
 2 
 3   Header file for template service
 4   based on the Gen 2 Events and Services Framework
 5 
 6  ****************************************************************************/
 7 
 8 #ifndef ServFollower_H
 9 #define ServFollower_H
10 
11 #include <xc.h>
12 #include "ES_Types.h"
13 #include "PIC32_SPI_HAL.h"
14 #include "PIC32PortHAL.h"
15 
16 #define SPI_MODULE SPI_SPI1
17 
18 // Public Function Prototypes
19 
20 bool InitFollowerService(uint8_t Priority);
21 bool PostFollowerService(ES_Event_t ThisEvent);
22 ES_Event_t RunFollowerService(ES_Event_t ThisEvent);
23 void PrintCmdDesc(uint8_t command);
24 
25 typedef enum {
26     InitFollower = 0,
27     ExpectCommand,
28     KeepCommand        
29 } FollowerState_t;
30 
31 static void InitSPI(void) {
32     SPISetup_BasicConfig(SPI_MODULE);
33     SPISetup_SetFollower(SPI_MODULE);
34 
35     SPISetup_MapSSInput(SPI_MODULE, SPI_RPA0); // 2, RA0
36     SPISetup_MapSDInput(SPI_MODULE, SPI_RPB8); // 17, RB8
37     SPISetup_MapSDOutput(SPI_MODULE, SPI_RPA2); // 9, RA2
38 
39     SPI1BUF; // Clear buffer
40 
41     SPI1STATbits.SPIROV = 0;
42     SPISetEnhancedBuffer(SPI_MODULE, true);
43     SPISetup_SetXferWidth(SPI_MODULE, SPI_8BIT);
44     SPISetup_SetActiveEdge(SPI_MODULE, SPI_FIRST_EDGE); // CKE = 0
45     SPISetup_SetClockIdleState(SPI_MODULE, SPI_CLK_LO); // CKP = 1
46     SPI1CONbits.SRXISEL = 0b01;
47 
48     IPC7bits.SPI1IP = 5;
49     IFS1CLR = _IFS1_SPI1RXIF_MASK;
50     IEC1SET = _IEC1_SPI1RXIE_MASK;
51 
52     SPISetup_EnableSPI(SPI_MODULE);
53 }
54 
55 #endif /* ServFollower_H */
56