/*** Spi2PutByte ** ** Parameters: ** bVal - byte value to write ** ** Return Value: ** Returns byte read ** ** Errors: ** none ** ** Description: ** Write/Read a byte on SPI port 2 */ uint8_t Spi2PutByte(uint8_t bVal) { int status = 0; uint8_t bRx; SpiInstance.SlaveSelectReg = XSpi_GetSlaveSelectReg(&SpiInstance); status = XSpi_Transfer(&SpiInstance, &bVal, &bRx, sizeof(uint8_t) ); if (status != XST_SUCCESS) { printf("Spi2PutByte: ERROR: transfer failed. staus: %d", status); } //bRx = spiCon.transfer(bVal); return bRx; }
/*** OledPutBuffer ** ** Parameters: ** cb - number of bytes to send/receive ** rgbTx - pointer to the buffer to send ** ** Return Value: ** none ** ** Errors: ** none ** ** Description: ** Send the bytes specified in rgbTx to the slave and return ** the bytes read from the slave in rgbRx */ void OledPutBuffer(int cb, uint8_t *rgbTx) { int status = 0; uint8_t rgbRx[cb]; //dummy read buffer SpiInstance.SlaveSelectReg = XSpi_GetSlaveSelectReg(&SpiInstance); status = XSpi_Transfer(&SpiInstance, rgbTx, rgbRx, cb); if (status != XST_SUCCESS) { printf("OledPutBuffer: ERROR: transfer failed. staus: %d", status); } /* Write/Read the data */ /*for (ib = 0; ib < cb; ib++) { bTmp = spiCon.transfer(*rgbTx++); }*/ }
/** * * Runs a self-test on the driver/device. The self-test is destructive in that * a reset of the device is performed in order to check the reset values of * the registers and to get the device into a known state. A simple loopback * test is also performed to verify that transmit and receive are working * properly. The device is changed to master mode for the loopback test, since * only a master can initiate a data transfer. * * Upon successful return from the self-test, the device is reset. * * @param InstancePtr is a pointer to the XSpi instance to be worked on. * * @return * - XST_SUCCESS if successful. * - XST_REGISTER_ERROR indicates a register did not read or write * correctly. * - XST_LOOPBACK_ERROR if a loopback error occurred. * * @note None. * ******************************************************************************/ int XSpi_SelfTest(XSpi *InstancePtr) { int Result; u32 Register; Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); /* Return Success if XIP Mode */ if((InstancePtr->XipMode) == 1) { return XST_SUCCESS; } /* * Reset the SPI device to leave it in a known good state. */ XSpi_Reset(InstancePtr); if(InstancePtr->XipMode) { Register = XSpi_GetControlReg(InstancePtr); if (Register != XSP_CR_RESET_STATE) { return XST_REGISTER_ERROR; } Register = XSpi_GetStatusReg(InstancePtr); if ((Register & XSP_SR_RESET_STATE) != XSP_SR_RESET_STATE) { return XST_REGISTER_ERROR; } } /* * All the SPI registers should be in their default state right now. */ Register = XSpi_GetControlReg(InstancePtr); if (Register != XSP_CR_RESET_STATE) { return XST_REGISTER_ERROR; } Register = XSpi_GetStatusReg(InstancePtr); if ((Register & XSP_SR_RESET_STATE) != XSP_SR_RESET_STATE) { return XST_REGISTER_ERROR; } /* * Each supported slave select bit should be set to 1. */ Register = XSpi_GetSlaveSelectReg(InstancePtr); if (Register != InstancePtr->SlaveSelectMask) { return XST_REGISTER_ERROR; } /* * If configured with FIFOs, the occupancy values should be 0. */ if (InstancePtr->HasFifos) { Register = XSpi_ReadReg(InstancePtr->BaseAddr, XSP_TFO_OFFSET); if (Register != 0) { return XST_REGISTER_ERROR; } Register = XSpi_ReadReg(InstancePtr->BaseAddr, XSP_RFO_OFFSET); if (Register != 0) { return XST_REGISTER_ERROR; } } /* * Run loopback test only in case of standard SPI mode. */ if (InstancePtr->SpiMode != XSP_STANDARD_MODE) { return XST_SUCCESS; } /* * Run an internal loopback test on the SPI. */ Result = LoopbackTest(InstancePtr); if (Result != XST_SUCCESS) { return Result; } /* * Reset the SPI device to leave it in a known good state. */ XSpi_Reset(InstancePtr); return XST_SUCCESS; }
void OledHostInit() { XSpi_Config *SPIConfigPtr; XGpio_Config *GPIOConfigPtr; int status; /* * Initialize the SPI driver so that it is ready to use. */ SPIConfigPtr = XSpi_LookupConfig(XPAR_SPI_1_DEVICE_ID); if (SPIConfigPtr == NULL) { printf("OledHostInit: ERROR: SPI device not found"); } status = XSpi_CfgInitialize(&SpiInstance, SPIConfigPtr, SPIConfigPtr->BaseAddress); if (status != XST_SUCCESS) { printf("OledHostInit: ERROR: cannot init SPI"); } /* * Set the Spi device as a master. */ status = XSpi_SetOptions( &SpiInstance, XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION | XSP_CLK_ACTIVE_LOW_OPTION | XSP_CLK_PHASE_1_OPTION); if (status != XST_SUCCESS) { printf("OledHostInit: ERROR: set SPI options"); } /* * Start the SPI driver so that the device is enabled. */ XSpi_Start(&SpiInstance); /* * Disable Global interrupt to use polled mode operation */ XSpi_IntrGlobalDisable(&SpiInstance); u32 slaveReg = XSpi_GetSlaveSelectReg(&SpiInstance); XSpi_SetSlaveSelectReg(&SpiInstance, 0x00); slaveReg = XSpi_GetSlaveSelectReg(&SpiInstance); //GPIO config GPIOConfigPtr = XGpio_LookupConfig(XPAR_GPIO_0_DEVICE_ID); if (GPIOConfigPtr == NULL) { printf("OledHostInit: ERROR: GPIO device not found"); } status = XGpio_CfgInitialize(&gpioInstance, GPIOConfigPtr, GPIOConfigPtr->BaseAddress); if (status != XST_SUCCESS) { printf("OledHostInit: ERROR: cannot init GPIO"); } XGpio_SetDataDirection(&gpioInstance, 1, 0xf0); XGpio_DiscreteWrite(&gpioInstance, 1, 0x0F); }