Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

C code question !! / NIOS II / SPI interface

Hello,

I have a question about implementing the spi interface in a FPGA, I already constructed a working CPU with a implementation of the SPI, and added the correct settings in SOIC, however i have a problem with my code for the niosII proccesor, for some reason it jumps right over my written code, it jumps straigt to the end of the main() function, I am as you would say a nOOb in programming... so i think i just made a stupid mistake in my code, but I cannot locate it...

Below states the code that i have written, for testing purpose's my idea was to send 0xFF a thousand times.... however when i go in debug mode it goes into int alt_avalon_spi_command(alt_u32 base, alt_u32 slave,......) and after that it jumps strait to the end without execucution the code under it.

The development board is a NIOS development board, Cyclone I edition...

I hope one of you could explain what i am doing wrong !! :confused:

Thanks a lot !! :)

greetings,

Remko

# include <stdio.h># include <system.h># include <altera_avalon_pio_regs.h>

//#include <altera_avalon_pwm_routines.h>

//#include <altera_avalon_uart_regs.h># include <string.h># include <sys/alt_alarm.h># include <alt_types.h># include <sys/time.h># include <fcntl.h>

//SPI headers# include <altera_avalon_spi_regs.h># include <altera_avalon_spi.h>

int delay_1();

int spi();

int main()

{

int alt_avalon_spi_command(alt_u32 base, alt_u32 slave,

alt_u32 write_length,

const alt_u8* wdata,

alt_u32 read_length,

alt_u8* read_data,

alt_u32 flags)

{

int i;

// force SS_N active

IOWR_ALTERA_AVALON_SPI_CONTROL(base, ALTERA_AVALON_SPI_CONTROL_SSO_MSK);

for (i = 0; i<10000 ; ++i)

{

// transmit a byte

while (!(IORD_ALTERA_AVALON_SPI_STATUS(base) & ALTERA_AVALON_SPI_STATUS_TRDY_MSK));

IOWR_ALTERA_AVALON_SPI_TXDATA(base, 0xFF);

// wait for all bytes to be send

while(!(IORD_ALTERA_AVALON_SPI_STATUS(base) & ALTERA_AVALON_SPI_STATUS_TMT_MSK));

}

//release SS_N

IOWR_ALTERA_AVALON_SPI_CONTROL(base, ~ALTERA_AVALON_SPI_CONTROL_SSO_MSK);

}

delay_1();

}

int delay_1()

{

int count = 0;

int delay;

while(1)

{

IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, count & 0x03);

IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_SEG_PIO_BASE, count & 0xff);

delay = 0;

while(delay < 200000)

{

delay++;

}

count++;

}

}

14 Replies