Example #1
0
void Mrf24j::write_short(uint8_t address, uint8_t data)
{
	uint8_t rgbRx = 0;
	uint8_t rgbTx = (address<<1 & 0b01111110) | 0x01;

    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x00);
    MSpiInstance.SlaveSelectReg = 0;
    MSpiInstance.IsStarted = XIL_COMPONENT_IS_STARTED;
    // 0 for top short address, 1 bottom for write
	XSpi_Transfer( &MSpiInstance, &rgbTx, &rgbRx, sizeof(uint8_t) );
    int status = XSpi_Transfer( &MSpiInstance, &data, &rgbRx, sizeof(uint8_t) );
    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x01);
}
Example #2
0
uint8_t Mrf24j::read_short(uint8_t address)
{
	uint8_t rgbRx = 0;
	uint8_t rgbTx = address<<1 & 0b01111110;
    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x00);

    MSpiInstance.IsStarted = XIL_COMPONENT_IS_STARTED;
    MSpiInstance.SlaveSelectReg = 0;
	XSpi_Transfer( &MSpiInstance, &rgbTx, &rgbRx, sizeof(uint8_t) ); //transmit address
	rgbTx = 0;
	XSpi_Transfer( &MSpiInstance, &rgbTx, &rgbRx, sizeof(uint8_t) ); //receive data

    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x01);
    return rgbRx;
}
Example #3
0
void spi_write(XSpi *SpiInstancePtr, u8 addr, u8 value) {
	u8 sendbuffer[2];
	sendbuffer[0] = (addr << 1) | 1;
	sendbuffer[1] = value;
	int bytecount = 2;
	XSpi_Transfer(SpiInstancePtr, sendbuffer, NULL, bytecount);
}
Example #4
0
void Mrf24j::write_long(word address, uint8_t data) 
{
	uint8_t rgbRx = 0;
    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x00);
    MSpiInstance.SlaveSelectReg = 0;
    uint8_t ahigh = address >> 3;
    uint8_t alow = address << 5;
    uint8_t rgbTx =  0x80 | ahigh;
    MSpiInstance.IsStarted = XIL_COMPONENT_IS_STARTED;
	XSpi_Transfer( &MSpiInstance, &rgbTx, &rgbRx, sizeof(uint8_t) );	 // high bit for long
	rgbTx = alow | 0x10;
	XSpi_Transfer( &MSpiInstance, &rgbTx, &rgbRx, sizeof(uint8_t) ); 	 // last bit for write
	XSpi_Transfer( &MSpiInstance, &data, &rgbRx, sizeof(uint8_t) ); 			 //write data

    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x01);
}
Example #5
0
static
void skip_mmc (
	WORD n		/* Number of bytes to skip */
)
{
#if AXI_SPI
	do {
		BYTE w = 0xff;
		//xil_printf("skip_mmc\r\n");
		XStatus Status = XSpi_Transfer(&Spi, &w, 0, 1);
	 	if(Status != XST_SUCCESS) {
			xil_printf("Error in skip transfer\r\n");
			xil_printf("Status = %d\r\n", Status);
		}
		//DLY_US(1);
	} while (--n);
#else
	DI_H();	/* Send 0xFF */

	do {
		CK_H(); CK_L();
		CK_H(); CK_L();
		CK_H(); CK_L();
		CK_H(); CK_L();
		CK_H(); CK_L();
		CK_H(); CK_L();
		CK_H(); CK_L();
		CK_H(); CK_L();
	} while (--n);
#endif
}
Example #6
0
u8 spi_read(XSpi *SpiInstancePtr, u8 addr){
	u8 RcvBuf[2];
	u8 SendBuf[2];
	SendBuf[0] = (addr << 1) + 1; // shifted for proper reading, also with write flag
	XSpi_Transfer(SpiInstancePtr, SendBuf, RcvBuf, 2);
	return RcvBuf[1];
}
Example #7
0
static
void xmit_mmc (
	BYTE d			/* Data to be sent */
)
{
#if AXI_SPI
	//xil_printf("xmit_mmc\r\n");
	XStatus Status = XSpi_Transfer(&Spi, &d, 0, 1);
 	if(Status != XST_SUCCESS) {
		xil_printf("Error in write transfer\r\n");
	}

	//DLY_US(1);
#else
	if (d & 0x80) DI_H(); else DI_L();	/* bit7 */
	CK_H(); CK_L();
	if (d & 0x40) DI_H(); else DI_L();	/* bit6 */
	CK_H(); CK_L();
	if (d & 0x20) DI_H(); else DI_L();	/* bit5 */
	CK_H(); CK_L();
	if (d & 0x10) DI_H(); else DI_L();	/* bit4 */
	CK_H(); CK_L();
	if (d & 0x08) DI_H(); else DI_L();	/* bit3 */
	CK_H(); CK_L();
	if (d & 0x04) DI_H(); else DI_L();	/* bit2 */
	CK_H(); CK_L();
	if (d & 0x02) DI_H(); else DI_L();	/* bit1 */
	CK_H(); CK_L();
	if (d & 0x01) DI_H(); else DI_L();	/* bit0 */
	CK_H(); CK_L();
#endif
}
/*****
 * PmodCtlSys_readADC() - read the ADC on the PmodCtlSys
 * 
 * This function performs a SPI read of the ADC on the PmodCtlSys.  It
 * returns a 12 bit ADC count giving the output voltage (e.g. the phototransistor)
 * of the control system.  This code is based on the Xilinx SPI
 * driver "spi_polled_example.c" example included in the EDK.  Uses the global
 * PmodCtlSys send and receive buffers.  the send buffer is configured by
 * PmodCtlSys_init() and does not change.
 *
 * NOTE:  The functions returns an ADC Count of PMODCTLSYS_ADC_ERROR (an illegal
 * count in a 12-bit ADC) if the SPI transfer fails
 *****/
u16 PmodCtlSys_readADC(XSpi *SpiInstancePtr)
{
	XStatus Status;				// return status from SPI driver functions
	u16 adc_cnt;				// The ADC count

	// Set the slave select mask.  This mask is used by the transfer command
	// to indicate which slave select line to enable
	Status = XSpi_SetSlaveSelect(SpiInstancePtr, PMODCTLSYS_SPI_SS);
	if (Status != XST_SUCCESS)
	{
		return PMODCTLSYS_ADC_ERROR;
	}

	// Transfer the command and receive the ADC count.  In polled mode
	// the transfer function blocks until the transfer is complete
	// We will send an receive all 3 bytes
	Status = XSpi_Transfer(SpiInstancePtr, PmodCtlSys_SndBuf, PmodCtlSys_RcvBuf, PMODCTLSYS_BUF_SIZE);
	if (Status != XST_SUCCESS)
	{
		return PMODCTLSYS_ADC_ERROR;
	}
		
	
	// SPI transfer was successful so we assume there is a valid ADC count in the buffer
	// The ADC count is returned in the following format:
	//      RcvBuf[0] = x   x   x   x   x   x   x   x
	//		RcvBuf[1] = x   x   x   0   B11 B10 B09 B08
	//		RcvBuf[2] = B07 B06 B05 B04 B03 B02 B01 B00
	adc_cnt = ((PmodCtlSys_RcvBuf[1] << 8) | (PmodCtlSys_RcvBuf[2] << 0)) & 0x0FFF;
	return adc_cnt;
}
Example #9
0
uint8_t Mrf24j::read_long(word address) 
{
	uint8_t rgbRx = 0;
    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x00);
    MSpiInstance.SlaveSelectReg = 0;
    uint8_t ahigh = address >> 3;
    uint8_t alow = address << 5;
    uint8_t rgbTx = 0x80 | ahigh;
    MSpiInstance.IsStarted = XIL_COMPONENT_IS_STARTED;
	XSpi_Transfer( &MSpiInstance, &rgbTx, &rgbRx, sizeof(uint8_t) );
	XSpi_Transfer( &MSpiInstance, &alow, &rgbRx, sizeof(uint8_t) );
	rgbTx = 0;
	XSpi_Transfer( &MSpiInstance, &rgbTx, &rgbRx, sizeof(uint8_t) ); //receive data

    XSpi_SetSlaveSelectReg(&MSpiInstance, 0x01);
    return rgbRx;
}
Example #10
0
u8 spi_read(XSpi *SpiInstancePtr, u8 addr) {
	u8 rcvbuf[2];
	int bytecount = 2;
	rcvbuf[0] = (addr << 1) | 1;
	XSpi_Transfer(SpiInstancePtr, rcvbuf, rcvbuf, bytecount);
	// TODO: Your code here!
	return rcvbuf[1];
}
Example #11
0
/***************************************************************************//**
 * @brief spi_read
*******************************************************************************/
int32_t spi_read(uint8_t *data,
				 uint8_t bytes_number)
{
	uint32_t cnt		 = 0;
#ifdef _XPARAMETERS_PS_H_
	uint32_t base_addr	 = 0;
	uint32_t control_val = 0;
	uint32_t status	  	 = 0;

	base_addr = spi_config->BaseAddress;
	control_val = XSpiPs_ReadReg(base_addr, XSPIPS_CR_OFFSET);

	XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET,
					control_val & ~(1 << XSPIPS_CR_SSCTRL_SHIFT));

	XSpiPs_WriteReg(base_addr, XSPIPS_TXWR_OFFSET, 0x01);

	XSpiPs_WriteReg(base_addr, XSPIPS_SR_OFFSET, XSPIPS_IXR_TXOW_MASK);
	XSpiPs_WriteReg(base_addr, XSPIPS_IER_OFFSET, XSPIPS_IXR_TXOW_MASK);

	while(cnt < bytes_number)
	{
		XSpiPs_WriteReg(base_addr, XSPIPS_TXD_OFFSET, data[cnt]);
		cnt++;
	}

	XSpiPs_WriteReg(base_addr, XSPIPS_ER_OFFSET, XSPIPS_ER_ENABLE_MASK);

	do
	{
		status = XSpiPs_ReadReg(base_addr, XSPIPS_SR_OFFSET);
	}
	while((status & XSPIPS_IXR_TXOW_MASK) == 0x0);

	XSpiPs_WriteReg(base_addr, XSPIPS_SR_OFFSET, XSPIPS_IXR_TXOW_MASK);

	XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET, control_val);

	cnt = 0;
	while(cnt < bytes_number)
	{
		data[cnt] = XSpiPs_ReadReg(base_addr, XSPIPS_RXD_OFFSET);
		cnt++;
	}

	XSpiPs_WriteReg(base_addr, XSPIPS_ER_OFFSET, 0x0);
#else
	uint8_t send_buffer[20];

	for(cnt = 0; cnt < bytes_number; cnt++)
	{
		send_buffer[cnt] = data[cnt];
	}

	XSpi_Transfer(&spi_instance, send_buffer, data, bytes_number);
#endif
	return SUCCESS;
}
Example #12
0
void LED_WrDat(u8 data)
{

	XGpio_DiscreteWrite(&dc, DC_CHANNEL, 0x1);

	XSpi_Transfer(&Spi, &data, 0, sizeof(data));
	//oled_SPI->RWOneByte(oled_SPI, data, 0);

	}
Example #13
0
/***	void AD1_readData(PmodAD1* InstancePtr)
**
**	Parameters:
**		InstancePtr: A PmodAD1 object to start
**
**
**	Return Value:
**		none
**
**	Errors:
**		none
**
**	Description:
**		this function reads data into PmodAD1* InstancePtr->data using SPI
*/
void AD1_readData(PmodAD1* InstancePtr){
	u8 byte[4];
	InstancePtr->data = 0;
	XSpi_Transfer(&InstancePtr->AD1Spi, byte, byte, 4);

	// convert the byte array to an int
	InstancePtr->data |= byte[0];
	InstancePtr->data = InstancePtr->data << 8;
	InstancePtr->data |= byte[1];


}
/***	ACL_WriteSpi
**
**	Synopsis:
**		ACL_WriteSpi(&ACLobj, 0x3A, &bytearray, 5);
**
**	Parameters:
**		PmodACL *InstancePtr	- the PmodACL object to communicate with
**		u8 reg			- the starting register to write to
**		u8* wData		- the data to write
**		int nData		- the number of data bytes to write
**
**	Return Value:
**		none
**
**	Errors:
**		none
**
**	Description:
**		Writes the byte array to the chip via SPI. It will write the first byte into the specified register, then the next into the following register until all of the data has been sent.

*/
void ACL_WriteSpi(PmodACL* InstancePtr, uint8_t reg, uint8_t *wData, int nData)
{
	// As requested by documentation, first byte contains:
	//	bit 7 = 0 because is a write operation
	//	bit 6 = 1 if more than one bytes is written, 0 if a single byte is written
	// 	bits 5-0 - the address
	u8 bytearray[nData+1];
	bytearray[0] = ((nData>1) ? 0x40: 0) | (reg&0x3F);
	memcpy(&bytearray[1],wData, nData);//Copy write commands over to bytearray
	XSpi_Transfer(&InstancePtr->ACLSpi, bytearray, 0, nData+1);

}
/***	ACL_ReadSpi
**
**	Synopsis:
**		ACL_ReadSpi(&ACLobj, 0x3A, &bytearray, 5);
**
**	Parameters:
**		PmodACL *InstancePtr	- the PmodACL object to communicate with
**		u8 reg			- the starting register to read from
**		u8* rData		- the byte array to read into
**		int nData		- the number of data bytes to read
**
**	Return Value:
**		none
**
**	Errors:
**		none
**
**	Description:
**		Reads data in through SPI. It will read the first byte from the starting register, then the next from the following register. Data is stored into rData.
*/
void ACL_ReadSpi(PmodACL* InstancePtr, uint8_t reg, uint8_t *rData, int nData)
{
	// As requested by documentation, first byte contains:
	//	bit 7 = 1 because is a read operation
	//	bit 6 = 1 if more than one bytes is written, 0 if a single byte is written
	// 	bits 5-0 - the address
	u8 bytearray[nData+1];

	bytearray[0] = ((nData>1) ? 0xC0: 0x80) | (reg&0x3F);
	XSpi_Transfer(&InstancePtr->ACLSpi, bytearray, bytearray, nData+1);
	memcpy(rData,&bytearray[1], nData);
}
Example #16
0
void LED_WrCmd(u8 cmd)
{

	XGpio_DiscreteWrite(&dc, DC_CHANNEL, 0x0);
	XSpi_Transfer(&Spi, &cmd, 0, sizeof(cmd));

	XGpio_DiscreteWrite(&led1, reset_CHANNEL, 0x1);
	//oled_gpio->digitalWrite(oled_gpio, 0, OLED_DC_PIN, 0);

	//oled_SPI->RWOneByte(oled_SPI, cmd, 0);

}
Example #17
0
/******************************************************************************
 * This function displays the pixel buffer, pointed to with psScreenBuf, onto
 * the OLED display.
 *
 * @param	psSpi is the pointer to the SPI driver structure.
 * @param	psGpio is the pointer to the GPIO driver structure.
 * @param	pchScreenBuf is the pointer to the pixel buffer to display on the
 * 			OLED screen.
 *
 * @return	XST_SUCCESS - Everything went well
 * 			XST_FAILURE - Failure
 *****************************************************************************/
XStatus fnOledPixelToDisplay(XSpi *psSpi, XGpio *psGpio,
		const sPixmap_t *psScreenBuf) {

	XStatus Status;
	u32 iPg;
	u8 rgbWrBuf[10];
	u8 u8LowerStartColumn = 0x00;
	u8 u8UpperStartColumn = 0x10;

	// Going through each character line (4)
	for(iPg = 0; iPg < OLED_MAX_PG_CNT; iPg++) {

		rgbWrBuf[0] = OLED_SET_PG_ADDR_CMD;
		rgbWrBuf[1] = iPg;
		rgbWrBuf[2] = u8LowerStartColumn;
		rgbWrBuf[3] = u8UpperStartColumn;

		XGpio_DiscreteClear(psGpio, 1, OLED_DC_MASK);

		u8TransferInProg = TRUE;
		Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 4);
		if(Status != XST_SUCCESS) {
			return Status;
		}
		while(u8TransferInProg);

		XGpio_DiscreteSet(psGpio, 1, OLED_DC_MASK);

		// Writing the line to the OLED
		u8TransferInProg = TRUE;
		Status = XSpi_Transfer(psSpi, (u8 *)(psScreenBuf->rgbPixmap +
				(iPg * OLED_CONTROLLER_PG_SZ)), NULL, OLED_CONTROLLER_PG_SZ);
		if(Status != XST_SUCCESS) {
			return Status;
		}
		while(u8TransferInProg);
	}

	return XST_SUCCESS;
}
Example #18
0
/******************************************************************************
* @brief Reads from an ADXL362 Register.
*
* @param addr - register address.
*
* @return rx - data read from register.
******************************************************************************/
u8  ADXL362_ReadReg(XSpi *SpiInstancePtr, char addr)
{
	tx_buffer[0] = 0x0B;
	tx_buffer[1] = addr;
	tx_buffer[2] = 0x00;

	rx_buffer[0] = 0x00;
	rx_buffer[1] = 0x00;
	rx_buffer[2] = 0x00;

	XSpi_Transfer(SpiInstancePtr, tx_buffer, rx_buffer, 3);

	return rx_buffer[2];
}
Example #19
0
/***	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;
}
Example #20
0
void ADXL362_WriteReg(XSpi *SpiInstancePtr, char addr, char data)
{


	tx_buffer[0] = 0x0A;
	tx_buffer[1] = addr;
	tx_buffer[2] = data;

	rx_buffer[0] = 0x00;
	rx_buffer[1] = 0x00;
	rx_buffer[2] = 0x00;


	XSpi_Transfer(SpiInstancePtr, tx_buffer, rx_buffer, 3);


}
Example #21
0
/*        DACSPI1::WriteIntegerValue
**
**        Synopsis:
**				WriteIntegerValue(0x00FF);
**        Parameters:
**				- uint8_t bIntegerValue - the 8 bits value to be written to DA converter
**              - InstancePtr: A PmodDA1 object to start
**        Return Value:
**                uint8_t
**					- DACSPI1_ERR_SUCCESS (0) 				- The action completed successfully
**					- DACSPI1_ERR_VAL_OUT_OF_RANGE	(1) 	- The value is not within the 0 - 0xFF range
**
**        Errors:
**			If module is not initialized (using begin), the function does nothing. Also, see return value.
**
**        Description:
**			If the value is inside the allowed range (0 - 0xFF), this function writes the 12 bits value to the DA converter, by writing 16 bits to SPI and returns the DACSPI1_ERR_SUCCESS status message.
**			If the value is outside the allowed range, the function does nothing and returns the DACSPI1_ERR_VAL_OUT_OF_RANGE message.
**
**
*/
u8 DA1_WriteIntegerValue(u8 bIntegerValue, PmodDA1* InstancePtr )
{
	u8 recv[2];
     recv[0]= DACSPI1_CTRL_BYTE;
     recv[1]= bIntegerValue;

	u8 bResult = 0;
	if(bIntegerValue < 0 || bIntegerValue >= (1 << DACSPI1_NO_BITS))
		{
			bResult = DACSPI1_ERR_VAL_OUT_OF_RANGE;
		}
		else
		{
			//xil_printf(" made it here, %d , yes i did %x   " , recv[1], recv[1]);

			XSpi_Transfer(&InstancePtr->DA1Spi, recv, recv, 2);
		}
	return bResult;
}
Example #22
0
/***	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++);
	}*/

}
Example #23
0
static
BYTE rcvr_mmc (void)
{
	BYTE r;

#if AXI_SPI
	//xil_printf("rcvr_mmc\r\n");
	BYTE w = 0xff;
	XStatus Status = XSpi_Transfer(&Spi, &w, &r, 1);
 	if(Status != XST_SUCCESS) {
		xil_printf("Error in read transfer\r\n");
		return XST_FAILURE;
	}

	//DLY_US(1);
#else
	DI_H();	/* Send 0xFF */

	r = 0;   if (DO) r++;	/* bit7 */
	CK_H(); CK_L();
	r <<= 1; if (DO) r++;	/* bit6 */
	CK_H(); CK_L();
	r <<= 1; if (DO) r++;	/* bit5 */
	CK_H(); CK_L();
	r <<= 1; if (DO) r++;	/* bit4 */
	CK_H(); CK_L();
	r <<= 1; if (DO) r++;	/* bit3 */
	CK_H(); CK_L();
	r <<= 1; if (DO) r++;	/* bit2 */
	CK_H(); CK_L();
	r <<= 1; if (DO) r++;	/* bit1 */
	CK_H(); CK_L();
	r <<= 1; if (DO) r++;	/* bit0 */
	CK_H(); CK_L();
#endif

	return r;
}
Example #24
0
/***************************************************************************//**
 * @brief spi_read
*******************************************************************************/
int32_t spi_read(struct spi_device *spi,
				 uint8_t *data,
				 uint8_t bytes_number)
{
#ifdef _XPARAMETERS_PS_H_
	XSpiPs_SetSlaveSelect(&spi_instance, (spi->id_no == 0 ? 0 : 1));

	XSpiPs_PolledTransfer(&spi_instance, data, data, bytes_number);
#else
	uint32_t cnt = 0;
#ifdef XPAR_AXI_SPI_0_DEVICE_ID
	uint8_t send_buffer[20];

	for(cnt = 0; cnt < bytes_number; cnt++)
	{
		send_buffer[cnt] = data[cnt];
	}

	XSpi_Transfer(&spi_instance, send_buffer, data, bytes_number);
#else
	Xil_Out32((spi_instance.BaseAddr + 0x60), 0x1e6);
	Xil_Out32((spi_instance.BaseAddr + 0x70), 0x000);
	while(cnt < bytes_number)
	{
		Xil_Out32((spi_instance.BaseAddr + 0x68), data[cnt]);
		Xil_Out32((spi_instance.BaseAddr + 0x60), 0x096);
		do {usleep(100);}
		while ((Xil_In32((spi_instance.BaseAddr + 0x64)) & 0x4) == 0x0);
		Xil_Out32((spi_instance.BaseAddr + 0x60), 0x186);
		data[cnt] = Xil_In32(spi_instance.BaseAddr + 0x6c);
		cnt++;
	}
	Xil_Out32((spi_instance.BaseAddr + 0x70), 0x001);
	Xil_Out32((spi_instance.BaseAddr + 0x60), 0x180);
#endif
#endif
	return SUCCESS;
}
Example #25
0
/***************************************************************************//**
* @brief spi_write_and_read
*******************************************************************************/
int32_t spi_write_and_read(uint8_t ss,
						   uint8_t *data,
						   uint8_t bytes_number)
{
#ifdef _XPARAMETERS_PS_H_
	XSpiPs_SetSlaveSelect(&spi_instance, ss);

	XSpiPs_PolledTransfer(&spi_instance, data, data, bytes_number);
#else
	uint8_t	 send_buffer[20];
	uint32_t cnt = 0;

	ss = (1 << ss);
	XSpi_SetSlaveSelect(&spi_instance, ss);

	for(cnt = 0; cnt < bytes_number; cnt++)
	{
		send_buffer[cnt] = data[cnt];
	}
	XSpi_Transfer(&spi_instance, send_buffer, data, bytes_number);
#endif

	return 0;
}
Example #26
0
void spi_write(XSpi *SpiInstancePtr, u8 addr, u8 value){
	u8 SendBuf[2];
	SendBuf[0] = addr << 1; // shifted for proper reading
	SendBuf[1] = value;
	XSpi_Transfer(SpiInstancePtr, SendBuf, NULL, 2);
}
Example #27
0
/***************************************************************************//**
 * @brief spi_read
*******************************************************************************/
int32_t spi_read(struct spi_device *spi,
				 uint8_t *data,
				 uint8_t bytes_number)
{
	uint32_t cnt		 = 0;
#ifdef _XPARAMETERS_PS_H_
	uint32_t base_addr	 = 0;
	uint32_t control_val = 0;
	uint32_t status	  	 = 0;

	base_addr = spi_config->BaseAddress;
	control_val = XSpiPs_ReadReg(base_addr, XSPIPS_CR_OFFSET);

	XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET,
					control_val & ~((spi->id_no == 0 ? 1 : 2) << XSPIPS_CR_SSCTRL_SHIFT));

	XSpiPs_WriteReg(base_addr, XSPIPS_TXWR_OFFSET, 0x01);

	XSpiPs_WriteReg(base_addr, XSPIPS_SR_OFFSET, XSPIPS_IXR_TXOW_MASK);
	XSpiPs_WriteReg(base_addr, XSPIPS_IER_OFFSET, XSPIPS_IXR_TXOW_MASK);

	while(cnt < bytes_number)
	{
		XSpiPs_WriteReg(base_addr, XSPIPS_TXD_OFFSET, data[cnt]);
		cnt++;
	}

	XSpiPs_WriteReg(base_addr, XSPIPS_ER_OFFSET, XSPIPS_ER_ENABLE_MASK);

	do
	{
		status = XSpiPs_ReadReg(base_addr, XSPIPS_SR_OFFSET);
	}
	while((status & XSPIPS_IXR_TXOW_MASK) == 0x0);

	XSpiPs_WriteReg(base_addr, XSPIPS_SR_OFFSET, XSPIPS_IXR_TXOW_MASK);

	XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET, control_val);

	cnt = 0;
	while(cnt < bytes_number)
	{
		data[cnt] = XSpiPs_ReadReg(base_addr, XSPIPS_RXD_OFFSET);
		cnt++;
	}

	XSpiPs_WriteReg(base_addr, XSPIPS_ER_OFFSET, 0x0);
#else
#ifdef XPAR_AXI_SPI_0_DEVICE_ID
	uint8_t send_buffer[20];

	for(cnt = 0; cnt < bytes_number; cnt++)
	{
		send_buffer[cnt] = data[cnt];
	}

	XSpi_Transfer(&spi_instance, send_buffer, data, bytes_number);
#else
	Xil_Out32((spi_instance.BaseAddr + 0x60), 0x1e6);
	Xil_Out32((spi_instance.BaseAddr + 0x70), 0x000);
	while(cnt < bytes_number)
	{
		Xil_Out32((spi_instance.BaseAddr + 0x68), data[cnt]);
		Xil_Out32((spi_instance.BaseAddr + 0x60), 0x096);
		do {usleep(100);}
		while ((Xil_In32((spi_instance.BaseAddr + 0x64)) & 0x4) == 0x0);
		Xil_Out32((spi_instance.BaseAddr + 0x60), 0x186);
		data[cnt] = Xil_In32(spi_instance.BaseAddr + 0x6c);
		cnt++;
	}
	Xil_Out32((spi_instance.BaseAddr + 0x70), 0x001);
	Xil_Out32((spi_instance.BaseAddr + 0x60), 0x180);
#endif
#endif
	return SUCCESS;
}
Example #28
0
void DoSpiTransfer(u8* tx, u8* rx, u16 nbytes) {
  XSpi_Transfer(&SpiInstance, tx, rx, nbytes);
}
Example #29
0
/******************************************************************************
 * This function initializes the OLED display with the proper procedure.
 *
 * @param	psSpi is the pointer to the SPI driver structure.
 * @param	psGpio is the pointer to the GPIO driver structure.
 *
 * @return	XST_SUCCESS - Everything went well
 * 			XST_FAILURE - Failure
 *
 * @note	This is how the GPIO is mapped to the OLED controller:
 *				gpio(0) = vbat
 *				gpio(1) = vdd
 *				gpio(2) = res
 *				gpio(3) = dc
 *****************************************************************************/
XStatus fnOledDisplayInit(XSpi *psSpi, XGpio *psGpio) {

	XStatus Status;
	u8 rgbWrBuf[10];

	// Clear the data/cmd bit
	XGpio_DiscreteClear(psGpio, 1, OLED_DC_MASK);

	// Start by turning VDD on and wait for the power to come up
	XGpio_DiscreteClear(psGpio, 1, OLED_VDD_MASK);
	MB_Sleep(1);

	// Send to SPI the display off command
	rgbWrBuf[0] = OLED_DISPLAY_OFF_CMD;
	u8TransferInProg = TRUE;
	Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 1);
	while(u8TransferInProg);

	// Bring reset low and then high
	XGpio_DiscreteSet(psGpio, 1, OLED_RES_MASK);
	MB_Sleep(1);
	XGpio_DiscreteClear(psGpio, 1, OLED_RES_MASK);
	MB_Sleep(1);
	XGpio_DiscreteSet(psGpio, 1, OLED_RES_MASK);

	// Send the set charge pump and set pre-charge period commands
	rgbWrBuf[0] = 0x8D;
	rgbWrBuf[1] = 0x14;
	rgbWrBuf[2] = OLED_SET_PRECHARGE_PER_CMD;
	rgbWrBuf[3] = 0xF1;
	u8TransferInProg = TRUE;
	Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 4);
	while(u8TransferInProg);

	// Turn on VCC and wait 100 ms
	XGpio_DiscreteClear(psGpio, 1, OLED_VBAT_MASK);
	MB_Sleep(100);

	// Set display contrast
	rgbWrBuf[0] = OLED_CONTRAST_CTRL_CMD;
	rgbWrBuf[1] = 0x0F;

	// Invert the display
	rgbWrBuf[2] = OLED_SET_SEGMENT_REMAP_CMD;
	rgbWrBuf[3] = OLED_SET_COM_DIR_CMD;

	// Select sequential COM configuration
	rgbWrBuf[4] = OLED_SET_COM_PINS_CMD;
	rgbWrBuf[5] = 0x00;
	rgbWrBuf[6] = 0xC0;
	rgbWrBuf[7] = 0x20;
	rgbWrBuf[8] = 0x00;

	// Turn display on
	rgbWrBuf[9]/*[6]*/ = OLED_DISPLAY_ON_CMD;

	u8TransferInProg = TRUE;
	Status = XSpi_Transfer(psSpi, rgbWrBuf, NULL, 10);
	while(u8TransferInProg);

	//Display Digilent logo
	fnOledPixelToDisplay(psSpi, psGpio, &sLogoPixmap);

	return Status;
}
/**
*
* This function does a minimal test on the Spi device and driver as a
* design example. The purpose of this function is to illustrate how to use
* the XSpi component using the polled mode.
*
* This function sends data and expects to receive the same data.
*
*
* @param	SpiInstancePtr is a pointer to the instance of Spi component.
* @param	SpiDeviceId is the Device ID of the Spi Device and is the
*		XPAR_<SPI_instance>_DEVICE_ID value from xparameters.h.
*
* @return	XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note
*
* This function contains an infinite loop such that if the Spi device is not
* working it may never return.
*
******************************************************************************/
int SpiPolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
{
	int Status;
	u32 Count;
	u8 Test;
	XSpi_Config *ConfigPtr;	/* Pointer to Configuration data */

	/*
	 * Initialize the SPI driver so that it is  ready to use.
	 */
	ConfigPtr = XSpi_LookupConfig(SpiDeviceId);
	if (ConfigPtr == NULL) {
		return XST_DEVICE_NOT_FOUND;
	}

	Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
				  ConfigPtr->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Perform a self-test to ensure that the hardware was built correctly.
	 */
	Status = XSpi_SelfTest(SpiInstancePtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Run loopback test only in case of standard SPI mode.
	 */
	if (SpiInstancePtr->SpiMode != XSP_STANDARD_MODE) {
		return XST_SUCCESS;
	}

	/*
	 * Set the Spi device as a master and in loopback mode.
	 */
	Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION |
 					XSP_LOOPBACK_OPTION);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}


	/*
	 * Start the SPI driver so that the device is enabled.
	 */
	XSpi_Start(SpiInstancePtr);

	/*
	 * Disable Global interrupt to use polled mode operation
	 */
	XSpi_IntrGlobalDisable(SpiInstancePtr);

	/*
	 * Initialize the write buffer with pattern to write, initialize the
	 * read buffer to zero so it can be verified after the read, the
	 * Test value that is added to the unique value allows the value to be
	 * changed in a debug environment.
	 */
	Test = 0x10;
	for (Count = 0; Count < BUFFER_SIZE; Count++) {
		WriteBuffer[Count] = (u8)(Count + Test);
		ReadBuffer[Count] = 0;
	}


	/*
	 * Transmit the data.
	 */
	XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer, BUFFER_SIZE);

	/*
	 * Compare the data received with the data that was transmitted.
	 */
	for (Count = 0; Count < BUFFER_SIZE; Count++) {
		if (WriteBuffer[Count] != ReadBuffer[Count]) {
			return XST_FAILURE;
		}
	}

	return XST_SUCCESS;
}