/***************************************************************************** Function: void WF_SpiInit(void); Summary: Initializes SPI controller used to communicate with MRF24WG Description: Called by Universal Driver during initialization to initialize SPI interface. Parameters: None Returns: None Remarks: Should not be called directly from application code. *****************************************************************************/ void WF_SpiInit(void) { volatile uint8_t rxData; #if defined(__PIC32MZ__) WF_INT_PPS(); WF_HIB_PPS(); WF_RESET_PPS(); WF_CS_PPS(); WF_SCK_PPS(); WF_SDI_PPS(); WF_SDO_PPS(); #endif // The MRF24WG chip select line will be controlled using an I/O pin, not by // the SPI controller. WF_CS_IO = 1; // set the level high (chip select disabled) WF_CS_TRIS = 0; // Configure the I/O pin as an output and drive it // disable the three possible SPI interrupts (SP1 Fault, SP1 Rx done, // SPI1 Transfer Done). Universal Driver does not require SPI interrupts. // SPI_IECxCLR = 0x03800000; // WIFI_SPI_INT_EIE = 0; WIFI_SPI_INT_TXIE = 0; WIFI_SPI_INT_RXIE = 0; // disable SPI controller WIFI_SPICONbits.ON = 0; // clear the receive buffer rxData = WIFI_SPIBUF; (void) rxData; // prevent unsued warning // set the SPI baud rate to 10MHz // Fsck = Fpb / (2 * (SPI1BRG + 1) = 80MHz / (2 * (3 + 1)) = 80MHz / 8 = 10MHz WIFI_SPIBRG = max((((int) ((GetPeripheralClock() + WF_SPI_FREQ) / 2 / WF_SPI_FREQ)) - 1), 0); // configure SPI1: // * Master Mode enabled (MSTEN = 1) // * Clock Polarity is idle high, active low (CKP = 1) // * Serial output data changes on transition from idle clock state to active // clock state, or high to low transition (CKE = 0) // * Data sampled at end of output time (SMP = 1), on rising edge WIFI_SPICON = 0x00000260; // enable SPI controller WIFI_SPICONbits.ON = 1; }
/* * Initializes SPI controller used to communicate with MRF24WG. * Called by Universal Driver during initialization to initialize SPI interface. */ void WF_SpiInit() { uint8_t rxData __attribute__((unused)); #ifdef WF_INT_PPS WF_INT_PPS(); WF_HIBERNATE_PPS(); WF_RESET_PPS(); WF_CS_PPS(); WF_SCK_PPS(); WF_SDI_PPS(); WF_SDO_PPS(); #endif // The MRF24WG chip select line will be controlled using an I/O pin, not by // the SPI controller. WF_CS_SET(1); // set the level high (chip select disabled) WF_CS_INPUT(0); // Configure the I/O pin as an output and drive it // disable the three possible SPI interrupts (SPI Fault, SPI Rx done, // SPI Transfer Done). Universal Driver does not require SPI interrupts. IECCLR(WIFI_SPI_IRQ_E >> 5) = 1 << (WIFI_SPI_IRQ_E & 31); IECCLR(WIFI_SPI_IRQ_TX >> 5) = 1 << (WIFI_SPI_IRQ_TX & 31); IECCLR(WIFI_SPI_IRQ_RX >> 5) = 1 << (WIFI_SPI_IRQ_RX & 31); // disable SPI controller WIFI_SPICONCLR = PIC32_SPICON_ON; // clear the receive buffer rxData = WIFI_SPIBUF; // set the SPI baud rate WIFI_SPIBRG = ((BUS_FREQ + WF_SPI_FREQ) / 2 / WF_SPI_FREQ) - 1; // configure SPI port: // * Master Mode enabled (MSTEN = 1) // * Clock Polarity is idle high, active low (CKP = 1) // * Serial output data changes on transition from idle clock state to active // clock state, or high to low transition (CKE = 0) // * Data sampled at end of output time (SMP = 1), on rising edge WIFI_SPICON = PIC32_SPICON_MSTEN | PIC32_SPICON_CKP | PIC32_SPICON_SMP; // enable SPI controller WIFI_SPICONSET = PIC32_SPICON_ON; }