void InitSPI(void){ if(initialized) return; initialized=TRUE; //printfDEBUG("Initializing SPI interface"); mPORTGOpenDrainOpen(BIT_6);// Clock is output mPORTGOpenDrainOpen(BIT_8);// Data Out is an output OpenSPI2(SPI_MODE8_ON|ENABLE_SDO_PIN|SLAVE_ENABLE_OFF|SPI_CKE_ON|MASTER_ENABLE_ON|SEC_PRESCAL_8_1|PRI_PRESCAL_64_1, SPI_ENABLE); }
void InitSPI(void){ println_I("Initializing the SPI perpheral"); mPORTGOpenDrainOpen(BIT_6);// Clock is output mPORTGOpenDrainOpen(BIT_8);// Data Out is an output SPI_SCK_IO=1; SPI_SDO_IO=1; OpenSPI2(SPI_MODE8_ON|ENABLE_SDO_PIN|SLAVE_ENABLE_OFF|SPI_CKE_ON|MASTER_ENABLE_ON|SEC_PRESCAL_8_1|PRI_PRESCAL_64_1, SPI_ENABLE); println_I("Setting up SPI perpheral"); SetCoProcMode(0,IS_SPI_SCK); SetCoProcMode(1,IS_SPI_MISO); SetCoProcMode(2,IS_SPI_MOSI); }
/**************************************************************************** Function: bool DRV_SPI_Initialize(SpiChannel chn, SpiOpenFlags oFlags, unsigned int fpbDiv) Summary: This function initializes the SPI channel and also sets the brg register. Description: This function initializes the SPI channel and also sets the brg register. The SPI baudrate BR is given by: BR=Fpb/(2*(SPIBRG+1)) The input parametes fpbDiv specifies the Fpb divisor term (2*(SPIBRG+1)), so the BRG is calculated as SPIBRG=fpbDiv/2-1. Precondition: None Parameters: chn - the channel to set oFlags - a SpiOpenFlags or __SPIxCONbits_t structure that sets the module behavior fpbDiv - Fpb divisor to extract the baud rate: BR=Fpb/fpbDiv. Returns: true if success false otherwise Remarks: - The baud rate is always obtained by dividing the Fpb to an even number between 2 and 1024. - When selecting the number of bits per character, SPI_OPEN_MODE32 has the highest priority. If SPI_OPEN_MODE32 is not set, then SPI_OPEN_MODE16 selects the character width. - The SPI_OPEN_SSEN is taken into account even in master mode. If it is set the library will properly se the SS pin as an digital output. ***************************************************************************/ bool DRV_SPI_Initialize(SpiChannel chn, SpiOpenFlags oFlags, unsigned int fpbDiv) { #if defined (__C32__) SpiChnOpen(chn, oFlags, fpbDiv); #elif defined (__C30__) volatile uint16_t con1 = 0; uint16_t con2 = 0; uint16_t con3 = 0; uint8_t i; if((SYS_CLK_PeripheralClockGet()/fpbDiv) > 10000000ul) { SYS_ASSERT(false, "Requested SPI frequency is not supported!"); return false; // the SPI clock is selected more than 10MHz. // Select the frequency as per the data sheet of the particular 16bit device. } for(i = 0; i < SPI_CLK_TBL_ELEMENT_COUNT; i++) { if((SYS_CLK_PeripheralClockGet()/fpbDiv) <= SpiClkTbl[i].clock) { con1 = SpiClkTbl[i].scale; break; } } con1 |= oFlags; con3 |= SPI_EN; switch(chn) { case 1: OpenSPI1(con1,con2,con3); break; case 2: SPI2STAT &= 0x7FFF; OpenSPI2(con1,con2,con3); break; default: SYS_ASSERT(false, "Requested SPI channel is not supported!"); return false; } #endif return true; }
/* * I/O and SPI bus configuration */ void spieepromInit(void) { EE_nCS = 1; EE_nCS_TRIS = 0; #if defined(HEXA_BOARD_V1) #elif defined(MIWI_DEMO_KIT) SDI2_TRIS = 1; SDO2_TRIS = 0; SCK2_TRIS = 0; OpenSPI2(SPI_FOSC_4, MODE_00, SMPMID); SSP2STAT = 0x00; SSP2CON1 = 0x31; PIR3bits.SSP2IF = 0; #endif }