--- Quote Start ---
originally posted by dpiessens@Apr 29 2005, 04:50 PM
hello everyone,
i'm working on a project that utilizes the spi interface of the sopc builder. i'm having some trouble getting it to work though, mostly because i'm not sure if i'm writing the command correctly. does anyone have some sample code they can share on how to use the spi command to send and receive data?
thanks,
dan --- Quote End ---
This is my best guess...
This routine is based on the legacy SPI routine and does a single-character write to a 16-bit spi DAC. Or will when I get back from vacation.
The benefit of this routine over HAL's "alt_avalon_spi_command" is that it supports a 16-bit data word.
The downside is that it is too simple. Try not to break it.
http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif
#include <stdio.h># include <alt_types.h># include <io.h># include <string.h>
// Project-specific headers# include "system.h"
/* SPI status and control bits */# define STAT_TMT_MSK (0x20)# define STAT_TRDY_MSK (0x40)# define STAT_RRDY_MSK (0x80)# define CTRL_SSO_MSK (0x400)
/* From system.h - base address of DAC at U6.
* # define SPI_DAC_U6_BASE 0x00000820
*/
.
.
.
void spi_tx_char(int tx_data, long spi_DAC_addr)
{
/* Barebones SPI tx routine.
*
* Strategy:
* Write the slave select bit to pick an SS_n.
* (Writing all 3 as a unit, will change later.)
* Force SS_n
* Wait until ready then tx
* Wait until ready then rx and ignore data
* Wait until last bit is transferred
* Deassert SS_n
* Deselect slave
*
* Set your spi device for uC mode, not DSP since SSn drops before SCLK starts.
*
*/
int ctrlbyte;
/* select all 3 DACs */
IOWR(spi_DAC_addr, 5, 0x7);
/* Assert SS_n */
ctrlbyte = IORD(spi_DAC_addr, 3);
IOWR(spi_DAC_addr, 3, (ctrlbyte | CTRL_SSO_MSK));
/* Transmit a byte: */
while (!(IORD(spi_DAC_addr, 2) & STAT_TRDY_MSK));
IOWR(spi_DAC_addr, 1, tx_data);
/* Read and ignore the received data: */
while (!(IORD(spi_DAC_addr, 2) & STAT_RRDY_MSK));
IORD(spi_DAC_addr, 0);
/* Wait until the last byte is transmitted: */
while (!(IORD(spi_DAC_addr, 2) & STAT_TMT_MSK));
/* Release SS_n: */
IOWR(spi_DAC_addr, 3, IORD(spi_DAC_addr, 3) & ~CTRL_SSO_MSK );
/* Deselect all DACs: */
IOWR(spi_DAC_addr, 5, 0x0);
}